1 /*- 2 * Copyright (c) 2013 Dmitry Chagin 3 * Copyright (c) 2004 Tim J. Robbins 4 * Copyright (c) 2002 Doug Rabson 5 * Copyright (c) 2000 Marcel Moolenaar 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer 13 * in this position and unchanged. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. The name of the author may not be used to endorse or promote products 18 * derived from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/capsicum.h> 37 #include <sys/clock.h> 38 #include <sys/dirent.h> 39 #include <sys/fcntl.h> 40 #include <sys/file.h> 41 #include <sys/filedesc.h> 42 #include <sys/imgact.h> 43 #include <sys/kernel.h> 44 #include <sys/ktr.h> 45 #include <sys/limits.h> 46 #include <sys/lock.h> 47 #include <sys/malloc.h> 48 #include <sys/mman.h> 49 #include <sys/mutex.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/resource.h> 53 #include <sys/resourcevar.h> 54 #include <sys/sched.h> 55 #include <sys/syscallsubr.h> 56 #include <sys/sysproto.h> 57 #include <sys/systm.h> 58 #include <sys/unistd.h> 59 #include <sys/vnode.h> 60 #include <sys/wait.h> 61 62 #include <security/mac/mac_framework.h> 63 64 #include <ufs/ufs/extattr.h> 65 #include <ufs/ufs/quota.h> 66 #include <ufs/ufs/ufsmount.h> 67 68 #include <machine/frame.h> 69 #include <machine/md_var.h> 70 #include <machine/pcb.h> 71 #include <machine/psl.h> 72 #include <machine/segments.h> 73 #include <machine/specialreg.h> 74 75 #include <vm/pmap.h> 76 #include <vm/vm.h> 77 #include <vm/vm_extern.h> 78 #include <vm/vm_kern.h> 79 #include <vm/vm_map.h> 80 81 #include <x86/ifunc.h> 82 #include <x86/sysarch.h> 83 84 #include <amd64/linux/linux.h> 85 #include <amd64/linux/linux_proto.h> 86 #include <compat/linux/linux_emul.h> 87 #include <compat/linux/linux_file.h> 88 #include <compat/linux/linux_ipc.h> 89 #include <compat/linux/linux_misc.h> 90 #include <compat/linux/linux_mmap.h> 91 #include <compat/linux/linux_signal.h> 92 #include <compat/linux/linux_util.h> 93 94 int 95 linux_execve(struct thread *td, struct linux_execve_args *args) 96 { 97 struct image_args eargs; 98 char *path; 99 int error; 100 101 LINUX_CTR(execve); 102 103 if (!LUSECONVPATH(td)) { 104 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE, 105 args->argp, args->envp); 106 } else { 107 LCONVPATHEXIST(td, args->path, &path); 108 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp, 109 args->envp); 110 LFREEPATH(path); 111 } 112 if (error == 0) 113 error = linux_common_execve(td, &eargs); 114 return (error); 115 } 116 117 int 118 linux_set_upcall_kse(struct thread *td, register_t stack) 119 { 120 121 if (stack) 122 td->td_frame->tf_rsp = stack; 123 124 /* 125 * The newly created Linux thread returns 126 * to the user space by the same path that a parent do. 127 */ 128 td->td_frame->tf_rax = 0; 129 return (0); 130 } 131 132 int 133 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 134 { 135 136 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot, 137 args->flags, args->fd, args->pgoff)); 138 } 139 140 int 141 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 142 { 143 144 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot)); 145 } 146 147 int 148 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 149 { 150 151 return (linux_madvise_common(td, PTROUT(uap->addr), uap->len, uap->behav)); 152 } 153 154 int 155 linux_iopl(struct thread *td, struct linux_iopl_args *args) 156 { 157 int error; 158 159 LINUX_CTR(iopl); 160 161 if (args->level > 3) 162 return (EINVAL); 163 if ((error = priv_check(td, PRIV_IO)) != 0) 164 return (error); 165 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 166 return (error); 167 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 168 (args->level * (PSL_IOPL / 3)); 169 170 return (0); 171 } 172 173 int 174 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 175 { 176 l_sigset_t lmask; 177 sigset_t sigmask; 178 int error; 179 180 LINUX_CTR2(rt_sigsuspend, "%p, %ld", 181 uap->newset, uap->sigsetsize); 182 183 if (uap->sigsetsize != sizeof(l_sigset_t)) 184 return (EINVAL); 185 186 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 187 if (error) 188 return (error); 189 190 linux_to_bsd_sigset(&lmask, &sigmask); 191 return (kern_sigsuspend(td, sigmask)); 192 } 193 194 int 195 linux_pause(struct thread *td, struct linux_pause_args *args) 196 { 197 struct proc *p = td->td_proc; 198 sigset_t sigmask; 199 200 LINUX_CTR(pause); 201 202 PROC_LOCK(p); 203 sigmask = td->td_sigmask; 204 PROC_UNLOCK(p); 205 return (kern_sigsuspend(td, sigmask)); 206 } 207 208 int 209 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 210 { 211 stack_t ss, oss; 212 l_stack_t lss; 213 int error; 214 215 memset(&lss, 0, sizeof(lss)); 216 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss); 217 218 if (uap->uss != NULL) { 219 error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 220 if (error) 221 return (error); 222 223 ss.ss_sp = PTRIN(lss.ss_sp); 224 ss.ss_size = lss.ss_size; 225 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 226 } 227 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 228 (uap->uoss != NULL) ? &oss : NULL); 229 if (!error && uap->uoss != NULL) { 230 lss.ss_sp = PTROUT(oss.ss_sp); 231 lss.ss_size = oss.ss_size; 232 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 233 error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 234 } 235 236 return (error); 237 } 238 239 int 240 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args) 241 { 242 struct pcb *pcb; 243 int error; 244 245 pcb = td->td_pcb; 246 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr); 247 248 switch (args->code) { 249 case LINUX_ARCH_SET_GS: 250 if (args->addr < VM_MAXUSER_ADDRESS) { 251 set_pcb_flags(pcb, PCB_FULL_IRET); 252 pcb->pcb_gsbase = args->addr; 253 td->td_frame->tf_gs = _ugssel; 254 error = 0; 255 } else 256 error = EPERM; 257 break; 258 case LINUX_ARCH_SET_FS: 259 if (args->addr < VM_MAXUSER_ADDRESS) { 260 set_pcb_flags(pcb, PCB_FULL_IRET); 261 pcb->pcb_fsbase = args->addr; 262 td->td_frame->tf_fs = _ufssel; 263 error = 0; 264 } else 265 error = EPERM; 266 break; 267 case LINUX_ARCH_GET_FS: 268 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr), 269 sizeof(args->addr)); 270 break; 271 case LINUX_ARCH_GET_GS: 272 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr), 273 sizeof(args->addr)); 274 break; 275 default: 276 error = EINVAL; 277 } 278 return (error); 279 } 280 281 int 282 linux_set_cloned_tls(struct thread *td, void *desc) 283 { 284 struct pcb *pcb; 285 286 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 287 return (EPERM); 288 289 pcb = td->td_pcb; 290 pcb->pcb_fsbase = (register_t)desc; 291 td->td_frame->tf_fs = _ufssel; 292 293 return (0); 294 } 295 296 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 297 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval); 298 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *)) 299 { 300 301 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 302 futex_xchgl_smap : futex_xchgl_nosmap); 303 } 304 305 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 306 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval); 307 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *)) 308 { 309 310 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 311 futex_addl_smap : futex_addl_nosmap); 312 } 313 314 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 315 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval); 316 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *)) 317 { 318 319 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 320 futex_orl_smap : futex_orl_nosmap); 321 } 322 323 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 324 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval); 325 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *)) 326 { 327 328 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 329 futex_andl_smap : futex_andl_nosmap); 330 } 331 332 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 333 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval); 334 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *)) 335 { 336 337 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 338 futex_xorl_smap : futex_xorl_nosmap); 339 } 340