xref: /netbsd-src/usr.bin/kdump/setemul.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: setemul.c,v 1.25 2007/12/31 15:26:31 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the NetBSD
18  *	Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1988, 1993
38  *	The Regents of the University of California.  All rights reserved.
39  * (c) UNIX System Laboratories, Inc.
40  * All or some portions of this file are derived from material licensed
41  * to the University of California by American Telephone and Telegraph
42  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
43  * the permission of UNIX System Laboratories, Inc.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  * 1. Redistributions of source code must retain the above copyright
49  *    notice, this list of conditions and the following disclaimer.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice, this list of conditions and the following disclaimer in the
52  *    documentation and/or other materials provided with the distribution.
53  * 3. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 #ifndef lint
72 __RCSID("$NetBSD: setemul.c,v 1.25 2007/12/31 15:26:31 ad Exp $");
73 #endif /* not lint */
74 
75 #include <sys/param.h>
76 #include <sys/errno.h>
77 #include <sys/time.h>
78 #include <sys/queue.h>
79 
80 #include <err.h>
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <string.h>
84 #include <unistd.h>
85 #include <vis.h>
86 
87 #include "setemul.h"
88 
89 #include <sys/syscall.h>
90 
91 #include "../../sys/compat/netbsd32/netbsd32_syscall.h"
92 #include "../../sys/compat/freebsd/freebsd_syscall.h"
93 #include "../../sys/compat/ibcs2/ibcs2_syscall.h"
94 #include "../../sys/compat/irix/irix_syscall.h"
95 #include "../../sys/compat/linux/linux_syscall.h"
96 #include "../../sys/compat/linux32/linux32_syscall.h"
97 #include "../../sys/compat/mach/mach_syscall.h"
98 #include "../../sys/compat/darwin/darwin_syscall.h"
99 #include "../../sys/compat/mach/arch/powerpc/ppccalls/mach_ppccalls_syscall.h"
100 #include "../../sys/compat/mach/arch/powerpc/fasttraps/mach_fasttraps_syscall.h"
101 #include "../../sys/compat/osf1/osf1_syscall.h"
102 #include "../../sys/compat/sunos32/sunos32_syscall.h"
103 #include "../../sys/compat/sunos/sunos_syscall.h"
104 #include "../../sys/compat/svr4/svr4_syscall.h"
105 #include "../../sys/compat/svr4_32/svr4_32_syscall.h"
106 #include "../../sys/compat/ultrix/ultrix_syscall.h"
107 #ifdef __m68k__
108 #include "../../sys/compat/aoutm68k/aoutm68k_syscall.h"
109 #endif
110 
111 #define KTRACE
112 #include "../../sys/kern/syscalls.c"
113 
114 #include "../../sys/compat/netbsd32/netbsd32_syscalls.c"
115 #include "../../sys/compat/freebsd/freebsd_syscalls.c"
116 #include "../../sys/compat/ibcs2/ibcs2_syscalls.c"
117 #include "../../sys/compat/irix/irix_syscalls.c"
118 #include "../../sys/compat/linux/linux_syscalls.c"
119 #include "../../sys/compat/linux32/linux32_syscalls.c"
120 #include "../../sys/compat/darwin/darwin_syscalls.c"
121 #include "../../sys/compat/mach/mach_syscalls.c"
122 #include "../../sys/compat/mach/arch/powerpc/ppccalls/mach_ppccalls_syscalls.c"
123 #include "../../sys/compat/mach/arch/powerpc/fasttraps/mach_fasttraps_syscalls.c"
124 #include "../../sys/compat/osf1/osf1_syscalls.c"
125 #include "../../sys/compat/sunos/sunos_syscalls.c"
126 #include "../../sys/compat/sunos32/sunos32_syscalls.c"
127 #include "../../sys/compat/svr4/svr4_syscalls.c"
128 #include "../../sys/compat/svr4_32/svr4_32_syscalls.c"
129 #include "../../sys/compat/ultrix/ultrix_syscalls.c"
130 #ifdef __m68k__
131 #include "../../sys/compat/aoutm68k/aoutm68k_syscalls.c"
132 #endif
133 
134 #include "../../sys/compat/svr4/svr4_errno.c"
135 #include "../../sys/compat/ibcs2/ibcs2_errno.c"
136 #include "../../sys/compat/irix/irix_errno.c"
137 #include "../../sys/compat/osf1/osf1_errno.c"
138 #include "../../sys/compat/linux/common/linux_errno.c"
139 #undef KTRACE
140 
141 #define SIGRTMIN	33	/* XXX */
142 #include "../../sys/compat/svr4/svr4_signo.c"
143 #include "../../sys/compat/ibcs2/ibcs2_signo.c"
144 /* irix uses svr4 */
145 #include "../../sys/compat/osf1/osf1_signo.c"
146 #include "../../sys/compat/linux/common/linux_signo.c"
147 
148 /* For Mach services names in MMSG traces */
149 #ifndef LETS_GET_SMALL
150 #include "../../sys/compat/mach/mach_services_names.c"
151 #endif
152 
153 #define NELEM(a) (sizeof(a) / sizeof(a[0]))
154 
155 /* static */
156 const struct emulation emulations[] = {
157 	{ "netbsd",	syscallnames,		SYS_MAXSYSCALL,
158 	  NULL,				0,
159 	  NULL,				0,	0 },
160 
161 	{ "netbsd32",	netbsd32_syscallnames,	SYS_MAXSYSCALL,
162 	  NULL,				0,
163 	  NULL,				0,	EMUL_FLAG_NETBSD32 },
164 
165 	{ "freebsd",	freebsd_syscallnames,	FREEBSD_SYS_MAXSYSCALL,
166 	  NULL,				0,
167 	  NULL,				0,	0 },
168 
169 	{ "ibcs2",	ibcs2_syscallnames,	IBCS2_SYS_MAXSYSCALL,
170 	  native_to_ibcs2_errno,	NELEM(native_to_ibcs2_errno),
171 	  ibcs2_to_native_signo,	NSIG,	0 },
172 
173 	{ "irix o32",	irix_syscallnames,	IRIX_SYS_MAXSYSCALL,
174 	  native_to_irix_errno,		NELEM(native_to_irix_errno),
175 	  svr4_to_native_signo,		NSIG,	0 },
176 
177 	{ "irix n32",	irix_syscallnames,	IRIX_SYS_MAXSYSCALL,
178 	  native_to_irix_errno,		NELEM(native_to_irix_errno),
179 	  svr4_to_native_signo,		NSIG,	0 },
180 
181 	{ "linux",	linux_syscallnames,	LINUX_SYS_MAXSYSCALL,
182 	  native_to_linux_errno,	NELEM(native_to_linux_errno),
183 	  linux_to_native_signo,	NSIG,	0 },
184 
185 	{ "linux32",	linux32_syscallnames,	LINUX32_SYS_MAXSYSCALL,
186 	  native_to_linux_errno,	NELEM(native_to_linux_errno),
187 	  linux_to_native_signo,	NSIG,	EMUL_FLAG_NETBSD32 },
188 
189 	{ "darwin",	darwin_syscallnames,	DARWIN_SYS_MAXSYSCALL,
190 	  NULL,				0,
191 	  NULL,				0,	0 },
192 
193 	{ "mach",	mach_syscallnames,	MACH_SYS_MAXSYSCALL,
194 	  NULL,				0,
195 	  NULL,				0,	0 },
196 
197 	{ "mach ppccalls",	mach_ppccalls_syscallnames,
198 	  MACH_PPCCALLS_SYS_MAXSYSCALL,
199 	  NULL,				0,
200 	  NULL,				0,	0 },
201 
202 	{ "mach fasttraps",	mach_fasttraps_syscallnames,
203 	  MACH_FASTTRAPS_SYS_MAXSYSCALL,
204 	  NULL,				0,
205 	  NULL,				0,	0 },
206 
207 	{ "osf1",	osf1_syscallnames,	OSF1_SYS_MAXSYSCALL,
208 	  native_to_osf1_errno,		NELEM(native_to_osf1_errno),
209 	  osf1_to_native_signo,		NSIG,	0 },
210 
211 	{ "sunos32",	sunos32_syscallnames,	SUNOS32_SYS_MAXSYSCALL,
212 	  NULL,				0,
213 	  NULL,				0,	EMUL_FLAG_NETBSD32 },
214 
215 	{ "sunos",	sunos_syscallnames,	SUNOS_SYS_MAXSYSCALL,
216 	  NULL,				0,
217 	  NULL,				0,	0 },
218 
219 	{ "svr4",	svr4_syscallnames,	SVR4_SYS_MAXSYSCALL,
220 	  native_to_svr4_errno,		NELEM(native_to_svr4_errno),
221 	  svr4_to_native_signo,		NSIG,	0 },
222 
223 	{ "svr4_32",	svr4_syscallnames,	SVR4_SYS_MAXSYSCALL,
224 	  native_to_svr4_errno,		NELEM(native_to_svr4_errno),
225 	  svr4_to_native_signo,		NSIG,	EMUL_FLAG_NETBSD32 },
226 
227 	{ "ultrix",	ultrix_syscallnames,	ULTRIX_SYS_MAXSYSCALL,
228 	  NULL,				0,
229 	  NULL,				0,	0 },
230 
231 	{ "pecoff",	syscallnames,		SYS_MAXSYSCALL,
232 	  NULL,				0,
233 	  NULL,				0,	0 },
234 
235 #ifdef __m68k__
236 	{ "aoutm68k",	aoutm68k_syscallnames,	AOUTM68K_SYS_MAXSYSCALL,
237 	  NULL,				0,
238 	  NULL,				0,	0 },
239 #endif
240 
241 	{ NULL,		NULL,			0,
242 	  NULL,				0,
243 	  NULL,				0,	0 }
244 };
245 
246 struct emulation_ctx {
247 	pid_t	pid;
248 	const struct emulation *emulation;
249 	LIST_ENTRY(emulation_ctx) ctx_link;
250 };
251 
252 const struct emulation *cur_emul;
253 const struct emulation *prev_emul;
254 /* Mach emulation require extra emulation contexts */
255 static const struct emulation *mach;
256 static const struct emulation *mach_ppccalls;
257 static const struct emulation *mach_fasttraps;
258 
259 static const struct emulation *default_emul = &emulations[0];
260 
261 struct emulation_ctx *current_ctx;
262 static LIST_HEAD(, emulation_ctx) emul_ctx =
263 	LIST_HEAD_INITIALIZER(emul_ctx);
264 
265 static struct emulation_ctx *ectx_find(pid_t);
266 static void	ectx_update(pid_t, const struct emulation *);
267 
268 void
269 setemul(const char *name, pid_t pid, int update_ectx)
270 {
271 	int i;
272 	const struct emulation *match = NULL;
273 
274 	for (i = 0; emulations[i].name != NULL; i++) {
275 		if (strcmp(emulations[i].name, name) == 0) {
276 			match = &emulations[i];
277 			break;
278 		}
279 	}
280 
281 	if (!match) {
282 		warnx("Emulation `%s' unknown", name);
283 		return;
284 	}
285 
286 	if (update_ectx)
287 		ectx_update(pid, match);
288 	else
289 		default_emul = match;
290 
291 	if (cur_emul != NULL)
292 		prev_emul = cur_emul;
293 	else
294 		prev_emul = match;
295 
296 	cur_emul = match;
297 }
298 
299 /*
300  * Emulation context list is very simple chained list, not even hashed.
301  * We expect the number of separate traced contexts/processes to be
302  * fairly low, so it's not worth it to optimize this.
303  * MMMmmmm not when I use it, it is only bounded PID_MAX!
304  * Requeue looked up item at start of list to cache result since the
305  * trace file tendes to have a burst of calls for a single process.
306  */
307 
308 /*
309  * Find an emulation context appropriate for the given pid.
310  */
311 static struct emulation_ctx *
312 ectx_find(pid_t pid)
313 {
314 	struct emulation_ctx *ctx;
315 
316 	/* Find an existing entry */
317 	LIST_FOREACH(ctx, &emul_ctx, ctx_link) {
318 		if (ctx->pid == pid)
319 			break;
320 	}
321 
322 	if (ctx == NULL) {
323 		/* create entry with default emulation */
324 		ctx = malloc(sizeof *ctx);
325 		if (ctx == NULL)
326 			err(1, "malloc emul context");
327 		ctx->pid = pid;
328 		ctx->emulation = default_emul;
329 
330 		/* chain into the list */
331 		LIST_INSERT_HEAD(&emul_ctx, ctx, ctx_link);
332 	} else {
333 		/* move entry to head to optimize lookup for syscall bursts */
334 		LIST_REMOVE(ctx, ctx_link);
335 		LIST_INSERT_HEAD(&emul_ctx, ctx, ctx_link);
336 	}
337 
338 	return ctx;
339 }
340 
341 /*
342  * Update emulation context for given pid, or create new if no context
343  * for this pid exists.
344  */
345 static void
346 ectx_update(pid_t pid, const struct emulation *emul)
347 {
348 	struct emulation_ctx *ctx;
349 
350 	ctx = ectx_find(pid);
351 	ctx->emulation = emul;
352 }
353 
354 /*
355  * Ensure current emulation context is correct for given pid.
356  */
357 void
358 ectx_sanify(pid_t pid)
359 {
360 	struct emulation_ctx *ctx;
361 
362 	ctx = ectx_find(pid);
363 	cur_emul = ctx->emulation;
364 }
365 
366 /*
367  * Delete emulation context for current pid.
368  * (eg when tracing exit())
369  * Defer delete just in case we've cached a pointer...
370  */
371 void
372 ectx_delete(void)
373 {
374 	static struct emulation_ctx *ctx = NULL;
375 
376 	if (ctx != NULL)
377 		free(ctx);
378 
379 	/*
380 	 * The emulation for current syscall entry is always on HEAD, due
381 	 * to code in ectx_find().
382 	 */
383 	ctx = LIST_FIRST(&emul_ctx);
384 
385 	if (ctx)
386 		LIST_REMOVE(ctx, ctx_link);
387 }
388 
389 /*
390  * Temporarily modify code and emulations to handle Mach traps
391  * XXX The define are duplicated from sys/arch/powerpc/include/mach_syscall.c
392  */
393 #define MACH_FASTTRAPS		0x00007ff0
394 #define MACH_PPCCALLS		0x00006000
395 #define MACH_ODD_SYSCALL_MASK	0x0000fff0
396 int
397 mach_traps_dispatch(int *code, const struct emulation **emul)
398 {
399 	switch (*code & MACH_ODD_SYSCALL_MASK) {
400 	case MACH_FASTTRAPS:
401 		*emul = mach_fasttraps;
402 		*code -= MACH_FASTTRAPS;
403 		return 1;
404 
405 	case MACH_PPCCALLS:
406 		*emul = mach_ppccalls;
407 		*code -= MACH_PPCCALLS;
408 		return 1;
409 
410 	default:
411 		if (*code < 0 && *code > -MACH_SYS_MAXSYSCALL) {
412 			*emul = mach;
413 			*code = -*code;
414 			return 1;
415 		}
416 		return 0;
417 	}
418 }
419 
420 /*
421  * Lookup Machs emulations
422  */
423 void
424 mach_lookup_emul(void)
425 {
426 	const struct emulation *emul_idx;
427 
428 	for (emul_idx = emulations; emul_idx->name; emul_idx++) {
429 		if (strcmp("mach", emul_idx->name) == 0)
430 			mach = emul_idx;
431 		if (strcmp("mach fasttraps", emul_idx->name) == 0)
432 			mach_fasttraps = emul_idx;
433 		if (strcmp("mach ppccalls", emul_idx->name) == 0)
434 			mach_ppccalls = emul_idx;
435 	}
436 	if (mach == NULL || mach_fasttraps == NULL || mach_ppccalls == NULL) {
437 		errx(1, "Cannot load mach emulations");
438 		exit(1);
439 	}
440 	return;
441 }
442 
443 /*
444  * Find the name of the Mach service responsible to a given message Id
445  */
446 const char *
447 mach_service_name(id)
448 	int id;
449 {
450 	const char *retval = NULL;
451 #ifndef LETS_GET_SMALL
452 	struct mach_service_name *srv;
453 
454 	for (srv = mach_services_names; srv->srv_id; srv++)
455 		if (srv->srv_id == id)
456 			break;
457 	retval = srv->srv_name;
458 #endif /* LETS_GET_SMALL */
459 
460 	return retval;
461 }
462