1 1.7 maxv /* $NetBSD: cpu_x86_64.c,v 1.7 2018/11/27 14:09:54 maxv Exp $ */ 2 1.1 reinoud 3 1.1 reinoud /*- 4 1.1 reinoud * Copyright (c) 2011 Reinoud Zandijk <reinoud (at) netbsd.org> 5 1.1 reinoud * Copyright (c) 2007 Jared D. McNeill <jmcneill (at) invisible.ca> 6 1.1 reinoud * All rights reserved. 7 1.1 reinoud * 8 1.1 reinoud * Redistribution and use in source and binary forms, with or without 9 1.1 reinoud * modification, are permitted provided that the following conditions 10 1.1 reinoud * are met: 11 1.1 reinoud * 1. Redistributions of source code must retain the above copyright 12 1.1 reinoud * notice, this list of conditions and the following disclaimer. 13 1.1 reinoud * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 reinoud * notice, this list of conditions and the following disclaimer in the 15 1.1 reinoud * documentation and/or other materials provided with the distribution. 16 1.1 reinoud * 17 1.1 reinoud * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 1.1 reinoud * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 1.1 reinoud * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 1.1 reinoud * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 1.1 reinoud * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 1.1 reinoud * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 1.1 reinoud * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 1.1 reinoud * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 1.1 reinoud * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 1.1 reinoud * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 1.1 reinoud * POSSIBILITY OF SUCH DAMAGE. 28 1.1 reinoud */ 29 1.1 reinoud 30 1.1 reinoud 31 1.1 reinoud #include <sys/cdefs.h> 32 1.7 maxv __KERNEL_RCSID(0, "$NetBSD: cpu_x86_64.c,v 1.7 2018/11/27 14:09:54 maxv Exp $"); 33 1.1 reinoud 34 1.1 reinoud #include <sys/types.h> 35 1.1 reinoud #include <sys/systm.h> 36 1.1 reinoud #include <sys/param.h> 37 1.1 reinoud #include <sys/time.h> 38 1.1 reinoud #include <sys/exec.h> 39 1.1 reinoud #include <sys/buf.h> 40 1.1 reinoud #include <sys/boot_flag.h> 41 1.1 reinoud #include <sys/ucontext.h> 42 1.1 reinoud #include <sys/utsname.h> 43 1.1 reinoud #include <machine/pcb.h> 44 1.1 reinoud #include <machine/psl.h> 45 1.1 reinoud 46 1.1 reinoud #include <uvm/uvm_extern.h> 47 1.1 reinoud #include <uvm/uvm_page.h> 48 1.1 reinoud 49 1.1 reinoud #include <dev/mm.h> 50 1.1 reinoud #include <machine/machdep.h> 51 1.1 reinoud #include <machine/thunk.h> 52 1.5 reinoud #include <machine/mcontext.h> 53 1.1 reinoud 54 1.1 reinoud #if 0 55 1.6 maya static void dump_regs(register_t *reg); 56 1.1 reinoud 57 1.1 reinoud static void 58 1.1 reinoud dump_regs(register_t *reg) 59 1.1 reinoud { 60 1.1 reinoud int i; 61 1.1 reinoud 62 1.1 reinoud /* register dump before call */ 63 1.1 reinoud const char *name[] = {"RDI", "RSI", "RDX", "RCX", "R8", "R9", "R10", 64 1.1 reinoud "R11", "R12", "R13", "R14", "R15", "RBP", "RAX", 65 1.1 reinoud "GS", "FS", "ES", "DS", "TRAPNO", "ERR", "RIP", "CS", 66 1.1 reinoud "RFLAGS", "RSP", "SS"}; 67 1.1 reinoud 68 1.1 reinoud for (i =0; i < 26; i++) 69 1.1 reinoud printf("reg[%02d] (%6s) = %"PRIx32"\n", 70 1.1 reinoud i, name[i], (uint32_t) reg[i]); 71 1.1 reinoud } 72 1.1 reinoud #endif 73 1.1 reinoud 74 1.1 reinoud 75 1.1 reinoud /* from sys/arch/amd64/include/frame.h : KEEP IN SYNC */ 76 1.1 reinoud 77 1.1 reinoud /* 78 1.1 reinoud * Signal frame 79 1.1 reinoud */ 80 1.1 reinoud struct sigframe_siginfo { 81 1.1 reinoud uint64_t sf_ra; /* return address for handler */ 82 1.1 reinoud siginfo_t sf_si; /* actual saved siginfo */ 83 1.1 reinoud ucontext_t sf_uc; /* actual saved ucontext */ 84 1.1 reinoud }; 85 1.1 reinoud 86 1.1 reinoud 87 1.1 reinoud /* 88 1.1 reinoud * mcontext extensions to handle signal delivery. 89 1.1 reinoud */ 90 1.1 reinoud void 91 1.1 reinoud sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask) 92 1.1 reinoud { 93 1.1 reinoud struct lwp *l = curlwp; 94 1.1 reinoud struct proc *p = l->l_proc; 95 1.1 reinoud struct pcb *pcb = lwp_getpcb(l); 96 1.1 reinoud struct sigacts *ps = p->p_sigacts; 97 1.1 reinoud struct sigframe_siginfo *fp, frame; 98 1.1 reinoud int sig = ksi->ksi_signo; 99 1.1 reinoud sig_t catcher = SIGACTION(p, sig).sa_handler; 100 1.2 reinoud ucontext_t *ucp; 101 1.2 reinoud register_t *reg; 102 1.1 reinoud int onstack, error; 103 1.1 reinoud char *sp; 104 1.1 reinoud 105 1.1 reinoud KASSERT(mutex_owned(p->p_lock)); 106 1.1 reinoud 107 1.2 reinoud ucp = &pcb->pcb_userret_ucp; 108 1.3 reinoud reg = (register_t *) &ucp->uc_mcontext.__gregs; 109 1.1 reinoud #if 0 110 1.1 reinoud thunk_printf("%s: ", __func__); 111 1.1 reinoud thunk_printf("flags %d, ", (int) ksi->ksi_flags); 112 1.1 reinoud thunk_printf("to lwp %d, signo %d, code %d, errno %d\n", 113 1.1 reinoud (int) ksi->ksi_lid, 114 1.1 reinoud ksi->ksi_signo, 115 1.1 reinoud ksi->ksi_code, 116 1.1 reinoud ksi->ksi_errno); 117 1.1 reinoud #endif 118 1.1 reinoud 119 1.1 reinoud /* do we need to jump onto the signal stack? */ 120 1.1 reinoud onstack = (l->l_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 121 1.1 reinoud && (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0; 122 1.1 reinoud 123 1.1 reinoud sp = ((char *) reg[24] - 128);/* why -128?*//* RSP */ 124 1.1 reinoud if (onstack) 125 1.1 reinoud sp = (char *) l->l_sigstk.ss_sp + l->l_sigstk.ss_size; 126 1.1 reinoud 127 1.1 reinoud sp -= sizeof(struct sigframe_siginfo); 128 1.1 reinoud /* 129 1.1 reinoud * Round down the stackpointer to a multiple of 16 for 130 1.1 reinoud * fxsave and the ABI 131 1.1 reinoud */ 132 1.1 reinoud fp = (struct sigframe_siginfo *) (((unsigned long)sp & ~15) - 8); 133 1.1 reinoud 134 1.1 reinoud /* set up stack frame */ 135 1.7 maxv memset(&frame, 0, sizeof(frame)); 136 1.1 reinoud frame.sf_ra = (uint64_t) ps->sa_sigdesc[sig].sd_tramp; 137 1.1 reinoud frame.sf_si._info = ksi->ksi_info; 138 1.1 reinoud 139 1.1 reinoud /* copy our userret context into sf_uc */ 140 1.1 reinoud memcpy(&frame.sf_uc, ucp, sizeof(ucontext_t)); 141 1.1 reinoud frame.sf_uc.uc_sigmask = *mask; 142 1.1 reinoud frame.sf_uc.uc_link = l->l_ctxlink; 143 1.1 reinoud frame.sf_uc.uc_flags |= (l->l_sigstk.ss_flags & SS_ONSTACK) 144 1.1 reinoud ? _UC_SETSTACK : _UC_CLRSTACK; 145 1.1 reinoud memset(&frame.sf_uc.uc_stack, 0, sizeof(frame.sf_uc.uc_stack)); 146 1.1 reinoud sendsig_reset(l, sig); 147 1.1 reinoud 148 1.1 reinoud /* copyout our frame to the stackframe */ 149 1.1 reinoud mutex_exit(p->p_lock); 150 1.1 reinoud error = copyout(&frame, fp, sizeof(frame)); 151 1.1 reinoud mutex_enter(p->p_lock); 152 1.1 reinoud 153 1.1 reinoud if (error != 0) { 154 1.1 reinoud /* 155 1.1 reinoud * Process has trashed its stack; give it an illegal 156 1.1 reinoud * instruction to halt it in its tracks. 157 1.1 reinoud */ 158 1.1 reinoud sigexit(l, SIGILL); 159 1.1 reinoud /* NOTREACHED */ 160 1.1 reinoud } 161 1.1 reinoud 162 1.1 reinoud /* set catcher and the new stack pointer */ 163 1.1 reinoud reg[24] = (register_t) fp; /* RSP */ 164 1.1 reinoud reg[21] = (register_t) catcher; /* RIP */ 165 1.1 reinoud 166 1.1 reinoud reg[ 0] = sig; /* RDI */ 167 1.1 reinoud reg[ 1] = (uint64_t) &fp->sf_si; /* RSI */ 168 1.1 reinoud reg[ 2] = (uint64_t) &fp->sf_uc; /* RDX */ 169 1.1 reinoud reg[11] = reg[ 2]; /* R15 = RDX */ 170 1.1 reinoud 171 1.1 reinoud /* Remember that we're now on the signal stack. */ 172 1.1 reinoud if (onstack) 173 1.1 reinoud l->l_sigstk.ss_flags |= SS_ONSTACK; 174 1.1 reinoud } 175 1.1 reinoud 176 1.1 reinoud void 177 1.1 reinoud setregs(struct lwp *l, struct exec_package *pack, vaddr_t stack) 178 1.1 reinoud { 179 1.1 reinoud struct pcb *pcb = lwp_getpcb(l); 180 1.2 reinoud ucontext_t *ucp; 181 1.1 reinoud register_t *reg; 182 1.1 reinoud int i; 183 1.1 reinoud 184 1.2 reinoud /* set up the user context */ 185 1.2 reinoud ucp = &pcb->pcb_userret_ucp; 186 1.3 reinoud reg = (register_t *) &ucp->uc_mcontext.__gregs; 187 1.1 reinoud for (i = 0; i < 15; i++) 188 1.1 reinoud reg[i] = 0; 189 1.1 reinoud 190 1.1 reinoud reg[13] = l->l_proc->p_psstrp; /* RBX */ 191 1.1 reinoud reg[21] = pack->ep_entry; /* RIP */ 192 1.1 reinoud reg[24] = (register_t) stack; /* RSP */ 193 1.1 reinoud 194 1.2 reinoud /* use given stack */ 195 1.1 reinoud ucp->uc_stack.ss_sp = (void *) stack; 196 1.1 reinoud ucp->uc_stack.ss_size = pack->ep_ssize; 197 1.1 reinoud 198 1.1 reinoud //dump_regs(reg); 199 1.1 reinoud } 200 1.1 reinoud 201 1.1 reinoud void 202 1.1 reinoud md_syscall_get_syscallnumber(ucontext_t *ucp, uint32_t *code) 203 1.1 reinoud { 204 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 205 1.1 reinoud *code = reg[14]; /* RAX */ 206 1.1 reinoud } 207 1.1 reinoud 208 1.1 reinoud int 209 1.1 reinoud md_syscall_getargs(lwp_t *l, ucontext_t *ucp, int nargs, int argsize, 210 1.1 reinoud register_t *args) 211 1.1 reinoud { 212 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 213 1.1 reinoud register_t *sp = (register_t *) reg[24];/* RSP */ 214 1.1 reinoud int ret; 215 1.1 reinoud 216 1.1 reinoud //dump_regs(reg); 217 1.1 reinoud 218 1.1 reinoud /* 219 1.1 reinoud * 1st 6 syscall args are passed in 220 1.1 reinoud * rdi, rsi, rdx, r10, r8 and r9 221 1.1 reinoud */ 222 1.1 reinoud args[0] = reg[ 0]; /* RDI */ 223 1.1 reinoud args[1] = reg[ 1]; /* RSI */ 224 1.1 reinoud args[2] = reg[ 2]; /* RDX */ 225 1.1 reinoud args[3] = reg[ 6]; /* R10 (RCX got clobbered) */ 226 1.1 reinoud args[4] = reg[ 4]; /* R8 */ 227 1.1 reinoud args[5] = reg[ 5]; /* R9 */ 228 1.1 reinoud 229 1.1 reinoud ret = 0; 230 1.1 reinoud if (argsize > 6 * 8) { 231 1.1 reinoud ret = copyin(sp + 1, 232 1.1 reinoud args + 6, argsize - 6 * 8); 233 1.1 reinoud } 234 1.1 reinoud 235 1.1 reinoud return ret; 236 1.1 reinoud } 237 1.1 reinoud 238 1.1 reinoud void 239 1.1 reinoud md_syscall_set_returnargs(lwp_t *l, ucontext_t *ucp, 240 1.1 reinoud int error, register_t *rval) 241 1.1 reinoud { 242 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 243 1.1 reinoud 244 1.1 reinoud reg[23] &= ~PSL_C; /* RFLAGS */ 245 1.1 reinoud if (error > 0) { 246 1.1 reinoud rval[0] = error; 247 1.1 reinoud reg[23] |= PSL_C; /* RFLAGS */ 248 1.1 reinoud } 249 1.1 reinoud 250 1.1 reinoud /* set return parameters */ 251 1.1 reinoud reg[14] = rval[0]; /* RAX */ 252 1.1 reinoud if (error == 0) 253 1.1 reinoud reg[ 2] = rval[1]; /* RDX */ 254 1.1 reinoud 255 1.1 reinoud //dump_regs(reg); 256 1.1 reinoud } 257 1.1 reinoud 258 1.1 reinoud register_t 259 1.1 reinoud md_get_pc(ucontext_t *ucp) 260 1.1 reinoud { 261 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 262 1.1 reinoud 263 1.1 reinoud return reg[21]; /* RIP */ 264 1.1 reinoud } 265 1.1 reinoud 266 1.2 reinoud register_t 267 1.2 reinoud md_get_sp(ucontext_t *ucp) 268 1.2 reinoud { 269 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 270 1.2 reinoud 271 1.2 reinoud return reg[24]; /* RSP */ 272 1.2 reinoud } 273 1.2 reinoud 274 1.1 reinoud int 275 1.1 reinoud md_syscall_check_opcode(ucontext_t *ucp) 276 1.1 reinoud { 277 1.1 reinoud uint32_t opcode; 278 1.1 reinoud 279 1.1 reinoud md_syscall_get_opcode(ucp, &opcode); 280 1.1 reinoud 281 1.1 reinoud switch (opcode) { 282 1.1 reinoud case 0xff0f: /* UD1 */ 283 1.1 reinoud case 0xff0b: /* UD2 */ 284 1.1 reinoud case 0x80cd: /* int $80 */ 285 1.1 reinoud case 0x340f: /* sysenter */ 286 1.1 reinoud case 0x050f: /* syscall */ 287 1.1 reinoud return 1; 288 1.1 reinoud default: 289 1.1 reinoud return 0; 290 1.1 reinoud } 291 1.1 reinoud } 292 1.1 reinoud 293 1.1 reinoud 294 1.1 reinoud void 295 1.1 reinoud md_syscall_get_opcode(ucontext_t *ucp, uint32_t *opcode) 296 1.1 reinoud { 297 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 298 1.1 reinoud // uint8_t *p8 = (uint8_t *) (reg[21]); 299 1.1 reinoud uint16_t *p16 = (uint16_t*) (reg[21]); /* RIP */ 300 1.1 reinoud 301 1.1 reinoud switch (*p16) { 302 1.1 reinoud case 0xff0f: /* UD1 */ 303 1.1 reinoud case 0xff0b: /* UD2 */ 304 1.1 reinoud case 0x80cd: /* int $80 */ 305 1.1 reinoud case 0x340f: /* sysenter */ 306 1.1 reinoud case 0x050f: /* syscall */ 307 1.1 reinoud *opcode = *p16; 308 1.1 reinoud break; 309 1.1 reinoud default: 310 1.1 reinoud *opcode = 0; 311 1.1 reinoud } 312 1.1 reinoud } 313 1.1 reinoud 314 1.1 reinoud void 315 1.1 reinoud md_syscall_inc_pc(ucontext_t *ucp, uint32_t opcode) 316 1.1 reinoud { 317 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 318 1.1 reinoud 319 1.1 reinoud /* advance program counter */ 320 1.1 reinoud switch (opcode) { 321 1.1 reinoud case 0xff0f: /* UD1 */ 322 1.1 reinoud case 0xff0b: /* UD2 */ 323 1.1 reinoud case 0x80cd: /* int $80 */ 324 1.1 reinoud case 0x340f: /* sysenter */ 325 1.1 reinoud case 0x050f: /* syscall */ 326 1.1 reinoud reg[21] += 2; /* RIP */ 327 1.1 reinoud break; 328 1.1 reinoud default: 329 1.1 reinoud panic("%s, unknown illegal instruction: opcode = %x\n", 330 1.1 reinoud __func__, (uint32_t) opcode); 331 1.1 reinoud } 332 1.1 reinoud } 333 1.1 reinoud 334 1.1 reinoud void 335 1.1 reinoud md_syscall_dec_pc(ucontext_t *ucp, uint32_t opcode) 336 1.1 reinoud { 337 1.3 reinoud register_t *reg = (register_t *) &ucp->uc_mcontext.__gregs; 338 1.1 reinoud 339 1.1 reinoud switch (opcode) { 340 1.1 reinoud case 0xff0f: /* UD1 */ 341 1.1 reinoud case 0xff0b: /* UD2 */ 342 1.1 reinoud case 0x80cd: /* int $80 */ 343 1.1 reinoud case 0x340f: /* sysenter */ 344 1.1 reinoud case 0x050f: /* syscall */ 345 1.1 reinoud reg[21] -= 2; /* RIP */ 346 1.1 reinoud break; 347 1.1 reinoud default: 348 1.1 reinoud panic("%s, unknown illegal instruction: opcode = %x\n", 349 1.1 reinoud __func__, (uint32_t) opcode); 350 1.1 reinoud } 351 1.1 reinoud } 352 1.1 reinoud 353