Module Name:    src
Committed By:   pgoyette
Date:           Sun Mar  8 00:53:13 UTC 2020

Modified Files:
        src/sys/arch/amd64/amd64: trap.c
        src/sys/compat/netbsd32: netbsd32_mod.c
        src/sys/sys: compat_stub.h

Log Message:
Fix another left-over from last year's [pgoyette-compat] work.  This
allows recognition of the oosyscall sequence for amd64.

Fixes PR kern/55038

XXX pullup-9


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.126 src/sys/arch/amd64/amd64/trap.c
cvs rdiff -u -r1.16 -r1.17 src/sys/compat/netbsd32/netbsd32_mod.c
cvs rdiff -u -r1.22 -r1.23 src/sys/sys/compat_stub.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/trap.c
diff -u src/sys/arch/amd64/amd64/trap.c:1.125 src/sys/arch/amd64/amd64/trap.c:1.126
--- src/sys/arch/amd64/amd64/trap.c:1.125	Thu Nov 21 19:23:58 2019
+++ src/sys/arch/amd64/amd64/trap.c	Sun Mar  8 00:53:12 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.c,v 1.125 2019/11/21 19:23:58 ad Exp $	*/
+/*	$NetBSD: trap.c,v 1.126 2020/03/08 00:53:12 pgoyette Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
@@ -64,7 +64,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.125 2019/11/21 19:23:58 ad Exp $");
+__KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.126 2020/03/08 00:53:12 pgoyette Exp $");
 
 #include "opt_ddb.h"
 #include "opt_kgdb.h"
@@ -83,14 +83,11 @@ __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.1
 #include <sys/syscall.h>
 #include <sys/cpu.h>
 #include <sys/ucontext.h>
+#include <sys/module_hook.h>
+#include <sys/compat_stub.h>
 
 #include <uvm/uvm_extern.h>
 
-#ifdef COMPAT_NETBSD32
-#include <sys/exec.h>
-#include <compat/netbsd32/netbsd32_exec.h>
-#endif
-
 #include <machine/cpufunc.h>
 #include <x86/fpu.h>
 #include <x86/dbregs.h>
@@ -118,6 +115,11 @@ dtrace_trap_func_t dtrace_trap_func = NU
 dtrace_doubletrap_func_t dtrace_doubletrap_func = NULL;
 #endif
 
+/*
+ * Module hook for amd64_oosyscall
+ */
+struct amd64_oosyscall_hook_t amd64_oosyscall_hook;
+
 void nmitrap(struct trapframe *);
 void doubletrap(struct trapframe *);
 void trap(struct trapframe *);
@@ -342,32 +344,13 @@ trap(struct trapframe *frame)
 		goto we_re_toast;
 
 	case T_PROTFLT|T_USER:		/* protection fault */
-#if defined(COMPAT_NETBSD32) && defined(COMPAT_10)
+	{	int hook_ret;
 
-/*
- * XXX This code currently not included in loadable module;  it is
- * only included in built-in modules.
- */
-	{
-		static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
-		const size_t sz = sizeof(lcall);
-		char tmp[sizeof(lcall) /* Avoids VLA */];
-
-		/* 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);
+		MODULE_HOOK_CALL(amd64_oosyscall_hook, (p, frame),
+			ENOSYS, hook_ret);
+		if (hook_ret == 0)
 			goto out;
-		}
 	}
-#endif
 		/* FALLTHROUGH */
 	case T_TSSFLT|T_USER:
 	case T_SEGNPFLT|T_USER:

Index: src/sys/compat/netbsd32/netbsd32_mod.c
diff -u src/sys/compat/netbsd32/netbsd32_mod.c:1.16 src/sys/compat/netbsd32/netbsd32_mod.c:1.17
--- src/sys/compat/netbsd32/netbsd32_mod.c:1.16	Wed Nov 20 19:37:53 2019
+++ src/sys/compat/netbsd32/netbsd32_mod.c	Sun Mar  8 00:53:12 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: netbsd32_mod.c,v 1.16 2019/11/20 19:37:53 pgoyette Exp $	*/
+/*	$NetBSD: netbsd32_mod.c,v 1.17 2020/03/08 00:53:12 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2008 The NetBSD Foundation, Inc.
@@ -29,8 +29,37 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+/*
+ * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Charles M. Hannum, and by Maxime Villard.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * 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 <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.16 2019/11/20 19:37:53 pgoyette Exp $");
+__KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.17 2020/03/08 00:53:12 pgoyette Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_execfmt.h"
@@ -45,6 +74,7 @@ __KERNEL_RCSID(0, "$NetBSD: netbsd32_mod
 #include <sys/exec.h>
 #include <sys/exec_elf.h>
 #include <sys/module_hook.h>
+#include <sys/compat_stub.h>
 
 #include <compat/netbsd32/netbsd32_sysctl.h>
 #include <compat/netbsd32/netbsd32_kern_proc.h>
@@ -100,6 +130,35 @@ static struct execsw netbsd32_execsw[] =
 #endif
 };
 
+#if defined(__amd64__)
+
+/* This code was moved here, from $SRC/arch/amd64/amd64/trap.c */
+
+static int
+amd64_oosyscall_handle(struct proc *p, struct trapframe *frame)
+{
+
+	static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
+	const size_t sz = sizeof(lcall);
+	char tmp[sizeof(lcall) /* Avoids VLA */];
+
+	/* 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);
+		return 0;
+	} else
+		return EPASSTHROUGH;
+}
+#endif
+
 static int
 compat_netbsd32_modcmd(modcmd_t cmd, void *arg)
 {
@@ -113,10 +172,16 @@ compat_netbsd32_modcmd(modcmd_t cmd, voi
 			netbsd32_sysctl_init();
 			netbsd32_machdep_md_init();
 			netbsd32_kern_proc_32_init();
+#if defined(__amd64__)
+		MODULE_HOOK_SET(amd64_oosyscall_hook, amd64_oosyscall_handle);
+#endif
 		}
 		return error;
 
 	case MODULE_CMD_FINI:
+#if defined(__amd64__)
+		MODULE_HOOK_UNSET(amd64_oosyscall_hook);
+#endif
 		netbsd32_machdep_md_fini();
 		netbsd32_sysctl_fini();
 		netbsd32_kern_proc_32_fini();
@@ -127,6 +192,10 @@ compat_netbsd32_modcmd(modcmd_t cmd, voi
 			netbsd32_kern_proc_32_init();
 			netbsd32_sysctl_init();
 			netbsd32_machdep_md_init();
+#if defined(__amd64__)
+			MODULE_HOOK_SET(amd64_oosyscall_hook,
+			    amd64_oosyscall_handle);
+#endif
 		}
 		return error;
 

Index: src/sys/sys/compat_stub.h
diff -u src/sys/sys/compat_stub.h:1.22 src/sys/sys/compat_stub.h:1.23
--- src/sys/sys/compat_stub.h:1.22	Wed Nov 20 19:37:54 2019
+++ src/sys/sys/compat_stub.h	Sun Mar  8 00:53:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: compat_stub.h,v 1.22 2019/11/20 19:37:54 pgoyette Exp $	*/
+/*	$NetBSD: compat_stub.h,v 1.23 2020/03/08 00:53:13 pgoyette Exp $	*/
 
 /*-
  * Copyright (c) 2018 The NetBSD Foundation, Inc.
@@ -381,4 +381,11 @@ MODULE_HOOK(uvm_coredump_walkmap_hook, i
     (struct proc *, int (*)(struct uvm_coredump_state *), void *));
 MODULE_HOOK(uvm_coredump_count_segs_hook, int, (struct proc *));
 
+/*
+ * Hook for amd64 handler for oosyscall for COMPAT_NETBSD32 && COMPAT_10)
+ */
+struct proc;
+struct trapframe;
+MODULE_HOOK(amd64_oosyscall_hook, int, (struct proc *, struct trapframe *));
+
 #endif	/* _SYS_COMPAT_STUB_H */

Reply via email to