netbsd32_compat_80.c revision 1.1.2.1 1 1.1.2.1 pgoyette /* $NetBSD: netbsd32_compat_80.c,v 1.1.2.1 2018/09/10 22:50:51 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.1 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_compat_80.c,v 1.1.2.1 2018/09/10 22:50:51 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.1 pgoyette #include <compat/netbsd32/netbsd32.h>
41 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32_syscall.h>
42 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32_syscallargs.h>
43 1.1.2.1 pgoyette #include <compat/netbsd32/netbsd32_conv.h>
44 1.1.2.1 pgoyette
45 1.1.2.1 pgoyette #ifdef COMPAT_80
46 1.1.2.1 pgoyette static int
47 1.1.2.1 pgoyette modctl32_handle_ostat(int cmd, struct netbsd32_iovec *iov, void *arg)
48 1.1.2.1 pgoyette {
49 1.1.2.1 pgoyette omodstat_t *oms, *omso;
50 1.1.2.1 pgoyette modinfo_t *mi;
51 1.1.2.1 pgoyette module_t *mod;
52 1.1.2.1 pgoyette vaddr_t addr;
53 1.1.2.1 pgoyette size_t size;
54 1.1.2.1 pgoyette size_t omslen;
55 1.1.2.1 pgoyette size_t used;
56 1.1.2.1 pgoyette int error;
57 1.1.2.1 pgoyette int omscnt;
58 1.1.2.1 pgoyette bool stataddr;
59 1.1.2.1 pgoyette const char *suffix = "...";
60 1.1.2.1 pgoyette
61 1.1.2.1 pgoyette if (cmd != MODCTL_OSTAT)
62 1.1.2.1 pgoyette return EINVAL;
63 1.1.2.1 pgoyette
64 1.1.2.1 pgoyette /* If not privileged, don't expose kernel addresses. */
65 1.1.2.1 pgoyette error = kauth_authorize_system(kauth_cred_get(), KAUTH_SYSTEM_MODULE,
66 1.1.2.1 pgoyette 0, (void *)(uintptr_t)MODCTL_STAT, NULL, NULL);
67 1.1.2.1 pgoyette stataddr = (error == 0);
68 1.1.2.1 pgoyette
69 1.1.2.1 pgoyette kernconfig_lock();
70 1.1.2.1 pgoyette omscnt = 0;
71 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) {
72 1.1.2.1 pgoyette omscnt++;
73 1.1.2.1 pgoyette mi = mod->mod_info;
74 1.1.2.1 pgoyette }
75 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
76 1.1.2.1 pgoyette omscnt++;
77 1.1.2.1 pgoyette mi = mod->mod_info;
78 1.1.2.1 pgoyette }
79 1.1.2.1 pgoyette omslen = omscnt * sizeof(omodstat_t);
80 1.1.2.1 pgoyette omso = kmem_zalloc(omslen, KM_SLEEP);
81 1.1.2.1 pgoyette oms = omso;
82 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_list, mod_chain) {
83 1.1.2.1 pgoyette mi = mod->mod_info;
84 1.1.2.1 pgoyette strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
85 1.1.2.1 pgoyette if (mi->mi_required != NULL) {
86 1.1.2.1 pgoyette used = strlcpy(oms->oms_required, mi->mi_required,
87 1.1.2.1 pgoyette sizeof(oms->oms_required));
88 1.1.2.1 pgoyette if (used >= sizeof(oms->oms_required)) {
89 1.1.2.1 pgoyette oms->oms_required[sizeof(oms->oms_required) -
90 1.1.2.1 pgoyette strlen(suffix) - 1] = '\0';
91 1.1.2.1 pgoyette strlcat(oms->oms_required, suffix,
92 1.1.2.1 pgoyette sizeof(oms->oms_required));
93 1.1.2.1 pgoyette }
94 1.1.2.1 pgoyette }
95 1.1.2.1 pgoyette if (mod->mod_kobj != NULL && stataddr) {
96 1.1.2.1 pgoyette kobj_stat(mod->mod_kobj, &addr, &size);
97 1.1.2.1 pgoyette oms->oms_addr = addr;
98 1.1.2.1 pgoyette oms->oms_size = size;
99 1.1.2.1 pgoyette }
100 1.1.2.1 pgoyette oms->oms_class = mi->mi_class;
101 1.1.2.1 pgoyette oms->oms_refcnt = mod->mod_refcnt;
102 1.1.2.1 pgoyette oms->oms_source = mod->mod_source;
103 1.1.2.1 pgoyette oms->oms_flags = mod->mod_flags;
104 1.1.2.1 pgoyette oms++;
105 1.1.2.1 pgoyette }
106 1.1.2.1 pgoyette TAILQ_FOREACH(mod, &module_builtins, mod_chain) {
107 1.1.2.1 pgoyette mi = mod->mod_info;
108 1.1.2.1 pgoyette strlcpy(oms->oms_name, mi->mi_name, sizeof(oms->oms_name));
109 1.1.2.1 pgoyette if (mi->mi_required != NULL) {
110 1.1.2.1 pgoyette used = strlcpy(oms->oms_required, mi->mi_required,
111 1.1.2.1 pgoyette sizeof(oms->oms_required));
112 1.1.2.1 pgoyette if (used >= sizeof(oms->oms_required)) {
113 1.1.2.1 pgoyette oms->oms_required[sizeof(oms->oms_required) -
114 1.1.2.1 pgoyette strlen(suffix) - 1] = '\0';
115 1.1.2.1 pgoyette strlcat(oms->oms_required, suffix,
116 1.1.2.1 pgoyette sizeof(oms->oms_required));
117 1.1.2.1 pgoyette }
118 1.1.2.1 pgoyette }
119 1.1.2.1 pgoyette if (mod->mod_kobj != NULL && stataddr) {
120 1.1.2.1 pgoyette kobj_stat(mod->mod_kobj, &addr, &size);
121 1.1.2.1 pgoyette oms->oms_addr = addr;
122 1.1.2.1 pgoyette oms->oms_size = size;
123 1.1.2.1 pgoyette }
124 1.1.2.1 pgoyette oms->oms_class = mi->mi_class;
125 1.1.2.1 pgoyette oms->oms_refcnt = -1;
126 1.1.2.1 pgoyette KASSERT(mod->mod_source == MODULE_SOURCE_KERNEL);
127 1.1.2.1 pgoyette oms->oms_source = mod->mod_source;
128 1.1.2.1 pgoyette oms++;
129 1.1.2.1 pgoyette }
130 1.1.2.1 pgoyette kernconfig_unlock();
131 1.1.2.1 pgoyette error = copyout(omso, NETBSD32PTR64(iov->iov_base),
132 1.1.2.1 pgoyette uimin(omslen - sizeof(modstat_t), iov->iov_len));
133 1.1.2.1 pgoyette kmem_free(omso, omslen);
134 1.1.2.1 pgoyette if (error == 0) {
135 1.1.2.1 pgoyette iov->iov_len = omslen - sizeof(modstat_t);
136 1.1.2.1 pgoyette error = copyout(iov, arg, sizeof(*iov));
137 1.1.2.1 pgoyette }
138 1.1.2.1 pgoyette
139 1.1.2.1 pgoyette return error;
140 1.1.2.1 pgoyette }
141 1.1.2.1 pgoyette
142 1.1.2.1 pgoyette int
143 1.1.2.1 pgoyette netbsd32_80_modctl(struct lwp *lwp, const struct netbsd32_modctl_args *uap,
144 1.1.2.1 pgoyette register_t *result)
145 1.1.2.1 pgoyette {
146 1.1.2.1 pgoyette /* {
147 1.1.2.1 pgoyette syscallarg(int) cmd;
148 1.1.2.1 pgoyette syscallarg(netbsd32_voidp) arg;
149 1.1.2.1 pgoyette } */
150 1.1.2.1 pgoyette char buf[MAXMODNAME];
151 1.1.2.1 pgoyette struct netbsd32_iovec iov;
152 1.1.2.1 pgoyette struct netbsd32_modctl_load ml;
153 1.1.2.1 pgoyette int error;
154 1.1.2.1 pgoyette void *arg;
155 1.1.2.1 pgoyette #ifdef MODULAR
156 1.1.2.1 pgoyette uintptr_t loadtype;
157 1.1.2.1 pgoyette #endif
158 1.1.2.1 pgoyette
159 1.1.2.1 pgoyette arg = SCARG_P32(uap, arg);
160 1.1.2.1 pgoyette
161 1.1.2.1 pgoyette switch (SCARG(uap, cmd)) {
162 1.1.2.1 pgoyette case MODCTL_OSTAT:
163 1.1.2.1 pgoyette error = copyin(arg, &iov, sizeof(iov));
164 1.1.2.1 pgoyette if (error != 0) {
165 1.1.2.1 pgoyette break;
166 1.1.2.1 pgoyette }
167 1.1.2.1 pgoyette error = modctl32_handle_ostat(SCARG(uap, cmd), &iov, arg);
168 1.1.2.1 pgoyette break;
169 1.1.2.1 pgoyette default:
170 1.1.2.1 pgoyette error = EPASSTHROUGH;
171 1.1.2.1 pgoyette break;
172 1.1.2.1 pgoyette }
173 1.1.2.1 pgoyette
174 1.1.2.1 pgoyette return error;
175 1.1.2.1 pgoyette }
176 1.1.2.1 pgoyette
177 1.1.2.1 pgoyette MODULE(MODULE_CLASS_EXEC, compat_netbsd32_80, "compat_netbsd32,compat_80");
178 1.1.2.1 pgoyette
179 1.1.2.1 pgoyette static int
180 1.1.2.1 pgoyette compat_netbsd32_80_modcmd(modcmd_t cmd, void *arg)
181 1.1.2.1 pgoyette {
182 1.1.2.1 pgoyette static int (*orig_netbsd32_80_modctl)(struct lwp *,
183 1.1.2.1 pgoyette const struct netbsd32_modctl_args *, register_t *);
184 1.1.2.1 pgoyette
185 1.1.2.1 pgoyette switch (cmd) {
186 1.1.2.1 pgoyette case MODULE_CMD_INIT:
187 1.1.2.1 pgoyette orig_netbsd32_80_modctl = vec_compat32_80_modctl;
188 1.1.2.1 pgoyette vec_compat32_80_modctl = netbsd32_80_modctl;
189 1.1.2.1 pgoyette return 0;
190 1.1.2.1 pgoyette
191 1.1.2.1 pgoyette case MODULE_CMD_FINI:
192 1.1.2.1 pgoyette vec_compat32_80_modctl = orig_netbsd32_80_modctl;
193 1.1.2.1 pgoyette return 0;
194 1.1.2.1 pgoyette
195 1.1.2.1 pgoyette default:
196 1.1.2.1 pgoyette return ENOTTY;
197 1.1.2.1 pgoyette }
198 1.1.2.1 pgoyette }
199 1.1.2.1 pgoyette #endif /* COMPAT_80 */
200 1.1.2.1 pgoyette
201 1.1.2.1 pgoyette vec_compat32_80_modctl = compat32_80_modctl_compat_stub;
202