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