netbsd32_sysctl.c revision 1.28 1 /* $NetBSD: netbsd32_sysctl.c,v 1.28 2008/01/07 16:12:53 ad 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.28 2008/01/07 16:12:53 ad 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/syscallargs.h>
50 #include <sys/proc.h>
51 #include <sys/sysctl.h>
52 #include <sys/dirent.h>
53 #include <sys/ktrace.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_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
68 .sysctl_num = 0,
69 .sysctl_name = "(netbsd32_root)",
70 sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
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 const struct sysctlnode *_root = &netbsd32_sysctl_root;
108 extern const char machine_arch32[];
109 extern const char machine32[];
110
111 sysctl_createv(clog, 0, &_root, NULL,
112 CTLFLAG_PERMANENT,
113 CTLTYPE_NODE, "kern", NULL,
114 NULL, 0, NULL, 0,
115 CTL_KERN, CTL_EOL);
116 sysctl_createv(clog, 0, &_root, NULL,
117 CTLFLAG_PERMANENT,
118 CTLTYPE_STRUCT, "boottime", NULL,
119 netbsd32_sysctl_kern_boottime, 0, NULL,
120 sizeof(struct netbsd32_timeval),
121 CTL_KERN, KERN_BOOTTIME, CTL_EOL);
122
123 sysctl_createv(clog, 0, &_root, NULL,
124 CTLFLAG_PERMANENT,
125 CTLTYPE_NODE, "vm", NULL,
126 NULL, 0, NULL, 0,
127 CTL_VM, CTL_EOL);
128 sysctl_createv(clog, 0, &_root, NULL,
129 CTLFLAG_PERMANENT,
130 CTLTYPE_STRUCT, "loadavg", NULL,
131 netbsd32_sysctl_vm_loadavg, 0, NULL,
132 sizeof(struct netbsd32_loadavg),
133 CTL_VM, VM_LOADAVG, CTL_EOL);
134
135 sysctl_createv(clog, 0, &_root, NULL,
136 CTLFLAG_PERMANENT,
137 CTLTYPE_NODE, "hw", NULL,
138 NULL, 0, NULL, 0,
139 CTL_HW, CTL_EOL);
140 sysctl_createv(clog, 0, &_root, NULL,
141 CTLFLAG_PERMANENT,
142 CTLTYPE_STRING, "machine", NULL,
143 NULL, 0, &machine32, 0,
144 CTL_HW, HW_MACHINE, CTL_EOL);
145 sysctl_createv(clog, 0, &_root, NULL,
146 CTLFLAG_PERMANENT,
147 CTLTYPE_STRING, "machine_arch", NULL,
148 NULL, 0, &machine_arch32, 0,
149 CTL_HW, HW_MACHINE_ARCH, CTL_EOL);
150 }
151
152 int
153 netbsd32___sysctl(struct lwp *l, const struct netbsd32___sysctl_args *uap, register_t *retval)
154 {
155 /* {
156 syscallarg(netbsd32_intp) name;
157 syscallarg(u_int) namelen;
158 syscallarg(netbsd32_voidp) old;
159 syscallarg(netbsd32_size_tp) oldlenp;
160 syscallarg(netbsd32_voidp) new;
161 syscallarg(netbsd32_size_t) newlen;
162 } */
163 const struct sysctlnode *pnode;
164 netbsd32_size_t netbsd32_oldlen;
165 size_t oldlen, *oldlenp, savelen;
166 int name[CTL_MAXNAME], error, nerror, *namep;
167 void *newp, *oldp;
168
169 /*
170 * get and convert 32 bit size_t to native size_t
171 */
172 namep = SCARG_P32(uap, name);
173 oldp = SCARG_P32(uap, old);
174 newp = SCARG_P32(uap, new);
175 oldlenp = SCARG_P32(uap, oldlenp);
176 oldlen = 0;
177 if (oldlenp != NULL) {
178 error = copyin(oldlenp, &netbsd32_oldlen,
179 sizeof(netbsd32_oldlen));
180 if (error)
181 return (error);
182 oldlen = netbsd32_oldlen;
183 }
184 savelen = oldlen;
185
186 /*
187 * retrieve name and see if we need to dispatch this query to
188 * the shadow tree. if we find it in the shadow tree,
189 * dispatch to there, otherwise NULL means use the built-in
190 * default main tree.
191 */
192 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1)
193 return (EINVAL);
194 error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int));
195 if (error)
196 return (error);
197
198 ktrmib(name, SCARG(uap, namelen));
199
200 sysctl_lock(newp != NULL);
201 pnode = &netbsd32_sysctl_root;
202 error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL);
203 pnode = (error == 0) ? &netbsd32_sysctl_root : NULL;
204 error = sysctl_dispatch(&name[0], SCARG(uap, namelen),
205 oldp, &oldlen,
206 newp, SCARG(uap, newlen),
207 &name[0], l, pnode);
208 sysctl_unlock();
209
210 /*
211 * reset caller's oldlen, even if we got an error
212 */
213 if (oldlenp) {
214 netbsd32_oldlen = oldlen;
215 nerror = copyout(&netbsd32_oldlen, oldlenp,
216 sizeof(netbsd32_oldlen));
217 if (error == 0)
218 error = nerror;
219 }
220
221 /*
222 * if the only problem is that we weren't given enough space,
223 * that's an ENOMEM error
224 */
225 if (error == 0 && oldp != NULL && savelen < oldlen)
226 error = ENOMEM;
227
228 return (error);
229 }
230