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