xref: /netbsd-src/sys/compat/netbsd32/netbsd32_sysctl.c (revision b7ae68fde0d8ef1c03714e8bbb1ee7c6118ea93b)
1 /*	$NetBSD: netbsd32_sysctl.c,v 1.22 2006/09/23 22:12:00 manu Exp $	*/
2 
3 /*
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Brown.
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  * 3. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.22 2006/09/23 22:12:00 manu Exp $");
36 
37 #if defined(_KERNEL_OPT)
38 #include "opt_ddb.h"
39 #include "opt_ktrace.h"
40 #endif
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/mount.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/vnode.h>
50 #include <sys/sa.h>
51 #include <sys/syscallargs.h>
52 #include <sys/proc.h>
53 #include <sys/sysctl.h>
54 #include <sys/dirent.h>
55 #ifdef KTRACE
56 #include <sys/ktrace.h>
57 #endif
58 
59 #include <uvm/uvm_extern.h>
60 
61 #include <compat/netbsd32/netbsd32.h>
62 #include <compat/netbsd32/netbsd32_syscall.h>
63 #include <compat/netbsd32/netbsd32_syscallargs.h>
64 #include <compat/netbsd32/netbsd32_conv.h>
65 
66 #if defined(DDB)
67 #include <ddb/ddbvar.h>
68 #endif
69 
70 struct sysctlnode netbsd32_sysctl_root = {
71 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
72 	.sysctl_num = 0,
73 	.sysctl_name = "(netbsd32_root)",
74 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
75 };
76 
77 /*
78  * sysctl helper routine for netbsd32's kern.boottime node
79  */
80 static int
81 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
82 {
83 	struct sysctlnode node;
84 	struct netbsd32_timeval bt32;
85 
86 	netbsd32_from_timeval(&boottime, &bt32);
87 
88 	node = *rnode;
89 	node.sysctl_data = &bt32;
90 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
91 }
92 
93 /*
94  * sysctl helper routine for netbsd32's vm.loadavg node
95  */
96 static int
97 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
98 {
99 	struct sysctlnode node;
100 	struct netbsd32_loadavg av32;
101 
102 	netbsd32_from_loadavg(&av32, &averunnable);
103 
104 	node = *rnode;
105 	node.sysctl_data = &av32;
106 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
107 }
108 
109 SYSCTL_SETUP(netbsd32_sysctl_emul_setup, "sysctl netbsd32 shadow tree setup")
110 {
111 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
112 	extern const char machine_arch32[];
113 	extern const char machine32[];
114 
115 	sysctl_createv(clog, 0, &_root, NULL,
116 		       CTLFLAG_PERMANENT,
117 		       CTLTYPE_NODE, "kern", NULL,
118 		       NULL, 0, NULL, 0,
119 		       CTL_KERN, CTL_EOL);
120 	sysctl_createv(clog, 0, &_root, NULL,
121 		       CTLFLAG_PERMANENT,
122 		       CTLTYPE_STRUCT, "boottime", NULL,
123 		       netbsd32_sysctl_kern_boottime, 0, NULL,
124 		       sizeof(struct netbsd32_timeval),
125 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
126 
127 	sysctl_createv(clog, 0, &_root, NULL,
128 		       CTLFLAG_PERMANENT,
129 		       CTLTYPE_NODE, "vm", NULL,
130 		       NULL, 0, NULL, 0,
131 		       CTL_VM, CTL_EOL);
132 	sysctl_createv(clog, 0, &_root, NULL,
133 		       CTLFLAG_PERMANENT,
134 		       CTLTYPE_STRUCT, "loadavg", NULL,
135 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
136 		       sizeof(struct netbsd32_loadavg),
137 		       CTL_VM, VM_LOADAVG, CTL_EOL);
138 
139 	sysctl_createv(clog, 0, &_root, NULL,
140 		       CTLFLAG_PERMANENT,
141 		       CTLTYPE_NODE, "hw", NULL,
142 		       NULL, 0, NULL, 0,
143 		       CTL_HW, CTL_EOL);
144 	sysctl_createv(clog, 0, &_root, NULL,
145 		       CTLFLAG_PERMANENT,
146 		       CTLTYPE_STRING, "machine", NULL,
147 		       NULL, 0, &machine32, 0,
148 		       CTL_HW, HW_MACHINE, CTL_EOL);
149 	sysctl_createv(clog, 0, &_root, NULL,
150 		       CTLFLAG_PERMANENT,
151 		       CTLTYPE_STRING, "machine_arch", NULL,
152 		       NULL, 0, &machine_arch32, 0,
153 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
154 }
155 
156 int
157 netbsd32___sysctl(l, v, retval)
158 	struct lwp *l;
159 	void *v;
160 	register_t *retval;
161 {
162 	struct netbsd32___sysctl_args /* {
163 		syscallarg(netbsd32_intp) name;
164 		syscallarg(u_int) namelen;
165 		syscallarg(netbsd32_voidp) old;
166 		syscallarg(netbsd32_size_tp) oldlenp;
167 		syscallarg(netbsd32_voidp) new;
168 		syscallarg(netbsd32_size_t) newlen;
169 	} */ *uap = v;
170 	const struct sysctlnode *pnode;
171 	netbsd32_size_t netbsd32_oldlen;
172 	size_t oldlen, *oldlenp, savelen;
173 	int name[CTL_MAXNAME], error, nerror, *namep;
174 	void *newp, *oldp;
175 
176 	/*
177 	 * get and convert 32 bit size_t to native size_t
178 	 */
179 	namep = NETBSD32PTR64(SCARG(uap, name));
180 	oldp = NETBSD32PTR64(SCARG(uap, old));
181 	newp = NETBSD32PTR64(SCARG(uap, new));
182 	oldlenp = NETBSD32PTR64(SCARG(uap, oldlenp));
183 	oldlen = 0;
184 	if (oldlenp != NULL) {
185 		error = copyin(oldlenp, &netbsd32_oldlen,
186 			       sizeof(netbsd32_oldlen));
187 		if (error)
188 			return (error);
189 		oldlen = netbsd32_oldlen;
190 	}
191 	savelen = oldlen;
192 
193 	/*
194 	 * retrieve name and see if we need to dispatch this query to
195 	 * the shadow tree.  if we find it in the shadow tree,
196 	 * dispatch to there, otherwise NULL means use the built-in
197 	 * default main tree.
198 	 */
199 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
200 		return (EINVAL);
201 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
202         if (error)
203                 return (error);
204 
205 #ifdef KTRACE
206 	if (KTRPOINT(l->l_proc, KTR_MIB))
207 		ktrmib(l, name, SCARG(uap, namelen));
208 #endif
209 
210 	/*
211 	 * wire old so that copyout() is less likely to fail?
212 	 */
213 	error = sysctl_lock(l, oldp, savelen);
214 	if (error)
215 		return (error);
216 
217 	pnode = &netbsd32_sysctl_root;
218 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
219 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
220 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
221 				oldp, &oldlen,
222 				newp, SCARG(uap, newlen),
223 				&name[0], l, pnode);
224 
225 	/*
226 	 * release the sysctl lock
227 	 */
228 	sysctl_unlock(l);
229 
230 	/*
231 	 * reset caller's oldlen, even if we got an error
232 	 */
233 	if (oldlenp) {
234 		netbsd32_oldlen = oldlen;
235                 nerror = copyout(&netbsd32_oldlen, oldlenp,
236 				 sizeof(netbsd32_oldlen));
237                 if (error == 0)
238                         error = nerror;
239 	}
240 
241 	/*
242 	 * if the only problem is that we weren't given enough space,
243 	 * that's an ENOMEM error
244 	 */
245 	if (error == 0 && oldp != NULL && savelen < oldlen)
246 		error = ENOMEM;
247 
248 	return (error);
249 }
250