CVS commit: src/sys/sys

2015-08-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Aug 15 10:24:29 UTC 2015

Modified Files:
src/sys/sys: pax.h

Log Message:
Remove pax_adjust() (does not exist).


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/sys/pax.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/sys/pax.h
diff -u src/sys/sys/pax.h:1.14 src/sys/sys/pax.h:1.15
--- src/sys/sys/pax.h:1.14	Tue Aug  4 18:28:10 2015
+++ src/sys/sys/pax.h	Sat Aug 15 10:24:29 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: pax.h,v 1.14 2015/08/04 18:28:10 maxv Exp $ */
+/* $NetBSD: pax.h,v 1.15 2015/08/15 10:24:29 maxv Exp $ */
 
 /*-
  * Copyright (c) 2006 Elad Efrat e...@netbsd.org
@@ -51,8 +51,6 @@ struct vmspace;
 
 void pax_init(void);
 void pax_setup_elf_flags(struct lwp *, uint32_t);
-void pax_adjust(struct lwp *, uint32_t);
-
 void pax_mprotect(struct lwp *, vm_prot_t *, vm_prot_t *);
 int pax_segvguard(struct lwp *, struct vnode *, const char *, bool);
 



CVS commit: src/share/man/man9

2015-08-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Aug 15 10:31:41 UTC 2015

Modified Files:
src/share/man/man9: uvm_km.9

Log Message:
Mention UVM_KMF_EXEC.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/share/man/man9/uvm_km.9

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/share/man/man9/uvm_km.9
diff -u src/share/man/man9/uvm_km.9:1.4 src/share/man/man9/uvm_km.9:1.5
--- src/share/man/man9/uvm_km.9:1.4	Thu Jan  8 23:43:11 2015
+++ src/share/man/man9/uvm_km.9	Sat Aug 15 10:31:41 2015
@@ -1,4 +1,4 @@
-.\	$NetBSD: uvm_km.9,v 1.4 2015/01/08 23:43:11 riastradh Exp $
+.\	$NetBSD: uvm_km.9,v 1.5 2015/08/15 10:31:41 maxv Exp $
 .\
 .\ Copyright (c) 1998 Matthew R. Green
 .\ All rights reserved.
@@ -24,7 +24,7 @@
 .\ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\ SUCH DAMAGE.
 .\
-.Dd June 3, 2011
+.Dd August 15, 2015
 .Dt UVM_KM 9
 .Os
 .Sh NAME
@@ -91,6 +91,8 @@ Request zero-filled memory.
 Only supported for
 .Dv UVM_KMF_WIRED .
 Should not be used with other types.
+.It UVM_KMF_EXEC
+Request memory with executable rights.
 .It UVM_KMF_TRYLOCK
 Fail if cannot lock the map without sleeping.
 .It UVM_KMF_NOWAIT



CVS commit: src/sys/arch

2015-08-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Aug 12 07:53:58 UTC 2015

Modified Files:
src/sys/arch/acorn26/conf: GENERIC
src/sys/arch/acorn32/conf: EB7500ATX GENERIC INSTALL LOWMEM_WSCONS
src/sys/arch/amd64/conf: XEN3_DOMU
src/sys/arch/amigappc/conf: GENERIC NULL
src/sys/arch/arc/conf: ARCTIC MIMORI RPC44
src/sys/arch/cobalt/conf: INSTALL
src/sys/arch/evbarm/conf: ARMADAXP ARMADILLO9 BEAGLEBOARD BEAGLEBOARDXM
BEAGLEBONE HDL_G IGEPV2 INTEGRATOR IXDP425 MARVELL_NAS MINI2440
MV2120 N900 NITROGEN6X OMAP5EVM OPENBLOCKS_A6 OPENBLOCKS_AX3
SMDK2410 SMDK2800 ZAO425
src/sys/arch/evbarm64/conf: A64EMUL
src/sys/arch/evbmips/conf: CI20
src/sys/arch/ews4800mips/conf: GENERIC
src/sys/arch/hpcarm/conf: JORNADA720 JORNADA820 NETBOOKPRO WZERO3
src/sys/arch/i386/conf: XEN3_DOMU
src/sys/arch/mmeye/conf: GENERIC MMEYE_WLF
src/sys/arch/sun2/conf: FOURMEG VME
src/sys/arch/sun3/conf: GENERIC GENERIC3X
src/sys/arch/vax/conf: INSTALL
src/sys/arch/x68k/conf: INSTALL

Log Message:
Remove KMEMSTATS.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/arch/acorn26/conf/GENERIC
cvs rdiff -u -r1.57 -r1.58 src/sys/arch/acorn32/conf/EB7500ATX
cvs rdiff -u -r1.116 -r1.117 src/sys/arch/acorn32/conf/GENERIC
cvs rdiff -u -r1.71 -r1.72 src/sys/arch/acorn32/conf/INSTALL
cvs rdiff -u -r1.66 -r1.67 src/sys/arch/acorn32/conf/LOWMEM_WSCONS
cvs rdiff -u -r1.62 -r1.63 src/sys/arch/amd64/conf/XEN3_DOMU
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/amigappc/conf/GENERIC
cvs rdiff -u -r1.45 -r1.46 src/sys/arch/amigappc/conf/NULL
cvs rdiff -u -r1.67 -r1.68 src/sys/arch/arc/conf/ARCTIC
cvs rdiff -u -r1.68 -r1.69 src/sys/arch/arc/conf/MIMORI
cvs rdiff -u -r1.49 -r1.50 src/sys/arch/arc/conf/RPC44
cvs rdiff -u -r1.57 -r1.58 src/sys/arch/cobalt/conf/INSTALL
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/evbarm/conf/ARMADAXP \
src/sys/arch/evbarm/conf/OPENBLOCKS_AX3
cvs rdiff -u -r1.48 -r1.49 src/sys/arch/evbarm/conf/ARMADILLO9
cvs rdiff -u -r1.59 -r1.60 src/sys/arch/evbarm/conf/BEAGLEBOARD
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/evbarm/conf/BEAGLEBOARDXM
cvs rdiff -u -r1.32 -r1.33 src/sys/arch/evbarm/conf/BEAGLEBONE
cvs rdiff -u -r1.43 -r1.44 src/sys/arch/evbarm/conf/HDL_G
cvs rdiff -u -r1.26 -r1.27 src/sys/arch/evbarm/conf/IGEPV2
cvs rdiff -u -r1.79 -r1.80 src/sys/arch/evbarm/conf/INTEGRATOR
cvs rdiff -u -r1.49 -r1.50 src/sys/arch/evbarm/conf/IXDP425
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/evbarm/conf/MARVELL_NAS
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/evbarm/conf/MINI2440
cvs rdiff -u -r1.25 -r1.26 src/sys/arch/evbarm/conf/MV2120
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/evbarm/conf/N900
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/evbarm/conf/NITROGEN6X
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/evbarm/conf/OMAP5EVM
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/evbarm/conf/OPENBLOCKS_A6
cvs rdiff -u -r1.57 -r1.58 src/sys/arch/evbarm/conf/SMDK2410 \
src/sys/arch/evbarm/conf/ZAO425
cvs rdiff -u -r1.56 -r1.57 src/sys/arch/evbarm/conf/SMDK2800
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/evbarm64/conf/A64EMUL
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/evbmips/conf/CI20
cvs rdiff -u -r1.51 -r1.52 src/sys/arch/ews4800mips/conf/GENERIC
cvs rdiff -u -r1.96 -r1.97 src/sys/arch/hpcarm/conf/JORNADA720
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/hpcarm/conf/JORNADA820
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/hpcarm/conf/NETBOOKPRO
cvs rdiff -u -r1.38 -r1.39 src/sys/arch/hpcarm/conf/WZERO3
cvs rdiff -u -r1.68 -r1.69 src/sys/arch/i386/conf/XEN3_DOMU
cvs rdiff -u -r1.120 -r1.121 src/sys/arch/mmeye/conf/GENERIC
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/mmeye/conf/MMEYE_WLF
cvs rdiff -u -r1.58 -r1.59 src/sys/arch/sun2/conf/FOURMEG
cvs rdiff -u -r1.47 -r1.48 src/sys/arch/sun2/conf/VME
cvs rdiff -u -r1.171 -r1.172 src/sys/arch/sun3/conf/GENERIC
cvs rdiff -u -r1.125 -r1.126 src/sys/arch/sun3/conf/GENERIC3X
cvs rdiff -u -r1.70 -r1.71 src/sys/arch/vax/conf/INSTALL
cvs rdiff -u -r1.107 -r1.108 src/sys/arch/x68k/conf/INSTALL

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/acorn26/conf/GENERIC
diff -u src/sys/arch/acorn26/conf/GENERIC:1.81 src/sys/arch/acorn26/conf/GENERIC:1.82
--- src/sys/arch/acorn26/conf/GENERIC:1.81	Sun Nov 16 16:01:39 2014
+++ src/sys/arch/acorn26/conf/GENERIC	Wed Aug 12 07:53:56 2015
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.81 2014/11/16 16:01:39 manu Exp $
+# $NetBSD: GENERIC,v 1.82 2015/08/12 07:53:56 maxv Exp $
 #
 # GENERIC machine description file
 # 
@@ -121,7 +121,6 @@ options 	SYSVSHM		# System V-like shared
 # Miscellaneous kernel options
 options 	KTRACE		# system call tracing, a la ktrace(1)
 #options 	IRQSTATS	# manage IRQ statistics
-#options 	KMEMSTATS	# kernel memory statistics
 #options 	SCSIVERBOSE	# Verbose SCSI errors
 options 	NTP		# Kernel PLL for ntpd(8).
 options 	USERCONF	

CVS commit: src/sys

2015-07-24 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jul 24 13:02:52 UTC 2015

Modified Files:
src/sys/compat/common: kern_time_50.c vfs_syscalls_20.c
src/sys/compat/linux/common: linux_socket.c
src/sys/compat/linux32/common: linux32_socket.c
src/sys/compat/netbsd32: netbsd32_compat_50.c
src/sys/compat/ultrix: ultrix_fs.c
src/sys/kern: kern_ntptime.c kern_time.c kern_veriexec.c sys_lwp.c
vfs_syscalls.c
src/sys/miscfs/procfs: procfs_linux.c
src/sys/ufs/ffs: ffs_vfsops.c

Log Message:
Unused inits (harmless).

Found by Brainy.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/sys/compat/common/kern_time_50.c
cvs rdiff -u -r1.38 -r1.39 src/sys/compat/common/vfs_syscalls_20.c
cvs rdiff -u -r1.125 -r1.126 src/sys/compat/linux/common/linux_socket.c
cvs rdiff -u -r1.19 -r1.20 src/sys/compat/linux32/common/linux32_socket.c
cvs rdiff -u -r1.29 -r1.30 src/sys/compat/netbsd32/netbsd32_compat_50.c
cvs rdiff -u -r1.54 -r1.55 src/sys/compat/ultrix/ultrix_fs.c
cvs rdiff -u -r1.55 -r1.56 src/sys/kern/kern_ntptime.c
cvs rdiff -u -r1.179 -r1.180 src/sys/kern/kern_time.c
cvs rdiff -u -r1.8 -r1.9 src/sys/kern/kern_veriexec.c
cvs rdiff -u -r1.56 -r1.57 src/sys/kern/sys_lwp.c
cvs rdiff -u -r1.499 -r1.500 src/sys/kern/vfs_syscalls.c
cvs rdiff -u -r1.70 -r1.71 src/sys/miscfs/procfs/procfs_linux.c
cvs rdiff -u -r1.334 -r1.335 src/sys/ufs/ffs/ffs_vfsops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/common/kern_time_50.c
diff -u src/sys/compat/common/kern_time_50.c:1.28 src/sys/compat/common/kern_time_50.c:1.29
--- src/sys/compat/common/kern_time_50.c:1.28	Sun Nov  9 17:48:07 2014
+++ src/sys/compat/common/kern_time_50.c	Fri Jul 24 13:02:52 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_time_50.c,v 1.28 2014/11/09 17:48:07 maxv Exp $	*/
+/*	$NetBSD: kern_time_50.c,v 1.29 2015/07/24 13:02:52 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: kern_time_50.c,v 1.28 2014/11/09 17:48:07 maxv Exp $);
+__KERNEL_RCSID(0, $NetBSD: kern_time_50.c,v 1.29 2015/07/24 13:02:52 maxv Exp $);
 
 #ifdef _KERNEL_OPT
 #include opt_aio.h
@@ -123,7 +123,7 @@ compat_50_sys_clock_getres(struct lwp *l
 	} */
 	struct timespec50 ats50;
 	struct timespec ats;
-	int error = 0;
+	int error;
 
 	error = clock_getres1(SCARG(uap, clock_id), ats);
 	if (error != 0)

Index: src/sys/compat/common/vfs_syscalls_20.c
diff -u src/sys/compat/common/vfs_syscalls_20.c:1.38 src/sys/compat/common/vfs_syscalls_20.c:1.39
--- src/sys/compat/common/vfs_syscalls_20.c:1.38	Fri Sep  5 09:21:54 2014
+++ src/sys/compat/common/vfs_syscalls_20.c	Fri Jul 24 13:02:52 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: vfs_syscalls_20.c,v 1.38 2014/09/05 09:21:54 matt Exp $	*/
+/*	$NetBSD: vfs_syscalls_20.c,v 1.39 2015/07/24 13:02:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -37,7 +37,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: vfs_syscalls_20.c,v 1.38 2014/09/05 09:21:54 matt Exp $);
+__KERNEL_RCSID(0, $NetBSD: vfs_syscalls_20.c,v 1.39 2015/07/24 13:02:52 maxv Exp $);
 
 #ifdef _KERNEL_OPT
 #include opt_compat_netbsd.h
@@ -90,7 +90,7 @@ static int
 vfs2fs(struct statfs12 *bfs, const struct statvfs *fs)
 {
 	struct statfs12 ofs;
-	int i = 0;
+	int i;
 	ofs.f_type = 0;
 	ofs.f_oflags = (short)fs-f_flag;
 
@@ -139,7 +139,7 @@ compat_20_sys_statfs(struct lwp *l, cons
 	} */
 	struct mount *mp;
 	struct statvfs *sbuf;
-	int error = 0;
+	int error;
 	struct vnode *vp;
 
 	error = namei_simple_user(SCARG(uap, path),

Index: src/sys/compat/linux/common/linux_socket.c
diff -u src/sys/compat/linux/common/linux_socket.c:1.125 src/sys/compat/linux/common/linux_socket.c:1.126
--- src/sys/compat/linux/common/linux_socket.c:1.125	Sun May 24 17:07:26 2015
+++ src/sys/compat/linux/common/linux_socket.c	Fri Jul 24 13:02:52 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_socket.c,v 1.125 2015/05/24 17:07:26 rtr Exp $	*/
+/*	$NetBSD: linux_socket.c,v 1.126 2015/07/24 13:02:52 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
@@ -35,7 +35,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: linux_socket.c,v 1.125 2015/05/24 17:07:26 rtr Exp $);
+__KERNEL_RCSID(0, $NetBSD: linux_socket.c,v 1.126 2015/07/24 13:02:52 maxv Exp $);
 
 #if defined(_KERNEL_OPT)
 #include opt_inet.h
@@ -1115,7 +1115,7 @@ linux_getifconf(struct lwp *l, register_
 	struct ifaddr *ifa;
 	struct sockaddr *sa;
 	struct osockaddr *osa;
-	int space = 0, error = 0;
+	int space = 0, error;
 	const int sz = (int)sizeof(ifr);
 	bool docopy;
 

Index: src/sys/compat/linux32/common/linux32_socket.c
diff -u src/sys/compat/linux32/common/linux32_socket.c:1.19 src/sys/compat/linux32/common/linux32_socket.c:1.20
--- src/sys/compat/linux32/common/linux32_socket.c:1.19	Wed Nov 26 

CVS commit: src/sys

2015-07-27 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Jul 27 09:24:28 UTC 2015

Modified Files:
src/sys/kern: subr_kmem.c
src/sys/uvm: files.uvm
Removed Files:
src/sys/uvm: uvm_kmguard.c uvm_kmguard.h

Log Message:
Several changes and improvements in KMEM_GUARD:
 - merge uvm_kmguard.{c,h} into subr_kmem.c. It is only user there, and
   makes it more consistent. Also, it allows us to enable KMEM_GUARD
   without enabling DEBUG.
 - rename uvm_kmguard_XXX to kmem_guard_XXX, for consistency
 - improve kmem_guard_alloc() so that it supports allocations bigger than
   PAGE_SIZE
 - remove the canary value, and use directly the kmem header as underflow
   pattern.
 - fix some comments

(The UAF fifo is disabled for the moment; we actually need to register
the va and its size, and add a weight support not to consume too much
memory.)


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/sys/kern/subr_kmem.c
cvs rdiff -u -r1.24 -r1.25 src/sys/uvm/files.uvm
cvs rdiff -u -r1.11 -r0 src/sys/uvm/uvm_kmguard.c
cvs rdiff -u -r1.2 -r0 src/sys/uvm/uvm_kmguard.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/subr_kmem.c
diff -u src/sys/kern/subr_kmem.c:1.60 src/sys/kern/subr_kmem.c:1.61
--- src/sys/kern/subr_kmem.c:1.60	Tue Jul 22 07:38:41 2014
+++ src/sys/kern/subr_kmem.c	Mon Jul 27 09:24:28 2015
@@ -1,11 +1,11 @@
-/*	$NetBSD: subr_kmem.c,v 1.60 2014/07/22 07:38:41 maxv Exp $	*/
+/*	$NetBSD: subr_kmem.c,v 1.61 2015/07/27 09:24:28 maxv Exp $	*/
 
 /*-
- * Copyright (c) 2009 The NetBSD Foundation, Inc.
+ * Copyright (c) 2009-2015 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Andrew Doran.
+ * by Andrew Doran and Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -87,10 +87,10 @@
  *	Check the pattern on allocation.
  *
  * KMEM_GUARD
- *	A kernel with option DEBUG has kmguard debugging feature compiled
- *	in. See the comment in uvm/uvm_kmguard.c for what kind of bugs it tries
- *	to detect.  Even if compiled in, it's disabled by default because it's
- *	very expensive.  You can enable it on boot by:
+ *	A kernel with option DEBUG has kmem_guard debugging feature compiled
+ *	in. See the comment below for what kind of bugs it tries to detect. Even
+ *	if compiled in, it's disabled by default because it's very expensive.
+ *	You can enable it on boot by:
  *		boot -d
  *		db w kmem_guard_depth 0t3
  *		db c
@@ -100,7 +100,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: subr_kmem.c,v 1.60 2014/07/22 07:38:41 maxv Exp $);
+__KERNEL_RCSID(0, $NetBSD: subr_kmem.c,v 1.61 2015/07/27 09:24:28 maxv Exp $);
 
 #include sys/param.h
 #include sys/callback.h
@@ -112,7 +112,6 @@ __KERNEL_RCSID(0, $NetBSD: subr_kmem.c,
 
 #include uvm/uvm_extern.h
 #include uvm/uvm_map.h
-#include uvm/uvm_kmguard.h
 
 #include lib/libkern/libkern.h
 
@@ -182,8 +181,10 @@ static size_t kmem_cache_big_maxidx __re
 #endif /* defined(DIAGNOSTIC) */
 
 #if defined(DEBUG)  defined(_HARDKERNEL)
+#define	KMEM_SIZE
 #define	KMEM_POISON
 #define	KMEM_GUARD
+static void *kmem_freecheck;
 #endif /* defined(DEBUG) */
 
 #if defined(KMEM_POISON)
@@ -222,10 +223,20 @@ static void kmem_size_check(void *, size
 #ifndef KMEM_GUARD_DEPTH
 #define KMEM_GUARD_DEPTH 0
 #endif
+struct kmem_guard {
+	u_int		kg_depth;
+	intptr_t *	kg_fifo;
+	u_int		kg_rotor;
+	vmem_t *	kg_vmem;
+};
+
+static bool	kmem_guard_init(struct kmem_guard *, u_int, vmem_t *);
+static void *kmem_guard_alloc(struct kmem_guard *, size_t, bool);
+static void kmem_guard_free(struct kmem_guard *, size_t, void *);
+
 int kmem_guard_depth = KMEM_GUARD_DEPTH;
-size_t kmem_guard_size;
-static struct uvm_kmguard kmem_guard;
-static void *kmem_freecheck;
+static bool kmem_guard_enabled;
+static struct kmem_guard kmem_guard;
 #endif /* defined(KMEM_GUARD) */
 
 CTASSERT(KM_SLEEP == PR_WAITOK);
@@ -246,8 +257,8 @@ kmem_intr_alloc(size_t requested_size, k
 	KASSERT(requested_size  0);
 
 #ifdef KMEM_GUARD
-	if (requested_size = kmem_guard_size) {
-		return uvm_kmguard_alloc(kmem_guard, requested_size,
+	if (kmem_guard_enabled) {
+		return kmem_guard_alloc(kmem_guard, requested_size,
 		(kmflags  KM_SLEEP) != 0);
 	}
 #endif
@@ -324,8 +335,8 @@ kmem_intr_free(void *p, size_t requested
 	KASSERT(requested_size  0);
 
 #ifdef KMEM_GUARD
-	if (requested_size = kmem_guard_size) {
-		uvm_kmguard_free(kmem_guard, requested_size, p);
+	if (kmem_guard_enabled) {
+		kmem_guard_free(kmem_guard, requested_size, p);
 		return;
 	}
 #endif
@@ -372,7 +383,6 @@ kmem_intr_free(void *p, size_t requested
 void *
 kmem_alloc(size_t size, km_flag_t kmflags)
 {
-
 	KASSERTMSG((!cpu_intr_p()  !cpu_softintr_p()),
 	kmem(9) should not be used from the interrupt context

CVS commit: src/sys/dev/pci

2015-07-25 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jul 25 08:36:44 UTC 2015

Modified Files:
src/sys/dev/pci: if_ti.c

Log Message:
Memory leak. Same as r1.93. I don't know why Brainy didn't detect it
earlier; or perhaps I forgot to report it.

Found by Brainy.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/dev/pci/if_ti.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/dev/pci/if_ti.c
diff -u src/sys/dev/pci/if_ti.c:1.94 src/sys/dev/pci/if_ti.c:1.95
--- src/sys/dev/pci/if_ti.c:1.94	Mon Apr 27 17:41:26 2015
+++ src/sys/dev/pci/if_ti.c	Sat Jul 25 08:36:44 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: if_ti.c,v 1.94 2015/04/27 17:41:26 christos Exp $ */
+/* $NetBSD: if_ti.c,v 1.95 2015/07/25 08:36:44 maxv Exp $ */
 
 /*
  * Copyright (c) 1997, 1998, 1999
@@ -81,7 +81,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: if_ti.c,v 1.94 2015/04/27 17:41:26 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: if_ti.c,v 1.95 2015/07/25 08:36:44 maxv Exp $);
 
 #include opt_inet.h
 
@@ -800,6 +800,7 @@ ti_newbuf_mini(struct ti_softc *sc, int 
 BUS_DMA_READ|BUS_DMA_NOWAIT)) != 0) {
 			aprint_error_dev(sc-sc_dev, can't load recv map, error = %d\n,
 			   error);
+			m_freem(m_new);
 			return (ENOMEM);
 		}
 	} else {



CVS commit: src/sys/arch/evbarm/beagle

2015-07-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jul 22 14:10:45 UTC 2015

Modified Files:
src/sys/arch/evbarm/beagle: beagle_machdep.c

Log Message:
Double compiler branch.

Found by Brainy


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/sys/arch/evbarm/beagle/beagle_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/evbarm/beagle/beagle_machdep.c
diff -u src/sys/arch/evbarm/beagle/beagle_machdep.c:1.60 src/sys/arch/evbarm/beagle/beagle_machdep.c:1.61
--- src/sys/arch/evbarm/beagle/beagle_machdep.c:1.60	Mon Jul 21 22:17:44 2014
+++ src/sys/arch/evbarm/beagle/beagle_machdep.c	Wed Jul 22 14:10:45 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: beagle_machdep.c,v 1.60 2014/07/21 22:17:44 riz Exp $ */
+/*	$NetBSD: beagle_machdep.c,v 1.61 2015/07/22 14:10:45 maxv Exp $ */
 
 /*
  * Machine dependent functions for kernel setup for TI OSK5912 board.
@@ -125,7 +125,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: beagle_machdep.c,v 1.60 2014/07/21 22:17:44 riz Exp $);
+__KERNEL_RCSID(0, $NetBSD: beagle_machdep.c,v 1.61 2015/07/22 14:10:45 maxv Exp $);
 
 #include opt_machdep.h
 #include opt_ddb.h
@@ -697,7 +697,6 @@ beagle_reset(void)
 	*(volatile uint32_t *)(OMAP_L4_CORE_VBASE + (OMAP_L4_WAKEUP_BASE - OMAP_L4_CORE_BASE) + OMAP4_PRM_RSTCTRL) = OMAP4_PRM_RSTCTRL_WARM;
 #elif defined(OMAP_5XXX)
 	*(volatile uint32_t *)(OMAP_L4_CORE_VBASE + (OMAP_L4_WAKEUP_BASE - OMAP_L4_CORE_BASE) + OMAP5_PRM_RSTCTRL) = OMAP4_PRM_RSTCTRL_COLD;
-#elif defined(OMAP_5XXX)
 #elif defined(TI_AM335X)
 	*(volatile uint32_t *)(OMAP_L4_CORE_VBASE + (OMAP2_CM_BASE - OMAP_L4_CORE_BASE) + AM335X_PRCM_PRM_DEVICE + PRM_RSTCTRL) = RST_GLOBAL_WARM_SW;
 #else



CVS commit: src/sys/compat/netbsd32

2015-07-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jul 22 14:25:39 UTC 2015

Modified Files:
src/sys/compat/netbsd32: netbsd32_socket.c

Log Message:
Memory leak, triggerable from an unprivileged user.


To generate a diff of this commit:
cvs rdiff -u -r1.41 -r1.42 src/sys/compat/netbsd32/netbsd32_socket.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/netbsd32/netbsd32_socket.c
diff -u src/sys/compat/netbsd32/netbsd32_socket.c:1.41 src/sys/compat/netbsd32/netbsd32_socket.c:1.42
--- src/sys/compat/netbsd32/netbsd32_socket.c:1.41	Sat Aug 18 15:25:15 2012
+++ src/sys/compat/netbsd32/netbsd32_socket.c	Wed Jul 22 14:25:39 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_socket.c,v 1.41 2012/08/18 15:25:15 martin Exp $	*/
+/*	$NetBSD: netbsd32_socket.c,v 1.42 2015/07/22 14:25:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: netbsd32_socket.c,v 1.41 2012/08/18 15:25:15 martin Exp $);
+__KERNEL_RCSID(0, $NetBSD: netbsd32_socket.c,v 1.42 2015/07/22 14:25:39 maxv Exp $);
 
 #include sys/param.h
 #include sys/systm.h
@@ -331,7 +331,7 @@ netbsd32_sendmsg(struct lwp *l, const st
 	} */
 	struct msghdr msg;
 	struct netbsd32_msghdr msg32;
-	struct iovec aiov[UIO_SMALLIOV], *iov;
+	struct iovec aiov[UIO_SMALLIOV], *iov = aiov;
 	struct netbsd32_iovec *iov32;
 	size_t iovsz;
 	int error;
@@ -346,6 +346,7 @@ netbsd32_sendmsg(struct lwp *l, const st
 		error = copyin32_msg_control(l, msg);
 		if (error)
 			return (error);
+		/* From here on, msg.msg_control is allocated */
 	} else {
 		msg.msg_control = NULL;
 		msg.msg_controllen = 0;
@@ -353,23 +354,32 @@ netbsd32_sendmsg(struct lwp *l, const st
 
 	iovsz = msg.msg_iovlen * sizeof(struct iovec);
 	if ((u_int)msg.msg_iovlen  UIO_SMALLIOV) {
-		if ((u_int)msg.msg_iovlen  IOV_MAX)
-			return (EMSGSIZE);
+		if ((u_int)msg.msg_iovlen  IOV_MAX) {
+			error = EMSGSIZE;
+			goto out;
+		}
 		iov = kmem_alloc(iovsz, KM_SLEEP);
-	} else
-		iov = aiov;
+	}
 
 	iov32 = NETBSD32PTR64(msg32.msg_iov);
 	error = netbsd32_to_iovecin(iov32, iov, msg.msg_iovlen);
 	if (error)
-		goto done;
+		goto out;
 	msg.msg_iov = iov;
 
 	error = do_sys_sendmsg(l, SCARG(uap, s), msg, SCARG(uap, flags), retval);
-done:
+	/* msg.msg_control freed by do_sys_sendmsg() */
+
 	if (iov != aiov)
 		kmem_free(iov, iovsz);
 	return (error);
+
+out:
+	if (iov != aiov)
+		kmem_free(iov, iovsz);
+	if (msg.msg_control)
+		m_free(msg.msg_control);
+	return error;
 }
 
 int



CVS commit: src/sys/compat/netbsd32

2015-07-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jul 22 14:06:26 UTC 2015

Modified Files:
src/sys/compat/netbsd32: netbsd32_time.c

Log Message:
Set 'error' properly.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/compat/netbsd32/netbsd32_time.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/netbsd32/netbsd32_time.c
diff -u src/sys/compat/netbsd32/netbsd32_time.c:1.44 src/sys/compat/netbsd32/netbsd32_time.c:1.45
--- src/sys/compat/netbsd32/netbsd32_time.c:1.44	Fri Jul 17 06:42:48 2015
+++ src/sys/compat/netbsd32/netbsd32_time.c	Wed Jul 22 14:06:26 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_time.c,v 1.44 2015/07/17 06:42:48 skrll Exp $	*/
+/*	$NetBSD: netbsd32_time.c,v 1.45 2015/07/22 14:06:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: netbsd32_time.c,v 1.44 2015/07/17 06:42:48 skrll Exp $);
+__KERNEL_RCSID(0, $NetBSD: netbsd32_time.c,v 1.45 2015/07/22 14:06:26 maxv Exp $);
 
 #if defined(_KERNEL_OPT)
 #include opt_ntp.h
@@ -323,7 +323,7 @@ netbsd32___adjtime50(struct lwp *l, cons
 			atv.tv_usec += 100;
 			atv.tv_sec--;
 		}
-		(void) copyout(atv, SCARG_P32(uap, olddelta), sizeof(atv));
+		error = copyout(atv, SCARG_P32(uap, olddelta), sizeof(atv));
 		if (error)
 			return (error);
 	}



CVS commit: src/sys/kern

2015-07-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jul 22 14:18:08 UTC 2015

Modified Files:
src/sys/kern: uipc_syscalls.c

Log Message:
Memory leak. Triggerable from an unprivileged user via COMPAT_43.


To generate a diff of this commit:
cvs rdiff -u -r1.178 -r1.179 src/sys/kern/uipc_syscalls.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/uipc_syscalls.c
diff -u src/sys/kern/uipc_syscalls.c:1.178 src/sys/kern/uipc_syscalls.c:1.179
--- src/sys/kern/uipc_syscalls.c:1.178	Sat May  9 15:22:47 2015
+++ src/sys/kern/uipc_syscalls.c	Wed Jul 22 14:18:08 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_syscalls.c,v 1.178 2015/05/09 15:22:47 rtr Exp $	*/
+/*	$NetBSD: uipc_syscalls.c,v 1.179 2015/07/22 14:18:08 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: uipc_syscalls.c,v 1.178 2015/05/09 15:22:47 rtr Exp $);
+__KERNEL_RCSID(0, $NetBSD: uipc_syscalls.c,v 1.179 2015/07/22 14:18:08 maxv Exp $);
 
 #include opt_pipe.h
 
@@ -659,9 +659,16 @@ do_sys_sendmsg(struct lwp *l, int s, str
 	struct socket	*so;
 	file_t		*fp;
 
-	if ((error = fd_getsock1(s, so, fp)) != 0)
+	if ((error = fd_getsock1(s, so, fp)) != 0) {
+		/* We have to free msg_name and msg_control ourselves */
+		if (mp-msg_flags  MSG_NAMEMBUF)
+			m_freem(mp-msg_name);
+		if (mp-msg_flags  MSG_CONTROLMBUF)
+			m_freem(mp-msg_control);
 		return error;
+	}
 	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
+	/* msg_name and msg_control freed */
 	fd_putfile(s);
 	return error;
 }



CVS commit: src/sys/kern

2015-07-24 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jul 24 12:29:55 UTC 2015

Modified Files:
src/sys/kern: uipc_mbuf.c

Log Message:
typo (comment)


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/kern/uipc_mbuf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/uipc_mbuf.c
diff -u src/sys/kern/uipc_mbuf.c:1.161 src/sys/kern/uipc_mbuf.c:1.162
--- src/sys/kern/uipc_mbuf.c:1.161	Sun Feb  8 14:46:30 2015
+++ src/sys/kern/uipc_mbuf.c	Fri Jul 24 12:29:55 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: uipc_mbuf.c,v 1.161 2015/02/08 14:46:30 mlelstv Exp $	*/
+/*	$NetBSD: uipc_mbuf.c,v 1.162 2015/07/24 12:29:55 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2001 The NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: uipc_mbuf.c,v 1.161 2015/02/08 14:46:30 mlelstv Exp $);
+__KERNEL_RCSID(0, $NetBSD: uipc_mbuf.c,v 1.162 2015/07/24 12:29:55 maxv Exp $);
 
 #include opt_mbuftrace.h
 #include opt_nmbclusters.h
@@ -1689,7 +1689,7 @@ m_getptr(struct mbuf *m, int loc, int *o
 /*
  * m_ext_free: release a reference to the mbuf external storage.
  *
- * = free the mbuf m itsself as well.
+ * = free the mbuf m itself as well.
  */
 
 void



CVS commit: src/sys/arch/xen/xen

2015-10-25 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 25 07:51:16 UTC 2015

Modified Files:
src/sys/arch/xen/xen: xbd_xenbus.c

Log Message:
Uninitialized variable. Found by Brainy.

ok pgoyette@


To generate a diff of this commit:
cvs rdiff -u -r1.74 -r1.75 src/sys/arch/xen/xen/xbd_xenbus.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/xen/xen/xbd_xenbus.c
diff -u src/sys/arch/xen/xen/xbd_xenbus.c:1.74 src/sys/arch/xen/xen/xbd_xenbus.c:1.75
--- src/sys/arch/xen/xen/xbd_xenbus.c:1.74	Fri Aug 28 17:41:49 2015
+++ src/sys/arch/xen/xen/xbd_xenbus.c	Sun Oct 25 07:51:16 2015
@@ -1,4 +1,4 @@
-/*  $NetBSD: xbd_xenbus.c,v 1.74 2015/08/28 17:41:49 mlelstv Exp $  */
+/*  $NetBSD: xbd_xenbus.c,v 1.75 2015/10/25 07:51:16 maxv Exp $  */
 
 /*
  * Copyright (c) 2006 Manuel Bouyer.
@@ -50,7 +50,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: xbd_xenbus.c,v 1.74 2015/08/28 17:41:49 mlelstv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xbd_xenbus.c,v 1.75 2015/10/25 07:51:16 maxv Exp $");
 
 #include "opt_xen.h"
 
@@ -652,9 +652,9 @@ again:
 	for (i = sc->sc_ring.rsp_cons; i != resp_prod; i++) {
 		blkif_response_t *rep = RING_GET_RESPONSE(>sc_ring, i);
 		struct xbd_req *xbdreq = >sc_reqs[rep->id];
+		bp = xbdreq->req_bp;
 		DPRINTF(("xbd_handler(%p): b_bcount = %ld\n",
 		xbdreq->req_bp, (long)bp->b_bcount));
-		bp = xbdreq->req_bp;
 		if (rep->operation == BLKIF_OP_FLUSH_DISKCACHE) {
 			xbdreq->req_sync.s_error = rep->status;
 			xbdreq->req_sync.s_done = 1;



CVS commit: src/sys/ufs/ffs

2015-10-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Oct 22 11:31:31 UTC 2015

Modified Files:
src/sys/ufs/ffs: ffs_vfsops.c

Log Message:
Fix PR 50070. From hannken@.


To generate a diff of this commit:
cvs rdiff -u -r1.335 -r1.336 src/sys/ufs/ffs/ffs_vfsops.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/ufs/ffs/ffs_vfsops.c
diff -u src/sys/ufs/ffs/ffs_vfsops.c:1.335 src/sys/ufs/ffs/ffs_vfsops.c:1.336
--- src/sys/ufs/ffs/ffs_vfsops.c:1.335	Fri Jul 24 13:02:52 2015
+++ src/sys/ufs/ffs/ffs_vfsops.c	Thu Oct 22 11:31:31 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: ffs_vfsops.c,v 1.335 2015/07/24 13:02:52 maxv Exp $	*/
+/*	$NetBSD: ffs_vfsops.c,v 1.336 2015/10/22 11:31:31 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -61,7 +61,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.335 2015/07/24 13:02:52 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ffs_vfsops.c,v 1.336 2015/10/22 11:31:31 maxv Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_ffs.h"
@@ -899,7 +899,7 @@ static int
 ffs_superblock_validate(struct fs *fs)
 {
 	int32_t i, fs_bshift = 0, fs_fshift = 0, fs_fragshift = 0, fs_frag;
-	int32_t fs_inopb, fs_cgsize;
+	int32_t fs_inopb;
 
 	/* Check the superblock size */
 	if (fs->fs_sbsize > SBLOCKSIZE || fs->fs_sbsize < sizeof(struct fs))
@@ -981,23 +981,9 @@ ffs_superblock_validate(struct fs *fs)
 		return 0;
 
 	/* Check the size of cylinder groups */
-	fs_cgsize = ffs_fragroundup(fs, CGSIZE(fs));
-	if (fs->fs_cgsize != fs_cgsize) {
-		if (fs->fs_cgsize+1 == CGSIZE(fs)) {
-			printf("CGSIZE(fs) miscalculated by one - this file "
-			"system may have been created by\n"
-			"  an old (buggy) userland, see\n"
-			"  http://www.NetBSD.org/;
-			"docs/ffsv1badsuperblock.html\n");
-		} else {
-			printf("ERROR: cylinder group size mismatch: "
-			"fs_cgsize = 0x%zx, "
-			"fs->fs_cgsize = 0x%zx, CGSIZE(fs) = 0x%zx\n",
-			(size_t)fs_cgsize, (size_t)fs->fs_cgsize,
-			(size_t)CGSIZE(fs));
-			return 0;
-		}
-	}
+	if ((fs->fs_cgsize < sizeof(struct cg)) ||
+	(fs->fs_cgsize > fs->fs_bsize))
+		return 0;
 
 	return 1;
 }



CVS commit: src/sys/kern

2015-10-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Oct 22 11:38:51 UTC 2015

Modified Files:
src/sys/kern: exec_elf.c

Log Message:
Check the error code from es_setup_stack, and correctly free ep_emul_arg if
it fails.

That bug is harmless, since ep_setup_stack never fails.


To generate a diff of this commit:
cvs rdiff -u -r1.77 -r1.78 src/sys/kern/exec_elf.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/exec_elf.c
diff -u src/sys/kern/exec_elf.c:1.77 src/sys/kern/exec_elf.c:1.78
--- src/sys/kern/exec_elf.c:1.77	Sat Sep 26 16:12:24 2015
+++ src/sys/kern/exec_elf.c	Thu Oct 22 11:38:51 2015
@@ -1,11 +1,11 @@
-/*	$NetBSD: exec_elf.c,v 1.77 2015/09/26 16:12:24 maxv Exp $	*/
+/*	$NetBSD: exec_elf.c,v 1.78 2015/10/22 11:38:51 maxv Exp $	*/
 
 /*-
- * Copyright (c) 1994, 2000, 2005 The NetBSD Foundation, Inc.
+ * Copyright (c) 1994, 2000, 2005, 2015 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Christos Zoulas.
+ * by Christos Zoulas and Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.77 2015/09/26 16:12:24 maxv Exp $");
+__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.78 2015/10/22 11:38:51 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -801,6 +801,7 @@ exec_elf_makecmds(struct lwp *l, struct 
 		epp->ep_entryoffset = interp_offset;
 		epp->ep_entry = ap->arg_interp + interp_offset;
 		PNBUF_PUT(interp);
+		interp = NULL;
 	} else {
 		epp->ep_entry = eh->e_entry;
 		if (epp->ep_flags & EXEC_FORCEAUX) {
@@ -824,8 +825,13 @@ exec_elf_makecmds(struct lwp *l, struct 
 	NEW_VMCMD(>ep_vmcmds, vmcmd_map_readvn, PAGE_SIZE, 0,
 	epp->ep_vp, 0, VM_PROT_READ);
 #endif
+
+	error = (*epp->ep_esch->es_setup_stack)(l, epp);
+	if (error)
+		goto bad;
+
 	kmem_free(ph, phsize);
-	return (*epp->ep_esch->es_setup_stack)(l, epp);
+	return 0;
 
 bad:
 	if (interp)



CVS commit: src/sys/kern

2015-10-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Oct 22 11:48:02 UTC 2015

Modified Files:
src/sys/kern: kern_exec.c

Log Message:
Reset the PaX flags, make sure ep_emul_arg is NULL, and add a comment.


To generate a diff of this commit:
cvs rdiff -u -r1.420 -r1.421 src/sys/kern/kern_exec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_exec.c
diff -u src/sys/kern/kern_exec.c:1.420 src/sys/kern/kern_exec.c:1.421
--- src/sys/kern/kern_exec.c:1.420	Tue Oct 13 00:29:34 2015
+++ src/sys/kern/kern_exec.c	Thu Oct 22 11:48:02 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_exec.c,v 1.420 2015/10/13 00:29:34 pgoyette Exp $	*/
+/*	$NetBSD: kern_exec.c,v 1.421 2015/10/22 11:48:02 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -59,7 +59,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.420 2015/10/13 00:29:34 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_exec.c,v 1.421 2015/10/22 11:48:02 maxv Exp $");
 
 #include "opt_exec.h"
 #include "opt_execfmt.h"
@@ -450,6 +450,11 @@ check_exec(struct lwp *l, struct exec_pa
 			return 0;
 		}
 
+		/*
+		 * Reset all the fields that may have been modified by the
+		 * loader.
+		 */
+		KASSERT(epp->ep_emul_arg == NULL);
 		if (epp->ep_emul_root != NULL) {
 			vrele(epp->ep_emul_root);
 			epp->ep_emul_root = NULL;
@@ -458,6 +463,7 @@ check_exec(struct lwp *l, struct exec_pa
 			vrele(epp->ep_interp);
 			epp->ep_interp = NULL;
 		}
+		epp->ep_pax_flags = 0;
 
 		/* make sure the first "interesting" error code is saved. */
 		if (error == ENOEXEC)



CVS commit: src/sys/arch/amd64

2015-11-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov 14 14:01:23 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/amd64/conf: kern.ldscript

Log Message:
KNF, and fix some comments


To generate a diff of this commit:
cvs rdiff -u -r1.78 -r1.79 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.78 src/sys/arch/amd64/amd64/locore.S:1.79
--- src/sys/arch/amd64/amd64/locore.S:1.78	Sun Aug 30 01:46:02 2015
+++ src/sys/arch/amd64/amd64/locore.S	Sat Nov 14 14:01:23 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.78 2015/08/30 01:46:02 uebayasi Exp $	*/
+/*	$NetBSD: locore.S,v 1.79 2015/11/14 14:01:23 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -355,10 +355,9 @@ tmpstk:
 
 /*
  * Some hackage to deal with 64bit symbols in 32 bit mode.
- * This may not be needed it things are cleaned up a little.
+ * This may not be needed if things are cleaned up a little.
  */
 
-
 	.text
 	.globl	_C_LABEL(kernel_text)
 	.set	_C_LABEL(kernel_text),KERNTEXTOFF
@@ -782,7 +781,7 @@ longmode_hi:
 	/*
 	 * Xen info:
 	 * - %rsi -> start_info struct
-	 * - %rsp -> stack, *theorically* the last used page
+	 * - %rsp -> stack, *theoretically* the last used page
 	 *	by Xen bootstrap
 	 */
 	movq	%rsi, %rbx
@@ -811,7 +810,7 @@ longmode_hi:
 	 * - console
 	 * - Xen bootstrap page tables
 	 * - kernel stack. provided by Xen
-	 * - guaranted 512kB padding
+	 * - guaranteed 512kB padding
 	 *
 	 * As we want to rebuild our page tables and place our stack
 	 * in proc0 struct, all data starting from after console can be

Index: src/sys/arch/amd64/conf/kern.ldscript
diff -u src/sys/arch/amd64/conf/kern.ldscript:1.15 src/sys/arch/amd64/conf/kern.ldscript:1.16
--- src/sys/arch/amd64/conf/kern.ldscript:1.15	Tue Aug 25 08:07:24 2015
+++ src/sys/arch/amd64/conf/kern.ldscript	Sat Nov 14 14:01:23 2015
@@ -1,70 +1,73 @@
-/*	$NetBSD: kern.ldscript,v 1.15 2015/08/25 08:07:24 uebayasi Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.16 2015/11/14 14:01:23 maxv Exp $	*/
 
 #include "assym.h"
 
 ENTRY(_start)
 SECTIONS
 {
-  /* Read-only sections, merged into text segment: */
-  .text :
-  {
-*(.text)
-*(.text.*)
-*(.stub)
-  }
-  _etext = . ;
-  PROVIDE (etext = .) ;
-
-  .rodata :
-  {
-*(.rodata)
-*(.rodata.*)
-  }
-
-  /* Adjust the address for the data segment.  We want to adjust up to
- the same address within the page on the next page up.  */
-  . = ALIGN(0x10) + (. & (0x10 - 1));
-  __data_start = . ;
-  .data :
-  {
-*(.data)
-  }
-
-  . = ALIGN(COHERENCY_UNIT);
-  .data.cacheline_aligned :
-  {
-*(.data.cacheline_aligned)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.read_mostly :
-  {
-*(.data.read_mostly)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-
-  _edata = . ;
-  PROVIDE (edata = .) ;
-  __bss_start = . ;
-  .bss :
-  {
-*(.bss)
-*(.bss.*)
-*(COMMON)
-. = ALIGN(64 / 8);
-  }
-  . = ALIGN(64 / 8);
-  _end = . ;
-  PROVIDE (end = .) ;
-  .note.netbsd.ident :
-  {
-KEEP(*(.note.netbsd.ident));
-  }
+	/* Read-only sections, merged into text segment: */
+	.text :
+	{
+		*(.text)
+		*(.text.*)
+		*(.stub)
+	}
+	_etext = . ;
+	PROVIDE (etext = .) ;
+
+	.rodata :
+	{
+		*(.rodata)
+		*(.rodata.*)
+	}
+
+	/*
+	 * Adjust the address for the data segment.  We want to adjust up to
+	 * the same address within the page on the next page up.
+	 */
+	. = ALIGN(0x10) + (. & (0x10 - 1));
+	__data_start = . ;
+	.data :
+	{
+		*(.data)
+	}
+
+	. = ALIGN(COHERENCY_UNIT);
+	.data.cacheline_aligned :
+	{
+		*(.data.cacheline_aligned)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+	.data.read_mostly :
+	{
+		*(.data.read_mostly)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+
+	_edata = . ;
+	PROVIDE (edata = .) ;
+	__bss_start = . ;
+	.bss :
+	{
+		*(.bss)
+		*(.bss.*)
+		*(COMMON)
+		. = ALIGN(64 / 8);
+	}
+	. = ALIGN(64 / 8);
+	_end = . ;
+	PROVIDE (end = .) ;
+	.note.netbsd.ident :
+	{
+		KEEP(*(.note.netbsd.ident));
+	}
 }
+
 SECTIONS
 {
-  .text :
-  AT (ADDR(.text) & 0x0fff)
-  {
-*(.text)
-  } =0
+	.text :
+	AT (ADDR(.text) & 0x0fff)
+	{
+		*(.text)
+	} = 0
 }



CVS commit: src/sys/net

2015-10-20 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Oct 20 14:46:46 UTC 2015

Modified Files:
src/sys/net: if_bridge.c

Log Message:
Harmless alloc inconsistency; make sure the exact same argument is given to
kmem_alloc/kmem_free. Found by Brainy.


To generate a diff of this commit:
cvs rdiff -u -r1.103 -r1.104 src/sys/net/if_bridge.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/net/if_bridge.c
diff -u src/sys/net/if_bridge.c:1.103 src/sys/net/if_bridge.c:1.104
--- src/sys/net/if_bridge.c:1.103	Wed Oct  7 08:48:04 2015
+++ src/sys/net/if_bridge.c	Tue Oct 20 14:46:45 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_bridge.c,v 1.103 2015/10/07 08:48:04 ozaki-r Exp $	*/
+/*	$NetBSD: if_bridge.c,v 1.104 2015/10/20 14:46:45 maxv Exp $	*/
 
 /*
  * Copyright 2001 Wasabi Systems, Inc.
@@ -80,7 +80,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.103 2015/10/07 08:48:04 ozaki-r Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_bridge.c,v 1.104 2015/10/20 14:46:45 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_bridge_ipf.h"
@@ -2181,7 +2181,7 @@ retry:
 	count = sc->sc_brtcnt;
 	if (count == 0)
 		return;
-	brt_list = kmem_alloc(sizeof(struct bridge_rtnode *) * count, KM_SLEEP);
+	brt_list = kmem_alloc(sizeof(*brt_list) * count, KM_SLEEP);
 
 	BRIDGE_RT_LOCK(sc);
 	BRIDGE_RT_INTR_LOCK(sc);



CVS commit: src/sys/arch

2015-10-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 18 17:13:33 UTC 2015

Modified Files:
src/sys/arch/hppa/hppa: machdep.c
src/sys/arch/m68k/m68k: db_trace.c

Log Message:
Add some {} when the meaning is too ambiguous. From Brainy.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/hppa/hppa/machdep.c
cvs rdiff -u -r1.58 -r1.59 src/sys/arch/m68k/m68k/db_trace.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/hppa/hppa/machdep.c
diff -u src/sys/arch/hppa/hppa/machdep.c:1.5 src/sys/arch/hppa/hppa/machdep.c:1.6
--- src/sys/arch/hppa/hppa/machdep.c:1.5	Sat Jul 11 10:32:46 2015
+++ src/sys/arch/hppa/hppa/machdep.c	Sun Oct 18 17:13:33 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.5 2015/07/11 10:32:46 kamil Exp $	*/
+/*	$NetBSD: machdep.c,v 1.6 2015/10/18 17:13:33 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc.
@@ -58,7 +58,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.5 2015/07/11 10:32:46 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.6 2015/10/18 17:13:33 maxv Exp $");
 
 #include "opt_cputype.h"
 #include "opt_ddb.h"
@@ -981,10 +981,11 @@ delay(u_int us)
 		end = start + n * cpu_ticksnum / cpu_ticksdenom;
 
 		/* N.B. Interval Timer may wrap around */
-		if (end < start)
-			do
+		if (end < start) {
+			do {
 mfctl(CR_ITMR, start);
-			while (start > end);
+			} while (start > end);
+		}
 
 		do
 			mfctl(CR_ITMR, start);

Index: src/sys/arch/m68k/m68k/db_trace.c
diff -u src/sys/arch/m68k/m68k/db_trace.c:1.58 src/sys/arch/m68k/m68k/db_trace.c:1.59
--- src/sys/arch/m68k/m68k/db_trace.c:1.58	Thu Nov  7 01:49:46 2013
+++ src/sys/arch/m68k/m68k/db_trace.c	Sun Oct 18 17:13:32 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: db_trace.c,v 1.58 2013/11/07 01:49:46 christos Exp $	*/
+/*	$NetBSD: db_trace.c,v 1.59 2015/10/18 17:13:32 maxv Exp $	*/
 
 /* 
  * Mach Operating System
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.58 2013/11/07 01:49:46 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: db_trace.c,v 1.59 2015/10/18 17:13:32 maxv Exp $");
 
 #include 
 #include 
@@ -404,7 +404,7 @@ db_stack_trace_print(db_expr_t addr, boo
 		const char *cp = modif;
 		char c;
 
-		while ((c = *cp++) != 0)
+		while ((c = *cp++) != 0) {
 			if (c == 'a') {
 lwpaddr = true;
 trace_thread = true;
@@ -414,6 +414,7 @@ db_stack_trace_print(db_expr_t addr, boo
 			else if (c == 'u')
 kernel_only = false;
 #endif
+		}
 	}
 
 	if (!have_addr)



CVS commit: src/sys/compat

2015-10-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 18 16:59:19 UTC 2015

Modified Files:
src/sys/compat/linux/common: linux_exec_aout.c
src/sys/compat/sunos: sunos_exec_aout.c
src/sys/compat/sunos32: sunos32_exec_aout.c

Log Message:
Make sure we have space for the aout header.


To generate a diff of this commit:
cvs rdiff -u -r1.67 -r1.68 src/sys/compat/linux/common/linux_exec_aout.c
cvs rdiff -u -r1.18 -r1.19 src/sys/compat/sunos/sunos_exec_aout.c
cvs rdiff -u -r1.11 -r1.12 src/sys/compat/sunos32/sunos32_exec_aout.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/linux/common/linux_exec_aout.c
diff -u src/sys/compat/linux/common/linux_exec_aout.c:1.67 src/sys/compat/linux/common/linux_exec_aout.c:1.68
--- src/sys/compat/linux/common/linux_exec_aout.c:1.67	Sun Nov  9 17:48:08 2014
+++ src/sys/compat/linux/common/linux_exec_aout.c	Sun Oct 18 16:59:19 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_exec_aout.c,v 1.67 2014/11/09 17:48:08 maxv Exp $	*/
+/*	$NetBSD: linux_exec_aout.c,v 1.68 2015/10/18 16:59:19 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1995, 1998 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.67 2014/11/09 17:48:08 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_exec_aout.c,v 1.68 2015/10/18 16:59:19 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_execfmt.h"
@@ -135,10 +135,12 @@ exec_linux_aout_makecmds(struct lwp *l, 
 	int machtype, magic;
 	int error = ENOEXEC;
 
+	if (epp->ep_hdrvalid < sizeof(struct exec))
+		return ENOEXEC;
+
 	magic = LINUX_N_MAGIC(linux_ep);
 	machtype = LINUX_N_MACHTYPE(linux_ep);
 
-
 	if (machtype != LINUX_MID_MACHINE)
 		return (ENOEXEC);
 

Index: src/sys/compat/sunos/sunos_exec_aout.c
diff -u src/sys/compat/sunos/sunos_exec_aout.c:1.18 src/sys/compat/sunos/sunos_exec_aout.c:1.19
--- src/sys/compat/sunos/sunos_exec_aout.c:1.18	Sat Aug 15 23:39:35 2009
+++ src/sys/compat/sunos/sunos_exec_aout.c	Sun Oct 18 16:59:19 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: sunos_exec_aout.c,v 1.18 2009/08/15 23:39:35 matt Exp $	*/
+/*	$NetBSD: sunos_exec_aout.c,v 1.19 2015/10/18 16:59:19 maxv Exp $	*/
 
 /*
  * Copyright (c) 1993 Theo de Raadt
@@ -28,7 +28,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sunos_exec_aout.c,v 1.18 2009/08/15 23:39:35 matt Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunos_exec_aout.c,v 1.19 2015/10/18 16:59:19 maxv Exp $");
 
 #include 
 #include 
@@ -71,6 +71,8 @@ exec_sunos_aout_makecmds(struct lwp *l, 
 	struct sunos_exec *sunmag = epp->ep_hdr;
 	int error = ENOEXEC;
 
+	if (epp->ep_hdrvalid < sizeof(struct sunos_exec))
+		return ENOEXEC;
 	if (!SUNOS_M_NATIVE(sunmag->a_machtype))
 		return (ENOEXEC);
 

Index: src/sys/compat/sunos32/sunos32_exec_aout.c
diff -u src/sys/compat/sunos32/sunos32_exec_aout.c:1.11 src/sys/compat/sunos32/sunos32_exec_aout.c:1.12
--- src/sys/compat/sunos32/sunos32_exec_aout.c:1.11	Thu May 29 14:51:26 2008
+++ src/sys/compat/sunos32/sunos32_exec_aout.c	Sun Oct 18 16:59:19 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: sunos32_exec_aout.c,v 1.11 2008/05/29 14:51:26 mrg Exp $	*/
+/*	$NetBSD: sunos32_exec_aout.c,v 1.12 2015/10/18 16:59:19 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Matthew R. Green
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sunos32_exec_aout.c,v 1.11 2008/05/29 14:51:26 mrg Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunos32_exec_aout.c,v 1.12 2015/10/18 16:59:19 maxv Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_execfmt.h"
@@ -54,6 +54,8 @@ exec_sunos32_aout_makecmds(struct lwp *l
 	struct sunos_exec *sunmag = epp->ep_hdr;
 	int error = ENOEXEC;
 
+	if (epp->ep_hdrvalid < sizeof(struct sunos_exec))
+		return ENOEXEC;
 	if (!SUNOS_M_NATIVE(sunmag->a_machtype))
 		return (ENOEXEC);
 



CVS commit: src/sys/sys

2015-10-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct 10 10:51:15 UTC 2015

Modified Files:
src/sys/sys: exec.h

Log Message:
Remove the mach entry.


To generate a diff of this commit:
cvs rdiff -u -r1.148 -r1.149 src/sys/sys/exec.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/sys/exec.h
diff -u src/sys/sys/exec.h:1.148 src/sys/sys/exec.h:1.149
--- src/sys/sys/exec.h:1.148	Sun Dec 14 23:49:28 2014
+++ src/sys/sys/exec.h	Sat Oct 10 10:51:15 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec.h,v 1.148 2014/12/14 23:49:28 chs Exp $	*/
+/*	$NetBSD: exec.h,v 1.149 2015/10/10 10:51:15 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -154,7 +154,6 @@ struct execsw {
 		int (*elf_probe_func)(struct lwp *,
 			struct exec_package *, void *, char *, vaddr_t *);
 		int (*ecoff_probe_func)(struct lwp *, struct exec_package *);
-		int (*mach_probe_func)(const char **);
 	} u;
 	struct  emul *es_emul;		/* os emulation */
 	int	es_prio;		/* entry priority */



CVS commit: src/sys/kern

2015-08-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Aug 29 12:24:00 UTC 2015

Modified Files:
src/sys/kern: kern_cpu.c

Log Message:
Don't decrement the number of offline cpus if we fail to shut down one.

ok christos@, via tech-kern@


To generate a diff of this commit:
cvs rdiff -u -r1.70 -r1.71 src/sys/kern/kern_cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_cpu.c
diff -u src/sys/kern/kern_cpu.c:1.70 src/sys/kern/kern_cpu.c:1.71
--- src/sys/kern/kern_cpu.c:1.70	Thu Aug 20 09:45:45 2015
+++ src/sys/kern/kern_cpu.c	Sat Aug 29 12:24:00 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_cpu.c,v 1.70 2015/08/20 09:45:45 christos Exp $	*/
+/*	$NetBSD: kern_cpu.c,v 1.71 2015/08/29 12:24:00 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2007, 2008, 2009, 2010, 2012 The NetBSD Foundation, Inc.
@@ -56,7 +56,7 @@
  */
 
 #include sys/cdefs.h
-__KERNEL_RCSID(0, $NetBSD: kern_cpu.c,v 1.70 2015/08/20 09:45:45 christos Exp $);
+__KERNEL_RCSID(0, $NetBSD: kern_cpu.c,v 1.71 2015/08/29 12:24:00 maxv Exp $);
 
 #include opt_cpu_ucode.h
 #include opt_compat_netbsd.h
@@ -444,7 +444,6 @@ cpu_setstate(struct cpu_info *ci, bool o
 		if ((spc-spc_flags  SPCF_OFFLINE) == 0)
 			return 0;
 		func = (xcfunc_t)cpu_xc_online;
-		ncpuonline++;
 	} else {
 		if ((spc-spc_flags  SPCF_OFFLINE) != 0)
 			return 0;
@@ -463,16 +462,19 @@ cpu_setstate(struct cpu_info *ci, bool o
 		if (nonline == 1)
 			return EBUSY;
 		func = (xcfunc_t)cpu_xc_offline;
-		ncpuonline--;
 	}
 
 	where = xc_unicast(0, func, ci, NULL, ci);
 	xc_wait(where);
 	if (online) {
 		KASSERT((spc-spc_flags  SPCF_OFFLINE) == 0);
-	} else if ((spc-spc_flags  SPCF_OFFLINE) == 0) {
-		/* If was not set offline, then it is busy */
-		return EBUSY;
+		ncpuonline++;
+	} else {
+		if ((spc-spc_flags  SPCF_OFFLINE) == 0) {
+			/* If was not set offline, then it is busy */
+			return EBUSY;
+		}
+		ncpuonline--;
 	}
 
 	spc-spc_lastmod = time_second;



CVS commit: src/sys/arch

2015-09-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Sep 26 11:16:13 UTC 2015

Modified Files:
src/sys/arch/evbarm/conf: MMNET_GENERIC MPCSA_GENERIC OVERO PANDABOARD
SHEEVAPLUG TS7200 TWINTAIL
src/sys/arch/evbmips/conf: MALTA
src/sys/arch/hppa/conf: GENERIC
src/sys/arch/i386/conf: ALL GENERIC GENERIC_TINY INSTALL_FLOPPY
INSTALL_TINY XEN3_DOM0
src/sys/arch/iyonix/conf: GENERIC
src/sys/arch/ofppc/conf: GENERIC
src/sys/arch/prep/conf: GENERIC
src/sys/arch/sgimips/conf: GENERIC32_IP2x GENERIC32_IP3x
src/sys/arch/sparc/conf: GENERIC INSTALL KRUPS MRCOFFEE TADPOLE3GX
src/sys/arch/sparc64/conf: GENERIC NONPLUS64
src/sys/arch/sun2/conf: GENERIC
src/sys/arch/zaurus/conf: GENERIC

Log Message:
Remove KMEMSTATS. Normally it's ok now.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/evbarm/conf/MMNET_GENERIC
cvs rdiff -u -r1.43 -r1.44 src/sys/arch/evbarm/conf/MPCSA_GENERIC
cvs rdiff -u -r1.42 -r1.43 src/sys/arch/evbarm/conf/OVERO
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/evbarm/conf/PANDABOARD
cvs rdiff -u -r1.46 -r1.47 src/sys/arch/evbarm/conf/SHEEVAPLUG
cvs rdiff -u -r1.62 -r1.63 src/sys/arch/evbarm/conf/TS7200
cvs rdiff -u -r1.65 -r1.66 src/sys/arch/evbarm/conf/TWINTAIL
cvs rdiff -u -r1.81 -r1.82 src/sys/arch/evbmips/conf/MALTA
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/hppa/conf/GENERIC
cvs rdiff -u -r1.394 -r1.395 src/sys/arch/i386/conf/ALL
cvs rdiff -u -r1.1127 -r1.1128 src/sys/arch/i386/conf/GENERIC
cvs rdiff -u -r1.141 -r1.142 src/sys/arch/i386/conf/GENERIC_TINY
cvs rdiff -u -r1.28 -r1.29 src/sys/arch/i386/conf/INSTALL_FLOPPY
cvs rdiff -u -r1.143 -r1.144 src/sys/arch/i386/conf/INSTALL_TINY
cvs rdiff -u -r1.96 -r1.97 src/sys/arch/i386/conf/XEN3_DOM0
cvs rdiff -u -r1.88 -r1.89 src/sys/arch/iyonix/conf/GENERIC
cvs rdiff -u -r1.157 -r1.158 src/sys/arch/ofppc/conf/GENERIC
cvs rdiff -u -r1.174 -r1.175 src/sys/arch/prep/conf/GENERIC
cvs rdiff -u -r1.104 -r1.105 src/sys/arch/sgimips/conf/GENERIC32_IP2x
cvs rdiff -u -r1.106 -r1.107 src/sys/arch/sgimips/conf/GENERIC32_IP3x
cvs rdiff -u -r1.248 -r1.249 src/sys/arch/sparc/conf/GENERIC
cvs rdiff -u -r1.90 -r1.91 src/sys/arch/sparc/conf/INSTALL
cvs rdiff -u -r1.67 -r1.68 src/sys/arch/sparc/conf/KRUPS
cvs rdiff -u -r1.47 -r1.48 src/sys/arch/sparc/conf/MRCOFFEE
cvs rdiff -u -r1.66 -r1.67 src/sys/arch/sparc/conf/TADPOLE3GX
cvs rdiff -u -r1.181 -r1.182 src/sys/arch/sparc64/conf/GENERIC
cvs rdiff -u -r1.45 -r1.46 src/sys/arch/sparc64/conf/NONPLUS64
cvs rdiff -u -r1.94 -r1.95 src/sys/arch/sun2/conf/GENERIC
cvs rdiff -u -r1.66 -r1.67 src/sys/arch/zaurus/conf/GENERIC

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/evbarm/conf/MMNET_GENERIC
diff -u src/sys/arch/evbarm/conf/MMNET_GENERIC:1.20 src/sys/arch/evbarm/conf/MMNET_GENERIC:1.21
--- src/sys/arch/evbarm/conf/MMNET_GENERIC:1.20	Sat Aug 23 20:26:57 2014
+++ src/sys/arch/evbarm/conf/MMNET_GENERIC	Sat Sep 26 11:16:12 2015
@@ -1,4 +1,4 @@
-# $NetBSD: MMNET_GENERIC,v 1.20 2014/08/23 20:26:57 dholland Exp $
+# $NetBSD: MMNET_GENERIC,v 1.21 2015/09/26 11:16:12 maxv Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include		"arch/evbarm/conf/std.mmnet"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident 		"GENERIC-$Revision: 1.20 $"
+#ident 		"GENERIC-$Revision: 1.21 $"
 
 maxusers	32		# estimated number of users
 
@@ -80,7 +80,6 @@ options 	SYSCTL_INCLUDE_DESCR	# Include 
 # Diagnostic/debugging support options
 options 	DIAGNOSTIC	# expensive kernel consistency checks
 options 	DEBUG		# expensive debugging checks/support
-options 	KMEMSTATS	# kernel memory statistics (vmstat -m)
 options 	DDB		# in-kernel debugger
 options 	DDB_ONPANIC=1	# see also sysctl(7): `ddb.onpanic'
 options 	DDB_HISTORY_SIZE=512	# enable history editing in DDB

Index: src/sys/arch/evbarm/conf/MPCSA_GENERIC
diff -u src/sys/arch/evbarm/conf/MPCSA_GENERIC:1.43 src/sys/arch/evbarm/conf/MPCSA_GENERIC:1.44
--- src/sys/arch/evbarm/conf/MPCSA_GENERIC:1.43	Sat Aug 23 20:26:57 2014
+++ src/sys/arch/evbarm/conf/MPCSA_GENERIC	Sat Sep 26 11:16:12 2015
@@ -1,4 +1,4 @@
-# $NetBSD: MPCSA_GENERIC,v 1.43 2014/08/23 20:26:57 dholland Exp $
+# $NetBSD: MPCSA_GENERIC,v 1.44 2015/09/26 11:16:12 maxv Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include		"arch/evbarm/conf/std.mpcsa"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident 		"GENERIC-$Revision: 1.43 $"
+#ident 		"GENERIC-$Revision: 1.44 $"
 
 maxusers	32		# estimated number of users
 
@@ -80,7 +80,6 @@ options 	SYSCTL_INCLUDE_DESCR	# Include 
 # Diagnostic/debugging support options
 options 	DIAGNOSTIC	# expensive kernel consistency checks
 options 	DEBUG		# expensive debugging checks/support
-options 	KMEMSTATS	# kernel memory statistics (vmstat -m)
 options 	DDB		# in-kernel debugger
 

CVS commit: src/sys/fs/efs

2015-09-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Sep 26 12:16:28 UTC 2015

Modified Files:
src/sys/fs/efs: efs_subr.c

Log Message:
Curious typo. Harmless. Found by Brainy


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/fs/efs/efs_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/fs/efs/efs_subr.c
diff -u src/sys/fs/efs/efs_subr.c:1.11 src/sys/fs/efs/efs_subr.c:1.12
--- src/sys/fs/efs/efs_subr.c:1.11	Sat Mar 28 19:24:05 2015
+++ src/sys/fs/efs/efs_subr.c	Sat Sep 26 12:16:28 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: efs_subr.c,v 1.11 2015/03/28 19:24:05 maxv Exp $	*/
+/*	$NetBSD: efs_subr.c,v 1.12 2015/09/26 12:16:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 2006 Stephen M. Rumble 
@@ -17,7 +17,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: efs_subr.c,v 1.11 2015/03/28 19:24:05 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: efs_subr.c,v 1.12 2015/09/26 12:16:28 maxv Exp $");
 
 #include 
 #include 
@@ -136,7 +136,7 @@ efs_locate_inode(ino_t ino, struct efs_s
 
 	cgisize = be16toh(sbp->sb_cgisize);
 	cgfsize = be32toh(sbp->sb_cgfsize);
-	firstcg = be32toh(sbp->sb_firstcg),
+	firstcg = be32toh(sbp->sb_firstcg);
 
 	*bboff = firstcg + ((ino / (cgisize * EFS_DINODES_PER_BB)) * cgfsize) +
 	((ino % (cgisize * EFS_DINODES_PER_BB)) / EFS_DINODES_PER_BB);



CVS commit: src/sys

2015-09-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Sep 26 16:12:24 UTC 2015

Modified Files:
src/sys/kern: exec_elf.c exec_subr.c kern_exec.c kern_pax.c
src/sys/sys: pax.h

Log Message:
Revamp the way processes are PaX'ed in the kernel. Sent on tech-kern@ two
months ago, but no one reviewed it - probably because it's not a trivial
change.

This change fixes the following bug: when loading a PaX'ed binary, the
kernel updates the PaX flag of the calling process before it makes sure
the new process is actually launched. If the kernel fails to launch the
new process, it does not restore the PaX flag of the calling process,
leaving it in an inconsistent state.

Actually, simply restoring it would be horrible as well, since in the
meantime another thread may have used the flag.

The solution is therefore: modify all the functions used by PaX so that
they take as argument the exec package instead of the lwp, and set the PaX
flag in the process *right before* launching the new process - it cannot
fail in the meantime.


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/sys/kern/exec_elf.c
cvs rdiff -u -r1.71 -r1.72 src/sys/kern/exec_subr.c
cvs rdiff -u -r1.416 -r1.417 src/sys/kern/kern_exec.c
cvs rdiff -u -r1.31 -r1.32 src/sys/kern/kern_pax.c
cvs rdiff -u -r1.15 -r1.16 src/sys/sys/pax.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/exec_elf.c
diff -u src/sys/kern/exec_elf.c:1.76 src/sys/kern/exec_elf.c:1.77
--- src/sys/kern/exec_elf.c:1.76	Sat Aug  8 06:24:40 2015
+++ src/sys/kern/exec_elf.c	Sat Sep 26 16:12:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_elf.c,v 1.76 2015/08/08 06:24:40 maxv Exp $	*/
+/*	$NetBSD: exec_elf.c,v 1.77 2015/09/26 16:12:24 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1994, 2000, 2005 The NetBSD Foundation, Inc.
@@ -57,7 +57,7 @@
  */
 
 #include 
-__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.76 2015/08/08 06:24:40 maxv Exp $");
+__KERNEL_RCSID(1, "$NetBSD: exec_elf.c,v 1.77 2015/09/26 16:12:24 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_pax.h"
@@ -116,8 +116,7 @@ static void	elf_free_emul_arg(void *);
 #define	ELF_TRUNC(a, b)		((a) & ~((b) - 1))
 
 static void
-elf_placedynexec(struct lwp *l, struct exec_package *epp, Elf_Ehdr *eh,
-Elf_Phdr *ph)
+elf_placedynexec(struct exec_package *epp, Elf_Ehdr *eh, Elf_Phdr *ph)
 {
 	Elf_Addr align, offset;
 	int i;
@@ -127,7 +126,7 @@ elf_placedynexec(struct lwp *l, struct e
 			align = ph[i].p_align;
 
 #ifdef PAX_ASLR
-	if (pax_aslr_active(l)) {
+	if (pax_aslr_epp_active(epp)) {
 		size_t pax_align, l2, delta;
 		uint32_t r;
 
@@ -711,12 +710,8 @@ exec_elf_makecmds(struct lwp *l, struct 
 		pos = (Elf_Addr)startp;
 	}
 
-#if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
-	pax_setup_elf_flags(l, epp->ep_pax_flags);
-#endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
-
 	if (is_dyn)
-		elf_placedynexec(l, epp, eh, ph);
+		elf_placedynexec(epp, eh, ph);
 
 	/*
 	 * Load all the necessary sections
@@ -941,8 +936,15 @@ netbsd_elf_signature(struct lwp *l, stru
 			np->n_descsz == ELF_NOTE_PAX_DESCSZ &&
 			memcmp(ndata, ELF_NOTE_PAX_NAME,
 			ELF_NOTE_PAX_NAMESZ) == 0) {
-memcpy(>ep_pax_flags, ndesc,
-sizeof(epp->ep_pax_flags));
+uint32_t flags;
+memcpy(, ndesc, sizeof(flags));
+#if defined(PAX_MPROTECT) || defined(PAX_SEGVGUARD) || defined(PAX_ASLR)
+/* Convert the flags and insert them into
+ * the exec package. */
+pax_setup_elf_flags(epp, flags);
+#else
+(void)flags; /* UNUSED */
+#endif /* PAX_MPROTECT || PAX_SEGVGUARD || PAX_ASLR */
 break;
 			}
 			BADNOTE("PaX tag");

Index: src/sys/kern/exec_subr.c
diff -u src/sys/kern/exec_subr.c:1.71 src/sys/kern/exec_subr.c:1.72
--- src/sys/kern/exec_subr.c:1.71	Sat Mar 29 09:31:11 2014
+++ src/sys/kern/exec_subr.c	Sat Sep 26 16:12:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec_subr.c,v 1.71 2014/03/29 09:31:11 maxv Exp $	*/
+/*	$NetBSD: exec_subr.c,v 1.72 2015/09/26 16:12:24 maxv Exp $	*/
 
 /*
  * Copyright (c) 1993, 1994, 1996 Christopher G. Demetriou
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.71 2014/03/29 09:31:11 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: exec_subr.c,v 1.72 2015/09/26 16:12:24 maxv Exp $");
 
 #include "opt_pax.h"
 
@@ -408,7 +408,7 @@ exec_setup_stack(struct lwp *l, struct e
 	max_stack_size);
 
 #ifdef PAX_ASLR
-	pax_aslr_stack(l, epp, _stack_size);
+	pax_aslr_stack(epp, _stack_size);
 #endif /* PAX_ASLR */
 
 	l->l_proc->p_stackbase = epp->ep_minsaddr;

Index: src/sys/kern/kern_exec.c
diff -u src/sys/kern/kern_exec.c:1.416 src/sys/kern/kern_exec.c:1.417
--- src/sys/kern/kern_exec.c:1.416	Sat Sep 12 18:30:46 2015
+++ src/sys/kern/kern_exec.c	Sat Sep 26 16:12:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_exec.c,v 1.416 2015/09/12 18:30:46 christos Exp $	*/
+/*	$NetBSD: kern_exec.c,v 1.417 2015/09/26 16:12:24 maxv Exp $	*/
 
 /*-
 

CVS commit: src/sys/arch

2015-09-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Sep 26 16:33:16 UTC 2015

Modified Files:
src/sys/arch/alpha/conf: GENERIC
src/sys/arch/amd64/conf: ALL
src/sys/arch/evbarm64/conf: A64EMUL
src/sys/arch/i386/conf: ALL
src/sys/arch/shark/conf: GENERIC

Log Message:
Disable PAX_SEGVGUARD.

We actually have a big problem: the fileassocs are never deleted.
Therefore, if a user generates a lot of buggy binaries and launches them
all, the kernel will allocate memory again again and again for all these
entries and will never free them (unless the files are deleted from the
disk). Which means that a user can too easily put the kernel under memory
pressure.


To generate a diff of this commit:
cvs rdiff -u -r1.365 -r1.366 src/sys/arch/alpha/conf/GENERIC
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/evbarm64/conf/A64EMUL
cvs rdiff -u -r1.395 -r1.396 src/sys/arch/i386/conf/ALL
cvs rdiff -u -r1.122 -r1.123 src/sys/arch/shark/conf/GENERIC

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/alpha/conf/GENERIC
diff -u src/sys/arch/alpha/conf/GENERIC:1.365 src/sys/arch/alpha/conf/GENERIC:1.366
--- src/sys/arch/alpha/conf/GENERIC:1.365	Sat Aug  8 06:36:24 2015
+++ src/sys/arch/alpha/conf/GENERIC	Sat Sep 26 16:33:16 2015
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.365 2015/08/08 06:36:24 maxv Exp $
+# $NetBSD: GENERIC,v 1.366 2015/09/26 16:33:16 maxv Exp $
 #
 # This machine description file is used to generate the default NetBSD
 # kernel.
@@ -19,7 +19,7 @@ include 	"arch/alpha/conf/std.alpha"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-ident		"GENERIC-$Revision: 1.365 $"
+ident		"GENERIC-$Revision: 1.366 $"
 
 maxusers 32
 
@@ -789,5 +789,4 @@ pseudo-device	putter			# for puffs and p
 #options 	VERIFIED_EXEC_FP_MD5
 
 options PAX_MPROTECT=0			# PaX mprotect(2) restrictions
-#options PAX_SEGVGUARD=0			# PaX Segmentation fault guard
 options PAX_ASLR=0			# PaX Address Space Layout Randomization

Index: src/sys/arch/amd64/conf/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.30 src/sys/arch/amd64/conf/ALL:1.31
--- src/sys/arch/amd64/conf/ALL:1.30	Sat Aug  8 06:36:24 2015
+++ src/sys/arch/amd64/conf/ALL	Sat Sep 26 16:33:16 2015
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.30 2015/08/08 06:36:24 maxv Exp $
+# $NetBSD: ALL,v 1.31 2015/09/26 16:33:16 maxv Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident 		"ALL-$Revision: 1.30 $"
+#ident 		"ALL-$Revision: 1.31 $"
 
 maxusers	64		# estimated number of users
 
@@ -1633,7 +1633,7 @@ options 	VERIFIED_EXEC_FP_MD5
 
 options 	PAX_MPROTECT=0		# PaX mprotect(2) restrictions
 options 	PAX_ASLR=0		# PaX Address Space Layout Randomization
-options 	PAX_SEGVGUARD=0		# PaX Segmentation fault guard
+#options 	PAX_SEGVGUARD=0		# PaX Segmentation fault guard
 
 #
 # NetBSD: GENERIC_ISDN,v 1.16 2010/01/03 03:53:34 dholland Exp

Index: src/sys/arch/evbarm64/conf/A64EMUL
diff -u src/sys/arch/evbarm64/conf/A64EMUL:1.4 src/sys/arch/evbarm64/conf/A64EMUL:1.5
--- src/sys/arch/evbarm64/conf/A64EMUL:1.4	Wed Aug 12 07:53:57 2015
+++ src/sys/arch/evbarm64/conf/A64EMUL	Sat Sep 26 16:33:16 2015
@@ -1,4 +1,4 @@
-# $NetBSD: A64EMUL,v 1.4 2015/08/12 07:53:57 maxv Exp $
+# $NetBSD: A64EMUL,v 1.5 2015/09/26 16:33:16 maxv Exp $
 #
 # This machine description file is used to generate the default NetBSD
 # kernel.
@@ -19,7 +19,7 @@ include 	"arch/evbarm64/conf/std.a64emul
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-ident		"A64EMUL-$Revision: 1.4 $"
+ident		"A64EMUL-$Revision: 1.5 $"
 
 maxusers 32
 
@@ -267,5 +267,4 @@ pseudo-device	putter			# for puffs and p
 #options 	VERIFIED_EXEC_FP_MD5
 
 #options PAX_MPROTECT=0			# PaX mprotect(2) restrictions
-#options PAX_SEGVGUARD=0			# PaX Segmentation fault guard
 #options PAX_ASLR=0			# PaX Address Space Layout Randomization

Index: src/sys/arch/i386/conf/ALL
diff -u src/sys/arch/i386/conf/ALL:1.395 src/sys/arch/i386/conf/ALL:1.396
--- src/sys/arch/i386/conf/ALL:1.395	Sat Sep 26 11:16:12 2015
+++ src/sys/arch/i386/conf/ALL	Sat Sep 26 16:33:16 2015
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.395 2015/09/26 11:16:12 maxv Exp $
+# $NetBSD: ALL,v 1.396 2015/09/26 16:33:16 maxv Exp $
 # From NetBSD: GENERIC,v 1.787 2006/10/01 18:37:54 bouyer Exp
 #
 # ALL machine description file
@@ -17,7 +17,7 @@ include 	"arch/i386/conf/std.i386"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident 		"ALL-$Revision: 1.395 $"
+#ident 		"ALL-$Revision: 1.396 $"
 
 maxusers	64		# estimated number of users
 
@@ -1823,7 +1823,7 @@ options 	VERIFIED_EXEC_FP_MD5
 
 options 	PAX_MPROTECT=0		# PaX mprotect(2) restrictions
 options 	PAX_ASLR=0		# PaX Address Space 

CVS commit: src/sys/kern

2015-12-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Dec  9 16:26:16 UTC 2015

Modified Files:
src/sys/kern: kern_ksyms.c kern_module.c

Log Message:
KNF


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/kern/kern_ksyms.c
cvs rdiff -u -r1.108 -r1.109 src/sys/kern/kern_module.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_ksyms.c
diff -u src/sys/kern/kern_ksyms.c:1.81 src/sys/kern/kern_ksyms.c:1.82
--- src/sys/kern/kern_ksyms.c:1.81	Sun Aug 30 01:46:02 2015
+++ src/sys/kern/kern_ksyms.c	Wed Dec  9 16:26:16 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_ksyms.c,v 1.81 2015/08/30 01:46:02 uebayasi Exp $	*/
+/*	$NetBSD: kern_ksyms.c,v 1.82 2015/12/09 16:26:16 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -73,7 +73,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.81 2015/08/30 01:46:02 uebayasi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_ksyms.c,v 1.82 2015/12/09 16:26:16 maxv Exp $");
 
 #if defined(_KERNEL) && defined(_KERNEL_OPT)
 #include "opt_copy_symtab.h"
@@ -336,7 +336,7 @@ addsymtab(const char *name, void *symsta
 	nglob = 0;
 	for (i = n = 0; i < nsyms; i++) {
 
-		/* This breaks CTF mapping, so don't do it when
+		/* This breaks CTF mapping, so don't do it when
 		 * DTrace is enabled
 		 */
 #ifndef KDTRACE_HOOKS
@@ -401,7 +401,7 @@ addsymtab(const char *name, void *symsta
 		panic("addsymtab");
 
 #ifdef KDTRACE_HOOKS
-	/* 
+	/*
 	 * Build the mapping from original symbol id to new symbol table.
 	 * Deleted symbols will have a zero map, indices will be one based
 	 * instead of zero based.
@@ -493,7 +493,7 @@ ksyms_addsyms_elf(int symsize, void *sta
 		shdr[ehdr->e_shstrndx].sh_offset;
 		for (i = 1; i < ehdr->e_shnum; i++) {
 #ifdef DEBUG
-			printf("ksyms: checking %s\n", [shdr[i].sh_name]);
+			printf("ksyms: checking %s\n", [shdr[i].sh_name]);
 #endif
 			if (shdr[i].sh_type != SHT_PROGBITS)
 continue;
@@ -511,7 +511,7 @@ ksyms_addsyms_elf(int symsize, void *sta
 		}
 #ifdef DEBUG
 	} else {
-		printf("ksyms: e_shstrndx == 0\n");
+		printf("ksyms: e_shstrndx == 0\n");
 #endif
 	}
 #endif
@@ -539,9 +539,8 @@ ksyms_addsyms_elf(int symsize, void *sta
  */
 void
 ksyms_addsyms_explicit(void *ehdr, void *symstart, size_t symsize,
-		void *strstart, size_t strsize)
+void *strstart, size_t strsize)
 {
-
 	if (!ksyms_verify(symstart, strstart))
 		return;
 
@@ -561,7 +560,7 @@ ksyms_addsyms_explicit(void *ehdr, void 
  */
 int
 ksyms_getval_unlocked(const char *mod, const char *sym, unsigned long *val,
-		  int type)
+int type)
 {
 	struct ksyms_symtab *st;
 	Elf_Sym *es;
@@ -729,7 +728,7 @@ ksyms_getname(const char **mod, const ch
  */
 void
 ksyms_modload(const char *name, void *symstart, vsize_t symsize,
-	  char *strstart, vsize_t strsize)
+char *strstart, vsize_t strsize)
 {
 	struct ksyms_symtab *st;
 
@@ -839,20 +838,20 @@ ksyms_sift(char *mod, char *sym, int mod
 static void
 ksyms_sizes_calc(void)
 {
-struct ksyms_symtab *st;
+	struct ksyms_symtab *st;
 	int i, delta;
 
-ksyms_symsz = ksyms_strsz = 0;
-TAILQ_FOREACH(st, _symtabs, sd_queue) {
+	ksyms_symsz = ksyms_strsz = 0;
+	TAILQ_FOREACH(st, _symtabs, sd_queue) {
 		delta = ksyms_strsz - st->sd_usroffset;
 		if (delta != 0) {
 			for (i = 0; i < st->sd_symsize/sizeof(Elf_Sym); i++)
 st->sd_symstart[i].st_name += delta;
 			st->sd_usroffset = ksyms_strsz;
 		}
-ksyms_symsz += st->sd_symsize;
-ksyms_strsz += st->sd_strsize;
-}
+		ksyms_symsz += st->sd_symsize;
+		ksyms_strsz += st->sd_strsize;
+	}
 }
 
 static void
@@ -949,7 +948,6 @@ ksyms_hdr_init(const void *hdraddr)
 static int
 ksymsopen(dev_t dev, int oflags, int devtype, struct lwp *l)
 {
-
 	if (minor(dev) != 0 || !ksyms_loaded)
 		return ENXIO;
 
@@ -978,7 +976,7 @@ ksymsclose(dev_t dev, int oflags, int de
 	struct ksyms_symtab *st, *next;
 	bool resize;
 
-	/* Discard refernces to symbol tables. */
+	/* Discard references to symbol tables. */
 	mutex_enter(_lock);
 	ksyms_isopen = false;
 	resize = false;
@@ -1074,7 +1072,6 @@ ksymsread(dev_t dev, struct uio *uio, in
 static int
 ksymswrite(dev_t dev, struct uio *uio, int ioflag)
 {
-
 	return EROFS;
 }
 
@@ -1097,8 +1094,8 @@ ksymsioctl(dev_t dev, u_long cmd, void *
 	/* Read ksyms_maxlen only once while not holding the lock. */
 	len = ksyms_maxlen;
 
-	if (cmd == OKIOCGVALUE || cmd == OKIOCGSYMBOL
-	|| cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
+	if (cmd == OKIOCGVALUE || cmd == OKIOCGSYMBOL ||
+	cmd == KIOCGVALUE || cmd == KIOCGSYMBOL) {
 		str = kmem_alloc(len, KM_SLEEP);
 		if ((error = copyinstr(kg->kg_name, str, len, NULL)) != 0) {
 			kmem_free(str, len);

Index: src/sys/kern/kern_module.c
diff -u src/sys/kern/kern_module.c:1.108 src/sys/kern/kern_module.c:1.109
--- src/sys/kern/kern_module.c:1.108	Tue Nov  3 03:33:43 

CVS commit: src

2015-12-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Dec  9 18:25:32 UTC 2015

Modified Files:
src/share/man/man9: veriexec.9
src/sys/conf: files
Added Files:
src/sys/dev: veriexec.c
Removed Files:
src/sys/dev: verified_exec.c

Log Message:
Rename verified_exec.c -> veriexec.c.

The old log is now in Attic/.


To generate a diff of this commit:
cvs rdiff -u -r1.28 -r1.29 src/share/man/man9/veriexec.9
cvs rdiff -u -r1.1151 -r1.1152 src/sys/conf/files
cvs rdiff -u -r0 -r1.1 src/sys/dev/veriexec.c
cvs rdiff -u -r1.71 -r0 src/sys/dev/verified_exec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/share/man/man9/veriexec.9
diff -u src/share/man/man9/veriexec.9:1.28 src/share/man/man9/veriexec.9:1.29
--- src/share/man/man9/veriexec.9:1.28	Fri Feb 13 17:50:48 2015
+++ src/share/man/man9/veriexec.9	Wed Dec  9 18:25:32 2015
@@ -1,4 +1,4 @@
-.\" $NetBSD: veriexec.9,v 1.28 2015/02/13 17:50:48 maxv Exp $
+.\" $NetBSD: veriexec.9,v 1.29 2015/12/09 18:25:32 maxv Exp $
 .\"
 .\" Copyright 2006 Elad Efrat 
 .\" Copyright 2006 Brett Lymn 
@@ -27,7 +27,7 @@
 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 .\" POSSIBILITY OF SUCH DAMAGE.
 .\"
-.Dd February 13, 2015
+.Dd December 9, 2015
 .Dt VERIEXEC 9
 .Os
 .Sh NAME
@@ -293,7 +293,7 @@ containing the filename.
 .Sh FILES
 .Bl -column srcxsysxkernxkernxverifiedexecxc foo
 .It Sy Path	Purpose
-.It src/sys/dev/verified_exec.c	driver for userland communication
+.It src/sys/dev/veriexec.c	driver for userland communication
 .It src/sys/sys/verified_exec.h	shared (userland/kernel) header file
 .It src/sys/kern/kern_veriexec.c	subsystem code
 .It src/sys/kern/vfs_syscalls.c	rename, remove, and unmount policies

Index: src/sys/conf/files
diff -u src/sys/conf/files:1.1151 src/sys/conf/files:1.1152
--- src/sys/conf/files:1.1151	Sat Dec  5 13:31:07 2015
+++ src/sys/conf/files	Wed Dec  9 18:25:32 2015
@@ -1,4 +1,4 @@
-#	$NetBSD: files,v 1.1151 2015/12/05 13:31:07 jmcneill Exp $
+#	$NetBSD: files,v 1.1152 2015/12/09 18:25:32 maxv Exp $
 #	@(#)files.newconf	7.5 (Berkeley) 5/10/93
 
 version 	20150846
@@ -1436,7 +1436,7 @@ file	dev/kttcp.c			kttcp		needs-flag
 # Veriexec
 defpseudo veriexec
 file	kern/kern_veriexec.c	veriexec
-file	dev/verified_exec.c		veriexec	needs-flag
+file	dev/veriexec.c		veriexec	needs-flag
 
 # driver attach / detach user interface
 defpseudo drvctl

Added files:

Index: src/sys/dev/veriexec.c
diff -u /dev/null src/sys/dev/veriexec.c:1.1
--- /dev/null	Wed Dec  9 18:25:32 2015
+++ src/sys/dev/veriexec.c	Wed Dec  9 18:25:32 2015
@@ -0,0 +1,270 @@
+/*	$NetBSD: veriexec.c,v 1.1 2015/12/09 18:25:32 maxv Exp $	*/
+
+/*-
+ * Copyright (c) 2005, 2006 Elad Efrat 
+ * Copyright (c) 2005, 2006 Brett Lymn 
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ * 3. The name of the authors may not be used to endorse or promote products
+ *derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include 
+__KERNEL_RCSID(0, "$NetBSD: veriexec.c,v 1.1 2015/12/09 18:25:32 maxv Exp $");
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#define DEVPORT_DEVICE struct device
+
+#include 
+
+void veriexecattach(device_t, device_t, void *);
+static dev_type_open(veriexecopen);
+static dev_type_close(veriexecclose);
+static dev_type_ioctl(veriexecioctl);
+
+struct veriexec_softc {
+	DEVPORT_DEVICE veriexec_dev;
+};
+
+const struct cdevsw veriexec_cdevsw = {
+	.d_open = veriexecopen,
+	.d_close = veriexecclose,
+	.d_read = 

CVS commit: src/sys/arch

2015-12-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Dec 16 18:54:03 UTC 2015

Modified Files:
src/sys/arch/i386/i386: trap.c
src/sys/arch/x86/x86: cpu.c

Log Message:
Extend SMEP support to i386 (does not require PAE).


To generate a diff of this commit:
cvs rdiff -u -r1.275 -r1.276 src/sys/arch/i386/i386/trap.c
cvs rdiff -u -r1.118 -r1.119 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/trap.c
diff -u src/sys/arch/i386/i386/trap.c:1.275 src/sys/arch/i386/i386/trap.c:1.276
--- src/sys/arch/i386/i386/trap.c:1.275	Fri Feb 27 17:45:52 2015
+++ src/sys/arch/i386/i386/trap.c	Wed Dec 16 18:54:03 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.275 2015/02/27 17:45:52 christos Exp $	*/
+/*	$NetBSD: trap.c,v 1.276 2015/12/16 18:54:03 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000, 2005, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.275 2015/02/27 17:45:52 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.276 2015/12/16 18:54:03 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -553,6 +553,14 @@ kernelfault:
 		}
 
 		cr2 = rcr2();
+
+		if (frame->tf_err & PGEX_X) {
+			/* SMEP might have brought us here */
+			if (cr2 > VM_MIN_ADDRESS && cr2 <= VM_MAXUSER_ADDRESS)
+panic("prevented execution of %p (SMEP)",
+(void *)cr2);
+		}
+
 		goto faultcommon;
 
 	case T_PAGEFLT|T_USER: {	/* page fault */

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.118 src/sys/arch/x86/x86/cpu.c:1.119
--- src/sys/arch/x86/x86/cpu.c:1.118	Sun Dec 13 15:53:06 2015
+++ src/sys/arch/x86/x86/cpu.c	Wed Dec 16 18:54:03 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.118 2015/12/13 15:53:06 maxv Exp $	*/
+/*	$NetBSD: cpu.c,v 1.119 2015/12/16 18:54:03 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.118 2015/12/13 15:53:06 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.119 2015/12/16 18:54:03 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -581,11 +581,9 @@ cpu_init(struct cpu_info *ci)
 	if (cpu_feature[1] & CPUID2_XSAVE)
 		cr4 |= CR4_OSXSAVE;
 
-#ifdef __x86_64__
 	/* If SMEP is supported, enable it */
 	if (cpu_feature[5] & CPUID_SEF_SMEP)
 		cr4 |= CR4_SMEP;
-#endif
 
 	if (cr4) {
 		cr4 |= rcr4();



CVS commit: src/sys/arch/x86

2015-12-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Dec 13 15:02:20 UTC 2015

Modified Files:
src/sys/arch/x86/include: cpu.h cpuvar.h
src/sys/arch/x86/x86: cpu.c identcpu.c

Log Message:
Retrieve cpuid7 (Structured Extended Features) into ci_feat_val.


To generate a diff of this commit:
cvs rdiff -u -r1.66 -r1.67 src/sys/arch/x86/include/cpu.h
cvs rdiff -u -r1.46 -r1.47 src/sys/arch/x86/include/cpuvar.h
cvs rdiff -u -r1.116 -r1.117 src/sys/arch/x86/x86/cpu.c
cvs rdiff -u -r1.48 -r1.49 src/sys/arch/x86/x86/identcpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/include/cpu.h
diff -u src/sys/arch/x86/include/cpu.h:1.66 src/sys/arch/x86/include/cpu.h:1.67
--- src/sys/arch/x86/include/cpu.h:1.66	Sun Feb 23 22:38:40 2014
+++ src/sys/arch/x86/include/cpu.h	Sun Dec 13 15:02:19 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.h,v 1.66 2014/02/23 22:38:40 dsl Exp $	*/
+/*	$NetBSD: cpu.h,v 1.67 2015/12/13 15:02:19 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -157,12 +157,14 @@ struct cpu_info {
 	uint32_t	ci_max_ext_cpuid; /* cpuid.8000:%eax */
 	volatile uint32_t	ci_lapic_counter;
 
-	uint32_t	ci_feat_val[5]; /* X86 CPUID feature bits */
+	uint32_t	ci_feat_val[7]; /* X86 CPUID feature bits */
 			/* [0] basic features cpuid.1:%edx
 			 * [1] basic features cpuid.1:%ecx (CPUID2_xxx bits)
 			 * [2] extended features cpuid:8001:%edx
 			 * [3] extended features cpuid:8001:%ecx
 			 * [4] VIA padlock features
+			 * [5] structured extended features cpuid.7:%ebx
+			 * [6] structured extended features cpuid.7:%ecx
 			 */
 	
 	const struct cpu_functions *ci_func;  /* start/stop functions */

Index: src/sys/arch/x86/include/cpuvar.h
diff -u src/sys/arch/x86/include/cpuvar.h:1.46 src/sys/arch/x86/include/cpuvar.h:1.47
--- src/sys/arch/x86/include/cpuvar.h:1.46	Fri Apr 20 22:23:24 2012
+++ src/sys/arch/x86/include/cpuvar.h	Sun Dec 13 15:02:19 2015
@@ -1,4 +1,4 @@
-/* 	$NetBSD: cpuvar.h,v 1.46 2012/04/20 22:23:24 rmind Exp $ */
+/* 	$NetBSD: cpuvar.h,v 1.47 2015/12/13 15:02:19 maxv Exp $ */
 
 /*-
  * Copyright (c) 2000, 2007 The NetBSD Foundation, Inc.
@@ -125,7 +125,7 @@ void	pat_init(struct cpu_info *);
 extern int cpu_vendor;
 extern bool x86_mp_online;
 
-extern uint32_t cpu_feature[5];
+extern uint32_t cpu_feature[7];
 
 #endif /* _KERNEL */
 

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.116 src/sys/arch/x86/x86/cpu.c:1.117
--- src/sys/arch/x86/x86/cpu.c:1.116	Thu Sep 17 23:48:01 2015
+++ src/sys/arch/x86/x86/cpu.c	Sun Dec 13 15:02:19 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.116 2015/09/17 23:48:01 nat Exp $	*/
+/*	$NetBSD: cpu.c,v 1.117 2015/12/13 15:02:19 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.116 2015/09/17 23:48:01 nat Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.117 2015/12/13 15:02:19 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -177,13 +177,15 @@ static void	tss_init(struct i386tss *, v
 
 static void	cpu_init_idle_lwp(struct cpu_info *);
 
-uint32_t cpu_feature[5]; /* X86 CPUID feature bits
-			  *	[0] basic features %edx
-			  *	[1] basic features %ecx
-			  *	[2] extended features %edx
-			  *	[3] extended features %ecx
-			  *	[4] VIA padlock features
-			  */
+uint32_t cpu_feature[7]; /* X86 CPUID feature bits */
+			/* [0] basic features cpuid.1:%edx
+			 * [1] basic features cpuid.1:%ecx (CPUID2_xxx bits)
+			 * [2] extended features cpuid:8001:%edx
+			 * [3] extended features cpuid:8001:%ecx
+			 * [4] VIA padlock features
+			 * [5] structured extended features cpuid.7:%ebx
+			 * [6] structured extended features cpuid.7:%ecx
+			 */
 
 extern char x86_64_doubleflt_stack[];
 
@@ -783,7 +785,7 @@ cpu_boot_secondary(struct cpu_info *ci)
 }
 
 /*
- * The CPU ends up here when its ready to run
+ * The CPU ends up here when it's ready to run.
  * This is called from code in mptramp.s; at this point, we are running
  * in the idle pcb/idle stack of the new CPU.  When this function returns,
  * this processor will enter the idle loop and start looking for work.

Index: src/sys/arch/x86/x86/identcpu.c
diff -u src/sys/arch/x86/x86/identcpu.c:1.48 src/sys/arch/x86/x86/identcpu.c:1.49
--- src/sys/arch/x86/x86/identcpu.c:1.48	Mon Dec  8 15:22:47 2014
+++ src/sys/arch/x86/x86/identcpu.c	Sun Dec 13 15:02:19 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: identcpu.c,v 1.48 2014/12/08 15:22:47 msaitoh Exp $	*/
+/*	$NetBSD: identcpu.c,v 1.49 2015/12/13 15:02:19 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1999, 2000, 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.48 2014/12/08 15:22:47 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: identcpu.c,v 1.49 2015/12/13 15:02:19 maxv Exp $");
 
 

CVS commit: src/sys/arch

2015-12-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Dec 13 15:53:06 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: trap.c
src/sys/arch/x86/x86: cpu.c

Log Message:
Implement amd64 support for SMEP - Supervisor Mode Execution Protection.

Now, on CPUs that support this feature, if the kernel tries to execute
an instruction located in userland, the CPU will trigger a page fault.

Tested on amd64 (Intel Core i5).


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/arch/amd64/amd64/trap.c
cvs rdiff -u -r1.117 -r1.118 src/sys/arch/x86/x86/cpu.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.82 src/sys/arch/amd64/amd64/trap.c:1.83
--- src/sys/arch/amd64/amd64/trap.c:1.82	Sat Nov 28 15:06:55 2015
+++ src/sys/arch/amd64/amd64/trap.c	Sun Dec 13 15:53:05 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.82 2015/11/28 15:06:55 dholland Exp $	*/
+/*	$NetBSD: trap.c,v 1.83 2015/12/13 15:53:05 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.82 2015/11/28 15:06:55 dholland Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.83 2015/12/13 15:53:05 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -515,6 +515,14 @@ kernelfault:
 		}
 
 		cr2 = rcr2();
+
+		if (frame->tf_err & PGEX_X) {
+			/* SMEP might have brought us here */
+			if (cr2 > VM_MIN_ADDRESS && cr2 <= VM_MAXUSER_ADDRESS)
+panic("prevented execution of %p (SMEP)",
+(void *)cr2);
+		}
+
 		goto faultcommon;
 
 	case T_PAGEFLT|T_USER: {	/* page fault */

Index: src/sys/arch/x86/x86/cpu.c
diff -u src/sys/arch/x86/x86/cpu.c:1.117 src/sys/arch/x86/x86/cpu.c:1.118
--- src/sys/arch/x86/x86/cpu.c:1.117	Sun Dec 13 15:02:19 2015
+++ src/sys/arch/x86/x86/cpu.c	Sun Dec 13 15:53:06 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpu.c,v 1.117 2015/12/13 15:02:19 maxv Exp $	*/
+/*	$NetBSD: cpu.c,v 1.118 2015/12/13 15:53:06 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2000-2012 NetBSD Foundation, Inc.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.117 2015/12/13 15:02:19 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cpu.c,v 1.118 2015/12/13 15:53:06 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -581,6 +581,12 @@ cpu_init(struct cpu_info *ci)
 	if (cpu_feature[1] & CPUID2_XSAVE)
 		cr4 |= CR4_OSXSAVE;
 
+#ifdef __x86_64__
+	/* If SMEP is supported, enable it */
+	if (cpu_feature[5] & CPUID_SEF_SMEP)
+		cr4 |= CR4_SMEP;
+#endif
+
 	if (cr4) {
 		cr4 |= rcr4();
 		lcr4(cr4);



CVS commit: src/sys/kern

2015-12-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 12 14:47:37 UTC 2015

Modified Files:
src/sys/kern: sys_module.c

Log Message:
Put the code in charge of handling MODCTL_STAT into a separate function.

No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/kern/sys_module.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/sys_module.c
diff -u src/sys/kern/sys_module.c:1.20 src/sys/kern/sys_module.c:1.21
--- src/sys/kern/sys_module.c:1.20	Wed Nov  4 04:28:58 2015
+++ src/sys/kern/sys_module.c	Sat Dec 12 14:47:37 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: sys_module.c,v 1.20 2015/11/04 04:28:58 pgoyette Exp $	*/
+/*	$NetBSD: sys_module.c,v 1.21 2015/12/12 14:47:37 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.20 2015/11/04 04:28:58 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sys_module.c,v 1.21 2015/12/12 14:47:37 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_modular.h"
@@ -115,6 +115,73 @@ out1:
 	return error;
 }
 
+static int
+handle_modctl_stat(struct iovec *iov, void *arg)
+{
+	modstat_t *ms, *mso;
+	modinfo_t *mi;
+	module_t *mod;
+	vaddr_t addr;
+	size_t size;
+	size_t mslen;
+	int error;
+
+	kernconfig_lock();
+	mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
+	mso = kmem_zalloc(mslen, KM_SLEEP);
+	if (mso == NULL) {
+		kernconfig_unlock();
+		return ENOMEM;
+	}
+	ms = mso;
+	TAILQ_FOREACH(mod, _list, mod_chain) {
+		mi = mod->mod_info;
+		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
+		if (mi->mi_required != NULL) {
+			strlcpy(ms->ms_required, mi->mi_required,
+			sizeof(ms->ms_required));
+		}
+		if (mod->mod_kobj != NULL) {
+			kobj_stat(mod->mod_kobj, , );
+			ms->ms_addr = addr;
+			ms->ms_size = size;
+		}
+		ms->ms_class = mi->mi_class;
+		ms->ms_refcnt = mod->mod_refcnt;
+		ms->ms_source = mod->mod_source;
+		ms->ms_flags = mod->mod_flags;
+		ms++;
+	}
+	TAILQ_FOREACH(mod, _builtins, mod_chain) {
+		mi = mod->mod_info;
+		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
+		if (mi->mi_required != NULL) {
+			strlcpy(ms->ms_required, mi->mi_required,
+			sizeof(ms->ms_required));
+		}
+		if (mod->mod_kobj != NULL) {
+			kobj_stat(mod->mod_kobj, , );
+			ms->ms_addr = addr;
+			ms->ms_size = size;
+		}
+		ms->ms_class = mi->mi_class;
+		ms->ms_refcnt = -1;
+		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
+		ms->ms_source = mod->mod_source;
+		ms++;
+	}
+	kernconfig_unlock();
+	error = copyout(mso, iov->iov_base,
+	min(mslen - sizeof(modstat_t), iov->iov_len));
+	kmem_free(mso, mslen);
+	if (error == 0) {
+		iov->iov_len = mslen - sizeof(modstat_t);
+		error = copyout(iov, arg, sizeof(*iov));
+	}
+
+	return error;
+}
+
 int
 sys_modctl(struct lwp *l, const struct sys_modctl_args *uap,
 	   register_t *retval)
@@ -124,12 +191,6 @@ sys_modctl(struct lwp *l, const struct s
 		syscallarg(void *)	arg;
 	} */
 	char buf[MAXMODNAME];
-	size_t mslen;
-	module_t *mod;
-	modinfo_t *mi;
-	modstat_t *ms, *mso;
-	vaddr_t addr;
-	size_t size;
 	struct iovec iov;
 	modctl_load_t ml;
 	int error;
@@ -161,58 +222,7 @@ sys_modctl(struct lwp *l, const struct s
 		if (error != 0) {
 			break;
 		}
-		kernconfig_lock();
-		mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
-		mso = kmem_zalloc(mslen, KM_SLEEP);
-		if (mso == NULL) {
-			kernconfig_unlock();
-			return ENOMEM;
-		}
-		ms = mso;
-		TAILQ_FOREACH(mod, _list, mod_chain) {
-			mi = mod->mod_info;
-			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
-			if (mi->mi_required != NULL) {
-strlcpy(ms->ms_required, mi->mi_required,
-sizeof(ms->ms_required));
-			}
-			if (mod->mod_kobj != NULL) {
-kobj_stat(mod->mod_kobj, , );
-ms->ms_addr = addr;
-ms->ms_size = size;
-			}
-			ms->ms_class = mi->mi_class;
-			ms->ms_refcnt = mod->mod_refcnt;
-			ms->ms_source = mod->mod_source;
-			ms->ms_flags = mod->mod_flags;
-			ms++;
-		}
-		TAILQ_FOREACH(mod, _builtins, mod_chain) {
-			mi = mod->mod_info;
-			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
-			if (mi->mi_required != NULL) {
-strlcpy(ms->ms_required, mi->mi_required,
-sizeof(ms->ms_required));
-			}
-			if (mod->mod_kobj != NULL) {
-kobj_stat(mod->mod_kobj, , );
-ms->ms_addr = addr;
-ms->ms_size = size;
-			}
-			ms->ms_class = mi->mi_class;
-			ms->ms_refcnt = -1;
-			KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
-			ms->ms_source = mod->mod_source;
-			ms++;
-		}
-		kernconfig_unlock();
-		error = copyout(mso, iov.iov_base,
-		min(mslen - sizeof(modstat_t), iov.iov_len));
-		kmem_free(mso, mslen);
-		if (error == 0) {
-			iov.iov_len = mslen - sizeof(modstat_t);
-			error = copyout(, arg, sizeof(iov));
-		}
+		error = handle_modctl_stat(, arg);
 		break;
 
 	case MODCTL_EXISTS:



CVS commit: src/sys/secmodel/extensions

2015-12-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 12 14:57:52 UTC 2015

Modified Files:
src/sys/secmodel/extensions: secmodel_extensions.c

Log Message:
secmodel_extensions_system_cb() is not mount-specific, even though
KAUTH_SYSTEM_MOUNT happens to be the only option handled here.

Put everything into a swith(action). No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/secmodel/extensions/secmodel_extensions.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/secmodel/extensions/secmodel_extensions.c
diff -u src/sys/secmodel/extensions/secmodel_extensions.c:1.6 src/sys/secmodel/extensions/secmodel_extensions.c:1.7
--- src/sys/secmodel/extensions/secmodel_extensions.c:1.6	Tue Feb 25 18:30:13 2014
+++ src/sys/secmodel/extensions/secmodel_extensions.c	Sat Dec 12 14:57:52 2015
@@ -1,4 +1,4 @@
-/* $NetBSD: secmodel_extensions.c,v 1.6 2014/02/25 18:30:13 pooka Exp $ */
+/* $NetBSD: secmodel_extensions.c,v 1.7 2015/12/12 14:57:52 maxv Exp $ */
 /*-
  * Copyright (c) 2011 Elad Efrat 
  * All rights reserved.
@@ -27,7 +27,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: secmodel_extensions.c,v 1.6 2014/02/25 18:30:13 pooka Exp $");
+__KERNEL_RCSID(0, "$NetBSD: secmodel_extensions.c,v 1.7 2015/12/12 14:57:52 maxv Exp $");
 
 #include 
 #include 
@@ -330,54 +330,60 @@ secmodel_extensions_system_cb(kauth_cred
 	req = (enum kauth_system_req)arg0;
 	result = KAUTH_RESULT_DEFER;
 
-	if (action != KAUTH_SYSTEM_MOUNT || dovfsusermount == 0)
-		return result;
-
-	switch (req) {
-	case KAUTH_REQ_SYSTEM_MOUNT_NEW:
-		vp = (vnode_t *)arg1;
-		mp = vp->v_mount;
-		flags = (u_long)arg2;
-
-		/*
-		 * Ensure that the user owns the directory onto which the
-		 * mount is attempted.
-		 */
-		vn_lock(vp, LK_SHARED | LK_RETRY);
-		error = VOP_GETATTR(vp, , cred);
-		VOP_UNLOCK(vp);
-		if (error)
+	switch (action) {
+	case KAUTH_SYSTEM_MOUNT:
+		if (dovfsusermount == 0)
 			break;
+		switch (req) {
+		case KAUTH_REQ_SYSTEM_MOUNT_NEW:
+			vp = (vnode_t *)arg1;
+			mp = vp->v_mount;
+			flags = (u_long)arg2;
+
+			/*
+			 * Ensure that the user owns the directory onto which
+			 * the mount is attempted.
+			 */
+			vn_lock(vp, LK_SHARED | LK_RETRY);
+			error = VOP_GETATTR(vp, , cred);
+			VOP_UNLOCK(vp);
+			if (error)
+break;
+
+			if (va.va_uid != kauth_cred_geteuid(cred))
+break;
+
+			error = usermount_common_policy(mp, flags);
+			if (error)
+break;
 
-		if (va.va_uid != kauth_cred_geteuid(cred))
-			break;
+			result = KAUTH_RESULT_ALLOW;
 
-		error = usermount_common_policy(mp, flags);
-		if (error)
 			break;
 
-		result = KAUTH_RESULT_ALLOW;
-
-		break;
+		case KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT:
+			mp = arg1;
 
-	case KAUTH_REQ_SYSTEM_MOUNT_UNMOUNT:
-		mp = arg1;
+			/* Must own the mount. */
+			if (mp->mnt_stat.f_owner == kauth_cred_geteuid(cred))
+result = KAUTH_RESULT_ALLOW;
 
-		/* Must own the mount. */
-		if (mp->mnt_stat.f_owner == kauth_cred_geteuid(cred))
-			result = KAUTH_RESULT_ALLOW;
+			break;
 
-		break;
+		case KAUTH_REQ_SYSTEM_MOUNT_UPDATE:
+			mp = arg1;
+			flags = (u_long)arg2;
+
+			/* Must own the mount. */
+			if (mp->mnt_stat.f_owner == kauth_cred_geteuid(cred) &&
+usermount_common_policy(mp, flags) == 0)
+result = KAUTH_RESULT_ALLOW;
 
-	case KAUTH_REQ_SYSTEM_MOUNT_UPDATE:
-		mp = arg1;
-		flags = (u_long)arg2;
-
-		/* Must own the mount. */
-		if (mp->mnt_stat.f_owner == kauth_cred_geteuid(cred) &&
-		usermount_common_policy(mp, flags) == 0)
-			result = KAUTH_RESULT_ALLOW;
+			break;
 
+		default:
+			break;
+		}
 		break;
 
 	default:



CVS commit: src/sys/compat/netbsd32

2015-12-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 12 15:27:42 UTC 2015

Modified Files:
src/sys/compat/netbsd32: netbsd32_module.c

Log Message:
Put the code in charge of handling MODCTL_STAT (32bit) into a separate
function. No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/compat/netbsd32/netbsd32_module.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/netbsd32/netbsd32_module.c
diff -u src/sys/compat/netbsd32/netbsd32_module.c:1.2 src/sys/compat/netbsd32/netbsd32_module.c:1.3
--- src/sys/compat/netbsd32/netbsd32_module.c:1.2	Sun Jun 21 06:51:05 2015
+++ src/sys/compat/netbsd32/netbsd32_module.c	Sat Dec 12 15:27:42 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_module.c,v 1.2 2015/06/21 06:51:05 msaitoh Exp $	*/
+/*	$NetBSD: netbsd32_module.c,v 1.3 2015/12/12 15:27:42 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.2 2015/06/21 06:51:05 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.3 2015/12/12 15:27:42 maxv Exp $");
 
 #include 
 #include 
@@ -42,6 +42,72 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_mod
 #include 
 #include 
 
+static int
+modctl32_handle_stat(struct netbsd32_iovec *iov, void *arg)
+{
+	modstat_t *ms, *mso;
+	modinfo_t *mi;
+	module_t *mod;
+	vaddr_t addr;
+	size_t size;
+	size_t mslen;
+	int error;
+
+	kernconfig_lock();
+	mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
+	mso = kmem_zalloc(mslen, KM_SLEEP);
+	if (mso == NULL) {
+		kernconfig_unlock();
+		return ENOMEM;
+	}
+	ms = mso;
+	TAILQ_FOREACH(mod, _list, mod_chain) {
+		mi = mod->mod_info;
+		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
+		if (mi->mi_required != NULL) {
+			strlcpy(ms->ms_required, mi->mi_required,
+			sizeof(ms->ms_required));
+		}
+		if (mod->mod_kobj != NULL) {
+			kobj_stat(mod->mod_kobj, , );
+			ms->ms_addr = addr;
+			ms->ms_size = size;
+		}
+		ms->ms_class = mi->mi_class;
+		ms->ms_refcnt = mod->mod_refcnt;
+		ms->ms_source = mod->mod_source;
+		ms++;
+	}
+	TAILQ_FOREACH(mod, _builtins, mod_chain) {
+		mi = mod->mod_info;
+		strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
+		if (mi->mi_required != NULL) {
+			strlcpy(ms->ms_required, mi->mi_required,
+			sizeof(ms->ms_required));
+		}
+		if (mod->mod_kobj != NULL) {
+			kobj_stat(mod->mod_kobj, , );
+			ms->ms_addr = addr;
+			ms->ms_size = size;
+		}
+		ms->ms_class = mi->mi_class;
+		ms->ms_refcnt = -1;
+		KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
+		ms->ms_source = mod->mod_source;
+		ms++;
+	}
+	kernconfig_unlock();
+	error = copyout(mso, NETBSD32PTR64(iov->iov_base),
+	min(mslen - sizeof(modstat_t), iov->iov_len));
+	kmem_free(mso, mslen);
+	if (error == 0) {
+		iov->iov_len = mslen - sizeof(modstat_t);
+		error = copyout(iov, arg, sizeof(*iov));
+	}
+
+	return error;
+}
+
 int
 netbsd32_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
 	register_t *result)
@@ -51,12 +117,6 @@ netbsd32_modctl(struct lwp *lwp, const s
 		syscallarg(netbsd32_voidp) arg;
 	} */
 	char buf[MAXMODNAME];
-	size_t mslen;
-	module_t *mod;
-	modinfo_t *mi;
-	modstat_t *ms, *mso;
-	vaddr_t addr;
-	size_t size;
 	struct netbsd32_iovec iov;
 	struct netbsd32_modctl_load ml;
 	int error;
@@ -88,57 +148,7 @@ netbsd32_modctl(struct lwp *lwp, const s
 		if (error != 0) {
 			break;
 		}
-		kernconfig_lock();
-		mslen = (module_count+module_builtinlist+1) * sizeof(modstat_t);
-		mso = kmem_zalloc(mslen, KM_SLEEP);
-		if (mso == NULL) {
-			kernconfig_unlock();
-			return ENOMEM;
-		}
-		ms = mso;
-		TAILQ_FOREACH(mod, _list, mod_chain) {
-			mi = mod->mod_info;
-			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
-			if (mi->mi_required != NULL) {
-strlcpy(ms->ms_required, mi->mi_required,
-sizeof(ms->ms_required));
-			}
-			if (mod->mod_kobj != NULL) {
-kobj_stat(mod->mod_kobj, , );
-ms->ms_addr = addr;
-ms->ms_size = size;
-			}
-			ms->ms_class = mi->mi_class;
-			ms->ms_refcnt = mod->mod_refcnt;
-			ms->ms_source = mod->mod_source;
-			ms++;
-		}
-		TAILQ_FOREACH(mod, _builtins, mod_chain) {
-			mi = mod->mod_info;
-			strlcpy(ms->ms_name, mi->mi_name, sizeof(ms->ms_name));
-			if (mi->mi_required != NULL) {
-strlcpy(ms->ms_required, mi->mi_required,
-sizeof(ms->ms_required));
-			}
-			if (mod->mod_kobj != NULL) {
-kobj_stat(mod->mod_kobj, , );
-ms->ms_addr = addr;
-ms->ms_size = size;
-			}
-			ms->ms_class = mi->mi_class;
-			ms->ms_refcnt = -1;
-			KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
-			ms->ms_source = mod->mod_source;
-			ms++;
-		}
-		kernconfig_unlock();
-		error = copyout(mso, NETBSD32PTR64(iov.iov_base),
-		min(mslen - sizeof(modstat_t), iov.iov_len));
-		kmem_free(mso, mslen);
-		if (error == 0) {
-			iov.iov_len = mslen - 

CVS commit: src/sys/arch/amd64/amd64

2015-12-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Dec  9 16:55:18 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: copy.S mptramp.S

Log Message:
KNF, and use C-style comments. Also, remove fusword/susword.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/amd64/amd64/copy.S
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/amd64/mptramp.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/copy.S
diff -u src/sys/arch/amd64/amd64/copy.S:1.19 src/sys/arch/amd64/amd64/copy.S:1.20
--- src/sys/arch/amd64/amd64/copy.S:1.19	Sat Nov 21 12:34:48 2015
+++ src/sys/arch/amd64/amd64/copy.S	Wed Dec  9 16:55:18 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: copy.S,v 1.19 2015/11/21 12:34:48 maxv Exp $	*/
+/*	$NetBSD: copy.S,v 1.20 2015/12/09 16:55:18 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -119,7 +119,7 @@ ENTRY(do_pmap_load)
  * Also note that the setup time for 'rep movs' is horrid - especially on P4
  * netburst - but on my AMD X2 it manages one copy (read+write) per clock
  * which can be achieved with a code loop, but is probably impossible to beat.
- * Howver the use of 'rep movsb' for the final bytes should be killed.
+ * However the use of 'rep movsb' for the final bytes should be killed.
  *
  * Newer Intel cpus have a much lower setup time, and may (someday)
  * be ably to do cache-line size copies
@@ -131,35 +131,37 @@ ENTRY(kcopy)
 .Lkcopy_start:
 	movq	%rdi,%rax
 	subq	%rsi,%rax
-	cmpq	%rcx,%rax		# overlapping?
+	cmpq	%rcx,%rax		/* overlapping? */
 	jb	1f
-	# nope, copy forward
-	shrq	$3,%rcx			# copy by 64-bit words
+	/* nope, copy forward */
+	shrq	$3,%rcx			/* copy by 64-bit words */
 	rep
 	movsq
 
 	movq	%rdx,%rcx
-	andl	$7,%ecx			# any bytes left?
+	andl	$7,%ecx			/* any bytes left? */
 	rep
 	movsb
 
 	xorq	%rax,%rax
 	ret
 
-# Using 'rep movs' to copy backwards is not as fast as for forwards copies
-# and ought not be done when the copy doesn't acually overlap.
-# However kcopy() isn't used any that looks even vaguely used often.
-# I'm also not sure it is ever asked to do overlapping copies!
+/*
+ * Using 'rep movs' to copy backwards is not as fast as for forwards copies
+ * and ought not be done when the copy doesn't acually overlap.
+ * However kcopy() isn't used any that looks even vaguely used often.
+ * I'm also not sure it is ever asked to do overlapping copies!
+ */
 
-1:	addq	%rcx,%rdi		# copy backward
+1:	addq	%rcx,%rdi		/* copy backward */
 	addq	%rcx,%rsi
 	std
-	andq	$7,%rcx			# any fractional bytes?
+	andq	$7,%rcx			/* any fractional bytes? */
 	decq	%rdi
 	decq	%rsi
 	rep
 	movsb
-	movq	%rdx,%rcx		# copy remainder by 64-bit words
+	movq	%rdx,%rcx		/* copy remainder by 64-bit words */
 	shrq	$3,%rcx
 	subq	$7,%rsi
 	subq	$7,%rdi
@@ -173,24 +175,24 @@ ENTRY(kcopy)
 ENTRY(copyout)
 	DEFERRED_SWITCH_CHECK
 
-	xchgq	%rdi,%rsi		# kernel address to %rsi, user to %rdi
-	movq	%rdx,%rax		# save transfer length (bytes)
+	xchgq	%rdi,%rsi		/* kernel address to %rsi, user to %rdi */
+	movq	%rdx,%rax		/* save transfer length (bytes) */
 
-	addq	%rdi,%rdx		# end address to %rdx
-	jc	_C_LABEL(copy_efault)	# jump if wraps
+	addq	%rdi,%rdx		/* end address to %rdx */
+	jc	_C_LABEL(copy_efault)	/* jump if wraps */
 	movq	$VM_MAXUSER_ADDRESS,%r8
 	cmpq	%r8,%rdx
-	ja	_C_LABEL(copy_efault)	# jump if end in kernel space
+	ja	_C_LABEL(copy_efault)	/* jump if end in kernel space */
 
 .Lcopyout_start:
-	movq	%rax,%rcx		# length
-	shrq	$3,%rcx			# count of 8-byte words
+	movq	%rax,%rcx		/* length */
+	shrq	$3,%rcx			/* count of 8-byte words */
 	rep
-	movsq# copy from %rsi to %rdi
+	movsq/* copy from %rsi to %rdi */
 	movb	%al,%cl
-	andb	$7,%cl			# remaining number of bytes
+	andb	$7,%cl			/* remaining number of bytes */
 	rep
-	movsb# copy remaining bytes
+	movsb/* copy remaining bytes */
 .Lcopyout_end:
 	xorl	%eax,%eax
 	ret
@@ -202,11 +204,11 @@ ENTRY(copyin)
 	xchgq	%rdi,%rsi
 	movq	%rdx,%rax
 
-	addq	%rsi,%rdx		# Check source address not wrapped
+	addq	%rsi,%rdx		/* check source address not wrapped */
 	jc	_C_LABEL(copy_efault)
-	movq	$VM_MAXUSER_ADDRESS,%r8	
+	movq	$VM_MAXUSER_ADDRESS,%r8
 	cmpq	%r8,%rdx
-	ja	_C_LABEL(copy_efault)	# j if end in kernel space
+	ja	_C_LABEL(copy_efault)	/* j if end in kernel space */
 
 .Lcopyin_start:
 3:	/* bcopy(%rsi, %rdi, %rax); */
@@ -359,19 +361,7 @@ ENTRY(copystr)
 
 7:	ret
 
-ENTRY(fusword)
-	DEFERRED_SWITCH_CHECK
-	movq	$VM_MAXUSER_ADDRESS-2,%r11
-	cmpq	%r11,%rdi
-	ja	_C_LABEL(fusuaddrfault)
-	GET_CURPCB(%rcx)
-	leaq	_C_LABEL(fusufailure)(%rip),%r11
-	movq	%r11,PCB_ONFAULT(%rcx)
-	movzwl	(%rdi),%eax
-	movq	$0,PCB_ONFAULT(%rcx)
-	ret
-	DEFERRED_SWITCH_CALL
-	
+
 ENTRY(fuswintr)
 	cmpl	$TLBSTATE_VALID, CPUVAR(TLBSTATE)
 	jnz	_C_LABEL(fusuaddrfault)
@@ -384,7 +374,7 @@ ENTRY(fuswintr)
 	movzwl	(%rdi),%eax
 	movq	$0,PCB_ONFAULT(%rcx)
 	ret
-	
+
 ENTRY(fubyte)
 	DEFERRED_SWITCH_CHECK

CVS commit: src/sys/compat/netbsd32

2015-12-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec 19 13:15:21 UTC 2015

Modified Files:
src/sys/compat/netbsd32: netbsd32_module.c

Log Message:
Missing field (was here before my change).


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/compat/netbsd32/netbsd32_module.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/netbsd32/netbsd32_module.c
diff -u src/sys/compat/netbsd32/netbsd32_module.c:1.3 src/sys/compat/netbsd32/netbsd32_module.c:1.4
--- src/sys/compat/netbsd32/netbsd32_module.c:1.3	Sat Dec 12 15:27:42 2015
+++ src/sys/compat/netbsd32/netbsd32_module.c	Sat Dec 19 13:15:21 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_module.c,v 1.3 2015/12/12 15:27:42 maxv Exp $	*/
+/*	$NetBSD: netbsd32_module.c,v 1.4 2015/12/19 13:15:21 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -29,7 +29,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.3 2015/12/12 15:27:42 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_module.c,v 1.4 2015/12/19 13:15:21 maxv Exp $");
 
 #include 
 #include 
@@ -76,6 +76,7 @@ modctl32_handle_stat(struct netbsd32_iov
 		ms->ms_class = mi->mi_class;
 		ms->ms_refcnt = mod->mod_refcnt;
 		ms->ms_source = mod->mod_source;
+		ms->ms_flags = mod->mod_flags;
 		ms++;
 	}
 	TAILQ_FOREACH(mod, _builtins, mod_chain) {



CVS commit: src/sys/arch/amd64/amd64

2015-11-25 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 25 16:00:10 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Cosmetic changes.


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.83 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.82 src/sys/arch/amd64/amd64/locore.S:1.83
--- src/sys/arch/amd64/amd64/locore.S:1.82	Sun Nov 22 14:06:08 2015
+++ src/sys/arch/amd64/amd64/locore.S	Wed Nov 25 16:00:09 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.82 2015/11/22 14:06:08 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.83 2015/11/25 16:00:09 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -191,6 +191,43 @@
 #define	_RELOC(x)	((x) - KERNBASE)
 #define	RELOC(x)	_RELOC(_C_LABEL(x))
 
+#if L2_SLOT_KERNBASE > 0
+#define TABLE_L2_ENTRIES (2 * (NKL2_KIMG_ENTRIES + 1))
+#else
+#define TABLE_L2_ENTRIES (NKL2_KIMG_ENTRIES + 1)
+#endif
+
+#if L3_SLOT_KERNBASE > 0
+#define TABLE_L3_ENTRIES (2 * NKL3_KIMG_ENTRIES)
+#else
+#define TABLE_L3_ENTRIES NKL3_KIMG_ENTRIES
+#endif
+
+#define PROC0_PML4_OFF	0
+#define PROC0_STK_OFF	(PROC0_PML4_OFF + PAGE_SIZE)
+#define PROC0_PTP3_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
+#define PROC0_PTP2_OFF	(PROC0_PTP3_OFF + NKL4_KIMG_ENTRIES * PAGE_SIZE)
+#define PROC0_PTP1_OFF	(PROC0_PTP2_OFF + TABLE_L3_ENTRIES * PAGE_SIZE)
+#define TABLESIZE \
+  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1 + UPAGES) \
+* PAGE_SIZE)
+
+/*
+ * fillkpt - Fill in a kernel page table
+ *	eax = pte (page frame | control | status)
+ *	ebx = page table address
+ *	ecx = number of pages to map
+ *
+ * Each entry is 8 (PDE_SIZE) bytes long: we must set the 4 upper bytes to 0.
+ */
+#define fillkpt	\
+1:	movl	%eax,(%ebx)		; 	/* store phys addr */	\
+	movl	$0,(PDE_SIZE-4)(%ebx)	; 	/* upper 32 bits 0 */	\
+	addl	$PDE_SIZE,%ebx		; 	/* next PTE/PDE */	\
+	addl	$PAGE_SIZE,%eax		; 	/* next phys page */	\
+	loop	1b			;
+
+
 #ifdef XEN
 
 /*
@@ -358,7 +395,7 @@ ENTRY(start)
 
 	/*
 	 * Load parameters from the stack (32 bits):   
-	 * boothowto, [bootdev], bootinfo, esym, basemem, extmem
+	 * boothowto, [bootdev], bootinfo, esym, biosbasemem, biosextmem
 	 * We are not interested in 'bootdev'.
 	 */
 
@@ -443,24 +480,29 @@ bootinfo_finished:
 	movl	%eax,(%ebp)
 	movl	$KERNBASE_HI,4(%ebp)
 
+	/* Load 'biosextmem' */
 	movl	$RELOC(biosextmem),%ebp
 	movl	(%ebp),%eax
-	testl	%eax,%eax
-	jnz	1f
+	testl	%eax,%eax		/* already set? */
+	jnz	biosextmem_finished
 
 	movl	20(%esp),%eax
 	movl	%eax,(%ebp)
 
-1:
+biosextmem_finished:
+	/* Load 'biosbasemem' */
 	movl	$RELOC(biosbasemem),%ebp
 	movl	(%ebp),%eax
-	testl	%eax,%eax
-	jnz	1f
+	testl	%eax,%eax		/* already set? */
+	jnz	biosbasemem_finished
+
 	movl	24(%esp),%eax
 	movl	%eax,(%ebp)
-1:
 
-	/* First, reset the PSL. */
+biosbasemem_finished:
+	/*
+	 * Done with the parameters. First, reset the PSL.
+	 */
 	pushl	$PSL_MBO
 	popfl
 
@@ -489,45 +531,9 @@ bootinfo_finished:
  *0  1   2  3
  */
 
-#if L2_SLOT_KERNBASE > 0
-#define TABLE_L2_ENTRIES (2 * (NKL2_KIMG_ENTRIES + 1))
-#else
-#define TABLE_L2_ENTRIES (NKL2_KIMG_ENTRIES + 1)
-#endif
-
-#if L3_SLOT_KERNBASE > 0
-#define TABLE_L3_ENTRIES (2 * NKL3_KIMG_ENTRIES)
-#else
-#define TABLE_L3_ENTRIES NKL3_KIMG_ENTRIES
-#endif
-
-#define PROC0_PML4_OFF	0
-#define PROC0_STK_OFF	(PROC0_PML4_OFF + PAGE_SIZE)
-#define PROC0_PTP3_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
-#define PROC0_PTP2_OFF	(PROC0_PTP3_OFF + NKL4_KIMG_ENTRIES * PAGE_SIZE)
-#define PROC0_PTP1_OFF	(PROC0_PTP2_OFF + TABLE_L3_ENTRIES * PAGE_SIZE)
-#define TABLESIZE \
-  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1 + UPAGES) \
-* PAGE_SIZE)
-
-/*
- * fillkpt - Fill in a kernel page table
- *	eax = pte (page frame | control | status)
- *	ebx = page table address
- *	ecx = number of pages to map
- *
- * Each entry is 8 (PDE_SIZE) bytes long: we must set the 4 upper bytes to 0.
- */
-
-#define fillkpt	\
-1:	movl	%eax,(%ebx)		; 	/* store phys addr */	\
-	movl	$0,(PDE_SIZE-4)(%ebx)	; 	/* upper 32 bits 0 */	\
-	addl	$PDE_SIZE,%ebx		; 	/* next PTE/PDE */	\
-	addl	$PAGE_SIZE,%eax		; 	/* next phys page */	\
-	loop	1b			;
-
 	/* Find end of kernel image. */
 	movl	$RELOC(end),%edi
+
 #if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(makeoptions_COPY_SYMTAB)
 	/* Save the symbols (if loaded). */
 	movl	RELOC(esym),%eax
@@ -551,17 +557,18 @@ bootinfo_finished:
 	andl	$~PGOFSET,%esi
 	movl	%esi,%edi
 
+	/* Clear the tables */
 	xorl	%eax,%eax
 	cld
 	movl	$TABLESIZE,%ecx
 	shrl	$2,%ecx
 	rep
-	stosl
+	stosl/* copy eax -> edi */
 
 /*
  * Build initial page tables.
  */
-	leal	(PROC0_PTP1_OFF)(%esi), %ebx
+	leal	(PROC0_PTP1_OFF)(%esi),%ebx
 
 	/* Skip the first MB */
 	movl	$(KERNTEXTOFF_LO - KERNBASE_LO),%eax
@@ -570,9 +577,9 @@ 

CVS commit: src/sys/arch/i386/conf

2015-11-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov 28 18:08:40 UTC 2015

Modified Files:
src/sys/arch/i386/conf: kern.ldscript

Log Message:
KNF


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/i386/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/conf/kern.ldscript
diff -u src/sys/arch/i386/conf/kern.ldscript:1.15 src/sys/arch/i386/conf/kern.ldscript:1.16
--- src/sys/arch/i386/conf/kern.ldscript:1.15	Tue Aug 25 12:56:58 2015
+++ src/sys/arch/i386/conf/kern.ldscript	Sat Nov 28 18:08:40 2015
@@ -1,68 +1,72 @@
-/*	$NetBSD: kern.ldscript,v 1.15 2015/08/25 12:56:58 uebayasi Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.16 2015/11/28 18:08:40 maxv Exp $	*/
 
 #include "assym.h"
 
 ENTRY(_start)
 SECTIONS
 {
-  /* Read-only sections, merged into text segment: */
-  .text :
-  {
-*(.text)
-*(.text.*)
-*(.stub)
-  }
-  _etext = . ;
-  PROVIDE (etext = .) ;
-
-  .rodata :
-  {
-*(.rodata)
-*(.rodata.*)
-  }
-
-  /* Adjust the address for the data segment.  We want to adjust up to
- the same address within the page on the next page up.  */
-  . = ALIGN(0x1000) + (. & (0x1000 - 1));
-  __data_start = . ;
-  .data :
-  {
-*(.data)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.cacheline_aligned :
-  {
-*(.data.cacheline_aligned)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.read_mostly :
-  {
-*(.data.read_mostly)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  _edata = . ;
-  PROVIDE (edata = .) ;
-  __bss_start = . ;
-  .bss :
-  {
-*(.bss)
-*(.bss.*)
-*(COMMON)
-. = ALIGN(32 / 8);
-  }
-  . = ALIGN(32 / 8);
-  _end = . ;
-  PROVIDE (end = .) ;
-  .note.netbsd.ident :
-  {
-KEEP(*(.note.netbsd.ident));
-  }
+	/* Read-only sections, merged into text segment: */
+	.text :
+	{
+		*(.text)
+		*(.text.*)
+		*(.stub)
+	}
+	_etext = . ;
+	PROVIDE (etext = .) ;
+
+	.rodata :
+	{
+		*(.rodata)
+		*(.rodata.*)
+	}
+
+	/*
+	 * Adjust the address for the data segment.  We want to adjust up to
+	 * the same address within the page on the next page up.
+	 */
+	. = ALIGN(0x1000) + (. & (0x1000 - 1));
+	__data_start = . ;
+	.data :
+	{
+		*(.data)
+	}
+
+	. = ALIGN(COHERENCY_UNIT);
+	.data.cacheline_aligned :
+	{
+		*(.data.cacheline_aligned)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+	.data.read_mostly :
+	{
+		*(.data.read_mostly)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+	_edata = . ;
+	PROVIDE (edata = .) ;
+	__bss_start = . ;
+	.bss :
+	{
+		*(.bss)
+		*(.bss.*)
+		*(COMMON)
+	. = ALIGN(32 / 8);
+	}
+	. = ALIGN(32 / 8);
+	_end = . ;
+	PROVIDE (end = .) ;
+	.note.netbsd.ident :
+	{
+		KEEP(*(.note.netbsd.ident));
+	}
 }
+
 SECTIONS
 {
-  .text :
-  AT (ADDR(.text) & 0x0fff)
-  {
-*(.text)
-  } =0
+	.text :
+	AT (ADDR(.text) & 0x0fff)
+	{
+		*(.text)
+	} = 0
 }



CVS commit: src/sys/arch/amd64/amd64

2015-11-20 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 20 11:58:00 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: genassym.cf locore.S

Log Message:
A few changes:
 - remove cpu_id and cpu_brand_id (unused)
 - copy a comment from i386 about fillkpt
 - define PDE_SIZE (i386)


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/sys/arch/amd64/amd64/genassym.cf
cvs rdiff -u -r1.79 -r1.80 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/genassym.cf
diff -u src/sys/arch/amd64/amd64/genassym.cf:1.59 src/sys/arch/amd64/amd64/genassym.cf:1.60
--- src/sys/arch/amd64/amd64/genassym.cf:1.59	Wed Aug 26 03:00:53 2015
+++ src/sys/arch/amd64/amd64/genassym.cf	Fri Nov 20 11:58:00 2015
@@ -1,4 +1,4 @@
-#	$NetBSD: genassym.cf,v 1.59 2015/08/26 03:00:53 uebayasi Exp $
+#	$NetBSD: genassym.cf,v 1.60 2015/11/20 11:58:00 maxv Exp $
 
 #
 # Copyright (c) 1998, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -145,6 +145,8 @@ define	L1_SLOT_KERNBASE	pl1_pi(KERNBASE)
 
 define	PDIR_SLOT_PTE		PDIR_SLOT_PTE
 
+define	PDE_SIZE		sizeof(pd_entry_t)
+
 define	VM_MAXUSER_ADDRESS	(unsigned long long)VM_MAXUSER_ADDRESS
 
 define	L_PCB			offsetof(struct lwp, l_addr)

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.79 src/sys/arch/amd64/amd64/locore.S:1.80
--- src/sys/arch/amd64/amd64/locore.S:1.79	Sat Nov 14 14:01:23 2015
+++ src/sys/arch/amd64/amd64/locore.S	Fri Nov 20 11:58:00 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.79 2015/11/14 14:01:23 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.80 2015/11/20 11:58:00 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -247,9 +247,7 @@ _C_LABEL(lapic_isr):
 END(lapic_isr)
 #endif
 
-	.globl	_C_LABEL(cpu_id)
 	.globl	_C_LABEL(cpu_vendorname)
-	.globl	_C_LABEL(cpu_brand_id)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
 	.globl	_C_LABEL(eblob)
@@ -265,9 +263,6 @@ END(lapic_isr)
 	.type	_C_LABEL(cputype), @object
 LABEL(cputype)		.long	0	# are we 80486, Pentium, or..
 END(cputype)
-	.type	_C_LABEL(cpu_id), @object
-LABEL(cpu_id)		.long	0	# saved from `cpuid' instruction
-END(cpu_id)
 	.type	_C_LABEL(cpuid_level), @object
 LABEL(cpuid_level)	.long	-1	# max. level accepted by 'cpuid'
 	#   instruction
@@ -276,9 +271,6 @@ END(cpuid_level)
 LABEL(cpu_vendorname)	.space	16	# vendor string returned by `cpuid'
 	#   instruction
 END(cpu_vendorname)
-	.type	_C_LABEL(cpu_brand_id), @object
-LABEL(cpu_brand_id)	.long	0	# brand ID from 'cpuid' instruction
-END(cpu_brand_id)
 	.type	_C_LABEL(esym), @object
 LABEL(esym)		.quad	0	# ptr to end of syms
 END(esym)
@@ -460,14 +452,6 @@ ENTRY(start)
 	movl	%ecx,8(%ebp)
 	movl	$0, 12(%ebp)
 
-	movl	$1,%eax
-	cpuid
-	movl	%eax,RELOC(cpu_id)
-
-	/* Brand ID is bits 0-7 of %ebx */
-	andl	$255,%ebx
-	movl	%ebx,RELOC(cpu_brand_id)
-
 	/*
 	 * 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
@@ -511,10 +495,19 @@ ENTRY(start)
   ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1 + UPAGES) \
 * PAGE_SIZE)
 
+/*
+ * fillkpt - Fill in a kernel page table
+ *	eax = pte (page frame | control | status)
+ *	ebx = page table address
+ *	ecx = number of pages to map
+ *
+ * Each entry is 8 (PDE_SIZE) bytes long: we must set the 4 upper bytes to 0.
+ */
+
 #define fillkpt	\
 1:	movl	%eax,(%ebx)		; 	/* store phys addr */	\
-	movl	$0,4(%ebx)		; 	/* upper 32 bits 0 */	\
-	addl	$8,%ebx			; 	/* next pte/pde */	\
+	movl	$0,(PDE_SIZE-4)(%ebx)	; 	/* upper 32 bits 0 */	\
+	addl	$PDE_SIZE,%ebx		; 	/* next pte/pde */	\
 	addl	$PAGE_SIZE,%eax		; 	/* next phys page */	\
 	loop	1b			;
 
@@ -567,7 +560,7 @@ ENTRY(start)
 	 */
 	movl	$(KERNTEXTOFF_LO - KERNBASE_LO),%eax
 	movl	%eax,%ecx
-	shrl	$(PGSHIFT-3),%ecx	/* ((n >> PGSHIFT) << 3) for # pdes */
+	shrl	$(PGSHIFT-3),%ecx	/* ((n >> PGSHIFT) << 3) for # PDEs */
 	addl	%ecx,%ebx
 
 	/* Map the kernel text read-only. */
@@ -580,14 +573,14 @@ ENTRY(start)
 	/* Map the data, BSS, and bootstrap tables read-write. */
 	leal	(PG_V|PG_KW)(%edx),%eax
 	movl	$TABLESIZE,%ecx
-	addl	%esi,%ecx# end of tables
-	subl	%edx,%ecx# subtract end of text
+	addl	%esi,%ecx		/* end of tables */
+	subl	%edx,%ecx		/* subtract end of text */
 	shrl	$PGSHIFT,%ecx
 	fillkpt
 
 	/* Map ISA I/O mem (later atdevbase) */
-	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax	# having these bits set
-	movl	$(IOM_SIZE>>PGSHIFT),%ecx		# for this many pte s,
+	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
+	movl	$(IOM_SIZE>>PGSHIFT),%ecx
 	fillkpt
 
 /*



CVS commit: src/sys/arch/amd64/amd64

2015-11-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 22 13:41:24 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: amd64_trap.S autoconf.c bios32.c gdt.c
kgdb_machdep.c linux32_sigcode.S machdep.c mptramp.S
netbsd32_machdep.c spl.S trap.c vector.S

Log Message:
KNF a bit, so I don't get scared each time I open a file


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/amd64/amd64_trap.S \
src/sys/arch/amd64/amd64/linux32_sigcode.S
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/amd64/amd64/autoconf.c
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/amd64/bios32.c
cvs rdiff -u -r1.25 -r1.26 src/sys/arch/amd64/amd64/gdt.c
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/amd64/amd64/kgdb_machdep.c
cvs rdiff -u -r1.213 -r1.214 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/amd64/mptramp.S
cvs rdiff -u -r1.93 -r1.94 src/sys/arch/amd64/amd64/netbsd32_machdep.c
cvs rdiff -u -r1.29 -r1.30 src/sys/arch/amd64/amd64/spl.S
cvs rdiff -u -r1.80 -r1.81 src/sys/arch/amd64/amd64/trap.c
cvs rdiff -u -r1.44 -r1.45 src/sys/arch/amd64/amd64/vector.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/amd64_trap.S
diff -u src/sys/arch/amd64/amd64/amd64_trap.S:1.2 src/sys/arch/amd64/amd64/amd64_trap.S:1.3
--- src/sys/arch/amd64/amd64/amd64_trap.S:1.2	Wed Feb 12 19:53:49 2014
+++ src/sys/arch/amd64/amd64/amd64_trap.S	Sun Nov 22 13:41:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: amd64_trap.S,v 1.2 2014/02/12 19:53:49 dsl Exp $	*/
+/*	$NetBSD: amd64_trap.S,v 1.3 2015/11/22 13:41:24 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2007, 2008 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
 
 #if 0
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.2 2014/02/12 19:53:49 dsl Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.3 2015/11/22 13:41:24 maxv Exp $");
 #endif
 
 /*
@@ -144,7 +144,7 @@ IDTVEC(trap03)
 #else
 	ZTRAP_NJ(T_BPTFLT)
 	INTRENTRY
-  	STI(si)
+	STI(si)
 	/*
 	 * DTrace Function Boundary Trace (fbt) probes are triggered
 	 * by int3 (0xcc).
@@ -378,7 +378,7 @@ END(trap_return_fault_return)
  */
 NENTRY(alltraps)
 	INTRENTRY
-  	STI(si)
+	STI(si)
 
 calltrap:
 #ifdef DIAGNOSTIC
@@ -393,11 +393,11 @@ calltrap:
 .Lalltraps_checkast:
 	movq	CPUVAR(CURLWP),%r14
 	/* Check for ASTs on exit to user mode. */
-  	CLI(si)
+	CLI(si)
 	CHECK_ASTPENDING(%r14)
 	je	3f
 	CLEAR_ASTPENDING(%r14)
-  	STI(si)
+	STI(si)
 	movl	$T_ASTFLT,TF_TRAPNO(%rsp)
 	movq	%rsp,%rdi
 	incq	CPUVAR(NTRAP)
@@ -411,7 +411,7 @@ calltrap:
 6:	cmpl	CPUVAR(ILEVEL),%ebx
 	jne	3f
 	INTRFASTEXIT
-3:  	STI(si)
+3:	STI(si)
 	movabsq	$4f,%rdi
 	movl	CPUVAR(ILEVEL),%esi
 	movl	%ebx,%edx
Index: src/sys/arch/amd64/amd64/linux32_sigcode.S
diff -u src/sys/arch/amd64/amd64/linux32_sigcode.S:1.2 src/sys/arch/amd64/amd64/linux32_sigcode.S:1.3
--- src/sys/arch/amd64/amd64/linux32_sigcode.S:1.2	Mon Aug  1 22:21:01 2011
+++ src/sys/arch/amd64/amd64/linux32_sigcode.S	Sun Nov 22 13:41:24 2015
@@ -1,28 +1,28 @@
-/*	$NetBSD: linux32_sigcode.S,v 1.2 2011/08/01 22:21:01 joerg Exp $ */
+/*	$NetBSD: linux32_sigcode.S,v 1.3 2015/11/22 13:41:24 maxv Exp $ */
 
 #include "assym.h"
 #include 
 
 	.code32
 NENTRY(linux32_sigcode)
-	 call*LINUX32_SF_HANDLER(%esp)
-	 lealLINUX32_SF_SC(%esp),%ebx # scp
-	 pushl   %eax
-	 movl$LINUX32_SYS_sigreturn,%eax
-	 int $0x80
-	 movl$LINUX32_SYS_exit,%eax
-	 int $0x80
-  
-	.balign 16
+	call	*LINUX32_SF_HANDLER(%esp)
+	leal	LINUX32_SF_SC(%esp),%ebx	# scp
+	pushl	%eax
+	movl	$LINUX32_SYS_sigreturn,%eax
+	int	$0x80
+	movl	$LINUX32_SYS_exit,%eax
+	int	$0x80
+
+	.balign	16
 NENTRY(linux32_rt_sigcode)
-	 call*LINUX32_RT_SF_HANDLER(%esp)
-	 lealLINUX32_RT_SF_UC(%esp),%ebx # scp
-	 pushl   %eax
-	 movl$LINUX32_SYS_rt_sigreturn,%eax
-	 int $0x80
-	 movl$LINUX32_SYS_exit,%eax
-	 int $0x80
-	.balign 16
-	 .globl  _C_LABEL(linux32_esigcode)
-_C_LABEL(linux32_esigcode): 
+	call	*LINUX32_RT_SF_HANDLER(%esp)
+	leal	LINUX32_RT_SF_UC(%esp),%ebx	# scp
+	pushl	%eax
+	movl	$LINUX32_SYS_rt_sigreturn,%eax
+	int	$0x80
+	movl	$LINUX32_SYS_exit,%eax
+	int	$0x80
+	.balign	16
+	.globl	_C_LABEL(linux32_esigcode)
+_C_LABEL(linux32_esigcode):
 

Index: src/sys/arch/amd64/amd64/autoconf.c
diff -u src/sys/arch/amd64/amd64/autoconf.c:1.24 src/sys/arch/amd64/amd64/autoconf.c:1.25
--- src/sys/arch/amd64/amd64/autoconf.c:1.24	Thu Nov  8 02:00:44 2012
+++ src/sys/arch/amd64/amd64/autoconf.c	Sun Nov 22 13:41:24 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: autoconf.c,v 1.24 2012/11/08 02:00:44 chs Exp $	*/
+/*	$NetBSD: autoconf.c,v 1.25 2015/11/22 13:41:24 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -46,7 +46,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.24 2012/11/08 02:00:44 chs Exp $");
+__KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.25 2015/11/22 13:41:24 maxv Exp $");
 
 #include 

CVS commit: src/sys/arch/amd64/amd64

2015-11-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 22 14:06:08 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Remove cpu_vendorname (unused). It is retrieved later in identcpu.c.


To generate a diff of this commit:
cvs rdiff -u -r1.81 -r1.82 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.81 src/sys/arch/amd64/amd64/locore.S:1.82
--- src/sys/arch/amd64/amd64/locore.S:1.81	Sun Nov 22 10:18:59 2015
+++ src/sys/arch/amd64/amd64/locore.S	Sun Nov 22 14:06:08 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.81 2015/11/22 10:18:59 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.82 2015/11/22 14:06:08 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -247,7 +247,6 @@ _C_LABEL(lapic_isr):
 END(lapic_isr)
 #endif
 
-	.globl	_C_LABEL(cpu_vendorname)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
 	.globl	_C_LABEL(eblob)
@@ -267,10 +266,6 @@ END(cputype)
 LABEL(cpuid_level)	.long	-1	# max. level accepted by 'cpuid'
 	#   instruction
 END(cpuid_level)
-	.type	_C_LABEL(cpu_vendorname), @object
-LABEL(cpu_vendorname)	.space	16	# vendor string returned by `cpuid'
-	#   instruction
-END(cpu_vendorname)
 	.type	_C_LABEL(esym), @object
 LABEL(esym)		.quad	0	# ptr to end of syms
 END(esym)
@@ -472,11 +467,6 @@ bootinfo_finished:
 	xorl	%eax,%eax
 	cpuid
 	movl	%eax,RELOC(cpuid_level)
-	movl	$RELOC(cpu_vendorname),%ebp
-	movl	%ebx,(%ebp)
-	movl	%edx,4(%ebp)
-	movl	%ecx,8(%ebp)
-	movl	$0, 12(%ebp)
 
 	/*
 	 * Finished with old stack; load new %esp now instead of later so we



CVS commit: src/sys/arch/amd64/amd64

2015-11-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 22 10:19:00 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Clarify:
 - add some comments
 - rename some jumps
 - KNF
No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.80 -r1.81 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.80 src/sys/arch/amd64/amd64/locore.S:1.81
--- src/sys/arch/amd64/amd64/locore.S:1.80	Fri Nov 20 11:58:00 2015
+++ src/sys/arch/amd64/amd64/locore.S	Sun Nov 22 10:18:59 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.80 2015/11/20 11:58:00 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.81 2015/11/22 10:18:59 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -357,70 +357,94 @@ tmpstk:
 ENTRY(start)
 #ifndef XEN
 	.code32
-	movw	$0x1234,0x472			# warm boot
+
+	/* Warm boot */
+	movw	$0x1234,0x472
+
 	/*
-	 * Load parameters from stack
-	 * (howto, [bootdev], bootinfo, esym, basemem, extmem).
+	 * Load parameters from the stack (32 bits):   
+	 * boothowto, [bootdev], bootinfo, esym, basemem, extmem
+	 * We are not interested in 'bootdev'.
 	 */
+
+	/* Load 'boothowto' */
 	movl	4(%esp),%eax
 	movl	%eax,RELOC(boothowto)
+
+	/* Load 'bootinfo' */
 	movl	12(%esp),%eax
-	testl	%eax, %eax
-	jz	1f
-	movl	(%eax), %ebx		/* number of entries */
+	testl	%eax,%eax		/* bootinfo = NULL? */
+	jz	bootinfo_finished
+
+	movl	(%eax),%ebx		/* number of entries */
 	movl	$RELOC(bootinfo),%ebp
-	movl	%ebp, %edx
+	movl	%ebp,%edx
 	addl	$BOOTINFO_MAXSIZE,%ebp
-	movl	%ebx, (%edx)
-	addl	$4, %edx
-2:
-	testl	%ebx, %ebx
-	jz	1f
-	addl	$4, %eax
-	movl	(%eax), %ecx		/* address of entry */
+	movl	%ebx,(%edx)
+	addl	$4,%edx
+
+bootinfo_entryloop:
+	testl	%ebx,%ebx		/* no remaining entries? */
+	jz	bootinfo_finished
+
+	addl	$4,%eax
+	movl	(%eax),%ecx		/* address of entry */
 	pushl	%edi
 	pushl	%esi
 	pushl	%eax
 
-	movl	(%ecx),%eax	/* len */
+	movl	(%ecx),%eax		/* size of entry */
 	movl	%edx,%edi
-	addl	(%ecx), %edx		/* update dest pointer */
-	cmpl	%ebp, %edx
-	jg	2f
+	addl	(%ecx),%edx		/* update dest pointer */
+	cmpl	%ebp,%edx		/* beyond bootinfo+BOOTINFO_MAXSIZE? */
+	jg	bootinfo_overflow
+
 	movl	%ecx,%esi
 	movl	%eax,%ecx
+
 	/*
-	 * If any modules were loaded, record where they
-	 * end.  We'll need to skip over them.
+	 * If any modules were loaded, record where they end.  We'll need to
+	 * skip over them.
 	 */
-	cmpl	$BTINFO_MODULELIST, 4(%esi)
+	cmpl	$BTINFO_MODULELIST,4(%esi)
 	jne	0f
+
 	pushl	12(%esi)		/* endpa */
 	popl	RELOC(eblob)
-	addl	$KERNBASE_LO, RELOC(eblob)
-	adcl	$KERNBASE_HI, RELOC(eblob)+4
+	addl	$KERNBASE_LO,RELOC(eblob)
+	adcl	$KERNBASE_HI,RELOC(eblob)+4
+
 0:
 	rep
-	movsb
+	movsb/* copy esi -> edi */
 	popl	%eax
 	popl	%esi
 	popl	%edi
-	subl	$1, %ebx
-	jmp	2b
-2:	/* cleanup for overflow case */
+	subl	$1,%ebx			/* decrement the # of entries */
+	jmp	bootinfo_entryloop
+
+bootinfo_overflow:
+	/*
+	 * Cleanup for overflow case. Pop the registers, and correct the number
+	 * of entries.
+	 */
 	popl	%eax
 	popl	%esi
 	popl	%edi
 	movl	$RELOC(bootinfo),%ebp
-	movl	%ebp, %edx
-	subl	%ebx, (%edx)		/* correct number of entries */
-1:
+	movl	%ebp,%edx
+	subl	%ebx,(%edx)		/* correct the number of entries */
 
+bootinfo_finished:
+	/* Load 'esym' */
 	movl	16(%esp),%eax
-	testl	%eax,%eax
+	testl	%eax,%eax		/* esym = NULL? */
 	jz	1f
+
 	addl	$KERNBASE_LO,%eax
-1: 	movl	$RELOC(esym),%ebp
+
+1:
+	movl	$RELOC(esym),%ebp
 	movl	%eax,(%ebp)
 	movl	$KERNBASE_HI,4(%ebp)
 
@@ -428,8 +452,10 @@ ENTRY(start)
 	movl	(%ebp),%eax
 	testl	%eax,%eax
 	jnz	1f
+
 	movl	20(%esp),%eax
 	movl	%eax,(%ebp)
+
 1:
 	movl	$RELOC(biosbasemem),%ebp
 	movl	(%ebp),%eax
@@ -470,7 +496,7 @@ ENTRY(start)
  * Virtual address space of kernel:
  *
  * text | data | bss | [syms] | page dir | proc0 kstack | L1 ptp | L2 ptp | L3
- *			  0  1   2  3
+ *0  1   2  3
  */
 
 #if L2_SLOT_KERNBASE > 0
@@ -485,7 +511,6 @@ ENTRY(start)
 #define TABLE_L3_ENTRIES NKL3_KIMG_ENTRIES
 #endif
 
-
 #define PROC0_PML4_OFF	0
 #define PROC0_STK_OFF	(PROC0_PML4_OFF + PAGE_SIZE)
 #define PROC0_PTP3_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
@@ -507,7 +532,7 @@ ENTRY(start)
 #define fillkpt	\
 1:	movl	%eax,(%ebx)		; 	/* store phys addr */	\
 	movl	$0,(PDE_SIZE-4)(%ebx)	; 	/* upper 32 bits 0 */	\
-	addl	$PDE_SIZE,%ebx		; 	/* next pte/pde */	\
+	addl	$PDE_SIZE,%ebx		; 	/* next PTE/PDE */	\
 	addl	$PAGE_SIZE,%eax		; 	/* next phys page */	\
 	loop	1b			;
 
@@ -529,12 +554,13 @@ ENTRY(start)
 	subl	$KERNBASE_LO,%eax	/* XXX */
 	movl	%eax,%edi
 1:
+
 	/* Compute sizes */
 	movl	%edi,%esi
-	addl	$PGOFSET,%esi			# page align up
+	addl	$PGOFSET,%esi		/* page align up */
 	andl	$~PGOFSET,%esi
-
 	movl	%esi,%edi
+
 	xorl	%eax,%eax
 	cld
 	movl	$TABLESIZE,%ecx
@@ -542,11 +568,17 @@ 

CVS commit: src/sys/arch/amd64/amd64

2015-11-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov 21 12:34:48 UTC 2015

Modified Files:
src/sys/arch/amd64/amd64: copy.S

Log Message:
Remove the amd64 implementation of fuword and suword. They are not used in
the MI+amd64 code - Christos replaced them yesterday by copy*.

They are both buggy:
 - suword does not properly check the userspace limit: 64 bits are copied,
   but the max address checked is VM_MAXUSER_ADDRESS-4, which means that 4
   bytes may overflow. Reported by Ed Schouten.
 - fuword is supposed to be symmetrical with suword. But it uses 32bit
   registers, so it stores 32bit values! Spotted by Chuck (chs@).


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/amd64/copy.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/copy.S
diff -u src/sys/arch/amd64/amd64/copy.S:1.18 src/sys/arch/amd64/amd64/copy.S:1.19
--- src/sys/arch/amd64/amd64/copy.S:1.18	Wed Jul  7 01:13:29 2010
+++ src/sys/arch/amd64/amd64/copy.S	Sat Nov 21 12:34:48 2015
@@ -1,4 +1,4 @@
-/*	$NetBSD: copy.S,v 1.18 2010/07/07 01:13:29 chs Exp $	*/
+/*	$NetBSD: copy.S,v 1.19 2015/11/21 12:34:48 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -359,19 +359,6 @@ ENTRY(copystr)
 
 7:	ret
 
-ENTRY(fuword)
-	DEFERRED_SWITCH_CHECK
-	movq	$VM_MAXUSER_ADDRESS-4,%r11
-	cmpq	%r11,%rdi
-	ja	_C_LABEL(fusuaddrfault)
-	GET_CURPCB(%rcx)
-	leaq	_C_LABEL(fusufailure)(%rip),%r11
-	movq	%r11,PCB_ONFAULT(%rcx)
-	movl	(%rdi),%eax
-	movq	$0,PCB_ONFAULT(%rcx)
-	ret
-	DEFERRED_SWITCH_CALL
-	
 ENTRY(fusword)
 	DEFERRED_SWITCH_CHECK
 	movq	$VM_MAXUSER_ADDRESS-2,%r11
@@ -411,22 +398,6 @@ ENTRY(fubyte)
 	ret
 	DEFERRED_SWITCH_CALL
 
-ENTRY(suword)
-	DEFERRED_SWITCH_CHECK
-	movq	$VM_MAXUSER_ADDRESS-4,%r11
-	cmpq	%r11,%rdi
-	ja	_C_LABEL(fusuaddrfault)
-
-	GET_CURPCB(%rcx)
-	leaq	_C_LABEL(fusufailure)(%rip),%r11
-	movq	%r11,PCB_ONFAULT(%rcx)
-
-	movq	%rsi,(%rdi)
-	xorq	%rax,%rax
-	movq	%rax,PCB_ONFAULT(%rcx)
-	ret
-	DEFERRED_SWITCH_CALL
-	
 ENTRY(susword)
 	DEFERRED_SWITCH_CHECK
 	movq	$VM_MAXUSER_ADDRESS-2,%r11



CVS commit: src/sys/lib/libsa

2016-06-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jun  5 13:44:48 UTC 2016

Modified Files:
src/sys/lib/libsa: alloc.c

Log Message:
Remove the ALLOC_FIRST_FIT and ALLOC_TRACE options. This is a rather simple
allocator, and it does not need to be that complicated.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/lib/libsa/alloc.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/lib/libsa/alloc.c
diff -u src/sys/lib/libsa/alloc.c:1.26 src/sys/lib/libsa/alloc.c:1.27
--- src/sys/lib/libsa/alloc.c:1.26	Sat Jul 30 03:43:20 2011
+++ src/sys/lib/libsa/alloc.c	Sun Jun  5 13:44:48 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: alloc.c,v 1.26 2011/07/30 03:43:20 jakllsch Exp $	*/
+/*	$NetBSD: alloc.c,v 1.27 2016/06/05 13:44:48 maxv Exp $	*/
 
 /*
  * Copyright (c) 1993
@@ -103,11 +103,6 @@
  *
  * Compile options:
  *
- *	ALLOC_TRACE	enable tracing of allocations/deallocations
-
- *	ALLOC_FIRST_FIT	use a first-fit allocation algorithm, rather than
- *			the default best-fit algorithm.
- *
  *	HEAP_LIMIT	heap limit address (defaults to "no limit").
  *
  *	HEAP_START	start address of heap (defaults to '').
@@ -138,6 +133,9 @@
  * However, note that ALIGN(sizeof(unsigned int)) + ALIGN(data size) must
  * be at least 'sizeof(struct fl)', so that blocks can be used as structures
  * when on the free list.
+ *
+ * When HEAP_LIMIT is defined and the heap limit is reached, alloc() panics.
+ * Otherwise, it never fails.
  */
 struct fl {
 	unsigned int	size;
@@ -166,22 +164,10 @@ __compactcall void *
 alloc(size_t size)
 {
 	struct fl **f = , **bestf = NULL;
-#ifndef ALLOC_FIRST_FIT
 	unsigned int bestsize = 0x;	/* greater than any real size */
-#endif
 	char *help;
 	int failed;
 
-#ifdef ALLOC_TRACE
-	printf("alloc(%zu)", size);
-#endif
-
-#ifdef ALLOC_FIRST_FIT
-	while (*f != (struct fl *)0 && (size_t)(*f)->size < size)
-		f = &((*f)->next);
-	bestf = f;
-	failed = (*bestf == (struct fl *)0);
-#else
 	/* scan freelist */
 	while (*f) {
 		if ((size_t)(*f)->size >= size) {
@@ -199,7 +185,6 @@ alloc(size_t size)
 
 	/* no match in freelist if bestsize unchanged */
 	failed = (bestsize == 0x);
-#endif
 
 	if (failed) { /* nothing found */
 		/*
@@ -217,25 +202,16 @@ alloc(size_t size)
 			panic("heap full (%p+%zu)", help, size);
 #endif
 		*(unsigned int *)(void *)help = (unsigned int)ALIGN(size);
-#ifdef ALLOC_TRACE
-		printf("=%lx\n", (u_long)help + ALIGN(sizeof(unsigned int)));
-#endif
 		return help + ALIGN(sizeof(unsigned int));
 	}
 
 	/* we take the best fit */
 	f = bestf;
 
-#ifndef ALLOC_FIRST_FIT
 found:
-#endif
 	/* remove from freelist */
 	help = (char *)(void *)*f;
 	*f = (*f)->next;
-#ifdef ALLOC_TRACE
-	printf("=%lx (origsize %u)\n",
-	(u_long)help + ALIGN(sizeof(unsigned int)), *(unsigned int *)help);
-#endif
 	return help + ALIGN(sizeof(unsigned int));
 }
 
@@ -246,9 +222,6 @@ dealloc(void *ptr, size_t size)
 	struct fl *f =
 	(struct fl *)(void *)((char *)(void *)ptr -
 	ALIGN(sizeof(unsigned int)));
-#ifdef ALLOC_TRACE
-	printf("dealloc(%lx, %zu) (origsize %u)\n", (u_long)ptr, size, f->size);
-#endif
 #ifdef DEBUG
 	if (size > (size_t)f->size) {
 		printf("dealloc %zu bytes @%lx, should be <=%u\n",



CVS commit: src/sys/arch/i386/stand/lib

2016-06-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jun  5 14:06:31 UTC 2016

Modified Files:
src/sys/arch/i386/stand/lib: biosdisk.c exec.c

Log Message:
The bootinfo is refreshed each time the bootloader tries to execute a
kernel, so there's no point in using this global variable. Because of
this variable, only one "boot" command can be issued in the prompt, and
you have to reboot the machine if you mistyped the kernel name.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/arch/i386/stand/lib/biosdisk.c
cvs rdiff -u -r1.59 -r1.60 src/sys/arch/i386/stand/lib/exec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/stand/lib/biosdisk.c
diff -u src/sys/arch/i386/stand/lib/biosdisk.c:1.44 src/sys/arch/i386/stand/lib/biosdisk.c:1.45
--- src/sys/arch/i386/stand/lib/biosdisk.c:1.44	Sun Jan 18 20:18:07 2015
+++ src/sys/arch/i386/stand/lib/biosdisk.c	Sun Jun  5 14:06:31 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: biosdisk.c,v 1.44 2015/01/18 20:18:07 jakllsch Exp $	*/
+/*	$NetBSD: biosdisk.c,v 1.45 2016/06/05 14:06:31 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1998
@@ -701,24 +701,13 @@ biosdisk_findpartition(int biosdev, dadd
 static void
 add_biosdisk_bootinfo(void)
 {
-	static bool done;
-
 	if (bootinfo == NULL) {
-		done = false;
 		return;
 	}
-	
-	if (done)
-		return;
-
 	BI_ADD(_disk, BTINFO_BOOTDISK, sizeof(bi_disk));
 	BI_ADD(_wedge, BTINFO_BOOTWEDGE, sizeof(bi_wedge));
-
-	done = true;
-
 	return;
 }
-
 #endif
 
 int

Index: src/sys/arch/i386/stand/lib/exec.c
diff -u src/sys/arch/i386/stand/lib/exec.c:1.59 src/sys/arch/i386/stand/lib/exec.c:1.60
--- src/sys/arch/i386/stand/lib/exec.c:1.59	Sun Apr  6 19:18:00 2014
+++ src/sys/arch/i386/stand/lib/exec.c	Sun Jun  5 14:06:31 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec.c,v 1.59 2014/04/06 19:18:00 jakllsch Exp $	 */
+/*	$NetBSD: exec.c,v 1.60 2016/06/05 14:06:31 maxv Exp $	 */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -411,7 +411,7 @@ exec_netbsd(const char *file, physaddr_t
 
 out:
 	BI_FREE();
-	bootinfo = 0;
+	bootinfo = NULL;
 	return -1;
 }
 



CVS commit: src/sys/arch/i386/stand/lib

2016-06-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jun  5 14:13:57 UTC 2016

Modified Files:
src/sys/arch/i386/stand/lib: bootinfo.c bootinfo.h exec.c

Log Message:
Don't use a magic value. Define a limit, and enforce it.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/i386/stand/lib/bootinfo.c
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/i386/stand/lib/bootinfo.h
cvs rdiff -u -r1.60 -r1.61 src/sys/arch/i386/stand/lib/exec.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/stand/lib/bootinfo.c
diff -u src/sys/arch/i386/stand/lib/bootinfo.c:1.5 src/sys/arch/i386/stand/lib/bootinfo.c:1.6
--- src/sys/arch/i386/stand/lib/bootinfo.c:1.5	Sun Dec 14 18:46:33 2008
+++ src/sys/arch/i386/stand/lib/bootinfo.c	Sun Jun  5 14:13:57 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootinfo.c,v 1.5 2008/12/14 18:46:33 christos Exp $	*/
+/*	$NetBSD: bootinfo.c,v 1.6 2016/06/05 14:13:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997
@@ -40,6 +40,11 @@ bi_add(struct btinfo_common *what, int t
 	what->len = size;
 	what->type = type;
 
-	if (bootinfo)
-		bootinfo->entry[bootinfo->nentries++] = vtophys(what);
+	if (bootinfo == NULL) {
+		return;
+	}
+	if (bootinfo->nentries >= BTINFO_MAX) {
+		panic("bootinfo too big");
+	}
+	bootinfo->entry[bootinfo->nentries++] = vtophys(what);
 }

Index: src/sys/arch/i386/stand/lib/bootinfo.h
diff -u src/sys/arch/i386/stand/lib/bootinfo.h:1.10 src/sys/arch/i386/stand/lib/bootinfo.h:1.11
--- src/sys/arch/i386/stand/lib/bootinfo.h:1.10	Mon Nov 18 03:52:45 2013
+++ src/sys/arch/i386/stand/lib/bootinfo.h	Sun Jun  5 14:13:57 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootinfo.h,v 1.10 2013/11/18 03:52:45 jakllsch Exp $	*/
+/*	$NetBSD: bootinfo.h,v 1.11 2016/06/05 14:13:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997
@@ -35,6 +35,8 @@ struct bootinfo {
 
 extern struct bootinfo *bootinfo;
 
+#define BTINFO_MAX	32
+
 #define BI_ALLOC(max) (bootinfo = alloc(sizeof(struct bootinfo) \
 + ((max) - 1) * sizeof(uint32_t))) \
   ->nentries = 0

Index: src/sys/arch/i386/stand/lib/exec.c
diff -u src/sys/arch/i386/stand/lib/exec.c:1.60 src/sys/arch/i386/stand/lib/exec.c:1.61
--- src/sys/arch/i386/stand/lib/exec.c:1.60	Sun Jun  5 14:06:31 2016
+++ src/sys/arch/i386/stand/lib/exec.c	Sun Jun  5 14:13:57 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: exec.c,v 1.60 2016/06/05 14:06:31 maxv Exp $	 */
+/*	$NetBSD: exec.c,v 1.61 2016/06/05 14:13:57 maxv Exp $	 */
 
 /*-
  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
@@ -356,7 +356,7 @@ exec_netbsd(const char *file, physaddr_t
 	   file ? file : "NULL", loadaddr);
 #endif
 
-	BI_ALLOC(32); /* ??? */
+	BI_ALLOC(BTINFO_MAX);
 
 	BI_ADD(_console, BTINFO_CONSOLE, sizeof(struct btinfo_console));
 



CVS commit: src/sys

2016-06-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jun  5 13:33:03 UTC 2016

Modified Files:
src/sys/arch/i386/stand/lib: bootmenu.c menuutils.c
src/sys/lib/libsa: gets.c stand.h

Log Message:
Use gets_s instead of gets. The x86 bootloader prompt is easy to
overflow.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/i386/stand/lib/bootmenu.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/i386/stand/lib/menuutils.c
cvs rdiff -u -r1.10 -r1.11 src/sys/lib/libsa/gets.c
cvs rdiff -u -r1.79 -r1.80 src/sys/lib/libsa/stand.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/stand/lib/bootmenu.c
diff -u src/sys/arch/i386/stand/lib/bootmenu.c:1.14 src/sys/arch/i386/stand/lib/bootmenu.c:1.15
--- src/sys/arch/i386/stand/lib/bootmenu.c:1.14	Sun Aug 10 07:40:49 2014
+++ src/sys/arch/i386/stand/lib/bootmenu.c	Sun Jun  5 13:33:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: bootmenu.c,v 1.14 2014/08/10 07:40:49 isaki Exp $	*/
+/*	$NetBSD: bootmenu.c,v 1.15 2016/06/05 13:33:03 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -178,7 +178,7 @@ doboottypemenu(void)
 printf("\nOption: [%d]:",
 bootcfg_info.def + 1);
 
-			gets(input);
+			gets_s(input, sizeof(input));
 			choice = getchoicefrominput(input, bootcfg_info.def);
 		} else if (bootcfg_info.timeout == 0)
 			choice = bootcfg_info.def;

Index: src/sys/arch/i386/stand/lib/menuutils.c
diff -u src/sys/arch/i386/stand/lib/menuutils.c:1.4 src/sys/arch/i386/stand/lib/menuutils.c:1.5
--- src/sys/arch/i386/stand/lib/menuutils.c:1.4	Sun Apr  6 19:11:26 2014
+++ src/sys/arch/i386/stand/lib/menuutils.c	Sun Jun  5 13:33:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: menuutils.c,v 1.4 2014/04/06 19:11:26 jakllsch Exp $	*/
+/*	$NetBSD: menuutils.c,v 1.5 2016/06/05 13:33:03 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997
@@ -71,7 +71,7 @@ bootmenu(void)
 
 		input[0] = '\0';
 		printf("> ");
-		gets(input);
+		gets_s(input, sizeof(input));
 
 		/*
 		 * Skip leading whitespace.

Index: src/sys/lib/libsa/gets.c
diff -u src/sys/lib/libsa/gets.c:1.10 src/sys/lib/libsa/gets.c:1.11
--- src/sys/lib/libsa/gets.c:1.10	Sat Nov 24 13:20:55 2007
+++ src/sys/lib/libsa/gets.c	Sun Jun  5 13:33:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: gets.c,v 1.10 2007/11/24 13:20:55 isaki Exp $	*/
+/*	$NetBSD: gets.c,v 1.11 2016/06/05 13:33:03 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1993
@@ -85,3 +85,61 @@ gets(char *buf)
 	}
 	/*NOTREACHED*/
 }
+
+void
+gets_s(char *buf, size_t size)
+{
+	int c;
+	char *lp;
+
+	for (lp = buf;;) {
+		if (lp - buf == size) {
+			lp--;
+			*lp = '\0';
+			return;
+		}
+		switch (c = getchar() & 0177) {
+		case '\n':
+		case '\r':
+			*lp = '\0';
+			putchar('\n');
+			return;
+		case '\b':
+		case '\177':
+			if (lp > buf) {
+lp--;
+putchar('\b');
+putchar(' ');
+putchar('\b');
+			}
+			break;
+#if HASH_ERASE
+		case '#':
+			if (lp > buf)
+--lp;
+			break;
+#endif
+		case 'r' & 037: {
+			char *p;
+
+			putchar('\n');
+			for (p = buf; p < lp; ++p)
+putchar(*p);
+			break;
+		}
+#if AT_ERASE
+		case '@':
+#endif
+		case 'u' & 037:
+		case 'w' & 037:
+			lp = buf;
+			putchar('\n');
+			break;
+		default:
+			*lp++ = c;
+			putchar(c);
+			break;
+		}
+	}
+	/*NOTREACHED*/
+}

Index: src/sys/lib/libsa/stand.h
diff -u src/sys/lib/libsa/stand.h:1.79 src/sys/lib/libsa/stand.h:1.80
--- src/sys/lib/libsa/stand.h:1.79	Sun Aug 10 07:40:49 2014
+++ src/sys/lib/libsa/stand.h	Sun Jun  5 13:33:03 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: stand.h,v 1.79 2014/08/10 07:40:49 isaki Exp $	*/
+/*	$NetBSD: stand.h,v 1.80 2016/06/05 13:33:03 maxv Exp $	*/
 
 /*
  * Copyright (c) 1999 Christopher G. Demetriou.  All rights reserved.
@@ -78,6 +78,7 @@
 #ifdef LIBSA_RENAME_PRINTF
 #define getchar		libsa_getchar
 #define gets		libsa_gets
+#define gets_s		libsa_gets_s
 #define printf		libsa_printf
 #define putchar		libsa_putchar
 #define vprintf		libsa_vprintf
@@ -258,6 +259,7 @@ int	vsnprintf(char *, size_t, const char
 __attribute__((__format__(__printf__, 3, 0)));
 void	twiddle(void);
 void	gets(char *);
+void	gets_s(char *, size_t);
 int	getfile(char *prompt, int mode);
 char	*strerror(int);
 __dead void	exit(int);



CVS commit: src/sys/arch/i386/i386

2016-06-04 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jun  4 09:52:41 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Initialize cpuid_level at compile-time, not run-time. Same as amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.127 -r1.128 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.127 src/sys/arch/i386/i386/locore.S:1.128
--- src/sys/arch/i386/i386/locore.S:1.127	Sat Jun  4 09:45:57 2016
+++ src/sys/arch/i386/i386/locore.S	Sat Jun  4 09:52:41 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.127 2016/06/04 09:45:57 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.128 2016/06/04 09:52:41 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.127 2016/06/04 09:45:57 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.128 2016/06/04 09:52:41 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -317,7 +317,7 @@ END(nox_flag)
 LABEL(cputype)		.long	0	/* are we 80486, Pentium, or.. */
 END(cputype)
 	.type	_C_LABEL(cpuid_level), @object
-LABEL(cpuid_level)	.long	0	/* max. level accepted by cpuid instr */
+LABEL(cpuid_level)	.long	-1	/* max. level accepted by cpuid instr */
 END(cpuid_level)
 	.type	_C_LABEL(atdevbase), @object
 LABEL(atdevbase)	.long	0	/* location of start of iomem in virt */
@@ -402,8 +402,6 @@ _C_LABEL(Multiboot_Header):
 	xorl	%eax,%eax
 	movw	%ax,%fs
 	movw	%ax,%gs
-	decl	%eax
-	movl	%eax,RELOC(cpuid_level)
 
 	/* Find out our CPU type. */
 



CVS commit: src/sys/arch

2016-06-04 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jun  4 10:19:09 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
Use the same instruction layout to map the ISA I/O. No functional
change.

The comment is still wrong: we are not on (4), we are actually below
the kernel area in physical memory. I'll fix that later.


To generate a diff of this commit:
cvs rdiff -u -r1.100 -r1.101 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.129 -r1.130 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.100 src/sys/arch/amd64/amd64/locore.S:1.101
--- src/sys/arch/amd64/amd64/locore.S:1.100	Sat Jun  4 09:45:57 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sat Jun  4 10:19:09 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.100 2016/06/04 09:45:57 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.101 2016/06/04 10:19:09 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -611,9 +611,9 @@ no_NOX:
  * +--++--+-++-+--
  * (1)  (2)   (3)
  *
- * --+-+-+++
- * -> PROC0 STK -> L3 -> L2 -> L1 | ISA IO MEM |
- * --+-+-+++
+ * --+-+-++-+
+ * -> PROC0 STK -> L3 -> L2 -> L1 | ISA I/O MEM |
+ * --+-+-++-+
  *   (4)
  *
  * PROC0 STK is obviously not linked as a page level. It just happens to be
@@ -718,9 +718,11 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
-	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
-	movl	$(IOM_SIZE>>PGSHIFT),%ecx
+	/* We are on (4). Map ISA I/O MEM RWX. */
+	movl	$IOM_BEGIN,%eax
+	movl	$IOM_SIZE,%ecx	/* size of ISA I/O MEM */
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW/*|PG_N*/),%eax
 	fillkpt
 
 	/*

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.129 src/sys/arch/i386/i386/locore.S:1.130
--- src/sys/arch/i386/i386/locore.S:1.129	Sat Jun  4 10:02:12 2016
+++ src/sys/arch/i386/i386/locore.S	Sat Jun  4 10:19:09 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.129 2016/06/04 10:02:12 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.130 2016/06/04 10:19:09 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.129 2016/06/04 10:02:12 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.130 2016/06/04 10:19:09 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -627,9 +627,9 @@ no_NOX:
  * +--++--+-++-+---
  * (1)  (2)   (3)
  *
- * ---++
- * TABLES | ISA IO MEM |
- * ---++
+ * ---+-+
+ * TABLES | ISA I/O MEM |
+ * ---+-+
  *   (4)
  *
  * PROC0 STK is obviously not linked as a page level. It just happens to be
@@ -753,9 +753,11 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
-	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
-	movl	$(IOM_SIZE>>PGSHIFT),%ecx
+	/* We are on (4). Map ISA I/O MEM RWX. */
+	movl	$IOM_BEGIN,%eax
+	movl	$IOM_SIZE,%ecx	/* size of ISA I/O MEM */
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW/*|PG_N*/),%eax
 	fillkpt
 
 	/*



CVS commit: src/sys/arch/i386/i386

2016-06-04 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jun  4 10:02:12 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Define and use fillkpt_blank on i386, like amd64. The PAE case is
included in fillkpt_blank, since PDE_SIZE is either 4 or 8 bytes.


To generate a diff of this commit:
cvs rdiff -u -r1.128 -r1.129 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.128 src/sys/arch/i386/i386/locore.S:1.129
--- src/sys/arch/i386/i386/locore.S:1.128	Sat Jun  4 09:52:41 2016
+++ src/sys/arch/i386/i386/locore.S	Sat Jun  4 10:02:12 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.128 2016/06/04 09:52:41 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.129 2016/06/04 10:02:12 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.128 2016/06/04 09:52:41 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.129 2016/06/04 10:02:12 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -219,6 +219,20 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 2:	;
 
 /*
+ * fillkpt_blank - Fill in a kernel page table with blank entries
+ *	ebx = page table address
+ *	ecx = number of pages to map
+ */
+#define fillkpt_blank	\
+	cmpl	$0,%ecx			;	/* zero-sized? */	\
+	je 	2f			; \
+1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
+	movl	$0,(%ebx)		;	/* lower 32 bits: 0 */	\
+	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
+	loop	1b			; \
+2:	;
+
+/*
  * killkpt - Destroy a kernel page table
  *	ebx = page table address
  *	ecx = number of pages to destroy
@@ -696,15 +710,12 @@ no_NOX:
 	leal	(PROC0_PTP1_OFF)(%esi),%ebx
 
 	/* Skip the first MB. */
-	movl	$(KERNTEXTOFF - KERNBASE),%eax
-	movl	%eax,%ecx
-	shrl	$(PGSHIFT-2),%ecx	/* ((n >> PGSHIFT) << 2) for # PDEs */
-#ifdef PAE
-	shll	$1,%ecx			/* PDEs are twice larger with PAE */
-#endif
-	addl	%ecx,%ebx
+	movl	$(KERNTEXTOFF - KERNBASE),%ecx
+	shrl	$PGSHIFT,%ecx
+	fillkpt_blank
 
 	/* Map the kernel text RX. */
+	movl	$(KERNTEXTOFF - KERNBASE),%eax	/* start of TEXT */
 	movl	$RELOC(__rodata_start),%ecx
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx



CVS commit: src/sys/arch

2016-06-04 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jun  4 09:45:58 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
Reorder some definitions. Reduces the diff between amd64 and i386.


To generate a diff of this commit:
cvs rdiff -u -r1.99 -r1.100 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.126 -r1.127 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.99 src/sys/arch/amd64/amd64/locore.S:1.100
--- src/sys/arch/amd64/amd64/locore.S:1.99	Sun May 29 17:06:17 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sat Jun  4 09:45:57 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.99 2016/05/29 17:06:17 bouyer Exp $	*/
+/*	$NetBSD: locore.S,v 1.100 2016/06/04 09:45:57 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -9,7 +9,7 @@
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Charles M. Hannum and Maxime Villard.
+ * by Charles M. Hannum and by Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -344,17 +344,17 @@ END(lapic_isr)
 
 	.globl	_C_LABEL(tablesize)
 	.globl	_C_LABEL(nox_flag)
+	.globl	_C_LABEL(cputype)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
 	.globl	_C_LABEL(eblob)
-	.globl	_C_LABEL(boothowto)
-	.globl	_C_LABEL(bootinfo)
 	.globl	_C_LABEL(atdevbase)
 	.globl	_C_LABEL(PDPpaddr)
+	.globl	_C_LABEL(boothowto)
+	.globl	_C_LABEL(bootinfo)
 	.globl	_C_LABEL(biosbasemem)
 	.globl	_C_LABEL(biosextmem)
 	.globl	_C_LABEL(gdtstore)
-	.globl	_C_LABEL(cputype)
 
 	.type	_C_LABEL(tablesize), @object
 _C_LABEL(tablesize):	.long	TABLESIZE
@@ -585,7 +585,7 @@ biosbasemem_finished:
 	 * The boot program should check:
 	 *	text+data <= _variable - more_space_for_stack
 	 *	text+data+bss+pad+space_for_page_tables <= end_of_memory
-	 * 
+	 *
 	 * XXX: the gdt is in the carcass of the boot program so clearing
 	 * the rest of memory is still not possible.
 	 */

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.126 src/sys/arch/i386/i386/locore.S:1.127
--- src/sys/arch/i386/i386/locore.S:1.126	Sun May 29 09:16:12 2016
+++ src/sys/arch/i386/i386/locore.S	Sat Jun  4 09:45:57 2016
@@ -1,10 +1,39 @@
-/*	$NetBSD: locore.S,v 1.126 2016/05/29 09:16:12 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.127 2016/06/04 09:45:57 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
  */
 
 /*
+ * Copyright (c) 1998, 2000, 2004, 2006, 2007, 2009, 2016
+ * The NetBSD Foundation, Inc., All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Charles M. Hannum, by Andrew Doran and by Maxime Villard.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
  * Copyright (c) 2006 Manuel Bouyer.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -65,35 +94,6 @@
  */
 
 /*-
- * Copyright (c) 1998, 2000, 2004, 2006, 2007, 2009 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Charles M. Hannum, and by Andrew Doran.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary for

CVS commit: src/sys/arch/amd64/amd64

2016-05-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 28 08:43:17 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Move proc0's stack out of the BOOTSTRAP TABLES, and map it independently
with RW permissions. Reduces the impact of a stack overflow.


To generate a diff of this commit:
cvs rdiff -u -r1.94 -r1.95 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.94 src/sys/arch/amd64/amd64/locore.S:1.95
--- src/sys/arch/amd64/amd64/locore.S:1.94	Thu May 26 07:24:55 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sat May 28 08:43:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.94 2016/05/26 07:24:55 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.95 2016/05/28 08:43:16 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -203,12 +203,11 @@
 #endif
 
 #define PROC0_PML4_OFF	0
-#define PROC0_STK_OFF	(PROC0_PML4_OFF + 1 * PAGE_SIZE)
-#define PROC0_PTP3_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
+#define PROC0_PTP3_OFF	(PROC0_PML4_OFF + 1 * PAGE_SIZE)
 #define PROC0_PTP2_OFF	(PROC0_PTP3_OFF + NKL4_KIMG_ENTRIES * PAGE_SIZE)
 #define PROC0_PTP1_OFF	(PROC0_PTP2_OFF + TABLE_L3_ENTRIES * PAGE_SIZE)
 #define TABLESIZE \
-  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1 + UPAGES) \
+  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1) \
 * PAGE_SIZE)
 
 /*
@@ -308,6 +307,7 @@ _C_LABEL(lapic_isr):
 END(lapic_isr)
 #endif /* NLAPIC > 0 */
 
+	.globl	_C_LABEL(proc0stack)
 	.globl	_C_LABEL(nox_flag)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
@@ -321,6 +321,9 @@ END(lapic_isr)
 	.globl	_C_LABEL(gdtstore)
 	.globl	_C_LABEL(cputype)
 
+	.type	_C_LABEL(proc0stack), @object
+LABEL(proc0stack)	.quad	0	/* physical address of proc0's stack */
+END(proc0stack)
 	.type	_C_LABEL(nox_flag), @object
 LABEL(nox_flag)		.long	0	/* 32bit NOX flag, set if supported */
 END(nox_flag)
@@ -568,20 +571,17 @@ no_NOX:
  * be referred to as: L4 -> L3 -> L2 -> L1.
  *
  * Virtual address space of the kernel:
- * +--++--+-++-+--
- * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | L4 ->
- * +--++--+-++-+--
- * (1)  (2)   (3)
- *
- * --+-+-+++
- * -> PROC0 STK -> L3 -> L2 -> L1 | ISA IO MEM |
- * --+-+-+++
- *   (4)
+ * +--++--+-++-+---+-
+ * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | PROC0 STK |
+ * +--++--+-++-+---+-
+ * (1)  (2)   (3) (4)
+ *
+ * -+-+-+++
+ *  L4 -> L3 -> L2 -> L1 | ISA IO MEM |
+ * -+-+-+++
+ *  (5)
  *
- * PROC0 STK is obviously not linked as a page level. It just happens to be
- * caught between L4 and L3.
- *
- * (PROC0 STK + L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
+ * (L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
  *
  * Important note: the kernel segments are properly 4k-aligned
  * (see kern.ldscript), so there's no need to enforce alignment.
@@ -607,11 +607,17 @@ no_NOX:
 	movl	%eax,%edi
 1:
 
-	/* We are on (3). Align up for BOOTSTRAP TABLES. */
+	/* We are on (3). Align up for PROC0 STK. */
 	movl	%edi,%esi
 	addl	$PGOFSET,%esi
 	andl	$~PGOFSET,%esi
 
+	/* Save proc0's stack pa, and allocate it. Brings us on (4). */
+	movl	$RELOC(proc0stack),%ebp
+	movl	%esi,(%ebp)
+	movl	$0,4(%ebp)
+	addl	$USPACE,%esi
+
 	/* We are on the BOOTSTRAP TABLES. Save L4's physical address. */
 	movl	$RELOC(PDPpaddr),%ebp
 	movl	%esi,(%ebp)
@@ -667,12 +673,20 @@ no_NOX:
 
 	/* Map [SYMS]+[PRELOADED MODULES] RWX. */
 	movl	$RELOC(__kernel_end),%eax
-	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
+	movl	RELOC(proc0stack),%ecx	/* start of PROC0 STK */
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt
 
+	/* Map PROC0 STK RW. */
+	movl	RELOC(proc0stack),%eax	/* start of PROC0 STK */
+	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
+	subl	%eax,%ecx
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
+	fillkpt_nox
+
 	/* Map the BOOTSTRAP TABLES RW. */
 	movl	%esi,%eax		/* start of BOOTSTRAP TABLES */
 	movl	$TABLESIZE,%ecx		/* length of BOOTSTRAP TABLES */
@@ -680,7 +694,7 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
+	/* We are on (5). Map ISA I/O mem (later atdevbase) RWX. */
 	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
 	movl	$(IOM_SIZE>>PGSHIFT),%ecx
 	fillkpt
@@ -842,7 +856,7 @@ longmode_hi:
 	movq	%rdx,_C_LABEL(atdevbase)(%rip)
 
 	

CVS commit: src/sys/arch/amd64/amd64

2016-05-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 28 09:03:16 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Define fillkpt_blank, which creates blank entries in a page table. Use
it to map the first MB. No functional change.


To generate a diff of this commit:
cvs rdiff -u -r1.95 -r1.96 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.95 src/sys/arch/amd64/amd64/locore.S:1.96
--- src/sys/arch/amd64/amd64/locore.S:1.95	Sat May 28 08:43:16 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sat May 28 09:03:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.95 2016/05/28 08:43:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.96 2016/05/28 09:03:16 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -245,6 +245,20 @@
 2:	;
 
 /*
+ * fillkpt_blank - Fill in a kernel page table with blank entries
+ *	ebx = page table address
+ *	ecx = number of pages to map
+ */
+#define fillkpt_blank	\
+	cmpl	$0,%ecx			;	/* zero-sized? */	\
+	je 	2f			; \
+1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
+	movl	$0,(%ebx)		;	/* lower 32 bits: 0 */	\
+	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
+	loop	1b			; \
+2:	;
+
+/*
  * killkpt - Destroy a kernel page table (long mode)
  *	rbx = page table address
  *	rcx = number of pages to destroy
@@ -643,12 +657,12 @@ no_NOX:
 	leal	(PROC0_PTP1_OFF)(%esi),%ebx
 
 	/* Skip the first MB. */
-	movl	$(KERNTEXTOFF_LO - KERNBASE_LO),%eax
-	movl	%eax,%ecx
-	shrl	$(PGSHIFT-3),%ecx	/* ((n >> PGSHIFT) << 3) for # PDEs */
-	addl	%ecx,%ebx
+	movl	$(KERNTEXTOFF_LO - KERNBASE_LO),%ecx
+	shrl	$PGSHIFT,%ecx
+	fillkpt_blank
 
 	/* Map the kernel text RX. */
+	movl	$(KERNTEXTOFF_LO - KERNBASE_LO),%eax	/* start of TEXT */
 	movl	$RELOC(__rodata_start),%ecx
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx



CVS commit: src/sys/arch

2016-06-04 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jun  4 10:48:12 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
The ISA I/O MEM does not need to be executable. Remove the X permission
on it.


To generate a diff of this commit:
cvs rdiff -u -r1.101 -r1.102 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.130 -r1.131 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.101 src/sys/arch/amd64/amd64/locore.S:1.102
--- src/sys/arch/amd64/amd64/locore.S:1.101	Sat Jun  4 10:19:09 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sat Jun  4 10:48:11 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.101 2016/06/04 10:19:09 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.102 2016/06/04 10:48:11 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -718,12 +718,12 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/* We are on (4). Map ISA I/O MEM RWX. */
+	/* We are on (4). Map ISA I/O MEM RW. */
 	movl	$IOM_BEGIN,%eax
 	movl	$IOM_SIZE,%ecx	/* size of ISA I/O MEM */
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW/*|PG_N*/),%eax
-	fillkpt
+	fillkpt_nox
 
 	/*
 	 * Build L2. Linked to L1.

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.130 src/sys/arch/i386/i386/locore.S:1.131
--- src/sys/arch/i386/i386/locore.S:1.130	Sat Jun  4 10:19:09 2016
+++ src/sys/arch/i386/i386/locore.S	Sat Jun  4 10:48:11 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.130 2016/06/04 10:19:09 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.131 2016/06/04 10:48:11 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.130 2016/06/04 10:19:09 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.131 2016/06/04 10:48:11 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -753,12 +753,12 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/* We are on (4). Map ISA I/O MEM RWX. */
+	/* We are on (4). Map ISA I/O MEM RW. */
 	movl	$IOM_BEGIN,%eax
 	movl	$IOM_SIZE,%ecx	/* size of ISA I/O MEM */
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW/*|PG_N*/),%eax
-	fillkpt
+	fillkpt_nox
 
 	/*
 	 * Build L2 for identity mapping. Linked to L1.



CVS commit: src/sys/arch

2016-05-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 29 09:16:12 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
Define tablesize. Useful when debugging.


To generate a diff of this commit:
cvs rdiff -u -r1.97 -r1.98 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.125 -r1.126 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.97 src/sys/arch/amd64/amd64/locore.S:1.98
--- src/sys/arch/amd64/amd64/locore.S:1.97	Sun May 29 09:04:19 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sun May 29 09:16:11 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.97 2016/05/29 09:04:19 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.98 2016/05/29 09:16:11 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -322,6 +322,7 @@ _C_LABEL(lapic_isr):
 END(lapic_isr)
 #endif /* NLAPIC > 0 */
 
+	.globl	_C_LABEL(tablesize)
 	.globl	_C_LABEL(nox_flag)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
@@ -335,6 +336,9 @@ END(lapic_isr)
 	.globl	_C_LABEL(gdtstore)
 	.globl	_C_LABEL(cputype)
 
+	.type	_C_LABEL(tablesize), @object
+_C_LABEL(tablesize):	.long	TABLESIZE
+END(tablesize)
 	.type	_C_LABEL(nox_flag), @object
 LABEL(nox_flag)		.long	0	/* 32bit NOX flag, set if supported */
 END(nox_flag)

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.125 src/sys/arch/i386/i386/locore.S:1.126
--- src/sys/arch/i386/i386/locore.S:1.125	Thu May 26 07:24:55 2016
+++ src/sys/arch/i386/i386/locore.S	Sun May 29 09:16:12 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.125 2016/05/26 07:24:55 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.126 2016/05/29 09:16:12 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.125 2016/05/26 07:24:55 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.126 2016/05/29 09:16:12 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -255,6 +255,7 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
  */
 	.data
 
+	.globl	_C_LABEL(tablesize)
 	.globl	_C_LABEL(nox_flag)
 	.globl	_C_LABEL(cputype)
 	.globl	_C_LABEL(cpuid_level)
@@ -302,6 +303,9 @@ LABEL(lapic_tpr)
 END(lapic_tpr)
 #endif
 
+	.type	_C_LABEL(tablesize), @object
+_C_LABEL(tablesize):	.long	0
+END(tablesize)
 	.type	_C_LABEL(nox_flag), @object
 LABEL(nox_flag)		.long	0	/* 32bit NOX flag, set if supported */
 END(nox_flag)
@@ -320,9 +324,6 @@ END(lwp0uarea)
 	.type	_C_LABEL(PDPpaddr), @object
 LABEL(PDPpaddr)		.long	0	/* paddr of PDP, for libkvm */
 END(PDPpaddr)
-	.type	_C_LABEL(tablesize), @object
-_C_LABEL(tablesize):	.long	0
-END(tablesize)
 
 	/* Space for the temporary stack */
 	.size	tmpstk, tmpstk - .



CVS commit: src/sys/arch/amd64/amd64

2016-05-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 29 09:04:20 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Revert rev1.94. It apparently raises a page fault from SMEP. I need to
investigate the whole kernel mappings anyway, so I'll recommit this
patch later.


To generate a diff of this commit:
cvs rdiff -u -r1.96 -r1.97 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.96 src/sys/arch/amd64/amd64/locore.S:1.97
--- src/sys/arch/amd64/amd64/locore.S:1.96	Sat May 28 09:03:16 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sun May 29 09:04:19 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.96 2016/05/28 09:03:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.97 2016/05/29 09:04:19 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -203,11 +203,12 @@
 #endif
 
 #define PROC0_PML4_OFF	0
-#define PROC0_PTP3_OFF	(PROC0_PML4_OFF + 1 * PAGE_SIZE)
+#define PROC0_STK_OFF	(PROC0_PML4_OFF + 1 * PAGE_SIZE)
+#define PROC0_PTP3_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
 #define PROC0_PTP2_OFF	(PROC0_PTP3_OFF + NKL4_KIMG_ENTRIES * PAGE_SIZE)
 #define PROC0_PTP1_OFF	(PROC0_PTP2_OFF + TABLE_L3_ENTRIES * PAGE_SIZE)
 #define TABLESIZE \
-  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1) \
+  ((NKL4_KIMG_ENTRIES + TABLE_L3_ENTRIES + TABLE_L2_ENTRIES + 1 + UPAGES) \
 * PAGE_SIZE)
 
 /*
@@ -321,7 +322,6 @@ _C_LABEL(lapic_isr):
 END(lapic_isr)
 #endif /* NLAPIC > 0 */
 
-	.globl	_C_LABEL(proc0stack)
 	.globl	_C_LABEL(nox_flag)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
@@ -335,9 +335,6 @@ END(lapic_isr)
 	.globl	_C_LABEL(gdtstore)
 	.globl	_C_LABEL(cputype)
 
-	.type	_C_LABEL(proc0stack), @object
-LABEL(proc0stack)	.quad	0	/* physical address of proc0's stack */
-END(proc0stack)
 	.type	_C_LABEL(nox_flag), @object
 LABEL(nox_flag)		.long	0	/* 32bit NOX flag, set if supported */
 END(nox_flag)
@@ -585,17 +582,20 @@ no_NOX:
  * be referred to as: L4 -> L3 -> L2 -> L1.
  *
  * Virtual address space of the kernel:
- * +--++--+-++-+---+-
- * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | PROC0 STK |
- * +--++--+-++-+---+-
- * (1)  (2)   (3) (4)
- *
- * -+-+-+++
- *  L4 -> L3 -> L2 -> L1 | ISA IO MEM |
- * -+-+-+++
- *  (5)
+ * +--++--+-++-+--
+ * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | L4 ->
+ * +--++--+-++-+--
+ * (1)  (2)   (3)
+ *
+ * --+-+-+++
+ * -> PROC0 STK -> L3 -> L2 -> L1 | ISA IO MEM |
+ * --+-+-+++
+ *   (4)
  *
- * (L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
+ * PROC0 STK is obviously not linked as a page level. It just happens to be
+ * caught between L4 and L3.
+ *
+ * (PROC0 STK + L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
  *
  * Important note: the kernel segments are properly 4k-aligned
  * (see kern.ldscript), so there's no need to enforce alignment.
@@ -621,17 +621,11 @@ no_NOX:
 	movl	%eax,%edi
 1:
 
-	/* We are on (3). Align up for PROC0 STK. */
+	/* We are on (3). Align up for BOOTSTRAP TABLES. */
 	movl	%edi,%esi
 	addl	$PGOFSET,%esi
 	andl	$~PGOFSET,%esi
 
-	/* Save proc0's stack pa, and allocate it. Brings us on (4). */
-	movl	$RELOC(proc0stack),%ebp
-	movl	%esi,(%ebp)
-	movl	$0,4(%ebp)
-	addl	$USPACE,%esi
-
 	/* We are on the BOOTSTRAP TABLES. Save L4's physical address. */
 	movl	$RELOC(PDPpaddr),%ebp
 	movl	%esi,(%ebp)
@@ -687,19 +681,11 @@ no_NOX:
 
 	/* Map [SYMS]+[PRELOADED MODULES] RWX. */
 	movl	$RELOC(__kernel_end),%eax
-	movl	RELOC(proc0stack),%ecx	/* start of PROC0 STK */
-	subl	%eax,%ecx
-	shrl	$PGSHIFT,%ecx
-	orl	$(PG_V|PG_KW),%eax
-	fillkpt
-
-	/* Map PROC0 STK RW. */
-	movl	RELOC(proc0stack),%eax	/* start of PROC0 STK */
 	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW),%eax
-	fillkpt_nox
+	fillkpt
 
 	/* Map the BOOTSTRAP TABLES RW. */
 	movl	%esi,%eax		/* start of BOOTSTRAP TABLES */
@@ -708,7 +694,7 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/* We are on (5). Map ISA I/O mem (later atdevbase) RWX. */
+	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
 	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
 	movl	$(IOM_SIZE>>PGSHIFT),%ecx
 	fillkpt
@@ -870,7 +856,7 @@ longmode_hi:
 	movq	%rdx,_C_LABEL(atdevbase)(%rip)
 
 	/* Set up bootstrap stack. */
-	movq	_C_LABEL(proc0stack),%rax
+	leaq	

CVS commit: src/sys/arch

2016-05-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 26 07:24:55 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
There is an issue in the way the fillkpt macro sets up pages on both
amd64 and i386.

The fillkpt loop is equivalent to the following:

do {
/* fill in the slot */
/* increment %ebx to the next slot */
/* increment %eax to the next pa */
} while (%ecx > 0)

The issue here is that if %ecx = 0 (i.e., the chunk we are trying to
map is zero-sized), there is still one entry created in the page table.
The kernel expects the va<->pa translation to be linear in low memory.
If there is a zero-sized chunk, the dead entry creates a +4096 offset in
the virtual space, with two consecutive entries that point to the same
physical address. In other words, the mappings are not linear anymore,
which causes the kernel to die.

Before my recent changes, there were only two big chunks that were
mapped, and neither of these could be zero-sized. Now, with multiple,
fine-grained chunks, it is possible that the [SYMS]+[PRELOADED_MODULES]
chunk could be zero-sized.

[PRELOADED_MODULES] is almost never here, and [SYMS] is always here on
default kernels. Except for floppies, where the bootloader does not load
[SYMS].

Should fix PR 51148.


To generate a diff of this commit:
cvs rdiff -u -r1.93 -r1.94 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.124 -r1.125 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.93 src/sys/arch/amd64/amd64/locore.S:1.94
--- src/sys/arch/amd64/amd64/locore.S:1.93	Sun May 22 10:11:55 2016
+++ src/sys/arch/amd64/amd64/locore.S	Thu May 26 07:24:55 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.93 2016/05/22 10:11:55 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.94 2016/05/26 07:24:55 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -220,16 +220,21 @@
  * Each entry is 8 (PDE_SIZE) bytes long: we must set the 4 upper bytes to 0.
  */
 #define fillkpt	\
+	cmpl	$0,%ecx			;	/* zero-sized? */	\
+	je 	2f			; \
 1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
 	movl	%eax,(%ebx)		;	/* store phys addr */	\
 	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
 	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
-	loop	1b			;
+	loop	1b			; \
+2:	;
 
 /*
  * fillkpt_nox - Same as fillkpt, but sets the NX/XD bit.
  */
 #define fillkpt_nox \
+	cmpl	$0,%ecx			;	/* zero-sized? */	\
+	je 	2f			; \
 	pushl	%ebp			; \
 	movl	RELOC(nox_flag),%ebp	; \
 1:	movl	%ebp,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: NX */ \
@@ -237,7 +242,8 @@
 	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
 	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
 	loop	1b			; \
-	popl	%ebp			;
+	popl	%ebp			; \
+2:	;
 
 /*
  * killkpt - Destroy a kernel page table (long mode)

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.124 src/sys/arch/i386/i386/locore.S:1.125
--- src/sys/arch/i386/i386/locore.S:1.124	Sun May 15 07:17:53 2016
+++ src/sys/arch/i386/i386/locore.S	Thu May 26 07:24:55 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.124 2016/05/15 07:17:53 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.125 2016/05/26 07:24:55 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.124 2016/05/15 07:17:53 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.125 2016/05/26 07:24:55 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -192,16 +192,21 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
  * instruction just clears the page table entry.
  */
 #define fillkpt	\
+	cmpl	$0,%ecx			;	/* zero-sized? */	\
+	je 	2f			; \
 1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
 	movl	%eax,(%ebx)		;	/* store phys addr */	\
 	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
 	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
-	loop	1b			;
+	loop	1b			; \
+2:	;
 
 /*
  * fillkpt_nox - Same as fillkpt, but sets the NX/XD bit.
  */
 #define fillkpt_nox \
+	cmpl	$0,%ecx			;	/* zero-sized? */	\
+	je 	2f			; \
 	pushl	%ebp			; \
 	movl	RELOC(nox_flag),%ebp	; \
 1:	movl	%ebp,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: NX */ \
@@ -209,7 +214,8 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
 	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
 	loop	1b			; \
-	popl	%ebp			;
+	popl	%ebp			; \
+2:	;
 
 /*
  * killkpt - Destroy a kernel page table



CVS commit: src/sys/arch/amd64/include

2016-05-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 21 07:00:18 UTC 2016

Modified Files:
src/sys/arch/amd64/include: pmap.h

Log Message:
Explain where this value comes from.


To generate a diff of this commit:
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/amd64/include/pmap.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/include/pmap.h
diff -u src/sys/arch/amd64/include/pmap.h:1.36 src/sys/arch/amd64/include/pmap.h:1.37
--- src/sys/arch/amd64/include/pmap.h:1.36	Sat May 14 12:48:31 2016
+++ src/sys/arch/amd64/include/pmap.h	Sat May 21 07:00:18 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.h,v 1.36 2016/05/14 12:48:31 maxv Exp $	*/
+/*	$NetBSD: pmap.h,v 1.37 2016/05/21 07:00:18 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -155,7 +155,7 @@
 /* Xen use slots 256-272, let's move farther */
 #define L4_SLOT_KERN		320
 #endif
-#define L4_SLOT_KERNBASE	511
+#define L4_SLOT_KERNBASE	511 /* pl4_i(KERNBASE) */
 
 #define PDIR_SLOT_KERN	L4_SLOT_KERN
 #define PDIR_SLOT_PTE	L4_SLOT_PTE



CVS commit: src/sys/uvm

2016-05-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 21 06:37:28 UTC 2016

Modified Files:
src/sys/uvm: uvm_glue.c

Log Message:
USPACE and USPACE_ALIGN are constants. Use a #if instead. Probably saves
some instructions.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/uvm/uvm_glue.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/uvm/uvm_glue.c
diff -u src/sys/uvm/uvm_glue.c:1.161 src/sys/uvm/uvm_glue.c:1.162
--- src/sys/uvm/uvm_glue.c:1.161	Thu Nov 27 14:25:01 2014
+++ src/sys/uvm/uvm_glue.c	Sat May 21 06:37:28 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: uvm_glue.c,v 1.161 2014/11/27 14:25:01 uebayasi Exp $	*/
+/*	$NetBSD: uvm_glue.c,v 1.162 2016/05/21 06:37:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.161 2014/11/27 14:25:01 uebayasi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.162 2016/05/21 06:37:28 maxv Exp $");
 
 #include "opt_kgdb.h"
 #include "opt_kstack.h"
@@ -240,31 +240,28 @@ static pool_cache_t uvm_uarea_system_cac
 static void *
 uarea_poolpage_alloc(struct pool *pp, int flags)
 {
-#if defined(PMAP_MAP_POOLPAGE)
-	if (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) {
-		struct vm_page *pg;
-		vaddr_t va;
+#if defined(PMAP_MAP_POOLPAGE) && (USPACE == PAGE_SIZE && USPACE_ALIGN == 0)
+	struct vm_page *pg;
+	vaddr_t va;
 
 #if defined(PMAP_ALLOC_POOLPAGE)
-		pg = PMAP_ALLOC_POOLPAGE(
-		   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
+	pg = PMAP_ALLOC_POOLPAGE(
+	   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
 #else
-		pg = uvm_pagealloc(NULL, 0, NULL,
-		   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
-#endif
-		if (pg == NULL)
-			return NULL;
-		va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
-		if (va == 0)
-			uvm_pagefree(pg);
-		return (void *)va;
-	}
-#endif
-#if defined(__HAVE_CPU_UAREA_ROUTINES)
+	pg = uvm_pagealloc(NULL, 0, NULL,
+	   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
+#endif /* PMAP_ALLOC_POOLPAGE */
+	if (pg == NULL)
+		return NULL;
+	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
+	if (va == 0)
+		uvm_pagefree(pg);
+	return (void *)va;
+#elif defined(__HAVE_CPU_UAREA_ROUTINES)
 	void *va = cpu_uarea_alloc(false);
 	if (va)
 		return (void *)va;
-#endif
+#endif /* __HAVE_CPU_UAREA_ROUTINES */
 	return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz,
 	USPACE_ALIGN, UVM_KMF_WIRED |
 	((flags & PR_WAITOK) ? UVM_KMF_WAITVA :



CVS commit: src/sys/arch/x86/x86

2016-05-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 21 07:15:56 UTC 2016

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
There is an issue in the way the direct map is set up on amd64.

When allocating memory, the kernel allocates physical pages and virtual
addresses for these pages. In order to optimize allocations smaller
than PAGE_SIZE, uvm_km_kmem_alloc can allocate a single physical page
and take its virtual address in the direct map in high virtual memory.
This direct map is set up at boot time, its PTEs do not change, and
therefore they don't need to be kentered. These high virtual PTEs being
constant, the permissions of the areas they point to are fixed at boot
time and cannot change.

The problem is that at boot time, they are created with RWX permissions.
Therefore, allocations smaller than PAGE_SIZE in the kernel heap are all
executable: mbufs, pnbufs, small kmem allocations, etc.

Fix this by setting the NOX bit in the direct map pages at boot time. We
also set the NOX bit in the temporary tmpva, since it does not need to
be executable either.

This also makes the U-area non executable on amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.195 -r1.196 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.195 src/sys/arch/x86/x86/pmap.c:1.196
--- src/sys/arch/x86/x86/pmap.c:1.195	Sun May 15 10:35:54 2016
+++ src/sys/arch/x86/x86/pmap.c	Sat May 21 07:15:56 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.195 2016/05/15 10:35:54 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.196 2016/05/21 07:15:56 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.195 2016/05/15 10:35:54 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.196 2016/05/21 07:15:56 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1366,7 +1366,7 @@ pmap_bootstrap(vaddr_t kva_start)
 	pte = PTE_BASE + pl1_i(tmpva);
 
 	/*
-	 * Map the direct map.  Use 1GB pages if they are available,
+	 * Map the direct map RW.  Use 1GB pages if they are available,
 	 * otherwise use 2MB pages.  Note that the unused parts of
 	 * PTPs * must be zero outed, as they might be accessed due
 	 * to speculative execution.  Also, PG_G is not allowed on
@@ -1382,51 +1382,51 @@ pmap_bootstrap(vaddr_t kva_start)
 	ndmpdp = (lastpa + NBPD_L3 - 1) >> L3_SHIFT;
 	dmpdp = avail_start;	avail_start += PAGE_SIZE;
 
-	*pte = dmpdp | PG_V | PG_RW;
+	*pte = dmpdp | PG_V | PG_RW | pg_nx;
 	pmap_update_pg(tmpva);
 	memset((void *)tmpva, 0, PAGE_SIZE);
 
 	if (cpu_feature[2] & CPUID_P1GB) {
 		for (i = 0; i < ndmpdp; i++) {
 			pdp = (paddr_t)&(((pd_entry_t *)dmpdp)[i]);
-			*pte = (pdp & PG_FRAME) | PG_V | PG_RW;
+			*pte = (pdp & PG_FRAME) | PG_V | PG_RW | pg_nx;
 			pmap_update_pg(tmpva);
 
 			pde = (pd_entry_t *)(tmpva + (pdp & ~PG_FRAME));
-			*pde = ((paddr_t)i << L3_SHIFT) |
-PG_RW | PG_V | PG_U | PG_PS | PG_G;
+			*pde = ((paddr_t)i << L3_SHIFT) | PG_RW | pg_nx |
+			PG_V | PG_U | PG_PS | PG_G;
 		}
 	} else {
 		dmpd = avail_start;	avail_start += ndmpdp * PAGE_SIZE;
 
 		for (i = 0; i < ndmpdp; i++) {
 			pdp = dmpd + i * PAGE_SIZE;
-			*pte = (pdp & PG_FRAME) | PG_V | PG_RW;
+			*pte = (pdp & PG_FRAME) | PG_V | PG_RW | pg_nx;
 			pmap_update_pg(tmpva);
 
 			memset((void *)tmpva, 0, PAGE_SIZE);
 		}
 		for (i = 0; i < NPDPG * ndmpdp; i++) {
 			pdp = (paddr_t)&(((pd_entry_t *)dmpd)[i]);
-			*pte = (pdp & PG_FRAME) | PG_V | PG_RW;
+			*pte = (pdp & PG_FRAME) | PG_V | PG_RW | pg_nx;
 			pmap_update_pg(tmpva);
 
 			pde = (pd_entry_t *)(tmpva + (pdp & ~PG_FRAME));
-			*pde = ((paddr_t)i << L2_SHIFT) |
-PG_RW | PG_V | PG_U | PG_PS | PG_G;
+			*pde = ((paddr_t)i << L2_SHIFT) | PG_RW | pg_nx |
+			PG_V | PG_U | PG_PS | PG_G;
 		}
 		for (i = 0; i < ndmpdp; i++) {
 			pdp = (paddr_t)&(((pd_entry_t *)dmpdp)[i]);
-			*pte = (pdp & PG_FRAME) | PG_V | PG_RW;
+			*pte = (pdp & PG_FRAME) | PG_V | PG_RW | pg_nx;
 			pmap_update_pg((vaddr_t)tmpva);
 
 			pde = (pd_entry_t *)(tmpva + (pdp & ~PG_FRAME));
-			*pde = (dmpd + (i << PAGE_SHIFT)) |
-PG_RW | PG_V | PG_U;
+			*pde = (dmpd + (i << PAGE_SHIFT)) | PG_RW | pg_nx |
+			PG_V | PG_U;
 		}
 	}
 
-	kpm->pm_pdir[PDIR_SLOT_DIRECT] = dmpdp | PG_KW | PG_V | PG_U;
+	kpm->pm_pdir[PDIR_SLOT_DIRECT] = dmpdp | PG_KW | pg_nx | PG_V | PG_U;
 
 	tlbflush();
 



CVS commit: src/sys/arch/amd64/amd64

2016-05-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 22 10:11:55 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Save L4's physical address earlier. Also, PDE_SIZE has nothing to do
here, we are just zeroing out the upper 32bits of the 64bit pointer.


To generate a diff of this commit:
cvs rdiff -u -r1.92 -r1.93 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.92 src/sys/arch/amd64/amd64/locore.S:1.93
--- src/sys/arch/amd64/amd64/locore.S:1.92	Sun May 15 07:17:53 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sun May 22 10:11:55 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.92 2016/05/15 07:17:53 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.93 2016/05/22 10:11:55 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -606,6 +606,11 @@ no_NOX:
 	addl	$PGOFSET,%esi
 	andl	$~PGOFSET,%esi
 
+	/* We are on the BOOTSTRAP TABLES. Save L4's physical address. */
+	movl	$RELOC(PDPpaddr),%ebp
+	movl	%esi,(%ebp)
+	movl	$0,4(%ebp)
+
 	/* Now, zero out the BOOTSTRAP TABLES (before filling them in). */
 	movl	%esi,%edi
 	xorl	%eax,%eax
@@ -733,11 +738,6 @@ no_NOX:
 	movl	%eax,(%ebx)
 	movl	$0,(PDE_SIZE-4)(%ebx)
 
-	/* Save physical address of L4. */
-	movl	$RELOC(PDPpaddr),%ebp
-	movl	%esi,(%ebp)
-	movl	$0,(PDE_SIZE-4)(%ebp)
-
 	/*
 	 * Startup checklist:
 	 * 1. Enable PAE (and SSE while here).



CVS commit: src/sys/uvm

2016-05-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 22 09:10:37 UTC 2016

Modified Files:
src/sys/uvm: uvm_glue.c

Log Message:
Revert my previous change. I missed an entry on NXR.


To generate a diff of this commit:
cvs rdiff -u -r1.162 -r1.163 src/sys/uvm/uvm_glue.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/uvm/uvm_glue.c
diff -u src/sys/uvm/uvm_glue.c:1.162 src/sys/uvm/uvm_glue.c:1.163
--- src/sys/uvm/uvm_glue.c:1.162	Sat May 21 06:37:28 2016
+++ src/sys/uvm/uvm_glue.c	Sun May 22 09:10:37 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: uvm_glue.c,v 1.162 2016/05/21 06:37:28 maxv Exp $	*/
+/*	$NetBSD: uvm_glue.c,v 1.163 2016/05/22 09:10:37 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -62,7 +62,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.162 2016/05/21 06:37:28 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uvm_glue.c,v 1.163 2016/05/22 09:10:37 maxv Exp $");
 
 #include "opt_kgdb.h"
 #include "opt_kstack.h"
@@ -240,28 +240,31 @@ static pool_cache_t uvm_uarea_system_cac
 static void *
 uarea_poolpage_alloc(struct pool *pp, int flags)
 {
-#if defined(PMAP_MAP_POOLPAGE) && (USPACE == PAGE_SIZE && USPACE_ALIGN == 0)
-	struct vm_page *pg;
-	vaddr_t va;
+#if defined(PMAP_MAP_POOLPAGE)
+	if (USPACE == PAGE_SIZE && USPACE_ALIGN == 0) {
+		struct vm_page *pg;
+		vaddr_t va;
 
 #if defined(PMAP_ALLOC_POOLPAGE)
-	pg = PMAP_ALLOC_POOLPAGE(
-	   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
+		pg = PMAP_ALLOC_POOLPAGE(
+		   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
 #else
-	pg = uvm_pagealloc(NULL, 0, NULL,
-	   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
-#endif /* PMAP_ALLOC_POOLPAGE */
-	if (pg == NULL)
-		return NULL;
-	va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
-	if (va == 0)
-		uvm_pagefree(pg);
-	return (void *)va;
-#elif defined(__HAVE_CPU_UAREA_ROUTINES)
+		pg = uvm_pagealloc(NULL, 0, NULL,
+		   ((flags & PR_WAITOK) == 0 ? UVM_KMF_NOWAIT : 0));
+#endif
+		if (pg == NULL)
+			return NULL;
+		va = PMAP_MAP_POOLPAGE(VM_PAGE_TO_PHYS(pg));
+		if (va == 0)
+			uvm_pagefree(pg);
+		return (void *)va;
+	}
+#endif
+#if defined(__HAVE_CPU_UAREA_ROUTINES)
 	void *va = cpu_uarea_alloc(false);
 	if (va)
 		return (void *)va;
-#endif /* __HAVE_CPU_UAREA_ROUTINES */
+#endif
 	return (void *)uvm_km_alloc(kernel_map, pp->pr_alloc->pa_pagesz,
 	USPACE_ALIGN, UVM_KMF_WIRED |
 	((flags & PR_WAITOK) ? UVM_KMF_WAITVA :



CVS commit: src/sys/arch/i386/i386

2016-05-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed May 11 17:48:05 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Switch to C-style comments, and reduce a little the diff between i386 and
amd64. No functional changes.


To generate a diff of this commit:
cvs rdiff -u -r1.114 -r1.115 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.114 src/sys/arch/i386/i386/locore.S:1.115
--- src/sys/arch/i386/i386/locore.S:1.114	Sun Aug 30 01:46:03 2015
+++ src/sys/arch/i386/i386/locore.S	Wed May 11 17:48:05 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.114 2015/08/30 01:46:03 uebayasi Exp $	*/
+/*	$NetBSD: locore.S,v 1.115 2016/05/11 17:48:05 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -64,7 +64,6 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-
 /*-
  * Copyright (c) 1998, 2000, 2004, 2006, 2007, 2009 The NetBSD Foundation, Inc.
  * All rights reserved.
@@ -129,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.114 2015/08/30 01:46:03 uebayasi Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.115 2016/05/11 17:48:05 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -159,7 +158,6 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 #include 
 #endif
 
-/* XXX temporary kluge; these should not be here */
 /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
 #include 
 
@@ -170,6 +168,35 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 #endif /* XEN */
 #define	RELOC(x)	_RELOC(_C_LABEL(x))
 
+#ifndef PAE
+#define	PROC0_PDIR_OFF	0
+#else
+#define PROC0_L3_OFF	0
+#define PROC0_PDIR_OFF	1 * PAGE_SIZE
+#endif
+
+#define	PROC0_STK_OFF	(PROC0_PDIR_OFF + PDP_SIZE * PAGE_SIZE)
+#define	PROC0_PTP1_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
+
+/*
+ * fillkpt - Fill in a kernel page table
+ *	eax = pte (page frame | control | status)
+ *	ebx = page table address
+ *	ecx = number of pages to map
+ *
+ * For PAE, each entry is 8 bytes long: we must set the 4 upper bytes to 0.
+ * This is done by the first instruction of fillkpt. In the non-PAE case, this
+ * instruction just clears the page table entry.
+ */
+
+#define fillkpt	\
+1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
+	movl	%eax,(%ebx)		;	/* store phys addr */	\
+	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
+	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
+	loop	1b			;
+
+
 #ifdef XEN
 /*
  * Xen guest identifier and loader selection
@@ -242,30 +269,32 @@ LABEL(lapic_tpr)
 END(lapic_tpr)
 #endif
 	.type	_C_LABEL(cputype), @object
-LABEL(cputype)		.long	0	# are we 80486, Pentium, or..
+LABEL(cputype)		.long	0	/* are we 80486, Pentium, or.. */
 END(cputype)
 	.type	_C_LABEL(cpuid_level), @object
 LABEL(cpuid_level)	.long	0
 END(cpuid_level)
 	.type	_C_LABEL(atdevbase), @object
-LABEL(atdevbase)	.long	0	# location of start of iomem in virtual
+LABEL(atdevbase)	.long	0	/* location of start of iomem in virt */
 END(atdevbase)
 	.type	_C_LABEL(lwp0uarea), @object
 LABEL(lwp0uarea)	.long	0
 END(lwp0uarea)
 	.type	_C_LABEL(PDPpaddr), @object
-LABEL(PDPpaddr)		.long	0	# paddr of PDP, for libkvm
+LABEL(PDPpaddr)		.long	0	/* paddr of PDP, for libkvm */
 END(PDPpaddr)
 	.type	_C_LABEL(tablesize), @object
 _C_LABEL(tablesize):	.long	0
 END(tablesize)
+
+	/* Space for the temporary stack */
 	.size	tmpstk, tmpstk - .
-	.space 512
+	.space	512
 tmpstk:
 #ifdef XEN
-	.align 		PAGE_SIZE, 0x0	# Align on page boundary
+	.align 		PAGE_SIZE, 0x0	/* Align on page boundary */
 LABEL(tmpgdt)
-	.space 		PAGE_SIZE	# Xen expects a page
+	.space 		PAGE_SIZE	/* Xen expects a page */
 END(tmpgdt)
 #endif /* XEN */
 
@@ -275,7 +304,10 @@ END(tmpgdt)
 
 ENTRY(start)
 #ifndef XEN
-	movw	$0x1234,0x472			# warm boot
+
+	/* Warm boot */
+	movw	$0x1234,0x472
+
 #if defined(MULTIBOOT)
 	jmp	1f
 
@@ -299,7 +331,7 @@ _C_LABEL(Multiboot_Header):
 	 * space to process it later -- after we are relocated.  It will
 	 * be safer to run complex C code than doing it at this point.
 	 */
-	pushl	%ebx		# Address of Multiboot information
+	pushl	%ebx		/* Address of Multiboot information */
 	call	_C_LABEL(multiboot_pre_reloc)
 	addl	$4,%esp
 	jmp	2f
@@ -312,7 +344,7 @@ _C_LABEL(Multiboot_Header):
 	 * (boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem),
 	 * 4 bytes each.
 	 */
-	addl	$4,%esp		# Discard return address to boot loader
+	addl	$4,%esp		/* Discard return address to boot loader */
 	call	_C_LABEL(native_loader)
 	addl	$24,%esp
 
@@ -404,30 +436,30 @@ is486:	movl	$CPU_486,RELOC(cputype)
 	popfl
 	jmp 2f
 trycyrix486:
-	movl	$CPU_6x86,RELOC(cputype)	# set CPU type
+	movl	$CPU_6x86,RELOC(cputype)	/* set CPU type */
 	/*
 	 * Check for Cyrix 486 CPU by seeing if the flags change during a
 	 * divide. This is documented in the Cx486SLC/e SMM Programmer's
 	 * Guide.
 	 */
 	xorl	%edx,%edx
-	cmpl	%edx,%edx		# set flags to 

CVS commit: src/sys/arch

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 12 06:57:55 UTC 2016

Modified Files:
src/sys/arch/amd64/conf: kern.ldscript.Xen
src/sys/arch/i386/conf: kern.ldscript.Xen

Log Message:
KNF the Xen ld scripts on x86.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/conf/kern.ldscript.Xen
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/i386/conf/kern.ldscript.Xen

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/conf/kern.ldscript.Xen
diff -u src/sys/arch/amd64/conf/kern.ldscript.Xen:1.10 src/sys/arch/amd64/conf/kern.ldscript.Xen:1.11
--- src/sys/arch/amd64/conf/kern.ldscript.Xen:1.10	Tue Aug 25 08:07:24 2015
+++ src/sys/arch/amd64/conf/kern.ldscript.Xen	Thu May 12 06:57:55 2016
@@ -1,63 +1,71 @@
-/*	$NetBSD: kern.ldscript.Xen,v 1.10 2015/08/25 08:07:24 uebayasi Exp $	*/
+/*	$NetBSD: kern.ldscript.Xen,v 1.11 2016/05/12 06:57:55 maxv Exp $	*/
 
 #include "assym.h"
 
+__PAGE_SIZE = 0x1000 ;
+
 ENTRY(_start)
 SECTIONS
 {
-  /* Read-only sections, merged into text segment: */
-  .text :
-  {
-*(.text)
-*(.text.*)
-*(.stub)
-*(.rodata)
-  }
-  _etext = . ;
-  PROVIDE (etext = .) ;
-
-  /* Adjust the address for the data segment.  We want to adjust up to
- the same address within the page on the next page up.  */
-  . = ALIGN(0x10) + (. & (0x10 - 1));
-  __data_start = . ;
-  .data :
-  {
-*(.data)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.cacheline_aligned :
-  {
-*(.data.cacheline_aligned)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.read_mostly :
-  {
-*(.data.read_mostly)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  _edata = . ;
-  PROVIDE (edata = .) ;
-  __bss_start = . ;
-  .bss :
-  {
-*(.bss)
-*(.bss.*)
-*(COMMON)
-. = ALIGN(64 / 8);
-  }
-  . = ALIGN(64 / 8);
-  _end = . ;
-  PROVIDE (end = .) ;
-  .note.netbsd.ident :
-  {
-KEEP(*(.note.netbsd.ident));
-  }
+	/* Read-only sections, merged into text segment: */
+	.text :
+	{
+		*(.text)
+		*(.text.*)
+		*(.stub)
+		*(.rodata)
+	}
+	_etext = . ;
+	PROVIDE (etext = .) ;
+
+	/*
+	 * Adjust the address for the data segment.  We want to adjust up to
+	 * the same address within the page on the next page up.
+	 */
+	. = ALIGN(0x10) + (. & (0x10 - 1));
+
+	__data_start = . ;
+	.data :
+	{
+		*(.data)
+	}
+
+	. = ALIGN(COHERENCY_UNIT);
+	.data.cacheline_aligned :
+	{
+		*(.data.cacheline_aligned)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+	.data.read_mostly :
+	{
+		*(.data.read_mostly)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+
+	_edata = . ;
+	PROVIDE (edata = .) ;
+	__bss_start = . ;
+	.bss :
+	{
+		*(.bss)
+		*(.bss.*)
+		*(COMMON)
+		. = ALIGN(64 / 8);
+	}
+	. = ALIGN(64 / 8);
+	_end = . ;
+	PROVIDE (end = .) ;
+	.note.netbsd.ident :
+	{
+		KEEP(*(.note.netbsd.ident));
+	}
 }
+
 SECTIONS
 {
-  .text :
-  AT (ADDR(.text))
-  {
-*(.text)
-  } =0
+	.text :
+	AT (ADDR(.text))
+	{
+		*(.text)
+	} = 0
 }

Index: src/sys/arch/i386/conf/kern.ldscript.Xen
diff -u src/sys/arch/i386/conf/kern.ldscript.Xen:1.10 src/sys/arch/i386/conf/kern.ldscript.Xen:1.11
--- src/sys/arch/i386/conf/kern.ldscript.Xen:1.10	Tue Aug 25 12:56:58 2015
+++ src/sys/arch/i386/conf/kern.ldscript.Xen	Thu May 12 06:57:55 2016
@@ -1,67 +1,75 @@
-/*	$NetBSD: kern.ldscript.Xen,v 1.10 2015/08/25 12:56:58 uebayasi Exp $	*/
+/*	$NetBSD: kern.ldscript.Xen,v 1.11 2016/05/12 06:57:55 maxv Exp $	*/
 
 #include "assym.h"
 
+__PAGE_SIZE = 0x1000 ;
+
 SECTIONS
 {
-  /* Read-only sections, merged into text segment: */
-  .text :
-  {
-*(.text)
-*(.text.*)
-*(.stub)
-  }
-  _etext = . ;
-  PROVIDE (etext = .) ;
-
-  .rodata :
-  {
-*(.rodata)
-*(.rodata.*)
-  }
-
-  /* Adjust the address for the data segment.  We want to adjust up to
- the same address within the page on the next page up.  */
-  . = ALIGN(0x1000) + (. & (0x1000 - 1));
-  __data_start = . ;
-  .data :
-  {
-*(.data)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.cacheline_aligned :
-  {
-*(.data.cacheline_aligned)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.read_mostly :
-  {
-*(.data.read_mostly)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  _edata = . ;
-  PROVIDE (edata = .) ;
-  __bss_start = . ;
-  .bss :
-  {
-*(.bss)
-*(.bss.*)
-*(COMMON)
-. = ALIGN(32 / 8);
-  }
-  . = ALIGN(32 / 8);
-  _end = . ;
-  PROVIDE (end = .) ;
-  .note.netbsd.ident :
-  {
-KEEP(*(.note.netbsd.ident));
-  }
+	/* Read-only sections, merged into text segment: */
+	.text :
+	{
+		*(.text)
+		*(.text.*)
+		*(.stub)
+	}
+	_etext = . ;
+	PROVIDE (etext = .) ;
+
+	.rodata :
+	{
+		*(.rodata)
+		*(.rodata.*)
+	}
+
+	/*
+	 * Adjust the address for the data segment.  We want to adjust up to
+	 * the same address within the page on the next page up.
+	 */
+	. = ALIGN(0x1000) + (. & (0x1000 - 1));
+
+	__data_start = . ;
+	.data :
+	{
+		*(.data)
+	}
+
+	. = ALIGN(COHERENCY_UNIT);
+	.data.cacheline_aligned :
+	{
+		

CVS commit: src/sys/arch/amd64

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 12 09:05:16 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/amd64/conf: kern.ldscript

Log Message:
Map the data+bss chunk independently on amd64, and remove the X
permission on it.


To generate a diff of this commit:
cvs rdiff -u -r1.89 -r1.90 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.89 src/sys/arch/amd64/amd64/locore.S:1.90
--- src/sys/arch/amd64/amd64/locore.S:1.89	Thu May 12 07:51:09 2016
+++ src/sys/arch/amd64/amd64/locore.S	Thu May 12 09:05:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.89 2016/05/12 07:51:09 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.90 2016/05/12 09:05:16 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -646,18 +646,26 @@ no_NOX:
 	orl	$(PG_V|PG_KR),%eax
 	fillkpt_nox
 
+	/* Map the kernel data+bss RW. */
+	movl	$RELOC(__data_start),%eax
+	movl	$RELOC(__kernel_end),%ecx
+	subl	%eax,%ecx
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
+	fillkpt_nox
+
 	/*
 	 * We actually have to be careful here. The memory layout is as
 	 * follows:
-	 *  ++--+---+--+
-	 *  | RODATA < DATA+BSS | PRELOADED MODULES | BOOTSTRAP TABLES >
-	 *  ++--+---+--+
+	 *+--+-+--+
+	 *| DATA+BSS < [PRELOADED MODULES] | BOOTSTRAP TABLES >
+	 *+--+-+--+
 	 * We just map everything from < to > with RWX rights.
 	 */
-	movl	$RELOC(__data_start),%eax
+	movl	$RELOC(__kernel_end),%eax
 	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
 	addl	$TABLESIZE,%ecx		/* end of BOOTSTRAP TABLES */
-	subl	%eax,%ecx		/* subtract start of DATA */
+	subl	%eax,%ecx		/* subtract end of kernel image */
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt

Index: src/sys/arch/amd64/conf/kern.ldscript
diff -u src/sys/arch/amd64/conf/kern.ldscript:1.20 src/sys/arch/amd64/conf/kern.ldscript:1.21
--- src/sys/arch/amd64/conf/kern.ldscript:1.20	Thu May 12 07:21:18 2016
+++ src/sys/arch/amd64/conf/kern.ldscript	Thu May 12 09:05:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript,v 1.20 2016/05/12 07:21:18 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.21 2016/05/12 09:05:16 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -13,7 +13,6 @@ __LARGE_PAGE_SIZE = 0x20 ;
 ENTRY(_start)
 SECTIONS
 {
-	/* Read-only sections, merged into text segment: */
 	.text :
 	{
 		*(.text)
@@ -66,7 +65,12 @@ SECTIONS
 		*(COMMON)
 		. = ALIGN(64 / 8);
 	}
-	. = ALIGN(64 / 8);
+
+	. = ALIGN(__PAGE_SIZE);
+
+	/* End of the kernel image */
+	__kernel_end = . ;
+
 	_end = . ;
 	PROVIDE (end = .) ;
 	.note.netbsd.ident :



CVS commit: src/sys/arch/amd64/amd64

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 12 07:51:10 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Define fillkpt_nox, which sets up a set of pages and puts the NOX bit on
them by using nox_flag. Use fillkpt_nox to map the rodata segment
without X permissions.


To generate a diff of this commit:
cvs rdiff -u -r1.88 -r1.89 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.88 src/sys/arch/amd64/amd64/locore.S:1.89
--- src/sys/arch/amd64/amd64/locore.S:1.88	Thu May 12 07:21:18 2016
+++ src/sys/arch/amd64/amd64/locore.S	Thu May 12 07:51:09 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.88 2016/05/12 07:21:18 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.89 2016/05/12 07:51:09 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -227,6 +227,19 @@
 	loop	1b			;
 
 /*
+ * fillkpt_nox - Same as fillkpt, but sets the NX/XD bit.
+ */
+#define fillkpt_nox \
+	pushl	%ebp			;	\
+	movl	RELOC(nox_flag),%ebp	;	\
+1:	movl	%eax,(%ebx)		;	/* store phys addr */	\
+	movl	%ebp,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: NX */ \
+	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
+	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
+	loop	1b			;	\
+	popl	%ebp			;
+
+/*
  * killkpt - Destroy a kernel page table (long mode)
  *	rbx = page table address
  *	rcx = number of pages to destroy
@@ -625,13 +638,13 @@ no_NOX:
 	orl	$(PG_V|PG_KR),%eax
 	fillkpt
 
-	/* Map the kernel rodata ROX. */
+	/* Map the kernel rodata RO. */
 	movl	$RELOC(__rodata_start),%eax
 	movl	$RELOC(__data_start),%ecx
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KR),%eax
-	fillkpt
+	fillkpt_nox
 
 	/*
 	 * We actually have to be careful here. The memory layout is as



CVS commit: src/sys/arch

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 12 06:45:16 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S machdep.c
src/sys/arch/amd64/conf: kern.ldscript
src/sys/arch/i386/conf: kern.ldscript
src/sys/arch/i386/i386: locore.S
src/sys/arch/x86/x86: db_memrw.c pmap.c

Log Message:
Split the {text+rodata} chunk in two separate chunks on x86. The
rodata segment now loses the large page optimization, gets mapped inside
the data segment, and therefore becomes RWX. It may break the build on
Xen.


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.215 -r1.216 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/conf/kern.ldscript
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/i386/conf/kern.ldscript
cvs rdiff -u -r1.115 -r1.116 src/sys/arch/i386/i386/locore.S
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/x86/x86/db_memrw.c
cvs rdiff -u -r1.190 -r1.191 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.86 src/sys/arch/amd64/amd64/locore.S:1.87
--- src/sys/arch/amd64/amd64/locore.S:1.86	Wed May 11 19:35:08 2016
+++ src/sys/arch/amd64/amd64/locore.S	Thu May 12 06:45:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.86 2016/05/11 19:35:08 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.87 2016/05/12 06:45:16 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -617,11 +617,11 @@ no_NOX:
 	addl	%ecx,%ebx
 
 	/*
-	 * Compute &__data_start - KERNBASE. This can't be > 4G, or we can't
+	 * Compute &__rodata_start - KERNBASE. This can't be > 4G, or we can't
 	 * deal with it anyway, since we can't load it in 32 bit mode. So use
 	 * the bottom 32 bits.
 	 */
-	movl	$RELOC(__data_start),%edx
+	movl	$RELOC(__rodata_start),%edx
 	andl	$~PGOFSET,%edx
 
 	/* Map the kernel text read-only. */

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.215 src/sys/arch/amd64/amd64/machdep.c:1.216
--- src/sys/arch/amd64/amd64/machdep.c:1.215	Mon Feb 15 20:35:59 2016
+++ src/sys/arch/amd64/amd64/machdep.c	Thu May 12 06:45:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.215 2016/02/15 20:35:59 riastradh Exp $	*/
+/*	$NetBSD: machdep.c,v 1.216 2016/05/12 06:45:16 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.215 2016/02/15 20:35:59 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.216 2016/05/12 06:45:16 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -2102,6 +2102,7 @@ mm_md_kernacc(void *ptr, vm_prot_t prot,
 
 	if (v >= (vaddr_t) && v < (vaddr_t)kern_end) {
 		*handled = true;
+		/* Either the text or rodata segment */
 		if (v < (vaddr_t)&__data_start && (prot & VM_PROT_WRITE))
 			return EFAULT;
 

Index: src/sys/arch/amd64/conf/kern.ldscript
diff -u src/sys/arch/amd64/conf/kern.ldscript:1.18 src/sys/arch/amd64/conf/kern.ldscript:1.19
--- src/sys/arch/amd64/conf/kern.ldscript:1.18	Sun May  8 08:30:41 2016
+++ src/sys/arch/amd64/conf/kern.ldscript	Thu May 12 06:45:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript,v 1.18 2016/05/08 08:30:41 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.19 2016/05/12 06:45:16 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -23,6 +23,12 @@ SECTIONS
 	_etext = . ;
 	PROVIDE (etext = .) ;
 
+	/*
+	 * Push the rodata segment up to the next large page boundary so that we
+	 * can map the text segment with large pages.
+	 */
+	. = ALIGN(__LARGE_PAGE_SIZE);
+
 	__rodata_start = . ;
 	.rodata :
 	{
@@ -30,12 +36,6 @@ SECTIONS
 		*(.rodata.*)
 	}
 
-	/*
-	 * Push the data segment up to the next large page boundary so that we
-	 * can map the text+rodata segments with large pages.
-	 */
-	. = ALIGN(__LARGE_PAGE_SIZE);
-
 	__data_start = . ;
 	.data :
 	{

Index: src/sys/arch/i386/conf/kern.ldscript
diff -u src/sys/arch/i386/conf/kern.ldscript:1.16 src/sys/arch/i386/conf/kern.ldscript:1.17
--- src/sys/arch/i386/conf/kern.ldscript:1.16	Sat Nov 28 18:08:40 2015
+++ src/sys/arch/i386/conf/kern.ldscript	Thu May 12 06:45:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript,v 1.16 2015/11/28 18:08:40 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.17 2016/05/12 06:45:16 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -15,17 +15,19 @@ SECTIONS
 	_etext = . ;
 	PROVIDE (etext = .) ;
 
+	/*
+	 * Adjust the address for the rodata segment.  We want to adjust up to
+	 * the same address within the page on the next page up.
+	 */
+	. = ALIGN(0x1000) + (. & (0x1000 - 1));
+
+	__rodata_start = . ;
 	.rodata :
 	{
 		*(.rodata)
 		*(.rodata.*)
 	}
 
-	/*
-	 * Adjust the address for the data segment.  We want to adjust up to
-	 * the same address within the page on the next page up.
-	 */
-	. = ALIGN(0x1000) + (. & (0x1000 - 1));
 	__data_start = . ;
 	.data :
 	{

CVS commit: src/sys/arch/amd64

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 12 07:21:18 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/amd64/conf: kern.ldscript

Log Message:
Map the rodata segment independently on amd64, and remove the W permission
on it.


To generate a diff of this commit:
cvs rdiff -u -r1.87 -r1.88 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/amd64/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.87 src/sys/arch/amd64/amd64/locore.S:1.88
--- src/sys/arch/amd64/amd64/locore.S:1.87	Thu May 12 06:45:16 2016
+++ src/sys/arch/amd64/amd64/locore.S	Thu May 12 07:21:18 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.87 2016/05/12 06:45:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.88 2016/05/12 07:21:18 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -564,6 +564,8 @@ no_NOX:
  *
  * (PROC0 STK + L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
  *
+ * Important note: the kernel segments are properly 4k-aligned
+ * (see kern.ldscript), so there's no need to enforce alignment.
  */
 
 	/* Find end of kernel image; brings us on (1). */
@@ -616,16 +618,16 @@ no_NOX:
 	shrl	$(PGSHIFT-3),%ecx	/* ((n >> PGSHIFT) << 3) for # PDEs */
 	addl	%ecx,%ebx
 
-	/*
-	 * Compute &__rodata_start - KERNBASE. This can't be > 4G, or we can't
-	 * deal with it anyway, since we can't load it in 32 bit mode. So use
-	 * the bottom 32 bits.
-	 */
-	movl	$RELOC(__rodata_start),%edx
-	andl	$~PGOFSET,%edx
+	/* Map the kernel text RX. */
+	movl	$RELOC(__rodata_start),%ecx
+	subl	%eax,%ecx
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KR),%eax
+	fillkpt
 
-	/* Map the kernel text read-only. */
-	movl	%edx,%ecx
+	/* Map the kernel rodata ROX. */
+	movl	$RELOC(__rodata_start),%eax
+	movl	$RELOC(__data_start),%ecx
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KR),%eax
@@ -639,11 +641,12 @@ no_NOX:
 	 *  ++--+---+--+
 	 * We just map everything from < to > with RWX rights.
 	 */
-	leal	(PG_V|PG_KW)(%edx),%eax
-	movl	$TABLESIZE,%ecx
-	addl	%esi,%ecx		/* end of tables */
-	subl	%edx,%ecx		/* subtract end of text */
+	movl	$RELOC(__data_start),%eax
+	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
+	addl	$TABLESIZE,%ecx		/* end of BOOTSTRAP TABLES */
+	subl	%eax,%ecx		/* subtract start of DATA */
 	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
 	fillkpt
 
 	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */

Index: src/sys/arch/amd64/conf/kern.ldscript
diff -u src/sys/arch/amd64/conf/kern.ldscript:1.19 src/sys/arch/amd64/conf/kern.ldscript:1.20
--- src/sys/arch/amd64/conf/kern.ldscript:1.19	Thu May 12 06:45:16 2016
+++ src/sys/arch/amd64/conf/kern.ldscript	Thu May 12 07:21:18 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript,v 1.19 2016/05/12 06:45:16 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.20 2016/05/12 07:21:18 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -36,6 +36,8 @@ SECTIONS
 		*(.rodata.*)
 	}
 
+	. = ALIGN(__PAGE_SIZE);
+
 	__data_start = . ;
 	.data :
 	{



CVS commit: src/sys/arch/amd64/include

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 12:48:31 UTC 2016

Modified Files:
src/sys/arch/amd64/include: pmap.h

Log Message:
KNF so it appears aligned on NXR, and fix a comment.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/arch/amd64/include/pmap.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/include/pmap.h
diff -u src/sys/arch/amd64/include/pmap.h:1.35 src/sys/arch/amd64/include/pmap.h:1.36
--- src/sys/arch/amd64/include/pmap.h:1.35	Fri Jan  9 01:08:49 2015
+++ src/sys/arch/amd64/include/pmap.h	Sat May 14 12:48:31 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.h,v 1.35 2015/01/09 01:08:49 riastradh Exp $	*/
+/*	$NetBSD: pmap.h,v 1.36 2016/05/14 12:48:31 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -161,21 +161,20 @@
 #define PDIR_SLOT_PTE	L4_SLOT_PTE
 
 /*
- * the following defines give the virtual addresses of various MMU
+ * The following defines give the virtual addresses of various MMU
  * data structures:
  * PTE_BASE: the base VA of the linear PTE mappings
- * PTD_BASE: the base VA of the recursive mapping of the PTD
+ * PDP_BASE: the base VA of the recursive mapping of the PTD
  * PDP_PDE: the VA of the PDE that points back to the PDP
- *
  */
 
-#define PTE_BASE  ((pt_entry_t *) (L4_SLOT_PTE * NBPD_L4))
-#define KERN_BASE  ((pt_entry_t *) (L4_SLOT_KERN * NBPD_L4))
+#define PTE_BASE	((pt_entry_t *)(L4_SLOT_PTE * NBPD_L4))
+#define KERN_BASE	((pt_entry_t *)(L4_SLOT_KERN * NBPD_L4))
 
-#define L1_BASE		PTE_BASE
-#define L2_BASE ((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))
-#define L3_BASE ((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2))
-#define L4_BASE ((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1))
+#define L1_BASE	PTE_BASE
+#define L2_BASE	((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))
+#define L3_BASE	((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2))
+#define L4_BASE	((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1))
 
 #define PDP_PDE		(L4_BASE + PDIR_SLOT_PTE)
 
@@ -307,7 +306,7 @@ pmap_pte_setbits(volatile pt_entry_t *pt
 
 static __inline void
 pmap_pte_clearbits(volatile pt_entry_t *pte, pt_entry_t bits)
-{	
+{
 	mutex_enter(_lock);
 	xpq_queue_pte_update(xpmap_ptetomach(__UNVOLATILE(pte)),
 	(*pte) & ~bits);



CVS commit: src/doc

2016-05-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon May 16 07:37:45 UTC 2016

Modified Files:
src/doc: CHANGES

Log Message:
Mention fine-grained permissions and large pages on x86.


To generate a diff of this commit:
cvs rdiff -u -r1.2161 -r1.2162 src/doc/CHANGES

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/doc/CHANGES
diff -u src/doc/CHANGES:1.2161 src/doc/CHANGES:1.2162
--- src/doc/CHANGES:1.2161	Mon May 16 04:20:13 2016
+++ src/doc/CHANGES	Mon May 16 07:37:45 2016
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.2161 $>
+# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.2162 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -289,3 +289,7 @@ Changes from NetBSD 7.0 to NetBSD 8.0:
 	openssl: Import openssl 1.0.1t - security fixes [christos 20160503]
 	acpi(4): Updated ACPICA to 20160422. [christos 20160504]
 	dhcpcd(8): Import dhcpcd-6.11.0 [roy 20160509]
+	x86: Map the kernel image segments independently with fine-grained
+		permissions on amd64 and i386. [maxv 20160514]
+	x86: Use processor-specific features to optimize memory access time to
+		the kernel image on amd64 and i386. [maxv 20160515]



CVS commit: src/sys/arch/i386/conf

2016-05-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon May 16 07:52:31 UTC 2016

Modified Files:
src/sys/arch/i386/conf: kern.ldscript.4MB

Log Message:
Update kern.ldscript.4MB. It is the same as kern.ldscript, but with a large
page alignment before rodata.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/i386/conf/kern.ldscript.4MB

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/conf/kern.ldscript.4MB
diff -u src/sys/arch/i386/conf/kern.ldscript.4MB:1.16 src/sys/arch/i386/conf/kern.ldscript.4MB:1.17
--- src/sys/arch/i386/conf/kern.ldscript.4MB:1.16	Tue Aug 25 12:56:58 2015
+++ src/sys/arch/i386/conf/kern.ldscript.4MB	Mon May 16 07:52:31 2016
@@ -1,69 +1,88 @@
-/*	$NetBSD: kern.ldscript.4MB,v 1.16 2015/08/25 12:56:58 uebayasi Exp $	*/
+/*	$NetBSD: kern.ldscript.4MB,v 1.17 2016/05/16 07:52:31 maxv Exp $	*/
 
 #include "assym.h"
 
+/*
+ * The large page size is 4MB in the non-PAE case.
+ */
+
+__PAGE_SIZE = 0x1000 ;
+__LARGE_PAGE_SIZE = 0x40 ;
+
 ENTRY(_start)
 SECTIONS
 {
-  /* Read-only sections, merged into text segment: */
-  .text :
-  {
-*(.text)
-*(.text.*)
-*(.stub)
-  }
-  _etext = . ;
-  PROVIDE (etext = .) ;
-
-  .rodata :
-  {
-*(.rodata)
-*(.rodata.*)
-  }
-
-  /* Adjust the address for the data segment.  We push the data segment
- up to the next 4MB boundary so that we can map the text with large
- pages. */
-  . = ALIGN(0x40);
-  __data_start = . ;
-  .data :
-  {
-*(.data)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.cacheline_aligned :
-  {
-*(.data.cacheline_aligned)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  .data.read_mostly :
-  {
-*(.data.read_mostly)
-  }
-  . = ALIGN(COHERENCY_UNIT);
-  _edata = . ;
-  PROVIDE (edata = .) ;
-  __bss_start = . ;
-  .bss :
-  {
-*(.bss)
-*(.bss.*)
-*(COMMON)
-. = ALIGN(32 / 8);
-  }
-  . = ALIGN(32 / 8);
-  _end = . ;
-  PROVIDE (end = .) ;
-  .note.netbsd.ident :
-  {
-KEEP(*(.note.netbsd.ident));
-  }
+	.text :
+	{
+		*(.text)
+		*(.text.*)
+		*(.stub)
+	}
+	_etext = . ;
+	PROVIDE (etext = .) ;
+
+	/*
+	 * Push the rodata segment up to the next large page boundary so that we
+	 * can map the text segment with large pages.
+	 */
+	. = ALIGN(__LARGE_PAGE_SIZE);
+
+	__rodata_start = . ;
+	.rodata :
+	{
+		*(.rodata)
+		*(.rodata.*)
+	}
+
+	. = ALIGN(__PAGE_SIZE);
+
+	__data_start = . ;
+	.data :
+	{
+		*(.data)
+	}
+
+	. = ALIGN(COHERENCY_UNIT);
+	.data.cacheline_aligned :
+	{
+		*(.data.cacheline_aligned)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+	.data.read_mostly :
+	{
+		*(.data.read_mostly)
+	}
+	. = ALIGN(COHERENCY_UNIT);
+
+	_edata = . ;
+	PROVIDE (edata = .) ;
+	__bss_start = . ;
+	.bss :
+	{
+		*(.bss)
+		*(.bss.*)
+		*(COMMON)
+		. = ALIGN(32 / 8);
+	}
+
+	. = ALIGN(__PAGE_SIZE);
+
+	/* End of the kernel image */
+	__kernel_end = . ;
+
+	_end = . ;
+	PROVIDE (end = .) ;
+	.note.netbsd.ident :
+	{
+		KEEP(*(.note.netbsd.ident));
+	}
 }
+
 SECTIONS
 {
-  .text :
-  AT (ADDR(.text) & 0x0fff)
-  {
-*(.text)
-  } =0
+	.text :
+	AT (ADDR(.text) & 0x0fff)
+	{
+		*(.text)
+	} = 0
 }



CVS commit: src/sys/arch

2016-05-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 15 07:01:36 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
Reduce the diff between amd64 and i386. We invert two instructions on
amd64, but it makes no difference since PDE_SIZE = 8.


To generate a diff of this commit:
cvs rdiff -u -r1.90 -r1.91 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.122 -r1.123 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.90 src/sys/arch/amd64/amd64/locore.S:1.91
--- src/sys/arch/amd64/amd64/locore.S:1.90	Thu May 12 09:05:16 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sun May 15 07:01:36 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.90 2016/05/12 09:05:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.91 2016/05/15 07:01:36 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -220,23 +220,23 @@
  * Each entry is 8 (PDE_SIZE) bytes long: we must set the 4 upper bytes to 0.
  */
 #define fillkpt	\
-1:	movl	%eax,(%ebx)		; 	/* store phys addr */	\
-	movl	$0,(PDE_SIZE-4)(%ebx)	; 	/* upper 32 bits: 0 */	\
-	addl	$PDE_SIZE,%ebx		; 	/* next PTE/PDE */	\
-	addl	$PAGE_SIZE,%eax		; 	/* next phys page */	\
+1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
+	movl	%eax,(%ebx)		;	/* store phys addr */	\
+	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
+	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
 	loop	1b			;
 
 /*
  * fillkpt_nox - Same as fillkpt, but sets the NX/XD bit.
  */
 #define fillkpt_nox \
-	pushl	%ebp			;	\
-	movl	RELOC(nox_flag),%ebp	;	\
-1:	movl	%eax,(%ebx)		;	/* store phys addr */	\
-	movl	%ebp,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: NX */ \
+	pushl	%ebp			; \
+	movl	RELOC(nox_flag),%ebp	; \
+1:	movl	%ebp,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: NX */ \
+	movl	%eax,(%ebx)		;	/* store phys addr */	\
 	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
 	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
-	loop	1b			;	\
+	loop	1b			; \
 	popl	%ebp			;
 
 /*
@@ -415,7 +415,7 @@ ENTRY(start)
 
 	/*
 	 * Load parameters from the stack (32 bits):
-	 * boothowto, [bootdev], bootinfo, esym, biosbasemem, biosextmem
+	 * boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem
 	 * We are not interested in 'bootdev'.
 	 */
 
@@ -605,9 +605,9 @@ no_NOX:
 	movl	%edi,%esi
 	addl	$PGOFSET,%esi
 	andl	$~PGOFSET,%esi
-	movl	%esi,%edi
 
 	/* Now, zero out the BOOTSTRAP TABLES (before filling them in). */
+	movl	%esi,%edi
 	xorl	%eax,%eax
 	cld
 	movl	$TABLESIZE,%ecx
@@ -638,7 +638,7 @@ no_NOX:
 	orl	$(PG_V|PG_KR),%eax
 	fillkpt
 
-	/* Map the kernel rodata RO. */
+	/* Map the kernel rodata R. */
 	movl	$RELOC(__rodata_start),%eax
 	movl	$RELOC(__data_start),%ecx
 	subl	%eax,%ecx

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.122 src/sys/arch/i386/i386/locore.S:1.123
--- src/sys/arch/i386/i386/locore.S:1.122	Sat May 14 09:51:56 2016
+++ src/sys/arch/i386/i386/locore.S	Sun May 15 07:01:36 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.122 2016/05/14 09:51:56 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.123 2016/05/15 07:01:36 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.122 2016/05/14 09:51:56 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.123 2016/05/15 07:01:36 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -191,7 +191,6 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
  * This is done by the first instruction of fillkpt. In the non-PAE case, this
  * instruction just clears the page table entry.
  */
-
 #define fillkpt	\
 1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: 0 */	\
 	movl	%eax,(%ebx)		;	/* store phys addr */	\
@@ -612,6 +611,9 @@ no_NOX:
  *
  * PROC0 STK is obviously not linked as a page level. It just happens to be
  * caught between L2 and L1.
+ *
+ * Important note: the kernel segments are properly 4k-aligned
+ * (see kern.ldscript), so there's no need to enforce alignment.
  */
 
 	/* Find end of kernel image; brings us on (1). */
@@ -732,7 +734,7 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt
 
-	/* Map ISA I/O mem (later atdevbase) */
+	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
 	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
 	movl	$(IOM_SIZE>>PGSHIFT),%ecx
 	fillkpt



CVS commit: src/sys/arch

2016-05-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 15 07:17:53 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S
src/sys/arch/i386/i386: locore.S

Log Message:
Split the PRELOADED_MODULES+BOOTSTRAP_TABLES chunk into two separate
chunks mapped independently with RWX and RW, on both amd64 and i386.

This way the BOOTSTRAP TABLES are non-executable.


To generate a diff of this commit:
cvs rdiff -u -r1.91 -r1.92 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.123 -r1.124 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.91 src/sys/arch/amd64/amd64/locore.S:1.92
--- src/sys/arch/amd64/amd64/locore.S:1.91	Sun May 15 07:01:36 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sun May 15 07:17:53 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.91 2016/05/15 07:01:36 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.92 2016/05/15 07:17:53 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -654,22 +654,21 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/*
-	 * We actually have to be careful here. The memory layout is as
-	 * follows:
-	 *+--+-+--+
-	 *| DATA+BSS < [PRELOADED MODULES] | BOOTSTRAP TABLES >
-	 *+--+-+--+
-	 * We just map everything from < to > with RWX rights.
-	 */
+	/* Map [SYMS]+[PRELOADED MODULES] RWX. */
 	movl	$RELOC(__kernel_end),%eax
 	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
-	addl	$TABLESIZE,%ecx		/* end of BOOTSTRAP TABLES */
-	subl	%eax,%ecx		/* subtract end of kernel image */
+	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt
 
+	/* Map the BOOTSTRAP TABLES RW. */
+	movl	%esi,%eax		/* start of BOOTSTRAP TABLES */
+	movl	$TABLESIZE,%ecx		/* length of BOOTSTRAP TABLES */
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
+	fillkpt_nox
+
 	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
 	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
 	movl	$(IOM_SIZE>>PGSHIFT),%ecx

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.123 src/sys/arch/i386/i386/locore.S:1.124
--- src/sys/arch/i386/i386/locore.S:1.123	Sun May 15 07:01:36 2016
+++ src/sys/arch/i386/i386/locore.S	Sun May 15 07:17:53 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.123 2016/05/15 07:01:36 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.124 2016/05/15 07:17:53 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.123 2016/05/15 07:01:36 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.124 2016/05/15 07:17:53 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -718,22 +718,21 @@ no_NOX:
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt_nox
 
-	/*
-	 * We actually have to be careful here. The memory layout is as
-	 * follows:
-	 *+--+-+--+
-	 *| DATA+BSS < [PRELOADED MODULES] | BOOTSTRAP TABLES >
-	 *+--+-+--+
-	 * We just map everything from < to > with RWX rights.
-	 */
+	/* Map [SYMS]+[PRELOADED MODULES] RWX. */
 	movl	$RELOC(__kernel_end),%eax
 	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
-	addl	RELOC(tablesize),%ecx	/* end of BOOTSTRAP TABLES */
-	subl	%eax,%ecx		/* subtract end of kernel image */
+	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KW),%eax
 	fillkpt
 
+	/* Map the BOOTSTRAP TABLES RW. */
+	movl	%esi,%eax		/* start of BOOTSTRAP TABLES */
+	movl	RELOC(tablesize),%ecx	/* length of BOOTSTRAP TABLES */
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
+	fillkpt_nox
+
 	/* We are on (4). Map ISA I/O mem (later atdevbase) RWX. */
 	movl	$(IOM_BEGIN|PG_V|PG_KW/*|PG_N*/),%eax
 	movl	$(IOM_SIZE>>PGSHIFT),%ecx



CVS commit: src/sys/arch

2016-05-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May 15 10:35:54 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: machdep.c
src/sys/arch/i386/i386: machdep.c
src/sys/arch/x86/x86: pmap.c

Log Message:
Explicitly mention MP_TRAMPOLINE in these comments, so that NXR links them.


To generate a diff of this commit:
cvs rdiff -u -r1.216 -r1.217 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.754 -r1.755 src/sys/arch/i386/i386/machdep.c
cvs rdiff -u -r1.194 -r1.195 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.216 src/sys/arch/amd64/amd64/machdep.c:1.217
--- src/sys/arch/amd64/amd64/machdep.c:1.216	Thu May 12 06:45:16 2016
+++ src/sys/arch/amd64/amd64/machdep.c	Sun May 15 10:35:54 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.216 2016/05/12 06:45:16 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.217 2016/05/15 10:35:54 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.216 2016/05/12 06:45:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.217 2016/05/15 10:35:54 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1579,7 +1579,7 @@ init_x86_64(paddr_t first_avail)
 	 * Low memory reservations:
 	 * Page 0:	BIOS data
 	 * Page 1:	BIOS callback (not used yet, for symmetry with i386)
-	 * Page 2:	MP bootstrap
+	 * Page 2:	MP bootstrap code (MP_TRAMPOLINE)
 	 * Page 3:	ACPI wakeup code (ACPI_WAKEUP_ADDR)
 	 * Page 4:	Temporary page table for 0MB-4MB
 	 * Page 5:	Temporary page directory

Index: src/sys/arch/i386/i386/machdep.c
diff -u src/sys/arch/i386/i386/machdep.c:1.754 src/sys/arch/i386/i386/machdep.c:1.755
--- src/sys/arch/i386/i386/machdep.c:1.754	Fri Apr 24 00:04:04 2015
+++ src/sys/arch/i386/i386/machdep.c	Sun May 15 10:35:54 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.754 2015/04/24 00:04:04 khorben Exp $	*/
+/*	$NetBSD: machdep.c,v 1.755 2016/05/15 10:35:54 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.754 2015/04/24 00:04:04 khorben Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.755 2016/05/15 10:35:54 maxv Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -1197,8 +1197,8 @@ init386(paddr_t first_avail)
 	 * Low memory reservations:
 	 * Page 0:	BIOS data
 	 * Page 1:	BIOS callback
-	 * Page 2:	MP bootstrap
-	 * Page 3:	ACPI wakeup code
+	 * Page 2:	MP bootstrap code (MP_TRAMPOLINE)
+	 * Page 3:	ACPI wakeup code (ACPI_WAKEUP_ADDR)
 	 * Page 4:	Temporary page table for 0MB-4MB
 	 * Page 5:	Temporary page directory
 	 */

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.194 src/sys/arch/x86/x86/pmap.c:1.195
--- src/sys/arch/x86/x86/pmap.c:1.194	Sat May 14 09:37:21 2016
+++ src/sys/arch/x86/x86/pmap.c	Sun May 15 10:35:54 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.194 2016/05/14 09:37:21 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.195 2016/05/15 10:35:54 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.194 2016/05/14 09:37:21 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.195 2016/05/15 10:35:54 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -4531,15 +4531,15 @@ pmap_init_tmp_pgtbl(paddr_t pg)
 {
 	static bool maps_loaded;
 	static const paddr_t x86_tmp_pml_paddr[] = {
-	4 * PAGE_SIZE,
-	5 * PAGE_SIZE,
-	6 * PAGE_SIZE,
-	7 * PAGE_SIZE
+	4 * PAGE_SIZE,	/* L1 */
+	5 * PAGE_SIZE,	/* L2 */
+	6 * PAGE_SIZE,	/* L3 */
+	7 * PAGE_SIZE	/* L4 */
 	};
 	static vaddr_t x86_tmp_pml_vaddr[] = { 0, 0, 0, 0 };
 
 	pd_entry_t *tmp_pml, *kernel_pml;
-	
+
 	int level;
 
 	if (!maps_loaded) {



CVS commit: src/sys/arch/x86/x86

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 11:47:02 UTC 2016

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Actually, make the NOX part amd64-specific. The secondary CPUs bug is not
yet fixed on i386.


To generate a diff of this commit:
cvs rdiff -u -r1.192 -r1.193 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.192 src/sys/arch/x86/x86/pmap.c:1.193
--- src/sys/arch/x86/x86/pmap.c:1.192	Fri May 13 10:24:42 2016
+++ src/sys/arch/x86/x86/pmap.c	Fri May 13 11:47:02 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.192 2016/05/13 10:24:42 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.193 2016/05/13 11:47:02 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.192 2016/05/13 10:24:42 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.193 2016/05/13 11:47:02 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1343,7 +1343,12 @@ pmap_bootstrap(vaddr_t kva_start)
 		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
 		pa += NBPD_L2) {
 			pde = _BASE[pl2_i(kva)];
+#ifdef __x86_64__
 			*pde = pa | pmap_pg_g | PG_PS | pg_nx | PG_KR | PG_V;
+#else
+			*pde = pa | pmap_pg_g | PG_PS | PG_KR | PG_V;
+#endif
+
 			tlbflushg();
 		}
 
@@ -1354,7 +1359,11 @@ pmap_bootstrap(vaddr_t kva_start)
 		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
 		pa += NBPD_L2) {
 			pde = _BASE[pl2_i(kva)];
+#ifdef __x86_64__
 			*pde = pa | pmap_pg_g | PG_PS | pg_nx | PG_KW | PG_V;
+#else
+			*pde = pa | pmap_pg_g | PG_PS | PG_KW | PG_V;
+#endif
 			tlbflushg();
 		}
 	}



CVS commit: src/sys/arch/i386/conf

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 10:18:02 UTC 2016

Modified Files:
src/sys/arch/i386/conf: kern.ldscript

Log Message:
Define __kernel_end.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/i386/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/conf/kern.ldscript
diff -u src/sys/arch/i386/conf/kern.ldscript:1.17 src/sys/arch/i386/conf/kern.ldscript:1.18
--- src/sys/arch/i386/conf/kern.ldscript:1.17	Thu May 12 06:45:16 2016
+++ src/sys/arch/i386/conf/kern.ldscript	Fri May 13 10:18:01 2016
@@ -1,11 +1,12 @@
-/*	$NetBSD: kern.ldscript,v 1.17 2016/05/12 06:45:16 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.18 2016/05/13 10:18:01 maxv Exp $	*/
 
 #include "assym.h"
 
+__PAGE_SIZE = 0x1000 ;
+
 ENTRY(_start)
 SECTIONS
 {
-	/* Read-only sections, merged into text segment: */
 	.text :
 	{
 		*(.text)
@@ -45,6 +46,7 @@ SECTIONS
 		*(.data.read_mostly)
 	}
 	. = ALIGN(COHERENCY_UNIT);
+
 	_edata = . ;
 	PROVIDE (edata = .) ;
 	__bss_start = . ;
@@ -55,7 +57,12 @@ SECTIONS
 		*(COMMON)
 	. = ALIGN(32 / 8);
 	}
-	. = ALIGN(32 / 8);
+
+	. = ALIGN(__PAGE_SIZE);
+
+	/* End of the kernel image */
+	__kernel_end = . ;
+
 	_end = . ;
 	PROVIDE (end = .) ;
 	.note.netbsd.ident :



CVS commit: src/sys/arch/x86/x86

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 10:24:42 UTC 2016

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
Remap the rodata and data+bss segments with large pages on x86. There still
is a bug in the way the text segment is mapped, but I'll see later.


To generate a diff of this commit:
cvs rdiff -u -r1.191 -r1.192 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.191 src/sys/arch/x86/x86/pmap.c:1.192
--- src/sys/arch/x86/x86/pmap.c:1.191	Thu May 12 06:45:16 2016
+++ src/sys/arch/x86/x86/pmap.c	Fri May 13 10:24:42 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.191 2016/05/12 06:45:16 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.192 2016/05/13 10:24:42 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.191 2016/05/12 06:45:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.192 2016/05/13 10:24:42 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1272,7 +1272,6 @@ pmap_bootstrap(vaddr_t kva_start)
 	 * which happens in cpu_init(), which is run on each cpu
 	 * (and happens later)
 	 */
-
 	if (cpu_feature[0] & CPUID_PGE) {
 		pmap_pg_g = PG_G;		/* enable software */
 
@@ -1296,18 +1295,19 @@ pmap_bootstrap(vaddr_t kva_start)
 	}
 
 	/*
-	 * enable large pages if they are supported.
+	 * Enable large pages if they are supported.
 	 */
-
 	if (cpu_feature[0] & CPUID_PSE) {
 		paddr_t pa;
 		extern char __rodata_start;
+		extern char __data_start;
+		extern char __kernel_end;
 
 		lcr4(rcr4() | CR4_PSE);	/* enable hardware (via %cr4) */
 		pmap_largepages = 1;	/* enable software */
 
 		/*
-		 * the TLB must be flushed after enabling large pages
+		 * The TLB must be flushed after enabling large pages
 		 * on Pentium CPUs, according to section 3.6.2.2 of
 		 * "Intel Architecture Software Developer's Manual,
 		 * Volume 3: System Programming".
@@ -1315,16 +1315,18 @@ pmap_bootstrap(vaddr_t kva_start)
 		tlbflushg();
 
 		/*
-		 * now, remap the kernel text using large pages.  we
-		 * assume that the linker has properly aligned the
-		 * .rodata segment to a NBPD_L2 boundary.
+		 * Now, we remap several kernel segments with large pages. We
+		 * cover as many pages as we can.
 		 */
+
+		/* Remap the kernel text using large pages. */
+		kva = KERNBASE;
 		kva_end = rounddown((vaddr_t)&__rodata_start, NBPD_L1);
-		for (pa = 0, kva = KERNBASE; kva + NBPD_L2 <= kva_end;
-		 kva += NBPD_L2, pa += NBPD_L2) {
+		pa = kva - KERNBASE;
+		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
+		pa += NBPD_L2) {
 			pde = _BASE[pl2_i(kva)];
-			*pde = pa | pmap_pg_g | PG_PS |
-			PG_KR | PG_V;	/* zap! */
+			*pde = pa | pmap_pg_g | PG_PS | PG_KR | PG_V;
 			tlbflushg();
 		}
 #if defined(DEBUG)
@@ -1333,6 +1335,28 @@ pmap_bootstrap(vaddr_t kva_start)
 		howmany(kva - KERNBASE, NBPD_L2),
 		howmany((vaddr_t)&__rodata_start - kva, NBPD_L1));
 #endif /* defined(DEBUG) */
+
+		/* Remap the kernel rodata using large pages. */
+		kva = roundup((vaddr_t)&__rodata_start, NBPD_L2);
+		kva_end = rounddown((vaddr_t)&__data_start, NBPD_L1);
+		pa = kva - KERNBASE;
+		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
+		pa += NBPD_L2) {
+			pde = _BASE[pl2_i(kva)];
+			*pde = pa | pmap_pg_g | PG_PS | pg_nx | PG_KR | PG_V;
+			tlbflushg();
+		}
+
+		/* Remap the kernel data+bss using large pages. */
+		kva = roundup((vaddr_t)&__data_start, NBPD_L2);
+		kva_end = rounddown((vaddr_t)&__kernel_end, NBPD_L1);
+		pa = kva - KERNBASE;
+		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
+		pa += NBPD_L2) {
+			pde = _BASE[pl2_i(kva)];
+			*pde = pa | pmap_pg_g | PG_PS | pg_nx | PG_KW | PG_V;
+			tlbflushg();
+		}
 	}
 #endif /* !XEN */
 



CVS commit: src/sys/arch/amd64/include

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 11:17:20 UTC 2016

Modified Files:
src/sys/arch/amd64/include: pte.h

Log Message:
KNF, so it appears aligned on NXR.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/amd64/include/pte.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/include/pte.h
diff -u src/sys/arch/amd64/include/pte.h:1.8 src/sys/arch/amd64/include/pte.h:1.9
--- src/sys/arch/amd64/include/pte.h:1.8	Sat Jul 24 17:43:47 2010
+++ src/sys/arch/amd64/include/pte.h	Fri May 13 11:17:20 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pte.h,v 1.8 2010/07/24 17:43:47 njoly Exp $	*/
+/*	$NetBSD: pte.h,v 1.9 2016/05/13 11:17:20 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -44,7 +44,7 @@
  * amd64 MMU hardware structure:
  *
  * the (first generation) amd64 MMU is a 4-level MMU which maps 2^48 bytes
- * of  virtual memory. The  pagesize we use is 4K (4096 [0x1000] bytes), 
+ * of virtual memory. The pagesize we use is 4K (4096 [0x1000] bytes),
  * although 2M and 4M can be used as well. The indexes in the levels
  * are 9 bits wide (512 64bit entries per level), dividing the bits
  * 9-9-9-9-12.
@@ -65,28 +65,24 @@
  */
 
 #if !defined(_LOCORE)
-
 /*
- * here we define the data types for PDEs and PTEs
+ * Here we define the data types for PDEs and PTEs.
  */
-
 typedef uint64_t pd_entry_t;		/* PDE */
 typedef uint64_t pt_entry_t;		/* PTE */
-
 #endif
 
 /*
- * now we define various for playing with virtual addresses
+ * Now we define various constants for playing with virtual addresses.
  */
-
 #define L1_SHIFT	12
-#define	L2_SHIFT	21
-#define	L3_SHIFT	30
-#define	L4_SHIFT	39
-#define	NBPD_L1		(1UL << L1_SHIFT) /* # bytes mapped by L1 ent (4K) */
-#define	NBPD_L2		(1UL << L2_SHIFT) /* # bytes mapped by L2 ent (2MB) */
-#define	NBPD_L3		(1UL << L3_SHIFT) /* # bytes mapped by L3 ent (1G) */
-#define	NBPD_L4		(1UL << L4_SHIFT) /* # bytes mapped by L4 ent (512G) */
+#define L2_SHIFT	21
+#define L3_SHIFT	30
+#define L4_SHIFT	39
+#define NBPD_L1		(1UL << L1_SHIFT) /* # bytes mapped by L1 ent (4K) */
+#define NBPD_L2		(1UL << L2_SHIFT) /* # bytes mapped by L2 ent (2MB) */
+#define NBPD_L3		(1UL << L3_SHIFT) /* # bytes mapped by L3 ent (1G) */
+#define NBPD_L4		(1UL << L4_SHIFT) /* # bytes mapped by L4 ent (512G) */
 
 #define L4_MASK		0xff80
 #define L3_MASK		0x007fc000
@@ -101,16 +97,15 @@ typedef uint64_t pt_entry_t;		/* PTE */
 /*
  * PDE/PTE bits. These are no different from their i386 counterparts.
  */
-
-#define	PG_V		0x0001	/* valid */
-#define	PG_RO		0x	/* read-only */
-#define	PG_RW		0x0002	/* read-write */
-#define	PG_u		0x0004	/* user accessible */
-#define	PG_PROT		0x0006
+#define PG_V		0x0001	/* valid */
+#define PG_RO		0x	/* read-only */
+#define PG_RW		0x0002	/* read-write */
+#define PG_u		0x0004	/* user accessible */
+#define PG_PROT		0x0006
 #define PG_WT		0x0008	/* write-through */
-#define	PG_N		0x0010	/* non-cacheable */
-#define	PG_U		0x0020	/* used */
-#define	PG_M		0x0040	/* modified */
+#define PG_N		0x0010	/* non-cacheable */
+#define PG_U		0x0020	/* used */
+#define PG_M		0x0040	/* modified */
 #define PG_PAT		0x0080	/* PAT (on pte) */
 #define PG_PS		0x0080	/* 2MB page size (on pde) */
 #define PG_G		0x0100	/* not flushed */
@@ -118,19 +113,18 @@ typedef uint64_t pt_entry_t;		/* PTE */
 #define PG_AVAIL2	0x0400
 #define PG_AVAIL3	0x0800
 #define PG_LGPAT	0x1000	/* PAT on large pages */
-#define	PG_FRAME	0x000ff000
-#define	PG_NX		0x8000
+#define PG_FRAME	0x000ff000
+#define PG_NX		0x8000
 
 #define PG_2MFRAME	0x000fffe0	/* large (2M) page frame mask */
 #define PG_1GFRAME	0x000fc000	/* large (1G) page frame mask */
-#define	PG_LGFRAME	PG_2MFRAME
+#define PG_LGFRAME	PG_2MFRAME
 
 /*
- * short forms of protection codes
+ * Short forms of protection codes.
  */
-
-#define	PG_KR		0x	/* kernel read-only */
-#define	PG_KW		0x0002	/* kernel read-write */
+#define PG_KR		0x	/* kernel read-only */
+#define PG_KW		0x0002	/* kernel read-write */
 
 #include 
 



CVS commit: src/sys/arch

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 05:45:13 UTC 2016

Modified Files:
src/sys/arch/amd64/conf: kern.ldscript.Xen
src/sys/arch/i386/conf: kern.ldscript.Xen

Log Message:
Xen therefore uses x86/db_memrw.c, as I suspected. Define __rodata_start
in the Xen ld scripts, so that it can compile. We put the __rodata_start
definition right before __data_start, for it to appear as dead code, since
the rodata segment is not yet mapped independently on Xen.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/conf/kern.ldscript.Xen
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/i386/conf/kern.ldscript.Xen

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/conf/kern.ldscript.Xen
diff -u src/sys/arch/amd64/conf/kern.ldscript.Xen:1.11 src/sys/arch/amd64/conf/kern.ldscript.Xen:1.12
--- src/sys/arch/amd64/conf/kern.ldscript.Xen:1.11	Thu May 12 06:57:55 2016
+++ src/sys/arch/amd64/conf/kern.ldscript.Xen	Fri May 13 05:45:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript.Xen,v 1.11 2016/05/12 06:57:55 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript.Xen,v 1.12 2016/05/13 05:45:13 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -24,6 +24,7 @@ SECTIONS
 	 */
 	. = ALIGN(0x10) + (. & (0x10 - 1));
 
+	__rodata_start = . ;
 	__data_start = . ;
 	.data :
 	{

Index: src/sys/arch/i386/conf/kern.ldscript.Xen
diff -u src/sys/arch/i386/conf/kern.ldscript.Xen:1.11 src/sys/arch/i386/conf/kern.ldscript.Xen:1.12
--- src/sys/arch/i386/conf/kern.ldscript.Xen:1.11	Thu May 12 06:57:55 2016
+++ src/sys/arch/i386/conf/kern.ldscript.Xen	Fri May 13 05:45:13 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript.Xen,v 1.11 2016/05/12 06:57:55 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript.Xen,v 1.12 2016/05/13 05:45:13 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -28,6 +28,7 @@ SECTIONS
 	 */
 	. = ALIGN(0x1000) + (. & (0x1000 - 1));
 
+	__rodata_start = . ;
 	__data_start = . ;
 	.data :
 	{



CVS commit: src/doc

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 14:09:38 UTC 2016

Modified Files:
src/doc: CHANGES

Log Message:
Mention SMEP.


To generate a diff of this commit:
cvs rdiff -u -r1.2159 -r1.2160 src/doc/CHANGES

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/doc/CHANGES
diff -u src/doc/CHANGES:1.2159 src/doc/CHANGES:1.2160
--- src/doc/CHANGES:1.2159	Mon May  9 10:21:42 2016
+++ src/doc/CHANGES	Fri May 13 14:09:38 2016
@@ -1,4 +1,4 @@
-# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.2159 $>
+# LIST OF CHANGES FROM LAST RELEASE:			<$Revision: 1.2160 $>
 #
 #
 # [Note: This file does not mention every change made to the NetBSD source tree.
@@ -233,6 +233,7 @@ Changes from NetBSD 7.0 to NetBSD 8.0:
 	bind: Import version 9.10.3-P2. [christos 20151216]
 	mdocml: Import 1.13.3. [christos 20151217]
 	sortinfo(1): Add utility to sort texinfo dir files [christos 20151218]
+	x86: Add support for SMEP on amd64 and i386. [maxv 20151219]
 	raidframe: Now built as a kernel module; it can be built-in at
 		kernel build time, or (for MODULAR kernels) loaded as
 		needed. [pgoyette 20151226]



CVS commit: src/sys/arch/i386/i386

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 13:24:01 UTC 2016

Modified Files:
src/sys/arch/i386/i386: copy.S

Log Message:
KNF a little, use C-style comments, and remove susword/fusword. No
functional changes.


To generate a diff of this commit:
cvs rdiff -u -r1.23 -r1.24 src/sys/arch/i386/i386/copy.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/copy.S
diff -u src/sys/arch/i386/i386/copy.S:1.23 src/sys/arch/i386/i386/copy.S:1.24
--- src/sys/arch/i386/i386/copy.S:1.23	Fri Jan 10 16:47:07 2014
+++ src/sys/arch/i386/i386/copy.S	Fri May 13 13:24:01 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: copy.S,v 1.23 2014/01/10 16:47:07 pedro Exp $	*/
+/*	$NetBSD: copy.S,v 1.24 2016/05/13 13:24:01 maxv Exp $	*/
 /*	NetBSD: locore.S,v 1.34 2005/04/01 11:59:31 yamt Exp $	*/
 
 /*-
@@ -65,7 +65,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.23 2014/01/10 16:47:07 pedro Exp $");
+__KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.24 2016/05/13 13:24:01 maxv Exp $");
 
 #include "assym.h"
 
@@ -86,10 +86,10 @@ __KERNEL_RCSID(0, "$NetBSD: copy.S,v 1.2
 #define DEFERRED_SWITCH_CHECK \
 	CHECK_DEFERRED_SWITCH			; \
 	jnz	99f; \
-98:
+	98:
 
 #define DEFERRED_SWITCH_CALL \
-99:		; \
+	99:		; \
 	call	_C_LABEL(do_pmap_load)		; \
 	jmp	98b
 
@@ -136,7 +136,7 @@ NENTRY(do_pmap_load)
  * `level' levels down the stack if level > 0.
  */
 ENTRY(return_address)
-	movl	%ebp,%eax	/* frame pointer -> %eax */ 
+	movl	%ebp,%eax	/* frame pointer -> %eax */
 	movl	4(%esp),%ecx	/* level -> %ecx */
 	movl	CPUVAR(CURLWP),%edx
 	movl	L_PCB(%edx),%edx
@@ -166,15 +166,15 @@ ENTRY(kcopy)
 .Lkcopy_start:
 	movl	%edi,%eax
 	subl	%esi,%eax
-	cmpl	%ecx,%eax		# overlapping?
+	cmpl	%ecx,%eax		/* overlapping? */
 	movl	%ecx,%edx
 	jb	1f
-	# nope, copy forward
-	shrl	$2,%ecx			# copy by 32-bit words
+	/* nope, copy forward */
+	shrl	$2,%ecx			/* copy by 32-bit words */
 	rep
 	movsl
 	movl	%edx,%ecx
-	andl	$3,%ecx			# any bytes left?
+	andl	$3,%ecx			/* any bytes left? */
 	jz	0f
 	rep
 	movsb
@@ -185,15 +185,15 @@ ENTRY(kcopy)
 	ret
 
 	ALIGN_TEXT
-1:	addl	%ecx,%edi		# copy backward
+1:	addl	%ecx,%edi		/* copy backward */
 	addl	%ecx,%esi
 	std
-	andl	$3,%ecx			# any fractional bytes?
+	andl	$3,%ecx			/* any fractional bytes? */
 	decl	%edi
 	decl	%esi
 	rep
 	movsb
-	movl	%edx,%ecx		# copy remainder by 32-bit words
+	movl	%edx,%ecx		/* copy remainder by 32-bit words */
 	shrl	$2,%ecx
 	subl	$3,%esi
 	subl	$3,%edi
@@ -337,11 +337,11 @@ ENTRY(copyoutstr)
 	DEFERRED_SWITCH_CHECK
 	pushl	%esi
 	pushl	%edi
-	movl	12(%esp),%esi		# esi = from
-	movl	16(%esp),%edi		# edi = to
-	movl	20(%esp),%edx		# edx = maxlen
+	movl	12(%esp),%esi		/* esi = from */
+	movl	16(%esp),%edi		/* edi = to */
+	movl	20(%esp),%edx		/* edx = maxlen */
 .Lcopyoutstr_start:
-5:	
+5:
 	/*
 	 * Get min(%edx, VM_MAXUSER_ADDRESS-%edi).
 	 */
@@ -388,9 +388,9 @@ ENTRY(copyinstr)
 	DEFERRED_SWITCH_CHECK
 	pushl	%esi
 	pushl	%edi
-	movl	12(%esp),%esi		# %esi = from
-	movl	16(%esp),%edi		# %edi = to
-	movl	20(%esp),%edx		# %edx = maxlen
+	movl	12(%esp),%esi		/* %esi = from */
+	movl	16(%esp),%edi		/* %edi = to */
+	movl	20(%esp),%edx		/* %edx = maxlen */
 
 	/*
 	 * Get min(%edx, VM_MAXUSER_ADDRESS-%esi).
@@ -457,9 +457,9 @@ ENTRY(copystr)
 	pushl	%esi
 	pushl	%edi
 
-	movl	12(%esp),%esi		# esi = from
-	movl	16(%esp),%edi		# edi = to
-	movl	20(%esp),%edx		# edx = maxlen
+	movl	12(%esp),%esi		/* esi = from */
+	movl	16(%esp),%edi		/* edi = to */
+	movl	20(%esp),%edx		/* edx = maxlen */
 	incl	%edx
 
 1:	decl	%edx
@@ -508,24 +508,6 @@ ENTRY(fuword)
 	DEFERRED_SWITCH_CALL
 
 /*
- * int fusword(const void *uaddr);
- * Fetch a short from the user's address space.
- * see fusword(9)
- */
-/* LINTSTUB: Func: int fusword(const void *base) */
-ENTRY(fusword)
-	DEFERRED_SWITCH_CHECK
-	movl	4(%esp),%edx
-	cmpl	$VM_MAXUSER_ADDRESS-2,%edx
-	ja	_C_LABEL(fusuaddrfault)
-	GET_CURPCB(%ecx)
-	movl	$_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
-	movzwl	(%edx),%eax
-	movl	$0,PCB_ONFAULT(%ecx)
-	ret
-	DEFERRED_SWITCH_CALL
-
-/*
  * int fuswintr(const void *uaddr);
  * Fetch a short from the user's address space.  Can be called during an
  * interrupt.
@@ -612,26 +594,6 @@ ENTRY(suword)
 	DEFERRED_SWITCH_CALL
 
 /*
- * int susword(void *uaddr, short x);
- * Store a short in the user's address space.
- * see susword(9)
- */
-/* LINTSTUB: Func: int susword(void *base, short c) */
-ENTRY(susword)
-	DEFERRED_SWITCH_CHECK
-	movl	4(%esp),%edx
-	cmpl	$VM_MAXUSER_ADDRESS-2,%edx
-	ja	_C_LABEL(fusuaddrfault)
-	GET_CURPCB(%ecx)
-	movl	$_C_LABEL(fusufault),PCB_ONFAULT(%ecx)
-	movl	8(%esp),%eax
-	movw	%ax,(%edx)
-	xorl	%eax,%eax
-	movl	%eax,PCB_ONFAULT(%ecx)
-	ret
-	DEFERRED_SWITCH_CALL
-
-/*
  * int suswintr(void *uaddr, short x);
  * Store a short in the user's address space.  Can be called during an
  * interrupt.
@@ -736,7 +698,7 @@ ENTRY(x86_copyargs)
 	cmpl	

CVS commit: src/sys/arch/i386/i386

2016-05-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 13 14:03:00 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Bring some amd64 swag. No functional changes.


To generate a diff of this commit:
cvs rdiff -u -r1.116 -r1.117 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.116 src/sys/arch/i386/i386/locore.S:1.117
--- src/sys/arch/i386/i386/locore.S:1.116	Thu May 12 06:45:16 2016
+++ src/sys/arch/i386/i386/locore.S	Fri May 13 14:03:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.116 2016/05/12 06:45:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.117 2016/05/13 14:03:00 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.116 2016/05/12 06:45:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.117 2016/05/13 14:03:00 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -340,10 +340,13 @@ _C_LABEL(Multiboot_Header):
 1:
 	/*
 	 * At this point, we know that a NetBSD-specific boot loader
-	 * booted this kernel.  The stack carries the following parameters:
-	 * (boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem),
-	 * 4 bytes each.
+	 * booted this kernel.
+	 *
+	 * Load parameters from the stack (32 bits):
+	 * boothowto, [bootdev], bootinfo, esym, biosextmem, biosbasemem
+	 * We are not interested in 'bootdev'.
 	 */
+
 	addl	$4,%esp		/* Discard return address to boot loader */
 	call	_C_LABEL(native_loader)
 	addl	$24,%esp
@@ -537,22 +540,43 @@ try586:	/* Use the `cpuid' instruction. 
 	movl	$_RELOC(tmpstk),%esp
 
 /*
- * Virtual address space of kernel, without PAE. The page dir is 1 page long.
+ * There are two different layouts possible, depending on whether PAE is
+ * enabled or not.
  *
- * text | data | bss | [syms] | [blobs] | page dir | proc0 kstack | L1 ptp
- *			0  1   2  3
+ * If PAE is not enabled, there are two levels of pages: PD -> PT. They will
+ * be referred to as: L2 -> L1. L2 is 1 page long. The BOOTSTRAP TABLES have
+ * the following layout:
+ * 	+-+++
+ * 	| L2 -> PROC0 STK -> L1 |
+ * 	+-+++
+ *
+ * If PAE is enabled, there are three levels of pages: PDP -> PD -> PT. They
+ * will be referred to as: L3 -> L2 -> L1. L3 is 1 page long, L2 is 4 page
+ * long. The BOOTSTRAP TABLES have the following layout:
+ * 	+-+-+++
+ * 	| L3 -> L2 -> PROC0 STK -> L1 |
+ * 	+-+-+++
+ *
+ * Virtual address space of the kernel in both cases:
+ * +--++--+-++-+---
+ * | TEXT | RODATA | DATA | BSS | [SYMS] | [PRELOADED MODULES] | BOOTSTRAP
+ * +--++--+-++-+---
+ * (1)  (2)   (3)
+ *
+ * ---++
+ * TABLES | ISA IO MEM |
+ * ---++
+ *   (4)
  *
- * Virtual address space of kernel, with PAE. We need 4 pages for the page dir
- * and 1 page for the L3.
- * text | data | bss | [syms] | [blobs] | L3 | page dir | proc0 kstack | L1 ptp
- * 	01  5   6  7
+ * PROC0 STK is obviously not linked as a page level. It just happens to be
+ * caught between L2 and L1.
  */
 
-	/* Find end of kernel image. */
+	/* Find end of kernel image; brings us on (1). */
 	movl	$RELOC(end),%edi
 
 #if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(makeoptions_COPY_SYMTAB)
-	/* Save the symbols (if loaded). */
+	/* Save the symbols (if loaded); brinds us on (2). */
 	movl	RELOC(esym),%eax
 	testl	%eax,%eax
 	jz	1f
@@ -561,14 +585,15 @@ try586:	/* Use the `cpuid' instruction. 
 1:
 #endif
 
-	/* Skip over any modules/blobs. */
+	/* Skip over any modules/blobs; brings us on (3). */
 	movl	RELOC(eblob),%eax
 	testl	%eax,%eax
 	jz	1f
 	subl	$KERNBASE,%eax
 	movl	%eax,%edi
 1:
-	/* Compute sizes */
+
+	/* We are on (3). Align up for BOOTSTRAP TABLES. */
 	movl	%edi,%esi
 	addl	$PGOFSET,%esi
 	andl	$~PGOFSET,%esi
@@ -577,20 +602,20 @@ try586:	/* Use the `cpuid' instruction. 
 	movl	%esi,%eax
 	addl	$~L2_FRAME,%eax
 	shrl	$L2_SHIFT,%eax
-	incl	%eax		/* one more ptp for VAs stolen by bootstrap */
+	incl	%eax		/* one more PTP for VAs stolen by bootstrap */
 1:	movl	%eax,RELOC(nkptp)+1*4
 
 	/* tablesize = (PDP_SIZE + UPAGES + nkptp) << PGSHIFT; */
 	addl	$(PDP_SIZE+UPAGES),%eax
 #ifdef PAE
-	incl	%eax 		/* one more page for the L3 PD */
+	incl	%eax 		/* one more page for L3 */
 	shll	$PGSHIFT+1,%eax	/* PTP tables are twice larger with PAE */
 #else
 	shll	$PGSHIFT,%eax
 #endif
 	movl	%eax,RELOC(tablesize)
 
-	/* ensure that nkptp covers bootstrap tables */
+	/* Ensure that nkptp covers BOOTSTRAP TABLES. */
 	addl	%esi,%eax
 	addl	$~L2_FRAME,%eax
 	

CVS commit: src/sys/arch/x86/x86

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 09:37:21 UTC 2016

Modified Files:
src/sys/arch/x86/x86: pmap.c

Log Message:
The NOX bit on large pages does not need to be amd64-specific anymore.
The i386 secondary CPUs can now properly handle it.


To generate a diff of this commit:
cvs rdiff -u -r1.193 -r1.194 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.193 src/sys/arch/x86/x86/pmap.c:1.194
--- src/sys/arch/x86/x86/pmap.c:1.193	Fri May 13 11:47:02 2016
+++ src/sys/arch/x86/x86/pmap.c	Sat May 14 09:37:21 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.193 2016/05/13 11:47:02 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.194 2016/05/14 09:37:21 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.193 2016/05/13 11:47:02 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.194 2016/05/14 09:37:21 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1343,12 +1343,7 @@ pmap_bootstrap(vaddr_t kva_start)
 		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
 		pa += NBPD_L2) {
 			pde = _BASE[pl2_i(kva)];
-#ifdef __x86_64__
 			*pde = pa | pmap_pg_g | PG_PS | pg_nx | PG_KR | PG_V;
-#else
-			*pde = pa | pmap_pg_g | PG_PS | PG_KR | PG_V;
-#endif
-
 			tlbflushg();
 		}
 
@@ -1359,11 +1354,7 @@ pmap_bootstrap(vaddr_t kva_start)
 		for (/* */; kva + NBPD_L2 <= kva_end; kva += NBPD_L2,
 		pa += NBPD_L2) {
 			pde = _BASE[pl2_i(kva)];
-#ifdef __x86_64__
 			*pde = pa | pmap_pg_g | PG_PS | pg_nx | PG_KW | PG_V;
-#else
-			*pde = pa | pmap_pg_g | PG_PS | PG_KW | PG_V;
-#endif
 			tlbflushg();
 		}
 	}



CVS commit: src/sys/arch/i386/i386

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 09:51:56 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Actually, put the NOX identification above. Old CPUs do not support the
cpuid instruction.


To generate a diff of this commit:
cvs rdiff -u -r1.121 -r1.122 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.121 src/sys/arch/i386/i386/locore.S:1.122
--- src/sys/arch/i386/i386/locore.S:1.121	Sat May 14 08:49:16 2016
+++ src/sys/arch/i386/i386/locore.S	Sat May 14 09:51:56 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.121 2016/05/14 08:49:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.122 2016/05/14 09:51:56 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.121 2016/05/14 08:49:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.122 2016/05/14 09:51:56 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -555,6 +555,16 @@ try586:	/* Use the `cpuid' instruction. 
 	cpuid
 	movl	%eax,RELOC(cpuid_level)
 
+	/*
+	 * Retrieve the NX/XD flag. We use the 32bit version of PG_NX.
+	 */
+	movl	$0x8001,%eax
+	cpuid
+	andl	$CPUID_NOX,%edx
+	jz	no_NOX
+	movl	$PG_NX32,RELOC(nox_flag)
+no_NOX:
+
 2:
 	/*
 	 * Finished with old stack; load new %esp now instead of later so we
@@ -571,16 +581,6 @@ try586:	/* Use the `cpuid' instruction. 
 	 */
 	movl	$_RELOC(tmpstk),%esp
 
-	/*
-	 * Retrieve the NX/XD flag. We use the 32bit version of PG_NX.
-	 */
-	movl	$0x8001,%eax
-	cpuid
-	andl	$CPUID_NOX,%edx
-	jz	no_NOX
-	movl	$PG_NX32,RELOC(nox_flag)
-no_NOX:
-
 /*
  * There are two different layouts possible, depending on whether PAE is
  * enabled or not.



CVS commit: src/sys/arch/i386/conf

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 08:19:42 UTC 2016

Modified Files:
src/sys/arch/i386/conf: kern.ldscript

Log Message:
Align the segments on i386. We're going to map them independently.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/i386/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/conf/kern.ldscript
diff -u src/sys/arch/i386/conf/kern.ldscript:1.18 src/sys/arch/i386/conf/kern.ldscript:1.19
--- src/sys/arch/i386/conf/kern.ldscript:1.18	Fri May 13 10:18:01 2016
+++ src/sys/arch/i386/conf/kern.ldscript	Sat May 14 08:19:42 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript,v 1.18 2016/05/13 10:18:01 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.19 2016/05/14 08:19:42 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -16,11 +16,7 @@ SECTIONS
 	_etext = . ;
 	PROVIDE (etext = .) ;
 
-	/*
-	 * Adjust the address for the rodata segment.  We want to adjust up to
-	 * the same address within the page on the next page up.
-	 */
-	. = ALIGN(0x1000) + (. & (0x1000 - 1));
+	. = ALIGN(__PAGE_SIZE);
 
 	__rodata_start = . ;
 	.rodata :
@@ -29,6 +25,8 @@ SECTIONS
 		*(.rodata.*)
 	}
 
+	. = ALIGN(__PAGE_SIZE);
+
 	__data_start = . ;
 	.data :
 	{
@@ -55,7 +53,7 @@ SECTIONS
 		*(.bss)
 		*(.bss.*)
 		*(COMMON)
-	. = ALIGN(32 / 8);
+		. = ALIGN(32 / 8);
 	}
 
 	. = ALIGN(__PAGE_SIZE);



CVS commit: src/sys/arch/i386/i386

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 08:34:01 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S mptramp.S

Log Message:
Fix the secondary CPUs bug in i386. Same as amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.118 -r1.119 src/sys/arch/i386/i386/locore.S
cvs rdiff -u -r1.28 -r1.29 src/sys/arch/i386/i386/mptramp.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.118 src/sys/arch/i386/i386/locore.S:1.119
--- src/sys/arch/i386/i386/locore.S:1.118	Sat May 14 06:49:34 2016
+++ src/sys/arch/i386/i386/locore.S	Sat May 14 08:34:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.118 2016/05/14 06:49:34 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.119 2016/05/14 08:34:00 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.118 2016/05/14 06:49:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.119 2016/05/14 08:34:00 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -168,6 +168,9 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 #endif /* XEN */
 #define	RELOC(x)	_RELOC(_C_LABEL(x))
 
+/* 32bit version of PG_NX */
+#define PG_NX32	0x8000
+
 #ifndef PAE
 #define	PROC0_PDIR_OFF	0
 #else
@@ -234,6 +237,7 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
  */
 	.data
 
+	.globl	_C_LABEL(nox_flag)
 	.globl	_C_LABEL(cputype)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
@@ -279,6 +283,10 @@ LABEL(lapic_tpr)
 	.long 0
 END(lapic_tpr)
 #endif
+
+	.type	_C_LABEL(nox_flag), @object
+LABEL(nox_flag)		.long	0	/* 32bit NOX flag, set if supported */
+END(nox_flag)
 	.type	_C_LABEL(cputype), @object
 LABEL(cputype)		.long	0	/* are we 80486, Pentium, or.. */
 END(cputype)
@@ -550,6 +558,16 @@ try586:	/* Use the `cpuid' instruction. 
 	 */
 	movl	$_RELOC(tmpstk),%esp
 
+	/*
+	 * Retrieve the NX/XD flag. We use the 32bit version of PG_NX.
+	 */
+	movl	$0x8001,%eax
+	cpuid
+	andl	$CPUID_NOX,%edx
+	jz	no_NOX
+	movl	$PG_NX32,RELOC(nox_flag)
+no_NOX:
+
 /*
  * There are two different layouts possible, depending on whether PAE is
  * enabled or not.
@@ -737,7 +755,20 @@ try586:	/* Use the `cpuid' instruction. 
 	movl	%eax,%cr3
 
 	/*
-	 * 2. Enable paging and the rest of it.
+	 * 2. Set NOX in EFER, if available.
+	 */
+	movl	RELOC(nox_flag),%ebx
+	cmpl	$0,%ebx
+	je 	skip_NOX
+	movl	$MSR_EFER,%ecx
+	rdmsr
+	xorl	%eax,%eax
+	orl	$(EFER_NXE),%eax
+	wrmsr
+skip_NOX:
+
+	/*
+	 * 3. Enable paging and the rest of it.
 	 */
 	movl	%cr0,%eax
 	orl	$(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax

Index: src/sys/arch/i386/i386/mptramp.S
diff -u src/sys/arch/i386/i386/mptramp.S:1.28 src/sys/arch/i386/i386/mptramp.S:1.29
--- src/sys/arch/i386/i386/mptramp.S:1.28	Thu May 12 09:40:23 2016
+++ src/sys/arch/i386/i386/mptramp.S	Sat May 14 08:34:00 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: mptramp.S,v 1.28 2016/05/12 09:40:23 maxv Exp $	*/
+/*	$NetBSD: mptramp.S,v 1.29 2016/05/14 08:34:00 maxv Exp $	*/
 
 /*
  * Copyright (c) 2000, 2016 The NetBSD Foundation, Inc.
@@ -75,7 +75,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: mptramp.S,v 1.28 2016/05/12 09:40:23 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: mptramp.S,v 1.29 2016/05/14 08:34:00 maxv Exp $");
 
 #include "opt_mpbios.h"		/* for MPDEBUG */
 
@@ -177,11 +177,25 @@ no_PSE:
 	movl	%eax,%cr4
 #endif
 
+	/*
+	 * Set NOX in EFER, if available.
+	 */
+	movl	RELOC(nox_flag),%ebx
+	cmpl	$0,%ebx
+	je 	no_NOX
+	movl	$MSR_EFER,%ecx
+	rdmsr
+	xorl	%eax,%eax
+	orl	$(EFER_NXE),%eax
+	wrmsr
+no_NOX:
+
+	/* Load %cr3. */
 	movl	RELOC(mp_pdirpa),%ecx
 	HALTT(0x5,%ecx)
-
-	/* Load base of page directory and enable mapping. */
 	movl	%ecx,%cr3		/* load PTD addr into MMU */
+
+	/* Enable paging and the rest of it. */
 	movl	%cr0,%eax
 	orl	$(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax
 	movl	%eax,%cr0
@@ -191,17 +205,6 @@ no_PSE:
 #endif
 	HALT(0x6)
 
-	/*
-	 * If EFER_NXE is not enabled, fetching a page with a NX bit set
-	 * will raise a #GP. Avoid that by setting the NXE feature now.
-	 */
-	movl	_C_LABEL(cpu_feature)+2*4,%eax	/* cpu_feature[2] */
-	andl	$CPUID_NOX,%eax
-	jz	1f
-	movl	$MSR_EFER,%ecx
-	rdmsr
-	orl	$EFER_NXE,%eax	/* enable No-Execute feature */
-	wrmsr
 
 /*
  * ok, we are now running with paging enabled and sharing page tables



CVS commit: src/sys/arch/i386/i386

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 08:39:42 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Define fillkpt_nox on i386, same as amd64.

But there is a difference in the way it is done here. If PAE is not enabled,
PDE_SIZE = 4, so there is no NOX bit set. If PAE is enabled, PDE_SIZE = 8,
so the NOX bit is set.

This works exactly as intended, since NOX does not exist in the non-PAE
case.


To generate a diff of this commit:
cvs rdiff -u -r1.119 -r1.120 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.119 src/sys/arch/i386/i386/locore.S:1.120
--- src/sys/arch/i386/i386/locore.S:1.119	Sat May 14 08:34:00 2016
+++ src/sys/arch/i386/i386/locore.S	Sat May 14 08:39:41 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.119 2016/05/14 08:34:00 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.120 2016/05/14 08:39:41 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.119 2016/05/14 08:34:00 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.120 2016/05/14 08:39:41 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -200,6 +200,19 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 	loop	1b			;
 
 /*
+ * fillkpt_nox - Same as fillkpt, but sets the NX/XD bit.
+ */
+#define fillkpt_nox \
+	pushl	%ebp			; \
+	movl	RELOC(nox_flag),%ebp	; \
+1:	movl	%ebp,(PDE_SIZE-4)(%ebx)	;	/* upper 32 bits: NX */ \
+	movl	%eax,(%ebx)		;	/* store phys addr */	\
+	addl	$PDE_SIZE,%ebx		;	/* next PTE/PDE */	\
+	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
+	loop	1b			; \
+	popl	%ebp			;
+
+/*
  * killkpt - Destroy a kernel page table
  *	ebx = page table address
  *	ecx = number of pages to destroy



CVS commit: src/sys/arch/i386/i386

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 08:49:16 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Map rodata and data+bss independently, and give them R and RW with
fillkpt_nox. The code is exactly the same as amd64's.


To generate a diff of this commit:
cvs rdiff -u -r1.120 -r1.121 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.120 src/sys/arch/i386/i386/locore.S:1.121
--- src/sys/arch/i386/i386/locore.S:1.120	Sat May 14 08:39:41 2016
+++ src/sys/arch/i386/i386/locore.S	Sat May 14 08:49:16 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.120 2016/05/14 08:39:41 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.121 2016/05/14 08:49:16 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.120 2016/05/14 08:39:41 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.121 2016/05/14 08:49:16 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -684,10 +684,6 @@ no_NOX:
 	 */
 	leal	(PROC0_PTP1_OFF)(%esi),%ebx
 
-	/* Compute &__rodata_start - KERNBASE. */
-	movl	$RELOC(__rodata_start),%edx
-	andl	$~PGOFSET,%edx
-
 	/* Skip the first MB. */
 	movl	$(KERNTEXTOFF - KERNBASE),%eax
 	movl	%eax,%ecx
@@ -697,19 +693,43 @@ no_NOX:
 #endif
 	addl	%ecx,%ebx
 
-	/* Map the kernel text read-only. */
-	movl	%edx,%ecx
+	/* Map the kernel text RX. */
+	movl	$RELOC(__rodata_start),%ecx
 	subl	%eax,%ecx
 	shrl	$PGSHIFT,%ecx
 	orl	$(PG_V|PG_KR),%eax
 	fillkpt
 
-	/* Map the data, BSS, and bootstrap tables read-write. */
-	leal	(PG_V|PG_KW)(%edx),%eax
-	movl	RELOC(tablesize),%ecx
-	addl	%esi,%ecx			/* end of tables */
-	subl	%edx,%ecx			/* subtract end of text */
+	/* Map the kernel rodata R. */
+	movl	$RELOC(__rodata_start),%eax
+	movl	$RELOC(__data_start),%ecx
+	subl	%eax,%ecx
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KR),%eax
+	fillkpt_nox
+
+	/* Map the kernel data+bss RW. */
+	movl	$RELOC(__data_start),%eax
+	movl	$RELOC(__kernel_end),%ecx
+	subl	%eax,%ecx
+	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
+	fillkpt_nox
+
+	/*
+	 * We actually have to be careful here. The memory layout is as
+	 * follows:
+	 *+--+-+--+
+	 *| DATA+BSS < [PRELOADED MODULES] | BOOTSTRAP TABLES >
+	 *+--+-+--+
+	 * We just map everything from < to > with RWX rights.
+	 */
+	movl	$RELOC(__kernel_end),%eax
+	movl	%esi,%ecx		/* start of BOOTSTRAP TABLES */
+	addl	RELOC(tablesize),%ecx	/* end of BOOTSTRAP TABLES */
+	subl	%eax,%ecx		/* subtract end of kernel image */
 	shrl	$PGSHIFT,%ecx
+	orl	$(PG_V|PG_KW),%eax
 	fillkpt
 
 	/* Map ISA I/O mem (later atdevbase) */



CVS commit: src/sys/arch

2016-05-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 12 09:40:23 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: mptramp.S
src/sys/arch/i386/i386: mptramp.S

Log Message:
KNF, and reduce the diff between amd64 and i386.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/amd64/amd64/mptramp.S
cvs rdiff -u -r1.27 -r1.28 src/sys/arch/i386/i386/mptramp.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/mptramp.S
diff -u src/sys/arch/amd64/amd64/mptramp.S:1.21 src/sys/arch/amd64/amd64/mptramp.S:1.22
--- src/sys/arch/amd64/amd64/mptramp.S:1.21	Wed May 11 19:35:08 2016
+++ src/sys/arch/amd64/amd64/mptramp.S	Thu May 12 09:40:23 2016
@@ -1,6 +1,6 @@
-/*	$NetBSD: mptramp.S,v 1.21 2016/05/11 19:35:08 maxv Exp $	*/
+/*	$NetBSD: mptramp.S,v 1.22 2016/05/12 09:40:23 maxv Exp $	*/
 
-/*-
+/*
  * Copyright (c) 2000, 2016 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
@@ -71,7 +71,7 @@
  * 1) Get the processors running kernel-code from a special
  *page-table and stack page, do chip identification.
  * 2) halt the processors waiting for them to be enabled
- *by a idle-thread 
+ *by a idle-thread
  */
 
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -84,10 +84,10 @@
 #include 
 #include 
 
-#define _RELOC(x)   ((x) - KERNBASE)
-#define RELOC(x)_RELOC(_C_LABEL(x))
+#define _RELOC(x)	((x) - KERNBASE)
+#define RELOC(x)	_RELOC(_C_LABEL(x))
 
-#define _TRMP_LABEL(a)  a = . - _C_LABEL(cpu_spinup_trampoline) + MP_TRAMPOLINE
+#define _TRMP_LABEL(a)	a = . - _C_LABEL(cpu_spinup_trampoline) + MP_TRAMPOLINE
 
 #ifdef MPDEBUG
 /*
@@ -131,10 +131,10 @@ LABEL(cpu_spinup_trampoline)
 #ifdef __clang__
 	lgdt (mptramp_gdt32_desc)	/* load flat descriptor table */
 #else
-	data32 addr32 lgdt (mptramp_gdt32_desc)   /* load flat descriptor table */
+	data32 addr32 lgdt (mptramp_gdt32_desc) /* load flat descriptor table */
 #endif
 	movl	%cr0, %eax	/* get cr0 */
-	orl	$0x1, %eax	/* enable protected mode */
+	orl	$CR0_PE, %eax	/* enable protected mode */
 	movl	%eax, %cr0	/* doit */
 	ljmpl	$0x8, $mp_startup
 
@@ -149,7 +149,7 @@ _TRMP_LABEL(mp_startup)
 	movw	%ax, %gs
 
 	/* bootstrap stack end, with scratch space.. */
-	movl	$(MP_TRAMPOLINE+NBPG-16),%esp
+	movl	$(MP_TRAMPOLINE+PAGE_SIZE-16),%esp
 
 #ifdef MPDEBUG
 	leal	RELOC(cpu_trace),%edi
@@ -208,8 +208,8 @@ mptramp_compat:
 	ljmp	*(%eax)
 
 _TRMP_LABEL(mptramp_jmp64)
-	.long   mptramp_longmode
-	.word   GSEL(GCODE_SEL, SEL_KPL)
+	.long	mptramp_longmode
+	.word	GSEL(GCODE_SEL, SEL_KPL)
 
 #define GDT_LIMIT	0x17	/* 23 = 3 * 8 - 1 */
 _TRMP_LABEL(mptramp_gdt32)
@@ -237,8 +237,8 @@ _TRMP_LABEL(mptramp_longmode)
 
 _C_LABEL(cpu_spinup_trampoline_end):	/* end of code copied to MP_TRAMPOLINE */
 
-1:
 	/* Don't touch lapic until BP has done init sequence. */
+1:
 	movq	_C_LABEL(cpu_starting),%rdi
 	pause
 	testq	%rdi, %rdi

Index: src/sys/arch/i386/i386/mptramp.S
diff -u src/sys/arch/i386/i386/mptramp.S:1.27 src/sys/arch/i386/i386/mptramp.S:1.28
--- src/sys/arch/i386/i386/mptramp.S:1.27	Sun Feb  2 22:41:20 2014
+++ src/sys/arch/i386/i386/mptramp.S	Thu May 12 09:40:23 2016
@@ -1,13 +1,11 @@
-/*	$NetBSD: mptramp.S,v 1.27 2014/02/02 22:41:20 dsl Exp $	*/
+/*	$NetBSD: mptramp.S,v 1.28 2016/05/12 09:40:23 maxv Exp $	*/
 
-/*-
- * Copyright (c) 2000 The NetBSD Foundation, Inc.
+/*
+ * Copyright (c) 2000, 2016 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by RedBack Networks Inc.
- *
- * Author: Bill Sommerfeld
+ * by RedBack Networks Inc. (Author: Bill Sommerfeld), and Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,7 +28,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-	
+
 /*
  * Copyright (c) 1999 Stefan Grefen
  *
@@ -62,24 +60,25 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 /*
  * MP startup ...
- * the stuff from cpu_spinup_trampoline to mp_startup 
- * is copied into the first 640 KB
+ * the stuff from cpu_spinup_trampoline to mp_startup is copied into the
+ * first 640 KB.
  *
  * We startup the processors now when the kthreads become ready.
  * The steps are:
- *1)   Get the processors running kernel-code from a special
- *  page-table and stack page, do chip identification.
- *2)   halt the processors waiting for them to be enabled
- *  by a idle-thread 
+ * 1) Get the processors running kernel-code from a special
+ *page-table and stack page, do chip identification.
+ * 2) halt the processors waiting for them to be enabled
+ *by a idle-thread
  */
 

CVS commit: src/sys/arch/amd64/amd64

2016-05-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed May 11 19:35:08 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S mptramp.S

Log Message:
There is a bug in the way the secondary CPUs are launched on amd64.

When CPU0 is launched, EFER_NXE is enabled in it, and it allows it to
handle pages that have the NOX bit. When the secondary CPUs are
launched, however, EFER_NXE is enabled only after paging is set in their
%cr0. And therefore, between the moment when paging is enabled and the
moment when EFER_NXE is enabled, the secondary CPUs cannot access pages
that have the NOX bit - they crash if they try to.

The funny thing is that in order to enable EFER_NXE, the secondary CPUs
give a look at cpu_feature[2], which is in the DATA segment, which in
turn could have the NOX bit. In other words, the secondary CPUs crash if
the DATA segment is mapped with the NOX bit.

Fix this by enabling EFER_NXE in the secondary CPUs before enabling
paging. CPU0 initializes nox_flag to the 32bit version of PG_NX if NOX
is supported; the secondary CPUs then use nox_flag to know whether NOX
is supported.

nox_flag will be used for other purposes soon.


To generate a diff of this commit:
cvs rdiff -u -r1.85 -r1.86 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/amd64/mptramp.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.85 src/sys/arch/amd64/amd64/locore.S:1.86
--- src/sys/arch/amd64/amd64/locore.S:1.85	Sun May  8 08:22:58 2016
+++ src/sys/arch/amd64/amd64/locore.S	Wed May 11 19:35:08 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.85 2016/05/08 08:22:58 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.86 2016/05/11 19:35:08 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -187,6 +187,9 @@
 #define	_RELOC(x)	((x) - KERNBASE)
 #define	RELOC(x)	_RELOC(_C_LABEL(x))
 
+/* 32bit version of PG_NX */
+#define PG_NX32	0x8000
+
 #if L2_SLOT_KERNBASE > 0
 #define TABLE_L2_ENTRIES (2 * (NKL2_KIMG_ENTRIES + 1))
 #else
@@ -286,6 +289,7 @@ _C_LABEL(lapic_isr):
 END(lapic_isr)
 #endif /* NLAPIC > 0 */
 
+	.globl	_C_LABEL(nox_flag)
 	.globl	_C_LABEL(cpuid_level)
 	.globl	_C_LABEL(esym)
 	.globl	_C_LABEL(eblob)
@@ -298,6 +302,9 @@ END(lapic_isr)
 	.globl	_C_LABEL(gdtstore)
 	.globl	_C_LABEL(cputype)
 
+	.type	_C_LABEL(nox_flag), @object
+LABEL(nox_flag)		.long	0	/* 32bit NOX flag, set if supported */
+END(nox_flag)
 	.type	_C_LABEL(cputype), @object
 LABEL(cputype)		.long	0	/* are we 80486, Pentium, or.. */
 END(cputype)
@@ -527,6 +534,16 @@ biosbasemem_finished:
 	 */
 	movl	$RELOC(tmpstk),%esp
 
+	/*
+	 * Retrieve the NX/XD flag. We use the 32bit version of PG_NX.
+	 */
+	movl	$0x8001,%eax
+	cpuid
+	andl	$CPUID_NOX,%edx
+	jz	no_NOX
+	movl	$PG_NX32,RELOC(nox_flag)
+no_NOX:
+
 /*
  * There are four levels of pages in amd64: PML4 -> PDP -> PD -> PT. They will
  * be referred to as: L4 -> L3 -> L2 -> L1.
@@ -707,13 +724,18 @@ biosbasemem_finished:
 	movl	%eax,%cr4
 
 	/*
-	 * 2. Set Long Mode Enable in EFER. Also enable the
-	 *syscall extensions.
+	 * 2. Set Long Mode Enable in EFER. Also enable the syscall extensions,
+	 *and NOX if available.
 	 */
 	movl	$MSR_EFER,%ecx
 	rdmsr
 	xorl	%eax,%eax	/* XXX */
 	orl	$(EFER_LME|EFER_SCE),%eax
+	movl	RELOC(nox_flag),%ebx
+	cmpl	$0,%ebx
+	je 	skip_NOX
+	orl	$(EFER_NXE),%eax
+skip_NOX:
 	wrmsr
 
 	/*

Index: src/sys/arch/amd64/amd64/mptramp.S
diff -u src/sys/arch/amd64/amd64/mptramp.S:1.20 src/sys/arch/amd64/amd64/mptramp.S:1.21
--- src/sys/arch/amd64/amd64/mptramp.S:1.20	Sat May  7 13:08:30 2016
+++ src/sys/arch/amd64/amd64/mptramp.S	Wed May 11 19:35:08 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: mptramp.S,v 1.20 2016/05/07 13:08:30 maxv Exp $	*/
+/*	$NetBSD: mptramp.S,v 1.21 2016/05/11 19:35:08 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2016 The NetBSD Foundation, Inc.
@@ -170,10 +170,19 @@ _TRMP_LABEL(mp_startup)
 no_PSE:
 	movl	%eax,%cr4
 
+	/*
+	 * Set Long Mode Enable in EFER. Also enable the syscall extensions,
+	 * and NOX if available.
+	 */
 	movl	$MSR_EFER,%ecx
 	rdmsr
 	xorl	%eax,%eax
 	orl	$(EFER_LME|EFER_SCE),%eax
+	movl	RELOC(nox_flag),%ebx
+	cmpl	$0,%ebx
+	je 	no_NOX
+	orl	$(EFER_NXE),%eax
+no_NOX:
 	wrmsr
 
 	/* Load %cr3. */
@@ -227,17 +236,6 @@ _TRMP_LABEL(mptramp_longmode)
 
 
 _C_LABEL(cpu_spinup_trampoline_end):	/* end of code copied to MP_TRAMPOLINE */
-	/*
-	 * If EFER_NXE is not enabled, fetching a page with a NX bit set
-	 * will raise a #GP. Avoid that by setting the NXE feature now.
-	 */
-	movl	_C_LABEL(cpu_feature)+2*4,%eax	/* cpu_feature[2] */
-	andl	$CPUID_NOX,%eax
-	jz	1f
-	movl	$MSR_EFER,%ecx
-	rdmsr
-	orl	$EFER_NXE,%eax	/* enable No-Execute feature */
-	wrmsr
 
 1:
 	/* Don't touch lapic until BP has done init sequence. */



CVS commit: src/sys/arch/i386/i386

2016-05-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 14 06:49:34 UTC 2016

Modified Files:
src/sys/arch/i386/i386: locore.S

Log Message:
Define killkpt, and don't use _RELOC. Same as amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.117 -r1.118 src/sys/arch/i386/i386/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.117 src/sys/arch/i386/i386/locore.S:1.118
--- src/sys/arch/i386/i386/locore.S:1.117	Fri May 13 14:03:00 2016
+++ src/sys/arch/i386/i386/locore.S	Sat May 14 06:49:34 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.117 2016/05/13 14:03:00 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.118 2016/05/14 06:49:34 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -128,7 +128,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.117 2016/05/13 14:03:00 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.118 2016/05/14 06:49:34 maxv Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_copy_symtab.h"
@@ -196,6 +196,17 @@ __KERNEL_RCSID(0, "$NetBSD: locore.S,v 1
 	addl	$PAGE_SIZE,%eax		;	/* next phys page */	\
 	loop	1b			;
 
+/*
+ * killkpt - Destroy a kernel page table
+ *	ebx = page table address
+ *	ecx = number of pages to destroy
+ */
+#define killkpt \
+1:	movl	$0,(PDE_SIZE-4)(%ebx)	;	/* upper bits (for PAE) */ \
+	movl	$0,(%ebx)		; \
+	addl	$PDE_SIZE,%ebx		; \
+	loop	1b			;
+
 
 #ifdef XEN
 /*
@@ -647,7 +658,7 @@ try586:	/* Use the `cpuid' instruction. 
 	andl	$~PGOFSET,%edx
 
 	/* Skip the first MB. */
-	movl	$_RELOC(KERNTEXTOFF),%eax
+	movl	$(KERNTEXTOFF - KERNBASE),%eax
 	movl	%eax,%ecx
 	shrl	$(PGSHIFT-2),%ecx	/* ((n >> PGSHIFT) << 2) for # PDEs */
 #ifdef PAE
@@ -743,10 +754,7 @@ begin:
 	movl	_C_LABEL(nkptp)+1*4,%ecx
 	leal	(PROC0_PDIR_OFF)(%esi),%ebx	/* old, phys address of PDIR */
 	addl	$(KERNBASE), %ebx		/* new, virt address of PDIR */
-1:	movl	$0,(PDE_SIZE-4)(%ebx)		/* upper bits (for PAE) */
-	movl	$0,(%ebx)
-	addl	$PDE_SIZE,%ebx
-	loop	1b
+	killkpt
 
 	/* Relocate atdevbase. */
 	movl	$KERNBASE,%edx



CVS commit: src/sys/arch/amd64/amd64

2016-05-08 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May  8 08:22:58 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
Use killkpt for the PML4 entries as well.


To generate a diff of this commit:
cvs rdiff -u -r1.84 -r1.85 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.84 src/sys/arch/amd64/amd64/locore.S:1.85
--- src/sys/arch/amd64/amd64/locore.S:1.84	Sat May  7 11:49:21 2016
+++ src/sys/arch/amd64/amd64/locore.S	Sun May  8 08:22:58 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.84 2016/05/07 11:49:21 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.85 2016/05/08 08:22:58 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -783,9 +783,7 @@ longmode_hi:
 	movq	$NKL4_KIMG_ENTRIES,%rcx
 	leaq	(PROC0_PML4_OFF)(%rsi),%rbx	/* old, phys address of PML4 */
 	addq	%r8,%rbx			/* new, virt address of PML4 */
-1:	movq	$0,(%rbx)
-	addq	$8,%rbx
-	loop	1b
+	killkpt
 
 	/* Relocate atdevbase. */
 	movq	$(TABLESIZE+KERNBASE),%rdx



CVS commit: src/sys/arch/amd64/conf

2016-05-08 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun May  8 08:30:41 UTC 2016

Modified Files:
src/sys/arch/amd64/conf: kern.ldscript

Log Message:
Define __rodata_start. Will be used soon.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/conf/kern.ldscript

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/conf/kern.ldscript
diff -u src/sys/arch/amd64/conf/kern.ldscript:1.17 src/sys/arch/amd64/conf/kern.ldscript:1.18
--- src/sys/arch/amd64/conf/kern.ldscript:1.17	Sat May  7 12:45:55 2016
+++ src/sys/arch/amd64/conf/kern.ldscript	Sun May  8 08:30:41 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript,v 1.17 2016/05/07 12:45:55 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.18 2016/05/08 08:30:41 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -23,6 +23,7 @@ SECTIONS
 	_etext = . ;
 	PROVIDE (etext = .) ;
 
+	__rodata_start = . ;
 	.rodata :
 	{
 		*(.rodata)



CVS commit: src/sys/arch/amd64/amd64

2016-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May  7 11:49:21 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: locore.S

Log Message:
clarify


To generate a diff of this commit:
cvs rdiff -u -r1.83 -r1.84 src/sys/arch/amd64/amd64/locore.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.83 src/sys/arch/amd64/amd64/locore.S:1.84
--- src/sys/arch/amd64/amd64/locore.S:1.83	Wed Nov 25 16:00:09 2015
+++ src/sys/arch/amd64/amd64/locore.S	Sat May  7 11:49:21 2016
@@ -1,10 +1,39 @@
-/*	$NetBSD: locore.S,v 1.83 2015/11/25 16:00:09 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.84 2016/05/07 11:49:21 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
  */
 
 /*
+ * Copyright (c) 1998, 2000, 2007, 2008, 2016 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Charles M. Hannum and Maxime Villard.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *notice, this list of conditions and the following disclaimer in the
+ *documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+/*
  * Copyright (c) 2007 Manuel Bouyer.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -80,36 +109,6 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-
-/*-
- * Copyright (c) 1998, 2000, 2007, 2008 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * This code is derived from software contributed to The NetBSD Foundation
- * by Charles M. Hannum.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *notice, this list of conditions and the following disclaimer in the
- *documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
@@ -144,9 +143,7 @@
  *	@(#)locore.s	7.3 (Berkeley) 5/13/91
  */
 
-/*
- * override user-land alignment before including asm.h
- */
+/* Override user-land alignment before including asm.h */
 #define	ALIGN_DATA	.align	8
 #define ALIGN_TEXT	.align 16,0x90
 #define _ALIGN_TEXT	ALIGN_TEXT
@@ -184,7 +181,6 @@
 #include 
 #endif
 
-/* XXX temporary kluge; these should not be here */
 /* Get definitions for IOM_BEGIN, IOM_END, and IOM_SIZE */
 #include 
 
@@ -204,7 +200,7 @@
 #endif
 
 #define PROC0_PML4_OFF	0
-#define PROC0_STK_OFF	(PROC0_PML4_OFF + PAGE_SIZE)
+#define PROC0_STK_OFF	(PROC0_PML4_OFF + 1 * PAGE_SIZE)
 #define PROC0_PTP3_OFF	(PROC0_STK_OFF + UPAGES * PAGE_SIZE)
 #define PROC0_PTP2_OFF	(PROC0_PTP3_OFF + NKL4_KIMG_ENTRIES * PAGE_SIZE)
 #define PROC0_PTP1_OFF

CVS commit: src/sys/arch/amd64/amd64

2016-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May  7 13:08:30 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: mptramp.S

Log Message:
clarify


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/amd64/amd64/mptramp.S

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/mptramp.S
diff -u src/sys/arch/amd64/amd64/mptramp.S:1.19 src/sys/arch/amd64/amd64/mptramp.S:1.20
--- src/sys/arch/amd64/amd64/mptramp.S:1.19	Wed Dec  9 16:55:18 2015
+++ src/sys/arch/amd64/amd64/mptramp.S	Sat May  7 13:08:30 2016
@@ -1,13 +1,11 @@
-/*	$NetBSD: mptramp.S,v 1.19 2015/12/09 16:55:18 maxv Exp $	*/
+/*	$NetBSD: mptramp.S,v 1.20 2016/05/07 13:08:30 maxv Exp $	*/
 
 /*-
- * Copyright (c) 2000 The NetBSD Foundation, Inc.
+ * Copyright (c) 2000, 2016 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by RedBack Networks Inc.
- *
- * Author: Bill Sommerfeld
+ * by RedBack Networks Inc. (Author: Bill Sommerfeld), and Maxime Villard.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,7 +28,7 @@
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  * POSSIBILITY OF SUCH DAMAGE.
  */
-	
+
 /*
  * Copyright (c) 1999 Stefan Grefen
  *
@@ -62,10 +60,11 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
+
 /*
  * MP startup ...
- * the stuff from cpu_spinup_trampoline to mp_startup 
- * is copied into the first 640 KB
+ * the stuff from cpu_spinup_trampoline to mp_startup is copied into the
+ * first 640 KB.
  *
  * We startup the processors now when the kthreads become ready.
  * The steps are:
@@ -74,9 +73,9 @@
  * 2) halt the processors waiting for them to be enabled
  *by a idle-thread 
  */
-	
+
 #include "opt_mpbios.h"		/* for MPDEBUG */
-		
+
 #include "assym.h"
 #include 
 #include 
@@ -91,22 +90,27 @@
 #define _TRMP_LABEL(a)  a = . - _C_LABEL(cpu_spinup_trampoline) + MP_TRAMPOLINE
 
 #ifdef MPDEBUG
-
 /*
  * Debug code to stop aux. processors in various stages based on the
  * value in cpu_trace.
  *
- * %edi points at cpu_trace;  cpu_trace[0] is the "hold point";
- * cpu_trace[1] is the point which the CPU has reached.
+ * %edi points at cpu_trace;
+ * cpu_trace[0] is the "hold point";
+ * cpu_trace[1] is the point which the CPU has reached;
  * cpu_trace[2] is the last value stored by HALTT.
  */
-
-#define HALT(x)	1: movl (%edi),%ebx;cmpl $ x,%ebx ; jle 1b ; movl $x,4(%edi)
-#define HALTT(x,y)	movl y,8(%edi); HALT(x)
+#define HALT(x) \
+1:	movl (%edi),%ebx	;	\
+	cmpl $x,%ebx		;	\
+	jle 1b			;	\
+	movl $x,4(%edi)		;
+#define HALTT(x,y) \
+	movl y,8(%edi)		;	\
+	HALT(x)			;
 #else
-#define HALT(x)	/**/
-#define HALTT(x,y) /**/
-#endif
+#define HALT(x)
+#define HALTT(x,y)
+#endif /* MPDEBUG */
 
 	.global _C_LABEL(cpu_spinup_trampoline)
 	.global _C_LABEL(cpu_spinup_trampoline_end)
@@ -156,13 +160,14 @@ _TRMP_LABEL(mp_startup)
 	pushl	$PSL_MBO
 	popfl
 
+	/* Enable PAE, SSE, and PSE if available */
 	movl	%cr4,%eax
 	orl	$(CR4_PAE|CR4_OSFXSR|CR4_OSXMMEXCPT),%eax
 	movl	RELOC(pmap_largepages),%ecx
 	orl	%ecx,%ecx
-	jz	1f
+	jz	no_PSE
 	orl	$CR4_PSE,%eax
-1:
+no_PSE:
 	movl	%eax,%cr4
 
 	movl	$MSR_EFER,%ecx
@@ -171,12 +176,15 @@ _TRMP_LABEL(mp_startup)
 	orl	$(EFER_LME|EFER_SCE),%eax
 	wrmsr
 
+	/* Load %cr3. */
 	movl	RELOC(mp_pdirpa),%ecx	/* guaranteed < 4G */
-	movl	%ecx,%cr3		/* load ptd addr into mmu */
+	movl	%ecx,%cr3		/* load PTD addr into MMU */
 
-	movl	%cr0,%eax		/* get control word */
+	/* Enable paging and the rest of it. */
+	movl	%cr0,%eax
 	orl	$(CR0_PE|CR0_PG|CR0_NE|CR0_TS|CR0_MP|CR0_WP|CR0_AM),%eax
 	movl	%eax,%cr0
+
 	jmp	mptramp_compat
 mptramp_compat:
 
@@ -194,22 +202,23 @@ _TRMP_LABEL(mptramp_jmp64)
 	.long   mptramp_longmode
 	.word   GSEL(GCODE_SEL, SEL_KPL)
 
+#define GDT_LIMIT	0x17	/* 23 = 3 * 8 - 1 */
 _TRMP_LABEL(mptramp_gdt32)
 	.quad 0x
-	.quad 0x00cf9f00
-	.quad 0x00cf9300
+	.quad 0x00cf9f00	/* CS */
+	.quad 0x00cf9300	/* DS */
 _TRMP_LABEL(mptramp_gdt32_desc)
-	.word 0x17
+	.word GDT_LIMIT
 	.long mptramp_gdt32
 
 _TRMP_LABEL(mptramp_gdt64)
 	.quad 0x
-	.quad 0x00af9a00
-	.quad 0x00cf9200
+	.quad 0x00af9a00	/* CS */
+	.quad 0x00cf9200	/* DS */
 _TRMP_LABEL(mptramp_gdt64_desc)
-	.word 0x17
+	.word GDT_LIMIT
 	.long mptramp_gdt64
-
+#undef	GDT_LIMIT
 
 _TRMP_LABEL(mptramp_longmode)
 	.code64



CVS commit: src/sys/ufs/ffs

2016-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May  7 11:59:09 UTC 2016

Modified Files:
src/sys/ufs/ffs: ffs_subr.c

Log Message:
uaf


To generate a diff of this commit:
cvs rdiff -u -r1.48 -r1.49 src/sys/ufs/ffs/ffs_subr.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/ufs/ffs/ffs_subr.c
diff -u src/sys/ufs/ffs/ffs_subr.c:1.48 src/sys/ufs/ffs/ffs_subr.c:1.49
--- src/sys/ufs/ffs/ffs_subr.c:1.48	Sun Oct 20 00:29:10 2013
+++ src/sys/ufs/ffs/ffs_subr.c	Sat May  7 11:59:08 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: ffs_subr.c,v 1.48 2013/10/20 00:29:10 htodd Exp $	*/
+/*	$NetBSD: ffs_subr.c,v 1.49 2016/05/07 11:59:08 maxv Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -36,7 +36,7 @@
 #endif
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.48 2013/10/20 00:29:10 htodd Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ffs_subr.c,v 1.49 2016/05/07 11:59:08 maxv Exp $");
 
 #include 
 
@@ -127,8 +127,10 @@ ffs_getblk(struct vnode *vp, daddr_t lbl
 		(*bpp)->b_blkno = blkno;
 	if (clearbuf)
 		clrbuf(*bpp);
-	if ((*bpp)->b_blkno >= 0 && (error = fscow_run(*bpp, false)) != 0)
+	if ((*bpp)->b_blkno >= 0 && (error = fscow_run(*bpp, false)) != 0) {
 		brelse(*bpp, BC_INVAL);
+		*bpp = NULL;
+	}
 	return error;
 }
 



CVS commit: src/sys/arch/amd64/conf

2016-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May  7 12:45:56 UTC 2016

Modified Files:
src/sys/arch/amd64/conf: kern.ldscript
Removed Files:
src/sys/arch/amd64/conf: kern.ldscript.2MB largepages.inc

Log Message:
Large pages are supported by default for the text+rodata segments. Apply
the proper alignment for the data segment, so that more pages can benefit
from it. Reduces TLB contention.

kern.ldscript.2MB and largepages.inc are useless.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/conf/kern.ldscript
cvs rdiff -u -r1.11 -r0 src/sys/arch/amd64/conf/kern.ldscript.2MB
cvs rdiff -u -r1.4 -r0 src/sys/arch/amd64/conf/largepages.inc

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/conf/kern.ldscript
diff -u src/sys/arch/amd64/conf/kern.ldscript:1.16 src/sys/arch/amd64/conf/kern.ldscript:1.17
--- src/sys/arch/amd64/conf/kern.ldscript:1.16	Sat Nov 14 14:01:23 2015
+++ src/sys/arch/amd64/conf/kern.ldscript	Sat May  7 12:45:55 2016
@@ -1,7 +1,15 @@
-/*	$NetBSD: kern.ldscript,v 1.16 2015/11/14 14:01:23 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript,v 1.17 2016/05/07 12:45:55 maxv Exp $	*/
 
 #include "assym.h"
 
+/*
+ * PAE is enabled by default on amd64, the large page size is therefore
+ * 2MB (and not 4MB!).
+ */
+
+__PAGE_SIZE = 0x1000 ;
+__LARGE_PAGE_SIZE = 0x20 ;
+
 ENTRY(_start)
 SECTIONS
 {
@@ -22,10 +30,11 @@ SECTIONS
 	}
 
 	/*
-	 * Adjust the address for the data segment.  We want to adjust up to
-	 * the same address within the page on the next page up.
+	 * Push the data segment up to the next large page boundary so that we
+	 * can map the text+rodata segments with large pages.
 	 */
-	. = ALIGN(0x10) + (. & (0x10 - 1));
+	. = ALIGN(__LARGE_PAGE_SIZE);
+
 	__data_start = . ;
 	.data :
 	{



CVS commit: src/sys/arch

2016-07-22 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jul 22 14:08:33 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: machdep.c
src/sys/arch/amd64/include: pmap.h
src/sys/arch/x86/x86: pmap.c

Log Message:
Remove pmap_prealloc_lowmem_ptps on amd64. This function creates levels in
the page tree so that the first 2MB of virtual memory can be kentered in
L1.

Strictly speaking, the kernel should never kenter a virtual page below
VM_MIN_KERNEL_ADDRESS, because then it wouldn't be available in userland.
It used to need the first 2MB in order to map the CPU trampoline and the
initial VAs used by the bootstrap code. Now, the CPU trampoline VA is
allocated with uvm_km_alloc and the VAs used by the bootstrap code are
allocated with pmap_bootstrap_valloc, and in either case the resulting VA
is above VM_MIN_KERNEL_ADDRESS.

The low levels in the page tree are therefore unused. By removing this
function, we are making sure no one will be tempted to map an area below
VM_MIN_KERNEL_ADDRESS in kernel mode, and particularly, we are making sure
NULL cannot be kentered.

In short, there is no way to map NULL in kernel mode anymore.


To generate a diff of this commit:
cvs rdiff -u -r1.225 -r1.226 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.37 -r1.38 src/sys/arch/amd64/include/pmap.h
cvs rdiff -u -r1.215 -r1.216 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.225 src/sys/arch/amd64/amd64/machdep.c:1.226
--- src/sys/arch/amd64/amd64/machdep.c:1.225	Wed Jul 20 13:49:17 2016
+++ src/sys/arch/amd64/amd64/machdep.c	Fri Jul 22 14:08:33 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.225 2016/07/20 13:49:17 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.226 2016/07/22 14:08:33 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.225 2016/07/20 13:49:17 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.226 2016/07/22 14:08:33 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1605,9 +1605,6 @@ init_x86_64(paddr_t first_avail)
 	 */
 	pmap_bootstrap(VM_MIN_KERNEL_ADDRESS);
 
-	if (avail_start != PAGE_SIZE)
-		pmap_prealloc_lowmem_ptps();
-
 #ifndef XEN
 	/* Internalize the physical pages into the VM system. */
 	init_x86_vm(first_avail);

Index: src/sys/arch/amd64/include/pmap.h
diff -u src/sys/arch/amd64/include/pmap.h:1.37 src/sys/arch/amd64/include/pmap.h:1.38
--- src/sys/arch/amd64/include/pmap.h:1.37	Sat May 21 07:00:18 2016
+++ src/sys/arch/amd64/include/pmap.h	Fri Jul 22 14:08:33 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.h,v 1.37 2016/05/21 07:00:18 maxv Exp $	*/
+/*	$NetBSD: pmap.h,v 1.38 2016/07/22 14:08:33 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -323,7 +323,6 @@ pmap_pte_flush(void)
 }
 #endif
 
-void pmap_prealloc_lowmem_ptps(void);
 void pmap_changeprot_local(vaddr_t, vm_prot_t);
 
 #include 

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.215 src/sys/arch/x86/x86/pmap.c:1.216
--- src/sys/arch/x86/x86/pmap.c:1.215	Fri Jul 22 13:01:43 2016
+++ src/sys/arch/x86/x86/pmap.c	Fri Jul 22 14:08:33 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.215 2016/07/22 13:01:43 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.216 2016/07/22 14:08:33 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010, 2016 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.215 2016/07/22 13:01:43 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.216 2016/07/22 14:08:33 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1616,72 +1616,6 @@ pmap_remap_largepages(void)
 }
 #endif /* !XEN */
 
-#if defined(__x86_64__)
-/*
- * Pre-allocate PTPs for low memory, so that 1:1 mappings for various
- * trampoline code can be entered.
- */
-void
-pmap_prealloc_lowmem_ptps(void)
-{
-	int level;
-	paddr_t newp;
-	pd_entry_t *pdes;
-
-	const pd_entry_t pteflags = PG_k | PG_V | PG_RW;
-
-	pdes = pmap_kernel()->pm_pdir;
-	level = PTP_LEVELS;
-	for (;;) {
-		newp = pmap_bootstrap_palloc(1);
-
-#ifdef __HAVE_DIRECT_MAP
-		memset((void *)PMAP_DIRECT_MAP(newp), 0, PAGE_SIZE);
-#else
-		pmap_pte_set(early_zero_pte, pmap_pa2pte(newp) | pteflags |
-		pmap_pg_nx);
-		pmap_pte_flush();
-		pmap_update_pg((vaddr_t)early_zerop);
-		memset(early_zerop, 0, PAGE_SIZE);
-#endif
-
-#ifdef XEN
-		/* Mark R/O before installing */
-		HYPERVISOR_update_va_mapping ((vaddr_t)early_zerop,
-		xpmap_ptom_masked(newp) | PG_u | PG_V, UVMF_INVLPG);
-		if (newp < (NKL2_KIMG_ENTRIES * NBPD_L2))
-			HYPERVISOR_update_va_mapping (newp + KERNBASE,
-			xpmap_ptom_masked(newp) | PG_u | PG_V, UVMF_INVLPG);
-
-		if (level == PTP_LEVELS) { /* Top level pde is per-cpu */
-			pd_entry_t *kpm_pdir;
-			/* Reach it 

CVS commit: src/sys/compat/linux/arch/i386

2016-07-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jul 13 15:59:54 UTC 2016

Modified Files:
src/sys/compat/linux/arch/i386: linux_machdep.c

Log Message:
x86_alldisks can be NULL, so don't dereference it. Not tested, but obvious
enough.


To generate a diff of this commit:
cvs rdiff -u -r1.161 -r1.162 src/sys/compat/linux/arch/i386/linux_machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/compat/linux/arch/i386/linux_machdep.c
diff -u src/sys/compat/linux/arch/i386/linux_machdep.c:1.161 src/sys/compat/linux/arch/i386/linux_machdep.c:1.162
--- src/sys/compat/linux/arch/i386/linux_machdep.c:1.161	Tue Dec  8 21:27:50 2015
+++ src/sys/compat/linux/arch/i386/linux_machdep.c	Wed Jul 13 15:59:54 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux_machdep.c,v 1.161 2015/12/08 21:27:50 christos Exp $	*/
+/*	$NetBSD: linux_machdep.c,v 1.162 2016/07/13 15:59:54 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1995, 2000, 2008, 2009 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.161 2015/12/08 21:27:50 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: linux_machdep.c,v 1.162 2016/07/13 15:59:54 maxv Exp $");
 
 #if defined(_KERNEL_OPT)
 #include "opt_vm86.h"
@@ -107,8 +107,9 @@ __KERNEL_RCSID(0, "$NetBSD: linux_machde
 #define DPRINTF(a)
 #endif
 
-static struct biosdisk_info *fd2biosinfo(struct proc *, struct file *);
 extern struct disklist *x86_alldisks;
+
+static struct biosdisk_info *fd2biosinfo(struct proc *, struct file *);
 static void linux_save_ucontext(struct lwp *, struct trapframe *,
 const sigset_t *, struct sigaltstack *, struct linux_ucontext *);
 static void linux_save_sigcontext(struct lwp *, struct trapframe *,
@@ -761,6 +762,8 @@ fd2biosinfo(struct proc *p, struct file 
 	struct nativedisk_info *nip;
 	struct disklist *dl = x86_alldisks;
 
+	if (dl == NULL)
+		return NULL;
 	if (fp->f_type != DTYPE_VNODE)
 		return NULL;
 	vp = (struct vnode *)fp->f_data;



CVS commit: src/sys/arch

2016-07-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jul 13 15:53:27 UTC 2016

Modified Files:
src/sys/arch/amd64/amd64: machdep.c
src/sys/arch/i386/i386: machdep.c

Log Message:
Reorder some instructions, reduces the diff between amd64 and i386.


To generate a diff of this commit:
cvs rdiff -u -r1.222 -r1.223 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.757 -r1.758 src/sys/arch/i386/i386/machdep.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.222 src/sys/arch/amd64/amd64/machdep.c:1.223
--- src/sys/arch/amd64/amd64/machdep.c:1.222	Wed Jul 13 15:39:33 2016
+++ src/sys/arch/amd64/amd64/machdep.c	Wed Jul 13 15:53:26 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.222 2016/07/13 15:39:33 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.223 2016/07/13 15:53:26 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.222 2016/07/13 15:39:33 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.223 2016/07/13 15:53:26 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1543,8 +1543,6 @@ init_x86_64(paddr_t first_avail)
 #endif
 #endif /* !XEN */
 
-	cpu_probe(_info_primary);
-
 #ifdef XEN
 	KASSERT(HYPERVISOR_shared_info != NULL);
 	cpu_info_primary.ci_vcpu = _shared_info->vcpu_info[0];
@@ -1552,6 +1550,7 @@ init_x86_64(paddr_t first_avail)
 	__PRINTK(("init_x86_64(0x%lx)\n", first_avail));
 #endif /* XEN */
 
+	cpu_probe(_info_primary);
 	cpu_init_msrs(_info_primary, true);
 
 	use_pae = 1; /* PAE always enabled in long mode */

Index: src/sys/arch/i386/i386/machdep.c
diff -u src/sys/arch/i386/i386/machdep.c:1.757 src/sys/arch/i386/i386/machdep.c:1.758
--- src/sys/arch/i386/i386/machdep.c:1.757	Wed Jul 13 15:39:33 2016
+++ src/sys/arch/i386/i386/machdep.c	Wed Jul 13 15:53:27 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.757 2016/07/13 15:39:33 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.758 2016/07/13 15:53:27 maxv Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.757 2016/07/13 15:39:33 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.758 2016/07/13 15:53:27 maxv Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -1145,10 +1145,9 @@ init386(paddr_t first_avail)
 	cpu_info_primary.ci_vcpu = _shared_info->vcpu_info[0];
 #endif
 
-	cpu_probe(_info_primary);
-
 	uvm_lwp_setuarea(, lwp0uarea);
 
+	cpu_probe(_info_primary);
 	cpu_init_msrs(_info_primary, true);
 
 #ifdef PAE
@@ -1177,10 +1176,6 @@ init386(paddr_t first_avail)
 	cpu_info_primary.ci_pae_l3_pdir = (pd_entry_t *)(rcr3() + KERNBASE);
 #endif /* PAE && !XEN */
 
-#ifdef XEN
-	xen_parse_cmdline(XEN_PARSE_BOOTFLAGS, NULL);
-#endif
-
 	/*
 	 * Initialize PAGE_SIZE-dependent variables.
 	 */
@@ -1205,10 +1200,14 @@ init386(paddr_t first_avail)
 	 */
 	avail_start = 6 * PAGE_SIZE;
 #else /* !XEN */
-	/* steal one page for gdt */
+	/* Parse Xen command line (replace bootinfo) */
+	xen_parse_cmdline(XEN_PARSE_BOOTFLAGS, NULL);
+
+	/* Steal one page for gdt */
 	gdt = (void *)((u_long)first_avail + KERNBASE);
 	first_avail += PAGE_SIZE;
-	/* Make sure the end of the space used by the kernel is rounded. */
+
+	/* Determine physical address space */
 	first_avail = round_page(first_avail);
 	avail_start = first_avail;
 	avail_end = ctob((paddr_t)xen_start_info.nr_pages);
@@ -1219,12 +1218,12 @@ init386(paddr_t first_avail)
 	mem_cluster_cnt++;
 	physmem += xen_start_info.nr_pages;
 	uvmexp.wired += atop(avail_start);
+
 	/*
 	 * initgdt() has to be done before consinit(), so that %fs is properly
 	 * initialised. initgdt() uses pmap_kenter_pa so it can't be called
 	 * before the above variables are set.
 	 */
-
 	initgdt(gdt);
 
 	mutex_init(_lock, MUTEX_DEFAULT, IPL_VM);



CVS commit: src/sys/arch/x86

2016-07-25 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Jul 25 12:11:40 UTC 2016

Modified Files:
src/sys/arch/x86/include: pmap.h
src/sys/arch/x86/x86: lapic.c pmap.c

Log Message:
The L1 entry of the first page of the data segment is overwritten for the
LAPIC page, and set as RWX+PG_N. The LAPIC pa is fixed, and its va resides
in the data segment. Because of this error-prone design, the kernel image
map is not linear, and I first thought it was a bug (as I vaguely said in
PR/51148). Using large pages for the data segment is therefore wrong, since
the first page does not actually belong to the data segment (even if its va
is in the range). This bug is not triggered currently, since local_apic is
not large-page-aligned.

We will certainly have to allocate a va dynamically instead of using the
first page of data; but for now, disable large pages on the data segment,
and map the LAPIC as RW.

This is the last x86-specific RWX page.


To generate a diff of this commit:
cvs rdiff -u -r1.58 -r1.59 src/sys/arch/x86/include/pmap.h
cvs rdiff -u -r1.51 -r1.52 src/sys/arch/x86/x86/lapic.c
cvs rdiff -u -r1.216 -r1.217 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/x86/include/pmap.h
diff -u src/sys/arch/x86/include/pmap.h:1.58 src/sys/arch/x86/include/pmap.h:1.59
--- src/sys/arch/x86/include/pmap.h:1.58	Fri Jul  1 11:10:48 2016
+++ src/sys/arch/x86/include/pmap.h	Mon Jul 25 12:11:40 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.h,v 1.58 2016/07/01 11:10:48 maxv Exp $	*/
+/*	$NetBSD: pmap.h,v 1.59 2016/07/25 12:11:40 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -209,6 +209,7 @@ struct pmap {
 extern u_long PDPpaddr;
 
 extern pd_entry_t pmap_pg_g;			/* do we support PG_G? */
+extern pd_entry_t pmap_pg_nx;			/* do we support PG_NX? */
 extern long nkptp[PTP_LEVELS];
 
 /*

Index: src/sys/arch/x86/x86/lapic.c
diff -u src/sys/arch/x86/x86/lapic.c:1.51 src/sys/arch/x86/x86/lapic.c:1.52
--- src/sys/arch/x86/x86/lapic.c:1.51	Mon Jul 27 15:45:20 2015
+++ src/sys/arch/x86/x86/lapic.c	Mon Jul 25 12:11:40 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: lapic.c,v 1.51 2015/07/27 15:45:20 msaitoh Exp $	*/
+/*	$NetBSD: lapic.c,v 1.52 2016/07/25 12:11:40 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2000, 2008 The NetBSD Foundation, Inc.
@@ -32,7 +32,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.51 2015/07/27 15:45:20 msaitoh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: lapic.c,v 1.52 2016/07/25 12:11:40 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_mpbios.h"		/* for MPDEBUG */
@@ -125,7 +125,7 @@ lapic_map(paddr_t lapic_base)
 	 */
 
 	pte = kvtopte(va);
-	*pte = lapic_base | PG_RW | PG_V | PG_N | pmap_pg_g;
+	*pte = lapic_base | PG_RW | PG_V | PG_N | pmap_pg_g | pmap_pg_nx;
 	invlpg(va);
 
 #ifdef MULTIPROCESSOR

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.216 src/sys/arch/x86/x86/pmap.c:1.217
--- src/sys/arch/x86/x86/pmap.c:1.216	Fri Jul 22 14:08:33 2016
+++ src/sys/arch/x86/x86/pmap.c	Mon Jul 25 12:11:40 2016
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.216 2016/07/22 14:08:33 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.217 2016/07/25 12:11:40 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2010, 2016 The NetBSD Foundation, Inc.
@@ -171,7 +171,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.216 2016/07/22 14:08:33 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.217 2016/07/25 12:11:40 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1573,7 +1573,9 @@ pmap_remap_largepages(void)
 {
 	extern char __rodata_start;
 	extern char __data_start;
+#if 0
 	extern char __kernel_end;
+#endif
 	pd_entry_t *pde;
 	vaddr_t kva, kva_end;
 	paddr_t pa;
@@ -1604,6 +1606,7 @@ pmap_remap_largepages(void)
 		tlbflushg();
 	}
 
+#if 0
 	/* Remap the kernel data+bss using large pages. */
 	kva = roundup((vaddr_t)&__data_start, NBPD_L2);
 	kva_end = rounddown((vaddr_t)&__kernel_end, NBPD_L1);
@@ -1613,6 +1616,7 @@ pmap_remap_largepages(void)
 		*pde = pa | pmap_pg_g | PG_PS | pmap_pg_nx | PG_KW | PG_V;
 		tlbflushg();
 	}
+#endif
 }
 #endif /* !XEN */
 



<    1   2   3   4   5   6   7   8   9   10   >