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