netbsd32_sysctl.c revision 1.1.4.6 1 /* $NetBSD: netbsd32_sysctl.c,v 1.1.4.6 2002/06/20 03:43:11 nathanw Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001 Matthew R. Green
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. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.1.4.6 2002/06/20 03:43:11 nathanw Exp $");
33
34 #if defined(_KERNEL_OPT)
35 #include "opt_ddb.h"
36 #endif
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/stat.h>
44 #include <sys/time.h>
45 #include <sys/vnode.h>
46 #include <sys/sa.h>
47 #include <sys/syscallargs.h>
48 #include <sys/proc.h>
49 #define __SYSCTL_PRIVATE
50 #include <sys/sysctl.h>
51
52 #include <uvm/uvm_extern.h>
53
54 #include <compat/netbsd32/netbsd32.h>
55 #include <compat/netbsd32/netbsd32_syscall.h>
56 #include <compat/netbsd32/netbsd32_syscallargs.h>
57 #include <compat/netbsd32/netbsd32_conv.h>
58
59 #if defined(DDB)
60 #include <ddb/ddbvar.h>
61 #endif
62
63 int uvm_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
64 int kern_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
65 int hw_sysctl32(int *, u_int, void *, size_t *, void *, size_t, struct proc *);
66
67 /*
68 * uvm_sysctl32: sysctl hook into UVM system, handling special 32-bit
69 * sensitive calls.
70 */
71 int
72 uvm_sysctl32(name, namelen, oldp, oldlenp, newp, newlen, p)
73 int *name;
74 u_int namelen;
75 void *oldp;
76 size_t *oldlenp;
77 void *newp;
78 size_t newlen;
79 struct proc *p;
80 {
81 struct netbsd32_loadavg av32;
82
83 /* all sysctl names at this level are terminal */
84 if (namelen != 1)
85 return (ENOTDIR); /* overloaded */
86
87 switch (name[0]) {
88 case VM_LOADAVG:
89 netbsd32_from_loadavg(&av32, &averunnable);
90 return (sysctl_rdstruct(oldp, oldlenp, newp, &av32,
91 sizeof(av32)));
92
93 default:
94 return (EOPNOTSUPP);
95 }
96 /* NOTREACHED */
97 }
98
99 /*
100 * kern_sysctl32: sysctl hook into KERN system, handling special 32-bit
101 * sensitive calls.
102 */
103 int
104 kern_sysctl32(name, namelen, oldp, oldlenp, newp, newlen, p)
105 int *name;
106 u_int namelen;
107 void *oldp;
108 size_t *oldlenp;
109 void *newp;
110 size_t newlen;
111 struct proc *p;
112 {
113 struct netbsd32_timeval bt32;
114
115 /* All sysctl names at this level, except for a few, are terminal. */
116 switch (name[0]) {
117 #if 0
118 case KERN_PROC:
119 case KERN_PROC2:
120 case KERN_PROF:
121 case KERN_MBUF:
122 case KERN_PROC_ARGS:
123 case KERN_SYSVIPC_INFO:
124 /* Not terminal. */
125 break;
126 #endif
127 default:
128 if (namelen != 1)
129 return (ENOTDIR); /* overloaded */
130 }
131
132 switch (name[0]) {
133 case KERN_BOOTTIME:
134 netbsd32_from_timeval(&boottime, &bt32);
135 return (sysctl_rdstruct(oldp, oldlenp, newp, &bt32,
136 sizeof(struct netbsd32_timeval)));
137
138 default:
139 return (EOPNOTSUPP);
140 }
141 /* NOTREACHED */
142 }
143
144 /*
145 * hardware related system variables.
146 */
147 int
148 hw_sysctl32(int *name, u_int namelen, void *oldp, size_t *oldlenp,
149 void *newp, size_t newlen, struct proc *p)
150 {
151 extern char machine_arch32[];
152
153 switch (name[0]) {
154 case HW_MACHINE_ARCH:
155 return (sysctl_rdstring(oldp, oldlenp, newp, machine_arch32));
156 default:
157 return (EOPNOTSUPP);
158 }
159 /* NOTREACHED */
160 }
161
162 int
163 netbsd32___sysctl(p, v, retval)
164 struct proc *p;
165 void *v;
166 register_t *retval;
167 {
168 struct netbsd32___sysctl_args /* {
169 syscallarg(netbsd32_intp) name;
170 syscallarg(u_int) namelen;
171 syscallarg(netbsd32_voidp) old;
172 syscallarg(netbsd32_size_tp) oldlenp;
173 syscallarg(netbsd32_voidp) new;
174 syscallarg(netbsd32_size_t) newlen;
175 } */ *uap = v;
176 int error;
177 netbsd32_size_t savelen = 0;
178 size_t oldlen = 0;
179 sysctlfn *fn;
180 int name[CTL_MAXNAME];
181
182 /*
183 * Some of these sysctl functions do their own copyin/copyout.
184 * We need to disable or emulate the ones that need their
185 * arguments converted.
186 */
187
188 if (SCARG(uap, new) != NULL &&
189 (error = suser(p->p_ucred, &p->p_acflag)))
190 return (error);
191 /*
192 * all top-level sysctl names are non-terminal
193 */
194 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 2)
195 return (EINVAL);
196 error = copyin((caddr_t)(u_long)SCARG(uap, name), &name,
197 SCARG(uap, namelen) * sizeof(int));
198 if (error)
199 return (error);
200
201 switch (name[0]) {
202 case CTL_KERN:
203 switch (name[1]) {
204 #if 0
205 case KERN_FILE:
206 case KERN_NTPTIME:
207 case KERN_SYSVIPC_INFO:
208 #endif
209 case KERN_BOOTTIME:
210 fn = kern_sysctl32;
211 break;
212 default:
213 fn = kern_sysctl;
214 break;
215 }
216 break;
217 case CTL_HW:
218 switch (name[1]) {
219 case HW_MACHINE_ARCH:
220 fn = hw_sysctl32;
221 break;
222 default:
223 fn = hw_sysctl;
224 break;
225 }
226 break;
227 case CTL_VM:
228 switch (name[1]) {
229 case VM_LOADAVG:
230 fn = uvm_sysctl32; /* need to convert a `long' */
231 break;
232 default:
233 fn = uvm_sysctl;
234 break;
235 }
236 break;
237 case CTL_NET:
238 fn = net_sysctl;
239 break;
240 case CTL_VFS:
241 fn = vfs_sysctl;
242 break;
243 case CTL_MACHDEP:
244 fn = cpu_sysctl;
245 break;
246 #ifdef DEBUG
247 case CTL_DEBUG:
248 fn = debug_sysctl;
249 break;
250 #endif
251 #ifdef DDB
252 case CTL_DDB:
253 fn = ddb_sysctl;
254 break;
255 #endif
256 case CTL_PROC:
257 fn = proc_sysctl;
258 break;
259 default:
260 return (EOPNOTSUPP);
261 }
262
263 /*
264 * XXX Hey, we wire `old', but what about `new'?
265 */
266
267 if (SCARG(uap, oldlenp) &&
268 (error = copyin((caddr_t)(u_long)SCARG(uap, oldlenp), &savelen,
269 sizeof(savelen))))
270 return (error);
271 if (SCARG(uap, old) != NULL) {
272 error = lockmgr(&sysctl_memlock, LK_EXCLUSIVE, NULL);
273 if (error)
274 return (error);
275 error = uvm_vslock(p, (void *)(vaddr_t)SCARG(uap, old), savelen,
276 VM_PROT_WRITE);
277 if (error) {
278 (void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
279 return error;
280 }
281 oldlen = savelen;
282 }
283 error = (*fn)(name + 1, SCARG(uap, namelen) - 1,
284 (void *)(u_long)SCARG(uap, old), &oldlen,
285 (void *)(u_long)SCARG(uap, new), SCARG(uap, newlen), p);
286 if (SCARG(uap, old) != NULL) {
287 uvm_vsunlock(p, (void *)(u_long)SCARG(uap, old), savelen);
288 (void) lockmgr(&sysctl_memlock, LK_RELEASE, NULL);
289 }
290 savelen = oldlen;
291 if (error)
292 return (error);
293 if (SCARG(uap, oldlenp))
294 error = copyout(&savelen,
295 (caddr_t)(u_long)SCARG(uap, oldlenp), sizeof(savelen));
296 return (error);
297 }
298