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/kernel.h> 43 #include <sys/ktr.h> 44 #include <sys/limits.h> 45 #include <sys/lock.h> 46 #include <sys/malloc.h> 47 #include <sys/mman.h> 48 #include <sys/mutex.h> 49 #include <sys/priv.h> 50 #include <sys/proc.h> 51 #include <sys/ptrace.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 <amd64/linux/linux.h> 86 #include <amd64/linux/linux_proto.h> 87 #include <compat/linux/linux_emul.h> 88 #include <compat/linux/linux_file.h> 89 #include <compat/linux/linux_fork.h> 90 #include <compat/linux/linux_ipc.h> 91 #include <compat/linux/linux_misc.h> 92 #include <compat/linux/linux_mmap.h> 93 #include <compat/linux/linux_signal.h> 94 #include <compat/linux/linux_util.h> 95 96 #define LINUX_ARCH_AMD64 0xc000003e 97 98 int 99 linux_set_upcall(struct thread *td, register_t stack) 100 { 101 102 if (stack) 103 td->td_frame->tf_rsp = stack; 104 105 /* 106 * The newly created Linux thread returns 107 * to the user space by the same path that a parent does. 108 */ 109 td->td_frame->tf_rax = 0; 110 return (0); 111 } 112 113 int 114 linux_mmap2(struct thread *td, struct linux_mmap2_args *args) 115 { 116 117 return (linux_mmap_common(td, args->addr, args->len, args->prot, 118 args->flags, args->fd, args->pgoff)); 119 } 120 121 int 122 linux_mprotect(struct thread *td, struct linux_mprotect_args *uap) 123 { 124 125 return (linux_mprotect_common(td, uap->addr, uap->len, uap->prot)); 126 } 127 128 int 129 linux_madvise(struct thread *td, struct linux_madvise_args *uap) 130 { 131 132 return (linux_madvise_common(td, uap->addr, uap->len, uap->behav)); 133 } 134 135 int 136 linux_iopl(struct thread *td, struct linux_iopl_args *args) 137 { 138 int error; 139 140 LINUX_CTR(iopl); 141 142 if (args->level > 3) 143 return (EINVAL); 144 if ((error = priv_check(td, PRIV_IO)) != 0) 145 return (error); 146 if ((error = securelevel_gt(td->td_ucred, 0)) != 0) 147 return (error); 148 td->td_frame->tf_rflags = (td->td_frame->tf_rflags & ~PSL_IOPL) | 149 (args->level * (PSL_IOPL / 3)); 150 151 return (0); 152 } 153 154 int 155 linux_pause(struct thread *td, struct linux_pause_args *args) 156 { 157 struct proc *p = td->td_proc; 158 sigset_t sigmask; 159 160 LINUX_CTR(pause); 161 162 PROC_LOCK(p); 163 sigmask = td->td_sigmask; 164 PROC_UNLOCK(p); 165 return (kern_sigsuspend(td, sigmask)); 166 } 167 168 int 169 linux_arch_prctl(struct thread *td, struct linux_arch_prctl_args *args) 170 { 171 unsigned long long cet[3]; 172 struct pcb *pcb; 173 int error; 174 175 pcb = td->td_pcb; 176 LINUX_CTR2(arch_prctl, "0x%x, %p", args->code, args->addr); 177 178 switch (args->code) { 179 case LINUX_ARCH_SET_GS: 180 if (args->addr < VM_MAXUSER_ADDRESS) { 181 update_pcb_bases(pcb); 182 pcb->pcb_gsbase = args->addr; 183 td->td_frame->tf_gs = _ugssel; 184 error = 0; 185 } else 186 error = EPERM; 187 break; 188 case LINUX_ARCH_SET_FS: 189 if (args->addr < VM_MAXUSER_ADDRESS) { 190 update_pcb_bases(pcb); 191 pcb->pcb_fsbase = args->addr; 192 td->td_frame->tf_fs = _ufssel; 193 error = 0; 194 } else 195 error = EPERM; 196 break; 197 case LINUX_ARCH_GET_FS: 198 error = copyout(&pcb->pcb_fsbase, PTRIN(args->addr), 199 sizeof(args->addr)); 200 break; 201 case LINUX_ARCH_GET_GS: 202 error = copyout(&pcb->pcb_gsbase, PTRIN(args->addr), 203 sizeof(args->addr)); 204 break; 205 case LINUX_ARCH_CET_STATUS: 206 memset(cet, 0, sizeof(cet)); 207 error = copyout(&cet, PTRIN(args->addr), sizeof(cet)); 208 break; 209 default: 210 linux_msg(td, "unsupported arch_prctl code %#x", args->code); 211 error = EINVAL; 212 } 213 return (error); 214 } 215 216 int 217 linux_set_cloned_tls(struct thread *td, void *desc) 218 { 219 struct pcb *pcb; 220 221 if ((uint64_t)desc >= VM_MAXUSER_ADDRESS) 222 return (EPERM); 223 224 pcb = td->td_pcb; 225 update_pcb_bases(pcb); 226 pcb->pcb_fsbase = (register_t)desc; 227 td->td_frame->tf_fs = _ufssel; 228 229 return (0); 230 } 231 232 int futex_xchgl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 233 int futex_xchgl_smap(int oparg, uint32_t *uaddr, int *oldval); 234 DEFINE_IFUNC(, int, futex_xchgl, (int, uint32_t *, int *)) 235 { 236 237 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 238 futex_xchgl_smap : futex_xchgl_nosmap); 239 } 240 241 int futex_addl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 242 int futex_addl_smap(int oparg, uint32_t *uaddr, int *oldval); 243 DEFINE_IFUNC(, int, futex_addl, (int, uint32_t *, int *)) 244 { 245 246 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 247 futex_addl_smap : futex_addl_nosmap); 248 } 249 250 int futex_orl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 251 int futex_orl_smap(int oparg, uint32_t *uaddr, int *oldval); 252 DEFINE_IFUNC(, int, futex_orl, (int, uint32_t *, int *)) 253 { 254 255 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 256 futex_orl_smap : futex_orl_nosmap); 257 } 258 259 int futex_andl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 260 int futex_andl_smap(int oparg, uint32_t *uaddr, int *oldval); 261 DEFINE_IFUNC(, int, futex_andl, (int, uint32_t *, int *)) 262 { 263 264 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 265 futex_andl_smap : futex_andl_nosmap); 266 } 267 268 int futex_xorl_nosmap(int oparg, uint32_t *uaddr, int *oldval); 269 int futex_xorl_smap(int oparg, uint32_t *uaddr, int *oldval); 270 DEFINE_IFUNC(, int, futex_xorl, (int, uint32_t *, int *)) 271 { 272 273 return ((cpu_stdext_feature & CPUID_STDEXT_SMAP) != 0 ? 274 futex_xorl_smap : futex_xorl_nosmap); 275 } 276 277 void 278 bsd_to_linux_regset(const struct reg *b_reg, struct linux_pt_regset *l_regset) 279 { 280 281 l_regset->r15 = b_reg->r_r15; 282 l_regset->r14 = b_reg->r_r14; 283 l_regset->r13 = b_reg->r_r13; 284 l_regset->r12 = b_reg->r_r12; 285 l_regset->rbp = b_reg->r_rbp; 286 l_regset->rbx = b_reg->r_rbx; 287 l_regset->r11 = b_reg->r_r11; 288 l_regset->r10 = b_reg->r_r10; 289 l_regset->r9 = b_reg->r_r9; 290 l_regset->r8 = b_reg->r_r8; 291 l_regset->rax = b_reg->r_rax; 292 l_regset->rcx = b_reg->r_rcx; 293 l_regset->rdx = b_reg->r_rdx; 294 l_regset->rsi = b_reg->r_rsi; 295 l_regset->rdi = b_reg->r_rdi; 296 l_regset->orig_rax = b_reg->r_rax; 297 l_regset->rip = b_reg->r_rip; 298 l_regset->cs = b_reg->r_cs; 299 l_regset->eflags = b_reg->r_rflags; 300 l_regset->rsp = b_reg->r_rsp; 301 l_regset->ss = b_reg->r_ss; 302 l_regset->fs_base = 0; 303 l_regset->gs_base = 0; 304 l_regset->ds = b_reg->r_ds; 305 l_regset->es = b_reg->r_es; 306 l_regset->fs = b_reg->r_fs; 307 l_regset->gs = b_reg->r_gs; 308 } 309 310 void 311 linux_to_bsd_regset(struct reg *b_reg, const struct linux_pt_regset *l_regset) 312 { 313 314 b_reg->r_r15 = l_regset->r15; 315 b_reg->r_r14 = l_regset->r14; 316 b_reg->r_r13 = l_regset->r13; 317 b_reg->r_r12 = l_regset->r12; 318 b_reg->r_rbp = l_regset->rbp; 319 b_reg->r_rbx = l_regset->rbx; 320 b_reg->r_r11 = l_regset->r11; 321 b_reg->r_r10 = l_regset->r10; 322 b_reg->r_r9 = l_regset->r9; 323 b_reg->r_r8 = l_regset->r8; 324 b_reg->r_rax = l_regset->rax; 325 b_reg->r_rcx = l_regset->rcx; 326 b_reg->r_rdx = l_regset->rdx; 327 b_reg->r_rsi = l_regset->rsi; 328 b_reg->r_rdi = l_regset->rdi; 329 b_reg->r_rax = l_regset->orig_rax; 330 b_reg->r_rip = l_regset->rip; 331 b_reg->r_cs = l_regset->cs; 332 b_reg->r_rflags = l_regset->eflags; 333 b_reg->r_rsp = l_regset->rsp; 334 b_reg->r_ss = l_regset->ss; 335 b_reg->r_ds = l_regset->ds; 336 b_reg->r_es = l_regset->es; 337 b_reg->r_fs = l_regset->fs; 338 b_reg->r_gs = l_regset->gs; 339 } 340 341 void 342 linux_ptrace_get_syscall_info_machdep(const struct reg *reg, 343 struct syscall_info *si) 344 { 345 346 si->arch = LINUX_ARCH_AMD64; 347 si->instruction_pointer = reg->r_rip; 348 si->stack_pointer = reg->r_rsp; 349 } 350 351 int 352 linux_ptrace_getregs_machdep(struct thread *td, pid_t pid, 353 struct linux_pt_regset *l_regset) 354 { 355 struct ptrace_lwpinfo lwpinfo; 356 struct pcb *pcb; 357 int error; 358 359 pcb = td->td_pcb; 360 if (td == curthread) 361 update_pcb_bases(pcb); 362 363 l_regset->fs_base = pcb->pcb_fsbase; 364 l_regset->gs_base = pcb->pcb_gsbase; 365 366 error = kern_ptrace(td, PT_LWPINFO, pid, &lwpinfo, sizeof(lwpinfo)); 367 if (error != 0) { 368 linux_msg(td, "PT_LWPINFO failed with error %d", error); 369 return (error); 370 } 371 if ((lwpinfo.pl_flags & PL_FLAG_SCE) != 0) { 372 /* 373 * Undo the mangling done in exception.S:fast_syscall_common(). 374 */ 375 l_regset->r10 = l_regset->rcx; 376 } 377 if ((lwpinfo.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX)) != 0) { 378 /* 379 * In Linux, the syscall number - passed to the syscall 380 * as rax - is preserved in orig_rax; rax gets overwritten 381 * with syscall return value. 382 */ 383 l_regset->orig_rax = lwpinfo.pl_syscall_code; 384 } 385 386 return (0); 387 } 388