1/*	$NetBSD: linux_mod.c,v 1.1 2008/11/19 18:36:03 ad Exp $	*/
2
3/*-
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software developed for The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
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
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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
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
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__KERNEL_RCSID(0, "$NetBSD: linux_mod.c,v 1.1 2008/11/19 18:36:03 ad Exp $");
34
35#ifdef _KERNEL_OPT
36#include "opt_execfmt.h"
37#endif
38
39#ifndef ELFSIZE
40#define ELFSIZE ARCH_ELFSIZE
41#endif
42
43#include <sys/param.h>
44#include <sys/module.h>
45#include <sys/exec.h>
46#include <sys/signalvar.h>
47
48#include <compat/linux/common/linux_sysctl.h>
49#include <compat/linux/common/linux_futex.h>
50#include <compat/linux/common/linux_exec.h>
51
52#if defined(EXEC_ELF32) && ELFSIZE == 32
53# define	MD1	",exec_elf32"
54#else
55# define	MD1	""
56#endif
57#if defined(EXEC_ELF64)
58# define	MD2	",exec_elf64"
59#else
60# define	MD2	""
61#endif
62#if defined(EXEC_AOUT)
63#  define	MD3	",exec_aout"
64#else
65# define	MD3	""
66#endif
67
68MODULE(MODULE_CLASS_MISC, compat_linux, "compat,compat_ossaudio" MD1 MD2 MD3);
69
70static struct execsw linux_execsw[] = {
71#if defined(EXEC_ELF32) && ELFSIZE == 32
72	{ sizeof (Elf32_Ehdr),
73	  exec_elf32_makecmds,
74	  { linux_elf32_probe },
75	  &emul_linux,
76	  EXECSW_PRIO_ANY,
77	  LINUX_ELF_AUX_ARGSIZ,
78	  linux_elf32_copyargs,
79	  NULL,
80	  coredump_elf32,
81	  linux_exec_setup_stack },
82#elif defined(EXEC_ELF64)
83	{ sizeof (Elf64_Ehdr),
84	  exec_elf64_makecmds,
85	  { linux_elf64_probe },
86	  &emul_linux,
87	  EXECSW_PRIO_ANY,
88	  LINUX_ELF_AUX_ARGSIZ,
89	  linux_elf64_copyargs,
90	  NULL,
91 	  coredump_elf64,
92	  linux_exec_setup_stack },
93#endif
94#ifdef EXEC_AOUT
95	{ LINUX_AOUT_HDR_SIZE,
96	  exec_linux_aout_makecmds,
97	  { NULL },
98	  &emul_linux,
99	  EXECSW_PRIO_LAST,
100	  LINUX_AOUT_AUX_ARGSIZ,
101	  linux_aout_copyargs,
102	  NULL,
103	  coredump_netbsd,
104	  linux_exec_setup_stack },
105#endif
106};
107
108static int
109compat_linux_modcmd(modcmd_t cmd, void *arg)
110{
111	int error;
112
113	switch (cmd) {
114	case MODULE_CMD_INIT:
115		linux_futex_init();
116		linux_sysctl_init();
117		error = exec_add(linux_execsw,
118		    __arraycount(linux_execsw));
119		if (error != 0)
120			linux_sysctl_fini();
121		return error;
122
123	case MODULE_CMD_FINI:
124		error = exec_remove(linux_execsw,
125		    __arraycount(linux_execsw));
126		if (error == 0) {
127			linux_sysctl_fini();
128			linux_futex_fini();
129		}
130		return error;
131
132	default:
133		return ENOTTY;
134	}
135}
136