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

2018-01-20 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jan 20 08:30:53 UTC 2018

Modified Files:
src/sys/arch/amd64/amd64: amd64_trap.S trap.c

Log Message:
Fix the double-fault handler. We're executing on ist1 and must not jump
out of it, so don't enable interrupts. And use the SVS_*_ALTSTACK macros.

While here, fix the NMI handler too: it should use SVS_LEAVE_ALTSTACK.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/amd64/amd64_trap.S
cvs rdiff -u -r1.110 -r1.111 src/sys/arch/amd64/amd64/trap.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/amd64_trap.S
diff -u src/sys/arch/amd64/amd64/amd64_trap.S:1.18 src/sys/arch/amd64/amd64/amd64_trap.S:1.19
--- src/sys/arch/amd64/amd64/amd64_trap.S:1.18	Thu Jan 18 07:25:34 2018
+++ src/sys/arch/amd64/amd64/amd64_trap.S	Sat Jan 20 08:30:53 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: amd64_trap.S,v 1.18 2018/01/18 07:25:34 maxv Exp $	*/
+/*	$NetBSD: amd64_trap.S,v 1.19 2018/01/20 08:30:53 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2008, 2017 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
 
 #if 0
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.18 2018/01/18 07:25:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.19 2018/01/20 08:30:53 maxv Exp $");
 #endif
 
 /*
@@ -146,7 +146,7 @@ IDTVEC(trap02)
 	movq	%rsp,%rdi
 	incq	CPUVAR(NTRAP)
 	call	_C_LABEL(nmitrap)
-	SVS_LEAVE
+	SVS_LEAVE_ALTSTACK
 
 .Lnmileave:
 	movw	TF_ES(%rsp),%es
@@ -224,8 +224,43 @@ IDTVEC(trap07)
 	jmp	.Lalltraps_checkusr
 IDTVEC_END(trap07)
 
+/*
+ * Double faults execute on a particular stack, and we must not jump out
+ * of it. So don't enable interrupts.
+ */
 IDTVEC(trap08)
+#if defined(XEN)
 	TRAP(T_DOUBLEFLT)
+#else
+	TRAP_NJ(T_DOUBLEFLT)
+	subq	$TF_REGSIZE,%rsp
+	INTR_SAVE_GPRS
+	SVS_ENTER_ALTSTACK
+	testb	$SEL_UPL,TF_CS(%rsp)
+	jz	1f
+	swapgs
+1:
+	cld
+	SMAP_ENABLE
+	movw	%gs,TF_GS(%rsp)
+	movw	%fs,TF_FS(%rsp)
+	movw	%es,TF_ES(%rsp)
+	movw	%ds,TF_DS(%rsp)
+
+	movq	%rsp,%rdi
+	incq	CPUVAR(NTRAP)
+	call	_C_LABEL(doubletrap)
+
+	SVS_LEAVE_ALTSTACK
+	INTR_RESTORE_GPRS
+
+	testb	$SEL_UPL,TF_CS(%rsp)
+	jz	1f
+	swapgs
+1:
+	addq	$TF_REGSIZE+16,%rsp
+	iretq
+#endif
 IDTVEC_END(trap08)
 
 IDTVEC(trap09)

Index: src/sys/arch/amd64/amd64/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.110 src/sys/arch/amd64/amd64/trap.c:1.111
--- src/sys/arch/amd64/amd64/trap.c:1.110	Wed Jan 10 20:51:11 2018
+++ src/sys/arch/amd64/amd64/trap.c	Sat Jan 20 08:30:53 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.110 2018/01/10 20:51:11 maxv Exp $	*/
+/*	$NetBSD: trap.c,v 1.111 2018/01/20 08:30:53 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.110 2018/01/10 20:51:11 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.111 2018/01/20 08:30:53 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -121,6 +121,7 @@ dtrace_doubletrap_func_t	dtrace_doubletr
 #endif
 
 void nmitrap(struct trapframe *);
+void doubletrap(struct trapframe *);
 void trap(struct trapframe *);
 void trap_return_fault_return(struct trapframe *) __dead;
 
@@ -228,6 +229,22 @@ nmitrap(struct trapframe *frame)
 	x86_nmi();
 }
 
+void
+doubletrap(struct trapframe *frame)
+{
+	const int type = T_DOUBLEFLT;
+	struct lwp *l = curlwp;
+
+	trap_print(frame, l);
+
+	if (kdb_trap(type, 0, frame))
+		return;
+	if (kgdb_trap(type, frame))
+		return;
+
+	panic("double fault");
+}
+
 /*
  * Did we receive in kernel mode a trap that ought to be considered as a user
  * trap? If this function returns, the answer is no.



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

2018-01-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jan 20 07:43:28 UTC 2018

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

Log Message:
Improve two comments and a KASSERT.


To generate a diff of this commit:
cvs rdiff -u -r1.291 -r1.292 src/sys/arch/amd64/amd64/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.291 src/sys/arch/amd64/amd64/machdep.c:1.292
--- src/sys/arch/amd64/amd64/machdep.c:1.291	Thu Jan 18 07:25:34 2018
+++ src/sys/arch/amd64/amd64/machdep.c	Sat Jan 20 07:43:28 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.291 2018/01/18 07:25:34 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.292 2018/01/20 07:43:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.291 2018/01/18 07:25:34 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.292 2018/01/20 07:43:28 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -2478,7 +2478,7 @@ svs_pmap_sync(struct pmap *pmap, int ind
 	KASSERT(pmap != pmap_kernel());
 	KASSERT(mutex_owned(pmap->pm_lock));
 	KASSERT(kpreempt_disabled());
-	KASSERT(index <= 255);
+	KASSERT(index < 255);
 
 	for (CPU_INFO_FOREACH(cii, ci)) {
 		cid = cpu_index(ci);
@@ -2530,9 +2530,8 @@ svs_lwp_switch(struct lwp *oldlwp, struc
 	(ci->ci_svs_ursp0 % PAGE_SIZE));
 
 	/*
-	 * Enter the user rsp0. We don't need to flush the TLB here, it will
-	 * be implicitly flushed when we reload CR3 next time we return to
-	 * userland.
+	 * Enter the user rsp0. We don't need to flush the TLB here, since
+	 * the user page tables are not loaded.
 	 */
 	pte = ci->ci_svs_rsp0_pte;
 	*pte = L1_BASE[pl1_i(va)];
@@ -2549,7 +2548,7 @@ svs_pte_atomic_read(struct pmap *pmap, s
 
 /*
  * We may come here with the pmap unlocked. So read its PTEs atomically. If
- * a remote CPU is updating them at the same time, it's not that bad: the
+ * a remote CPU is updating them at the same time, it's not a problem: the
  * remote CPU will call svs_pmap_sync afterwards, and our updirpa will be
  * synchronized properly.
  */



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

2018-01-15 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Jan 15 22:38:01 UTC 2018

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.h

Log Message:
avoid typedef redefinitiones


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.18 src/sys/arch/amd64/stand/prekern/prekern.h:1.19
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.18	Sun Nov 26 06:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Mon Jan 15 17:38:01 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.18 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.19 2018/01/15 22:38:01 christos Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -38,9 +38,6 @@
 #include "redef.h"
 
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
-typedef uint64_t paddr_t;
-typedef uint64_t vaddr_t;
-typedef uint64_t pt_entry_t;
 typedef uint64_t pte_prot_t;
 #define WHITE_ON_BLACK 0x07
 #define RED_ON_BLACK 0x04



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

2018-01-13 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Jan 13 12:38:16 UTC 2018

Modified Files:
src/sys/arch/amd64/conf: ALL GENERIC MODULAR XEN3_DOM0 XEN3_DOMU

Log Message:
added commented out AUTOFS pseudo-device entries


To generate a diff of this commit:
cvs rdiff -u -r1.77 -r1.78 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.480 -r1.481 src/sys/arch/amd64/conf/GENERIC
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/conf/MODULAR
cvs rdiff -u -r1.143 -r1.144 src/sys/arch/amd64/conf/XEN3_DOM0
cvs rdiff -u -r1.81 -r1.82 src/sys/arch/amd64/conf/XEN3_DOMU

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/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.77 src/sys/arch/amd64/conf/ALL:1.78
--- src/sys/arch/amd64/conf/ALL:1.77	Wed Jan 10 06:11:20 2018
+++ src/sys/arch/amd64/conf/ALL	Sat Jan 13 07:38:16 2018
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.77 2018/01/10 11:11:20 knakahara Exp $
+# $NetBSD: ALL,v 1.78 2018/01/13 12:38:16 christos 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.77 $"
+#ident		"ALL-$Revision: 1.78 $"
 
 maxusers	64		# estimated number of users
 
@@ -1654,6 +1654,9 @@ pseudo-device	vcoda			# coda minicache <
 # a pseudo device needed for SMBFS
 pseudo-device	nsmb			# experimental - SMB requester
 
+# a pseudo device needed for AUTOFS
+pseudo-device	autofs			# experimental - AUTOFS
+
 # iSCSI initiator
 pseudo-device	iscsi
 

Index: src/sys/arch/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.480 src/sys/arch/amd64/conf/GENERIC:1.481
--- src/sys/arch/amd64/conf/GENERIC:1.480	Wed Jan 10 06:11:20 2018
+++ src/sys/arch/amd64/conf/GENERIC	Sat Jan 13 07:38:16 2018
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.480 2018/01/10 11:11:20 knakahara Exp $
+# $NetBSD: GENERIC,v 1.481 2018/01/13 12:38:16 christos Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.480 $"
+#ident		"GENERIC-$Revision: 1.481 $"
 
 maxusers	64		# estimated number of users
 
@@ -1325,6 +1325,9 @@ pseudo-device	vcoda			# coda minicache <
 # a pseudo device needed for SMBFS
 pseudo-device	nsmb			# experimental - SMB requester
 
+# a pseudo device needed for AUTOFS
+#pseudo-device	autofs			# experimental - AUTOFS
+
 # wscons pseudo-devices
 pseudo-device	wsmux			# mouse & keyboard multiplexor
 pseudo-device	wsfont

Index: src/sys/arch/amd64/conf/MODULAR
diff -u src/sys/arch/amd64/conf/MODULAR:1.10 src/sys/arch/amd64/conf/MODULAR:1.11
--- src/sys/arch/amd64/conf/MODULAR:1.10	Mon Jan  8 22:31:12 2018
+++ src/sys/arch/amd64/conf/MODULAR	Sat Jan 13 07:38:16 2018
@@ -1,4 +1,4 @@
-# $NetBSD: MODULAR,v 1.10 2018/01/09 03:31:12 christos Exp $
+# $NetBSD: MODULAR,v 1.11 2018/01/13 12:38:16 christos Exp $
 #
 # Try to exclude all the drivers in GENERIC that have been modularized
 # XXX: incomplete
@@ -132,6 +132,7 @@ options 	MODULAR_DEFAULT_AUTOLOAD
 # miscellaneous pseudo-devices
 -no pseudo-device	clockctl		# user control of clock subsystem
 -no pseudo-device	vcoda			# coda minicache <-> venus comm.
+-no pseudo-device	autofs			# experimental - AUTOFS
 
 # a pseudo device needed for SMBFS
 -no pseudo-device	nsmb			# experimental - SMB requester

Index: src/sys/arch/amd64/conf/XEN3_DOM0
diff -u src/sys/arch/amd64/conf/XEN3_DOM0:1.143 src/sys/arch/amd64/conf/XEN3_DOM0:1.144
--- src/sys/arch/amd64/conf/XEN3_DOM0:1.143	Mon Jan  8 22:31:12 2018
+++ src/sys/arch/amd64/conf/XEN3_DOM0	Sat Jan 13 07:38:16 2018
@@ -1,4 +1,4 @@
-# $NetBSD: XEN3_DOM0,v 1.143 2018/01/09 03:31:12 christos Exp $
+# $NetBSD: XEN3_DOM0,v 1.144 2018/01/13 12:38:16 christos Exp $
 
 include 	"arch/amd64/conf/std.xen"
 
@@ -10,7 +10,7 @@ options 	INCLUDE_CONFIG_FILE	# embed con
 #options 	UVMHIST_PRINT
 #options 	SYSCALL_DEBUG
 
-#ident		"XEN3_DOM0-$Revision: 1.143 $"
+#ident		"XEN3_DOM0-$Revision: 1.144 $"
 
 maxusers	32		# estimated number of users
 
@@ -888,6 +888,9 @@ pseudo-device	vcoda			# coda minicache <
 # a pseudo device needed for SMBFS
 pseudo-device	nsmb			# experimental - SMB requester
 
+# a pseudo device needed for AUTOFS
+#pseudo-device	autofs			# experimental - AUTOFS
+
 # iSCSI initiator
 #pseudo-device	iscsi
 

Index: src/sys/arch/amd64/conf/XEN3_DOMU
diff -u src/sys/arch/amd64/conf/XEN3_DOMU:1.81 src/sys/arch/amd64/conf/XEN3_DOMU:1.82
--- src/sys/arch/amd64/conf/XEN3_DOMU:1.81	Mon Jan  8 22:31:12 2018
+++ src/sys/arch/amd64/conf/XEN3_DOMU	Sat Jan 13 07:38:16 2018
@@ -1,4 +1,4 @@
-# $NetBSD: XEN3_DOMU,v 1.81 2018/01/09 03:31:12 christos Exp $
+# $NetBSD: XEN3_DOMU,v 1.82 2018/01/13 12:38:16 christos Exp $
 
 include 	"arch/amd64/conf/std.xen"
 
@@ -10,7 +10,7 @@ options 	

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

2018-01-12 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Jan 12 09:12:02 UTC 2018

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

Log Message:
Split svs_page_add in two, one half will be used for other purposes, and
update a comment.


To generate a diff of this commit:
cvs rdiff -u -r1.289 -r1.290 src/sys/arch/amd64/amd64/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.289 src/sys/arch/amd64/amd64/machdep.c:1.290
--- src/sys/arch/amd64/amd64/machdep.c:1.289	Thu Jan 11 13:35:15 2018
+++ src/sys/arch/amd64/amd64/machdep.c	Fri Jan 12 09:12:01 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.289 2018/01/11 13:35:15 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.290 2018/01/12 09:12:01 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.289 2018/01/11 13:35:15 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.290 2018/01/12 09:12:01 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -2261,11 +2261,16 @@ mm_md_direct_mapped_phys(paddr_t paddr, 
  * remote CPUs that execute other threads of the user process we just
  * left will keep synchronizing us against their changes.
  *
- * TODO: for now, only PMAP_SLOT_PTE is unmapped.
+ * List of areas that are removed from userland:
+ * PTE Space [OK]
+ * Direct Map[OK]
+ * Remote PCPU Areas [OK]
+ * Kernel Heap   [TODO]
+ * Kernel Image  [TODO]
  */
 
-static void
-svs_page_add(struct cpu_info *ci, vaddr_t va)
+static pd_entry_t *
+svs_tree_add(struct cpu_info *ci, vaddr_t va)
 {
 	extern pd_entry_t * const normal_pdes[];
 	extern const vaddr_t ptp_masks[];
@@ -2276,8 +2281,6 @@ svs_page_add(struct cpu_info *ci, vaddr_
 	struct vm_page *pg;
 	paddr_t pa;
 
-	KASSERT(va % PAGE_SIZE == 0);
-
 	dstpde = ci->ci_svs_updir;
 	mod = (size_t)-1;
 
@@ -2305,13 +2308,25 @@ svs_page_add(struct cpu_info *ci, vaddr_
 		mod = nbpd[i-1];
 	}
 
-	/* Do the last level manually */
-	idx = pl_i(va, 1);
+	return dstpde;
+}
+
+static void
+svs_page_add(struct cpu_info *ci, vaddr_t va)
+{
+	pd_entry_t *srcpde, *dstpde;
+	size_t idx, pidx;
+
+	/* Create levels L4, L3 and L2. */
+	dstpde = svs_tree_add(ci, va);
+
+	/* Enter L1. */
+	idx = pl1_i(va);
 	srcpde = L1_BASE;
 	if (!pmap_valid_entry(srcpde[idx])) {
 		panic("%s: L1 page not mapped", __func__);
 	}
-	pidx = pl_i(va % mod, 1);
+	pidx = pl1_i(va % NBPD_L2);
 	if (pmap_valid_entry(dstpde[pidx])) {
 		panic("%s: L1 page already mapped", __func__);
 	}



CVS commit: src/sys/arch/amd64

2018-01-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Jan 11 09:00:04 UTC 2018

Modified Files:
src/sys/arch/amd64/amd64: locore.S machdep.c
src/sys/arch/amd64/include: frameasm.h types.h

Log Message:
Declare new SVS_* variants: SVS_ENTER_NOSTACK and SVS_LEAVE_NOSTACK. Use
SVS_ENTER_NOSTACK in the syscall entry point, and put it before the code
that touches curlwp. (curlwp is located in the direct map.)

Then, disable __HAVE_CPU_UAREA_ROUTINES (to be removed later). This moves
the kernel stack into pmap_kernel(), and not the direct map. That's a
change I've always wanted to make: because of the direct map we can't add
a redzone on the stack, and basically, a stack overflow can go very far
in memory without being detected (as far as erasing all of the system's
memory).

Finally, unmap the direct map from userland.


To generate a diff of this commit:
cvs rdiff -u -r1.145 -r1.146 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.285 -r1.286 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.27 -r1.28 src/sys/arch/amd64/include/frameasm.h
cvs rdiff -u -r1.53 -r1.54 src/sys/arch/amd64/include/types.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/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.145 src/sys/arch/amd64/amd64/locore.S:1.146
--- src/sys/arch/amd64/amd64/locore.S:1.145	Sun Jan  7 16:10:16 2018
+++ src/sys/arch/amd64/amd64/locore.S	Thu Jan 11 09:00:04 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.145 2018/01/07 16:10:16 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.146 2018/01/11 09:00:04 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1268,6 +1268,7 @@ IDTVEC(syscall)
 	 * is ignored as well.
 	 */
 	swapgs
+	SVS_ENTER_NOSTACK
 	movq	%r15,CPUVAR(SCRATCH)
 	movq	CPUVAR(CURLWP),%r15
 	movq	L_PCB(%r15),%r15
@@ -1295,7 +1296,6 @@ IDTVEC(syscall)
 	subq	$TF_REGSIZE,%rsp
 	cld
 #endif
-	SVS_ENTER
 	INTR_SAVE_GPRS
 	movw	$GSEL(GUDATA_SEL, SEL_UPL),TF_DS(%rsp)
 	movw	$GSEL(GUDATA_SEL, SEL_UPL),TF_ES(%rsp)

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.285 src/sys/arch/amd64/amd64/machdep.c:1.286
--- src/sys/arch/amd64/amd64/machdep.c:1.285	Sun Jan  7 16:10:16 2018
+++ src/sys/arch/amd64/amd64/machdep.c	Thu Jan 11 09:00:04 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.285 2018/01/07 16:10:16 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.286 2018/01/11 09:00:04 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.285 2018/01/07 16:10:16 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.286 2018/01/11 09:00:04 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -2339,6 +2339,8 @@ svs_pte_atomic_read(struct pmap *pmap, s
 void
 svs_pdir_switch(struct pmap *pmap)
 {
+	extern size_t pmap_direct_pdpe;
+	extern size_t pmap_direct_npdp;
 	struct cpu_info *ci = curcpu();
 	pt_entry_t pte;
 	size_t i;
@@ -2351,8 +2353,14 @@ svs_pdir_switch(struct pmap *pmap)
 	mutex_enter(>ci_svs_mtx);
 
 	for (i = 0; i < 512; i++) {
-		if (i == PDIR_SLOT_PTE) {
-			/* We don't want to have this mapped. */
+		/*
+		 * This is where we decide what to unmap from the user page
+		 * tables.
+		 */
+		if (pmap_direct_pdpe <= i &&
+		i < pmap_direct_pdpe + pmap_direct_npdp) {
+			ci->ci_svs_updir[i] = 0;
+		} else if (i == PDIR_SLOT_PTE) {
 			ci->ci_svs_updir[i] = 0;
 		} else {
 			pte = svs_pte_atomic_read(pmap, i);

Index: src/sys/arch/amd64/include/frameasm.h
diff -u src/sys/arch/amd64/include/frameasm.h:1.27 src/sys/arch/amd64/include/frameasm.h:1.28
--- src/sys/arch/amd64/include/frameasm.h:1.27	Sun Jan  7 16:10:16 2018
+++ src/sys/arch/amd64/include/frameasm.h	Thu Jan 11 09:00:04 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: frameasm.h,v 1.27 2018/01/07 16:10:16 maxv Exp $	*/
+/*	$NetBSD: frameasm.h,v 1.28 2018/01/11 09:00:04 maxv Exp $	*/
 
 #ifndef _AMD64_MACHINE_FRAMEASM_H
 #define _AMD64_MACHINE_FRAMEASM_H
@@ -107,9 +107,21 @@
 	movq	CPUVAR(UPDIRPA),%rax	; \
 	movq	%rax,%cr3		; \
 	popq	%rax
+#define SVS_ENTER_NOSTACK \
+	movq	%rax,CPUVAR(SCRATCH)	; \
+	movq	CPUVAR(KPDIRPA),%rax	; \
+	movq	%rax,%cr3		; \
+	movq	CPUVAR(SCRATCH),%rax
+#define SVS_LEAVE_NOSTACK \
+	movq	%rax,CPUVAR(SCRATCH)	; \
+	movq	CPUVAR(UPDIRPA),%rax	; \
+	movq	%rax,%cr3		; \
+	movq	CPUVAR(SCRATCH),%rax
 #else
 #define SVS_ENTER	/* nothing */
 #define SVS_LEAVE	/* nothing */
+#define SVS_ENTER_NOSTACK	/* nothing */
+#define SVS_LEAVE_NOSTACK	/* nothing */
 #endif
 
 #define	INTRENTRY_L(kernel_trap, usertrap) \

Index: src/sys/arch/amd64/include/types.h
diff -u src/sys/arch/amd64/include/types.h:1.53 src/sys/arch/amd64/include/types.h:1.54
--- src/sys/arch/amd64/include/types.h:1.53	Fri Jan  5 08:04:21 2018
+++ src/sys/arch/amd64/include/types.h	Thu Jan 11 09:00:04 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: types.h,v 1.53 2018/01/05 08:04:21 maxv Exp $	*/
+/*	$NetBSD: types.h,v 1.54 

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

2018-01-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Jan 10 20:51:11 UTC 2018

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

Log Message:
Restrict the check: SMAP faults are always protection violations, as the
SDM points out, so make sure we have PGEX_P. This way NULL dereferences -
which are caused by an unmapped VA, and therefore are not protection
violations - don't take this branch, and don't display a misleading
"SMAP" in ddb.

Adding a PGEX_P check, or not, does not essentially change anything from
a security point of view, it's just a matter of what gets displayed when
a fatal fault comes in.

I didn't put PGEX_P until now, because initially when I wrote the SMAP
implementation Qemu did not always receive the fault if the PGEX_P check
was there, while a native i5 would. I'm unable to reproduce this issue
with a recent Qemu, so I assume I did something wrong when testing in the
first place.


To generate a diff of this commit:
cvs rdiff -u -r1.109 -r1.110 src/sys/arch/amd64/amd64/trap.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.109 src/sys/arch/amd64/amd64/trap.c:1.110
--- src/sys/arch/amd64/amd64/trap.c:1.109	Sat Dec  9 00:52:41 2017
+++ src/sys/arch/amd64/amd64/trap.c	Wed Jan 10 20:51:11 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.109 2017/12/09 00:52:41 christos Exp $	*/
+/*	$NetBSD: trap.c,v 1.110 2018/01/10 20:51:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.109 2017/12/09 00:52:41 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.110 2018/01/10 20:51:11 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -562,13 +562,11 @@ trap(struct trapframe *frame)
 			}
 		}
 
-		if (cr2 < VM_MAXUSER_ADDRESS) {
+		if ((frame->tf_err & PGEX_P) &&
+		cr2 < VM_MAXUSER_ADDRESS) {
 			/* SMAP might have brought us here */
 			if (onfault_handler(pcb, frame) == NULL) {
-panic("prevented %s %p (SMAP)",
-(cr2 < PAGE_SIZE
-	? "null pointer dereference at"
-	: "access to"),
+panic("prevented access to %p (SMAP)",
 (void *)cr2);
 			}
 		}



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

2018-01-10 Thread Kengo NAKAHARA
Module Name:src
Committed By:   knakahara
Date:   Wed Jan 10 11:11:20 UTC 2018

Modified Files:
src/sys/arch/amd64/conf: ALL GENERIC

Log Message:
add ipsec(4) interface to amd64/GENERIC and amd64/ALL configs.


To generate a diff of this commit:
cvs rdiff -u -r1.76 -r1.77 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.479 -r1.480 src/sys/arch/amd64/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/amd64/conf/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.76 src/sys/arch/amd64/conf/ALL:1.77
--- src/sys/arch/amd64/conf/ALL:1.76	Tue Jan  9 03:31:12 2018
+++ src/sys/arch/amd64/conf/ALL	Wed Jan 10 11:11:20 2018
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.76 2018/01/09 03:31:12 christos Exp $
+# $NetBSD: ALL,v 1.77 2018/01/10 11:11:20 knakahara 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.76 $"
+#ident		"ALL-$Revision: 1.77 $"
 
 maxusers	64		# estimated number of users
 
@@ -1610,6 +1610,7 @@ pseudo-device	tap			# virtual Ethernet
 pseudo-device	tun			# network tunneling over tty
 pseudo-device	gre			# generic L3 over IP tunnel
 pseudo-device	gif			# IPv[46] over IPv[46] tunnel (RFC 1933)
+pseudo-device	ipsecif			# tunnel interface for routing based ipsec
 pseudo-device	faith			# IPv[46] tcp relay translation i/f
 pseudo-device	stf			# 6to4 IPv6 over IPv4 encapsulation
 pseudo-device	vlan			# IEEE 802.1q encapsulation

Index: src/sys/arch/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.479 src/sys/arch/amd64/conf/GENERIC:1.480
--- src/sys/arch/amd64/conf/GENERIC:1.479	Tue Jan  9 03:31:12 2018
+++ src/sys/arch/amd64/conf/GENERIC	Wed Jan 10 11:11:20 2018
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.479 2018/01/09 03:31:12 christos Exp $
+# $NetBSD: GENERIC,v 1.480 2018/01/10 11:11:20 knakahara Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.479 $"
+#ident		"GENERIC-$Revision: 1.480 $"
 
 maxusers	64		# estimated number of users
 
@@ -1288,6 +1288,7 @@ pseudo-device	tun			# network tunneling 
 pseudo-device	tap			# virtual Ethernet
 pseudo-device	gre			# generic L3 over IP tunnel
 pseudo-device	gif			# IPv[46] over IPv[46] tunnel (RFC1933)
+pseudo-device	ipsecif			# tunnel interface for routing based ipsec
 #pseudo-device	faith			# IPv[46] tcp relay translation i/f
 pseudo-device	stf			# 6to4 IPv6 over IPv4 encapsulation
 pseudo-device	vlan			# IEEE 802.1q encapsulation



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

2018-01-07 Thread Martin Husemann
Module Name:src
Committed By:   martin
Date:   Mon Jan  8 06:29:19 UTC 2018

Modified Files:
src/sys/arch/amd64/conf: files.amd64

Log Message:
Now that SVS is defined in file.x86, do not repeat it here


To generate a diff of this commit:
cvs rdiff -u -r1.98 -r1.99 src/sys/arch/amd64/conf/files.amd64

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/files.amd64
diff -u src/sys/arch/amd64/conf/files.amd64:1.98 src/sys/arch/amd64/conf/files.amd64:1.99
--- src/sys/arch/amd64/conf/files.amd64:1.98	Sun Jan  7 16:10:16 2018
+++ src/sys/arch/amd64/conf/files.amd64	Mon Jan  8 06:29:19 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.amd64,v 1.98 2018/01/07 16:10:16 maxv Exp $
+#	$NetBSD: files.amd64,v 1.99 2018/01/08 06:29:19 martin Exp $
 #
 # new style config file for amd64 architecture
 #
@@ -27,7 +27,7 @@ defparam opt_physmem.h	PHYSMEM_MAX_ADDR 
 defflag			PMC
 defflag			USER_LDT
 defflag			KASLR
-defflag			SVS
+# defflag			SVS	# already defined in files.x86
 defflag eisa.h EISA
 
 # Start code



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

2018-01-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Jan  7 16:47:22 UTC 2018

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

Log Message:
make this compile again


To generate a diff of this commit:
cvs rdiff -u -r1.65 -r1.66 src/sys/arch/amd64/amd64/genassym.cf

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.65 src/sys/arch/amd64/amd64/genassym.cf:1.66
--- src/sys/arch/amd64/amd64/genassym.cf:1.65	Sun Jan  7 11:10:16 2018
+++ src/sys/arch/amd64/amd64/genassym.cf	Sun Jan  7 11:47:22 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: genassym.cf,v 1.65 2018/01/07 16:10:16 maxv Exp $
+#	$NetBSD: genassym.cf,v 1.66 2018/01/07 16:47:22 christos Exp $
 
 #
 # Copyright (c) 1998, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -236,8 +236,10 @@ define	CPU_INFO_CURLDT		offsetof(struct 
 define	CPU_INFO_IDLELWP	offsetof(struct cpu_info, ci_data.cpu_idlelwp)
 define	CPU_INFO_PMAP		offsetof(struct cpu_info, ci_pmap)
 define	CPU_INFO_TSS		offsetof(struct cpu_info, ci_tss)
+ifdef SVS
 define	CPU_INFO_UPDIRPA	offsetof(struct cpu_info, ci_svs_updirpa)
 define	CPU_INFO_KPDIRPA	offsetof(struct cpu_info, ci_svs_kpdirpa)
+endif
 define	CPU_INFO_NSYSCALL	offsetof(struct cpu_info, ci_data.cpu_nsyscall)
 define	CPU_INFO_NTRAP		offsetof(struct cpu_info, ci_data.cpu_ntrap)
 define	CPU_INFO_NINTR		offsetof(struct cpu_info, ci_data.cpu_nintr)



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

2018-01-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Jan  7 16:10:52 UTC 2018

Modified Files:
src/sys/arch/amd64/conf: GENERIC

Log Message:
Don't enable SVS yet.


To generate a diff of this commit:
cvs rdiff -u -r1.477 -r1.478 src/sys/arch/amd64/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/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.477 src/sys/arch/amd64/conf/GENERIC:1.478
--- src/sys/arch/amd64/conf/GENERIC:1.477	Sun Jan  7 16:10:16 2018
+++ src/sys/arch/amd64/conf/GENERIC	Sun Jan  7 16:10:52 2018
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.477 2018/01/07 16:10:16 maxv Exp $
+# $NetBSD: GENERIC,v 1.478 2018/01/07 16:10:52 maxv Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.477 $"
+#ident		"GENERIC-$Revision: 1.478 $"
 
 maxusers	64		# estimated number of users
 
@@ -75,7 +75,7 @@ options 	SYSCTL_INCLUDE_DESCR	# Include 
 
 # CPU-related options
 #options 	USER_LDT	# user-settable LDT; used by WINE
-options 	SVS		# Separate Virtual Space
+#options 	SVS		# Separate Virtual Space
 
 # CPU features
 acpicpu*	at cpu?		# ACPI CPU (including frequency scaling)



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

2018-01-06 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jan  6 08:44:01 UTC 2018

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

Log Message:
Mmh, I made a mistake in r1.10 - I forgot to update this function call.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/amd64/amd64/amd64_trap.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.14 src/sys/arch/amd64/amd64/amd64_trap.S:1.15
--- src/sys/arch/amd64/amd64/amd64_trap.S:1.14	Tue Jan  2 18:41:14 2018
+++ src/sys/arch/amd64/amd64/amd64_trap.S	Sat Jan  6 08:44:01 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: amd64_trap.S,v 1.14 2018/01/02 18:41:14 maxv Exp $	*/
+/*	$NetBSD: amd64_trap.S,v 1.15 2018/01/06 08:44:01 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2008, 2017 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
 
 #if 0
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.14 2018/01/02 18:41:14 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.15 2018/01/06 08:44:01 maxv Exp $");
 #endif
 
 /*
@@ -143,7 +143,7 @@ IDTVEC(trap02)
 .Lnoswapgs:
 	movq	%rsp,%rdi
 	incq	CPUVAR(NTRAP)
-	call	_C_LABEL(trap)
+	call	_C_LABEL(nmitrap)
 
 .Lnmileave:
 	movw	TF_ES(%rsp),%es



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

2018-01-02 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Jan  2 18:54:26 UTC 2018

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

Log Message:
Stop sharing the double-fault stack. It is embedded in .data, and we won't
want that in the future. This has always been wrong anyway, even if it is
unlikely that two CPUs will double fault at the same time.


To generate a diff of this commit:
cvs rdiff -u -r1.280 -r1.281 src/sys/arch/amd64/amd64/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.280 src/sys/arch/amd64/amd64/machdep.c:1.281
--- src/sys/arch/amd64/amd64/machdep.c:1.280	Sun Dec 31 08:29:38 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Tue Jan  2 18:54:26 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.280 2017/12/31 08:29:38 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.281 2018/01/02 18:54:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.280 2017/12/31 08:29:38 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.281 2018/01/02 18:54:26 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -290,8 +290,6 @@ struct pool x86_dbregspl;
 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
 int mem_cluster_cnt;
 
-char x86_64_doubleflt_stack[4096];
-
 int cpu_dump(void);
 int cpu_dumpsize(void);
 u_long cpu_dump_mempagecnt(void);
@@ -511,11 +509,13 @@ cpu_init_tss(struct cpu_info *ci)
 	/* tss->tss_ist[0] is filled by cpu_intr_init */
 
 	/* double fault */
-	tss->tss_ist[1] = (uint64_t)x86_64_doubleflt_stack + PAGE_SIZE - 16;
+	p = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_WIRED);
+	tss->tss_ist[1] = p + PAGE_SIZE - 16;
 
 	/* NMI */
 	p = uvm_km_alloc(kernel_map, PAGE_SIZE, 0, UVM_KMF_WIRED);
 	tss->tss_ist[2] = p + PAGE_SIZE - 16;
+
 	ci->ci_tss_sel = tss_alloc(tss);
 }
 



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

2018-01-02 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Jan  2 18:41:14 UTC 2018

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

Log Message:
Use decimal numbering - hex is just misleading -, use ZTRAP_NJ for NMIs,
and declare intrspurious independently.


To generate a diff of this commit:
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/amd64/amd64/amd64_trap.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.13 src/sys/arch/amd64/amd64/amd64_trap.S:1.14
--- src/sys/arch/amd64/amd64/amd64_trap.S:1.13	Sun Nov 26 14:54:43 2017
+++ src/sys/arch/amd64/amd64/amd64_trap.S	Tue Jan  2 18:41:14 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: amd64_trap.S,v 1.13 2017/11/26 14:54:43 maxv Exp $	*/
+/*	$NetBSD: amd64_trap.S,v 1.14 2018/01/02 18:41:14 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2008, 2017 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
 
 #if 0
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.13 2017/11/26 14:54:43 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.14 2018/01/02 18:41:14 maxv Exp $");
 #endif
 
 /*
@@ -118,8 +118,7 @@ IDTVEC(trap02)
 #if defined(XEN)
 	ZTRAP(T_NMI)
 #else
-	pushq	$0
-	pushq	$T_NMI
+	ZTRAP_NJ(T_NMI)
 	subq	$TF_REGSIZE,%rsp
 	INTR_SAVE_GPRS
 	cld
@@ -230,9 +229,9 @@ IDTVEC(trap09)
 	ZTRAP(T_FPOPFLT)
 IDTVEC_END(trap09)
 
-IDTVEC(trap0a)
+IDTVEC(trap10)
 	TRAP(T_TSSFLT)
-IDTVEC_END(trap0a)
+IDTVEC_END(trap10)
 
 #ifdef XEN
 /*
@@ -243,37 +242,35 @@ IDTVEC_END(trap0a)
 #define check_swapgs alltraps
 #endif
 
-IDTVEC(trap0b)		/* #NP() Segment not present */
+IDTVEC(trap11)		/* #NP() Segment not present */
 	TRAP_NJ(T_SEGNPFLT)
 	jmp	check_swapgs
-IDTVEC_END(trap0b)
+IDTVEC_END(trap11)
 
-IDTVEC(trap0c)		/* #SS() Stack exception */
+IDTVEC(trap12)		/* #SS() Stack exception */
 	TRAP_NJ(T_STKFLT)
 	jmp	check_swapgs
-IDTVEC_END(trap0c)
+IDTVEC_END(trap12)
 
-IDTVEC(trap0d)		/* #GP() General protection */
+IDTVEC(trap13)		/* #GP() General protection */
 	TRAP_NJ(T_PROTFLT)
 	jmp	check_swapgs
-IDTVEC_END(trap0d)
+IDTVEC_END(trap13)
 
-IDTVEC(trap0e)
+IDTVEC(trap14)
 	TRAP(T_PAGEFLT)
-IDTVEC_END(trap0e)
+IDTVEC_END(trap14)
 
-IDTVEC(intrspurious)
-IDTVEC(trap0f)
+IDTVEC(trap15)
 	ZTRAP_NJ(T_ASTFLT)
 	INTRENTRY
 #ifdef DIAGNOSTIC
 	movl	CPUVAR(ILEVEL),%ebx
 #endif
 	jmp	.Lalltraps_checkusr
-IDTVEC_END(trap0f)
-IDTVEC_END(intrspurious)
+IDTVEC_END(trap15)
 
-IDTVEC(trap10)
+IDTVEC(trap16)
 	ZTRAP_NJ(T_ARITHTRAP)
 .Ldo_fputrap:
 	INTRENTRY
@@ -283,47 +280,47 @@ IDTVEC(trap10)
 	movq	%rsp,%rdi
 	call	_C_LABEL(fputrap)
 	jmp	.Lalltraps_checkusr
-IDTVEC_END(trap10)
+IDTVEC_END(trap16)
 
-IDTVEC(trap11)
+IDTVEC(trap17)
 	TRAP(T_ALIGNFLT)
-IDTVEC_END(trap11)
+IDTVEC_END(trap17)
 
-IDTVEC(trap12)
+IDTVEC(trap18)
 	ZTRAP(T_MCA)
-IDTVEC_END(trap12)
+IDTVEC_END(trap18)
 
-IDTVEC(trap13)
+IDTVEC(trap19)
 	ZTRAP_NJ(T_XMM)
 	jmp	.Ldo_fputrap
-IDTVEC_END(trap13)
+IDTVEC_END(trap19)
 
-IDTVEC(trap14)
-IDTVEC(trap15)
-IDTVEC(trap16)
-IDTVEC(trap17)
-IDTVEC(trap18)
-IDTVEC(trap19)
-IDTVEC(trap1a)
-IDTVEC(trap1b)
-IDTVEC(trap1c)
-IDTVEC(trap1d)
-IDTVEC(trap1e)
-IDTVEC(trap1f)
+IDTVEC(trap20)
+IDTVEC(trap21)
+IDTVEC(trap22)
+IDTVEC(trap23)
+IDTVEC(trap24)
+IDTVEC(trap25)
+IDTVEC(trap26)
+IDTVEC(trap27)
+IDTVEC(trap28)
+IDTVEC(trap29)
+IDTVEC(trap30)
+IDTVEC(trap31)
 	/* 20 - 31 reserved for future exp */
 	ZTRAP(T_RESERVED)
-IDTVEC_END(trap1f)
-IDTVEC_END(trap1e)
-IDTVEC_END(trap1d)
-IDTVEC_END(trap1c)
-IDTVEC_END(trap1b)
-IDTVEC_END(trap1a)
-IDTVEC_END(trap19)
-IDTVEC_END(trap18)
-IDTVEC_END(trap17)
-IDTVEC_END(trap16)
-IDTVEC_END(trap15)
-IDTVEC_END(trap14)
+IDTVEC_END(trap20)
+IDTVEC_END(trap21)
+IDTVEC_END(trap22)
+IDTVEC_END(trap23)
+IDTVEC_END(trap24)
+IDTVEC_END(trap25)
+IDTVEC_END(trap26)
+IDTVEC_END(trap27)
+IDTVEC_END(trap28)
+IDTVEC_END(trap29)
+IDTVEC_END(trap30)
+IDTVEC_END(trap31)
 
 IDTVEC(exceptions)
 	.quad	_C_LABEL(Xtrap00), _C_LABEL(Xtrap01)
@@ -331,19 +328,28 @@ IDTVEC(exceptions)
 	.quad	_C_LABEL(Xtrap04), _C_LABEL(Xtrap05)
 	.quad	_C_LABEL(Xtrap06), _C_LABEL(Xtrap07)
 	.quad	_C_LABEL(Xtrap08), _C_LABEL(Xtrap09)
-	.quad	_C_LABEL(Xtrap0a), _C_LABEL(Xtrap0b)
-	.quad	_C_LABEL(Xtrap0c), _C_LABEL(Xtrap0d)
-	.quad	_C_LABEL(Xtrap0e), _C_LABEL(Xtrap0f)
 	.quad	_C_LABEL(Xtrap10), _C_LABEL(Xtrap11)
 	.quad	_C_LABEL(Xtrap12), _C_LABEL(Xtrap13)
 	.quad	_C_LABEL(Xtrap14), _C_LABEL(Xtrap15)
 	.quad	_C_LABEL(Xtrap16), _C_LABEL(Xtrap17)
 	.quad	_C_LABEL(Xtrap18), _C_LABEL(Xtrap19)
-	.quad	_C_LABEL(Xtrap1a), _C_LABEL(Xtrap1b)
-	.quad	_C_LABEL(Xtrap1c), _C_LABEL(Xtrap1d)
-	.quad	_C_LABEL(Xtrap1e), _C_LABEL(Xtrap1f)
+	.quad	_C_LABEL(Xtrap20), _C_LABEL(Xtrap21)
+	.quad	_C_LABEL(Xtrap22), _C_LABEL(Xtrap23)
+	.quad	_C_LABEL(Xtrap24), _C_LABEL(Xtrap25)
+	.quad	_C_LABEL(Xtrap26), _C_LABEL(Xtrap27)
+	.quad	_C_LABEL(Xtrap28), _C_LABEL(Xtrap29)
+	.quad	_C_LABEL(Xtrap30), _C_LABEL(Xtrap31)
 IDTVEC_END(exceptions)
 

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

2018-01-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Jan  1 08:14:14 UTC 2018

Modified Files:
src/sys/arch/amd64/conf: files.amd64

Log Message:
Compile the prekern entry point only under KASLR.


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

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/files.amd64
diff -u src/sys/arch/amd64/conf/files.amd64:1.96 src/sys/arch/amd64/conf/files.amd64:1.97
--- src/sys/arch/amd64/conf/files.amd64:1.96	Thu Dec  7 23:11:50 2017
+++ src/sys/arch/amd64/conf/files.amd64	Mon Jan  1 08:14:13 2018
@@ -1,4 +1,4 @@
-#	$NetBSD: files.amd64,v 1.96 2017/12/07 23:11:50 christos Exp $
+#	$NetBSD: files.amd64,v 1.97 2018/01/01 08:14:13 maxv Exp $
 #
 # new style config file for amd64 architecture
 #
@@ -47,7 +47,7 @@ file	arch/amd64/amd64/kobj_machdep.c		mo
 file	kern/subr_disk_mbr.c			disk
 file	arch/amd64/amd64/gdt.c			machdep
 file	arch/amd64/amd64/machdep.c		machdep
-file	arch/amd64/amd64/prekern.c		machdep
+file	arch/amd64/amd64/prekern.c		kaslr
 file	arch/amd64/amd64/process_machdep.c	machdep
 file	arch/amd64/amd64/trap.c			machdep
 file	arch/x86/x86/fpu.c			machdep



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

2017-12-30 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Dec 31 07:23:09 UTC 2017

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

Log Message:
gc unused


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/arch/amd64/include/pcb.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/pcb.h
diff -u src/sys/arch/amd64/include/pcb.h:1.27 src/sys/arch/amd64/include/pcb.h:1.28
--- src/sys/arch/amd64/include/pcb.h:1.27	Tue Oct 31 12:02:20 2017
+++ src/sys/arch/amd64/include/pcb.h	Sun Dec 31 07:23:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: pcb.h,v 1.27 2017/10/31 12:02:20 maxv Exp $	*/
+/*	$NetBSD: pcb.h,v 1.28 2017/12/31 07:23:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998 The NetBSD Foundation, Inc.
@@ -79,8 +79,7 @@
 
 struct pcb {
 	int	  pcb_flags;
-#define	PCB_USER_LDT	0x01		/* has user-set LDT */
-#define	PCB_COMPAT32	0x02
+#define	PCB_COMPAT32	0x01
 	u_int	  pcb_cr0;		/* saved image of CR0 */
 	uint64_t pcb_rsp0;
 	uint64_t pcb_cr2;		/* page fault address (CR2) */



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

2017-12-30 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sun Dec 31 03:38:06 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: GENERIC

Log Message:
explain that a.out never shipped on x86_64 and put back compat_nomid


To generate a diff of this commit:
cvs rdiff -u -r1.475 -r1.476 src/sys/arch/amd64/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/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.475 src/sys/arch/amd64/conf/GENERIC:1.476
--- src/sys/arch/amd64/conf/GENERIC:1.475	Sun Dec 10 12:19:48 2017
+++ src/sys/arch/amd64/conf/GENERIC	Sat Dec 30 22:38:06 2017
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.475 2017/12/10 17:19:48 bouyer Exp $
+# $NetBSD: GENERIC,v 1.476 2017/12/31 03:38:06 christos Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.475 $"
+#ident		"GENERIC-$Revision: 1.476 $"
 
 maxusers	64		# estimated number of users
 
@@ -115,11 +115,14 @@ makeoptions	DEBUG="-g"	# compile full sy
 options 	KDTRACE_HOOKS	# kernel DTrace hooks
 
 # Compatibility options
+# x86_64 never shipped with a.out binaries; the two options below are
+# only relevant to 32-bit i386 binaries
 #options 	EXEC_AOUT	# required by binaries from before 1.5
+#options	COMPAT_NOMID	# NetBSD 0.8, 386BSD, and BSDI
 
 # NetBSD backward compatibility. Support goes from COMPAT_15 up until
 # the latest release. Note that really old compat (< COMPAT_16) is only
-# useful for 32-bit binaries.
+# useful for 32-bit i386 binaries.
 include 	"conf/compat_netbsd15.config"
 
 #options 	COMPAT_386BSD_MBRPART # recognize old partition ID



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

2017-12-22 Thread Ryo ONODERA
Module Name:src
Committed By:   ryoon
Date:   Sat Dec 23 06:48:30 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile

Log Message:
Use ldscript from src to fix build.sh build


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/Makefile

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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.5 src/sys/arch/amd64/stand/prekern/Makefile:1.6
--- src/sys/arch/amd64/stand/prekern/Makefile:1.5	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Sat Dec 23 06:48:30 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.5 2017/11/26 11:01:09 maxv Exp $
+#	$NetBSD: Makefile,v 1.6 2017/12/23 06:48:30 ryoon Exp $
 
 PROG=		prekern
 SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c prng.c
@@ -25,7 +25,8 @@ CPPFLAGS+=	-DKERNEL -D__x86_64__
 CFLAGS+=	-Wall -Werror -Wstrict-prototypes
 CFLAGS+=	-mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
 STRIPFLAG=
-LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
+LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 \
+		-T ${S}/arch/amd64/stand/prekern/prekern.ldscript
 
 KERN_AS=	library
 .include	"${S}/lib/libkern/Makefile.inc"



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

2017-12-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Dec 22 07:37:27 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S trap.S

Log Message:
Sync comments with reality.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/trap.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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.6 src/sys/arch/amd64/stand/prekern/locore.S:1.7
--- src/sys/arch/amd64/stand/prekern/locore.S:1.6	Sun Nov 26 10:21:20 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Fri Dec 22 07:37:27 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.6 2017/11/26 10:21:20 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.7 2017/12/22 07:37:27 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -349,18 +349,17 @@ no_NOX:
  * --+
  *  (5)
  *
- * Virtual address space of the prekern:
- * +---+--+--+-+
- * | PREKERN IMAGE |**UNUSED**| BOOTSTRAP TABLES | ISA I/O MEM |
- * +---+--+--+-+
+ * The virtual address space is the same, since it is identity-mapped (va = pa).
+ * However, the KERNEL IMAGE is mapped as read-only: the prekern reads it, but
+ * won't write to it. (Needed when relocating the kernel.)
  *
  * 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.
+ * Important note: the prekern segments are properly 4k-aligned
+ * (see prekern.ldscript), so there's no need to enforce alignment.
  */
 
 	/* Find end of the prekern image; brings us on (1). */

Index: src/sys/arch/amd64/stand/prekern/trap.S
diff -u src/sys/arch/amd64/stand/prekern/trap.S:1.1 src/sys/arch/amd64/stand/prekern/trap.S:1.2
--- src/sys/arch/amd64/stand/prekern/trap.S:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/trap.S	Fri Dec 22 07:37:27 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.S,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: trap.S,v 1.2 2017/12/22 07:37:27 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -178,15 +178,14 @@ IDTVEC_END(exceptions)
 
 /*
  * Arguments pushed on the stack:
- *  tf_trapno
- *  tf_err: Dummy inserted if not defined
- *  tf_rip
- *  tf_cs
- *  tf_rflags
- *  tf_rsp
- *  tf_ss
+ *  sf_trapno
+ *  sf_err  (dummy inserted if not defined)
+ *  sf_rip
+ *  sf_cs
+ *  sf_rflags
+ *  sf_rsp
+ *  sf_ss
  */
-
 NENTRY(alltraps)
 	movq	%rsp,%rdi
 	call	_C_LABEL(trap)



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

2017-12-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Dec 21 14:32:06 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Remove unused macros.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.20 src/sys/arch/amd64/stand/prekern/mm.c:1.21
--- src/sys/arch/amd64/stand/prekern/mm.c:1.20	Sun Nov 26 14:29:48 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Thu Dec 21 14:32:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.20 2017/11/26 14:29:48 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.21 2017/12/21 14:32:06 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -30,10 +30,6 @@
 
 #include "prekern.h"
 
-#define PAD_TEXT	0xCC
-#define PAD_RODATA	0x00
-#define PAD_DATA	0x00
-
 #define ELFROUND	64
 
 static const uint8_t pads[4] = {



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

2017-12-08 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Dec  9 00:52:41 UTC 2017

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

Log Message:
adjust for new hexdump signature


To generate a diff of this commit:
cvs rdiff -u -r1.108 -r1.109 src/sys/arch/amd64/amd64/trap.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.108 src/sys/arch/amd64/amd64/trap.c:1.109
--- src/sys/arch/amd64/amd64/trap.c:1.108	Fri Dec  8 16:52:21 2017
+++ src/sys/arch/amd64/amd64/trap.c	Fri Dec  8 19:52:41 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.108 2017/12/08 21:52:21 christos Exp $	*/
+/*	$NetBSD: trap.c,v 1.109 2017/12/09 00:52:41 christos Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.108 2017/12/08 21:52:21 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.109 2017/12/09 00:52:41 christos Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -815,7 +815,7 @@ frame_dump(const struct trapframe *tf, s
 	tf->tf_fs & 0x, tf->tf_gs & 0x, tf->tf_ss & 0x);
 	printf("fsbase %#018lx gsbase %#018lx\n", pcb->pcb_fs, pcb->pcb_gs);
 	printf("\n");
-	hexdump("Stack dump", tf, 256);
+	hexdump(printf, "Stack dump", tf, 256);
 }
 
 static void



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

2017-12-08 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Fri Dec  8 21:52:21 UTC 2017

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

Log Message:
make the TRAP_SIGDEBUG code less intrusive.


To generate a diff of this commit:
cvs rdiff -u -r1.107 -r1.108 src/sys/arch/amd64/amd64/trap.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.107 src/sys/arch/amd64/amd64/trap.c:1.108
--- src/sys/arch/amd64/amd64/trap.c:1.107	Thu Dec  7 18:13:17 2017
+++ src/sys/arch/amd64/amd64/trap.c	Fri Dec  8 16:52:21 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.107 2017/12/07 23:13:17 christos Exp $	*/
+/*	$NetBSD: trap.c,v 1.108 2017/12/08 21:52:21 christos Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.107 2017/12/07 23:13:17 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.108 2017/12/08 21:52:21 christos Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -152,7 +152,10 @@ int	trap_types = __arraycount(trap_type)
 #define	IDTVEC(name)	__CONCAT(X, name)
 
 #ifdef TRAP_SIGDEBUG
-static void frame_dump(struct trapframe *, struct pcb *);
+static void sigdebug(const struct trapframe *, const ksiginfo_t *, int);
+#define SIGDEBUG(a, b, c) sigdebug(a, b, c)
+#else
+#define SIGDEBUG(a, b, c)
 #endif
 
 static void
@@ -442,11 +445,6 @@ trap(struct trapframe *frame)
 	case T_SEGNPFLT|T_USER:
 	case T_STKFLT|T_USER:
 	case T_ALIGNFLT|T_USER:
-#ifdef TRAP_SIGDEBUG
-		printf("pid %d.%d (%s): BUS/SEGV (%#x) at rip %#lx addr %#lx\n",
-		p->p_pid, l->l_lid, p->p_comm, type, frame->tf_rip, rcr2());
-		frame_dump(frame, pcb);
-#endif
 		KSI_INIT_TRAP();
 		ksi.ksi_trap = type & ~T_USER;
 		ksi.ksi_addr = (void *)rcr2();
@@ -476,11 +474,6 @@ trap(struct trapframe *frame)
 
 	case T_PRIVINFLT|T_USER:	/* privileged instruction fault */
 	case T_FPOPFLT|T_USER:		/* coprocessor operand fault */
-#ifdef TRAP_SIGDEBUG
-		printf("pid %d.%d (%s): ILL at rip %#lx addr %#lx\n",
-		p->p_pid, l->l_lid, p->p_comm, frame->tf_rip, rcr2());
-		frame_dump(frame, pcb);
-#endif
 		KSI_INIT_TRAP();
 		ksi.ksi_signo = SIGILL;
 		ksi.ksi_trap = type & ~T_USER;
@@ -721,13 +714,7 @@ faultcommon:
 			break;
 		}
 
-#ifdef TRAP_SIGDEBUG
-		printf("pid %d.%d (%s): signal %d at rip %#lx addr %#lx "
-		"error %d trap %d cr2 %p\n", p->p_pid, l->l_lid, p->p_comm,
-		ksi.ksi_signo, frame->tf_rip, va, error, ksi.ksi_trap,
-		ksi.ksi_addr);
-		frame_dump(frame, pcb);
-#endif
+		SIGDEBUG(frame, , error);
  		(*p->p_emul->e_trapsignal)(l, );
 		break;
 	}
@@ -782,6 +769,7 @@ out:
 	userret(l);
 	return;
 trapsignal:
+	SIGDEBUG(frame, , 0);
 	(*p->p_emul->e_trapsignal)(l, );
 	userret(l);
 }
@@ -804,36 +792,42 @@ startlwp(void *arg)
 }
 
 #ifdef TRAP_SIGDEBUG
-void
-frame_dump(struct trapframe *tf, struct pcb *pcb)
+static void
+frame_dump(const struct trapframe *tf, struct pcb *pcb)
 {
-	int i;
-	unsigned long *p;
 
 	printf("trapframe %p\n", tf);
-	printf("rip 0x%016lx  rsp 0x%016lx  rfl 0x%016lx\n",
+	printf("rip %#018lx  rsp %#018lx  rfl %#018lx\n",
 	tf->tf_rip, tf->tf_rsp, tf->tf_rflags);
-	printf("rdi 0x%016lx  rsi 0x%016lx  rdx 0x%016lx\n",
+	printf("rdi %#018lx  rsi %#018lx  rdx %#018lx\n",
 	tf->tf_rdi, tf->tf_rsi, tf->tf_rdx);
-	printf("rcx 0x%016lx  r8  0x%016lx  r9  0x%016lx\n",
+	printf("rcx %#018lx  r8  %#018lx  r9  %#018lx\n",
 	tf->tf_rcx, tf->tf_r8, tf->tf_r9);
-	printf("r10 0x%016lx  r11 0x%016lx  r12 0x%016lx\n",
+	printf("r10 %#018lx  r11 %#018lx  r12 %#018lx\n",
 	tf->tf_r10, tf->tf_r11, tf->tf_r12);
-	printf("r13 0x%016lx  r14 0x%016lx  r15 0x%016lx\n",
+	printf("r13 %#018lx  r14 %#018lx  r15 %#018lx\n",
 	tf->tf_r13, tf->tf_r14, tf->tf_r15);
-	printf("rbp 0x%016lx  rbx 0x%016lx  rax 0x%016lx\n",
+	printf("rbp %#018lx  rbx %#018lx  rax %#018lx\n",
 	tf->tf_rbp, tf->tf_rbx, tf->tf_rax);
-	printf("cs 0x%04lx  ds 0x%04lx  es 0x%04lx  "
-	   "fs 0x%04lx  gs 0x%04lx  ss 0x%04lx\n",
-		tf->tf_cs & 0x, tf->tf_ds & 0x, tf->tf_es & 0x,
-		tf->tf_fs & 0x, tf->tf_gs & 0x, tf->tf_ss & 0x);
-	printf("fsbase 0x%016lx gsbase 0x%016lx\n",
-	   pcb->pcb_fs, pcb->pcb_gs);
-	printf("\n");
-	printf("Stack dump:\n");
-	for (i = 0, p = (unsigned long *) tf; i < 20; i ++, p += 4)
-		printf(" 0x%.16lx  0x%.16lx  0x%.16lx  0x%.16lx\n",
-		   p[0], p[1], p[2], p[3]);
+	printf("cs %#04lx  ds %#04lx  es %#04lx  "
+	"fs %#04lx  gs %#04lx  ss %#04lx\n",
+	tf->tf_cs & 0x, tf->tf_ds & 0x, tf->tf_es & 0x,
+	tf->tf_fs & 0x, tf->tf_gs & 0x, tf->tf_ss & 0x);
+	printf("fsbase %#018lx gsbase %#018lx\n", pcb->pcb_fs, pcb->pcb_gs);
 	printf("\n");
+	hexdump("Stack dump", tf, 256);
+}
+
+static void
+sigdebug(const struct trapframe *tf, const ksiginfo_t *ksi, int e)
+{
+	struct lwp *l 

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

2017-12-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Dec  7 23:13:17 UTC 2017

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

Log Message:
Put back the old syscall glue for netbsd32 binaries, and also TRAP_SIGDEBUG
while I am at it.


To generate a diff of this commit:
cvs rdiff -u -r1.106 -r1.107 src/sys/arch/amd64/amd64/trap.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.106 src/sys/arch/amd64/amd64/trap.c:1.107
--- src/sys/arch/amd64/amd64/trap.c:1.106	Wed Dec  6 22:25:51 2017
+++ src/sys/arch/amd64/amd64/trap.c	Thu Dec  7 18:13:17 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.106 2017/12/07 03:25:51 riastradh Exp $	*/
+/*	$NetBSD: trap.c,v 1.107 2017/12/07 23:13:17 christos Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.106 2017/12/07 03:25:51 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.107 2017/12/07 23:13:17 christos Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -86,6 +86,11 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.1
 
 #include 
 
+#ifdef COMPAT_NETBSD32
+#include 
+#include 
+#endif
+
 #include 
 #include 
 #include 
@@ -146,6 +151,10 @@ int	trap_types = __arraycount(trap_type)
 
 #define	IDTVEC(name)	__CONCAT(X, name)
 
+#ifdef TRAP_SIGDEBUG
+static void frame_dump(struct trapframe *, struct pcb *);
+#endif
+
 static void
 onfault_restore(struct trapframe *frame, void *onfault, int error)
 {
@@ -407,11 +416,37 @@ trap(struct trapframe *frame)
 		trap_user_kernelmode(frame, type, l, p);
 		goto we_re_toast;
 
-	case T_PROTFLT|T_USER:
+	case T_PROTFLT|T_USER:		/* protection fault */
+#if defined(COMPAT_NETBSD32) && defined(COMPAT_10)
+	{
+		static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
+		const size_t sz = sizeof(lcall);
+		char tmp[sz];
+
+		/* Check for the oosyscall lcall instruction. */
+		if (p->p_emul == _netbsd32 &&
+		frame->tf_rip < VM_MAXUSER_ADDRESS32 - sz &&
+		copyin((void *)frame->tf_rip, tmp, sz) == 0 &&
+		memcmp(tmp, lcall, sz) == 0) {
+
+			/* Advance past the lcall. */
+			frame->tf_rip += sz;
+
+			/* Do the syscall. */
+			p->p_md.md_syscall(frame);
+			goto out;
+		}
+	}
+#endif
 	case T_TSSFLT|T_USER:
 	case T_SEGNPFLT|T_USER:
 	case T_STKFLT|T_USER:
 	case T_ALIGNFLT|T_USER:
+#ifdef TRAP_SIGDEBUG
+		printf("pid %d.%d (%s): BUS/SEGV (%#x) at rip %#lx addr %#lx\n",
+		p->p_pid, l->l_lid, p->p_comm, type, frame->tf_rip, rcr2());
+		frame_dump(frame, pcb);
+#endif
 		KSI_INIT_TRAP();
 		ksi.ksi_trap = type & ~T_USER;
 		ksi.ksi_addr = (void *)rcr2();
@@ -439,8 +474,13 @@ trap(struct trapframe *frame)
 		}
 		goto trapsignal;
 
-	case T_PRIVINFLT|T_USER:
-	case T_FPOPFLT|T_USER:
+	case T_PRIVINFLT|T_USER:	/* privileged instruction fault */
+	case T_FPOPFLT|T_USER:		/* coprocessor operand fault */
+#ifdef TRAP_SIGDEBUG
+		printf("pid %d.%d (%s): ILL at rip %#lx addr %#lx\n",
+		p->p_pid, l->l_lid, p->p_comm, frame->tf_rip, rcr2());
+		frame_dump(frame, pcb);
+#endif
 		KSI_INIT_TRAP();
 		ksi.ksi_signo = SIGILL;
 		ksi.ksi_trap = type & ~T_USER;
@@ -681,7 +721,14 @@ faultcommon:
 			break;
 		}
 
-		(*p->p_emul->e_trapsignal)(l, );
+#ifdef TRAP_SIGDEBUG
+		printf("pid %d.%d (%s): signal %d at rip %#lx addr %#lx "
+		"error %d trap %d cr2 %p\n", p->p_pid, l->l_lid, p->p_comm,
+		ksi.ksi_signo, frame->tf_rip, va, error, ksi.ksi_trap,
+		ksi.ksi_addr);
+		frame_dump(frame, pcb);
+#endif
+ 		(*p->p_emul->e_trapsignal)(l, );
 		break;
 	}
 
@@ -707,8 +754,8 @@ faultcommon:
 		}
 		goto we_re_toast;
 
-	case T_BPTFLT|T_USER:
-	case T_TRCTRAP|T_USER:
+	case T_BPTFLT|T_USER:		/* bpt instruction fault */
+	case T_TRCTRAP|T_USER:		/* trace trap */
 		/*
 		 * Don't go single-stepping into a RAS.
 		 */
@@ -756,3 +803,37 @@ startlwp(void *arg)
 	userret(l);
 }
 
+#ifdef TRAP_SIGDEBUG
+void
+frame_dump(struct trapframe *tf, struct pcb *pcb)
+{
+	int i;
+	unsigned long *p;
+
+	printf("trapframe %p\n", tf);
+	printf("rip 0x%016lx  rsp 0x%016lx  rfl 0x%016lx\n",
+	tf->tf_rip, tf->tf_rsp, tf->tf_rflags);
+	printf("rdi 0x%016lx  rsi 0x%016lx  rdx 0x%016lx\n",
+	tf->tf_rdi, tf->tf_rsi, tf->tf_rdx);
+	printf("rcx 0x%016lx  r8  0x%016lx  r9  0x%016lx\n",
+	tf->tf_rcx, tf->tf_r8, tf->tf_r9);
+	printf("r10 0x%016lx  r11 0x%016lx  r12 0x%016lx\n",
+	tf->tf_r10, tf->tf_r11, tf->tf_r12);
+	printf("r13 0x%016lx  r14 0x%016lx  r15 0x%016lx\n",
+	tf->tf_r13, tf->tf_r14, tf->tf_r15);
+	printf("rbp 0x%016lx  rbx 0x%016lx  rax 0x%016lx\n",
+	tf->tf_rbp, tf->tf_rbx, tf->tf_rax);
+	printf("cs 0x%04lx  ds 0x%04lx  es 0x%04lx  "
+	   "fs 0x%04lx  gs 0x%04lx  ss 0x%04lx\n",
+		tf->tf_cs & 0x, tf->tf_ds & 0x, tf->tf_es & 0x,
+		tf->tf_fs & 0x, tf->tf_gs & 0x, tf->tf_ss & 0x);
+	printf("fsbase 0x%016lx 

CVS commit: src/sys/arch/amd64

2017-12-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Dec  7 23:11:50 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: netbsd32_machdep.c
src/sys/arch/amd64/conf: files.amd64
Added Files:
src/sys/arch/amd64/amd64: compat_13_machdep.c

Log Message:
Save maxv@ some work and put back the compat_13_sigreturn changes that allow
amd64 to run ancient i386 binaries.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.3 src/sys/arch/amd64/amd64/compat_13_machdep.c
cvs rdiff -u -r1.114 -r1.115 src/sys/arch/amd64/amd64/netbsd32_machdep.c
cvs rdiff -u -r1.95 -r1.96 src/sys/arch/amd64/conf/files.amd64

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/netbsd32_machdep.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.114 src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.115
--- src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.114	Thu Dec  7 11:22:22 2017
+++ src/sys/arch/amd64/amd64/netbsd32_machdep.c	Thu Dec  7 18:11:50 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.114 2017/12/07 16:22:22 christos Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.115 2017/12/07 23:11:50 christos Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.114 2017/12/07 16:22:22 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.115 2017/12/07 23:11:50 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -1114,3 +1114,67 @@ netbsd32_vm_default_addr(struct proc *p,
 	else
 		return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, sz);
 }
+
+#ifdef COMPAT_13
+int
+compat_13_netbsd32_sigreturn(struct lwp *l, const struct compat_13_netbsd32_sigreturn_args *uap, register_t *retval)
+{
+	/* {
+		syscallarg(struct netbsd32_sigcontext13 *) sigcntxp;
+	} */
+	struct proc *p = l->l_proc;
+	struct netbsd32_sigcontext13 *scp, context;
+	struct trapframe *tf;
+	sigset_t mask;
+	int error;
+
+	/*
+	 * The trampoline code hands us the context.
+	 * It is unsafe to keep track of it ourselves, in the event that a
+	 * program jumps out of a signal handler.
+	 */
+	scp = (struct netbsd32_sigcontext13 *)NETBSD32PTR64(SCARG(uap, sigcntxp));
+	if (copyin((void *)scp, , sizeof(*scp)) != 0)
+		return (EFAULT);
+
+	/* Restore register context. */
+	tf = l->l_md.md_regs;
+
+	/*
+	 * Check for security violations.
+	 */
+	error = check_sigcontext32(l, (const struct netbsd32_sigcontext *));
+	if (error != 0)
+		return error;
+
+	tf->tf_gs = context.sc_gs & 0x;
+	tf->tf_fs = context.sc_fs & 0x;		
+	tf->tf_es = context.sc_es & 0x;
+	tf->tf_ds = context.sc_ds & 0x;
+	tf->tf_rflags = context.sc_eflags;
+	tf->tf_rdi = context.sc_edi;
+	tf->tf_rsi = context.sc_esi;
+	tf->tf_rbp = context.sc_ebp;
+	tf->tf_rbx = context.sc_ebx;
+	tf->tf_rdx = context.sc_edx;
+	tf->tf_rcx = context.sc_ecx;
+	tf->tf_rax = context.sc_eax;
+	tf->tf_rip = context.sc_eip;
+	tf->tf_cs = context.sc_cs & 0x;
+	tf->tf_rsp = context.sc_esp;
+	tf->tf_ss = context.sc_ss & 0x;
+
+	mutex_enter(p->p_lock);
+	/* Restore signal stack. */
+	if (context.sc_onstack & SS_ONSTACK)
+		l->l_sigstk.ss_flags |= SS_ONSTACK;
+	else
+		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
+	/* Restore signal mask. */
+	native_sigset13_to_sigset((sigset13_t *)_mask, );
+	(void) sigprocmask1(l, SIG_SETMASK, , 0);
+	mutex_exit(p->p_lock);
+
+	return (EJUSTRETURN);
+}
+#endif

Index: src/sys/arch/amd64/conf/files.amd64
diff -u src/sys/arch/amd64/conf/files.amd64:1.95 src/sys/arch/amd64/conf/files.amd64:1.96
--- src/sys/arch/amd64/conf/files.amd64:1.95	Sat Dec  2 08:03:15 2017
+++ src/sys/arch/amd64/conf/files.amd64	Thu Dec  7 18:11:50 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: files.amd64,v 1.95 2017/12/02 13:03:15 maxv Exp $
+#	$NetBSD: files.amd64,v 1.96 2017/12/07 23:11:50 christos Exp $
 #
 # new style config file for amd64 architecture
 #
@@ -136,6 +136,7 @@ attach	fd at fdc
 # Compatibility modules
 #
 # Binary compatibility with previous NetBSD releases (COMPAT_XX)
+file	arch/amd64/amd64/compat_13_machdep.c	compat_13
 file	arch/amd64/amd64/compat_16_machdep.c	compat_16
 
 # NetBSD/i386 32-bit binary compatibility (COMPAT_NETBSD32)

Added files:

Index: src/sys/arch/amd64/amd64/compat_13_machdep.c
diff -u /dev/null src/sys/arch/amd64/amd64/compat_13_machdep.c:1.3
--- /dev/null	Thu Dec  7 18:11:50 2017
+++ src/sys/arch/amd64/amd64/compat_13_machdep.c	Thu Dec  7 18:11:50 2017
@@ -0,0 +1,63 @@
+/*	$NetBSD: compat_13_machdep.c,v 1.3 2017/12/07 23:11:50 christos Exp $	*/
+
+/*-
+ * Copyright (c) 2014 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * 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 

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

2017-12-07 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Thu Dec  7 16:22:22 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: netbsd32_machdep.c

Log Message:
Keep fs/gs the same for the signal context; otherwise calling things
like __lwp_getprivate_fast() from a signal handler (that uses %gs) die.

Merge context building code.


To generate a diff of this commit:
cvs rdiff -u -r1.113 -r1.114 src/sys/arch/amd64/amd64/netbsd32_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/netbsd32_machdep.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.113 src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.114
--- src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.113	Sat Dec  2 10:36:24 2017
+++ src/sys/arch/amd64/amd64/netbsd32_machdep.c	Thu Dec  7 11:22:22 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.113 2017/12/02 15:36:24 maxv Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.114 2017/12/07 16:22:22 christos Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.113 2017/12/02 15:36:24 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.114 2017/12/07 16:22:22 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -164,6 +164,43 @@ netbsd32_setregs(struct lwp *l, struct e
 	tf->tf_ss = LSEL(LUDATA32_SEL, SEL_UPL);
 }
 
+static void
+netbsd32_buildcontext(struct lwp *l, struct trapframe *tf, void *fp,
+sig_t catcher, int onstack)
+{
+	/*
+	 * Build context to run handler in.
+	 */
+	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL);
+	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL);
+#if 0
+	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL);
+	tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL);
+#endif
+
+	/* Ensure FP state is sane. */
+	fpu_save_area_reset(l);
+
+	tf->tf_rip = (uint64_t)catcher;
+	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL);
+	tf->tf_rflags &= ~PSL_CLEARSIG;
+	tf->tf_rsp = (uint64_t)fp;
+	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL);
+
+	/* Remember that we're now on the signal stack. */
+	if (onstack)
+		l->l_sigstk.ss_flags |= SS_ONSTACK;
+	if ((vaddr_t)catcher >= VM_MAXUSER_ADDRESS32) {
+		/*
+		 * process has given an invalid address for the
+		 * handler. Stop it, but do not do it before so
+		 * we can return the right info to userland (or in core dump)
+		 */
+		sigexit(l, SIGILL);
+		/* NOTREACHED */
+	}
+}
+
 #ifdef COMPAT_16
 static void
 netbsd32_sendsig_sigcontext(const ksiginfo_t *ksi, const sigset_t *mask)
@@ -249,35 +286,7 @@ netbsd32_sendsig_sigcontext(const ksigin
 		/* NOTREACHED */
 	}
 
-	/*
-	 * Build context to run handler in.
-	 */
-	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL);
-	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL);
-	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL);
-	tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL);
-
-	/* Ensure FP state is sane. */
-	fpu_save_area_reset(l);
-
-	tf->tf_rip = (uint64_t)catcher;
-	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL);
-	tf->tf_rflags &= ~PSL_CLEARSIG;
-	tf->tf_rsp = (uint64_t)fp;
-	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL);
-
-	/* Remember that we're now on the signal stack. */
-	if (onstack)
-		l->l_sigstk.ss_flags |= SS_ONSTACK;
-	if ((vaddr_t)catcher >= VM_MAXUSER_ADDRESS32) {
-		/*
-		 * process has given an invalid address for the
-		 * handler. Stop it, but do not do it before so
-		 * we can return the right info to userland (or in core dump)
-		 */
-		sigexit(l, SIGILL);
-		/* NOTREACHED */
-	}
+	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
 }
 #endif
 
@@ -346,35 +355,7 @@ netbsd32_sendsig_siginfo(const ksiginfo_
 		/* NOTREACHED */
 	}
 
-	/*
-	 * Build context to run handler in.
-	 */
-	tf->tf_ds = GSEL(GUDATA32_SEL, SEL_UPL);
-	tf->tf_es = GSEL(GUDATA32_SEL, SEL_UPL);
-	tf->tf_fs = GSEL(GUDATA32_SEL, SEL_UPL);
-	tf->tf_gs = GSEL(GUDATA32_SEL, SEL_UPL);
-
-	tf->tf_rip = (uint64_t)catcher;
-	tf->tf_cs = GSEL(GUCODE32_SEL, SEL_UPL);
-	tf->tf_rflags &= ~PSL_CLEARSIG;
-	tf->tf_rsp = (uint64_t)fp;
-	tf->tf_ss = GSEL(GUDATA32_SEL, SEL_UPL);
-
-	/* Ensure FP state is sane. */
-	fpu_save_area_reset(l);
-
-	/* Remember that we're now on the signal stack. */
-	if (onstack)
-		l->l_sigstk.ss_flags |= SS_ONSTACK;
-	if ((vaddr_t)catcher >= VM_MAXUSER_ADDRESS32) {
-		/*
-		 * process has given an invalid address for the
-		 * handler. Stop it, but do not do it before so
-		 * we can return the right info to userland (or in core dump)
-		 */
-		sigexit(l, SIGILL);
-		/* NOTREACHED */
-	}
+	netbsd32_buildcontext(l, tf, fp, catcher, onstack);
 }
 
 void



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

2017-12-06 Thread Taylor R Campbell
Module Name:src
Committed By:   riastradh
Date:   Thu Dec  7 03:25:51 UTC 2017

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

Log Message:
Attempt to clarify panic messages for null pointer access/execute.


To generate a diff of this commit:
cvs rdiff -u -r1.105 -r1.106 src/sys/arch/amd64/amd64/trap.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.105 src/sys/arch/amd64/amd64/trap.c:1.106
--- src/sys/arch/amd64/amd64/trap.c:1.105	Sat Dec  2 12:40:03 2017
+++ src/sys/arch/amd64/amd64/trap.c	Thu Dec  7 03:25:51 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.105 2017/12/02 12:40:03 maxv Exp $	*/
+/*	$NetBSD: trap.c,v 1.106 2017/12/07 03:25:51 riastradh Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.105 2017/12/02 12:40:03 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.106 2017/12/07 03:25:51 riastradh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -518,16 +518,26 @@ trap(struct trapframe *frame)
 
 		if (frame->tf_err & PGEX_X) {
 			/* SMEP might have brought us here */
-			if (cr2 < VM_MAXUSER_ADDRESS)
-panic("prevented execution of %p (SMEP)",
-(void *)cr2);
+			if (cr2 < VM_MAXUSER_ADDRESS) {
+if (cr2 == 0)
+	panic("prevented jump to null"
+	" instruction pointer (SMEP)");
+else
+	panic("prevented execution of"
+	" user address %p (SMEP)",
+	(void *)cr2);
+			}
 		}
 
 		if (cr2 < VM_MAXUSER_ADDRESS) {
 			/* SMAP might have brought us here */
-			if (onfault_handler(pcb, frame) == NULL)
-panic("prevented access to %p (SMAP)",
+			if (onfault_handler(pcb, frame) == NULL) {
+panic("prevented %s %p (SMAP)",
+(cr2 < PAGE_SIZE
+	? "null pointer dereference at"
+	: "access to"),
 (void *)cr2);
+			}
 		}
 
 		goto faultcommon;



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

2017-12-05 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Tue Dec  5 21:00:27 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: GENERIC

Log Message:
Add missing wsbell, commented out with a note that it's apparently only
a module by default.

This way people making their own configs have half a prayer of
realizing they might need to turn it on, instead of just not getting
beeps any more for no clear reason.

XXX: Wasn't the agreement after the last round of module flamage that
XXX: things shouldn't be module-only in GENERIC?


To generate a diff of this commit:
cvs rdiff -u -r1.472 -r1.473 src/sys/arch/amd64/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/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.472 src/sys/arch/amd64/conf/GENERIC:1.473
--- src/sys/arch/amd64/conf/GENERIC:1.472	Tue Dec  5 20:32:24 2017
+++ src/sys/arch/amd64/conf/GENERIC	Tue Dec  5 21:00:26 2017
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.472 2017/12/05 20:32:24 dholland Exp $
+# $NetBSD: GENERIC,v 1.473 2017/12/05 21:00:26 dholland Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.472 $"
+#ident		"GENERIC-$Revision: 1.473 $"
 
 maxusers	64		# estimated number of users
 
@@ -1117,6 +1117,7 @@ audio*	at audiobus?
 # The spkr driver provides a simple tone interface to the built in speaker.
 spkr*	at pcppi?		# PC speaker
 spkr*	at audio?		# PC speaker (synthesized)
+#wsbell* at spkr?		# Bell for wscons display (module by default)
 
 # MPU 401 UARTs
 #mpu*	at isa? port 0x330 irq 9	# MPU401 or compatible card



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

2017-12-05 Thread David A. Holland
Module Name:src
Committed By:   dholland
Date:   Tue Dec  5 20:32:24 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: GENERIC

Log Message:
Sort the file system options by which fs they apply to.


To generate a diff of this commit:
cvs rdiff -u -r1.471 -r1.472 src/sys/arch/amd64/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/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.471 src/sys/arch/amd64/conf/GENERIC:1.472
--- src/sys/arch/amd64/conf/GENERIC:1.471	Sat Dec  2 13:03:15 2017
+++ src/sys/arch/amd64/conf/GENERIC	Tue Dec  5 20:32:24 2017
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.471 2017/12/02 13:03:15 maxv Exp $
+# $NetBSD: GENERIC,v 1.472 2017/12/05 20:32:24 dholland Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.471 $"
+#ident		"GENERIC-$Revision: 1.472 $"
 
 maxusers	64		# estimated number of users
 
@@ -164,19 +164,22 @@ file-system	PTYFS		# /dev/ptm support
 #file-system	NILFS		# experimental - NTT's NiLFS(2)
 
 # File system options
+# ffs
 options 	QUOTA		# legacy UFS quotas
 options 	QUOTA2		# new, in-filesystem UFS quotas
-#options 	DISKLABEL_EI	# disklabel Endian Independent support
 options 	FFS_EI		# FFS Endian Independent support
 options 	WAPBL		# File system journaling support
 # Note that UFS_DIRHASH is suspected of causing kernel memory corruption.
 # It is not recommended for general use.
 #options 	UFS_DIRHASH	# UFS Large Directory Hashing - Experimental
-options 	NFSSERVER	# Network File System server
-#options 	EXT2FS_SYSTEM_FLAGS # makes ext2fs file flags (append and
-# immutable) behave as system flags.
 #options 	FFS_NO_SNAPSHOT	# No FFS snapshot support
 options 	UFS_EXTATTR	# Extended attribute support for UFS1
+# ext2fs
+#options 	EXT2FS_SYSTEM_FLAGS # makes ext2fs file flags (append and
+# immutable) behave as system flags.
+# other
+#options 	DISKLABEL_EI	# disklabel Endian Independent support
+options 	NFSSERVER	# Network File System server
 
 # Networking options
 #options 	GATEWAY		# packet forwarding



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

2017-12-02 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec  2 15:36:24 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: netbsd32_machdep.c

Log Message:
Remove a piece of COMPAT_13, that I mistakenly didn't commit three
hours ago (in my change to drop COMPAT_13 on amd64).


To generate a diff of this commit:
cvs rdiff -u -r1.112 -r1.113 src/sys/arch/amd64/amd64/netbsd32_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/netbsd32_machdep.c
diff -u src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.112 src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.113
--- src/sys/arch/amd64/amd64/netbsd32_machdep.c:1.112	Thu Oct 19 10:01:09 2017
+++ src/sys/arch/amd64/amd64/netbsd32_machdep.c	Sat Dec  2 15:36:24 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_machdep.c,v 1.112 2017/10/19 10:01:09 maxv Exp $	*/
+/*	$NetBSD: netbsd32_machdep.c,v 1.113 2017/12/02 15:36:24 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.112 2017/10/19 10:01:09 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.113 2017/12/02 15:36:24 maxv Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_compat_netbsd.h"
@@ -1133,67 +1133,3 @@ netbsd32_vm_default_addr(struct proc *p,
 	else
 		return VM_DEFAULT_ADDRESS32_BOTTOMUP(base, sz);
 }
-
-#ifdef COMPAT_13
-int
-compat_13_netbsd32_sigreturn(struct lwp *l, const struct compat_13_netbsd32_sigreturn_args *uap, register_t *retval)
-{
-	/* {
-		syscallarg(struct netbsd32_sigcontext13 *) sigcntxp;
-	} */
-	struct proc *p = l->l_proc;
-	struct netbsd32_sigcontext13 *scp, context;
-	struct trapframe *tf;
-	sigset_t mask;
-	int error;
-
-	/*
-	 * The trampoline code hands us the context.
-	 * It is unsafe to keep track of it ourselves, in the event that a
-	 * program jumps out of a signal handler.
-	 */
-	scp = (struct netbsd32_sigcontext13 *)NETBSD32PTR64(SCARG(uap, sigcntxp));
-	if (copyin((void *)scp, , sizeof(*scp)) != 0)
-		return (EFAULT);
-
-	/* Restore register context. */
-	tf = l->l_md.md_regs;
-
-	/*
-	 * Check for security violations.
-	 */
-	error = check_sigcontext32(l, (const struct netbsd32_sigcontext *));
-	if (error != 0)
-		return error;
-
-	tf->tf_gs = context.sc_gs & 0x;
-	tf->tf_fs = context.sc_fs & 0x;		
-	tf->tf_es = context.sc_es & 0x;
-	tf->tf_ds = context.sc_ds & 0x;
-	tf->tf_rflags = context.sc_eflags;
-	tf->tf_rdi = context.sc_edi;
-	tf->tf_rsi = context.sc_esi;
-	tf->tf_rbp = context.sc_ebp;
-	tf->tf_rbx = context.sc_ebx;
-	tf->tf_rdx = context.sc_edx;
-	tf->tf_rcx = context.sc_ecx;
-	tf->tf_rax = context.sc_eax;
-	tf->tf_rip = context.sc_eip;
-	tf->tf_cs = context.sc_cs & 0x;
-	tf->tf_rsp = context.sc_esp;
-	tf->tf_ss = context.sc_ss & 0x;
-
-	mutex_enter(p->p_lock);
-	/* Restore signal stack. */
-	if (context.sc_onstack & SS_ONSTACK)
-		l->l_sigstk.ss_flags |= SS_ONSTACK;
-	else
-		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
-	/* Restore signal mask. */
-	native_sigset13_to_sigset((sigset13_t *)_mask, );
-	(void) sigprocmask1(l, SIG_SETMASK, , 0);
-	mutex_exit(p->p_lock);
-
-	return (EJUSTRETURN);
-}
-#endif



CVS commit: src/sys/arch/amd64

2017-12-02 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec  2 12:40:03 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: trap.c
src/sys/arch/amd64/conf: ALL GENERIC

Log Message:
Drop COMPAT_10 on amd64. The support for it comes down to one ifdef in
trap.c - code that is incorrect anyway, there were originally three lcall
LDT slots, and here only one instruction is decoded.

Given that one of these slots was used by BSDi's syscall, also remove the
references to COMPAT_NOMID to make clear we don't support that (it already
is not enabled).

Note: for some reason, COMPAT_10 does not even compile, because there are
"multiple definitions of _KERNEL_OPT_COMPAT_...", and I don't really
understand where this comes from.


To generate a diff of this commit:
cvs rdiff -u -r1.104 -r1.105 src/sys/arch/amd64/amd64/trap.c
cvs rdiff -u -r1.73 -r1.74 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.469 -r1.470 src/sys/arch/amd64/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/amd64/amd64/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.104 src/sys/arch/amd64/amd64/trap.c:1.105
--- src/sys/arch/amd64/amd64/trap.c:1.104	Sat Oct 21 08:08:26 2017
+++ src/sys/arch/amd64/amd64/trap.c	Sat Dec  2 12:40:03 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.104 2017/10/21 08:08:26 maxv Exp $	*/
+/*	$NetBSD: trap.c,v 1.105 2017/12/02 12:40:03 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,14 +64,12 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.104 2017/10/21 08:08:26 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.105 2017/12/02 12:40:03 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
 #include "opt_xen.h"
 #include "opt_dtrace.h"
-#include "opt_compat_netbsd.h"
-#include "opt_compat_netbsd32.h"
 
 #include 
 #include 
@@ -88,11 +86,6 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.1
 
 #include 
 
-#ifdef COMPAT_NETBSD32
-#include 
-#include 
-#endif
-
 #include 
 #include 
 #include 
@@ -414,28 +407,7 @@ trap(struct trapframe *frame)
 		trap_user_kernelmode(frame, type, l, p);
 		goto we_re_toast;
 
-	case T_PROTFLT|T_USER:		/* protection fault */
-#if defined(COMPAT_NETBSD32) && defined(COMPAT_10)
-	{
-		static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
-		const size_t sz = sizeof(lcall);
-		char tmp[sz];
-
-		/* Check for the oosyscall lcall instruction. */
-		if (p->p_emul == _netbsd32 &&
-		frame->tf_rip < VM_MAXUSER_ADDRESS32 - sz &&
-		copyin((void *)frame->tf_rip, tmp, sz) == 0 &&
-		memcmp(tmp, lcall, sz) == 0) {
-
-			/* Advance past the lcall. */
-			frame->tf_rip += sz;
-
-			/* Do the syscall. */
-			p->p_md.md_syscall(frame);
-			goto out;
-		}
-	}
-#endif
+	case T_PROTFLT|T_USER:
 	case T_TSSFLT|T_USER:
 	case T_SEGNPFLT|T_USER:
 	case T_STKFLT|T_USER:

Index: src/sys/arch/amd64/conf/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.73 src/sys/arch/amd64/conf/ALL:1.74
--- src/sys/arch/amd64/conf/ALL:1.73	Sat Dec  2 09:59:02 2017
+++ src/sys/arch/amd64/conf/ALL	Sat Dec  2 12:40:03 2017
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.73 2017/12/02 09:59:02 maxv Exp $
+# $NetBSD: ALL,v 1.74 2017/12/02 12:40:03 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.73 $"
+#ident		"ALL-$Revision: 1.74 $"
 
 maxusers	64		# estimated number of users
 
@@ -130,7 +130,6 @@ options 	UVMHIST		# kernhist for uvm sub
 options 	BIOHIST		# kernhist for buff I/O
 
 # Compatibility options
-options 	COMPAT_NOMID	# NetBSD 0.8, 386BSD, and BSDI
 options 	EXEC_AOUT	# required by binaries from before 1.5
 options 	COMPAT_386BSD_MBRPART # recognize old partition ID
 include 	"conf/compat_netbsd09.config"

Index: src/sys/arch/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.469 src/sys/arch/amd64/conf/GENERIC:1.470
--- src/sys/arch/amd64/conf/GENERIC:1.469	Sat Nov 25 16:32:48 2017
+++ src/sys/arch/amd64/conf/GENERIC	Sat Dec  2 12:40:03 2017
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.469 2017/11/25 16:32:48 jmcneill Exp $
+# $NetBSD: GENERIC,v 1.470 2017/12/02 12:40:03 maxv Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.469 $"
+#ident		"GENERIC-$Revision: 1.470 $"
 
 maxusers	64		# estimated number of users
 
@@ -115,11 +115,13 @@ makeoptions	DEBUG="-g"	# compile full sy
 options 	KDTRACE_HOOKS	# kernel DTrace hooks
 
 # Compatibility options
-# (note that really old compat (< 1.6) is only useful for 32-bit binaries)
 #options 	EXEC_AOUT	# required by binaries from before 1.5
-#options 	COMPAT_NOMID	# NetBSD 0.8, 386BSD, and BSDI
 
+# NetBSD backward compatibility. Support goes 

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

2017-12-02 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Dec  2 09:59:02 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: ALL

Log Message:
Remove options that do not exist on amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.72 -r1.73 src/sys/arch/amd64/conf/ALL

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/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.72 src/sys/arch/amd64/conf/ALL:1.73
--- src/sys/arch/amd64/conf/ALL:1.72	Sat Nov 25 16:32:48 2017
+++ src/sys/arch/amd64/conf/ALL	Sat Dec  2 09:59:02 2017
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.72 2017/11/25 16:32:48 jmcneill Exp $
+# $NetBSD: ALL,v 1.73 2017/12/02 09:59:02 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.72 $"
+#ident		"ALL-$Revision: 1.73 $"
 
 maxusers	64		# estimated number of users
 
@@ -139,13 +139,7 @@ options 	COMPAT_OSSAUDIO	# OSS (Voxware)
 options 	COMPAT_NETBSD32 # NetBSD 32-bit
 options 	COMPAT_LINUX	# binary compatibility with Linux
 options 	COMPAT_LINUX32	# binary compatibility with Linux 32-bit
-options 	COMPAT_FREEBSD	# binary compatibility with FreeBSD
 options 	COMPAT_NDIS	# NDIS network driver
-options 	COMPAT_OSF1	# OSF1 binary compatibility
-#options 	COMPAT_SVR4	# SVR4 binary compatibility (no amd64)
-#options 	COMPAT_SVR4_32	# SVR4 32-bit binary compatibility (no amd64)
-options 	COMPAT_ULTRIX	# DEC Ultrix binary compatibility
-options 	COMPAT_SUNOS	# SunOS 4.x binary compatibility
 
 # Wedge support
 options 	DKWEDGE_AUTODISCOVER	# Automatically add dk(4) instances



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

2017-12-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Dec  1 21:22:45 UTC 2017

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

Log Message:
Don't even bother with the trap frame, and force the default values.


To generate a diff of this commit:
cvs rdiff -u -r1.278 -r1.279 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.37 -r1.38 src/sys/arch/amd64/amd64/process_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.278 src/sys/arch/amd64/amd64/machdep.c:1.279
--- src/sys/arch/amd64/amd64/machdep.c:1.278	Mon Nov 27 09:18:01 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Fri Dec  1 21:22:45 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.278 2017/11/27 09:18:01 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.279 2017/12/01 21:22:45 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.278 2017/11/27 09:18:01 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.279 2017/12/01 21:22:45 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1892,17 +1892,17 @@ cpu_getmcontext(struct lwp *l, mcontext_
 	mcp->__gregs[_REG_RBP] = tf->tf_rbp;
 	mcp->__gregs[_REG_RBX] = tf->tf_rbx;
 	mcp->__gregs[_REG_RAX] = tf->tf_rax;
-	mcp->__gregs[_REG_GS]  = tf->tf_gs & 0x;
-	mcp->__gregs[_REG_FS]  = tf->tf_fs & 0x;
-	mcp->__gregs[_REG_ES]  = tf->tf_es & 0x;
-	mcp->__gregs[_REG_DS]  = tf->tf_ds & 0x;
+	mcp->__gregs[_REG_GS]  = 0;
+	mcp->__gregs[_REG_FS]  = 0;
+	mcp->__gregs[_REG_ES]  = GSEL(GUDATA_SEL, SEL_UPL);
+	mcp->__gregs[_REG_DS]  = GSEL(GUDATA_SEL, SEL_UPL);
 	mcp->__gregs[_REG_TRAPNO] = tf->tf_trapno;
 	mcp->__gregs[_REG_ERR] = tf->tf_err;
 	mcp->__gregs[_REG_RIP] = tf->tf_rip;
-	mcp->__gregs[_REG_CS]  = tf->tf_cs & 0x;
+	mcp->__gregs[_REG_CS]  = LSEL(LUCODE_SEL, SEL_UPL);
 	mcp->__gregs[_REG_RFLAGS] = tf->tf_rflags;
 	mcp->__gregs[_REG_RSP] = tf->tf_rsp;
-	mcp->__gregs[_REG_SS]  = tf->tf_ss & 0x;
+	mcp->__gregs[_REG_SS]  = LSEL(LUDATA_SEL, SEL_UPL);
 
 	if ((ras_rip = (__greg_t)ras_lookup(l->l_proc,
 	(void *) mcp->__gregs[_REG_RIP])) != -1)
@@ -1949,18 +1949,18 @@ cpu_setmcontext(struct lwp *l, const mco
 		tf->tf_rbp  = gr[_REG_RBP];
 		tf->tf_rbx  = gr[_REG_RBX];
 		tf->tf_rax  = gr[_REG_RAX];
-		tf->tf_gs   = gr[_REG_GS] & 0x;
-		tf->tf_fs   = gr[_REG_FS] & 0x;
-		tf->tf_es   = gr[_REG_ES] & 0x;
-		tf->tf_ds   = gr[_REG_DS] & 0x;
+		tf->tf_gs   = 0;
+		tf->tf_fs   = 0;
+		tf->tf_es   = GSEL(GUDATA_SEL, SEL_UPL);
+		tf->tf_ds   = GSEL(GUDATA_SEL, SEL_UPL);
 		/* trapno, err not touched */
 		tf->tf_rip  = gr[_REG_RIP];
-		tf->tf_cs   = gr[_REG_CS] & 0x;
+		tf->tf_cs   = LSEL(LUCODE_SEL, SEL_UPL);
 		rflags = tf->tf_rflags;
 		rflags &= ~PSL_USER;
 		tf->tf_rflags = rflags | (gr[_REG_RFLAGS] & PSL_USER);
 		tf->tf_rsp  = gr[_REG_RSP];
-		tf->tf_ss   = gr[_REG_SS] & 0x;
+		tf->tf_ss   = LSEL(LUDATA_SEL, SEL_UPL);
 
 #ifdef XEN
 		/*

Index: src/sys/arch/amd64/amd64/process_machdep.c
diff -u src/sys/arch/amd64/amd64/process_machdep.c:1.37 src/sys/arch/amd64/amd64/process_machdep.c:1.38
--- src/sys/arch/amd64/amd64/process_machdep.c:1.37	Mon Nov 27 09:18:01 2017
+++ src/sys/arch/amd64/amd64/process_machdep.c	Fri Dec  1 21:22:45 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: process_machdep.c,v 1.37 2017/11/27 09:18:01 maxv Exp $	*/
+/*	$NetBSD: process_machdep.c,v 1.38 2017/12/01 21:22:45 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@@ -74,7 +74,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.37 2017/11/27 09:18:01 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.38 2017/12/01 21:22:45 maxv Exp $");
 
 #include "opt_xen.h"
 #include 
@@ -125,17 +125,17 @@ process_read_regs(struct lwp *l, struct 
 	regs->regs[_REG_RBP] = tf->tf_rbp;
 	regs->regs[_REG_RBX] = tf->tf_rbx;
 	regs->regs[_REG_RAX] = tf->tf_rax;
-	regs->regs[_REG_GS]  = tf->tf_gs & 0x;
-	regs->regs[_REG_FS]  = tf->tf_fs & 0x;
-	regs->regs[_REG_ES]  = tf->tf_es & 0x;
-	regs->regs[_REG_DS]  = tf->tf_ds & 0x;
+	regs->regs[_REG_GS]  = 0;
+	regs->regs[_REG_FS]  = 0;
+	regs->regs[_REG_ES]  = GSEL(GUDATA_SEL, SEL_UPL);
+	regs->regs[_REG_DS]  = GSEL(GUDATA_SEL, SEL_UPL);
 	regs->regs[_REG_TRAPNO] = tf->tf_trapno;
 	regs->regs[_REG_ERR] = tf->tf_err;
 	regs->regs[_REG_RIP] = tf->tf_rip;
-	regs->regs[_REG_CS]  = tf->tf_cs & 0x;
+	regs->regs[_REG_CS]  = LSEL(LUCODE_SEL, SEL_UPL);
 	regs->regs[_REG_RFLAGS] = tf->tf_rflags;
 	regs->regs[_REG_RSP] = tf->tf_rsp;
-	regs->regs[_REG_SS]  = tf->tf_ss & 0x;
+	regs->regs[_REG_SS]  = LSEL(LUDATA_SEL, SEL_UPL);
 
 	return 0;
 }
@@ -204,16 +204,16 @@ process_write_regs(struct lwp *l, const 
 	tf->tf_rbp  = regs[_REG_RBP];
 	tf->tf_rbx  = regs[_REG_RBX];
 	tf->tf_rax  = regs[_REG_RAX];
-	

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

2017-11-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 28 08:43:49 UTC 2017

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

Log Message:
style


To generate a diff of this commit:
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/amd64/amd64/spl.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/spl.S
diff -u src/sys/arch/amd64/amd64/spl.S:1.30 src/sys/arch/amd64/amd64/spl.S:1.31
--- src/sys/arch/amd64/amd64/spl.S:1.30	Sun Nov 22 13:41:24 2015
+++ src/sys/arch/amd64/amd64/spl.S	Tue Nov 28 08:43:49 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: spl.S,v 1.30 2015/11/22 13:41:24 maxv Exp $	*/
+/*	$NetBSD: spl.S,v 1.31 2017/11/28 08:43:49 maxv Exp $	*/
 
 /*
  * Copyright (c) 2003 Wasabi Systems, Inc.
@@ -90,28 +90,39 @@
  *	%r13		address to return to
  */
 IDTVEC(softintr)
-	pushq	$_C_LABEL(softintr_ret)	/* set up struct switchframe */
+	/* set up struct switchframe */
+	pushq	$_C_LABEL(softintr_ret)
 	pushq	%rbx
 	pushq	%r12
 	pushq	%r13
 	pushq	%r14
 	pushq	%r15
+
 	movl	$IPL_HIGH,CPUVAR(ILEVEL)
 	movq	CPUVAR(CURLWP),%r15
 	movq	IS_LWP(%rax),%rdi	/* switch to handler LWP */
 	movq	L_PCB(%rdi),%rdx
 	movq	L_PCB(%r15),%rcx
 	movq	%rdi,CPUVAR(CURLWP)
+
+	/* save old context */
 	movq	%rsp,PCB_RSP(%rcx)
 	movq	%rbp,PCB_RBP(%rcx)
-	movq	PCB_RSP0(%rdx),%rsp	/* onto new stack */
+
+	/* switch to the new stack */
+	movq	PCB_RSP0(%rdx),%rsp
+
+	/* dispatch */
 	sti
 	movq	%r15,%rdi		/* interrupted LWP */
 	movl	IS_MAXLEVEL(%rax),%esi	/* ipl to run at */
 	call	_C_LABEL(softint_dispatch)/* run handlers */
 	cli
+
+	/* restore old context */
 	movq	L_PCB(%r15),%rcx
 	movq	PCB_RSP(%rcx),%rsp
+
 	xchgq	%r15,CPUVAR(CURLWP)	/* must be globally visible */
 	popq	%r15			/* unwind switchframe */
 	addq	$(5 * 8),%rsp
@@ -128,7 +139,7 @@ END(Xsoftintr)
  */
 NENTRY(softintr_ret)
 	incl	CPUVAR(MTX_COUNT)	/* re-adjust after mi_switch */
-	movl	$0, L_CTXSWTCH(%rax)	/* %rax from cpu_switchto */
+	movl	$0,L_CTXSWTCH(%rax)	/* %rax from cpu_switchto */
 	cli
 	jmp	*%r13			/* back to Xspllower/Xdoreti */
 END(softintr_ret)
@@ -150,9 +161,9 @@ END(softint_trigger)
  * Handles preemption interrupts via Xspllower().
  */
 IDTVEC(preemptrecurse)
-	movl	$IPL_PREEMPT, CPUVAR(ILEVEL)
+	movl	$IPL_PREEMPT,CPUVAR(ILEVEL)
 	sti
-	xorq	%rdi, %rdi
+	xorq	%rdi,%rdi
 	call	_C_LABEL(kpreempt)
 	cli
 	jmp	*%r13			/* back to Xspllower */
@@ -164,16 +175,16 @@ END(Xpreemptrecurse)
  * Handles preemption interrupts via Xdoreti().
  */
 IDTVEC(preemptresume)
-	movl	$IPL_PREEMPT, CPUVAR(ILEVEL)
+	movl	$IPL_PREEMPT,CPUVAR(ILEVEL)
 	sti
-	testq	$SEL_RPL, TF_CS(%rsp)
+	testq	$SEL_RPL,TF_CS(%rsp)
 	jnz	1f
-	movq	TF_RIP(%rsp), %rdi
-	call	_C_LABEL(kpreempt)		# from kernel
+	movq	TF_RIP(%rsp),%rdi
+	call	_C_LABEL(kpreempt)	/* from kernel */
 	cli
 	jmp	*%r13			/* back to Xdoreti */
 1:
-	call	_C_LABEL(preempt)		# from user
+	call	_C_LABEL(preempt)	/* from user */
 	cli
 	jmp	*%r13			/* back to Xdoreti */
 END(Xpreemptresume)
@@ -197,14 +208,14 @@ END(splraise)
  * are disabled via eflags/IE.
  */
 ENTRY(spllower)
-	cmpl	CPUVAR(ILEVEL), %edi
+	cmpl	CPUVAR(ILEVEL),%edi
 	jae	1f
-	movl	CPUVAR(IUNMASK)(,%rdi,4), %edx
+	movl	CPUVAR(IUNMASK)(,%rdi,4),%edx
 	pushf
 	cli
-	testl	CPUVAR(IPENDING), %edx
+	testl	CPUVAR(IPENDING),%edx
 	jnz	2f
-	movl	%edi, CPUVAR(ILEVEL)
+	movl	%edi,CPUVAR(ILEVEL)
 	popf
 1:
 	ret
@@ -224,7 +235,7 @@ LABEL(spllower_end)
  *
  * For cmpxchg8b, edx/ecx are the high words and eax/ebx the low.
  *
- * edx : eax = old level / old ipending 
+ * edx : eax = old level / old ipending
  * ecx : ebx = new level / old ipending
  */
 ENTRY(cx8_spllower)
@@ -260,7 +271,7 @@ LABEL(cx8_spllower_end)
 
 /*
  * void Xspllower(int s);
- * 
+ *
  * Process pending interrupts.
  *
  * Important registers:
@@ -283,11 +294,11 @@ IDTVEC(spllower)
 	pushq	%r13
 	pushq	%r12
 	movl	%edi,%ebx
-	leaq	1f(%rip),%r13		# address to resume loop at
-1:	movl	%ebx,%eax		# get cpl
+	leaq	1f(%rip),%r13		/* address to resume loop at */
+1:	movl	%ebx,%eax		/* get cpl */
 	movl	CPUVAR(IUNMASK)(,%rax,4),%eax
 	CLI(si)
-	andl	CPUVAR(IPENDING),%eax		# any non-masked bits left?
+	andl	CPUVAR(IPENDING),%eax	/* any non-masked bits left? */
 	jz	2f
 	bsrl	%eax,%eax
 	btrl	%eax,CPUVAR(IPENDING)
@@ -304,7 +315,7 @@ END(Xspllower)
 
 /*
  * void Xdoreti(void);
- * 
+ *
  * Handle return from interrupt after device handler finishes.
  *
  * Important registers:
@@ -312,7 +323,7 @@ END(Xspllower)
  *   r13 - address to resume loop at
  */
 IDTVEC(doreti)
-	popq	%rbx			# get previous priority
+	popq	%rbx			/* get previous priority */
 	decl	CPUVAR(IDEPTH)
 	leaq	1f(%rip),%r13
 1:	movl	%ebx,%eax
@@ -320,9 +331,9 @@ IDTVEC(doreti)
 	CLI(si)
 	andl	CPUVAR(IPENDING),%eax
 	jz	2f
-	bsrl	%eax,%eax		# slow, but not worth optimizing
+	bsrl	%eax,%eax		/* slow, but not worth optimizing */
 	btrl	%eax,CPUVAR(IPENDING)
-	movq	CPUVAR(ISOURCES)(,%rax, 8),%rax
+	movq	CPUVAR(ISOURCES)(,%rax,8),%rax
 	

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

2017-11-27 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 27 09:18:01 UTC 2017

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

Log Message:
Inline _FRAME_GREG, and mask only 16 bits of the segment registers,
otherwise the upper 48 bits may contain stack garbage. By the way, I find
it suspicious that we're not masking regs[_REG_RFLAGS] with PSL_USER in
process_write_regs.


To generate a diff of this commit:
cvs rdiff -u -r1.277 -r1.278 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.36 -r1.37 src/sys/arch/amd64/amd64/process_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.277 src/sys/arch/amd64/amd64/machdep.c:1.278
--- src/sys/arch/amd64/amd64/machdep.c:1.277	Tue Nov 21 10:42:44 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Mon Nov 27 09:18:01 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.277 2017/11/21 10:42:44 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.278 2017/11/27 09:18:01 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.277 2017/11/21 10:42:44 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.278 2017/11/27 09:18:01 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1876,10 +1876,33 @@ cpu_getmcontext(struct lwp *l, mcontext_
 	const struct trapframe *tf = l->l_md.md_regs;
 	__greg_t ras_rip;
 
-	/* Copy general registers member by member */
-#define copy_from_tf(reg, REG, idx) mcp->__gregs[_REG_##REG] = tf->tf_##reg;
-	_FRAME_GREG(copy_from_tf)
-#undef copy_from_tf
+	mcp->__gregs[_REG_RDI] = tf->tf_rdi;
+	mcp->__gregs[_REG_RSI] = tf->tf_rsi;
+	mcp->__gregs[_REG_RDX] = tf->tf_rdx;
+	mcp->__gregs[_REG_R10] = tf->tf_r10;
+	mcp->__gregs[_REG_R8]  = tf->tf_r8;
+	mcp->__gregs[_REG_R9]  = tf->tf_r9;
+	/* argX not touched */
+	mcp->__gregs[_REG_RCX] = tf->tf_rcx;
+	mcp->__gregs[_REG_R11] = tf->tf_r11;
+	mcp->__gregs[_REG_R12] = tf->tf_r12;
+	mcp->__gregs[_REG_R13] = tf->tf_r13;
+	mcp->__gregs[_REG_R14] = tf->tf_r14;
+	mcp->__gregs[_REG_R15] = tf->tf_r15;
+	mcp->__gregs[_REG_RBP] = tf->tf_rbp;
+	mcp->__gregs[_REG_RBX] = tf->tf_rbx;
+	mcp->__gregs[_REG_RAX] = tf->tf_rax;
+	mcp->__gregs[_REG_GS]  = tf->tf_gs & 0x;
+	mcp->__gregs[_REG_FS]  = tf->tf_fs & 0x;
+	mcp->__gregs[_REG_ES]  = tf->tf_es & 0x;
+	mcp->__gregs[_REG_DS]  = tf->tf_ds & 0x;
+	mcp->__gregs[_REG_TRAPNO] = tf->tf_trapno;
+	mcp->__gregs[_REG_ERR] = tf->tf_err;
+	mcp->__gregs[_REG_RIP] = tf->tf_rip;
+	mcp->__gregs[_REG_CS]  = tf->tf_cs & 0x;
+	mcp->__gregs[_REG_RFLAGS] = tf->tf_rflags;
+	mcp->__gregs[_REG_RSP] = tf->tf_rsp;
+	mcp->__gregs[_REG_SS]  = tf->tf_ss & 0x;
 
 	if ((ras_rip = (__greg_t)ras_lookup(l->l_proc,
 	(void *) mcp->__gregs[_REG_RIP])) != -1)
@@ -1901,7 +1924,6 @@ cpu_setmcontext(struct lwp *l, const mco
 	const __greg_t *gr = mcp->__gregs;
 	struct proc *p = l->l_proc;
 	int error;
-	int err, trapno;
 	int64_t rflags;
 
 	CTASSERT(sizeof (mcontext_t) == 26 * 8 + 8 + 512);
@@ -1910,33 +1932,44 @@ cpu_setmcontext(struct lwp *l, const mco
 		error = cpu_mcontext_validate(l, mcp);
 		if (error != 0)
 			return error;
-		/*
-		 * save and restore some values we don't want to change.
-		 * _FRAME_GREG(copy_to_tf) below overwrites them.
-		 *
-		 * XXX maybe inline this.
-		 */
-		rflags = tf->tf_rflags;
-		err = tf->tf_err;
-		trapno = tf->tf_trapno;
 
-		/* Copy general registers member by member */
-#define copy_to_tf(reg, REG, idx) tf->tf_##reg = gr[_REG_##REG];
-		_FRAME_GREG(copy_to_tf)
-#undef copy_to_tf
+		tf->tf_rdi  = gr[_REG_RDI];
+		tf->tf_rsi  = gr[_REG_RSI];
+		tf->tf_rdx  = gr[_REG_RDX];
+		tf->tf_r10  = gr[_REG_R10];
+		tf->tf_r8   = gr[_REG_R8];
+		tf->tf_r9   = gr[_REG_R9];
+		/* argX not touched */
+		tf->tf_rcx  = gr[_REG_RCX];
+		tf->tf_r11  = gr[_REG_R11];
+		tf->tf_r12  = gr[_REG_R12];
+		tf->tf_r13  = gr[_REG_R13];
+		tf->tf_r14  = gr[_REG_R14];
+		tf->tf_r15  = gr[_REG_R15];
+		tf->tf_rbp  = gr[_REG_RBP];
+		tf->tf_rbx  = gr[_REG_RBX];
+		tf->tf_rax  = gr[_REG_RAX];
+		tf->tf_gs   = gr[_REG_GS] & 0x;
+		tf->tf_fs   = gr[_REG_FS] & 0x;
+		tf->tf_es   = gr[_REG_ES] & 0x;
+		tf->tf_ds   = gr[_REG_DS] & 0x;
+		/* trapno, err not touched */
+		tf->tf_rip  = gr[_REG_RIP];
+		tf->tf_cs   = gr[_REG_CS] & 0x;
+		rflags = tf->tf_rflags;
+		rflags &= ~PSL_USER;
+		tf->tf_rflags = rflags | (gr[_REG_RFLAGS] & PSL_USER);
+		tf->tf_rsp  = gr[_REG_RSP];
+		tf->tf_ss   = gr[_REG_SS] & 0x;
 
 #ifdef XEN
 		/*
 		 * Xen has its own way of dealing with %cs and %ss,
-		 * reset it to proper values.
+		 * reset them to proper values.
 		 */
 		tf->tf_ss = GSEL(GUDATA_SEL, SEL_UPL);
 		tf->tf_cs = GSEL(GUCODE_SEL, SEL_UPL);
 #endif
-		rflags &= ~PSL_USER;
-		tf->tf_rflags = rflags | (gr[_REG_RFLAGS] & PSL_USER);
-		tf->tf_err = err;
-		

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

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 15:00:16 UTC 2017

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

Log Message:
Update a comment, and use testw instead.


To generate a diff of this commit:
cvs rdiff -u -r1.142 -r1.143 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.142 src/sys/arch/amd64/amd64/locore.S:1.143
--- src/sys/arch/amd64/amd64/locore.S:1.142	Sun Nov 26 14:54:43 2017
+++ src/sys/arch/amd64/amd64/locore.S	Sun Nov 26 15:00:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.142 2017/11/26 14:54:43 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.143 2017/11/26 15:00:16 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1197,7 +1197,7 @@ ENTRY(cpu_switchto)
 	movq	PCB_GS(%r14),%rax
 	movq	%rax,(GUGS_SEL*8)(%rcx)
 
-	/* Set default 32bit values in %ds, %es and %fs. %gs is special. */
+	/* Set default 32bit values in %ds, %es, %fs and %gs. */
 	movq	L_MD_REGS(%r12),%rbx
 	movq	$GSEL(GUDATA32_SEL, SEL_UPL),%rax
 	movw	%ax,%ds
@@ -1474,7 +1474,7 @@ END(pagezero)
 
 ENTRY(intrfastexit)
 	INTR_RESTORE_GPRS
-	testq	$SEL_UPL,TF_CS(%rsp)	/* interrupted %cs */
+	testw	$SEL_UPL,TF_CS(%rsp)	/* interrupted %cs */
 	jz	.Lkexit
 	cmpw	$LSEL(LUCODE_SEL, SEL_UPL),TF_CS(%rsp)
 	je	.Luexit64



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

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 14:54:43 UTC 2017

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

Log Message:
Hide a bunch of raw symbols.


To generate a diff of this commit:
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/amd64/amd64/amd64_trap.S
cvs rdiff -u -r1.141 -r1.142 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.25 -r1.26 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/amd64_trap.S
diff -u src/sys/arch/amd64/amd64/amd64_trap.S:1.12 src/sys/arch/amd64/amd64/amd64_trap.S:1.13
--- src/sys/arch/amd64/amd64/amd64_trap.S:1.12	Tue Oct 17 07:33:44 2017
+++ src/sys/arch/amd64/amd64/amd64_trap.S	Sun Nov 26 14:54:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: amd64_trap.S,v 1.12 2017/10/17 07:33:44 maxv Exp $	*/
+/*	$NetBSD: amd64_trap.S,v 1.13 2017/11/26 14:54:43 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2008, 2017 The NetBSD Foundation, Inc.
@@ -66,7 +66,7 @@
 
 #if 0
 #include 
-__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.12 2017/10/17 07:33:44 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amd64_trap.S,v 1.13 2017/11/26 14:54:43 maxv Exp $");
 #endif
 
 /*
@@ -132,21 +132,21 @@ IDTVEC(trap02)
 	movl	$MSR_GSBASE,%ecx
 	rdmsr
 	cmpl	$VM_MIN_KERNEL_ADDRESS_HIGH32,%edx
-	jae	noswapgs
+	jae	.Lnoswapgs
 
 	swapgs
 	movq	%rsp,%rdi
 	incq	CPUVAR(NTRAP)
 	call	_C_LABEL(nmitrap)
 	swapgs
-	jmp	nmileave
+	jmp	.Lnmileave
 
-noswapgs:
+.Lnoswapgs:
 	movq	%rsp,%rdi
 	incq	CPUVAR(NTRAP)
 	call	_C_LABEL(trap)
 
-nmileave:
+.Lnmileave:
 	movw	TF_ES(%rsp),%es
 	movw	TF_DS(%rsp),%ds
 	INTR_RESTORE_GPRS

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.141 src/sys/arch/amd64/amd64/locore.S:1.142
--- src/sys/arch/amd64/amd64/locore.S:1.141	Tue Nov 21 09:58:09 2017
+++ src/sys/arch/amd64/amd64/locore.S	Sun Nov 26 14:54:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.141 2017/11/21 09:58:09 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.142 2017/11/26 14:54:43 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -450,7 +450,7 @@ ENTRY(start)
 	/* Load 'bootinfo' */
 	movl	12(%esp),%eax
 	testl	%eax,%eax		/* bootinfo = NULL? */
-	jz	bootinfo_finished
+	jz	.Lbootinfo_finished
 
 	movl	(%eax),%ebx		/* bootinfo::bi_nentries */
 	movl	$RELOC(bootinfo),%ebp
@@ -459,9 +459,9 @@ ENTRY(start)
 	movl	%ebx,(%edx)
 	addl	$4,%edx
 
-bootinfo_entryloop:
+.Lbootinfo_entryloop:
 	testl	%ebx,%ebx		/* no remaining entries? */
-	jz	bootinfo_finished
+	jz	.Lbootinfo_finished
 
 	addl	$4,%eax
 	movl	(%eax),%ecx		/* address of entry */
@@ -473,7 +473,7 @@ bootinfo_entryloop:
 	movl	%edx,%edi
 	addl	%eax,%edx		/* update dest pointer */
 	cmpl	%ebp,%edx		/* beyond bootinfo+BOOTINFO_MAXSIZE? */
-	jg	bootinfo_overflow
+	jg	.Lbootinfo_overflow
 
 	movl	%ecx,%esi
 	movl	%eax,%ecx
@@ -483,34 +483,34 @@ bootinfo_entryloop:
 	 * later to compute the initial bootstrap tables.
 	 */
 	cmpl	$BTINFO_MODULELIST,4(%esi) /* btinfo_common::type */
-	jne	bootinfo_copy
+	jne	.Lbootinfo_copy
 
 	/* Skip the modules if we won't have enough VA to map them */
 	movl	12(%esi),%eax		/* btinfo_modulelist::endpa */
 	addl	$PGOFSET,%eax		/* roundup to a page */
 	andl	$~PGOFSET,%eax
 	cmpl	$BOOTMAP_VA_SIZE,%eax
-	jg	bootinfo_skip
+	jg	.Lbootinfo_skip
 	movl	%eax,RELOC(eblob)
 	addl	$KERNBASE_LO,RELOC(eblob)
 	adcl	$KERNBASE_HI,RELOC(eblob)+4
 
-bootinfo_copy:
+.Lbootinfo_copy:
 	rep
 	movsb/* copy esi -> edi */
-	jmp	bootinfo_next
+	jmp	.Lbootinfo_next
 
-bootinfo_skip:
+.Lbootinfo_skip:
 	subl	%ecx,%edx		/* revert dest pointer */
 
-bootinfo_next:
+.Lbootinfo_next:
 	popl	%eax
 	popl	%esi
 	popl	%edi
 	subl	$1,%ebx			/* decrement the # of entries */
-	jmp	bootinfo_entryloop
+	jmp	.Lbootinfo_entryloop
 
-bootinfo_overflow:
+.Lbootinfo_overflow:
 	/*
 	 * Cleanup for overflow case. Pop the registers, and correct the number
 	 * of entries.
@@ -521,7 +521,7 @@ bootinfo_overflow:
 	movl	$RELOC(bootinfo),%ebp
 	movl	%ebp,%edx
 	subl	%ebx,(%edx)		/* correct the number of entries */
-bootinfo_finished:
+.Lbootinfo_finished:
 
 	/* Load 'esym' */
 	movl	16(%esp),%eax
@@ -539,22 +539,22 @@ bootinfo_finished:
 	movl	$RELOC(biosextmem),%ebp
 	movl	(%ebp),%eax
 	testl	%eax,%eax		/* already set? */
-	jnz	biosextmem_finished
+	jnz	.Lbiosextmem_finished
 
 	movl	20(%esp),%eax
 	movl	%eax,(%ebp)
 
-biosextmem_finished:
+.Lbiosextmem_finished:
 	/* Load 'biosbasemem' */
 	movl	$RELOC(biosbasemem),%ebp
 	movl	(%ebp),%eax
 	testl	%eax,%eax		/* already set? */
-	jnz	biosbasemem_finished
+	jnz	.Lbiosbasemem_finished
 
 	movl	24(%esp),%eax
 	movl	%eax,(%ebp)
 
-biosbasemem_finished:
+.Lbiosbasemem_finished:
 	/*
 	 * Done with the parameters!
 	 */
@@ -588,9 +588,9 @@ biosbasemem_finished:
 	movl	$0x8001,%eax
 	cpuid
 	andl	$CPUID_NOX,%edx
-	jz	no_NOX
+	jz	.Lno_NOX
 	movl	$PG_NX32,RELOC(nox_flag)
-no_NOX:
+.Lno_NOX:
 
 /*
  * There are four levels of 

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

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 14:29:48 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Oh, damn. Obviously I forgot one case here: an already-mapped region could
be contained entirely in the region we're trying to create. So go through
another round. While here add mm_reenter_pa, and make sure the va given to
mm_enter_pa does not already point to something.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.19 src/sys/arch/amd64/stand/prekern/mm.c:1.20
--- src/sys/arch/amd64/stand/prekern/mm.c:1.19	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Nov 26 14:29:48 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.19 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.20 2017/11/26 14:29:48 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -71,6 +71,15 @@ mm_init(paddr_t first_pa)
 static void
 mm_enter_pa(paddr_t pa, vaddr_t va, pte_prot_t prot)
 {
+	if (PTE_BASE[pl1_i(va)] & PG_V) {
+		fatal("mm_enter_pa: mapping already present");
+	}
+	PTE_BASE[pl1_i(va)] = pa | PG_V | protection_codes[prot];
+}
+
+static void
+mm_reenter_pa(paddr_t pa, vaddr_t va, pte_prot_t prot)
+{
 	PTE_BASE[pl1_i(va)] = pa | PG_V | protection_codes[prot];
 }
 
@@ -92,7 +101,7 @@ mm_palloc(size_t npages)
 
 	/* Zero them out */
 	for (i = 0; i < npages; i++) {
-		mm_enter_pa(pa + i * PAGE_SIZE, tmpva,
+		mm_reenter_pa(pa + i * PAGE_SIZE, tmpva,
 		MM_PROT_READ|MM_PROT_WRITE);
 		mm_flush_va(tmpva);
 		memset((void *)tmpva, 0, PAGE_SIZE);
@@ -120,7 +129,7 @@ mm_mprotect(vaddr_t startva, size_t size
 	for (i = 0; i < npages; i++) {
 		va = startva + i * PAGE_SIZE;
 		pa = (PTE_BASE[pl1_i(va)] & PG_FRAME);
-		mm_enter_pa(pa, va, prot);
+		mm_reenter_pa(pa, va, prot);
 		mm_flush_va(va);
 	}
 }
@@ -227,6 +236,10 @@ mm_randva_kregion(size_t size, size_t pa
 ok = false;
 break;
 			}
+			if (randva < sva && eva < (randva + size)) {
+ok = false;
+break;
+			}
 		}
 		if (ok) {
 			break;



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

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 11:08:35 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
I forgot to say in my previous commit that the PRNG is inspired from a
conversation with Taylor and Thor on tech-kern@.

(just add a comment)


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/prng.c
diff -u src/sys/arch/amd64/stand/prekern/prng.c:1.1 src/sys/arch/amd64/stand/prekern/prng.c:1.2
--- src/sys/arch/amd64/stand/prekern/prng.c:1.1	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/prng.c	Sun Nov 26 11:08:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prng.c,v 1.1 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: prng.c,v 1.2 2017/11/26 11:08:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -154,6 +154,7 @@ prng_init(void)
 
 	memset(, 0, sizeof(rng));
 
+	/* detect cpu features */
 	cpuid(0x07, 0x00, descs);
 	has_rdseed = (descs[1] & CPUID_SEF_RDSEED) != 0;
 	cpuid(0x01, 0x00, descs);



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

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 11:01:09 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile mm.c prekern.c prekern.h
Added Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
Add a PRNG for the prekern, based on SHA512. The formula is basically:

Y0   = SHA512(entropy-file, 256bit rdseed, 64bit rdtsc)
Yn+1 = SHA512(256bit lowerhalf(Yn), 256bit rdseed, 64bit rdtsc)

On each round, random values are taken from the higher half of Yn. If
rdseed is not available, rdrand is used.

The SHA1 checksum of entropy-file is verified. However, the rndsave_t::data
field is not updated by the prekern, because the area is accessed via the
read-only view we created in locore. I like this design, so it will have
to be updated differently.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.4 src/sys/arch/amd64/stand/prekern/Makefile:1.5
--- src/sys/arch/amd64/stand/prekern/Makefile:1.4	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Sun Nov 26 11:01:09 2017
@@ -1,7 +1,7 @@
-#	$NetBSD: Makefile,v 1.4 2017/11/17 07:07:52 maxv Exp $
+#	$NetBSD: Makefile,v 1.5 2017/11/26 11:01:09 maxv Exp $
 
 PROG=		prekern
-SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c
+SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c prng.c
 
 NOSSP=		# defined
 NOPIE=		# defined

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.18 src/sys/arch/amd64/stand/prekern/mm.c:1.19
--- src/sys/arch/amd64/stand/prekern/mm.c:1.18	Tue Nov 21 07:56:05 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Nov 26 11:01:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.18 2017/11/21 07:56:05 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.19 2017/11/26 11:01:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -196,13 +196,6 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	}
 }
 
-static uint64_t
-mm_rand_num64(void)
-{
-	/* XXX: yes, this is ridiculous, will be fixed soon */
-	return rdtsc();
-}
-
 static vaddr_t
 mm_randva_kregion(size_t size, size_t pagesz)
 {
@@ -213,7 +206,7 @@ mm_randva_kregion(size_t size, size_t pa
 	bool ok;
 
 	while (1) {
-		rnd = mm_rand_num64();
+		prng_get_rand(, sizeof(rnd));
 		randva = rounddown(KASLR_WINDOW_BASE +
 		rnd % (KASLR_WINDOW_SIZE - size), pagesz);
 
@@ -298,7 +291,7 @@ mm_shift_segment(vaddr_t va, size_t page
 		return 0;
 	}
 
-	rnd = mm_rand_num64();
+	prng_get_rand(, sizeof(rnd));
 	offset = roundup(rnd % shiftsize, elfalign);
 	ASSERT((va + offset) % elfalign == 0);
 
@@ -322,7 +315,7 @@ mm_map_head(void)
 	size = elf_get_head_size((vaddr_t)kernpa_start);
 	npages = size / PAGE_SIZE;
 
-	rnd = mm_rand_num64();
+	prng_get_rand(, sizeof(rnd));
 	randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
 	PAGE_SIZE);
 	mm_map_tree(randva, randva + size);

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.6 src/sys/arch/amd64/stand/prekern/prekern.c:1.7
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.6	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Sun Nov 26 11:01:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.6 2017/11/17 07:07:52 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.7 2017/11/26 11:01:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -336,6 +336,11 @@ init_prekern(paddr_t pa_start)
 	print_state(true, "Prekern loaded");
 
 	/*
+	 * Init the PRNG.
+	 */
+	prng_init();
+
+	/*
 	 * Relocate the kernel.
 	 */
 	mm_map_kernel();

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.17 src/sys/arch/amd64/stand/prekern/prekern.h:1.18
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.17	Sun Nov 26 10:21:20 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Sun Nov 26 11:01:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.17 2017/11/26 10:21:20 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.18 2017/11/26 11:01:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -111,3 +111,7 @@ void mm_map_kernel(void);
 
 /* prekern.c */
 void fatal(char *);
+
+/* prng.c */
+void prng_init(void);
+void prng_get_rand(void *, size_t);

Added files:

Index: src/sys/arch/amd64/stand/prekern/prng.c
diff -u /dev/null src/sys/arch/amd64/stand/prekern/prng.c:1.1
--- /dev/null	Sun 

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

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 10:21:20 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S prekern.h

Log Message:
Add rdrand.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.5 src/sys/arch/amd64/stand/prekern/locore.S:1.6
--- src/sys/arch/amd64/stand/prekern/locore.S:1.5	Tue Nov 14 13:58:07 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Sun Nov 26 10:21:20 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.5 2017/11/14 13:58:07 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.6 2017/11/26 10:21:20 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -612,15 +612,26 @@ END(rdtsc)
 
 ENTRY(rdseed)
 	rdseed	%rax
-	jc	.Lsuccess
+	jc	.Lrdseed_success
 	movq	$(-1),%rax
 	ret
-.Lsuccess:
+.Lrdseed_success:
 	movq	%rax,(%rdi)
 	xorq	%rax,%rax
 	ret
 END(rdseed)
 
+ENTRY(rdrand)
+	rdrand	%rax
+	jc	.Lrdrand_success
+	movq	$(-1),%rax
+	ret
+.Lrdrand_success:
+	movq	%rax,(%rdi)
+	xorq	%rax,%rax
+	ret
+END(rdrand)
+
 ENTRY(jump_kernel)
 	movq	_C_LABEL(stkva),%rsp
 	xorq	%rbp,%rbp

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.16 src/sys/arch/amd64/stand/prekern/prekern.h:1.17
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.16	Tue Nov 21 07:56:05 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Sun Nov 26 10:21:20 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.16 2017/11/21 07:56:05 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.17 2017/11/26 10:21:20 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -100,6 +100,7 @@ void cpuid(uint32_t, uint32_t, uint32_t 
 void lidt(void *);
 uint64_t rdtsc(void);
 int rdseed(uint64_t *);
+int rdrand(uint64_t *);
 void jump_kernel(vaddr_t);
 
 /* mm.c */



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

2017-11-25 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Sat Nov 25 16:32:48 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: ALL GENERIC

Log Message:
Add qemufwcfg (QEMU Firmware Configuration device)


To generate a diff of this commit:
cvs rdiff -u -r1.71 -r1.72 src/sys/arch/amd64/conf/ALL
cvs rdiff -u -r1.468 -r1.469 src/sys/arch/amd64/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/amd64/conf/ALL
diff -u src/sys/arch/amd64/conf/ALL:1.71 src/sys/arch/amd64/conf/ALL:1.72
--- src/sys/arch/amd64/conf/ALL:1.71	Thu Sep 14 07:58:39 2017
+++ src/sys/arch/amd64/conf/ALL	Sat Nov 25 16:32:48 2017
@@ -1,4 +1,4 @@
-# $NetBSD: ALL,v 1.71 2017/09/14 07:58:39 mrg Exp $
+# $NetBSD: ALL,v 1.72 2017/11/25 16:32:48 jmcneill 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.71 $"
+#ident		"ALL-$Revision: 1.72 $"
 
 maxusers	64		# estimated number of users
 
@@ -387,6 +387,7 @@ lpt*		at acpi?		# Parallel port
 mpu*		at acpi?		# Roland MPU-401 MIDI UART
 pckbc*		at acpi?		# PC keyboard controller
 pcppi*		at acpi?		# AT-style speaker sound
+qemufwcfg*	at acpi?		# QEMU Firmware Configuration device
 sdhc*		at acpi?		# SD Host Controller
 sony*		at acpi?		# Sony Notebook Controller
 spic*		at acpi?		# Sony Programmable I/O Controller

Index: src/sys/arch/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.468 src/sys/arch/amd64/conf/GENERIC:1.469
--- src/sys/arch/amd64/conf/GENERIC:1.468	Mon Nov  6 02:57:18 2017
+++ src/sys/arch/amd64/conf/GENERIC	Sat Nov 25 16:32:48 2017
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.468 2017/11/06 02:57:18 rin Exp $
+# $NetBSD: GENERIC,v 1.469 2017/11/25 16:32:48 jmcneill Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.468 $"
+#ident		"GENERIC-$Revision: 1.469 $"
 
 maxusers	64		# estimated number of users
 
@@ -313,6 +313,7 @@ joy*		at acpi?		# Joystick/Game port
 mpu*		at acpi?		# Roland MPU-401 MIDI UART
 pckbc*		at acpi?		# PC keyboard controller
 pcppi*		at acpi?		# AT-style speaker sound
+qemufwcfg*	at acpi?		# QEMU Firmware Configuration device
 sdhc*		at acpi?		# SD Host Controller
 sony*		at acpi?		# Sony Notebook Controller
 spic*		at acpi?		# Sony Programmable I/O Controller



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

2017-11-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 21 10:42:44 UTC 2017

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

Log Message:
Remove unused variables.


To generate a diff of this commit:
cvs rdiff -u -r1.276 -r1.277 src/sys/arch/amd64/amd64/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.276 src/sys/arch/amd64/amd64/machdep.c:1.277
--- src/sys/arch/amd64/amd64/machdep.c:1.276	Sat Nov 11 12:51:06 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Tue Nov 21 10:42:44 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.276 2017/11/11 12:51:06 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.277 2017/11/21 10:42:44 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.276 2017/11/11 12:51:06 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.277 2017/11/21 10:42:44 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -232,8 +232,6 @@ int cpu_class = CPUCLASS_686;
 struct mtrr_funcs *mtrr_funcs;
 #endif
 
-uint64_t dumpmem_low;
-uint64_t dumpmem_high;
 int cpu_class;
 int use_pae;
 



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

2017-11-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 21 09:58:09 UTC 2017

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

Log Message:
Remove unused symbol - it is aligned to 4096 and this reduces the number
of possible locations for .bss in KASLR kernels.


To generate a diff of this commit:
cvs rdiff -u -r1.140 -r1.141 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.140 src/sys/arch/amd64/amd64/locore.S:1.141
--- src/sys/arch/amd64/amd64/locore.S:1.140	Sat Oct 28 20:57:17 2017
+++ src/sys/arch/amd64/amd64/locore.S	Tue Nov 21 09:58:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.140 2017/10/28 20:57:17 bouyer Exp $	*/
+/*	$NetBSD: locore.S,v 1.141 2017/11/21 09:58:09 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -421,9 +421,6 @@ END(farjmp64)
 	.space	512
 tmpstk:
 
-	.globl _C_LABEL(cpu_private)
-	.comm _C_LABEL(cpu_private),PAGE_SIZE,PAGE_SIZE
-
 /*
  * Some hackage to deal with 64bit symbols in 32 bit mode.
  * This may not be needed if things are cleaned up a little.



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

2017-11-20 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 21 07:56:05 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Clean up and add some ASSERTs.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.16 src/sys/arch/amd64/stand/prekern/elf.c:1.17
--- src/sys/arch/amd64/stand/prekern/elf.c:1.16	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Tue Nov 21 07:56:05 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.16 2017/11/17 07:07:52 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.17 2017/11/21 07:56:05 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -347,6 +347,9 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	if (i == eif.ehdr->e_shnum) {
 		fatal("elf_build_boot: symtab not found");
 	}
+	if (eif.shdr[i].sh_offset == 0) {
+		fatal("elf_build_boot: symtab not loaded");
+	}
 	eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 	eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
 
@@ -358,6 +361,9 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	if (eif.shdr[j].sh_type != SHT_STRTAB) {
 		fatal("elf_build_boot: wrong strtab type");
 	}
+	if (eif.shdr[j].sh_offset == 0) {
+		fatal("elf_build_boot: strtab not loaded");
+	}
 	eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.strsz = eif.shdr[j].sh_size;
 }
@@ -380,6 +386,7 @@ elf_kernel_reloc(void)
 		eif.shdr[i].sh_type != SHT_PROGBITS) {
 			continue;
 		}
+		ASSERT(eif.shdr[i].sh_offset != 0);
 		secva = baseva + eif.shdr[i].sh_offset;
 		for (j = 0; j < eif.symcnt; j++) {
 			sym = [j];
@@ -400,9 +407,10 @@ elf_kernel_reloc(void)
 		size_t secidx, nrel;
 		uintptr_t base;
 
-		if (eif.shdr[i].sh_type != SHT_REL)
+		if (eif.shdr[i].sh_type != SHT_REL) {
 			continue;
-
+		}
+		ASSERT(eif.shdr[i].sh_offset != 0);
 		reltab = (Elf_Rel *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 		nrel = eif.shdr[i].sh_size / sizeof(Elf_Rel);
 
@@ -428,9 +436,10 @@ elf_kernel_reloc(void)
 		size_t secidx, nrela;
 		uintptr_t base;
 
-		if (eif.shdr[i].sh_type != SHT_RELA)
+		if (eif.shdr[i].sh_type != SHT_RELA) {
 			continue;
-
+		}
+		ASSERT(eif.shdr[i].sh_offset != 0);
 		relatab = (Elf_Rela *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 		nrela = eif.shdr[i].sh_size / sizeof(Elf_Rela);
 

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.17 src/sys/arch/amd64/stand/prekern/mm.c:1.18
--- src/sys/arch/amd64/stand/prekern/mm.c:1.17	Wed Nov 15 20:45:16 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Tue Nov 21 07:56:05 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.17 2017/11/15 20:45:16 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.18 2017/11/21 07:56:05 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,7 +36,7 @@
 
 #define ELFROUND	64
 
-static const int pads[4] = {
+static const uint8_t pads[4] = {
 	[BTSEG_NONE] = 0x00,
 	[BTSEG_TEXT] = 0xCC,
 	[BTSEG_RODATA] = 0x00,
@@ -107,12 +107,6 @@ mm_pte_is_valid(pt_entry_t pte)
 	return ((pte & PG_V) != 0);
 }
 
-paddr_t
-mm_vatopa(vaddr_t va)
-{
-	return (PTE_BASE[pl1_i(va)] & PG_FRAME);
-}
-
 static void
 mm_mprotect(vaddr_t startva, size_t size, pte_prot_t prot)
 {
@@ -169,9 +163,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	size_t L4e_idx, L3e_idx, L2e_idx;
 	paddr_t pa;
 
-	/*
-	 * Build L4.
-	 */
+	/* Build L4. */
 	L4e_idx = pl4_i(startva);
 	nL4e = mm_nentries_range(startva, endva, NBPD_L4);
 	ASSERT(L4e_idx == 511);
@@ -181,9 +173,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 		L4_BASE[L4e_idx] = pa | PG_V | PG_RW;
 	}
 
-	/*
-	 * Build L3.
-	 */
+	/* Build L3. */
 	L3e_idx = pl3_i(startva);
 	nL3e = mm_nentries_range(startva, endva, NBPD_L3);
 	for (i = 0; i < nL3e; i++) {
@@ -194,9 +184,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 		L3_BASE[L3e_idx+i] = pa | PG_V | PG_RW;
 	}
 
-	/*
-	 * Build L2.
-	 */
+	/* Build L2. */
 	L2e_idx = pl2_i(startva);
 	nL2e = mm_nentries_range(startva, endva, NBPD_L2);
 	for (i = 0; i < nL2e; i++) {
@@ -215,39 +203,6 @@ mm_rand_num64(void)
 	return rdtsc();
 }
 
-static void
-mm_map_head(void)
-{
-	size_t i, npages, size;
-	uint64_t rnd;
-	vaddr_t randva;
-
-	/*
-	 * To get the size of the head, we give a look at the read-only
-	 * mapping of the kernel we created in locore. We're identity mapped,
-	 * so kernpa = kernva.
-	 */
-	size = elf_get_head_size((vaddr_t)kernpa_start);
-	npages = size / PAGE_SIZE;
-
-	rnd = mm_rand_num64();
-	randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
-	PAGE_SIZE);
-	

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

2017-11-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 17 07:07:52 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile console.c elf.c pdir.h
prekern.c

Log Message:
style


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/console.c \
src/sys/arch/amd64/stand/prekern/pdir.h
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.3 src/sys/arch/amd64/stand/prekern/Makefile:1.4
--- src/sys/arch/amd64/stand/prekern/Makefile:1.3	Tue Nov 14 07:06:34 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Fri Nov 17 07:07:52 2017
@@ -1,7 +1,7 @@
-#	$NetBSD: Makefile,v 1.3 2017/11/14 07:06:34 maxv Exp $
+#	$NetBSD: Makefile,v 1.4 2017/11/17 07:07:52 maxv Exp $
 
 PROG=		prekern
-SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
+SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c
 
 NOSSP=		# defined
 NOPIE=		# defined
@@ -22,8 +22,8 @@ CPPFLAGS+=	-D_STANDALONE
 .include 
 
 CPPFLAGS+=	-DKERNEL -D__x86_64__
-CFLAGS+=	-Wall -Werror -mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
-CFLAGS+=	-Wstrict-prototypes
+CFLAGS+=	-Wall -Werror -Wstrict-prototypes
+CFLAGS+=	-mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
 STRIPFLAG=
 LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
 

Index: src/sys/arch/amd64/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.2 src/sys/arch/amd64/stand/prekern/console.c:1.3
--- src/sys/arch/amd64/stand/prekern/console.c:1.2	Tue Nov 14 07:06:34 2017
+++ src/sys/arch/amd64/stand/prekern/console.c	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.2 2017/11/14 07:06:34 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.3 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -108,7 +108,7 @@ void print_state(bool ok, char *buf)
 
 void print_banner(void)
 {
-	char *banner = 
+	char *banner =
 		"   __ __\n"
 		"   \\__   \\___    |  | __ ___    \n"
 		"| ___/\\_  __ \\_/ __ \\|  |/ // __ \\_  __ \\/\\ \n"
Index: src/sys/arch/amd64/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.2 src/sys/arch/amd64/stand/prekern/pdir.h:1.3
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.2	Sun Nov  5 16:27:18 2017
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: pdir.h,v 1.2 2017/11/05 16:27:18 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.3 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -28,11 +28,11 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#define PREKERNBASE		0x0
+#define PREKERNBASE	0x0
 #define PREKERNTEXTOFF	(PREKERNBASE + 0x10)
 
 #define L4_SLOT_PREKERN	0 /* pl4_i(PREKERNBASE) */
-#define L4_SLOT_PTE		255
+#define L4_SLOT_PTE	255
 
 #define PDIR_SLOT_KERN	L4_SLOT_PREKERN
 #define PDIR_SLOT_PTE	L4_SLOT_PTE

Index: src/sys/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.15 src/sys/arch/amd64/stand/prekern/elf.c:1.16
--- src/sys/arch/amd64/stand/prekern/elf.c:1.15	Wed Nov 15 20:45:16 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.15 2017/11/15 20:45:16 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.16 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -292,6 +292,7 @@ elf_map_sections(void)
 		secalign = shdr->sh_addralign;
 		ASSERT(shdr->sh_offset != 0);
 		ASSERT(secpa % PAGE_SIZE == 0);
+		ASSERT(secpa + secsz <= kernpa_end);
 
 		secva = mm_map_segment(segtype, secpa, secsz, secalign);
 

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.5 src/sys/arch/amd64/stand/prekern/prekern.c:1.6
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.5	Tue Nov 14 07:06:34 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.5 2017/11/14 07:06:34 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.6 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -46,10 +46,9 @@ struct bootinfo bootinfo;
 
 extern paddr_t kernpa_start, kernpa_end;
 
-extern uint64_t *gdt64_start;
-uint8_t idtstore[PAGE_SIZE];
-uint8_t faultstack[PAGE_SIZE];
-struct x86_64_tss prekern_tss;
+static uint8_t idtstore[PAGE_SIZE];
+static uint8_t faultstack[PAGE_SIZE];
+static struct x86_64_tss 

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

2017-11-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 15 20:45:16 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Small cleanup.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/amd64/stand/prekern/elf.c \
src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.14 src/sys/arch/amd64/stand/prekern/elf.c:1.15
--- src/sys/arch/amd64/stand/prekern/elf.c:1.14	Wed Nov 15 18:02:36 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Wed Nov 15 20:45:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.14 2017/11/15 18:02:36 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.15 2017/11/15 20:45:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -372,11 +372,6 @@ elf_kernel_reloc(void)
 	print_state(true, "ELF info created");
 
 	/*
-	 * The loaded sections are: SHT_PROGBITS, SHT_NOBITS, SHT_STRTAB,
-	 * SHT_SYMTAB.
-	 */
-
-	/*
 	 * Update all symbol values with the appropriate offset.
 	 */
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.14 src/sys/arch/amd64/stand/prekern/prekern.h:1.15
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.14	Wed Nov 15 18:44:34 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Wed Nov 15 20:45:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.14 2017/11/15 18:44:34 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.15 2017/11/15 20:45:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,7 +41,6 @@
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;
 typedef uint64_t pt_entry_t;
-typedef uint64_t pd_entry_t;
 typedef uint64_t pte_prot_t;
 #define WHITE_ON_BLACK 0x07
 #define RED_ON_BLACK 0x04

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.16 src/sys/arch/amd64/stand/prekern/mm.c:1.17
--- src/sys/arch/amd64/stand/prekern/mm.c:1.16	Wed Nov 15 20:25:29 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Nov 15 20:45:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.16 2017/11/15 20:25:29 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.17 2017/11/15 20:45:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,6 +36,13 @@
 
 #define ELFROUND	64
 
+static const int pads[4] = {
+	[BTSEG_NONE] = 0x00,
+	[BTSEG_TEXT] = 0xCC,
+	[BTSEG_RODATA] = 0x00,
+	[BTSEG_DATA] = 0x00
+};
+
 #define MM_PROT_READ	0x00
 #define MM_PROT_WRITE	0x01
 #define MM_PROT_EXECUTE	0x02
@@ -107,7 +114,7 @@ mm_vatopa(vaddr_t va)
 }
 
 static void
-mm_mprotect(vaddr_t startva, size_t size, int prot)
+mm_mprotect(vaddr_t startva, size_t size, pte_prot_t prot)
 {
 	size_t i, npages;
 	vaddr_t va;
@@ -127,7 +134,7 @@ mm_mprotect(vaddr_t startva, size_t size
 void
 mm_bootspace_mprotect(void)
 {
-	int prot;
+	pte_prot_t prot;
 	size_t i;
 
 	/* Remap the kernel segments with proper permissions. */
@@ -242,7 +249,7 @@ mm_map_head(void)
 }
 
 static vaddr_t
-mm_randva_kregion(size_t size, size_t align)
+mm_randva_kregion(size_t size, size_t pagesz)
 {
 	vaddr_t sva, eva;
 	vaddr_t randva;
@@ -253,7 +260,7 @@ mm_randva_kregion(size_t size, size_t al
 	while (1) {
 		rnd = mm_rand_num64();
 		randva = rounddown(KASLR_WINDOW_BASE +
-		rnd % (KASLR_WINDOW_SIZE - size), align);
+		rnd % (KASLR_WINDOW_SIZE - size), pagesz);
 
 		/* Detect collisions */
 		ok = true;
@@ -329,6 +336,8 @@ mm_shift_segment(vaddr_t va, size_t page
 		elfalign = ELFROUND;
 	}
 
+	ASSERT(pagesz >= elfalign);
+	ASSERT(pagesz % elfalign == 0);
 	shiftsize = roundup(elfsz, pagesz) - roundup(elfsz, elfalign);
 	if (shiftsize == 0) {
 		return 0;
@@ -368,13 +377,7 @@ mm_map_segment(int segtype, paddr_t pa, 
 	offset = mm_shift_segment(randva, pagesz, elfsz, elfalign);
 	ASSERT(offset + elfsz <= size);
 
-	if (segtype == BTSEG_TEXT) {
-		pad = PAD_TEXT;
-	} else if (segtype == BTSEG_RODATA) {
-		pad = PAD_RODATA;
-	} else {
-		pad = PAD_DATA;
-	}
+	pad = pads[segtype];
 	memset((void *)randva, pad, offset);
 	memset((void *)(randva + offset + elfsz), pad, size - elfsz - offset);
 



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

2017-11-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 15 20:25:29 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Mmh, should be <=.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.15 src/sys/arch/amd64/stand/prekern/mm.c:1.16
--- src/sys/arch/amd64/stand/prekern/mm.c:1.15	Wed Nov 15 18:44:34 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Nov 15 20:25:29 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.15 2017/11/15 18:44:34 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.16 2017/11/15 20:25:29 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -350,7 +350,7 @@ mm_map_segment(int segtype, paddr_t pa, 
 	vaddr_t randva;
 	char pad;
 
-	if (elfsz < PAGE_SIZE) {
+	if (elfsz <= PAGE_SIZE) {
 		pagesz = NBPD_L1;
 	} else {
 		pagesz = NBPD_L2;



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

2017-11-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 15 18:44:34 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c prekern.h

Log Message:
Define MM_PROT_* locally.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.14 src/sys/arch/amd64/stand/prekern/mm.c:1.15
--- src/sys/arch/amd64/stand/prekern/mm.c:1.14	Wed Nov 15 18:02:36 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Nov 15 18:44:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.14 2017/11/15 18:02:36 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.15 2017/11/15 18:44:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,6 +36,10 @@
 
 #define ELFROUND	64
 
+#define MM_PROT_READ	0x00
+#define MM_PROT_WRITE	0x01
+#define MM_PROT_EXECUTE	0x02
+
 static const pt_entry_t protection_codes[3] = {
 	[MM_PROT_READ] = PG_RO | PG_NX,
 	[MM_PROT_WRITE] = PG_RW | PG_NX,

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.13 src/sys/arch/amd64/stand/prekern/prekern.h:1.14
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.13	Wed Nov 15 18:02:36 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Wed Nov 15 18:44:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.13 2017/11/15 18:02:36 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.14 2017/11/15 18:44:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -37,10 +37,6 @@
 #include "pdir.h"
 #include "redef.h"
 
-#define MM_PROT_READ	0x00
-#define MM_PROT_WRITE	0x01
-#define MM_PROT_EXECUTE	0x02
-
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;



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

2017-11-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 14 13:58:08 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S redef.h

Log Message:
Remove XXX: set FRAMESIZE to the kernel value. Verily I don't understand
why we are doing that in the non-kaslr kernels, but let's just reproduce
the behavior.

jump_kernel is changed to use callq, so that the stack alignment is
preserved.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/redef.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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.4 src/sys/arch/amd64/stand/prekern/locore.S:1.5
--- src/sys/arch/amd64/stand/prekern/locore.S:1.4	Fri Nov 10 08:05:38 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Tue Nov 14 13:58:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.4 2017/11/10 08:05:38 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.5 2017/11/14 13:58:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -623,6 +623,6 @@ END(rdseed)
 
 ENTRY(jump_kernel)
 	movq	_C_LABEL(stkva),%rsp
-	movq	$exec_kernel,%rax
-	jmpq	*%rax
+	xorq	%rbp,%rbp
+	callq	exec_kernel
 END(jump_kernel)

Index: src/sys/arch/amd64/stand/prekern/redef.h
diff -u src/sys/arch/amd64/stand/prekern/redef.h:1.1 src/sys/arch/amd64/stand/prekern/redef.h:1.2
--- src/sys/arch/amd64/stand/prekern/redef.h:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/redef.h	Tue Nov 14 13:58:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: redef.h,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: redef.h,v 1.2 2017/11/14 13:58:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -43,5 +43,4 @@
  * -- */
 
 #define PDE_SIZE 8
-#define FRAMESIZE 8 /* XXX */
-
+#define FRAMESIZE 240



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

2017-11-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 14 10:15:40 UTC 2017

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

Log Message:
Split each kernel section into sub-blocks of approximately 2MB. The newly
created sections are named .origname.i, for example:

.text -> { .text .text.0 .text.1 .text.2 .text.3 .text.4 }

Each section is randomized independently by the prekern - and in a random
order obviously. As a result we can get intertwined mappings, of the type:

+---+---+--+-+---+---+---+--+-
| text1 | NOTMAPPED | bss0 | rodata1 | NOTMAPPED | data2 | text3 | bss1 |
+---+---+--+-+---+---+---+--+-

   -+-
rodata0 | ...
   -+-

The CTF section is dropped completely, because (a) when split it becomes
enormous for some reason (that I don't quite understand, verily), and (b)
the kernel expects only one CTF and can't handle several of them.


To generate a diff of this commit:
cvs rdiff -u -r1.62 -r1.63 src/sys/arch/amd64/conf/Makefile.amd64
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/conf/kern.ldscript.kaslr

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/Makefile.amd64
diff -u src/sys/arch/amd64/conf/Makefile.amd64:1.62 src/sys/arch/amd64/conf/Makefile.amd64:1.63
--- src/sys/arch/amd64/conf/Makefile.amd64:1.62	Tue Nov 14 09:56:26 2017
+++ src/sys/arch/amd64/conf/Makefile.amd64	Tue Nov 14 10:15:40 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.amd64,v 1.62 2017/11/14 09:56:26 maxv Exp $
+#	$NetBSD: Makefile.amd64,v 1.63 2017/11/14 10:15:40 maxv Exp $
 
 # Makefile for NetBSD
 #
@@ -62,7 +62,7 @@ KERN_AS=	library
 ##
 TEXTADDR?=	0x8020
 .if defined(KASLR)
-EXTRA_LINKFLAGS=	-r -d
+EXTRA_LINKFLAGS=	--split-by-file=0x20 -r -d
 KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscript.kaslr
 .else
 EXTRA_LINKFLAGS=	-z max-page-size=0x20

Index: src/sys/arch/amd64/conf/kern.ldscript.kaslr
diff -u src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.2 src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.3
--- src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.2	Mon Nov 13 20:01:48 2017
+++ src/sys/arch/amd64/conf/kern.ldscript.kaslr	Tue Nov 14 10:15:40 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript.kaslr,v 1.2 2017/11/13 20:01:48 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript.kaslr,v 1.3 2017/11/14 10:15:40 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -57,4 +57,10 @@ SECTIONS
 	{
 		KEEP(*(.note.netbsd.ident));
 	}
+
+	/DISCARD/ :
+	{
+		*(.SUNW_ctf)
+		*(.SUNW_ctf.*)
+	}
 }



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

2017-11-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 14 09:56:26 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: Makefile.amd64

Log Message:
Remove max-page-size on KASLR, it doesn't play any role.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/arch/amd64/conf/Makefile.amd64

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/Makefile.amd64
diff -u src/sys/arch/amd64/conf/Makefile.amd64:1.61 src/sys/arch/amd64/conf/Makefile.amd64:1.62
--- src/sys/arch/amd64/conf/Makefile.amd64:1.61	Thu Nov  9 15:46:48 2017
+++ src/sys/arch/amd64/conf/Makefile.amd64	Tue Nov 14 09:56:26 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.amd64,v 1.61 2017/11/09 15:46:48 maxv Exp $
+#	$NetBSD: Makefile.amd64,v 1.62 2017/11/14 09:56:26 maxv Exp $
 
 # Makefile for NetBSD
 #
@@ -62,7 +62,7 @@ KERN_AS=	library
 ##
 TEXTADDR?=	0x8020
 .if defined(KASLR)
-EXTRA_LINKFLAGS=	-z max-page-size=0x20 -r -d
+EXTRA_LINKFLAGS=	-r -d
 KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscript.kaslr
 .else
 EXTRA_LINKFLAGS=	-z max-page-size=0x20



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

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 14 07:06:34 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile console.c elf.c mm.c
prekern.c prekern.h

Log Message:
Add -Wstrict-prototypes, and fix each warning.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/console.c
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/amd64/stand/prekern/elf.c \
src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.2 src/sys/arch/amd64/stand/prekern/Makefile:1.3
--- src/sys/arch/amd64/stand/prekern/Makefile:1.2	Mon Nov 13 20:03:26 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.2 2017/11/13 20:03:26 maxv Exp $
+#	$NetBSD: Makefile,v 1.3 2017/11/14 07:06:34 maxv Exp $
 
 PROG=		prekern
 SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
@@ -23,6 +23,7 @@ CPPFLAGS+=	-D_STANDALONE
 
 CPPFLAGS+=	-DKERNEL -D__x86_64__
 CFLAGS+=	-Wall -Werror -mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
+CFLAGS+=	-Wstrict-prototypes
 STRIPFLAG=
 LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
 

Index: src/sys/arch/amd64/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.1 src/sys/arch/amd64/stand/prekern/console.c:1.2
--- src/sys/arch/amd64/stand/prekern/console.c:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/console.c	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.2 2017/11/14 07:06:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -38,14 +38,14 @@ static char *cons_start;
 static size_t cons_x, cons_y;
 static char cons_buffer[CONS_WID * 2 * CONS_HEI];
 
-void init_cons()
+void init_cons(void)
 {
 	cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN);
 	cons_x = 0;
 	cons_y = 0;
 }
 
-static void check_scroll()
+static void check_scroll(void)
 {
 	char *src, *dst;
 	size_t i;
@@ -106,7 +106,7 @@ void print_state(bool ok, char *buf)
 	print("\n");
 }
 
-void print_banner()
+void print_banner(void)
 {
 	char *banner = 
 		"   __ __\n"

Index: src/sys/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.12 src/sys/arch/amd64/stand/prekern/elf.c:1.13
--- src/sys/arch/amd64/stand/prekern/elf.c:1.12	Mon Nov 13 21:33:42 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.12 2017/11/13 21:33:42 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.13 2017/11/14 07:06:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -50,7 +50,7 @@ static struct elfinfo eif;
 static const char entrypoint[] = "start_prekern";
 
 static int
-elf_check_header()
+elf_check_header(void)
 {
 	if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
 	eif.ehdr->e_ident[EI_CLASS] != ELFCLASS ||
@@ -61,7 +61,7 @@ elf_check_header()
 }
 
 static vaddr_t
-elf_get_entrypoint()
+elf_get_entrypoint(void)
 {
 	Elf_Sym *sym;
 	size_t i;
@@ -259,7 +259,7 @@ elf_build_head(vaddr_t headva)
 }
 
 void
-elf_map_sections()
+elf_map_sections(void)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
@@ -361,7 +361,7 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 }
 
 vaddr_t
-elf_kernel_reloc()
+elf_kernel_reloc(void)
 {
 	const vaddr_t baseva = (vaddr_t)eif.ehdr;
 	vaddr_t secva, ent;
@@ -454,7 +454,7 @@ elf_kernel_reloc()
 	/*
 	 * Get the entry point.
 	 */
-	ent = elf_get_entrypoint();
+	ent = elf_get_entrypoint();
 	if (ent == 0) {
 		fatal("elf_kernel_reloc: entry point not found");
 	}
@@ -463,4 +463,3 @@ elf_kernel_reloc()
 
 	return ent;
 }
-
Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.12 src/sys/arch/amd64/stand/prekern/mm.c:1.13
--- src/sys/arch/amd64/stand/prekern/mm.c:1.12	Mon Nov 13 21:14:04 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.12 2017/11/13 21:14:04 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.13 2017/11/14 07:06:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -119,7 +119,7 @@ mm_mprotect(vaddr_t startva, size_t size
 }
 
 void
-mm_bootspace_mprotect()
+mm_bootspace_mprotect(void)
 {
 	int prot;
 	size_t i;
@@ -196,14 +196,14 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 }
 
 static uint64_t
-mm_rand_num64()

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

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 21:33:42 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
One more ASSERT, won't hurt.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/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/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.11 src/sys/arch/amd64/stand/prekern/elf.c:1.12
--- src/sys/arch/amd64/stand/prekern/elf.c:1.11	Mon Nov 13 21:32:21 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Mon Nov 13 21:33:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.11 2017/11/13 21:32:21 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.12 2017/11/13 21:33:42 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -295,6 +295,7 @@ elf_map_sections()
 		secva = mm_map_segment(segtype, secpa, secsz);
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
+		ASSERT(secva > headva);
 		shdr->sh_offset = secva - headva;
 	}
 }



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

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 21:14:04 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Change the mapping logic: don't group sections of the same type into
segments, and rather map each section independently at a random VA.

In particular, .data and .bss are not merged anymore and reside at
different addresses.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.9 src/sys/arch/amd64/stand/prekern/elf.c:1.10
--- src/sys/arch/amd64/stand/prekern/elf.c:1.9	Thu Nov  9 15:56:56 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Mon Nov 13 21:14:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.9 2017/11/09 15:56:56 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.10 2017/11/13 21:14:04 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -258,184 +258,41 @@ elf_build_head(vaddr_t headva)
 	}
 }
 
-static bool
-elf_section_is_text(Elf_Shdr *shdr)
-{
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	if (!(shdr->sh_flags & SHF_EXECINSTR)) {
-		return false;
-	}
-	return true;
-}
-
-static bool
-elf_section_is_rodata(Elf_Shdr *shdr)
-{
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	if (shdr->sh_flags & (SHF_EXECINSTR|SHF_WRITE)) {
-		return false;
-	}
-	return true;
-}
-
-static bool
-elf_section_is_data(Elf_Shdr *shdr)
-{
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	if (!(shdr->sh_flags & SHF_WRITE) ||
-	(shdr->sh_flags & SHF_EXECINSTR)) {
-		return false;
-	}
-	return true;
-}
-
 void
-elf_get_text(paddr_t *pa, size_t *sz)
-{
-	const paddr_t basepa = kernpa_start;
-	paddr_t minpa, maxpa, secpa;
-	size_t i, secsz;
-
-	minpa = 0x, maxpa = 0;
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_text([i])) {
-			continue;
-		}
-		secpa = basepa + eif.shdr[i].sh_offset;
-		secsz = eif.shdr[i].sh_size;
-		if (secpa < minpa) {
-			minpa = secpa;
-		}
-		if (secpa + secsz > maxpa) {
-			maxpa = secpa + secsz;
-		}
-	}
-	ASSERT(minpa % PAGE_SIZE == 0);
-
-	*pa = minpa;
-	*sz = maxpa - minpa;
-}
-
-void
-elf_build_text(vaddr_t textva, paddr_t textpa)
+elf_map_sections()
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
-	size_t i, offtext;
-
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_text([i])) {
-			continue;
-		}
-
-		/* Offset of the section within the text segment. */
-		offtext = basepa + eif.shdr[i].sh_offset - textpa;
-
-		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (textva + offtext - headva);
-	}
-}
-
-void
-elf_get_rodata(paddr_t *pa, size_t *sz)
-{
-	const paddr_t basepa = kernpa_start;
-	paddr_t minpa, maxpa, secpa;
+	Elf_Shdr *shdr;
+	int segtype;
+	vaddr_t secva;
+	paddr_t secpa;
 	size_t i, secsz;
 
-	minpa = 0x, maxpa = 0;
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_rodata([i])) {
-			continue;
-		}
-		secpa = basepa + eif.shdr[i].sh_offset;
-		secsz = eif.shdr[i].sh_size;
-		if (secpa < minpa) {
-			minpa = secpa;
-		}
-		if (secpa + secsz > maxpa) {
-			maxpa = secpa + secsz;
-		}
-	}
-	ASSERT(minpa % PAGE_SIZE == 0);
-
-	*pa = minpa;
-	*sz = maxpa - minpa;
-}
-
-void
-elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa)
-{
-	const paddr_t basepa = kernpa_start;
-	const vaddr_t headva = (vaddr_t)eif.ehdr;
-	size_t i, offrodata;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_rodata([i])) {
-			continue;
-		}
-
-		/* Offset of the section within the rodata segment. */
-		offrodata = basepa + eif.shdr[i].sh_offset - rodatapa;
-
-		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (rodatava + offrodata - headva);
-	}
-}
-
-void
-elf_get_data(paddr_t *pa, size_t *sz)
-{
-	const paddr_t basepa = kernpa_start;
-	paddr_t minpa, maxpa, secpa;
-	size_t i, secsz;
+		shdr = [i];
 
-	minpa = 0x, maxpa = 0;
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_data([i])) {
+		if (shdr->sh_type != SHT_NOBITS &&
+		shdr->sh_type != SHT_PROGBITS) {
 			continue;
 		}
-		secpa = basepa + eif.shdr[i].sh_offset;
-		secsz = eif.shdr[i].sh_size;
-		if (secpa < minpa) {
-			minpa = secpa;
-		}
-		if (secpa + secsz > maxpa) {
-			maxpa = secpa + secsz;
-		}
-	}
-	ASSERT(minpa % PAGE_SIZE == 0);
-
-	*pa = minpa;
-	*sz = maxpa - minpa;
-}
-
-void
-elf_build_data(vaddr_t datava, paddr_t 

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

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 20:03:26 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile prekern.h

Log Message:
Link libkern in the prekern, and remove redefined functions.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.1 src/sys/arch/amd64/stand/prekern/Makefile:1.2
--- src/sys/arch/amd64/stand/prekern/Makefile:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Mon Nov 13 20:03:26 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.1 2017/10/10 09:29:14 maxv Exp $
+#	$NetBSD: Makefile,v 1.2 2017/11/13 20:03:26 maxv Exp $
 
 PROG=		prekern
 SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
@@ -16,7 +16,8 @@ BINMODE=	444
 
 .include 
 
-CPPFLAGS+=	-I. -I${S}
+CPPFLAGS+=	-I. -I${S} -I${.OBJDIR} -I${.CURDIR}
+CPPFLAGS+=	-D_STANDALONE
 
 .include 
 
@@ -25,16 +26,25 @@ CFLAGS+=	-Wall -Werror -mno-red-zone -mn
 STRIPFLAG=
 LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
 
+KERN_AS=	library
+.include	"${S}/lib/libkern/Makefile.inc"
+LIBKERN=	${KERNLIB}
+
 LIBCRT0=	# nothing
 LIBCRTI=	# nothing
 LIBC=		# nothing
 LIBCRTBEGIN=	# nothing
 LIBCRTEND=	# nothing
 
-${PROG}: ${OBJS}
-	${LD} ${LINKFLAGS} -o ${.TARGET} ${OBJS}
+${PROG}: ${OBJS} ${LIBKERN}
+	${_MKTARGET_LINK}
+	${LD} ${LINKFLAGS} -o ${.TARGET} ${OBJS} ${LIBKERN}
 
 all:	${PROG}
 
 .include 
 
+cleandir distclean: .WAIT cleanlibdir
+
+cleanlibdir:
+	-rm -rf lib

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.9 src/sys/arch/amd64/stand/prekern/prekern.h:1.10
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.9	Sat Nov 11 12:51:06 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Mon Nov 13 20:03:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.9 2017/11/11 12:51:06 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.10 2017/11/13 20:03:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -31,6 +31,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "pdir.h"
@@ -58,50 +59,6 @@ typedef uint64_t pte_prot_t;
 
 /* -- */
 
-static inline void
-memcpy(void *dst, void *src, size_t sz)
-{
-	char *bdst = dst, *bsrc = src;
-	while (sz > 0) {
-		*bdst = *bsrc;
-		bdst++, bsrc++, sz--;
-	}
-}
-
-static inline void
-memset(void *dst, char c, size_t sz)
-{
-	char *bdst = dst;
-	while (sz > 0) {
-		*bdst = c;
-		bdst++, sz--;
-	}
-}
-
-static inline int
-memcmp(const char *a, const char *b, size_t c)
-{
-	size_t i;
-	for (i = 0; i < c; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-
-static inline int
-strcmp(char *a, char *b)
-{
-	size_t i;
-	for (i = 0; a[i] != '\0'; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-
-/* -- */
-
 #define BTSEG_NONE	0
 #define BTSEG_TEXT	1
 #define BTSEG_RODATA	2



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

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 20:01:48 UTC 2017

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

Log Message:
Use SUBALIGN, to force the alignment at the section level, and remove
the inter-section ALIGN which doesn't do anything since the physical
address of the section is chosen dynamically by the bootloader.


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

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.kaslr
diff -u src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.1 src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.2
--- src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.1	Thu Nov  9 15:46:48 2017
+++ src/sys/arch/amd64/conf/kern.ldscript.kaslr	Mon Nov 13 20:01:48 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern.ldscript.kaslr,v 1.1 2017/11/09 15:46:48 maxv Exp $	*/
+/*	$NetBSD: kern.ldscript.kaslr,v 1.2 2017/11/13 20:01:48 maxv Exp $	*/
 
 #include "assym.h"
 
@@ -27,17 +27,16 @@ SECTIONS
 		*(.data)
 	}
 
-	. = ALIGN(COHERENCY_UNIT);
-	.data.cacheline_aligned :
+	.data.cacheline_aligned : SUBALIGN(COHERENCY_UNIT)
 	{
 		*(.data.cacheline_aligned)
+		. = ALIGN(COHERENCY_UNIT);
 	}
-	. = ALIGN(COHERENCY_UNIT);
-	.data.read_mostly :
+	.data.read_mostly : SUBALIGN(COHERENCY_UNIT)
 	{
 		*(.data.read_mostly)
+		. = ALIGN(COHERENCY_UNIT);
 	}
-	. = ALIGN(COHERENCY_UNIT);
 
 	_edata = . ;
 	PROVIDE (edata = .) ;
@@ -59,4 +58,3 @@ SECTIONS
 		KEEP(*(.note.netbsd.ident));
 	}
 }
-



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

2017-11-12 Thread Thomas Klausner
Module Name:src
Committed By:   wiz
Date:   Mon Nov 13 07:06:49 UTC 2017

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

Log Message:
Remove superfluous word in comment. Noted by Geoff Wing.


To generate a diff of this commit:
cvs rdiff -u -r1.44 -r1.45 src/sys/arch/amd64/include/vmparam.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/vmparam.h
diff -u src/sys/arch/amd64/include/vmparam.h:1.44 src/sys/arch/amd64/include/vmparam.h:1.45
--- src/sys/arch/amd64/include/vmparam.h:1.44	Sat Nov 11 20:23:49 2017
+++ src/sys/arch/amd64/include/vmparam.h	Mon Nov 13 07:06:49 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: vmparam.h,v 1.44 2017/11/11 20:23:49 mrg Exp $	*/
+/*	$NetBSD: vmparam.h,v 1.45 2017/11/13 07:06:49 wiz Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -57,7 +57,7 @@
 #define	PAGE_MASK	(PAGE_SIZE - 1)
 
 /*
- * Default pager_map of 16MB is awfully small.  There is have plenty
+ * Default pager_map of 16MB is awfully small.  There is plenty
  * of VA so use it.
  */
 #define	PAGER_MAP_DEFAULT_SIZE (512 * 1024 * 1024)



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

2017-11-11 Thread matthew green
Module Name:src
Committed By:   mrg
Date:   Sat Nov 11 20:23:49 UTC 2017

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

Log Message:
bump PAGER_MAP_DEFAULT_SIZE to 512MB.  this should allow more
concurrent IOs to be possible, and i'm unable to see pager_map
contention any more.

other larger platforms should probably do this too.

ok chs@.


To generate a diff of this commit:
cvs rdiff -u -r1.43 -r1.44 src/sys/arch/amd64/include/vmparam.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/vmparam.h
diff -u src/sys/arch/amd64/include/vmparam.h:1.43 src/sys/arch/amd64/include/vmparam.h:1.44
--- src/sys/arch/amd64/include/vmparam.h:1.43	Sat Jun 24 13:43:36 2017
+++ src/sys/arch/amd64/include/vmparam.h	Sat Nov 11 20:23:49 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: vmparam.h,v 1.43 2017/06/24 13:43:36 joerg Exp $	*/
+/*	$NetBSD: vmparam.h,v 1.44 2017/11/11 20:23:49 mrg Exp $	*/
 
 /*-
  * Copyright (c) 1990 The Regents of the University of California.
@@ -57,6 +57,12 @@
 #define	PAGE_MASK	(PAGE_SIZE - 1)
 
 /*
+ * Default pager_map of 16MB is awfully small.  There is have plenty
+ * of VA so use it.
+ */
+#define	PAGER_MAP_DEFAULT_SIZE (512 * 1024 * 1024)
+
+/*
  * USRSTACK is the top (end) of the user stack. Immediately above the
  * user stack resides the user structure, which is UPAGES long and contains
  * the kernel stack.



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

2017-11-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov 11 13:50:57 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Detect collisions from bootspace directly.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.10 src/sys/arch/amd64/stand/prekern/mm.c:1.11
--- src/sys/arch/amd64/stand/prekern/mm.c:1.10	Sat Nov 11 12:51:06 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sat Nov 11 13:50:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.10 2017/11/11 12:51:06 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.11 2017/11/11 13:50:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -238,18 +238,12 @@ mm_map_head()
 static vaddr_t
 mm_randva_kregion(size_t size)
 {
-	static struct {
-		vaddr_t sva;
-		vaddr_t eva;
-	} regions[4];
-	static size_t idx = 0;
+	vaddr_t sva, eva;
 	vaddr_t randva;
 	uint64_t rnd;
 	size_t i;
 	bool ok;
 
-	ASSERT(idx < 4);
-
 	while (1) {
 		rnd = mm_rand_num64();
 		randva = rounddown(KASLR_WINDOW_BASE +
@@ -257,14 +251,18 @@ mm_randva_kregion(size_t size)
 
 		/* Detect collisions */
 		ok = true;
-		for (i = 0; i < idx; i++) {
-			if ((regions[i].sva <= randva) &&
-			(randva < regions[i].eva)) {
+		for (i = 0; i < BTSPACE_NSEGS; i++) {
+			if (bootspace.segs[i].type == BTSEG_NONE) {
+continue;
+			}
+			sva = bootspace.segs[i].va;
+			eva = sva + bootspace.segs[i].sz;
+
+			if ((sva <= randva) && (randva < eva)) {
 ok = false;
 break;
 			}
-			if ((regions[i].sva < randva + size) &&
-			(randva + size <= regions[i].eva)) {
+			if ((sva < randva + size) && (randva + size <= eva)) {
 ok = false;
 break;
 			}
@@ -274,10 +272,6 @@ mm_randva_kregion(size_t size)
 		}
 	}
 
-	regions[idx].eva = randva;
-	regions[idx].sva = randva + size;
-	idx++;
-
 	mm_map_tree(randva, randva + size);
 
 	return randva;



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

2017-11-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 10 08:52:57 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.h

Log Message:
Implement memcpy, the builtin version does not work with variable sizes.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.7 src/sys/arch/amd64/stand/prekern/prekern.h:1.8
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.7	Fri Nov 10 08:05:38 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Fri Nov 10 08:52:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.7 2017/11/10 08:05:38 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.8 2017/11/10 08:52:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,7 +41,6 @@
 #define MM_PROT_EXECUTE	0x02
 
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
-#define memcpy(d, v, l) __builtin_memcpy(d, v, l)
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;
 typedef uint64_t pt_entry_t;
@@ -60,6 +59,16 @@ typedef uint64_t pte_prot_t;
 /* -- */
 
 static inline void
+memcpy(void *dst, void *src, size_t sz)
+{
+	char *bdst = dst, *bsrc = src;
+	while (sz > 0) {
+		*bdst = *bsrc;
+		bdst++, bsrc++, sz--;
+	}
+}
+
+static inline void
 memset(void *dst, char c, size_t sz)
 {
 	char *bdst = dst;



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

2017-11-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 10 08:05:38 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S prekern.h

Log Message:
Add cpuid and rdseed.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.3 src/sys/arch/amd64/stand/prekern/locore.S:1.4
--- src/sys/arch/amd64/stand/prekern/locore.S:1.3	Sun Oct 29 11:28:30 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Fri Nov 10 08:05:38 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.3 2017/10/29 11:28:30 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.4 2017/11/10 08:05:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -583,9 +583,24 @@ END(start)
 
 /* -- */
 
+ENTRY(cpuid)
+	movq	%rbx,%r8
+	movq	%rdi,%rax
+	movq	%rsi,%rcx
+	movq	%rdx,%rsi
+	cpuid
+	movl	%eax,0(%rsi)
+	movl	%ebx,4(%rsi)
+	movl	%ecx,8(%rsi)
+	movl	%edx,12(%rsi)
+	movq	%r8,%rbx
+	ret
+END(cpuid)
+
 ENTRY(lidt)
 	lidt	(%rdi)
 	ret
+END(lidt)
 
 ENTRY(rdtsc)
 	xorq	%rax,%rax
@@ -593,9 +608,21 @@ ENTRY(rdtsc)
 	shlq	$32,%rdx
 	orq	%rdx,%rax
 	ret
+END(rdtsc)
+
+ENTRY(rdseed)
+	rdseed	%rax
+	jc	.Lsuccess
+	movq	$(-1),%rax
+	ret
+.Lsuccess:
+	movq	%rax,(%rdi)
+	xorq	%rax,%rax
+	ret
+END(rdseed)
 
 ENTRY(jump_kernel)
 	movq	_C_LABEL(stkva),%rsp
 	movq	$exec_kernel,%rax
 	jmpq	*%rax
-
+END(jump_kernel)

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.6 src/sys/arch/amd64/stand/prekern/prekern.h:1.7
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.6	Thu Nov  9 15:56:56 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Fri Nov 10 08:05:38 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.6 2017/11/09 15:56:56 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.7 2017/11/10 08:05:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -144,8 +144,10 @@ void elf_build_boot(vaddr_t, paddr_t);
 vaddr_t elf_kernel_reloc();
 
 /* locore.S */
+void cpuid(uint32_t, uint32_t, uint32_t *);
 void lidt(void *);
 uint64_t rdtsc();
+int rdseed(uint64_t *);
 void jump_kernel();
 
 /* mm.c */



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

2017-11-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Nov  9 15:56:56 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c prekern.h

Log Message:
Define utility functions as inlines in prekern.h.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.8 src/sys/arch/amd64/stand/prekern/elf.c:1.9
--- src/sys/arch/amd64/stand/prekern/elf.c:1.8	Thu Nov  9 15:24:39 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Thu Nov  9 15:56:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.8 2017/11/09 15:24:39 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.9 2017/11/09 15:56:56 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -49,29 +49,6 @@ extern paddr_t kernpa_start, kernpa_end;
 static struct elfinfo eif;
 static const char entrypoint[] = "start_prekern";
 
-/* XXX */
-static int
-memcmp(const char *a, const char *b, size_t c)
-{
-	size_t i;
-	for (i = 0; i < c; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-static int
-strcmp(char *a, char *b)
-{
-	size_t i;
-	for (i = 0; a[i] != '\0'; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-
-
 static int
 elf_check_header()
 {

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.5 src/sys/arch/amd64/stand/prekern/prekern.h:1.6
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.5	Thu Nov  9 15:24:39 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Thu Nov  9 15:56:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.5 2017/11/09 15:24:39 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.6 2017/11/09 15:56:56 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -69,6 +69,28 @@ memset(void *dst, char c, size_t sz)
 	}
 }
 
+static inline int
+memcmp(const char *a, const char *b, size_t c)
+{
+	size_t i;
+	for (i = 0; i < c; i++) {
+		if (a[i] != b[i])
+			return 1;
+	}
+	return 0;
+}
+
+static inline int
+strcmp(char *a, char *b)
+{
+	size_t i;
+	for (i = 0; a[i] != '\0'; i++) {
+		if (a[i] != b[i])
+			return 1;
+	}
+	return 0;
+}
+
 /* -- */
 
 struct bootspace {



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

2017-11-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Nov  9 15:46:48 UTC 2017

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

Log Message:
Use another ld script for kaslr kernels, in which there are no alignment
directives. They don't matter since the bootloader overwrites them.

But, normally we still need to make sure .data.read_mostly is aligned.
Unfortunately I couldn't find any way to force sh_addralign to be 64, so
I'm leaving the alignment there as a useless reminder.


To generate a diff of this commit:
cvs rdiff -u -r1.60 -r1.61 src/sys/arch/amd64/conf/Makefile.amd64
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/conf/kern.ldscript.kaslr

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/Makefile.amd64
diff -u src/sys/arch/amd64/conf/Makefile.amd64:1.60 src/sys/arch/amd64/conf/Makefile.amd64:1.61
--- src/sys/arch/amd64/conf/Makefile.amd64:1.60	Wed Nov  1 09:31:24 2017
+++ src/sys/arch/amd64/conf/Makefile.amd64	Thu Nov  9 15:46:48 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.amd64,v 1.60 2017/11/01 09:31:24 maxv Exp $
+#	$NetBSD: Makefile.amd64,v 1.61 2017/11/09 15:46:48 maxv Exp $
 
 # Makefile for NetBSD
 #
@@ -63,11 +63,12 @@ KERN_AS=	library
 TEXTADDR?=	0x8020
 .if defined(KASLR)
 EXTRA_LINKFLAGS=	-z max-page-size=0x20 -r -d
+KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscript.kaslr
 .else
 EXTRA_LINKFLAGS=	-z max-page-size=0x20
+KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscript
 .endif
 LINKFLAGS_NORMAL=	-X
-KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscript
 
 ##
 ## (6) port specific target dependencies

Added files:

Index: src/sys/arch/amd64/conf/kern.ldscript.kaslr
diff -u /dev/null src/sys/arch/amd64/conf/kern.ldscript.kaslr:1.1
--- /dev/null	Thu Nov  9 15:46:48 2017
+++ src/sys/arch/amd64/conf/kern.ldscript.kaslr	Thu Nov  9 15:46:48 2017
@@ -0,0 +1,62 @@
+/*	$NetBSD: kern.ldscript.kaslr,v 1.1 2017/11/09 15:46:48 maxv Exp $	*/
+
+#include "assym.h"
+
+ENTRY(_start)
+SECTIONS
+{
+	.text :
+	{
+		*(.text)
+		*(.text.*)
+		*(.stub)
+	} =0xCC
+	_etext = . ;
+	PROVIDE (etext = .) ;
+
+	__rodata_start = . ;
+	.rodata :
+	{
+		*(.rodata)
+		*(.rodata.*)
+	}
+
+	__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)
+	}
+
+	/* End of the kernel image */
+	__kernel_end = . ;
+
+	_end = . ;
+	PROVIDE (end = .) ;
+	.note.netbsd.ident :
+	{
+		KEEP(*(.note.netbsd.ident));
+	}
+}
+



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

2017-11-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Nov  9 15:24:39 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Fill in the page padding. Only .text is pre-filled by the ld script, but
this will change in the future.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.7 src/sys/arch/amd64/stand/prekern/elf.c:1.8
--- src/sys/arch/amd64/stand/prekern/elf.c:1.7	Sun Nov  5 16:26:15 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Thu Nov  9 15:24:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.7 2017/11/05 16:26:15 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.8 2017/11/09 15:24:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -345,7 +345,7 @@ elf_get_text(paddr_t *pa, size_t *sz)
 	ASSERT(minpa % PAGE_SIZE == 0);
 
 	*pa = minpa;
-	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+	*sz = maxpa - minpa;
 }
 
 void
@@ -392,7 +392,7 @@ elf_get_rodata(paddr_t *pa, size_t *sz)
 	ASSERT(minpa % PAGE_SIZE == 0);
 
 	*pa = minpa;
-	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+	*sz = maxpa - minpa;
 }
 
 void
@@ -439,7 +439,7 @@ elf_get_data(paddr_t *pa, size_t *sz)
 	ASSERT(minpa % PAGE_SIZE == 0);
 
 	*pa = minpa;
-	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+	*sz = maxpa - minpa;
 }
 
 void

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.8 src/sys/arch/amd64/stand/prekern/mm.c:1.9
--- src/sys/arch/amd64/stand/prekern/mm.c:1.8	Sun Nov  5 16:26:15 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Thu Nov  9 15:24:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.8 2017/11/05 16:26:15 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.9 2017/11/09 15:24:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -30,6 +30,10 @@
 
 #include "prekern.h"
 
+#define PAD_TEXT	0xCC
+#define PAD_RODATA	0x00
+#define PAD_DATA	0x00
+
 static const pt_entry_t protection_codes[3] = {
 	[MM_PROT_READ] = PG_RO | PG_NX,
 	[MM_PROT_WRITE] = PG_RW | PG_NX,
@@ -275,14 +279,15 @@ mm_randva_kregion(size_t size)
 static void
 mm_map_segments()
 {
-	size_t i, npages, size;
+	size_t i, npages, size, elfsz;
 	vaddr_t randva;
 	paddr_t pa;
 
 	/*
 	 * Kernel text segment.
 	 */
-	elf_get_text(, );
+	elf_get_text(, );
+	size = roundup(elfsz, PAGE_SIZE);
 	randva = mm_randva_kregion(size);
 	npages = size / PAGE_SIZE;
 
@@ -293,6 +298,9 @@ mm_map_segments()
 	}
 	elf_build_text(randva, pa);
 
+	/* Fill in the padding */
+	memset((void *)(randva + elfsz), PAD_TEXT, size - elfsz);
+
 	/* Register the values in bootspace */
 	bootspace.text.va = randva;
 	bootspace.text.pa = pa;
@@ -301,7 +309,8 @@ mm_map_segments()
 	/*
 	 * Kernel rodata segment.
 	 */
-	elf_get_rodata(, );
+	elf_get_rodata(, );
+	size = roundup(elfsz, PAGE_SIZE);
 	randva = mm_randva_kregion(size);
 	npages = size / PAGE_SIZE;
 
@@ -312,6 +321,9 @@ mm_map_segments()
 	}
 	elf_build_rodata(randva, pa);
 
+	/* Fill in the padding */
+	memset((void *)(randva + elfsz), PAD_RODATA, size - elfsz);
+
 	/* Register the values in bootspace */
 	bootspace.rodata.va = randva;
 	bootspace.rodata.pa = pa;
@@ -320,7 +332,8 @@ mm_map_segments()
 	/*
 	 * Kernel data segment.
 	 */
-	elf_get_data(, );
+	elf_get_data(, );
+	size = roundup(elfsz, PAGE_SIZE);
 	randva = mm_randva_kregion(size);
 	npages = size / PAGE_SIZE;
 
@@ -331,6 +344,9 @@ mm_map_segments()
 	}
 	elf_build_data(randva, pa);
 
+	/* Fill in the padding */
+	memset((void *)(randva + elfsz), PAD_DATA, size - elfsz);
+
 	/* Register the values in bootspace */
 	bootspace.data.va = randva;
 	bootspace.data.pa = pa;

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.4 src/sys/arch/amd64/stand/prekern/prekern.h:1.5
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.4	Sun Nov  5 16:26:15 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Thu Nov  9 15:24:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.4 2017/11/05 16:26:15 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.5 2017/11/09 15:24:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,7 +41,6 @@
 #define MM_PROT_EXECUTE	0x02
 
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
-#define memset(d, v, l) __builtin_memset(d, v, l)
 #define memcpy(d, v, l) __builtin_memcpy(d, v, l)
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;
@@ -60,6 +59,18 @@ typedef uint64_t pte_prot_t;
 
 /* -- */
 
+static inline void
+memset(void *dst, char c, size_t sz)
+{
+	char *bdst = 

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

2017-11-08 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  8 18:29:04 UTC 2017

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

Log Message:
Don't fall through.


To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 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/vector.S
diff -u src/sys/arch/amd64/amd64/vector.S:1.52 src/sys/arch/amd64/amd64/vector.S:1.53
--- src/sys/arch/amd64/amd64/vector.S:1.52	Mon Oct 30 17:06:42 2017
+++ src/sys/arch/amd64/amd64/vector.S	Wed Nov  8 18:29:04 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: vector.S,v 1.52 2017/10/30 17:06:42 maxv Exp $	*/
+/*	$NetBSD: vector.S,v 1.53 2017/11/08 18:29:04 maxv Exp $	*/
 
-/*-
+/*
  * Copyright (c) 1998, 2007, 2008 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
@@ -140,6 +140,7 @@ IDTVEC(intr_lapic_ipi)
 	movl	CPUVAR(ILEVEL),%ebx
 	cmpl	$IPL_HIGH,%ebx
 	jae	2f
+	jmp	1f
 IDTVEC_END(intr_lapic_ipi)
 IDTVEC(resume_lapic_ipi)
 1:
@@ -223,6 +224,7 @@ IDTVEC(intr_lapic_ltimer)
 	movl	CPUVAR(ILEVEL),%ebx
 	cmpl	$IPL_CLOCK,%ebx
 	jae	2f
+	jmp	1f
 IDTVEC_END(intr_lapic_ltimer)
 IDTVEC(resume_lapic_ltimer)
 1:



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

2017-11-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov  5 16:27:18 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: pdir.h

Log Message:
Remove unused.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/pdir.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/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.1 src/sys/arch/amd64/stand/prekern/pdir.h:1.2
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Sun Nov  5 16:27:18 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: pdir.h,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.2 2017/11/05 16:27:18 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -44,13 +44,6 @@
 #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_BASE	L4_BASE
-
-#define NKL4_MAX_ENTRIES	(unsigned long)1
-#define NKL3_MAX_ENTRIES	(unsigned long)(NKL4_MAX_ENTRIES * 512)
-#define NKL2_MAX_ENTRIES	(unsigned long)(NKL3_MAX_ENTRIES * 512)
-#define NKL1_MAX_ENTRIES	(unsigned long)(NKL2_MAX_ENTRIES * 512)
-
 #define NKL4_KIMG_ENTRIES	1
 #define NKL3_KIMG_ENTRIES	1
 #define NKL2_KIMG_ENTRIES	32



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

2017-11-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov  5 16:26:15 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.c prekern.h

Log Message:
Mprotect the segments in mm.c using bootspace, and remove the now unused
fields of elfinfo.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/prekern.c \
src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.6 src/sys/arch/amd64/stand/prekern/elf.c:1.7
--- src/sys/arch/amd64/stand/prekern/elf.c:1.6	Wed Nov  1 17:00:17 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Nov  5 16:26:15 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.6 2017/11/01 17:00:17 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.7 2017/11/05 16:26:15 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -42,18 +42,6 @@ struct elfinfo {
 	size_t symcnt;
 	char *strtab;
 	size_t strsz;
-	struct {
-		vaddr_t va;
-		size_t sz;
-	} text;
-	struct {
-		vaddr_t va;
-		size_t sz;
-	} rodata;
-	struct {
-		vaddr_t va;
-		size_t sz;
-	} data;
 };
 
 extern paddr_t kernpa_start, kernpa_end;
@@ -361,15 +349,12 @@ elf_get_text(paddr_t *pa, size_t *sz)
 }
 
 void
-elf_build_text(vaddr_t textva, paddr_t textpa, size_t textsz)
+elf_build_text(vaddr_t textva, paddr_t textpa)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
 	size_t i, offtext;
 
-	eif.text.va = textva;
-	eif.text.sz = textsz;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		if (!elf_section_is_text([i])) {
 			continue;
@@ -379,7 +364,7 @@ elf_build_text(vaddr_t textva, paddr_t t
 		offtext = basepa + eif.shdr[i].sh_offset - textpa;
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (eif.text.va + offtext - headva);
+		eif.shdr[i].sh_offset = (textva + offtext - headva);
 	}
 }
 
@@ -411,15 +396,12 @@ elf_get_rodata(paddr_t *pa, size_t *sz)
 }
 
 void
-elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa, size_t rodatasz)
+elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
 	size_t i, offrodata;
 
-	eif.rodata.va = rodatava;
-	eif.rodata.sz = rodatasz;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		if (!elf_section_is_rodata([i])) {
 			continue;
@@ -429,7 +411,7 @@ elf_build_rodata(vaddr_t rodatava, paddr
 		offrodata = basepa + eif.shdr[i].sh_offset - rodatapa;
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (eif.rodata.va + offrodata - headva);
+		eif.shdr[i].sh_offset = (rodatava + offrodata - headva);
 	}
 }
 
@@ -461,15 +443,12 @@ elf_get_data(paddr_t *pa, size_t *sz)
 }
 
 void
-elf_build_data(vaddr_t datava, paddr_t datapa, size_t datasz)
+elf_build_data(vaddr_t datava, paddr_t datapa)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
 	size_t i, offdata;
 
-	eif.data.va = datava;
-	eif.data.sz = datasz;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		if (!elf_section_is_data([i])) {
 			continue;
@@ -479,7 +458,7 @@ elf_build_data(vaddr_t datava, paddr_t d
 		offdata = basepa + eif.shdr[i].sh_offset - datapa;
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (eif.data.va + offdata - headva);
+		eif.shdr[i].sh_offset = (datava + offdata - headva);
 	}
 }
 
@@ -644,15 +623,6 @@ elf_kernel_reloc()
 
 	print_state(true, "Entry point found");
 
-	/*
-	 * Remap the code segments with proper permissions.
-	 */
-	mm_mprotect(eif.text.va, eif.text.sz, MM_PROT_READ|MM_PROT_EXECUTE);
-	mm_mprotect(eif.rodata.va, eif.rodata.sz, MM_PROT_READ);
-	mm_mprotect(eif.data.va, eif.data.sz, MM_PROT_READ|MM_PROT_WRITE);
-
-	print_state(true, "Segments protection updated");
-
 	return ent;
 }
 

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.7 src/sys/arch/amd64/stand/prekern/mm.c:1.8
--- src/sys/arch/amd64/stand/prekern/mm.c:1.7	Sun Oct 29 11:38:43 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Nov  5 16:26:15 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.7 2017/10/29 11:38:43 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.8 2017/11/05 16:26:15 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -96,7 +96,7 @@ mm_vatopa(vaddr_t va)
 	return (PTE_BASE[pl1_i(va)] & PG_FRAME);
 }
 
-void
+static void
 mm_mprotect(vaddr_t startva, size_t size, int prot)
 {
 	size_t i, npages;
@@ -114,6 +114,20 @@ mm_mprotect(vaddr_t startva, size_t size
 	}
 }
 
+void
+mm_bootspace_mprotect()
+{
+	/*
+	 * Remap the kernel segments with proper 

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

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 17:00:18 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Handle absolute symbols. Since my linux_sigcode.S::rev1.4 there are two
Elf_Rela that point to the NULL symbol - which the prekern thought was an
external reference.

In the ELF spec, STN_UNDEF means the value of the symbol is zero.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/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/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.5 src/sys/arch/amd64/stand/prekern/elf.c:1.6
--- src/sys/arch/amd64/stand/prekern/elf.c:1.5	Sun Oct 29 11:38:43 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Wed Nov  1 17:00:17 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.5 2017/10/29 11:38:43 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.6 2017/11/01 17:00:17 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -147,6 +147,10 @@ elf_sym_lookup(size_t symidx)
 	char *buf, *secname;
 	Elf_Shdr *sec;
 
+	if (symidx == STN_UNDEF) {
+		return 0;
+	}
+
 	if (symidx >= eif.symcnt) {
 		fatal("elf_sym_lookup: symbol beyond table");
 	}



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

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 09:47:53 UTC 2017

Modified Files:
src/sys/arch/amd64/acpi: acpi_wakeup_low.S

Log Message:
Use NENTRY -> END.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/amd64/acpi/acpi_wakeup_low.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/acpi/acpi_wakeup_low.S
diff -u src/sys/arch/amd64/acpi/acpi_wakeup_low.S:1.8 src/sys/arch/amd64/acpi/acpi_wakeup_low.S:1.9
--- src/sys/arch/amd64/acpi/acpi_wakeup_low.S:1.8	Sat Oct 21 06:55:54 2017
+++ src/sys/arch/amd64/acpi/acpi_wakeup_low.S	Wed Nov  1 09:47:53 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: acpi_wakeup_low.S,v 1.8 2017/10/21 06:55:54 maxv Exp $	*/
+/*	$NetBSD: acpi_wakeup_low.S,v 1.9 2017/11/01 09:47:53 maxv Exp $	*/
 
-/*-
+/*
  * Copyright (c) 2007 Joerg Sonnenberger 
  * Copyright (c) 2001 Takanori Watanabe 
  * Copyright (c) 2001 Mitsuru IWASAKI 
@@ -34,9 +34,9 @@
 #include 
 
 	.text
+
 	.p2align 2, 0x90
-	.globl acpi_md_sleep_exit
-acpi_md_sleep_exit:
+NENTRY(acpi_md_sleep_exit)
 	lgdt	ACPI_SUSPEND_GDT(%r8)
 
 	/* Reload fixed descriptors for new GDT */
@@ -110,11 +110,10 @@ acpi_md_sleep_exit:
 	pushq	ACPI_SUSPEND_REG+(7*8)(%r8)
 	popfq
 	ret
+END(acpi_md_sleep_exit)
 
 	.p2align 2, 0x90
-	.type acpi_md_sleep_prepare, @function
-	.globl acpi_md_sleep_prepare
-acpi_md_sleep_prepare:
+NENTRY(acpi_md_sleep_prepare)
 	movq	CPUVAR(SELF),%r8
 	movq	%rbx,ACPI_SUSPEND_REG+(1*8)(%r8)
 	movq	%rbp,ACPI_SUSPEND_REG+(2*8)(%r8)
@@ -167,3 +166,4 @@ acpi_md_sleep_prepare:
 	/* acpi_md_sleep_enter only returns on failure. */
 	movl	$-1,%eax
 	ret
+END(acpi_md_sleep_prepare)



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

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 09:38:43 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: linux32_sigcode.S linux_sigcode.S
netbsd32_sigcode.S

Log Message:
More END(). In linux_sigcode.S we only provide symbols, not defined as
functions.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/amd64/linux32_sigcode.S
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/amd64/linux_sigcode.S
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/amd64/netbsd32_sigcode.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/linux32_sigcode.S
diff -u src/sys/arch/amd64/amd64/linux32_sigcode.S:1.4 src/sys/arch/amd64/amd64/linux32_sigcode.S:1.5
--- src/sys/arch/amd64/amd64/linux32_sigcode.S:1.4	Mon Oct 30 17:06:42 2017
+++ src/sys/arch/amd64/amd64/linux32_sigcode.S	Wed Nov  1 09:38:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: linux32_sigcode.S,v 1.4 2017/10/30 17:06:42 maxv Exp $ */
+/*	$NetBSD: linux32_sigcode.S,v 1.5 2017/11/01 09:38:43 maxv Exp $ */
 
 #include "assym.h"
 #include 
@@ -6,7 +6,7 @@
 	.code32
 NENTRY(linux32_sigcode)
 	call	*LINUX32_SF_HANDLER(%esp)
-	leal	LINUX32_SF_SC(%esp),%ebx	# scp
+	leal	LINUX32_SF_SC(%esp),%ebx	/* scp */
 	pushl	%eax
 	movl	$LINUX32_SYS_sigreturn,%eax
 	int	$0x80
@@ -17,7 +17,7 @@ END(linux32_sigcode)
 	.balign	16
 NENTRY(linux32_rt_sigcode)
 	call	*LINUX32_RT_SF_HANDLER(%esp)
-	leal	LINUX32_RT_SF_UC(%esp),%ebx	# scp
+	leal	LINUX32_RT_SF_UC(%esp),%ebx	/* scp */
 	pushl	%eax
 	movl	$LINUX32_SYS_rt_sigreturn,%eax
 	int	$0x80
@@ -26,4 +26,5 @@ NENTRY(linux32_rt_sigcode)
 	.balign	16
 	.globl	_C_LABEL(linux32_esigcode)
 _C_LABEL(linux32_esigcode):
+END(linux32_rt_sigcode)
 

Index: src/sys/arch/amd64/amd64/linux_sigcode.S
diff -u src/sys/arch/amd64/amd64/linux_sigcode.S:1.3 src/sys/arch/amd64/amd64/linux_sigcode.S:1.4
--- src/sys/arch/amd64/amd64/linux_sigcode.S:1.3	Mon Oct 30 17:06:42 2017
+++ src/sys/arch/amd64/amd64/linux_sigcode.S	Wed Nov  1 09:38:43 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: linux_sigcode.S,v 1.3 2017/10/30 17:06:42 maxv Exp $ */
+/*	$NetBSD: linux_sigcode.S,v 1.4 2017/11/01 09:38:43 maxv Exp $ */
 
-/*-
+/*
  * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -34,16 +34,12 @@
 #include 
 
 /* 
- * The signal trampoline are not used on Linux/amd64: a
- * libc provided trampoline is always used.
- * We just provide the symbol so that the kernel builds.
+ * The signal trampoline are not used on Linux/amd64: a libc provided
+ * trampoline is always used. We just provide the symbol so that the kernel
+ * builds.
  */
 
-/* LINTSTUB: Var: char linux_sigcode[1], linux_esigcode[1]; */
-NENTRY(linux_sigcode)
-END(linux_sigcode)
+	.globl _C_LABEL(linux_sigcode), _C_LABEL(linux_esigcode)
+	.set _C_LABEL(linux_sigcode),0
+	.set _C_LABEL(linux_esigcode),0
 
-/* LINTSTUB: Var: char linux_rt_sigcode[1]; */
-NENTRY(linux_rt_sigcode)
-	.globl	_C_LABEL(linux_esigcode)
-_C_LABEL(linux_esigcode):

Index: src/sys/arch/amd64/amd64/netbsd32_sigcode.S
diff -u src/sys/arch/amd64/amd64/netbsd32_sigcode.S:1.7 src/sys/arch/amd64/amd64/netbsd32_sigcode.S:1.8
--- src/sys/arch/amd64/amd64/netbsd32_sigcode.S:1.7	Mon Apr 28 20:23:12 2008
+++ src/sys/arch/amd64/amd64/netbsd32_sigcode.S	Wed Nov  1 09:38:43 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: netbsd32_sigcode.S,v 1.7 2008/04/28 20:23:12 martin Exp $	*/
+/*	$NetBSD: netbsd32_sigcode.S,v 1.8 2017/11/01 09:38:43 maxv Exp $	*/
 
-/*-
+/*
  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
@@ -36,23 +36,22 @@
 /*
  * Signal trampoline for 1.6 compatibility; copied to top of user stack.
  */
-
-NENTRY(netbsd32_sigcode)
-
 	.code32
+NENTRY(netbsd32_sigcode)
 	/*
 	 * Handler has returned here as if we called it.  The sigcontext
 	 * is on the stack after the 3 args "we" pushed.
 	 */
-	leal	12(%esp),%eax		# get pointer to sigcontext
-	movl	%eax,4(%esp)		# put it in the argument slot
-	# fake return address already there
+	leal	12(%esp),%eax		/* get pointer to sigcontext */
+	movl	%eax,4(%esp)		/* put it in the argument slot */
+	/* fake return address already there */
 	movl	$SYS_compat_16___sigreturn14,%eax
-	int	$0x80	 		# enter kernel with args on stack
+	int	$0x80			/* enter kernel with args on stack */
 	movl	$SYS_exit,%eax
-	int	$0x80			# exit if sigreturn fails
+	int	$0x80			/* exit if sigreturn fails */
 	.globl	_C_LABEL(netbsd32_esigcode)
 _C_LABEL(netbsd32_esigcode):
+END(netbsd32_sigcode)
 
 /*
  * There is no NetBSD-1.6 compatibility for native code.



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

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 09:31:24 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: Makefile.amd64

Log Message:
Add linux_sigcode.o, otherwise it doesn't get rebuilt.


To generate a diff of this commit:
cvs rdiff -u -r1.59 -r1.60 src/sys/arch/amd64/conf/Makefile.amd64

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/Makefile.amd64
diff -u src/sys/arch/amd64/conf/Makefile.amd64:1.59 src/sys/arch/amd64/conf/Makefile.amd64:1.60
--- src/sys/arch/amd64/conf/Makefile.amd64:1.59	Sat Oct  7 10:16:47 2017
+++ src/sys/arch/amd64/conf/Makefile.amd64	Wed Nov  1 09:31:24 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.amd64,v 1.59 2017/10/07 10:16:47 maxv Exp $
+#	$NetBSD: Makefile.amd64,v 1.60 2017/11/01 09:31:24 maxv Exp $
 
 # Makefile for NetBSD
 #
@@ -77,7 +77,7 @@ KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscri
 locore.o machdep.o: Makefile
 
 acpi_wakeup_low.o amd64func.o busfunc.o cpufunc.o cpu_in_cksum.o: assym.h
-linux32_sigcode.o lock_stubs.o mptramp.o: assym.h
+linux_sigcode.o linux32_sigcode.o lock_stubs.o mptramp.o: assym.h
 netbsd32_sigcode.o: assym.h
 
 ##



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

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 09:17:28 UTC 2017

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

Log Message:
Don't fall through functions, explicitly jump instead. While here don't
call smap_enable twice (harmless), and add END() markers.


To generate a diff of this commit:
cvs rdiff -u -r1.27 -r1.28 src/sys/arch/amd64/amd64/copy.S
cvs rdiff -u -r1.30 -r1.31 src/sys/arch/amd64/amd64/cpufunc.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.27 src/sys/arch/amd64/amd64/copy.S:1.28
--- src/sys/arch/amd64/amd64/copy.S:1.27	Mon Oct 30 17:06:42 2017
+++ src/sys/arch/amd64/amd64/copy.S	Wed Nov  1 09:17:28 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: copy.S,v 1.27 2017/10/30 17:06:42 maxv Exp $	*/
+/*	$NetBSD: copy.S,v 1.28 2017/11/01 09:17:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -258,19 +258,17 @@ END(copyin)
 
 NENTRY(copy_efault)
 	movq	$EFAULT,%rax
-
-/*
- * kcopy_fault is used by kcopy and copy_fault is used by copyin/out.
- *
- * they're distinguished for lazy pmap switching.  see trap().
- */
+	ret
+END(copy_efault)
 
 NENTRY(kcopy_fault)
 	ret
+END(kcopy_fault)
 
 NENTRY(copy_fault)
 	callq	smap_enable
 	ret
+END(copy_fault)
 
 ENTRY(copyoutstr)
 	DEFERRED_SWITCH_CHECK
@@ -362,6 +360,8 @@ END(copyinstr)
 
 ENTRY(copystr_efault)
 	movl	$EFAULT,%eax
+	jmp	copystr_return
+END(copystr_efault)
 
 ENTRY(copystr_fault)
 	callq	smap_enable
@@ -371,8 +371,8 @@ copystr_return:
 	jz	8f
 	subq	%rdx,%r8
 	movq	%r8,(%r9)
-
 8:	ret
+END(copystr_fault)
 
 ENTRY(copystr)
 	xchgq	%rdi,%rsi
@@ -564,10 +564,13 @@ END(ucas_32)
 
 ENTRY(ucas_efault)
 	movq	$EFAULT,%rax
+	ret
+END(ucas_efault)
 
 NENTRY(ucas_fault)
 	callq	smap_enable
 	ret
+END(ucas_fault)
 
 /*
  * int	ucas_ptr(volatile void **uptr, void *old, void *new, void **ret);

Index: src/sys/arch/amd64/amd64/cpufunc.S
diff -u src/sys/arch/amd64/amd64/cpufunc.S:1.30 src/sys/arch/amd64/amd64/cpufunc.S:1.31
--- src/sys/arch/amd64/amd64/cpufunc.S:1.30	Mon Oct 30 17:06:42 2017
+++ src/sys/arch/amd64/amd64/cpufunc.S	Wed Nov  1 09:17:28 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: cpufunc.S,v 1.30 2017/10/30 17:06:42 maxv Exp $	*/
+/*	$NetBSD: cpufunc.S,v 1.31 2017/11/01 09:17:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2007, 2008 The NetBSD Foundation, Inc.
@@ -424,8 +424,7 @@ END(__byte_swap_u16_variable)
  * Load a new GDT pointer (and do any necessary cleanup).
  * XXX It's somewhat questionable whether reloading all the segment registers
  * is necessary, since the actual descriptor data is not changed except by
- * process creation and exit, both of which clean up via task switches.  OTOH,
- * this only happens at run time when the GDT is resized.
+ * process creation and exit, both of which clean up via task switches.
  */
 #ifndef XEN
 ENTRY(lgdt)
@@ -435,19 +434,21 @@ ENTRY(lgdt)
 	/* Flush the prefetch q. */
 	jmp	1f
 	nop
-1:	/* Reload "stale" selectors. */
-#else /* XEN */
+1:	jmp	_C_LABEL(lgdt_finish)
+END(lgdt)
+#endif
+
 /*
  * void lgdt_finish(void);
  * Reload segments after a GDT change
  */
 ENTRY(lgdt_finish)
-#endif /* XEN */
 	movl	$GSEL(GDATA_SEL, SEL_KPL),%eax
 	movl	%eax,%ds
 	movl	%eax,%es
 	movl	%eax,%ss
-	/* FALLTHROUGH */
+	jmp	_C_LABEL(x86_flush)
+END(lgdt_finish)
 
 /*
  * void x86_flush()



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

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 07:14:29 UTC 2017

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

Log Message:
Remove unused macros and LDT entries.


To generate a diff of this commit:
cvs rdiff -u -r1.31 -r1.32 src/sys/arch/amd64/include/segments.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/segments.h
diff -u src/sys/arch/amd64/include/segments.h:1.31 src/sys/arch/amd64/include/segments.h:1.32
--- src/sys/arch/amd64/include/segments.h:1.31	Sun Oct 15 12:49:53 2017
+++ src/sys/arch/amd64/include/segments.h	Wed Nov  1 07:14:29 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: segments.h,v 1.31 2017/10/15 12:49:53 maxv Exp $	*/
+/*	$NetBSD: segments.h,v 1.32 2017/11/01 07:14:29 maxv Exp $	*/
 
-/*-
+/*
  * Copyright (c) 1990 The Regents of the University of California.
  * All rights reserved.
  *
@@ -34,7 +34,7 @@
  *	@(#)segments.h	7.1 (Berkeley) 5/9/91
  */
 
-/*-
+/*
  * Copyright (c) 1995, 1997
  *	Charles M. Hannum.  All rights reserved.
  * Copyright (c) 1989, 1990 William F. Jolitz
@@ -297,27 +297,6 @@ void cpu_fsgs_reload(struct lwp *, int, 
 #define SDT_MEMERC	30	/* memory execute read conforming */
 #define SDT_MEMERAC	31	/* memory execute read accessed conforming */
 
-/* is memory segment descriptor pointer ? */
-#define ISMEMSDP(s)	((s->d_type) >= SDT_MEMRO && \
-			 (s->d_type) <= SDT_MEMERAC)
-
-/* is 286 gate descriptor pointer ? */
-#define IS286GDP(s)	((s->d_type) >= SDT_SYS286CGT && \
-			 (s->d_type) < SDT_SYS286TGT)
-
-/* is 386 gate descriptor pointer ? */
-#define IS386GDP(s)	((s->d_type) >= SDT_SYS386CGT && \
-			 (s->d_type) < SDT_SYS386TGT)
-
-/* is gate descriptor pointer ? */
-#define ISGDP(s)	(IS286GDP(s) || IS386GDP(s))
-
-/* is segment descriptor pointer ? */
-#define ISSDP(s)	(ISMEMSDP(s) || !ISGDP(s))
-
-/* is system segment descriptor pointer ? */
-#define ISSYSSDP(s)	(!ISMEMSDP(s) && !ISGDP(s))
-
 /*
  * Segment Protection Exception code bits
  */
@@ -365,24 +344,18 @@ void cpu_fsgs_reload(struct lwp *, int, 
 #define GDT_ADDR_MEM(s,i)	\
 ((struct mem_segment_descriptor *)((s) + ((i) << 3)))
 #define GDT_ADDR_SYS(s,i)	\
-   ((struct sys_segment_descriptor *)((s) + (((i) << 4) + SYSSEL_START)))
+((struct sys_segment_descriptor *)((s) + (((i) << 4) + SYSSEL_START)))
 
 /*
  * Byte offsets in the Local Descriptor Table (LDT)
  * Strange order because of syscall/sysret insns
  */
-#define LSYS5CALLS_SEL	0	/* iBCS system call gate */
-/*			8	   second half */
-#define LSOL26CALLS_SEL	32	/* Solaris 2.6 system call gate */
-/*			40	   second half */
 #define LUCODE32_SEL	48	/* 32 bit user code descriptor */
 #define LUDATA_SEL	56	/* User data descriptor */
 #define LUCODE_SEL	64	/* User code descriptor */
 #define LUDATA32_SEL	72	/* 32 bit user data descriptor (needed?)*/
-#define LBSDICALLS_SEL	128	/* BSDI system call gate */
-/*			136	   second half */
 
-#define LDT_SIZE	144
+#define LDT_SIZE	80
 
 #define LSYSRETBASE_SEL	LUCODE32_SEL
 



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

2017-10-30 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Oct 30 17:06:42 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: copy.S cpufunc.S linux32_sigcode.S
linux_sigcode.S vector.S

Log Message:
Always use END() markers when declaring functions in assembly, so that ld
can compute the size of the functions. A few remain.

While here, fix a bug in the INTRSTUB macro: we are falling through
resume_, but it is aligned, so it looks like we're executing the inter-
function padding - which probably happens to contain NOPs, but that's
still bad.


To generate a diff of this commit:
cvs rdiff -u -r1.26 -r1.27 src/sys/arch/amd64/amd64/copy.S
cvs rdiff -u -r1.29 -r1.30 src/sys/arch/amd64/amd64/cpufunc.S
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/amd64/linux32_sigcode.S
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/amd64/linux_sigcode.S
cvs rdiff -u -r1.51 -r1.52 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/copy.S
diff -u src/sys/arch/amd64/amd64/copy.S:1.26 src/sys/arch/amd64/amd64/copy.S:1.27
--- src/sys/arch/amd64/amd64/copy.S:1.26	Tue Oct 17 07:02:50 2017
+++ src/sys/arch/amd64/amd64/copy.S	Mon Oct 30 17:06:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: copy.S,v 1.26 2017/10/17 07:02:50 maxv Exp $	*/
+/*	$NetBSD: copy.S,v 1.27 2017/10/30 17:06:42 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -105,6 +105,7 @@ ENTRY(do_pmap_load)
 	popq	%rdi
 	leaveq
 	ret
+END(do_pmap_load)
 
 /*
  * SMAP functions. ret+int3+int3 is patched dynamically to STAC/CLAC.
@@ -116,6 +117,7 @@ ENTRY(smap_enable)
 	int3
 	int3
 	ret
+END(smap_enable)
 
 ENTRY(smap_disable)
 .Lstacpatch:
@@ -123,6 +125,7 @@ ENTRY(smap_disable)
 	int3
 	int3
 	ret
+END(smap_disable)
 
 /*
  * Copy routines from and to userland, plus a few more. See the
@@ -190,6 +193,7 @@ ENTRY(kcopy)
 .Lkcopy_end:
 	xorq	%rax,%rax
 	ret
+END(kcopy)
 
 ENTRY(copyout)
 	DEFERRED_SWITCH_CHECK
@@ -219,6 +223,7 @@ ENTRY(copyout)
 	xorl	%eax,%eax
 	ret
 	DEFERRED_SWITCH_CALL
+END(copyout)
 
 ENTRY(copyin)
 	DEFERRED_SWITCH_CHECK
@@ -249,6 +254,7 @@ ENTRY(copyin)
 	xorl	%eax,%eax
 	ret
 	DEFERRED_SWITCH_CALL
+END(copyin)
 
 NENTRY(copy_efault)
 	movq	$EFAULT,%rax
@@ -308,6 +314,7 @@ ENTRY(copyoutstr)
 	movq	$ENAMETOOLONG,%rax
 	jmp	copystr_return
 	DEFERRED_SWITCH_CALL
+END(copyoutstr)
 
 ENTRY(copyinstr)
 	DEFERRED_SWITCH_CHECK
@@ -351,6 +358,7 @@ ENTRY(copyinstr)
 	movq	$ENAMETOOLONG,%rax
 	jmp	copystr_return
 	DEFERRED_SWITCH_CALL
+END(copyinstr)
 
 ENTRY(copystr_efault)
 	movl	$EFAULT,%eax
@@ -394,7 +402,7 @@ ENTRY(copystr)
 	movq	%r8,(%rcx)
 
 7:	ret
-
+END(copystr)
 
 ENTRY(fuswintr)
 	cmpl	$TLBSTATE_VALID,CPUVAR(TLBSTATE)
@@ -412,6 +420,7 @@ ENTRY(fuswintr)
 
 	movq	$0,PCB_ONFAULT(%rcx)
 	ret
+END(fuswintr)
 
 ENTRY(fubyte)
 	DEFERRED_SWITCH_CHECK
@@ -429,6 +438,7 @@ ENTRY(fubyte)
 	movq	$0,PCB_ONFAULT(%rcx)
 	ret
 	DEFERRED_SWITCH_CALL
+END(fubyte)
 
 ENTRY(suswintr)
 	cmpl	$TLBSTATE_VALID,CPUVAR(TLBSTATE)
@@ -447,6 +457,7 @@ ENTRY(suswintr)
 	xorq	%rax,%rax
 	movq	%rax,PCB_ONFAULT(%rcx)
 	ret
+END(suswintr)
 
 ENTRY(subyte)
 	DEFERRED_SWITCH_CHECK
@@ -466,6 +477,7 @@ ENTRY(subyte)
 	movq	%rax,PCB_ONFAULT(%rcx)
 	ret
 	DEFERRED_SWITCH_CALL
+END(subyte)
 
 /*
  * These are the same, but must reside at different addresses,
@@ -476,16 +488,19 @@ ENTRY(fusuintrfailure)
 	movq	$0,PCB_ONFAULT(%rcx)
 	movl	$-1,%eax
 	ret
+END(fusuintrfailure)
 
 ENTRY(fusufailure)
 	callq	smap_enable
 	movq	$0,PCB_ONFAULT(%rcx)
 	movl	$-1,%eax
 	ret
+END(fusufailure)
 
 ENTRY(fusuaddrfault)
 	movl	$-1,%eax
 	ret
+END(fusuaddrfault)
 
 /*
  * Compare-and-swap the 64-bit integer in the user-space.
@@ -516,6 +531,7 @@ ENTRY(ucas_64)
 	xorq	%rax,%rax
 	ret
 	DEFERRED_SWITCH_CALL
+END(ucas_64)
 
 /*
  * int	ucas_32(volatile int32_t *uptr, int32_t old, int32_t new, int32_t *ret);
@@ -544,6 +560,7 @@ ENTRY(ucas_32)
 	xorq	%rax,%rax
 	ret
 	DEFERRED_SWITCH_CALL
+END(ucas_32)
 
 ENTRY(ucas_efault)
 	movq	$EFAULT,%rax

Index: src/sys/arch/amd64/amd64/cpufunc.S
diff -u src/sys/arch/amd64/amd64/cpufunc.S:1.29 src/sys/arch/amd64/amd64/cpufunc.S:1.30
--- src/sys/arch/amd64/amd64/cpufunc.S:1.29	Sun Oct 15 11:31:00 2017
+++ src/sys/arch/amd64/amd64/cpufunc.S	Mon Oct 30 17:06:42 2017
@@ -1,6 +1,6 @@
-/*	$NetBSD: cpufunc.S,v 1.29 2017/10/15 11:31:00 maxv Exp $	*/
+/*	$NetBSD: cpufunc.S,v 1.30 2017/10/30 17:06:42 maxv Exp $	*/
 
-/*-
+/*
  * Copyright (c) 1998, 2007, 2008 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
@@ -51,23 +51,28 @@
 ENTRY(x86_lfence)
 	lfence
 	ret
+END(x86_lfence)
 
 ENTRY(x86_sfence)
 	sfence
 	ret
+END(x86_sfence)
 
 ENTRY(x86_mfence)
 	mfence
 	ret
+END(x86_mfence)
 
 #ifndef XEN
 ENTRY(invlpg)
 	invlpg	(%rdi)
 	ret
+END(invlpg)
 
 ENTRY(lidt)
 	lidt	(%rdi)
 	ret
+END(lidt)
 
 ENTRY(lldt)
 	cmpl	%edi, CPUVAR(CURLDT)
@@ -77,51 +82,63 @@ ENTRY(lldt)
 	movl	%edi, CPUVAR(CURLDT)
 	lldt	%di
 	ret

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

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 17:19:14 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: GENERIC_KASLR

Log Message:
Mmh, we don't map the CTF section on kaslr kernels, so disable
KDTRACE_HOOKS for now.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/conf/GENERIC_KASLR

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/GENERIC_KASLR
diff -u src/sys/arch/amd64/conf/GENERIC_KASLR:1.2 src/sys/arch/amd64/conf/GENERIC_KASLR:1.3
--- src/sys/arch/amd64/conf/GENERIC_KASLR:1.2	Sun Oct  8 09:06:50 2017
+++ src/sys/arch/amd64/conf/GENERIC_KASLR	Sun Oct 29 17:19:14 2017
@@ -1,6 +1,8 @@
-# $NetBSD: GENERIC_KASLR,v 1.2 2017/10/08 09:06:50 maxv Exp $
+# $NetBSD: GENERIC_KASLR,v 1.3 2017/10/29 17:19:14 maxv Exp $
 
 include "arch/amd64/conf/GENERIC"
 
+no options	KDTRACE_HOOKS
+
 makeoptions 	KASLR=1		# Kernel ASLR
 options 	KASLR



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

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 11:38:43 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c

Log Message:
Fix a few error messages, and be a little more verbose.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.4 src/sys/arch/amd64/stand/prekern/elf.c:1.5
--- src/sys/arch/amd64/stand/prekern/elf.c:1.4	Sun Oct 29 11:28:30 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Oct 29 11:38:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.4 2017/10/29 11:28:30 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.5 2017/10/29 11:38:43 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -285,7 +285,7 @@ elf_build_head(vaddr_t headva)
 	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
 
 	if (elf_check_header() == -1) {
-		fatal("elf_build_info: wrong kernel ELF header");
+		fatal("elf_build_head: wrong kernel ELF header");
 	}
 }
 
@@ -508,10 +508,10 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	/* Locate the section names */
 	j = eif.ehdr->e_shstrndx;
 	if (j == SHN_UNDEF) {
-		fatal("elf_build_info: shstrtab not found");
+		fatal("elf_build_boot: shstrtab not found");
 	}
 	if (j >= eif.ehdr->e_shnum) {
-		fatal("elf_build_info: wrong shstrtab index");
+		fatal("elf_build_boot: wrong shstrtab index");
 	}
 	eif.shstrtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.shstrsz = eif.shdr[j].sh_size;
@@ -522,7 +522,7 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 			break;
 	}
 	if (i == eif.ehdr->e_shnum) {
-		fatal("elf_build_info: symtab not found");
+		fatal("elf_build_boot: symtab not found");
 	}
 	eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 	eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
@@ -530,10 +530,10 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	/* Also locate the string table */
 	j = eif.shdr[i].sh_link;
 	if (j == SHN_UNDEF || j >= eif.ehdr->e_shnum) {
-		fatal("elf_build_info: wrong strtab index");
+		fatal("elf_build_boot: wrong strtab index");
 	}
 	if (eif.shdr[j].sh_type != SHT_STRTAB) {
-		fatal("elf_build_info: wrong strtab type");
+		fatal("elf_build_boot: wrong strtab type");
 	}
 	eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.strsz = eif.shdr[j].sh_size;

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.6 src/sys/arch/amd64/stand/prekern/mm.c:1.7
--- src/sys/arch/amd64/stand/prekern/mm.c:1.6	Sun Oct 29 11:28:30 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Oct 29 11:38:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.6 2017/10/29 11:28:30 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.7 2017/10/29 11:38:43 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -385,7 +385,10 @@ mm_map_kernel()
 {
 	memset(, 0, sizeof(bootspace));
 	mm_map_head();
+	print_state(true, "Head region mapped");
 	mm_map_segments();
+	print_state(true, "Segments mapped");
 	mm_map_boot();
+	print_state(true, "Boot region mapped");
 }
 



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

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 11:28:30 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c locore.S mm.c prekern.c
prekern.h

Log Message:
Randomize the kernel segments independently. That is to say, put text,
rodata and data at different addresses (and in a random order).

To achieve that, the mapping order in the prekern is changed. Until now,
we were creating the kernel map the following way:
-> choose a random VA
-> map [kernpa_start; kernpa_end[ at this VA
-> parse the ELF structures from there
-> determine where exactly the kernel segments are located
-> relocate etc
Now, we are doing:
-> create a read-only view of [kernpa_start; kernpa_end[
-> from this view, compute the size of the "head" region
-> choose a random VA in the HEAD window, and map the head there
-> for each region in (text, rodata, data, boot)
-> compute the size of the region from the RO view
-> choose a random VA in the KASLR window
-> map the region there
-> relocate etc

Each time we map a region, we initialize its bootspace fields right away.

The "head" region must be put before the other regions in memory, because
the kernel uses (headva + sh_offset) to get the addresses of the symbols,
and the offset is unsigned.

Given that the head does not have an mcmodel constraint, its location is
randomized in a window located below the KASLR window.

The rest of the regions being in the same window, we need to detect
collisions.

Note that the module map is embedded in the "boot" region, and that
therefore its location is randomized too.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/locore.S \
src/sys/arch/amd64/stand/prekern/prekern.c \
src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.3 src/sys/arch/amd64/stand/prekern/elf.c:1.4
--- src/sys/arch/amd64/stand/prekern/elf.c:1.3	Sun Oct 29 10:07:08 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Oct 29 11:28:30 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.3 2017/10/29 10:07:08 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.4 2017/10/29 11:28:30 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -56,6 +56,8 @@ struct elfinfo {
 	} data;
 };
 
+extern paddr_t kernpa_start, kernpa_end;
+
 static struct elfinfo eif;
 static const char entrypoint[] = "start_prekern";
 
@@ -256,6 +258,37 @@ elf_apply_reloc(uintptr_t relocbase, con
 	}
 }
 
+/* -- */
+
+size_t
+elf_get_head_size(vaddr_t headva)
+{
+	Elf_Ehdr *ehdr;
+	Elf_Shdr *shdr;
+	size_t size;
+
+	ehdr = (Elf_Ehdr *)headva;
+	shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
+
+	size = (vaddr_t)shdr + (vaddr_t)(ehdr->e_shnum * sizeof(Elf_Shdr)) -
+	(vaddr_t)ehdr;
+
+	return roundup(size, PAGE_SIZE);
+}
+
+void
+elf_build_head(vaddr_t headva)
+{
+	memset(, 0, sizeof(struct elfinfo));
+
+	eif.ehdr = (Elf_Ehdr *)headva;
+	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
+
+	if (elf_check_header() == -1) {
+		fatal("elf_build_info: wrong kernel ELF header");
+	}
+}
+
 static bool
 elf_section_is_text(Elf_Shdr *shdr)
 {
@@ -296,20 +329,180 @@ elf_section_is_data(Elf_Shdr *shdr)
 	return true;
 }
 
-static void
-elf_build_info(vaddr_t baseva)
+void
+elf_get_text(paddr_t *pa, size_t *sz)
 {
-	vaddr_t secva, minva, maxva;
-	size_t secsz;
-	size_t i, j;
+	const paddr_t basepa = kernpa_start;
+	paddr_t minpa, maxpa, secpa;
+	size_t i, secsz;
 
-	memset(, 0, sizeof(struct elfinfo));
+	minpa = 0x, maxpa = 0;
+	for (i = 0; i < eif.ehdr->e_shnum; i++) {
+		if (!elf_section_is_text([i])) {
+			continue;
+		}
+		secpa = basepa + eif.shdr[i].sh_offset;
+		secsz = eif.shdr[i].sh_size;
+		if (secpa < minpa) {
+			minpa = secpa;
+		}
+		if (secpa + secsz > maxpa) {
+			maxpa = secpa + secsz;
+		}
+	}
+	ASSERT(minpa % PAGE_SIZE == 0);
 
-	eif.ehdr = (Elf_Ehdr *)baseva;
-	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
+	*pa = minpa;
+	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+}
 
-	if (elf_check_header() == -1) {
-		fatal("elf_build_info: wrong kernel ELF header");
+void
+elf_build_text(vaddr_t textva, paddr_t textpa, size_t textsz)
+{
+	const paddr_t basepa = kernpa_start;
+	const vaddr_t headva = (vaddr_t)eif.ehdr;
+	size_t i, offtext;
+
+	eif.text.va = textva;
+	eif.text.sz = textsz;
+
+	for (i = 0; i < eif.ehdr->e_shnum; i++) {
+		if (!elf_section_is_text([i])) {
+			continue;
+		}
+
+		/* Offset of 

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

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 10:25:28 UTC 2017

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

Log Message:
Use bootspace.head.va instead of the direct map. Otherwise there's the
assumption that the offsets contained in sh_offset in physical memory are
equal to the offsets in virtual memory, which won't be true in the future.


To generate a diff of this commit:
cvs rdiff -u -r1.273 -r1.274 src/sys/arch/amd64/amd64/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.273 src/sys/arch/amd64/amd64/machdep.c:1.274
--- src/sys/arch/amd64/amd64/machdep.c:1.273	Sun Oct 29 10:01:21 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Sun Oct 29 10:25:28 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.273 2017/10/29 10:01:21 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.274 2017/10/29 10:25:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.273 2017/10/29 10:01:21 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.274 2017/10/29 10:25:28 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1481,8 +1481,8 @@ init_x86_64_ksyms(void)
 	symtab = lookup_bootinfo(BTINFO_SYMTAB);
 	if (symtab) {
 #ifdef KASLR
-		tssym = PMAP_DIRECT_MAP((paddr_t)symtab->ssym);
-		tesym = PMAP_DIRECT_MAP((paddr_t)symtab->esym);
+		tssym = bootspace.head.va;
+		tesym = bootspace.head.va; /* (unused...) */
 #else
 		tssym = (vaddr_t)symtab->ssym + KERNBASE;
 		tesym = (vaddr_t)symtab->esym + KERNBASE;



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

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 10:07:08 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Add three functions and start using them; will be more useful soon.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/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/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.2 src/sys/arch/amd64/stand/prekern/elf.c:1.3
--- src/sys/arch/amd64/stand/prekern/elf.c:1.2	Wed Oct 11 16:21:06 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Oct 29 10:07:08 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.2 2017/10/11 16:21:06 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.3 2017/10/29 10:07:08 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -256,6 +256,46 @@ elf_apply_reloc(uintptr_t relocbase, con
 	}
 }
 
+static bool
+elf_section_is_text(Elf_Shdr *shdr)
+{
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	if (!(shdr->sh_flags & SHF_EXECINSTR)) {
+		return false;
+	}
+	return true;
+}
+
+static bool
+elf_section_is_rodata(Elf_Shdr *shdr)
+{
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	if (shdr->sh_flags & (SHF_EXECINSTR|SHF_WRITE)) {
+		return false;
+	}
+	return true;
+}
+
+static bool
+elf_section_is_data(Elf_Shdr *shdr)
+{
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	if (!(shdr->sh_flags & SHF_WRITE) ||
+	(shdr->sh_flags & SHF_EXECINSTR)) {
+		return false;
+	}
+	return true;
+}
+
 static void
 elf_build_info(vaddr_t baseva)
 {
@@ -314,11 +354,7 @@ elf_build_info(vaddr_t baseva)
 	/* text */
 	minva = 0x, maxva = 0;
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
-			continue;
-		}
-		if (!(eif.shdr[i].sh_flags & SHF_EXECINSTR)) {
+		if (!elf_section_is_text([i])) {
 			continue;
 		}
 		secva = baseva + eif.shdr[i].sh_offset;
@@ -337,11 +373,7 @@ elf_build_info(vaddr_t baseva)
 	/* rodata */
 	minva = 0x, maxva = 0;
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
-			continue;
-		}
-		if ((eif.shdr[i].sh_flags & (SHF_EXECINSTR|SHF_WRITE))) {
+		if (!elf_section_is_rodata([i])) {
 			continue;
 		}
 		secva = baseva + eif.shdr[i].sh_offset;
@@ -360,12 +392,7 @@ elf_build_info(vaddr_t baseva)
 	/* data */
 	minva = 0x, maxva = 0;
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
-			continue;
-		}
-		if (!(eif.shdr[i].sh_flags & SHF_WRITE) ||
-		(eif.shdr[i].sh_flags & SHF_EXECINSTR)) {
+		if (!elf_section_is_data([i])) {
 			continue;
 		}
 		secva = baseva + eif.shdr[i].sh_offset;



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

2017-10-28 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sat Oct 28 20:57:17 UTC 2017

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

Log Message:
Use FLAT_RING3_CS64 (defined in Xen public headers) instead of numeric
value.


To generate a diff of this commit:
cvs rdiff -u -r1.61 -r1.62 src/sys/arch/amd64/amd64/genassym.cf
cvs rdiff -u -r1.139 -r1.140 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.61 src/sys/arch/amd64/amd64/genassym.cf:1.62
--- src/sys/arch/amd64/amd64/genassym.cf:1.61	Sun Jul 16 14:02:48 2017
+++ src/sys/arch/amd64/amd64/genassym.cf	Sat Oct 28 20:57:17 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: genassym.cf,v 1.61 2017/07/16 14:02:48 cherry Exp $
+#	$NetBSD: genassym.cf,v 1.62 2017/10/28 20:57:17 bouyer Exp $
 
 #
 # Copyright (c) 1998, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -349,6 +349,7 @@ define EVTCHN_UPCALL_MASK	offsetof(struc
 define XEN_PT_BASE		offsetof(struct start_info, pt_base)
 define XEN_NR_PT_FRAMES		offsetof(struct start_info, nr_pt_frames)
 define __HYPERVISOR_iret	__HYPERVISOR_iret
+define FLAT_RING3_CS64		FLAT_RING3_CS64
 endif
 
 define	NKL4_KIMG_ENTRIES	NKL4_KIMG_ENTRIES

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.139 src/sys/arch/amd64/amd64/locore.S:1.140
--- src/sys/arch/amd64/amd64/locore.S:1.139	Sat Oct 28 20:06:31 2017
+++ src/sys/arch/amd64/amd64/locore.S	Sat Oct 28 20:57:17 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.139 2017/10/28 20:06:31 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.140 2017/10/28 20:57:17 bouyer Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1484,7 +1484,7 @@ ENTRY(intrfastexit)
 	cmpw	$GSEL(GUCODE_SEL, SEL_UPL),TF_CS(%rsp)
 	je	.Luexit64
 #ifdef XEN
-	cmpw	$0xe033,TF_CS(%rsp)
+	cmpw	$FLAT_RING3_CS64,TF_CS(%rsp)
 	je	.Luexit64
 #endif
 



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

2017-10-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct 28 20:06:31 UTC 2017

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

Log Message:
It appears that Xen remaps the userland %cs to 0xE033. So add it to the
checklist. Otherwise we're going through Luexit32: %fs gets reloaded,
which sets the FS.base to NULL, which will cause the thread to page-fault
next time it accesses its TLS (as seen in PR/52662).

This fix is not very clean, and it would be nice to understand why Xen
remaps %cs. But I'm committing it now anyway, so that people can test.


To generate a diff of this commit:
cvs rdiff -u -r1.138 -r1.139 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.138 src/sys/arch/amd64/amd64/locore.S:1.139
--- src/sys/arch/amd64/amd64/locore.S:1.138	Sat Oct 21 08:08:26 2017
+++ src/sys/arch/amd64/amd64/locore.S	Sat Oct 28 20:06:31 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.138 2017/10/21 08:08:26 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.139 2017/10/28 20:06:31 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1483,6 +1483,10 @@ ENTRY(intrfastexit)
 	je	.Luexit64
 	cmpw	$GSEL(GUCODE_SEL, SEL_UPL),TF_CS(%rsp)
 	je	.Luexit64
+#ifdef XEN
+	cmpw	$0xe033,TF_CS(%rsp)
+	je	.Luexit64
+#endif
 
 .Luexit32:
 	NOT_XEN(cli;)



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

2017-10-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct 28 19:28:11 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Fix a mistake I made in the very first revision. The calculation of the
number of slots was incorrect in some cases, and it could cause the
prekern to fault right away at boot time, or the kernel to fault when
loading kernel modules near the end of the module map.

The variables are divided by PAGE_SIZE to prevent integer overflows.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.4 src/sys/arch/amd64/stand/prekern/mm.c:1.5
--- src/sys/arch/amd64/stand/prekern/mm.c:1.4	Mon Oct 23 06:00:59 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sat Oct 28 19:28:11 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.4 2017/10/23 06:00:59 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.5 2017/10/28 19:28:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -112,20 +112,28 @@ mm_mprotect(vaddr_t startva, size_t size
 	}
 }
 
+static size_t
+mm_nentries_range(vaddr_t startva, vaddr_t endva, size_t pgsz)
+{
+	size_t npages;
+
+	npages = roundup((endva / PAGE_SIZE), (pgsz / PAGE_SIZE)) -
+	rounddown((startva / PAGE_SIZE), (pgsz / PAGE_SIZE));
+	return (npages / (pgsz / PAGE_SIZE));
+}
+
 static void
 mm_map_tree(vaddr_t startva, vaddr_t endva)
 {
-	size_t i, size, nL4e, nL3e, nL2e;
+	size_t i, nL4e, nL3e, nL2e;
 	size_t L4e_idx, L3e_idx, L2e_idx;
 	paddr_t pa;
 
-	size = endva - startva;
-
 	/*
 	 * Build L4.
 	 */
 	L4e_idx = pl4_i(startva);
-	nL4e = roundup(size, NBPD_L4) / NBPD_L4;
+	nL4e = mm_nentries_range(startva, endva, NBPD_L4);
 	ASSERT(L4e_idx == 511);
 	ASSERT(nL4e == 1);
 	if (!mm_pte_is_valid(L4_BASE[L4e_idx])) {
@@ -137,7 +145,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	 * Build L3.
 	 */
 	L3e_idx = pl3_i(startva);
-	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
+	nL3e = mm_nentries_range(startva, endva, NBPD_L3);
 	for (i = 0; i < nL3e; i++) {
 		if (mm_pte_is_valid(L3_BASE[L3e_idx+i])) {
 			continue;
@@ -150,7 +158,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	 * Build L2.
 	 */
 	L2e_idx = pl2_i(startva);
-	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
+	nL2e = mm_nentries_range(startva, endva, NBPD_L2);
 	for (i = 0; i < nL2e; i++) {
 		if (mm_pte_is_valid(L2_BASE[L2e_idx+i])) {
 			continue;



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

2017-10-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct 21 08:08:26 UTC 2017

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

Log Message:
Use labels instead of disassembling *(%rip). intrfastexit is now the
only place where the segregs can fault.


To generate a diff of this commit:
cvs rdiff -u -r1.137 -r1.138 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.103 -r1.104 src/sys/arch/amd64/amd64/trap.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.137 src/sys/arch/amd64/amd64/locore.S:1.138
--- src/sys/arch/amd64/amd64/locore.S:1.137	Sat Oct 21 06:55:54 2017
+++ src/sys/arch/amd64/amd64/locore.S	Sat Oct 21 08:08:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.137 2017/10/21 06:55:54 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.138 2017/10/21 08:08:26 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -330,6 +330,10 @@
 	.globl	_C_LABEL(biosextmem)
 	.globl	_C_LABEL(lwp0uarea)
 	.globl	do_sysret
+	.globl	do_mov_es
+	.globl	do_mov_ds
+	.globl	do_mov_fs
+	.globl	do_mov_gs
 	.globl	do_iret
 
 	.type	_C_LABEL(tablesize), @object
@@ -1196,7 +1200,7 @@ lwp_32bit:
 	movq	PCB_GS(%r14),%rax
 	movq	%rax,(GUGS_SEL*8)(%rcx)
 
-	/* Set default 32bit values in %ds, %es, %fs and %gs. */
+	/* Set default 32bit values in %ds, %es and %fs. %gs is special. */
 	movq	L_MD_REGS(%r12),%rbx
 	movq	$GSEL(GUDATA32_SEL, SEL_UPL),%rax
 	movw	%ax,%ds
@@ -1482,11 +1486,15 @@ ENTRY(intrfastexit)
 
 .Luexit32:
 	NOT_XEN(cli;)
+do_mov_es:
 	movw	TF_ES(%rsp),%es
+do_mov_ds:
 	movw	TF_DS(%rsp),%ds
+do_mov_fs:
 	movw	TF_FS(%rsp),%fs
 	SWAPGS
 #ifndef XEN
+do_mov_gs:
 	movw	TF_GS(%rsp),%gs
 #endif
 	jmp	.Lkexit

Index: src/sys/arch/amd64/amd64/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.103 src/sys/arch/amd64/amd64/trap.c:1.104
--- src/sys/arch/amd64/amd64/trap.c:1.103	Sat Oct 21 07:23:22 2017
+++ src/sys/arch/amd64/amd64/trap.c	Sat Oct 21 08:08:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.103 2017/10/21 07:23:22 maxv Exp $	*/
+/*	$NetBSD: trap.c,v 1.104 2017/10/21 08:08:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.103 2017/10/21 07:23:22 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.104 2017/10/21 08:08:26 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -236,6 +236,8 @@ static void trap_user_kernelmode(struct 
 static void
 trap_user_kernelmode(struct trapframe *frame, int type, lwp_t *l, proc_t *p)
 {
+	extern uint64_t do_mov_es, do_mov_ds, do_mov_fs, do_mov_gs;
+	extern uint64_t do_iret;
 	struct trapframe *vframe;
 	ksiginfo_t ksi;
 
@@ -260,8 +262,7 @@ trap_user_kernelmode(struct trapframe *f
 	 */
 	vframe = (void *)frame->tf_rsp;
 
-	switch (*(uint16_t *)frame->tf_rip) {
-	case 0xcf48:	/* iretq */
+	if (frame->tf_rip == (uint64_t)_iret) {
 		/*
 		 * The 'iretq' instruction faulted, so we have the
 		 * 'user' registers saved after the kernel
@@ -277,12 +278,10 @@ trap_user_kernelmode(struct trapframe *f
 		memmove(vframe, frame, offsetof(struct trapframe, tf_rip));
 		/* Set the faulting address to the user %rip */
 		ksi.ksi_addr = (void *)vframe->tf_rip;
-		break;
-
-	case 0x848e:	/* mov 0xa8(%rsp),%es (8e 84 24 a8 00 00 00) */
-	case 0x9c8e:	/* mov 0xb0(%rsp),%ds (8e 9c 24 b0 00 00 00) */
-	case 0xa48e:	/* mov 0xa0(%rsp),%fs (8e a4 24 a0 00 00 00) */
-	case 0xac8e:	/* mov 0x98(%rsp),%gs (8e ac 24 98 00 00 00) */
+	} else if (frame->tf_rip == (uint64_t)_mov_es ||
+	frame->tf_rip == (uint64_t)_mov_ds ||
+	frame->tf_rip == (uint64_t)_mov_fs ||
+	frame->tf_rip == (uint64_t)_mov_gs) {
 		/*
 		 * We faulted loading one of the user segment registers.
 		 * The stack frame containing the user registers is
@@ -291,9 +290,7 @@ trap_user_kernelmode(struct trapframe *f
 		if (KERNELMODE(vframe->tf_cs))
 			return;
 		/* There is no valid address for the fault */
-		break;
-
-	default:
+	} else {
 		return;
 	}
 



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

2017-10-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct 21 07:23:22 UTC 2017

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

Log Message:
Handle by default.


To generate a diff of this commit:
cvs rdiff -u -r1.102 -r1.103 src/sys/arch/amd64/amd64/trap.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.102 src/sys/arch/amd64/amd64/trap.c:1.103
--- src/sys/arch/amd64/amd64/trap.c:1.102	Tue Oct 17 06:58:15 2017
+++ src/sys/arch/amd64/amd64/trap.c	Sat Oct 21 07:23:22 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.102 2017/10/17 06:58:15 maxv Exp $	*/
+/*	$NetBSD: trap.c,v 1.103 2017/10/21 07:23:22 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.102 2017/10/17 06:58:15 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.103 2017/10/21 07:23:22 maxv Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -281,10 +281,8 @@ trap_user_kernelmode(struct trapframe *f
 
 	case 0x848e:	/* mov 0xa8(%rsp),%es (8e 84 24 a8 00 00 00) */
 	case 0x9c8e:	/* mov 0xb0(%rsp),%ds (8e 9c 24 b0 00 00 00) */
-#ifdef USER_LDT
 	case 0xa48e:	/* mov 0xa0(%rsp),%fs (8e a4 24 a0 00 00 00) */
 	case 0xac8e:	/* mov 0x98(%rsp),%gs (8e ac 24 98 00 00 00) */
-#endif
 		/*
 		 * We faulted loading one of the user segment registers.
 		 * The stack frame containing the user registers is



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

2017-10-19 Thread Jared D. McNeill
Module Name:src
Committed By:   jmcneill
Date:   Thu Oct 19 23:59:56 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: GENERIC

Log Message:
add bwfm


To generate a diff of this commit:
cvs rdiff -u -r1.466 -r1.467 src/sys/arch/amd64/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/amd64/conf/GENERIC
diff -u src/sys/arch/amd64/conf/GENERIC:1.466 src/sys/arch/amd64/conf/GENERIC:1.467
--- src/sys/arch/amd64/conf/GENERIC:1.466	Thu Sep 14 07:58:39 2017
+++ src/sys/arch/amd64/conf/GENERIC	Thu Oct 19 23:59:56 2017
@@ -1,4 +1,4 @@
-# $NetBSD: GENERIC,v 1.466 2017/09/14 07:58:39 mrg Exp $
+# $NetBSD: GENERIC,v 1.467 2017/10/19 23:59:56 jmcneill Exp $
 #
 # GENERIC machine description file
 #
@@ -22,7 +22,7 @@ include 	"arch/amd64/conf/std.amd64"
 
 options 	INCLUDE_CONFIG_FILE	# embed config file in kernel binary
 
-#ident		"GENERIC-$Revision: 1.466 $"
+#ident		"GENERIC-$Revision: 1.467 $"
 
 maxusers	64		# estimated number of users
 
@@ -1025,6 +1025,7 @@ run*	at uhub? port ?		# Ralink Technolog
 urtw*	at uhub? port ?		# Realtek RTL8187/RTL8187B 802.11b/g
 urtwn*	at uhub? port ?		# Realtek RTL8188CU/RTL8192CU 802.11b/g/n
 zyd*	at uhub? port ?		# Zydas ZD1211
+bwfm*	at uhub? port ?		# Broadcom FullMAC
 
 # USB scanners that use SCSI emulation, e.g., HP5300
 usscanner* at uhub? port ?



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

2017-10-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Oct 19 20:27:12 UTC 2017

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

Log Message:
Use cmpw.


To generate a diff of this commit:
cvs rdiff -u -r1.135 -r1.136 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.135 src/sys/arch/amd64/amd64/locore.S:1.136
--- src/sys/arch/amd64/amd64/locore.S:1.135	Thu Oct 19 19:05:53 2017
+++ src/sys/arch/amd64/amd64/locore.S	Thu Oct 19 20:27:12 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.135 2017/10/19 19:05:53 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.136 2017/10/19 20:27:12 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1475,9 +1475,9 @@ ENTRY(intrfastexit)
 	INTR_RESTORE_GPRS
 	testq	$SEL_UPL,TF_CS(%rsp)	/* interrupted %cs */
 	jz	.Lkexit
-	cmpq	$LSEL(LUCODE_SEL, SEL_UPL),TF_CS(%rsp)
+	cmpw	$LSEL(LUCODE_SEL, SEL_UPL),TF_CS(%rsp)
 	je	.Luexit64
-	cmpq	$GSEL(GUCODE_SEL, SEL_UPL),TF_CS(%rsp)
+	cmpw	$GSEL(GUCODE_SEL, SEL_UPL),TF_CS(%rsp)
 	je	.Luexit64
 
 .Luexit32:



CVS commit: src/sys/arch/amd64

2017-10-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Oct 19 18:36:31 UTC 2017

Modified Files:
src/sys/arch/amd64/acpi: acpi_wakeup_low.S
src/sys/arch/amd64/amd64: locore.S machdep.c

Log Message:
Improve our segregs model. Pass 1/3.

Right now, we are saving and restoring %ds/%es each time we enter/leave the
kernel. However, we let %fs/%gs live in the kernel space, and we rely on
the fact that when switching to an LWP, %fs/%gs are set right away (via
cpu_switchto or setregs).

It has two drawbacks: we are taking care of %ds/%es while they are
deprecated (useless) on 64bit LWPs, and we are restricting %fs/%gs while
they still have a meaning on 32bit LWPs.

Therefore, handle 32bit and 64bit LWPs differently:
 * 64bit LWPs use fixed segregs, which are not taken care of.
 * 32bit LWPs have dynamic segregs, always saved/restored.

For now, only %ds and %es are changed; %fs and %gs will be in the next
passes.

The trapframe is constructed as usual. In INTRFASTEXIT, we restore %ds/%es
depending on the %cs value. If %cs contains one of the two standard 64bit
selectors, don't do anything. Otherwise, restore everything.

When doing a context switch, just restore %ds/%es to their default values.
On a 32bit LWP they will be overwritten by INTRFASTEXIT; on a 64bit LWP
they won't be updated.

In the ACPI wakeup code, restore %ds/%es to the default 64bit user value.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/acpi/acpi_wakeup_low.S
cvs rdiff -u -r1.133 -r1.134 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.269 -r1.270 src/sys/arch/amd64/amd64/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/acpi/acpi_wakeup_low.S
diff -u src/sys/arch/amd64/acpi/acpi_wakeup_low.S:1.6 src/sys/arch/amd64/acpi/acpi_wakeup_low.S:1.7
--- src/sys/arch/amd64/acpi/acpi_wakeup_low.S:1.6	Sat Sep 23 10:18:49 2017
+++ src/sys/arch/amd64/acpi/acpi_wakeup_low.S	Thu Oct 19 18:36:31 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: acpi_wakeup_low.S,v 1.6 2017/09/23 10:18:49 maxv Exp $	*/
+/*	$NetBSD: acpi_wakeup_low.S,v 1.7 2017/10/19 18:36:31 maxv Exp $	*/
 
 /*-
  * Copyright (c) 2007 Joerg Sonnenberger 
@@ -40,12 +40,17 @@ acpi_md_sleep_exit:
 	lgdt	ACPI_SUSPEND_GDT(%r8)
 
 	/* Reload fixed descriptors for new GDT */
-	movw	$GSEL(GDATA_SEL, SEL_KPL),%ax
+	movw	$GSEL(GUDATA_SEL, SEL_UPL),%ax
 	movw	%ax,%ds
 	movw	%ax,%es
+	movw	$GSEL(GDATA_SEL, SEL_KPL),%ax
 	movw	%ax,%ss
 
-	/* FS and GS are driven by MSRs, so use NULL for them */
+	/*
+	 * FS and GS are driven by MSRs, so use NULL for them.
+	 * XXX XXX XXX That's not the case if we're returning to a 32bit
+	 * LWP!
+	 */
 	xorw	%ax,%ax
 	movw	%ax,%fs
 	movw	%ax,%gs

Index: src/sys/arch/amd64/amd64/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.133 src/sys/arch/amd64/amd64/locore.S:1.134
--- src/sys/arch/amd64/amd64/locore.S:1.133	Tue Oct 17 07:48:10 2017
+++ src/sys/arch/amd64/amd64/locore.S	Thu Oct 19 18:36:31 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.133 2017/10/17 07:48:10 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.134 2017/10/19 18:36:31 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1159,7 +1159,10 @@ skip_CR0:
 	jnz	lwp_32bit
 
 lwp_64bit:
-	/* Zero out %fs/%gs registers. */
+	/* Set default 64bit values in %ds, %es, %fs and %gs. */
+	movq	$GSEL(GUDATA_SEL, SEL_UPL),%rax
+	movw	%ax,%ds
+	movw	%ax,%es
 	xorq	%rax,%rax
 	movw	%ax,%fs
 	CLI(cx)
@@ -1193,8 +1196,11 @@ lwp_32bit:
 	movq	PCB_GS(%r14),%rax
 	movq	%rax,(GUGS_SEL*8)(%rcx)
 
-	/* Reload %fs and %gs */
+	/* Set default 32bit values in %ds, %es. %fs and %gs are special. */
 	movq	L_MD_REGS(%r12),%rbx
+	movq	$GSEL(GUDATA32_SEL, SEL_UPL),%rax
+	movw	%ax,%ds
+	movw	%ax,%es
 	movw	TF_FS(%rbx),%fs
 	CLI(ax)
 	SWAPGS
@@ -1281,10 +1287,10 @@ IDTVEC(syscall)
 	cld
 #endif
 	INTR_SAVE_GPRS
-	movw	%es,TF_ES(%rsp)
-	movw	%fs,TF_FS(%rsp)
-	movw	%gs,TF_GS(%rsp)
-	movw	$(GSEL(GUDATA_SEL, SEL_UPL)),TF_DS(%rsp)
+	movw	$GSEL(GUDATA_SEL, SEL_UPL),TF_DS(%rsp)
+	movw	$GSEL(GUDATA_SEL, SEL_UPL),TF_ES(%rsp)
+	movw	$0,TF_FS(%rsp)
+	movw	$0,TF_GS(%rsp)
 	STI(si)
 
 do_syscall:
@@ -1313,18 +1319,18 @@ do_syscall:
 #endif
 
 	/*
-	 * If the syscall might have modified some registers, or we are a 32bit
-	 * process we must return to user with an 'iret' instruction.
-	 * If the iret faults in kernel (assumed due to illegal register values)
-	 * then a SIGSEGV will be signalled.
+	 * Decide if we need to take a slow path. That's the case when we
+	 * want to reload %cs and %ss on a 64bit LWP (MDL_IRET set), or when
+	 * we're returning to a 32bit LWP (MDL_COMPAT32 set).
+	 *
+	 * In either case, we jump into intrfastexit and return to userland
+	 * with the iret instruction.
 	 */
 	testl	$(MDL_IRET|MDL_COMPAT32),L_MD_FLAGS(%r14)
+	jnz	intrfastexit
+
 	INTR_RESTORE_GPRS
-	movw	TF_ES(%rsp),%es
-	movw	TF_DS(%rsp),%ds
 	SWAPGS
-	jnz	.Lkexit
-
 #ifndef XEN
 	movq	TF_RIP(%rsp),%rcx	

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

2017-10-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Oct 19 09:32:01 UTC 2017

Modified Files:
src/sys/arch/amd64/amd64: process_machdep.c

Log Message:
Make sure we don't go farther with 32bit LWPs. There appears to be some
confusion in the code - in part introduced by myself -, and clearly this
place is not supposed to handle 32bit LWPs.

Right now we're returning EINVAL, but verily we would need to redirect
these calls to their netbsd32 counterparts.


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.36 src/sys/arch/amd64/amd64/process_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/process_machdep.c
diff -u src/sys/arch/amd64/amd64/process_machdep.c:1.35 src/sys/arch/amd64/amd64/process_machdep.c:1.36
--- src/sys/arch/amd64/amd64/process_machdep.c:1.35	Sun Aug 13 08:07:52 2017
+++ src/sys/arch/amd64/amd64/process_machdep.c	Thu Oct 19 09:32:01 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: process_machdep.c,v 1.35 2017/08/13 08:07:52 maxv Exp $	*/
+/*	$NetBSD: process_machdep.c,v 1.36 2017/10/19 09:32:01 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@@ -74,7 +74,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.35 2017/08/13 08:07:52 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: process_machdep.c,v 1.36 2017/10/19 09:32:01 maxv Exp $");
 
 #include "opt_xen.h"
 #include 
@@ -103,6 +103,11 @@ int
 process_read_regs(struct lwp *l, struct reg *regs)
 {
 	struct trapframe *tf = process_frame(l);
+	struct proc *p = l->l_proc;
+
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
 
 #define copy_to_reg(reg, REG, idx) regs->regs[_REG_##REG] = tf->tf_##reg;
 	_FRAME_GREG(copy_to_reg)
@@ -114,6 +119,11 @@ process_read_regs(struct lwp *l, struct 
 int
 process_read_fpregs(struct lwp *l, struct fpreg *regs, size_t *sz)
 {
+	struct proc *p = l->l_proc;
+
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
 
 	process_read_fpregs_xmm(l, >fxstate);
 
@@ -123,6 +133,11 @@ process_read_fpregs(struct lwp *l, struc
 int
 process_read_dbregs(struct lwp *l, struct dbreg *regs, size_t *sz)
 {
+	struct proc *p = l->l_proc;
+
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
 
 	x86_dbregs_read(l, regs);
 
@@ -133,10 +148,15 @@ int
 process_write_regs(struct lwp *l, const struct reg *regp)
 {
 	struct trapframe *tf = process_frame(l);
+	struct proc *p = l->l_proc;
 	int error;
 	const long *regs = regp->regs;
 	int err, trapno;
 
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
+
 	/*
 	 * Check for security violations.
 	 * Note that struct regs is compatible with
@@ -168,6 +188,11 @@ process_write_regs(struct lwp *l, const 
 int
 process_write_fpregs(struct lwp *l, const struct fpreg *regs, size_t sz)
 {
+	struct proc *p = l->l_proc;
+
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
 
 	process_write_fpregs_xmm(l, >fxstate);
 	return 0;
@@ -176,8 +201,13 @@ process_write_fpregs(struct lwp *l, cons
 int
 process_write_dbregs(struct lwp *l, const struct dbreg *regs, size_t sz)
 {
+	struct proc *p = l->l_proc;
 	int error;
 
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
+
 	/*
 	 * Check for security violations.
 	 */
@@ -207,6 +237,11 @@ int
 process_set_pc(struct lwp *l, void *addr)
 {
 	struct trapframe *tf = process_frame(l);
+	struct proc *p = l->l_proc;
+
+	if (p->p_flag & PK_32) {
+		return EINVAL;
+	}
 
 	if ((uint64_t)addr >= VM_MAXUSER_ADDRESS)
 		return EINVAL;



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

2017-10-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 18 17:12:42 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
If a branch is already there, use it and don't create a new one. This way
we can call mm_map_tree twice with neighboring regions.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.2 src/sys/arch/amd64/stand/prekern/mm.c:1.3
--- src/sys/arch/amd64/stand/prekern/mm.c:1.2	Sun Oct 15 06:37:32 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Oct 18 17:12:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.2 2017/10/15 06:37:32 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.3 2017/10/18 17:12:42 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -82,6 +82,12 @@ mm_palloc(size_t npages)
 	return pa;
 }
 
+static bool
+mm_pte_is_valid(pt_entry_t pte)
+{
+	return ((pte & PG_V) != 0);
+}
+
 paddr_t
 mm_vatopa(vaddr_t va)
 {
@@ -111,39 +117,46 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 {
 	size_t i, size, nL4e, nL3e, nL2e;
 	size_t L4e_idx, L3e_idx, L2e_idx;
-	paddr_t L3page_pa, L2page_pa, L1page_pa;
+	paddr_t pa;
+
+	size = endva - startva;
 
 	/*
-	 * Initialize constants.
+	 * Build L4.
 	 */
-	size = endva - startva;
-	nL4e = roundup(size, NBPD_L4) / NBPD_L4;
-	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
-	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
 	L4e_idx = pl4_i(startva);
-	L3e_idx = pl3_i(startva);
-	L2e_idx = pl2_i(startva);
-
-	ASSERT(nL4e == 1);
+	nL4e = roundup(size, NBPD_L4) / NBPD_L4;
 	ASSERT(L4e_idx == 511);
+	ASSERT(nL4e == 1);
+	if (!mm_pte_is_valid(L4_BASE[L4e_idx])) {
+		pa = mm_palloc(1);
+		L4_BASE[L4e_idx] = pa | PG_V | PG_RW;
+	}
 
 	/*
-	 * Allocate the physical pages.
+	 * Build L3.
 	 */
-	L3page_pa = mm_palloc(nL4e);
-	L2page_pa = mm_palloc(nL3e);
-	L1page_pa = mm_palloc(nL2e);
+	L3e_idx = pl3_i(startva);
+	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
+	for (i = 0; i < nL3e; i++) {
+		if (mm_pte_is_valid(L3_BASE[L3e_idx+i])) {
+			continue;
+		}
+		pa = mm_palloc(1);
+		L3_BASE[L3e_idx+i] = pa | PG_V | PG_RW;
+	}
 
 	/*
-	 * Build the branch in the page tree. We link the levels together,
-	 * from L4 to L1.
+	 * Build L2.
 	 */
-	L4_BASE[L4e_idx] = L3page_pa | PG_V | PG_RW;
-	for (i = 0; i < nL3e; i++) {
-		L3_BASE[L3e_idx+i] = (L2page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
-	}
+	L2e_idx = pl2_i(startva);
+	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
 	for (i = 0; i < nL2e; i++) {
-		L2_BASE[L2e_idx+i] = (L1page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
+		if (mm_pte_is_valid(L2_BASE[L2e_idx+i])) {
+			continue;
+		}
+		pa = mm_palloc(1);
+		L2_BASE[L2e_idx+i] = pa | PG_V | PG_RW;
 	}
 }
 



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

2017-10-17 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Oct 17 07:48:10 UTC 2017

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

Log Message:
Move %ds and %es into the GDT on 64bit LWPs.


To generate a diff of this commit:
cvs rdiff -u -r1.132 -r1.133 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.267 -r1.268 src/sys/arch/amd64/amd64/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/locore.S
diff -u src/sys/arch/amd64/amd64/locore.S:1.132 src/sys/arch/amd64/amd64/locore.S:1.133
--- src/sys/arch/amd64/amd64/locore.S:1.132	Tue Oct 17 07:33:44 2017
+++ src/sys/arch/amd64/amd64/locore.S	Tue Oct 17 07:48:10 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.132 2017/10/17 07:33:44 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.133 2017/10/17 07:48:10 maxv Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1284,7 +1284,7 @@ IDTVEC(syscall)
 	movw	%es,TF_ES(%rsp)
 	movw	%fs,TF_FS(%rsp)
 	movw	%gs,TF_GS(%rsp)
-	movw	$(LSEL(LUDATA_SEL, SEL_UPL)),TF_DS(%rsp)
+	movw	$(GSEL(GUDATA_SEL, SEL_UPL)),TF_DS(%rsp)
 	STI(si)
 
 do_syscall:

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.267 src/sys/arch/amd64/amd64/machdep.c:1.268
--- src/sys/arch/amd64/amd64/machdep.c:1.267	Sun Oct 15 13:34:24 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Tue Oct 17 07:48:10 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.267 2017/10/15 13:34:24 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.268 2017/10/17 07:48:10 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.267 2017/10/15 13:34:24 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.268 2017/10/17 07:48:10 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1331,8 +1331,8 @@ setregs(struct lwp *l, struct exec_packa
 	l->l_md.md_flags = MDL_IRET;
 
 	tf = l->l_md.md_regs;
-	tf->tf_ds = LSEL(LUDATA_SEL, SEL_UPL);
-	tf->tf_es = LSEL(LUDATA_SEL, SEL_UPL);
+	tf->tf_ds = GSEL(GUDATA_SEL, SEL_UPL);
+	tf->tf_es = GSEL(GUDATA_SEL, SEL_UPL);
 	cpu_segregs64_zero(l);
 	tf->tf_rdi = 0;
 	tf->tf_rsi = 0;



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

2017-10-17 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Oct 17 07:02:50 UTC 2017

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

Log Message:
fix comment, rdx, not edx


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 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.25 src/sys/arch/amd64/amd64/copy.S:1.26
--- src/sys/arch/amd64/amd64/copy.S:1.25	Tue Oct 17 06:58:15 2017
+++ src/sys/arch/amd64/amd64/copy.S	Tue Oct 17 07:02:50 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: copy.S,v 1.25 2017/10/17 06:58:15 maxv Exp $	*/
+/*	$NetBSD: copy.S,v 1.26 2017/10/17 07:02:50 maxv Exp $	*/
 
 /*
  * Copyright (c) 2001 Wasabi Systems, Inc.
@@ -343,7 +343,7 @@ ENTRY(copyinstr)
 	xorq	%rax,%rax
 	jmp	copystr_return
 
-2:	/* edx is zero -- return EFAULT or ENAMETOOLONG. */
+2:	/* rdx is zero -- return EFAULT or ENAMETOOLONG. */
 	callq	smap_enable
 	movq	$VM_MAXUSER_ADDRESS,%r11
 	cmpq	%r11,%rsi
@@ -384,7 +384,7 @@ ENTRY(copystr)
 	xorl	%eax,%eax
 	jmp	6f
 
-4:	/* edx is zero -- return ENAMETOOLONG. */
+4:	/* rdx is zero -- return ENAMETOOLONG. */
 	movl	$ENAMETOOLONG,%eax
 
 6:	/* Set *lencopied and return %eax. */



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

2017-10-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 15 13:34:24 UTC 2017

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

Log Message:
Mmh, don't forget to clear the TLS gdt slots on Xen. Otherwise, when doing
a lwp32->lwp64 context switch, the new lwp can use the slots to reconstruct
the address of the previous lwp's TLS space (and defeat ASLR?).


To generate a diff of this commit:
cvs rdiff -u -r1.266 -r1.267 src/sys/arch/amd64/amd64/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.266 src/sys/arch/amd64/amd64/machdep.c:1.267
--- src/sys/arch/amd64/amd64/machdep.c:1.266	Sun Oct 15 12:49:53 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Sun Oct 15 13:34:24 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.266 2017/10/15 12:49:53 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.267 2017/10/15 13:34:24 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.266 2017/10/15 12:49:53 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.267 2017/10/15 13:34:24 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -431,6 +431,7 @@ x86_64_tls_switch(struct lwp *l)
 	struct cpu_info *ci = curcpu();
 	struct pcb *pcb = lwp_getpcb(l);
 	struct trapframe *tf = l->l_md.md_regs;
+	uint64_t zero = 0;
 
 	/*
 	 * Raise the IPL to IPL_HIGH.
@@ -453,6 +454,8 @@ x86_64_tls_switch(struct lwp *l)
 		setfs(tf->tf_fs);
 		HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, tf->tf_gs);
 	} else {
+		update_descriptor(()->ci_gdt[GUFS_SEL], );
+		update_descriptor(()->ci_gdt[GUGS_SEL], );
 		setfs(0);
 		HYPERVISOR_set_segment_base(SEGBASE_GS_USER_SEL, 0);
 		HYPERVISOR_set_segment_base(SEGBASE_FS, pcb->pcb_fs);



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

2017-10-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 15 06:37:32 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Descend the page tree from L4 to L1, instead of allocating a separate
branch and linking it at the end. This way we don't need to allocate VA
from the (tiny) prekern map.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.1 src/sys/arch/amd64/stand/prekern/mm.c:1.2
--- src/sys/arch/amd64/stand/prekern/mm.c:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Oct 15 06:37:32 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.2 2017/10/15 06:37:32 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,8 +41,7 @@ extern paddr_t kernpa_start, kernpa_end;
 vaddr_t iom_base;
 
 paddr_t pa_avail = 0;
-static vaddr_t va_avail = (PREKERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2);
-static vaddr_t va_end = (PREKERNBASE + (NKL2_KIMG_ENTRIES + 1) * NBPD_L2);
+static const vaddr_t tmpva = (PREKERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2);
 
 void
 mm_init(paddr_t first_pa)
@@ -50,25 +49,6 @@ mm_init(paddr_t first_pa)
 	pa_avail = first_pa;
 }
 
-static paddr_t
-mm_palloc(size_t npages)
-{
-	paddr_t pa = pa_avail;
-	pa_avail += npages * PAGE_SIZE;
-	return pa;
-}
-
-static vaddr_t
-mm_valloc(size_t npages)
-{
-	vaddr_t va = va_avail;
-	va_avail += npages * PAGE_SIZE;
-	if (va_avail > va_end) {
-		fatal("mm_valloc: no VA left");
-	}
-	return va;
-}
-
 static void
 mm_enter_pa(paddr_t pa, vaddr_t va, pte_prot_t prot)
 {
@@ -81,6 +61,27 @@ mm_flush_va(vaddr_t va)
 	asm volatile("invlpg (%0)" ::"r" (va) : "memory");
 }
 
+static paddr_t
+mm_palloc(size_t npages)
+{
+	paddr_t pa;
+	size_t i;
+
+	/* Allocate the physical pages */
+	pa = pa_avail;
+	pa_avail += npages * PAGE_SIZE;
+
+	/* Zero them out */
+	for (i = 0; i < npages; i++) {
+		mm_enter_pa(pa + i * PAGE_SIZE, tmpva,
+		MM_PROT_READ|MM_PROT_WRITE);
+		mm_flush_va(tmpva);
+		memset((void *)tmpva, 0, PAGE_SIZE);
+	}
+
+	return pa;
+}
+
 paddr_t
 mm_vatopa(vaddr_t va)
 {
@@ -106,13 +107,11 @@ mm_mprotect(vaddr_t startva, size_t size
 }
 
 static void
-mm_map_va(vaddr_t startva, vaddr_t endva)
+mm_map_tree(vaddr_t startva, vaddr_t endva)
 {
-	size_t i, idx, size, nL4e, nL3e, nL2e;
+	size_t i, size, nL4e, nL3e, nL2e;
 	size_t L4e_idx, L3e_idx, L2e_idx;
-	vaddr_t L3page_va, L2page_va;
 	paddr_t L3page_pa, L2page_pa, L1page_pa;
-	pd_entry_t *pdir;
 
 	/*
 	 * Initialize constants.
@@ -122,48 +121,30 @@ mm_map_va(vaddr_t startva, vaddr_t endva
 	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
 	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
 	L4e_idx = pl4_i(startva);
-	L3e_idx = pl3_i(startva % NBPD_L4);
-	L2e_idx = pl2_i(startva % NBPD_L3);
+	L3e_idx = pl3_i(startva);
+	L2e_idx = pl2_i(startva);
+
+	ASSERT(nL4e == 1);
+	ASSERT(L4e_idx == 511);
 
 	/*
-	 * Map the sub-tree itself.
+	 * Allocate the physical pages.
 	 */
-	L3page_va = mm_valloc(nL4e);
 	L3page_pa = mm_palloc(nL4e);
-	L2page_va = mm_valloc(nL3e);
 	L2page_pa = mm_palloc(nL3e);
-
 	L1page_pa = mm_palloc(nL2e);
 
-	for (i = 0; i < nL4e; i++) {
-		mm_enter_pa(L3page_pa + i * PAGE_SIZE,
-		L3page_va + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
-		memset((void *)(L3page_va + i * PAGE_SIZE), 0, PAGE_SIZE);
-	}
-
-	for (i = 0; i < nL3e; i++) {
-		mm_enter_pa(L2page_pa + i * PAGE_SIZE,
-		L2page_va + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
-		memset((void *)(L2page_va + i * PAGE_SIZE), 0, PAGE_SIZE);
-	}
-
 	/*
-	 * Now link the levels together.
+	 * Build the branch in the page tree. We link the levels together,
+	 * from L4 to L1.
 	 */
-	pdir = (pt_entry_t *)L3page_va;
-	for (i = 0, idx = L3e_idx; i < nL3e; i++, idx++) {
-		pdir[idx] = (L2page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
+	L4_BASE[L4e_idx] = L3page_pa | PG_V | PG_RW;
+	for (i = 0; i < nL3e; i++) {
+		L3_BASE[L3e_idx+i] = (L2page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
 	}
-
-	pdir = (pt_entry_t *)L2page_va;
-	for (i = 0, idx = L2e_idx; i < nL2e; i++, idx++) {
-		pdir[idx] = (L1page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
+	for (i = 0; i < nL2e; i++) {
+		L2_BASE[L2e_idx+i] = (L1page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
 	}
-
-	/*
-	 * Finally, link the sub-tree into the tree.
-	 */
-	L4_BASE[L4e_idx] = L3page_pa | PG_V | PG_RW;
 }
 
 /*
@@ -185,7 +166,7 @@ mm_rand_base()
 	randva = rounddown(KASLR_WINDOW_BASE + rnd % (KASLR_WINDOW_SIZE - size),
 	PAGE_SIZE);
 
-	mm_map_va(randva, randva + size);
+	mm_map_tree(randva, randva + size);
 
 	return randva;
 }



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

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:56:26 UTC 2017

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

Log Message:
Use bootspace.


To generate a diff of this commit:
cvs rdiff -u -r1.263 -r1.264 src/sys/arch/amd64/amd64/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.263 src/sys/arch/amd64/amd64/machdep.c:1.264
--- src/sys/arch/amd64/amd64/machdep.c:1.263	Sun Oct  8 09:06:50 2017
+++ src/sys/arch/amd64/amd64/machdep.c	Wed Oct 11 16:56:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.263 2017/10/08 09:06:50 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.264 2017/10/11 16:56:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include 
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.263 2017/10/08 09:06:50 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.264 2017/10/11 16:56:26 maxv Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1994,16 +1994,44 @@ cpu_initclocks(void)
 int
 mm_md_kernacc(void *ptr, vm_prot_t prot, bool *handled)
 {
-	extern char start, __data_start;
 	const vaddr_t v = (vaddr_t)ptr;
+	vaddr_t kva, kva_end;
 
-	if (v >= (vaddr_t) && v < (vaddr_t)kern_end) {
+	kva = bootspace.text.va;
+	kva_end = kva + bootspace.text.sz;
+	if (v >= kva && v < kva_end) {
 		*handled = true;
-		/* Either the text or rodata segment */
-		if (v < (vaddr_t)&__data_start && (prot & VM_PROT_WRITE))
+		if (prot & VM_PROT_WRITE) {
 			return EFAULT;
+		}
+		return 0;
+	}
+
+	kva = bootspace.rodata.va;
+	kva_end = kva + bootspace.rodata.sz;
+	if (v >= kva && v < kva_end) {
+		*handled = true;
+		if (prot & VM_PROT_WRITE) {
+			return EFAULT;
+		}
+		return 0;
+	}
+
+	kva = bootspace.data.va;
+	kva_end = kva + bootspace.data.sz;
+	if (v >= kva && v < kva_end) {
+		*handled = true;
+		return 0;
+	}
+
+	kva = bootspace.boot.va;
+	kva_end = kva + bootspace.boot.sz;
+	if (v >= kva && v < kva_end) {
+		*handled = true;
+		return 0;
+	}
 
-	} else if (v >= module_start && v < module_end) {
+	if (v >= module_start && v < module_end) {
 		*handled = true;
 		if (!uvm_map_checkprot(module_map, v, v + 1, prot))
 			return EFAULT;



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

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:21:06 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Make sure we're relocating a relocatable kernel.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/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/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.1 src/sys/arch/amd64/stand/prekern/elf.c:1.2
--- src/sys/arch/amd64/stand/prekern/elf.c:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Wed Oct 11 16:21:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.2 2017/10/11 16:21:06 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -86,7 +86,8 @@ static int
 elf_check_header()
 {
 	if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
-	eif.ehdr->e_ident[EI_CLASS] != ELFCLASS) {
+	eif.ehdr->e_ident[EI_CLASS] != ELFCLASS ||
+	eif.ehdr->e_type != ET_REL) {
 		return -1;
 	}
 	return 0;



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

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:18:11 UTC 2017

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

Log Message:
Remove this #if, these options belong to the kernel and not the prekern.
No real change since eblob is always here. And I was apparently drunk
when writing some comments.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.1 src/sys/arch/amd64/stand/prekern/locore.S:1.2
--- src/sys/arch/amd64/stand/prekern/locore.S:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Wed Oct 11 16:18:11 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.2 2017/10/11 16:18:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -366,21 +366,19 @@ no_NOX:
 	/* Find end of the prekern image; brings us on (1). */
 	movl	$_C_LABEL(__prekern_end),%edi
 
-	/* Find end of the kernel image; brind us on (2). */
+	/* Find end of the kernel image; brings us on (2). */
 	movl	_C_LABEL(kernpa_end),%eax
 	testl	%eax,%eax
 	jz	1f
 	movl	%eax,%edi
 1:
 
-	/* Find end of the kernel symbols; brinds us on (3). */
-#if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(makeoptions_COPY_SYMTAB) /* XXX */
+	/* Find end of the kernel symbols; brings us on (3). */
 	movl	_C_LABEL(esym),%eax
 	testl	%eax,%eax
 	jz	1f
 	movl	%eax,%edi
 1:
-#endif
 
 	/* Find end of the kernel preloaded modules; brings us on (4). */
 	movl	_C_LABEL(eblob),%eax



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

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:13:16 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.ldscript

Log Message:
Add an alignment to fill strictly all of the padding; does not increase
the size of the prekern.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/prekern.ldscript
diff -u src/sys/arch/amd64/stand/prekern/prekern.ldscript:1.1 src/sys/arch/amd64/stand/prekern/prekern.ldscript:1.2
--- src/sys/arch/amd64/stand/prekern/prekern.ldscript:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.ldscript	Wed Oct 11 16:13:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.ldscript,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: prekern.ldscript,v 1.2 2017/10/11 16:13:16 maxv Exp $	*/
 
 __PAGE_SIZE = 0x1000 ;
 
@@ -10,6 +10,7 @@ SECTIONS
 		*(.text)
 		*(.text.*)
 		*(.stub)
+		. = ALIGN(__PAGE_SIZE);
 	} =0xCC
 	_etext = . ;
 	PROVIDE (etext = .) ;



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

2017-10-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Oct 10 09:29:14 UTC 2017

Added Files:
src/sys/arch/amd64/stand: Makefile
src/sys/arch/amd64/stand/prekern: Makefile console.c elf.c locore.S
mm.c pdir.h prekern.c prekern.h prekern.ldscript redef.h trap.S

Log Message:
Add the amd64 prekern. It is a kernel relocator used for Kernel ASLR (see
tech-kern@). It works, but is not yet linked to the build system, because
I can't build a distribution right now.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/stand/Makefile
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/stand/prekern/Makefile \
src/sys/arch/amd64/stand/prekern/console.c \
src/sys/arch/amd64/stand/prekern/elf.c \
src/sys/arch/amd64/stand/prekern/locore.S \
src/sys/arch/amd64/stand/prekern/mm.c \
src/sys/arch/amd64/stand/prekern/pdir.h \
src/sys/arch/amd64/stand/prekern/prekern.c \
src/sys/arch/amd64/stand/prekern/prekern.h \
src/sys/arch/amd64/stand/prekern/prekern.ldscript \
src/sys/arch/amd64/stand/prekern/redef.h \
src/sys/arch/amd64/stand/prekern/trap.S

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

Added files:

Index: src/sys/arch/amd64/stand/Makefile
diff -u /dev/null src/sys/arch/amd64/stand/Makefile:1.1
--- /dev/null	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/Makefile	Tue Oct 10 09:29:14 2017
@@ -0,0 +1,5 @@
+#	$NetBSD: Makefile,v 1.1 2017/10/10 09:29:14 maxv Exp $
+
+SUBDIR= prekern
+
+.include 

Index: src/sys/arch/amd64/stand/prekern/Makefile
diff -u /dev/null src/sys/arch/amd64/stand/prekern/Makefile:1.1
--- /dev/null	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Tue Oct 10 09:29:14 2017
@@ -0,0 +1,40 @@
+#	$NetBSD: Makefile,v 1.1 2017/10/10 09:29:14 maxv Exp $
+
+PROG=		prekern
+SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
+
+NOSSP=		# defined
+NOPIE=		# defined
+NOMAN=		1
+
+S=	${.CURDIR}/../../../..
+
+.PATH: ${.CURDIR}
+
+BINDIR=		/usr/mdec
+BINMODE=	444
+
+.include 
+
+CPPFLAGS+=	-I. -I${S}
+
+.include 
+
+CPPFLAGS+=	-DKERNEL -D__x86_64__
+CFLAGS+=	-Wall -Werror -mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
+STRIPFLAG=
+LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
+
+LIBCRT0=	# nothing
+LIBCRTI=	# nothing
+LIBC=		# nothing
+LIBCRTBEGIN=	# nothing
+LIBCRTEND=	# nothing
+
+${PROG}: ${OBJS}
+	${LD} ${LINKFLAGS} -o ${.TARGET} ${OBJS}
+
+all:	${PROG}
+
+.include 
+
Index: src/sys/arch/amd64/stand/prekern/console.c
diff -u /dev/null src/sys/arch/amd64/stand/prekern/console.c:1.1
--- /dev/null	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/console.c	Tue Oct 10 09:29:14 2017
@@ -0,0 +1,120 @@
+/*	$NetBSD: console.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+
+/*
+ * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * 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.
+ */
+
+#include "prekern.h"
+
+extern vaddr_t atdevbase;
+#define CONS_WID 80
+#define CONS_HEI 25
+
+static char *cons_start;
+static size_t cons_x, cons_y;
+static char cons_buffer[CONS_WID * 2 * CONS_HEI];
+
+void init_cons()
+{
+	cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN);
+	cons_x = 0;
+	cons_y = 0;
+}
+
+static void check_scroll()
+{
+	char *src, *dst;
+	size_t i;
+
+	if (cons_y != CONS_HEI)
+		return;
+
+	for (i = 0; i < CONS_HEI-1; i++) {
+		dst = _buffer[0] + i * (CONS_WID * 2);
+		src = _buffer[0] + (i + 1) * (CONS_WID * 2);
+		memcpy(dst, src, (CONS_WID * 2));
+	}
+	memset(_buffer[0] + (CONS_WID * 2) * (CONS_HEI-1), 0,

CVS commit: src/sys/arch/amd64

2017-10-08 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct  8 08:26:01 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: files.amd64
Added Files:
src/sys/arch/amd64/amd64: prekern.c

Log Message:
Add the prekern entry point in the kernel.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/amd64/prekern.c
cvs rdiff -u -r1.92 -r1.93 src/sys/arch/amd64/conf/files.amd64

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/files.amd64
diff -u src/sys/arch/amd64/conf/files.amd64:1.92 src/sys/arch/amd64/conf/files.amd64:1.93
--- src/sys/arch/amd64/conf/files.amd64:1.92	Tue Aug 15 08:51:38 2017
+++ src/sys/arch/amd64/conf/files.amd64	Sun Oct  8 08:26:01 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: files.amd64,v 1.92 2017/08/15 08:51:38 maxv Exp $
+#	$NetBSD: files.amd64,v 1.93 2017/10/08 08:26:01 maxv Exp $
 #
 # new style config file for amd64 architecture
 #
@@ -46,6 +46,7 @@ file	arch/amd64/amd64/kobj_machdep.c		mo
 file	kern/subr_disk_mbr.c			disk
 file	arch/amd64/amd64/gdt.c			machdep
 file	arch/amd64/amd64/machdep.c		machdep
+file	arch/amd64/amd64/prekern.c		machdep
 file	arch/amd64/amd64/process_machdep.c	machdep
 file	arch/amd64/amd64/trap.c			machdep
 file	arch/x86/x86/fpu.c			machdep

Added files:

Index: src/sys/arch/amd64/amd64/prekern.c
diff -u /dev/null src/sys/arch/amd64/amd64/prekern.c:1.1
--- /dev/null	Sun Oct  8 08:26:01 2017
+++ src/sys/arch/amd64/amd64/prekern.c	Sun Oct  8 08:26:01 2017
@@ -0,0 +1,136 @@
+/*	$NetBSD: prekern.c,v 1.1 2017/10/08 08:26:01 maxv Exp $	*/
+
+/*
+ * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * 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.
+ */
+
+#include 
+
+#include "opt_realmem.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+struct prekern_args {
+	int boothowto;
+	void *bootinfo;
+	void *bootspace;
+	int esym;
+	int biosextmem;
+	int biosbasemem;
+	int cpuid_level;
+	uint32_t nox_flag;
+	uint64_t PDPpaddr;
+	vaddr_t atdevbase;
+	vaddr_t lwp0uarea;
+	paddr_t first_avail;
+};
+
+void main(void);
+void init_x86_64(paddr_t);
+
+static void prekern_copy_args(struct prekern_args *);
+static void prekern_unmap(void);
+int start_prekern(struct prekern_args *);
+
+static void
+prekern_copy_args(struct prekern_args *pkargs)
+{
+	extern int boothowto;
+	extern struct bootinfo bootinfo;
+	extern struct bootspace bootspace;
+	extern int esym;
+	extern int biosextmem;
+	extern int biosbasemem;
+	extern int cpuid_level;
+	extern uint32_t nox_flag;
+	extern uint64_t PDPpaddr;
+	extern vaddr_t lwp0uarea;
+
+	boothowto = pkargs->boothowto;
+	memcpy(, pkargs->bootinfo, sizeof(bootinfo));
+	memcpy(, pkargs->bootspace, sizeof(bootspace));
+	esym = pkargs->esym;
+
+#ifndef REALEXTMEM
+	biosextmem = pkargs->biosextmem;
+#else
+	biosextmem = REALEXTMEM;
+#endif
+
+#ifndef REALBASEMEM
+	biosbasemem = pkargs->biosbasemem;
+#else
+	biosbasemem = REALBASEMEM;
+#endif
+
+	cpuid_level = pkargs->cpuid_level;
+	nox_flag = pkargs->nox_flag;
+	PDPpaddr = pkargs->PDPpaddr;
+	atdevbase = pkargs->atdevbase;
+	lwp0uarea = pkargs->lwp0uarea;
+}
+
+static void
+prekern_unmap(void)
+{
+	L4_BASE[0] = 0;
+	tlbflushg();
+}
+
+/*
+ * The prekern jumps here.
+ */
+int
+start_prekern(struct prekern_args *pkargs)
+{
+	paddr_t first_avail;
+
+	prekern_copy_args(pkargs);
+	first_avail = pkargs->first_avail;
+
+	init_x86_64(first_avail);
+
+	prekern_unmap();
+
+	main();
+
+	panic("main returned");
+
+	

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

2017-10-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct  7 10:16:47 UTC 2017

Modified Files:
src/sys/arch/amd64/conf: Makefile.amd64
Added Files:
src/sys/arch/amd64/conf: GENERIC_KASLR

Log Message:
Add GENERIC_KASLR, only toolchain parts for now.


To generate a diff of this commit:
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/conf/GENERIC_KASLR
cvs rdiff -u -r1.58 -r1.59 src/sys/arch/amd64/conf/Makefile.amd64

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/Makefile.amd64
diff -u src/sys/arch/amd64/conf/Makefile.amd64:1.58 src/sys/arch/amd64/conf/Makefile.amd64:1.59
--- src/sys/arch/amd64/conf/Makefile.amd64:1.58	Wed Aug  9 19:11:13 2017
+++ src/sys/arch/amd64/conf/Makefile.amd64	Sat Oct  7 10:16:47 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.amd64,v 1.58 2017/08/09 19:11:13 maxv Exp $
+#	$NetBSD: Makefile.amd64,v 1.59 2017/10/07 10:16:47 maxv Exp $
 
 # Makefile for NetBSD
 #
@@ -61,7 +61,11 @@ KERN_AS=	library
 ## (5) link settings
 ##
 TEXTADDR?=	0x8020
+.if defined(KASLR)
+EXTRA_LINKFLAGS=	-z max-page-size=0x20 -r -d
+.else
 EXTRA_LINKFLAGS=	-z max-page-size=0x20
+.endif
 LINKFLAGS_NORMAL=	-X
 KERNLDSCRIPT?= ${AMD64}/conf/kern.ldscript
 

Added files:

Index: src/sys/arch/amd64/conf/GENERIC_KASLR
diff -u /dev/null src/sys/arch/amd64/conf/GENERIC_KASLR:1.1
--- /dev/null	Sat Oct  7 10:16:47 2017
+++ src/sys/arch/amd64/conf/GENERIC_KASLR	Sat Oct  7 10:16:47 2017
@@ -0,0 +1,5 @@
+# $NetBSD: GENERIC_KASLR,v 1.1 2017/10/07 10:16:47 maxv Exp $
+
+include "arch/amd64/conf/GENERIC"
+
+makeoptions 	KASLR=1		# Kernel ASLR



<    1   2   3   4   5   6   7   >