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