1 /* $NetBSD: netbsd32_sysctl.c,v 1.19 2005/07/03 17:18:02 cube 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.19 2005/07/03 17:18:02 cube 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/sa.h> 50 #include <sys/syscallargs.h> 51 #include <sys/proc.h> 52 #include <sys/sysctl.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 sysc_init_field(_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_timeval bt32; 80 81 netbsd32_from_timeval(&boottime, &bt32); 82 83 node = *rnode; 84 node.sysctl_data = &bt32; 85 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 86 } 87 88 /* 89 * sysctl helper routine for netbsd32's vm.loadavg node 90 */ 91 static int 92 netbsd32_sysctl_vm_loadavg(SYSCTLFN_ARGS) 93 { 94 struct sysctlnode node; 95 struct netbsd32_loadavg av32; 96 97 netbsd32_from_loadavg(&av32, &averunnable); 98 99 node = *rnode; 100 node.sysctl_data = &av32; 101 return (sysctl_lookup(SYSCTLFN_CALL(&node))); 102 } 103 104 SYSCTL_SETUP(netbsd32_sysctl_emul_setup, "sysctl netbsd32 shadow tree setup") 105 { 106 const struct sysctlnode *_root = &netbsd32_sysctl_root; 107 extern const char machine_arch32[]; 108 extern const char machine32[]; 109 110 sysctl_createv(clog, 0, &_root, NULL, 111 CTLFLAG_PERMANENT, 112 CTLTYPE_NODE, "kern", NULL, 113 NULL, 0, NULL, 0, 114 CTL_KERN, CTL_EOL); 115 sysctl_createv(clog, 0, &_root, NULL, 116 CTLFLAG_PERMANENT, 117 CTLTYPE_STRUCT, "boottime", NULL, 118 netbsd32_sysctl_kern_boottime, 0, NULL, 119 sizeof(struct netbsd32_timeval), 120 CTL_KERN, KERN_BOOTTIME, CTL_EOL); 121 122 sysctl_createv(clog, 0, &_root, NULL, 123 CTLFLAG_PERMANENT, 124 CTLTYPE_NODE, "vm", NULL, 125 NULL, 0, NULL, 0, 126 CTL_VM, CTL_EOL); 127 sysctl_createv(clog, 0, &_root, NULL, 128 CTLFLAG_PERMANENT, 129 CTLTYPE_STRUCT, "loadavg", NULL, 130 netbsd32_sysctl_vm_loadavg, 0, NULL, 131 sizeof(struct netbsd32_loadavg), 132 CTL_VM, VM_LOADAVG, CTL_EOL); 133 134 sysctl_createv(clog, 0, &_root, NULL, 135 CTLFLAG_PERMANENT, 136 CTLTYPE_NODE, "hw", NULL, 137 NULL, 0, NULL, 0, 138 CTL_HW, CTL_EOL); 139 sysctl_createv(clog, 0, &_root, NULL, 140 CTLFLAG_PERMANENT, 141 CTLTYPE_STRING, "machine", NULL, 142 NULL, 0, &machine32, 0, 143 CTL_HW, HW_MACHINE, CTL_EOL); 144 sysctl_createv(clog, 0, &_root, NULL, 145 CTLFLAG_PERMANENT, 146 CTLTYPE_STRING, "machine_arch", NULL, 147 NULL, 0, &machine_arch32, 0, 148 CTL_HW, HW_MACHINE_ARCH, CTL_EOL); 149 } 150 151 int 152 netbsd32___sysctl(l, v, retval) 153 struct lwp *l; 154 void *v; 155 register_t *retval; 156 { 157 struct netbsd32___sysctl_args /* { 158 syscallarg(netbsd32_intp) name; 159 syscallarg(u_int) namelen; 160 syscallarg(netbsd32_voidp) old; 161 syscallarg(netbsd32_size_tp) oldlenp; 162 syscallarg(netbsd32_voidp) new; 163 syscallarg(netbsd32_size_t) newlen; 164 } */ *uap = v; 165 const struct sysctlnode *pnode; 166 netbsd32_size_t netbsd32_oldlen; 167 size_t oldlen, *oldlenp, savelen; 168 int name[CTL_MAXNAME], error, nerror, *namep; 169 void *newp, *oldp; 170 171 /* 172 * get and convert 32 bit size_t to native size_t 173 */ 174 namep = NETBSD32PTR64(SCARG(uap, name)); 175 oldp = NETBSD32PTR64(SCARG(uap, old)); 176 newp = NETBSD32PTR64(SCARG(uap, new)); 177 oldlenp = NETBSD32PTR64(SCARG(uap, oldlenp)); 178 oldlen = 0; 179 if (oldlenp != NULL) { 180 error = copyin(oldlenp, &netbsd32_oldlen, 181 sizeof(netbsd32_oldlen)); 182 if (error) 183 return (error); 184 oldlen = netbsd32_oldlen; 185 } 186 savelen = oldlen; 187 188 /* 189 * retrieve name and see if we need to dispatch this query to 190 * the shadow tree. if we find it in the shadow tree, 191 * dispatch to there, otherwise NULL means use the built-in 192 * default main tree. 193 */ 194 if (SCARG(uap, namelen) > CTL_MAXNAME || SCARG(uap, namelen) < 1) 195 return (EINVAL); 196 error = copyin(namep, &name[0], SCARG(uap, namelen) * sizeof(int)); 197 if (error) 198 return (error); 199 200 /* 201 * wire old so that copyout() is less likely to fail? 202 */ 203 error = sysctl_lock(l, oldp, savelen); 204 if (error) 205 return (error); 206 207 pnode = &netbsd32_sysctl_root; 208 error = sysctl_locate(l, &name[0], SCARG(uap, namelen), &pnode, NULL); 209 pnode = (error == 0) ? &netbsd32_sysctl_root : NULL; 210 error = sysctl_dispatch(&name[0], SCARG(uap, namelen), 211 oldp, &oldlen, 212 newp, SCARG(uap, newlen), 213 &name[0], l, pnode); 214 215 /* 216 * release the sysctl lock 217 */ 218 sysctl_unlock(l); 219 220 /* 221 * reset caller's oldlen, even if we got an error 222 */ 223 if (oldlenp) { 224 netbsd32_oldlen = oldlen; 225 nerror = copyout(&netbsd32_oldlen, oldlenp, 226 sizeof(netbsd32_oldlen)); 227 if (error == 0) 228 error = nerror; 229 } 230 231 /* 232 * if the only problem is that we weren't given enough space, 233 * that's an ENOMEM error 234 */ 235 if (error == 0 && oldp != NULL && savelen < oldlen) 236 error = ENOMEM; 237 238 return (error); 239 } 240