Module Name:    src
Committed By:   martin
Date:           Tue Aug  8 11:59:16 UTC 2017

Modified Files:
        src/sys/arch/amd64/amd64 [netbsd-6-1]: locore.S machdep.c trap.c
        src/sys/arch/i386/i386 [netbsd-6-1]: locore.S machdep.c trap.c

Log Message:
Pull up following revision(s) (requested by maxv in ticket #1464):

        sys/arch/i386/i386/trap.c:     revision 1.288           (patch)
        sys/arch/i386/i386/machdep.c:  revision 1.783           (patch)
        sys/arch/i386/i386/locore.S:   revision 1.146           (patch)
        sys/arch/amd64/amd64/locore.S: revision 1.122,1.124     (patch)
        sys/arch/amd64/amd64/machdep.c revision 1.254           (patch)
        sys/arch/amd64/amd64/trap.c:   revision 1.95-1.96       (patch)

Remove the osyscall call gate and emulate it. There is a
one-instruction race in it that could panic the kernel.

Restore the ability to run netbsd 1.0 32-bit executables by checking
for the relevant lcall instruction in the trap handler and treating it
as a syscall.


To generate a diff of this commit:
cvs rdiff -u -r1.66.2.1 -r1.66.2.1.6.1 src/sys/arch/amd64/amd64/locore.S
cvs rdiff -u -r1.175.2.8 -r1.175.2.8.2.1 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.69.2.1.6.1 -r1.69.2.1.6.2 src/sys/arch/amd64/amd64/trap.c
cvs rdiff -u -r1.95.10.3 -r1.95.10.3.2.1 src/sys/arch/i386/i386/locore.S
cvs rdiff -u -r1.717.2.7 -r1.717.2.7.6.1 src/sys/arch/i386/i386/machdep.c
cvs rdiff -u -r1.262.14.1 -r1.262.14.2 src/sys/arch/i386/i386/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.66.2.1 src/sys/arch/amd64/amd64/locore.S:1.66.2.1.6.1
--- src/sys/arch/amd64/amd64/locore.S:1.66.2.1	Fri Apr 20 23:32:14 2012
+++ src/sys/arch/amd64/amd64/locore.S	Tue Aug  8 11:59:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.66.2.1 2012/04/20 23:32:14 riz Exp $	*/
+/*	$NetBSD: locore.S,v 1.66.2.1.6.1 2017/08/08 11:59:16 martin Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -1209,26 +1209,6 @@ NENTRY(child_trampoline)
 	.globl  _C_LABEL(osyscall_return)
 
 /*
- * oosyscall()
- *
- * Old call gate entry for syscall. only needed if we're
- * going to support running old i386 NetBSD 1.0 or ibcs2 binaries, etc,
- * on NetBSD/amd64.
- * The 64bit call gate can't request that arguments be copied from the
- * user stack (which the i386 code uses to get a gap for the flags).
- * push/pop are <read>:<modify_sp>:<write> cycles.
- */
-IDTVEC(oosyscall)
-	/* Set rflags in trap frame. */
-	pushq	(%rsp)		# move user's %eip
-	pushq	16(%rsp)	# and %cs
-	popq	8(%rsp)
-	pushfq
-	popq	16(%rsp)
-	pushq	$7		# size of instruction for restart
-	jmp	osyscall1
-
-/*
  * osyscall()
  *
  * Trap gate entry for int $80 syscall, also used by sigreturn.
@@ -1240,7 +1220,6 @@ IDTVEC(osyscall)
 	addq $0x10,%rsp
 #endif
 	pushq	$2		# size of instruction for restart
-osyscall1:
 	pushq	$T_ASTFLT	# trap # for doing ASTs
 	INTRENTRY
 	STI(si)

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.175.2.8 src/sys/arch/amd64/amd64/machdep.c:1.175.2.8.2.1
--- src/sys/arch/amd64/amd64/machdep.c:1.175.2.8	Sat Apr 20 09:59:39 2013
+++ src/sys/arch/amd64/amd64/machdep.c	Tue Aug  8 11:59:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.175.2.8 2013/04/20 09:59:39 bouyer Exp $	*/
+/*	$NetBSD: machdep.c,v 1.175.2.8.2.1 2017/08/08 11:59:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -111,7 +111,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.175.2.8 2013/04/20 09:59:39 bouyer Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.175.2.8.2.1 2017/08/08 11:59:16 martin Exp $");
 
 /* #define XENDEBUG_LOW  */
 
@@ -1575,7 +1575,6 @@ typedef void (vector)(void);
 extern vector IDTVEC(syscall);
 extern vector IDTVEC(syscall32);
 extern vector IDTVEC(osyscall);
-extern vector IDTVEC(oosyscall);
 extern vector *IDTVEC(exceptions)[];
 
 static void
@@ -1838,10 +1837,7 @@ init_x86_64(paddr_t first_avail)
 	set_mem_segment(GDT_ADDR_MEM(gdtstore, GUDATA_SEL), 0,
 	    x86_btop(VM_MAXUSER_ADDRESS) - 1, SDT_MEMRWA, SEL_UPL, 1, 0, 1);
 
-	/* make ldt gates and memory segments */
-	setgate((struct gate_descriptor *)(ldtstore + LSYS5CALLS_SEL),
-	    &IDTVEC(oosyscall), 0, SDT_SYS386CGT, SEL_UPL,
-	    GSEL(GCODE_SEL, SEL_KPL));
+	/* make ldt memory segments */
 	*(struct mem_segment_descriptor *)(ldtstore + LUCODE_SEL) =
 	    *GDT_ADDR_MEM(gdtstore, GUCODE_SEL);
 	*(struct mem_segment_descriptor *)(ldtstore + LUDATA_SEL) =
@@ -1873,16 +1869,6 @@ init_x86_64(paddr_t first_avail)
 	set_mem_segment(ldt_segp, 0, x86_btop(VM_MAXUSER_ADDRESS32) - 1,
 	    SDT_MEMRWA, SEL_UPL, 1, 1, 0);
 
-	/*
-	 * Other entries.
-	 */
-	memcpy((struct gate_descriptor *)(ldtstore + LSOL26CALLS_SEL),
-	    (struct gate_descriptor *)(ldtstore + LSYS5CALLS_SEL),
-	    sizeof (struct gate_descriptor));
-	memcpy((struct gate_descriptor *)(ldtstore + LBSDICALLS_SEL),
-	    (struct gate_descriptor *)(ldtstore + LSYS5CALLS_SEL),
-	    sizeof (struct gate_descriptor));
-
 	/* exceptions */
 	for (x = 0; x < 32; x++) {
 #ifndef XEN

Index: src/sys/arch/amd64/amd64/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.69.2.1.6.1 src/sys/arch/amd64/amd64/trap.c:1.69.2.1.6.2
--- src/sys/arch/amd64/amd64/trap.c:1.69.2.1.6.1	Sat Mar 25 17:19:32 2017
+++ src/sys/arch/amd64/amd64/trap.c	Tue Aug  8 11:59:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.69.2.1.6.1 2017/03/25 17:19:32 snj Exp $	*/
+/*	$NetBSD: trap.c,v 1.69.2.1.6.2 2017/08/08 11:59:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000 The NetBSD Foundation, Inc.
@@ -68,12 +68,14 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.69.2.1.6.1 2017/03/25 17:19:32 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.69.2.1.6.2 2017/08/08 11:59:16 martin 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 <sys/param.h>
 #include <sys/systm.h>
@@ -92,6 +94,11 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.6
 
 #include <uvm/uvm_extern.h>
 
+#ifdef COMPAT_NETBSD32
+#include <sys/exec.h>
+#include <compat/netbsd32/netbsd32_exec.h>
+#endif
+
 #include <machine/cpufunc.h>
 #include <machine/fpu.h>
 #include <machine/psl.h>
@@ -202,7 +209,6 @@ trap(struct trapframe *frame)
 	struct proc *p;
 	struct pcb *pcb;
 	extern char fusuintrfailure[], kcopy_fault[];
-	extern char IDTVEC(oosyscall)[];
 	extern char IDTVEC(osyscall)[];
 	extern char IDTVEC(syscall32)[];
 #ifndef XEN
@@ -383,6 +389,27 @@ kernelfault:
 #endif
 
 	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 == &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:
@@ -661,8 +688,7 @@ faultcommon:
 
 	case T_TRCTRAP:
 		/* Check whether they single-stepped into a lcall. */
-		if (frame->tf_rip == (uint64_t)IDTVEC(oosyscall) ||
-		    frame->tf_rip == (uint64_t)IDTVEC(osyscall) ||
+		if (frame->tf_rip == (uint64_t)IDTVEC(osyscall) ||
 		    frame->tf_rip == (uint64_t)IDTVEC(syscall32)) {
 			frame->tf_rflags &= ~PSL_T;
 			return;

Index: src/sys/arch/i386/i386/locore.S
diff -u src/sys/arch/i386/i386/locore.S:1.95.10.3 src/sys/arch/i386/i386/locore.S:1.95.10.3.2.1
--- src/sys/arch/i386/i386/locore.S:1.95.10.3	Wed Oct 31 17:19:49 2012
+++ src/sys/arch/i386/i386/locore.S	Tue Aug  8 11:59:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.95.10.3 2012/10/31 17:19:49 riz Exp $	*/
+/*	$NetBSD: locore.S,v 1.95.10.3.2.1 2017/08/08 11:59:16 martin Exp $	*/
 
 /*
  * Copyright-o-rama!
@@ -129,7 +129,7 @@
  */
 
 #include <machine/asm.h>
-__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.95.10.3 2012/10/31 17:19:49 riz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: locore.S,v 1.95.10.3.2.1 2017/08/08 11:59:16 martin Exp $");
 
 #include "opt_compat_oldboot.h"
 #include "opt_ddb.h"
@@ -1112,30 +1112,12 @@ ENTRY(savectx)
 END(savectx)
 
 /*
- * osyscall()
- *
- * Old call gate entry for syscall
- */
-IDTVEC(osyscall)
-#ifndef XEN
-	/* XXX we are in trouble! interrupts be off here. */
-	cli			# must be first instruction
-#endif
-	pushfl			# set eflags in trap frame
-	popl	8(%esp)
-	orl	$PSL_I,8(%esp)	# re-enable ints on return to user
-	pushl	$7		# size of instruction for restart
-	jmp	syscall1
-IDTVEC_END(osyscall)
-
-/*
  * syscall()
  *
  * Trap gate entry for syscall
  */
 IDTVEC(syscall)
 	pushl	$2		# size of instruction for restart
-syscall1:
 	pushl	$T_ASTFLT	# trap # for doing ASTs
 	INTRENTRY
 	STI(%eax)

Index: src/sys/arch/i386/i386/machdep.c
diff -u src/sys/arch/i386/i386/machdep.c:1.717.2.7 src/sys/arch/i386/i386/machdep.c:1.717.2.7.6.1
--- src/sys/arch/i386/i386/machdep.c:1.717.2.7	Mon May 21 15:25:58 2012
+++ src/sys/arch/i386/i386/machdep.c	Tue Aug  8 11:59:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.717.2.7 2012/05/21 15:25:58 riz Exp $	*/
+/*	$NetBSD: machdep.c,v 1.717.2.7.6.1 2017/08/08 11:59:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009
@@ -67,7 +67,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.717.2.7 2012/05/21 15:25:58 riz Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.717.2.7.6.1 2017/08/08 11:59:16 martin Exp $");
 
 #include "opt_beep.h"
 #include "opt_compat_ibcs2.h"
@@ -1121,7 +1121,6 @@ setsegment(struct segment_descriptor *sd
 #define	IDTVEC(name)	__CONCAT(X, name)
 typedef void (vector)(void);
 extern vector IDTVEC(syscall);
-extern vector IDTVEC(osyscall);
 extern vector *IDTVEC(exceptions)[];
 extern vector IDTVEC(svr4_fasttrap);
 void (*svr4_fasttrap_vec)(void) = (void (*)(void))nullop;
@@ -1525,13 +1524,9 @@ init386(paddr_t first_avail)
 #endif /* XEN */
 
 	/* make ldt gates and memory segments */
-	setgate(&ldt[LSYS5CALLS_SEL].gd, &IDTVEC(osyscall), 1,
-	    SDT_SYS386CGT, SEL_UPL, GSEL(GCODE_SEL, SEL_KPL));
-
 	ldt[LUCODE_SEL] = gdt[GUCODE_SEL];
 	ldt[LUCODEBIG_SEL] = gdt[GUCODEBIG_SEL];
 	ldt[LUDATA_SEL] = gdt[GUDATA_SEL];
-	ldt[LSOL26CALLS_SEL] = ldt[LBSDICALLS_SEL] = ldt[LSYS5CALLS_SEL];
 
 #ifndef XEN
 	/* exceptions */

Index: src/sys/arch/i386/i386/trap.c
diff -u src/sys/arch/i386/i386/trap.c:1.262.14.1 src/sys/arch/i386/i386/trap.c:1.262.14.2
--- src/sys/arch/i386/i386/trap.c:1.262.14.1	Sat Mar 25 17:19:32 2017
+++ src/sys/arch/i386/i386/trap.c	Tue Aug  8 11:59:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.262.14.1 2017/03/25 17:19:32 snj Exp $	*/
+/*	$NetBSD: trap.c,v 1.262.14.2 2017/08/08 11:59:16 martin Exp $	*/
 
 /*-
  * Copyright (c) 1998, 2000, 2005, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -68,7 +68,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.262.14.1 2017/03/25 17:19:32 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.262.14.2 2017/08/08 11:59:16 martin Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -308,8 +308,7 @@ trap(struct trapframe *frame)
 	struct lwp *l = curlwp;
 	struct proc *p;
 	struct pcb *pcb;
-	extern char fusubail[], kcopy_fault[], return_address_fault[],
-	    IDTVEC(osyscall)[];
+	extern char fusubail[], kcopy_fault[], return_address_fault[];
 	struct trapframe *vframe;
 	ksiginfo_t ksi;
 	void *onfault;
@@ -495,6 +494,26 @@ kernelfault:
 		/* NOTREACHED */
 
 	case T_PROTFLT|T_USER:		/* protection fault */
+#if 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 osyscall lcall instruction. */
+		if (frame->tf_eip < VM_MAXUSER_ADDRESS - sz &&
+		    copyin((void *)frame->tf_eip, tmp, sz) == 0 &&
+		    memcmp(tmp, lcall, sz) == 0) {
+
+			/* Advance past the lcall. */
+			frame->tf_eip += 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:
@@ -765,13 +784,6 @@ faultcommon:
 	}
 
 	case T_TRCTRAP:
-		/* Check whether they single-stepped into a lcall. */
-		if (frame->tf_eip == (int)IDTVEC(osyscall))
-			return;
-		if (frame->tf_eip == (int)IDTVEC(osyscall) + 1) {
-			frame->tf_eflags &= ~PSL_T;
-			return;
-		}
 		goto we_re_toast;
 
 	case T_BPTFLT|T_USER:		/* bpt instruction fault */

Reply via email to