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 (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,206 +1,207 @@ @@ -1,206 +1,207 @@
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.
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the 16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution. 17 * documentation and/or other materials provided with the distribution.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32/* 32/*
33 * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc. 33 * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
34 * All rights reserved. 34 * All rights reserved.
35 * 35 *
36 * This code is derived from software contributed to The NetBSD Foundation 36 * This code is derived from software contributed to The NetBSD Foundation
37 * by Charles M. Hannum, and by Maxime Villard. 37 * by Charles M. Hannum, and by Maxime Villard.
38 * 38 *
39 * Redistribution and use in source and binary forms, with or without 39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions 40 * modification, are permitted provided that the following conditions
41 * are met: 41 * are met:
42 * 1. Redistributions of source code must retain the above copyright 42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer. 43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright 44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the 45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution. 46 * documentation and/or other materials provided with the distribution.
47 * 47 *
48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 48 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
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>
76#include <sys/module_hook.h> 76#include <sys/module_hook.h>
77#include <sys/compat_stub.h> 77#include <sys/compat_stub.h>
78 78
79#include <compat/netbsd32/netbsd32_sysctl.h> 79#include <compat/netbsd32/netbsd32_sysctl.h>
80#include <compat/netbsd32/netbsd32_kern_proc.h> 80#include <compat/netbsd32/netbsd32_kern_proc.h>
81#include <compat/netbsd32/netbsd32_exec.h> 81#include <compat/netbsd32/netbsd32_exec.h>
82 82
83#define ELF32_AUXSIZE (howmany(ELF_AUX_ENTRIES * sizeof(Aux32Info), \ 83#define ELF32_AUXSIZE (howmany(ELF_AUX_ENTRIES * sizeof(Aux32Info), \
84 sizeof(Elf32_Addr)) + MAXPATHLEN + ALIGN(1)) 84 sizeof(Elf32_Addr)) + MAXPATHLEN + ALIGN(1))
85 85
86struct compat32_80_modctl_hook_t compat32_80_modctl_hook; 86struct compat32_80_modctl_hook_t compat32_80_modctl_hook;
87 87
88# define DEPS1 "ksem,coredump,compat_util" 88# define DEPS1 "ksem,coredump,compat_util"
89 89
90#if defined(EXEC_ELF32) 90#if defined(EXEC_ELF32)
91# define DEPS2 ",exec_elf32" 91# define DEPS2 ",exec_elf32"
92#else 92#else
93# define DEPS2 "" 93# define DEPS2 ""
94#endif 94#endif
95 95
96MODULE(MODULE_CLASS_EXEC, compat_netbsd32, DEPS1 DEPS2); 96MODULE(MODULE_CLASS_EXEC, compat_netbsd32, DEPS1 DEPS2);
97 97
98static struct execsw netbsd32_execsw[] = { 98static struct execsw netbsd32_execsw[] = {
99#ifdef EXEC_AOUT 99#ifdef EXEC_AOUT
100 { 100 {
101 .es_hdrsz = sizeof(struct netbsd32_exec), 101 .es_hdrsz = sizeof(struct netbsd32_exec),
102 .es_makecmds = exec_netbsd32_makecmds, 102 .es_makecmds = exec_netbsd32_makecmds,
103 .u = { 103 .u = {
104 .elf_probe_func = NULL, 104 .elf_probe_func = NULL,
105 }, 105 },
106 .es_emul = &emul_netbsd32, 106 .es_emul = &emul_netbsd32,
107 .es_prio = EXECSW_PRIO_FIRST, 107 .es_prio = EXECSW_PRIO_FIRST,
108 .es_arglen = 0, 108 .es_arglen = 0,
109 .es_copyargs = netbsd32_copyargs, 109 .es_copyargs = netbsd32_copyargs,
110 .es_setregs = NULL, 110 .es_setregs = NULL,
111 .es_coredump = coredump_netbsd32, 111 .es_coredump = coredump_netbsd32,
112 .es_setup_stack = exec_setup_stack, 112 .es_setup_stack = exec_setup_stack,
113 }, 113 },
114#endif 114#endif
115#ifdef EXEC_ELF32 115#ifdef EXEC_ELF32
116 { 116 {
117 .es_hdrsz = sizeof (Elf32_Ehdr), 117 .es_hdrsz = sizeof (Elf32_Ehdr),
118 .es_makecmds = exec_elf32_makecmds, 118 .es_makecmds = exec_elf32_makecmds,
119 .u = { 119 .u = {
120 .elf_probe_func = netbsd32_elf32_probe, 120 .elf_probe_func = netbsd32_elf32_probe,
121 }, 121 },
122 .es_emul = &emul_netbsd32, 122 .es_emul = &emul_netbsd32,
123 .es_prio = EXECSW_PRIO_ANY, 123 .es_prio = EXECSW_PRIO_ANY,
124 .es_arglen = ELF32_AUXSIZE, 124 .es_arglen = ELF32_AUXSIZE,
125 .es_copyargs = netbsd32_elf32_copyargs, 125 .es_copyargs = netbsd32_elf32_copyargs,
126 .es_setregs = NULL, 126 .es_setregs = NULL,
127 .es_coredump = coredump_elf32, 127 .es_coredump = coredump_elf32,
128 .es_setup_stack = exec_setup_stack, 128 .es_setup_stack = exec_setup_stack,
129 }, 129 },
130#endif 130#endif
131}; 131};
132 132
133#if defined(__amd64__) 133#if defined(__amd64__)
134 134
135/* This code was moved here, from $SRC/arch/amd64/amd64/trap.c */ 135/* This code was moved here, from $SRC/arch/amd64/amd64/trap.c */
136 136
137static int 137static 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}