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