1 /*- 2 * Copyright (c) 2004 Tim J. Robbins 3 * Copyright (c) 2002 Doug Rabson 4 * Copyright (c) 2000 Marcel Moolenaar 5 * All rights reserved. 6 * Copyright (c) 2013 Dmitry Chagin <dchagin@FreeBSD.org> 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/reg.h> 83 #include <x86/sysarch.h> 84 85 #include <security/audit/audit.h> 86 87 #include <amd64/linux/linux.h> 88 #include <amd64/linux/linux_proto.h> 89 #include <compat/linux/linux_emul.h> 90 #include <compat/linux/linux_file.h> 91 #include <compat/linux/linux_fork.h> 92 #include <compat/linux/linux_ipc.h> 93 #include <compat/linux/linux_misc.h> 94 #include <compat/linux/linux_mmap.h> 95 #include <compat/linux/linux_signal.h> 96 #include <compat/linux/linux_util.h> 97 98 int 99 linux_execve(struct thread *td, struct linux_execve_args *args) 100 { 101 struct image_args eargs; 102 char *path; 103 int error; 104 105 LINUX_CTR(execve); 106 107 if (!LUSECONVPATH(td)) { 108 error = exec_copyin_args(&eargs, args->path, UIO_USERSPACE, 109 args->argp, args->envp); 110 } else { 111 LCONVPATHEXIST(td, args->path, &path); 112 error = exec_copyin_args(&eargs, path, UIO_SYSSPACE, args->argp, 113 args->envp); 114 LFREEPATH(path); 115 } 116 if (error == 0) 117 error = linux_common_execve(td, &eargs); 118 AUDIT_SYSCALL_EXIT(error == EJUSTRETURN ? 0 : error, td); 119 return (error); 120 } 121 122 int 123 linux_set_upcall(struct thread *td, register_t stack) 124 { 125 126 if (stack) 127 td->td_frame->tf_rsp = stack; 128 129 /* 130 * The newly created Linux thread returns 131 * to the user space by the same path that a parent does. 132 */ 133 td->td_frame->tf_rax = 0; 134 return (0); 135 } 136 137 int 138 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 139 { 140 141 return (linux_mmap_common(td, args->addr, args->len, args->prot, 142 args->flags, args->fd, args->pgoff)); 143 } 144 145 int 146 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 147 { 148 149 return (linux_mprotect_common(td, uap->addr, uap->len, uap->prot)); 150 } 151 152 int 153 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 154 { 155 156 return (linux_madvise_common(td, uap->addr, uap->len, uap->behav)); 157 } 158 159 int 160 linux_iopl(struct thread *td, struct linux_iopl_args *args) 161 { 162 int error; 163 164 LINUX_CTR(iopl); 165 166 if (args->level > 3) 167 return (EINVAL); 168 if ((error = priv_check(td, PRIV_IO)) != 0) 169 return (error); 170 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 171 return (error); 172 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 173 (args->level * (PSL_IOPL / 3)); 174 175 return (0); 176 } 177 178 int 179 linux_pause(struct thread *td, struct linux_pause_args *args) 180 { 181 struct proc *p = td->td_proc; 182 sigset_t sigmask; 183 184 LINUX_CTR(pause); 185 186 PROC_LOCK(p); 187 sigmask = td->td_sigmask; 188 PROC_UNLOCK(p); 189 return (kern_sigsuspend(td, sigmask)); 190 } 191 192 int 193 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args) 194 { 195 unsigned long long cet[3]; 196 struct pcb *pcb; 197 int error; 198 199 pcb = td->td_pcb; 200 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr); 201 202 switch (args->code) { 203 case LINUX_ARCH_SET_GS: 204 if (args->addr < VM_MAXUSER_ADDRESS) { 205 update_pcb_bases(pcb); 206 pcb->pcb_gsbase = args->addr; 207 td->td_frame->tf_gs = _ugssel; 208 error = 0; 209 } else 210 error = EPERM; 211 break; 212 case LINUX_ARCH_SET_FS: 213 if (args->addr < VM_MAXUSER_ADDRESS) { 214 update_pcb_bases(pcb); 215 pcb->pcb_fsbase = args->addr; 216 td->td_frame->tf_fs = _ufssel; 217 error = 0; 218 } else 219 error = EPERM; 220 break; 221 case LINUX_ARCH_GET_FS: 222 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr), 223 sizeof(args->addr)); 224 break; 225 case LINUX_ARCH_GET_GS: 226 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr), 227 sizeof(args->addr)); 228 break; 229 case LINUX_ARCH_CET_STATUS: 230 memset(cet, 0, sizeof(cet)); 231 error = copyout(&cet, PTRIN(args->addr), sizeof(cet)); 232 break; 233 default: 234 linux_msg(td, "unsupported arch_prctl code %#x", args->code); 235 error = EINVAL; 236 } 237 return (error); 238 } 239 240 int 241 linux_set_cloned_tls(struct thread *td, void *desc) 242 { 243 struct pcb *pcb; 244 245 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 246 return (EPERM); 247 248 pcb = td->td_pcb; 249 update_pcb_bases(pcb); 250 pcb->pcb_fsbase = (register_t)desc; 251 td->td_frame->tf_fs = _ufssel; 252 253 return (0); 254 } 255 256 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 257 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval); 258 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *)) 259 { 260 261 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 262 futex_xchgl_smap : futex_xchgl_nosmap); 263 } 264 265 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 266 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval); 267 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *)) 268 { 269 270 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 271 futex_addl_smap : futex_addl_nosmap); 272 } 273 274 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 275 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval); 276 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *)) 277 { 278 279 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 280 futex_orl_smap : futex_orl_nosmap); 281 } 282 283 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 284 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval); 285 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *)) 286 { 287 288 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 289 futex_andl_smap : futex_andl_nosmap); 290 } 291 292 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 293 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval); 294 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *)) 295 { 296 297 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 298 futex_xorl_smap : futex_xorl_nosmap); 299 } 300 301 void 302 bsd_to_linux_regset(struct reg *b_reg, struct linux_pt_regset *l_regset) 303 { 304 305 l_regset->r15 = b_reg->r_r15; 306 l_regset->r14 = b_reg->r_r14; 307 l_regset->r13 = b_reg->r_r13; 308 l_regset->r12 = b_reg->r_r12; 309 l_regset->rbp = b_reg->r_rbp; 310 l_regset->rbx = b_reg->r_rbx; 311 l_regset->r11 = b_reg->r_r11; 312 l_regset->r10 = b_reg->r_r10; 313 l_regset->r9 = b_reg->r_r9; 314 l_regset->r8 = b_reg->r_r8; 315 l_regset->rax = b_reg->r_rax; 316 l_regset->rcx = b_reg->r_rcx; 317 l_regset->rdx = b_reg->r_rdx; 318 l_regset->rsi = b_reg->r_rsi; 319 l_regset->rdi = b_reg->r_rdi; 320 l_regset->orig_rax = b_reg->r_rax; 321 l_regset->rip = b_reg->r_rip; 322 l_regset->cs = b_reg->r_cs; 323 l_regset->eflags = b_reg->r_rflags; 324 l_regset->rsp = b_reg->r_rsp; 325 l_regset->ss = b_reg->r_ss; 326 l_regset->fs_base = 0; 327 l_regset->gs_base = 0; 328 l_regset->ds = b_reg->r_ds; 329 l_regset->es = b_reg->r_es; 330 l_regset->fs = b_reg->r_fs; 331 l_regset->gs = b_reg->r_gs; 332 } 333