netbsd32_sysctl.c revision 1.38.2.4 1 1.38.2.4 pgoyette /* $NetBSD: netbsd32_sysctl.c,v 1.38.2.4 2019/01/24 04:31:19 pgoyette Exp $ */
2 1.1 mrg
3 1.1 mrg /*
4 1.12 atatat * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.12 atatat * This code is derived from software contributed to The NetBSD Foundation
8 1.12 atatat * by Andrew Brown.
9 1.12 atatat *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.1 mrg * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 mrg * notice, this list of conditions and the following disclaimer in the
17 1.1 mrg * documentation and/or other materials provided with the distribution.
18 1.1 mrg * 3. The name of the author may not be used to endorse or promote products
19 1.1 mrg * derived from this software without specific prior written permission.
20 1.1 mrg *
21 1.1 mrg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 mrg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 mrg * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 mrg * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 mrg * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 1.1 mrg * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 1.1 mrg * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 1.1 mrg * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 1.1 mrg * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 mrg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 mrg * SUCH DAMAGE.
32 1.1 mrg */
33 1.4 lukem
34 1.4 lukem #include <sys/cdefs.h>
35 1.38.2.4 pgoyette __KERNEL_RCSID(0, "$NetBSD: netbsd32_sysctl.c,v 1.38.2.4 2019/01/24 04:31:19 pgoyette Exp $");
36 1.1 mrg
37 1.3 mrg #if defined(_KERNEL_OPT)
38 1.1 mrg #include "opt_ddb.h"
39 1.1 mrg #endif
40 1.1 mrg
41 1.1 mrg #include <sys/param.h>
42 1.1 mrg #include <sys/systm.h>
43 1.1 mrg #include <sys/kernel.h>
44 1.1 mrg #include <sys/mount.h>
45 1.1 mrg #include <sys/stat.h>
46 1.1 mrg #include <sys/time.h>
47 1.1 mrg #include <sys/vnode.h>
48 1.1 mrg #include <sys/syscallargs.h>
49 1.1 mrg #include <sys/proc.h>
50 1.1 mrg #include <sys/sysctl.h>
51 1.20 christos #include <sys/dirent.h>
52 1.22 manu #include <sys/ktrace.h>
53 1.38.2.2 pgoyette #include <sys/compat_stub.h>
54 1.1 mrg
55 1.1 mrg #include <uvm/uvm_extern.h>
56 1.1 mrg
57 1.1 mrg #include <compat/netbsd32/netbsd32.h>
58 1.1 mrg #include <compat/netbsd32/netbsd32_syscall.h>
59 1.1 mrg #include <compat/netbsd32/netbsd32_syscallargs.h>
60 1.1 mrg #include <compat/netbsd32/netbsd32_conv.h>
61 1.29 ad #include <compat/netbsd32/netbsd32_sysctl.h>
62 1.1 mrg
63 1.1 mrg #if defined(DDB)
64 1.1 mrg #include <ddb/ddbvar.h>
65 1.1 mrg #endif
66 1.1 mrg
67 1.12 atatat struct sysctlnode netbsd32_sysctl_root = {
68 1.14 atatat .sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
69 1.12 atatat .sysctl_num = 0,
70 1.12 atatat .sysctl_name = "(netbsd32_root)",
71 1.34 martin .sysctl_size = sizeof(struct sysctlnode),
72 1.12 atatat };
73 1.1 mrg
74 1.29 ad static struct sysctllog *netbsd32_clog;
75 1.29 ad
76 1.1 mrg /*
77 1.12 atatat * sysctl helper routine for netbsd32's kern.boottime node
78 1.1 mrg */
79 1.12 atatat static int
80 1.12 atatat netbsd32_sysctl_kern_boottime(SYSCTLFN_ARGS)
81 1.1 mrg {
82 1.12 atatat struct sysctlnode node;
83 1.30 christos struct netbsd32_timespec bt32;
84 1.1 mrg
85 1.30 christos netbsd32_from_timespec(&boottime, &bt32);
86 1.1 mrg
87 1.12 atatat node = *rnode;
88 1.12 atatat node.sysctl_data = &bt32;
89 1.12 atatat return (sysctl_lookup(SYSCTLFN_CALL(&node)));
90 1.1 mrg }
91 1.1 mrg
92 1.1 mrg /*
93 1.12 atatat * sysctl helper routine for netbsd32's vm.loadavg node
94 1.1 mrg */
95 1.12 atatat static int
96 1.12 atatat netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS)
97 1.1 mrg {
98 1.12 atatat struct sysctlnode node;
99 1.12 atatat struct netbsd32_loadavg av32;
100 1.1 mrg
101 1.12 atatat netbsd32_from_loadavg(&av32, &averunnable);
102 1.1 mrg
103 1.12 atatat node = *rnode;
104 1.12 atatat node.sysctl_data = &av32;
105 1.12 atatat return (sysctl_lookup(SYSCTLFN_CALL(&node)));
106 1.1 mrg }
107 1.1 mrg
108 1.37 martin static int
109 1.37 martin sysctl_hw_machine_arch32(SYSCTLFN_ARGS)
110 1.37 martin {
111 1.37 martin struct sysctlnode node = *rnode;
112 1.37 martin #ifndef PROC_MACHINE_ARCH32
113 1.37 martin extern const char machine_arch32[];
114 1.38 kre #define PROC_MACHINE_ARCH32(P) __UNCONST(machine_arch32)
115 1.37 martin #endif
116 1.37 martin
117 1.37 martin node.sysctl_data = PROC_MACHINE_ARCH32(l->l_proc);
118 1.37 martin node.sysctl_size = strlen(node.sysctl_data) + 1;
119 1.37 martin return sysctl_lookup(SYSCTLFN_CALL(&node));
120 1.37 martin }
121 1.37 martin
122 1.29 ad void
123 1.29 ad netbsd32_sysctl_init(void)
124 1.6 eeh {
125 1.16 atatat const struct sysctlnode *_root = &netbsd32_sysctl_root;
126 1.38.2.2 pgoyette const char *m;
127 1.6 eeh
128 1.29 ad sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
129 1.14 atatat CTLFLAG_PERMANENT,
130 1.14 atatat CTLTYPE_NODE, "kern", NULL,
131 1.12 atatat NULL, 0, NULL, 0,
132 1.12 atatat CTL_KERN, CTL_EOL);
133 1.29 ad sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
134 1.14 atatat CTLFLAG_PERMANENT,
135 1.14 atatat CTLTYPE_STRUCT, "boottime", NULL,
136 1.12 atatat netbsd32_sysctl_kern_boottime, 0, NULL,
137 1.12 atatat sizeof(struct netbsd32_timeval),
138 1.12 atatat CTL_KERN, KERN_BOOTTIME, CTL_EOL);
139 1.12 atatat
140 1.29 ad sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
141 1.14 atatat CTLFLAG_PERMANENT,
142 1.14 atatat CTLTYPE_NODE, "vm", NULL,
143 1.12 atatat NULL, 0, NULL, 0,
144 1.12 atatat CTL_VM, CTL_EOL);
145 1.29 ad sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
146 1.14 atatat CTLFLAG_PERMANENT,
147 1.14 atatat CTLTYPE_STRUCT, "loadavg", NULL,
148 1.12 atatat netbsd32_sysctl_vm_loadavg, 0, NULL,
149 1.12 atatat sizeof(struct netbsd32_loadavg),
150 1.12 atatat CTL_VM, VM_LOADAVG, CTL_EOL);
151 1.38.2.1 pgoyette sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
152 1.38.2.1 pgoyette CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
153 1.38.2.1 pgoyette CTLTYPE_INT, "maxaddress",
154 1.38.2.1 pgoyette SYSCTL_DESCR("Maximum user address"),
155 1.38.2.1 pgoyette NULL, VM_MAXUSER_ADDRESS32, NULL, 0,
156 1.38.2.1 pgoyette CTL_VM, VM_MAXADDRESS, CTL_EOL);
157 1.38.2.1 pgoyette sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
158 1.38.2.1 pgoyette CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
159 1.38.2.1 pgoyette CTLTYPE_INT, "minaddress",
160 1.38.2.1 pgoyette SYSCTL_DESCR("Minimum user address"),
161 1.38.2.1 pgoyette NULL, VM_MIN_ADDRESS, NULL, 0,
162 1.38.2.1 pgoyette CTL_VM, VM_MINADDRESS, CTL_EOL);
163 1.12 atatat
164 1.29 ad sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
165 1.14 atatat CTLFLAG_PERMANENT,
166 1.14 atatat CTLTYPE_NODE, "hw", NULL,
167 1.12 atatat NULL, 0, NULL, 0,
168 1.12 atatat CTL_HW, CTL_EOL);
169 1.29 ad sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
170 1.33 joerg CTLFLAG_PERMANENT|CTLFLAG_IMMEDIATE,
171 1.33 joerg CTLTYPE_INT, "alignbytes", NULL,
172 1.33 joerg NULL, ALIGNBYTES32, NULL, 0,
173 1.33 joerg CTL_HW, HW_ALIGNBYTES, CTL_EOL);
174 1.38.2.4 pgoyette MODULE_CALL_HOOK(netbsd32_machine_32_hook, (), machine, m);
175 1.33 joerg sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
176 1.14 atatat CTLFLAG_PERMANENT,
177 1.18 cube CTLTYPE_STRING, "machine", NULL,
178 1.38.2.2 pgoyette NULL, 0, __UNCONST(m), 0,
179 1.18 cube CTL_HW, HW_MACHINE, CTL_EOL);
180 1.36 matt sysctl_createv(&netbsd32_clog, 0, &_root, NULL,
181 1.37 martin CTLFLAG_PERMANENT|CTLFLAG_READONLY,
182 1.36 matt CTLTYPE_STRING, "machine_arch", NULL,
183 1.37 martin sysctl_hw_machine_arch32, 0, NULL, 0,
184 1.36 matt CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
185 1.6 eeh }
186 1.6 eeh
187 1.29 ad void
188 1.29 ad netbsd32_sysctl_fini(void)
189 1.29 ad {
190 1.29 ad
191 1.29 ad sysctl_teardown(&netbsd32_clog);
192 1.29 ad sysctl_free(&netbsd32_sysctl_root);
193 1.29 ad }
194 1.29 ad
195 1.1 mrg int
196 1.27 dsl netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
197 1.1 mrg {
198 1.27 dsl /* {
199 1.1 mrg syscallarg(netbsd32_intp) name;
200 1.1 mrg syscallarg(u_int) namelen;
201 1.1 mrg syscallarg(netbsd32_voidp) old;
202 1.1 mrg syscallarg(netbsd32_size_tp) oldlenp;
203 1.1 mrg syscallarg(netbsd32_voidp) new;
204 1.1 mrg syscallarg(netbsd32_size_t) newlen;
205 1.27 dsl } */
206 1.17 atatat const struct sysctlnode *pnode;
207 1.12 atatat netbsd32_size_t netbsd32_oldlen;
208 1.12 atatat size_t oldlen, *oldlenp, savelen;
209 1.12 atatat int name[CTL_MAXNAME], error, nerror, *namep;
210 1.12 atatat void *newp, *oldp;
211 1.1 mrg
212 1.12 atatat /*
213 1.12 atatat * get and convert 32 bit size_t to native size_t
214 1.12 atatat */
215 1.24 dsl namep = SCARG_P32(uap, name);
216 1.35 joerg oldp = SCARG_P32(uap, oldv);
217 1.35 joerg newp = SCARG_P32(uap, newv);
218 1.24 dsl oldlenp = SCARG_P32(uap, oldlenp);
219 1.12 atatat oldlen = 0;
220 1.12 atatat if (oldlenp != NULL) {
221 1.12 atatat error = copyin(oldlenp, &netbsd32_oldlen,
222 1.12 atatat sizeof(netbsd32_oldlen));
223 1.12 atatat if (error)
224 1.12 atatat return (error);
225 1.12 atatat oldlen = netbsd32_oldlen;
226 1.12 atatat }
227 1.12 atatat savelen = oldlen;
228 1.1 mrg
229 1.1 mrg /*
230 1.12 atatat * retrieve name and see if we need to dispatch this query to
231 1.12 atatat * the shadow tree. if we find it in the shadow tree,
232 1.12 atatat * dispatch to there, otherwise NULL means use the built-in
233 1.12 atatat * default main tree.
234 1.1 mrg */
235 1.12 atatat if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
236 1.1 mrg return (EINVAL);
237 1.12 atatat error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
238 1.12 atatat if (error)
239 1.12 atatat return (error);
240 1.12 atatat
241 1.25 ad ktrmib(name, SCARG(uap, namelen));
242 1.22 manu
243 1.28 ad sysctl_lock(newp != NULL);
244 1.12 atatat pnode = &netbsd32_sysctl_root;
245 1.12 atatat error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
246 1.12 atatat pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
247 1.12 atatat error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
248 1.12 atatat oldp, &oldlen,
249 1.12 atatat newp, SCARG(uap, newlen),
250 1.12 atatat &name[0], l, pnode);
251 1.28 ad sysctl_unlock();
252 1.12 atatat
253 1.12 atatat /*
254 1.12 atatat * reset caller's oldlen, even if we got an error
255 1.12 atatat */
256 1.12 atatat if (oldlenp) {
257 1.12 atatat netbsd32_oldlen = oldlen;
258 1.12 atatat nerror = copyout(&netbsd32_oldlen, oldlenp,
259 1.12 atatat sizeof(netbsd32_oldlen));
260 1.12 atatat if (error == 0)
261 1.12 atatat error = nerror;
262 1.1 mrg }
263 1.1 mrg
264 1.1 mrg /*
265 1.12 atatat * if the only problem is that we weren't given enough space,
266 1.12 atatat * that's an ENOMEM error
267 1.1 mrg */
268 1.12 atatat if (error == 0 && oldp != NULL && savelen < oldlen)
269 1.12 atatat error = ENOMEM;
270 1.1 mrg
271 1.1 mrg return (error);
272 1.1 mrg }
273