xref: /netbsd-src/sys/compat/netbsd32/netbsd32_sysctl.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: netbsd32_sysctl.c,v 1.15 2004/04/27 03:49:03 atatat 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.15 2004/04/27 03:49:03 atatat Exp $");
36 
37 #if defined(_KERNEL_OPT)
38 #include "opt_ddb.h"
39 #endif
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/vnode.h>
49 #include <sys/sa.h>
50 #include <sys/syscallargs.h>
51 #include <sys/proc.h>
52 #include <sys/sysctl.h>
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <compat/netbsd32/netbsd32.h>
57 #include <compat/netbsd32/netbsd32_syscall.h>
58 #include <compat/netbsd32/netbsd32_syscallargs.h>
59 #include <compat/netbsd32/netbsd32_conv.h>
60 
61 #if defined(DDB)
62 #include <ddb/ddbvar.h>
63 #endif
64 
65 struct sysctlnode netbsd32_sysctl_root = {
66 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
67 	.sysctl_num = 0,
68 	.sysctl_name = "(netbsd32_root)",
69 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
70 };
71 
72 /*
73  * sysctl helper routine for netbsd32's kern.boottime node
74  */
75 static int
76 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
77 {
78 	struct sysctlnode node;
79 	struct netbsd32_timeval bt32;
80 
81 	netbsd32_from_timeval(&boottime, &bt32);
82 
83 	node = *rnode;
84 	node.sysctl_data = &bt32;
85 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
86 }
87 
88 /*
89  * sysctl helper routine for netbsd32's vm.loadavg node
90  */
91 static int
92 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
93 {
94 	struct sysctlnode node;
95 	struct netbsd32_loadavg av32;
96 
97 	netbsd32_from_loadavg(&av32, &averunnable);
98 
99 	node = *rnode;
100 	node.sysctl_data = &av32;
101 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
102 }
103 
104 SYSCTL_SETUP(netbsd32_sysctl_emul_setup, "sysctl netbsd32 shadow tree setup")
105 {
106 	struct sysctlnode *_root = &netbsd32_sysctl_root;
107 	extern char machine_arch32[];
108 
109 	sysctl_createv(clog, 0, &_root, NULL,
110 		       CTLFLAG_PERMANENT,
111 		       CTLTYPE_NODE, "kern", NULL,
112 		       NULL, 0, NULL, 0,
113 		       CTL_KERN, CTL_EOL);
114 	sysctl_createv(clog, 0, &_root, NULL,
115 		       CTLFLAG_PERMANENT,
116 		       CTLTYPE_STRUCT, "boottime", NULL,
117 		       netbsd32_sysctl_kern_boottime, 0, NULL,
118 		       sizeof(struct netbsd32_timeval),
119 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
120 
121 	sysctl_createv(clog, 0, &_root, NULL,
122 		       CTLFLAG_PERMANENT,
123 		       CTLTYPE_NODE, "vm", NULL,
124 		       NULL, 0, NULL, 0,
125 		       CTL_VM, CTL_EOL);
126 	sysctl_createv(clog, 0, &_root, NULL,
127 		       CTLFLAG_PERMANENT,
128 		       CTLTYPE_STRUCT, "loadavg", NULL,
129 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
130 		       sizeof(struct netbsd32_loadavg),
131 		       CTL_VM, VM_LOADAVG, CTL_EOL);
132 
133 	sysctl_createv(clog, 0, &_root, NULL,
134 		       CTLFLAG_PERMANENT,
135 		       CTLTYPE_NODE, "hw", NULL,
136 		       NULL, 0, NULL, 0,
137 		       CTL_HW, CTL_EOL);
138 	sysctl_createv(clog, 0, &_root, NULL,
139 		       CTLFLAG_PERMANENT,
140 		       CTLTYPE_STRING, "machine_arch", NULL,
141 		       NULL, 0, machine_arch32, 0,
142 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
143 }
144 
145 int
146 netbsd32___sysctl(l, v, retval)
147 	struct lwp *l;
148 	void *v;
149 	register_t *retval;
150 {
151 	struct netbsd32___sysctl_args /* {
152 		syscallarg(netbsd32_intp) name;
153 		syscallarg(u_int) namelen;
154 		syscallarg(netbsd32_voidp) old;
155 		syscallarg(netbsd32_size_tp) oldlenp;
156 		syscallarg(netbsd32_voidp) new;
157 		syscallarg(netbsd32_size_t) newlen;
158 	} */ *uap = v;
159 	struct sysctlnode *pnode;
160 	netbsd32_size_t netbsd32_oldlen;
161 	size_t oldlen, *oldlenp, savelen;
162 	int name[CTL_MAXNAME], error, nerror, *namep;
163 	void *newp, *oldp;
164 
165 	/*
166 	 * get and convert 32 bit size_t to native size_t
167 	 */
168 	namep = NETBSD32PTR64(SCARG(uap, name));
169 	oldp = NETBSD32PTR64(SCARG(uap, old));
170 	newp = NETBSD32PTR64(SCARG(uap, new));
171 	oldlenp = NETBSD32PTR64(SCARG(uap, oldlenp));
172 	oldlen = 0;
173 	if (oldlenp != NULL) {
174 		error = copyin(oldlenp, &netbsd32_oldlen,
175 			       sizeof(netbsd32_oldlen));
176 		if (error)
177 			return (error);
178 		oldlen = netbsd32_oldlen;
179 	}
180 	savelen = oldlen;
181 
182 	/*
183 	 * retrieve name and see if we need to dispatch this query to
184 	 * the shadow tree.  if we find it in the shadow tree,
185 	 * dispatch to there, otherwise NULL means use the built-in
186 	 * default main tree.
187 	 */
188 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
189 		return (EINVAL);
190 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
191         if (error)
192                 return (error);
193 
194 	/*
195 	 * wire old so that copyout() is less likely to fail?
196 	 */
197 	error = sysctl_lock(l, oldp, savelen);
198 	if (error)
199 		return (error);
200 
201 	pnode = &netbsd32_sysctl_root;
202 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
203 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
204 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
205 				oldp, &oldlen,
206 				newp, SCARG(uap, newlen),
207 				&name[0], l, pnode);
208 
209 	/*
210 	 * release the sysctl lock
211 	 */
212 	sysctl_unlock(l);
213 
214 	/*
215 	 * reset caller's oldlen, even if we got an error
216 	 */
217 	if (oldlenp) {
218 		netbsd32_oldlen = oldlen;
219                 nerror = copyout(&netbsd32_oldlen, oldlenp,
220 				 sizeof(netbsd32_oldlen));
221                 if (error == 0)
222                         error = nerror;
223 	}
224 
225 	/*
226 	 * if the only problem is that we weren't given enough space,
227 	 * that's an ENOMEM error
228 	 */
229 	if (error == 0 && oldp != NULL && savelen < oldlen)
230 		error = ENOMEM;
231 
232 	return (error);
233 }
234