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