xref: /netbsd-src/sys/compat/netbsd32/netbsd32_sysctl.c (revision 5dd36a3bc8bf2a9dec29ceb6349550414570c447)
1 /*	$NetBSD: netbsd32_sysctl.c,v 1.41 2020/01/02 15:42:26 thorpej 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.41 2020/01/02 15:42:26 thorpej 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/mount.h>
45 #include <sys/stat.h>
46 #include <sys/time.h>
47 #include <sys/vnode.h>
48 #include <sys/syscallargs.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51 #include <sys/dirent.h>
52 #include <sys/ktrace.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 #include <compat/netbsd32/netbsd32_sysctl.h>
61 
62 #if defined(DDB)
63 #include <ddb/ddbvar.h>
64 #endif
65 
66 struct sysctlnode netbsd32_sysctl_root = {
67 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
68 	.sysctl_num = 0,
69 	.sysctl_name = "(netbsd32_root)",
70 	.sysctl_size = sizeof(struct sysctlnode),
71 };
72 
73 static struct sysctllog *netbsd32_clog;
74 
75 /*
76  * sysctl helper routine for netbsd32's kern.boottime node
77  */
78 static int
79 netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
80 {
81 	struct sysctlnode node;
82 	struct netbsd32_timespec bt32;
83 	struct timespec ts;
84 
85 	getnanoboottime(&ts);
86 	netbsd32_from_timespec(&ts, &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 static int
110 sysctl_hw_machine_arch32(SYSCTLFN_ARGS)
111 {
112 	struct sysctlnode node = *rnode;
113 #ifndef PROC_MACHINE_ARCH32
114 	extern const char machine_arch32[];
115 #define PROC_MACHINE_ARCH32(P)	__UNCONST(machine_arch32)
116 #endif
117 
118 	node.sysctl_data = PROC_MACHINE_ARCH32(l->l_proc);
119 	node.sysctl_size = strlen(node.sysctl_data) + 1;
120 	return sysctl_lookup(SYSCTLFN_CALL(&node));
121 }
122 
123 void
124 netbsd32_sysctl_init(void)
125 {
126 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
127 	extern const char machine32[];
128 
129 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
130 		       CTLFLAG_PERMANENT,
131 		       CTLTYPE_NODE, "kern", NULL,
132 		       NULL, 0, NULL, 0,
133 		       CTL_KERN, CTL_EOL);
134 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
135 		       CTLFLAG_PERMANENT,
136 		       CTLTYPE_STRUCT, "boottime", NULL,
137 		       netbsd32_sysctl_kern_boottime, 0, NULL,
138 		       sizeof(struct netbsd32_timeval),
139 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
140 
141 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
142 		       CTLFLAG_PERMANENT,
143 		       CTLTYPE_NODE, "vm", NULL,
144 		       NULL, 0, NULL, 0,
145 		       CTL_VM, CTL_EOL);
146 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
147 		       CTLFLAG_PERMANENT,
148 		       CTLTYPE_STRUCT, "loadavg", NULL,
149 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
150 		       sizeof(struct netbsd32_loadavg),
151 		       CTL_VM, VM_LOADAVG, CTL_EOL);
152 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
153 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
154 		       CTLTYPE_INT, "maxaddress",
155 		       SYSCTL_DESCR("Maximum user address"),
156 		       NULL, VM_MAXUSER_ADDRESS32, NULL, 0,
157 		       CTL_VM, VM_MAXADDRESS, CTL_EOL);
158 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
159 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
160 		       CTLTYPE_INT, "minaddress",
161 		       SYSCTL_DESCR("Minimum user address"),
162 		       NULL, VM_MIN_ADDRESS, NULL, 0,
163 		       CTL_VM, VM_MINADDRESS, CTL_EOL);
164 
165 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
166 		       CTLFLAG_PERMANENT,
167 		       CTLTYPE_NODE, "hw", NULL,
168 		       NULL, 0, NULL, 0,
169 		       CTL_HW, CTL_EOL);
170 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
171 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
172 		       CTLTYPE_INT, "alignbytes", NULL,
173 		       NULL, ALIGNBYTES32, NULL, 0,
174 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
175 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
176 		       CTLFLAG_PERMANENT,
177 		       CTLTYPE_STRING, "machine", NULL,
178 		       NULL, 0, __UNCONST(&machine32), 0,
179 		       CTL_HW, HW_MACHINE, CTL_EOL);
180 	sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
181 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
182 		       CTLTYPE_STRING, "machine_arch", NULL,
183 		       sysctl_hw_machine_arch32, 0, NULL, 0,
184 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
185 }
186 
187 void
188 netbsd32_sysctl_fini(void)
189 {
190 
191 	sysctl_teardown(&netbsd32_clog);
192 	sysctl_free(&netbsd32_sysctl_root);
193 }
194 
195 int
196 netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
197 {
198 	/* {
199 		syscallarg(netbsd32_intp) name;
200 		syscallarg(u_int) namelen;
201 		syscallarg(netbsd32_voidp) old;
202 		syscallarg(netbsd32_size_tp) oldlenp;
203 		syscallarg(netbsd32_voidp) new;
204 		syscallarg(netbsd32_size_t) newlen;
205 	} */
206 	const struct sysctlnode *pnode;
207 	netbsd32_size_t netbsd32_oldlen;
208 	size_t oldlen, *oldlenp, savelen;
209 	int name[CTL_MAXNAME], error, nerror, *namep;
210 	void *newp, *oldp;
211 
212 	/*
213 	 * get and convert 32 bit size_t to native size_t
214 	 */
215 	namep = SCARG_P32(uap, name);
216 	oldp = SCARG_P32(uap, oldv);
217 	newp = SCARG_P32(uap, newv);
218 	oldlenp = SCARG_P32(uap, oldlenp);
219 	oldlen = 0;
220 	if (oldlenp != NULL) {
221 		error = copyin(oldlenp, &netbsd32_oldlen,
222 			       sizeof(netbsd32_oldlen));
223 		if (error)
224 			return (error);
225 		oldlen = netbsd32_oldlen;
226 	}
227 	savelen = oldlen;
228 
229 	/*
230 	 * retrieve name and see if we need to dispatch this query to
231 	 * the shadow tree.  if we find it in the shadow tree,
232 	 * dispatch to there, otherwise NULL means use the built-in
233 	 * default main tree.
234 	 */
235 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
236 		return (EINVAL);
237 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
238         if (error)
239                 return (error);
240 
241 	ktrmib(name, SCARG(uap, namelen));
242 
243 	sysctl_lock(newp != NULL);
244 	pnode = &netbsd32_sysctl_root;
245 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
246 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
247 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
248 				oldp, &oldlen,
249 				newp, SCARG(uap, newlen),
250 				&name[0], l, pnode);
251 	sysctl_unlock();
252 
253 	/*
254 	 * reset caller's oldlen, even if we got an error
255 	 */
256 	if (oldlenp) {
257 		netbsd32_oldlen = oldlen;
258                 nerror = copyout(&netbsd32_oldlen, oldlenp,
259 				 sizeof(netbsd32_oldlen));
260                 if (error == 0)
261                         error = nerror;
262 	}
263 
264 	/*
265 	 * if the only problem is that we weren't given enough space,
266 	 * that's an ENOMEM error
267 	 */
268 	if (error == 0 && oldp != NULL && savelen < oldlen)
269 		error = ENOMEM;
270 
271 	return (error);
272 }
273