xref: /netbsd-src/sys/compat/netbsd32/netbsd32_sysctl.c (revision 33881f779a77dce6440bdc44610d94de75bebefe)
1 /*	$NetBSD: netbsd32_sysctl.c,v 1.43 2020/03/17 17:16:26 pgoyette 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.43 2020/03/17 17:16:26 pgoyette 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 
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 	.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_timespec bt32;
80 	struct timespec ts;
81 
82 	getnanoboottime(&ts);
83 	netbsd32_from_timespec(&ts, &bt32);
84 
85 	node = *rnode;
86 	node.sysctl_data = &bt32;
87 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
88 }
89 
90 /*
91  * sysctl helper routine for netbsd32's vm.loadavg node
92  */
93 static int
94 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
95 {
96 	struct sysctlnode node;
97 	struct netbsd32_loadavg av32;
98 
99 	netbsd32_from_loadavg(&av32, &averunnable);
100 
101 	node = *rnode;
102 	node.sysctl_data = &av32;
103 	return (sysctl_lookup(SYSCTLFN_CALL(&node)));
104 }
105 
106 static int
107 sysctl_hw_machine_arch32(SYSCTLFN_ARGS)
108 {
109 	struct sysctlnode node = *rnode;
110 #ifndef PROC_MACHINE_ARCH32
111 	extern const char machine_arch32[];
112 #define PROC_MACHINE_ARCH32(P)	__UNCONST(machine_arch32)
113 #endif
114 
115 	node.sysctl_data = PROC_MACHINE_ARCH32(l->l_proc);
116 	node.sysctl_size = strlen(node.sysctl_data) + 1;
117 	return sysctl_lookup(SYSCTLFN_CALL(&node));
118 }
119 
120 SYSCTL_SETUP(netbsd32_sysctl_emul_setup, "netbsd32 shadow tree")
121 {
122 	const struct sysctlnode *_root = &netbsd32_sysctl_root;
123 	extern const char machine32[];
124 
125 	sysctl_createv(clog, 0, &_root, NULL,
126 		       CTLFLAG_PERMANENT,
127 		       CTLTYPE_NODE, "kern", NULL,
128 		       NULL, 0, NULL, 0,
129 		       CTL_KERN, CTL_EOL);
130 	sysctl_createv(clog, 0, &_root, NULL,
131 		       CTLFLAG_PERMANENT,
132 		       CTLTYPE_STRUCT, "boottime", NULL,
133 		       netbsd32_sysctl_kern_boottime, 0, NULL,
134 		       sizeof(struct netbsd32_timeval),
135 		       CTL_KERN, KERN_BOOTTIME, CTL_EOL);
136 
137 	sysctl_createv(clog, 0, &_root, NULL,
138 		       CTLFLAG_PERMANENT,
139 		       CTLTYPE_NODE, "vm", NULL,
140 		       NULL, 0, NULL, 0,
141 		       CTL_VM, CTL_EOL);
142 	sysctl_createv(clog, 0, &_root, NULL,
143 		       CTLFLAG_PERMANENT,
144 		       CTLTYPE_STRUCT, "loadavg", NULL,
145 		       netbsd32_sysctl_vm_loadavg, 0, NULL,
146 		       sizeof(struct netbsd32_loadavg),
147 		       CTL_VM, VM_LOADAVG, CTL_EOL);
148 	sysctl_createv(clog, 0, &_root, NULL,
149 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
150 		       CTLTYPE_INT, "maxaddress",
151 		       SYSCTL_DESCR("Maximum user address"),
152 		       NULL, VM_MAXUSER_ADDRESS32, NULL, 0,
153 		       CTL_VM, VM_MAXADDRESS, CTL_EOL);
154 	sysctl_createv(clog, 0, &_root, NULL,
155 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
156 		       CTLTYPE_INT, "minaddress",
157 		       SYSCTL_DESCR("Minimum user address"),
158 		       NULL, VM_MIN_ADDRESS, NULL, 0,
159 		       CTL_VM, VM_MINADDRESS, CTL_EOL);
160 
161 	sysctl_createv(clog, 0, &_root, NULL,
162 		       CTLFLAG_PERMANENT,
163 		       CTLTYPE_NODE, "hw", NULL,
164 		       NULL, 0, NULL, 0,
165 		       CTL_HW, CTL_EOL);
166 	sysctl_createv(clog, 0, &_root, NULL,
167 		       CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
168 		       CTLTYPE_INT, "alignbytes", NULL,
169 		       NULL, ALIGNBYTES32, NULL, 0,
170 		       CTL_HW, HW_ALIGNBYTES, CTL_EOL);
171 	sysctl_createv(clog, 0, &_root, NULL,
172 		       CTLFLAG_PERMANENT,
173 		       CTLTYPE_STRING, "machine", NULL,
174 		       NULL, 0, __UNCONST(&machine32), 0,
175 		       CTL_HW, HW_MACHINE, CTL_EOL);
176 	sysctl_createv(clog, 0, &_root, NULL,
177 		       CTLFLAG_PERMANENT|CTLFLAG_READONLY,
178 		       CTLTYPE_STRING, "machine_arch", NULL,
179 		       sysctl_hw_machine_arch32, 0, NULL, 0,
180 		       CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
181 }
182 
183 int
184 netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
185 {
186 	/* {
187 		syscallarg(netbsd32_intp) name;
188 		syscallarg(u_int) namelen;
189 		syscallarg(netbsd32_voidp) old;
190 		syscallarg(netbsd32_size_tp) oldlenp;
191 		syscallarg(netbsd32_voidp) new;
192 		syscallarg(netbsd32_size_t) newlen;
193 	} */
194 	const struct sysctlnode *pnode;
195 	netbsd32_size_t netbsd32_oldlen;
196 	size_t oldlen, *oldlenp, savelen;
197 	int name[CTL_MAXNAME], error, nerror, *namep;
198 	void *newp, *oldp;
199 
200 	/*
201 	 * get and convert 32 bit size_t to native size_t
202 	 */
203 	namep = SCARG_P32(uap, name);
204 	oldp = SCARG_P32(uap, oldv);
205 	newp = SCARG_P32(uap, newv);
206 	oldlenp = SCARG_P32(uap, oldlenp);
207 	oldlen = 0;
208 	if (oldlenp != NULL) {
209 		error = copyin(oldlenp, &netbsd32_oldlen,
210 			       sizeof(netbsd32_oldlen));
211 		if (error)
212 			return (error);
213 		oldlen = netbsd32_oldlen;
214 	}
215 	savelen = oldlen;
216 
217 	/*
218 	 * retrieve name and see if we need to dispatch this query to
219 	 * the shadow tree.  if we find it in the shadow tree,
220 	 * dispatch to there, otherwise NULL means use the built-in
221 	 * default main tree.
222 	 */
223 	if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
224 		return (EINVAL);
225 	error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
226         if (error)
227                 return (error);
228 
229 	ktrmib(name, SCARG(uap, namelen));
230 
231 	sysctl_lock(newp != NULL);
232 	pnode = &netbsd32_sysctl_root;
233 	error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
234 	pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
235 	error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
236 				oldp, &oldlen,
237 				newp, SCARG(uap, newlen),
238 				&name[0], l, pnode);
239 	sysctl_unlock();
240 
241 	/*
242 	 * reset caller's oldlen, even if we got an error
243 	 */
244 	if (oldlenp) {
245 		netbsd32_oldlen = oldlen;
246                 nerror = copyout(&netbsd32_oldlen, oldlenp,
247 				 sizeof(netbsd32_oldlen));
248                 if (error == 0)
249                         error = nerror;
250 	}
251 
252 	/*
253 	 * if the only problem is that we weren't given enough space,
254 	 * that's an ENOMEM error
255 	 */
256 	if (error == 0 && oldp != NULL && savelen < oldlen)
257 		error = ENOMEM;
258 
259 	return (error);
260 }
261