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 <amd64/linux/linux.h> 82 #include <amd64/linux/linux_proto.h> 83 #include <compat/linux/linux_emul.h> 84 #include <compat/linux/linux_file.h> 85 #include <compat/linux/linux_ipc.h> 86 #include <compat/linux/linux_misc.h> 87 #include <compat/linux/linux_mmap.h> 88 #include <compat/linux/linux_signal.h> 89 #include <compat/linux/linux_util.h> 90 91 #include <x86/include/sysarch.h> 92 93 int 94 linux_execve(struct thread *td, struct linux_execve_args *args) 95 { 96 struct image_args eargs; 97 char *path; 98 int error; 99 100 LCONVPATHEXIST(td, args->path, &path); 101 102 LINUX_CTR(execve); 103 104 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp, 105 args->envp); 106 free(path, M_TEMP); 107 if (error == 0) 108 error = linux_common_execve(td, &eargs); 109 return (error); 110 } 111 112 int 113 linux_set_upcall_kse(struct thread *td, register_t stack) 114 { 115 116 if (stack) 117 td->td_frame->tf_rsp = stack; 118 119 /* 120 * The newly created Linux thread returns 121 * to the user space by the same path that a parent do. 122 */ 123 td->td_frame->tf_rax = 0; 124 return (0); 125 } 126 127 int 128 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 129 { 130 131 return (linux_mmap_common(td, PTROUT(args->addr), args->len, args->prot, 132 args->flags, args->fd, args->pgoff)); 133 } 134 135 int 136 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 137 { 138 139 return (linux_mprotect_common(td, PTROUT(uap->addr), uap->len, uap->prot)); 140 } 141 142 int 143 linux_iopl(struct thread *td, struct linux_iopl_args *args) 144 { 145 int error; 146 147 LINUX_CTR(iopl); 148 149 if (args->level > 3) 150 return (EINVAL); 151 if ((error = priv_check(td, PRIV_IO)) != 0) 152 return (error); 153 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 154 return (error); 155 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 156 (args->level * (PSL_IOPL / 3)); 157 158 return (0); 159 } 160 161 int 162 linux_rt_sigsuspend(struct thread *td, struct linux_rt_sigsuspend_args *uap) 163 { 164 l_sigset_t lmask; 165 sigset_t sigmask; 166 int error; 167 168 LINUX_CTR2(rt_sigsuspend, "%p, %ld", 169 uap->newset, uap->sigsetsize); 170 171 if (uap->sigsetsize != sizeof(l_sigset_t)) 172 return (EINVAL); 173 174 error = copyin(uap->newset, &lmask, sizeof(l_sigset_t)); 175 if (error) 176 return (error); 177 178 linux_to_bsd_sigset(&lmask, &sigmask); 179 return (kern_sigsuspend(td, sigmask)); 180 } 181 182 int 183 linux_pause(struct thread *td, struct linux_pause_args *args) 184 { 185 struct proc *p = td->td_proc; 186 sigset_t sigmask; 187 188 LINUX_CTR(pause); 189 190 PROC_LOCK(p); 191 sigmask = td->td_sigmask; 192 PROC_UNLOCK(p); 193 return (kern_sigsuspend(td, sigmask)); 194 } 195 196 int 197 linux_sigaltstack(struct thread *td, struct linux_sigaltstack_args *uap) 198 { 199 stack_t ss, oss; 200 l_stack_t lss; 201 int error; 202 203 LINUX_CTR2(sigaltstack, "%p, %p", uap->uss, uap->uoss); 204 205 if (uap->uss != NULL) { 206 error = copyin(uap->uss, &lss, sizeof(l_stack_t)); 207 if (error) 208 return (error); 209 210 ss.ss_sp = PTRIN(lss.ss_sp); 211 ss.ss_size = lss.ss_size; 212 ss.ss_flags = linux_to_bsd_sigaltstack(lss.ss_flags); 213 } 214 error = kern_sigaltstack(td, (uap->uss != NULL) ? &ss : NULL, 215 (uap->uoss != NULL) ? &oss : NULL); 216 if (!error && uap->uoss != NULL) { 217 lss.ss_sp = PTROUT(oss.ss_sp); 218 lss.ss_size = oss.ss_size; 219 lss.ss_flags = bsd_to_linux_sigaltstack(oss.ss_flags); 220 error = copyout(&lss, uap->uoss, sizeof(l_stack_t)); 221 } 222 223 return (error); 224 } 225 226 int 227 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args) 228 { 229 int error; 230 struct sysarch_args bsd_args; 231 232 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr); 233 234 switch (args->code) { 235 case LINUX_ARCH_SET_GS: 236 bsd_args.op = AMD64_SET_GSBASE; 237 bsd_args.parms = (void *)args->addr; 238 error = sysarch(td, &bsd_args); 239 if (error == EINVAL) 240 error = EPERM; 241 break; 242 case LINUX_ARCH_SET_FS: 243 bsd_args.op = AMD64_SET_FSBASE; 244 bsd_args.parms = (void *)args->addr; 245 error = sysarch(td, &bsd_args); 246 if (error == EINVAL) 247 error = EPERM; 248 break; 249 case LINUX_ARCH_GET_FS: 250 bsd_args.op = AMD64_GET_FSBASE; 251 bsd_args.parms = (void *)args->addr; 252 error = sysarch(td, &bsd_args); 253 break; 254 case LINUX_ARCH_GET_GS: 255 bsd_args.op = AMD64_GET_GSBASE; 256 bsd_args.parms = (void *)args->addr; 257 error = sysarch(td, &bsd_args); 258 break; 259 default: 260 error = EINVAL; 261 } 262 return (error); 263 } 264 265 int 266 linux_set_cloned_tls(struct thread *td, void *desc) 267 { 268 struct pcb *pcb; 269 270 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 271 return (EPERM); 272 273 pcb = td->td_pcb; 274 pcb->pcb_fsbase = (register_t)desc; 275 td->td_frame->tf_fs = _ufssel; 276 277 return (0); 278 } 279