Mon Mar 9 09:55:52 2020 UTC ()
Pull up following revision(s) (requested by pgoyette in ticket #770):

	sys/compat/netbsd32/netbsd32_mod.c: revision 1.19

If a syscall requires a module to be autoloaded, the initial invocation
of that syscall will return ERESTART.  For amd64's netbsd32_syscall()
that means we need to back up the PC saved in the trap frame so we can
re-issue the syscall instruction.  For "normal" syscall traps, we saved
the instruction length in the trap frame, but this was missing for the
oosyscall/lcall path.  Since the PC was not backed up, the kernel-only
value ERESTART was returned to userland, causing all sort of grief for
old compat_netbsd32 executables!

XXX Pullup-9


(martin)
diff -r1.15.4.2 -r1.15.4.3 src/sys/compat/netbsd32/netbsd32_mod.c

cvs diff -r1.15.4.2 -r1.15.4.3 src/sys/compat/netbsd32/netbsd32_mod.c (expand / switch to unified diff)

--- src/sys/compat/netbsd32/netbsd32_mod.c 2020/03/09 05:36:24 1.15.4.2
+++ src/sys/compat/netbsd32/netbsd32_mod.c 2020/03/09 09:55:52 1.15.4.3
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: netbsd32_mod.c,v 1.15.4.2 2020/03/09 05:36:24 martin Exp $ */ 1/* $NetBSD: netbsd32_mod.c,v 1.15.4.3 2020/03/09 09:55:52 martin Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software developed for The NetBSD Foundation 7 * This code is derived from software developed for The NetBSD Foundation
8 * by Andrew Doran. 8 * by Andrew Doran.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -49,27 +49,27 @@ @@ -49,27 +49,27 @@
49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 49 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 50 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE. 58 * POSSIBILITY OF SUCH DAMAGE.
59 */ 59 */
60 60
61#include <sys/cdefs.h> 61#include <sys/cdefs.h>
62__KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.15.4.2 2020/03/09 05:36:24 martin Exp $"); 62__KERNEL_RCSID(0, "$NetBSD: netbsd32_mod.c,v 1.15.4.3 2020/03/09 09:55:52 martin Exp $");
63 63
64#ifdef _KERNEL_OPT 64#ifdef _KERNEL_OPT
65#include "opt_execfmt.h" 65#include "opt_execfmt.h"
66#endif 66#endif
67 67
68#ifndef ELFSIZE 68#ifndef ELFSIZE
69#define ELFSIZE ARCH_ELFSIZE 69#define ELFSIZE ARCH_ELFSIZE
70#endif 70#endif
71 71
72#include <sys/param.h> 72#include <sys/param.h>
73#include <sys/module.h> 73#include <sys/module.h>
74#include <sys/exec.h> 74#include <sys/exec.h>
75#include <sys/exec_elf.h> 75#include <sys/exec_elf.h>
@@ -138,69 +138,70 @@ static int @@ -138,69 +138,70 @@ static int
138amd64_oosyscall_handle(struct proc *p, struct trapframe *frame) 138amd64_oosyscall_handle(struct proc *p, struct trapframe *frame)
139{ 139{
140 140
141 static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 }; 141 static const char lcall[7] = { 0x9a, 0, 0, 0, 0, 7, 0 };
142 const size_t sz = sizeof(lcall); 142 const size_t sz = sizeof(lcall);
143 char tmp[sizeof(lcall) /* Avoids VLA */]; 143 char tmp[sizeof(lcall) /* Avoids VLA */];
144 144
145 /* Check for the oosyscall lcall instruction. */ 145 /* Check for the oosyscall lcall instruction. */
146 if (p->p_emul == &emul_netbsd32 && 146 if (p->p_emul == &emul_netbsd32 &&
147 frame->tf_rip < VM_MAXUSER_ADDRESS32 - sz && 147 frame->tf_rip < VM_MAXUSER_ADDRESS32 - sz &&
148 copyin((void *)frame->tf_rip, tmp, sz) == 0 && 148 copyin((void *)frame->tf_rip, tmp, sz) == 0 &&
149 memcmp(tmp, lcall, sz) == 0) { 149 memcmp(tmp, lcall, sz) == 0) {
150 150
151 /* Advance past the lcall. */ 151 /* Advance past the lcall and save instruction size. */
152 frame->tf_rip += sz; 152 frame->tf_rip += sz;
 153 frame->tf_err = sz;
153 154
154 /* Do the syscall */ 155 /* Do the syscall */
155 p->p_md.md_syscall(frame); 156 p->p_md.md_syscall(frame);
156 return 0; 157 return 0;
157 } else 158 } else
158 return EPASSTHROUGH; 159 return EPASSTHROUGH;
159} 160}
160#endif 161#endif /* defined(__amd64__) */
161 162
162static int 163static int
163compat_netbsd32_modcmd(modcmd_t cmd, void *arg) 164compat_netbsd32_modcmd(modcmd_t cmd, void *arg)
164{ 165{
165 int error; 166 int error;
166 167
167 switch (cmd) { 168 switch (cmd) {
168 case MODULE_CMD_INIT: 169 case MODULE_CMD_INIT:
169 error = exec_add(netbsd32_execsw, 170 error = exec_add(netbsd32_execsw,
170 __arraycount(netbsd32_execsw)); 171 __arraycount(netbsd32_execsw));
171 if (error == 0) { 172 if (error == 0) {
172 netbsd32_sysctl_init(); 173 netbsd32_sysctl_init();
173 netbsd32_machdep_md_init(); 174 netbsd32_machdep_md_init();
174 netbsd32_kern_proc_32_init(); 175 netbsd32_kern_proc_32_init();
175#if defined(__amd64__) 176#if defined(__amd64__)
176 MODULE_HOOK_SET(amd64_oosyscall_hook, "nb32oo", 177 MODULE_HOOK_SET(amd64_oosyscall_hook, "nb32oo",
177 amd64_oosyscall_handle); 178 amd64_oosyscall_handle);
178#endif 179#endif /* defined(__amd64__) */
179 } 180 }
180 return error; 181 return error;
181 182
182 case MODULE_CMD_FINI: 183 case MODULE_CMD_FINI:
183#if defined(__amd64__) 184#if defined(__amd64__)
184 MODULE_HOOK_UNSET(amd64_oosyscall_hook); 185 MODULE_HOOK_UNSET(amd64_oosyscall_hook);
185#endif 186#endif /* defined(__amd64__) */
186 netbsd32_machdep_md_fini(); 187 netbsd32_machdep_md_fini();
187 netbsd32_sysctl_fini(); 188 netbsd32_sysctl_fini();
188 netbsd32_kern_proc_32_fini(); 189 netbsd32_kern_proc_32_fini();
189 190
190 error = exec_remove(netbsd32_execsw, 191 error = exec_remove(netbsd32_execsw,
191 __arraycount(netbsd32_execsw)); 192 __arraycount(netbsd32_execsw));
192 if (error) { 193 if (error) {
193 netbsd32_kern_proc_32_init(); 194 netbsd32_kern_proc_32_init();
194 netbsd32_sysctl_init(); 195 netbsd32_sysctl_init();
195 netbsd32_machdep_md_init(); 196 netbsd32_machdep_md_init();
196#if defined(__amd64__) 197#if defined(__amd64__)
197 MODULE_HOOK_SET(amd64_oosyscall_hook, "nb32oo", 198 MODULE_HOOK_SET(amd64_oosyscall_hook, "nb32oo",
198 amd64_oosyscall_handle); 199 amd64_oosyscall_handle);
199#endif 200#endif /* defined(__amd64__) */
200 } 201 }
201 return error; 202 return error;
202 203
203 default: 204 default:
204 return ENOTTY; 205 return ENOTTY;
205 } 206 }
206} 207}