netbsd32_compat_80.c revision 1.1.2.4 1 1.1.2.4 pgoyette /* $NetBSD: netbsd32_compat_80.c,v 1.1.2.4 2018/09/18 10:35:04 pgoyette Exp $ */
2 1.1.2.1 pgoyette
3 1.1.2.1 pgoyette /*-
4 1.1.2.1 pgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
5 1.1.2.1 pgoyette * All rights reserved.
6 1.1.2.1 pgoyette *
7 1.1.2.1 pgoyette * This code is derived from software developed for The NetBSD Foundation.
8 1.1.2.1 pgoyette *
9 1.1.2.1 pgoyette * Redistribution and use in source and binary forms, with or without
10 1.1.2.1 pgoyette * modification, are permitted provided that the following conditions
11 1.1.2.1 pgoyette * are met:
12 1.1.2.1 pgoyette * 1. Redistributions of source code must retain the above copyright
13 1.1.2.1 pgoyette * notice, this list of conditions and the following disclaimer.
14 1.1.2.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
15 1.1.2.1 pgoyette * notice, this list of conditions and the following disclaimer in the
16 1.1.2.1 pgoyette * documentation and/or other materials provided with the distribution.
17 1.1.2.1 pgoyette *
18 1.1.2.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 1.1.2.1 pgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.1.2.1 pgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.1.2.1 pgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 1.1.2.1 pgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.1.2.1 pgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.1.2.1 pgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.1.2.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.1.2.1 pgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.1.2.1 pgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.1.2.1 pgoyette * POSSIBILITY OF SUCH DAMAGE.
29 1.1.2.1 pgoyette */
30 1.1.2.1 pgoyette
31 1.1.2.1 pgoyette #include <sys/cdefs.h>
32 1.1.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_80.c,v 1.1.2.4 2018/09/18 10:35:04 pgoyette Exp $");
33 1.1.2.1 pgoyette
34 1.1.2.1 pgoyette #include <sys/param.h>
35 1.1.2.1 pgoyette #include <sys/dirent.h>
36 1.1.2.1 pgoyette #include <sys/kauth.h>
37 1.1.2.1 pgoyette #include <sys/module.h>
38 1.1.2.1 pgoyette #include <sys/kobj.h>
39 1.1.2.1 pgoyette
40 1.1.2.2 pgoyette #include <compat/sys/siginfo.h>
41 1.1.2.2 pgoyette
42 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32.h>
43 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32_syscall.h>
44 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32_syscallargs.h>
45 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32_conv.h>
46 1.1.2.1 pgoyette
47 1.1.2.1 pgoyette #ifdef COMPAT_80
48 1.1.2.2 pgoyette
49 1.1.2.2 pgoyette int netbsd32_80_modctl(struct lwp *, const struct netbsd32_modctl_args *,
50 1.1.2.2 pgoyette register_t *);
51 1.1.2.2 pgoyette
52 1.1.2.1 pgoyette static int
53 1.1.2.1 pgoyette modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg)
54 1.1.2.1 pgoyette {
55 1.1.2.1 pgoyette omodstat_t *oms, *omso;
56 1.1.2.1 pgoyette modinfo_t *mi;
57 1.1.2.1 pgoyette module_t *mod;
58 1.1.2.1 pgoyette vaddr_t addr;
59 1.1.2.1 pgoyette size_t size;
60 1.1.2.1 pgoyette size_t omslen;
61 1.1.2.1 pgoyette size_t used;
62 1.1.2.1 pgoyette int error;
63 1.1.2.1 pgoyette int omscnt;
64 1.1.2.1 pgoyette bool stataddr;
65 1.1.2.1 pgoyette const char *suffix = "...";
66 1.1.2.1 pgoyette
67 1.1.2.1 pgoyette if (cmd != MODCTL_OSTAT)
68 1.1.2.1 pgoyette return EINVAL;
69 1.1.2.1 pgoyette
70 1.1.2.1 pgoyette /* If not privileged, don't expose kernel addresses. */
71 1.1.2.1 pgoyette error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
72 1.1.2.1 pgoyette 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
73 1.1.2.1 pgoyette stataddr = (error == 0);
74 1.1.2.1 pgoyette
75 1.1.2.1 pgoyette kernconfig_lock();
76 1.1.2.1 pgoyette omscnt = 0;
77 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) {
78 1.1.2.1 pgoyette omscnt++;
79 1.1.2.1 pgoyette mi = mod->mod_info;
80 1.1.2.1 pgoyette }
81 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
82 1.1.2.1 pgoyette omscnt++;
83 1.1.2.1 pgoyette mi = mod->mod_info;
84 1.1.2.1 pgoyette }
85 1.1.2.1 pgoyette omslen = omscnt * sizeof(omodstat_t);
86 1.1.2.1 pgoyette omso = kmem_zalloc(omslen, KM_SLEEP);
87 1.1.2.1 pgoyette oms = omso;
88 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) {
89 1.1.2.1 pgoyette mi = mod->mod_info;
90 1.1.2.1 pgoyette strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
91 1.1.2.1 pgoyette if (mi->mi_required != NULL) {
92 1.1.2.1 pgoyette used = strlcpy(oms->oms_required, mi->mi_required,
93 1.1.2.1 pgoyette sizeof(oms->oms_required));
94 1.1.2.1 pgoyette if (used >= sizeof(oms->oms_required)) {
95 1.1.2.1 pgoyette oms->oms_required[sizeof(oms->oms_required) -
96 1.1.2.1 pgoyette strlen(suffix) - 1] = '\0';
97 1.1.2.1 pgoyette strlcat(oms->oms_required, suffix,
98 1.1.2.1 pgoyette sizeof(oms->oms_required));
99 1.1.2.1 pgoyette }
100 1.1.2.1 pgoyette }
101 1.1.2.1 pgoyette if (mod->mod_kobj != NULL && stataddr) {
102 1.1.2.1 pgoyette kobj_stat(mod->mod_kobj, &addr, &size);
103 1.1.2.1 pgoyette oms->oms_addr = addr;
104 1.1.2.1 pgoyette oms->oms_size = size;
105 1.1.2.1 pgoyette }
106 1.1.2.1 pgoyette oms->oms_class = mi->mi_class;
107 1.1.2.1 pgoyette oms->oms_refcnt = mod->mod_refcnt;
108 1.1.2.1 pgoyette oms->oms_source = mod->mod_source;
109 1.1.2.1 pgoyette oms->oms_flags = mod->mod_flags;
110 1.1.2.1 pgoyette oms++;
111 1.1.2.1 pgoyette }
112 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
113 1.1.2.1 pgoyette mi = mod->mod_info;
114 1.1.2.1 pgoyette strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
115 1.1.2.1 pgoyette if (mi->mi_required != NULL) {
116 1.1.2.1 pgoyette used = strlcpy(oms->oms_required, mi->mi_required,
117 1.1.2.1 pgoyette sizeof(oms->oms_required));
118 1.1.2.1 pgoyette if (used >= sizeof(oms->oms_required)) {
119 1.1.2.1 pgoyette oms->oms_required[sizeof(oms->oms_required) -
120 1.1.2.1 pgoyette strlen(suffix) - 1] = '\0';
121 1.1.2.1 pgoyette strlcat(oms->oms_required, suffix,
122 1.1.2.1 pgoyette sizeof(oms->oms_required));
123 1.1.2.1 pgoyette }
124 1.1.2.1 pgoyette }
125 1.1.2.1 pgoyette if (mod->mod_kobj != NULL && stataddr) {
126 1.1.2.1 pgoyette kobj_stat(mod->mod_kobj, &addr, &size);
127 1.1.2.1 pgoyette oms->oms_addr = addr;
128 1.1.2.1 pgoyette oms->oms_size = size;
129 1.1.2.1 pgoyette }
130 1.1.2.1 pgoyette oms->oms_class = mi->mi_class;
131 1.1.2.1 pgoyette oms->oms_refcnt = -1;
132 1.1.2.1 pgoyette KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
133 1.1.2.1 pgoyette oms->oms_source = mod->mod_source;
134 1.1.2.1 pgoyette oms++;
135 1.1.2.1 pgoyette }
136 1.1.2.1 pgoyette kernconfig_unlock();
137 1.1.2.1 pgoyette error = copyout(omso, NETBSD32PTR64(iov->iov_base),
138 1.1.2.1 pgoyette uimin(omslen - sizeof(modstat_t), iov->iov_len));
139 1.1.2.1 pgoyette kmem_free(omso, omslen);
140 1.1.2.1 pgoyette if (error == 0) {
141 1.1.2.1 pgoyette iov->iov_len = omslen - sizeof(modstat_t);
142 1.1.2.1 pgoyette error = copyout(iov, arg, sizeof(*iov));
143 1.1.2.1 pgoyette }
144 1.1.2.1 pgoyette
145 1.1.2.1 pgoyette return error;
146 1.1.2.1 pgoyette }
147 1.1.2.1 pgoyette
148 1.1.2.1 pgoyette int
149 1.1.2.1 pgoyette netbsd32_80_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
150 1.1.2.1 pgoyette register_t *result)
151 1.1.2.1 pgoyette {
152 1.1.2.1 pgoyette /* {
153 1.1.2.1 pgoyette syscallarg(int) cmd;
154 1.1.2.1 pgoyette syscallarg(netbsd32_voidp) arg;
155 1.1.2.1 pgoyette } */
156 1.1.2.1 pgoyette struct netbsd32_iovec iov;
157 1.1.2.1 pgoyette int error;
158 1.1.2.1 pgoyette void *arg;
159 1.1.2.1 pgoyette
160 1.1.2.1 pgoyette arg = SCARG_P32(uap, arg);
161 1.1.2.1 pgoyette
162 1.1.2.1 pgoyette switch (SCARG(uap, cmd)) {
163 1.1.2.1 pgoyette case MODCTL_OSTAT:
164 1.1.2.1 pgoyette error = copyin(arg, &iov, sizeof(iov));
165 1.1.2.1 pgoyette if (error != 0) {
166 1.1.2.1 pgoyette break;
167 1.1.2.1 pgoyette }
168 1.1.2.1 pgoyette error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg);
169 1.1.2.1 pgoyette break;
170 1.1.2.1 pgoyette default:
171 1.1.2.1 pgoyette error = EPASSTHROUGH;
172 1.1.2.1 pgoyette break;
173 1.1.2.1 pgoyette }
174 1.1.2.1 pgoyette
175 1.1.2.1 pgoyette return error;
176 1.1.2.1 pgoyette }
177 1.1.2.1 pgoyette
178 1.1.2.4 pgoyette COMPAT_SET_HOOK(compat32_80_modctl_hook, "nb32_modctl_80", netbsd32_80_modctl);
179 1.1.2.4 pgoyette COMPAT_UNSET_HOOK(compat32_80_modctl_hook);
180 1.1.2.4 pgoyette
181 1.1.2.1 pgoyette MODULE(MODULE_CLASS_EXEC, compat_netbsd32_80, "compat_netbsd32,compat_80");
182 1.1.2.1 pgoyette
183 1.1.2.1 pgoyette static int
184 1.1.2.1 pgoyette compat_netbsd32_80_modcmd(modcmd_t cmd, void *arg)
185 1.1.2.1 pgoyette {
186 1.1.2.1 pgoyette
187 1.1.2.1 pgoyette switch (cmd) {
188 1.1.2.1 pgoyette case MODULE_CMD_INIT:
189 1.1.2.4 pgoyette compat32_80_modctl_hook_set();
190 1.1.2.1 pgoyette return 0;
191 1.1.2.1 pgoyette
192 1.1.2.1 pgoyette case MODULE_CMD_FINI:
193 1.1.2.4 pgoyette compat32_80_modctl_hook_unset();
194 1.1.2.1 pgoyette return 0;
195 1.1.2.1 pgoyette
196 1.1.2.1 pgoyette default:
197 1.1.2.1 pgoyette return ENOTTY;
198 1.1.2.1 pgoyette }
199 1.1.2.1 pgoyette }
200 1.1.2.1 pgoyette #endif /* COMPAT_80 */
201