1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * the University of Utah, and William Jolitz. 7 * 8 * %sccs.include.redist.c% 9 * 10 * @(#)trap.c 7.10 (Berkeley) 05/11/92 11 */ 12 13 /* 14 * 386 Trap and System call handleing 15 */ 16 17 #include "machine/cpu.h" 18 #include "machine/psl.h" 19 #include "machine/reg.h" 20 21 #include "param.h" 22 #include "systm.h" 23 #include "proc.h" 24 #include "user.h" 25 #include "acct.h" 26 #include "kernel.h" 27 #ifdef KTRACE 28 #include "ktrace.h" 29 #endif 30 31 #include "vm/vm_param.h" 32 #include "vm/pmap.h" 33 #include "vm/vm_map.h" 34 35 #include "machine/trap.h" 36 #include "machine/dbg.h" 37 38 39 struct sysent sysent[]; 40 int nsysent; 41 unsigned rcr2(); 42 extern short cpl; 43 44 45 /* 46 * trap(frame): 47 * Exception, fault, and trap interface to BSD kernel. This 48 * common code is called from assembly language IDT gate entry 49 * routines that prepare a suitable stack frame, and restore this 50 * frame after the exception has been processed. Note that the 51 * effect is as if the arguments were passed call by reference. 52 */ 53 54 /*ARGSUSED*/ 55 trap(frame) 56 struct trapframe frame; 57 { 58 register int i; 59 register struct proc *p = curproc; 60 struct timeval syst; 61 int ucode, type, code, eva; 62 extern int cold; 63 64 if(cold) goto we_re_toast; 65 frame.tf_eflags &= ~PSL_NT; /* clear nested trap XXX */ 66 type = frame.tf_trapno; 67 68 if (curpcb->pcb_onfault && frame.tf_trapno != 0xc) { 69 frame.tf_eip = (int)curpcb->pcb_onfault; 70 return; 71 } 72 73 syst = p->p_stime; 74 if (ISPL(frame.tf_cs) == SEL_UPL) { 75 type |= T_USER; 76 p->p_md.md_regs = (int *)&frame; 77 curpcb->pcb_flags |= FM_TRAP; /* used by sendsig */ 78 } 79 80 ucode=0; 81 eva = rcr2(); 82 code = frame.tf_err; 83 switch (type) { 84 85 default: 86 we_re_toast: 87 #ifdef KDB 88 if (kdb_trap(&psl)) 89 return; 90 #endif 91 92 printf("trap type %d code = %x eip = %x cs = %x eflags = %x ", 93 frame.tf_trapno, frame.tf_err, frame.tf_eip, 94 frame.tf_cs, frame.tf_eflags); 95 printf("cr2 %x cpl %x\n", eva, cpl); 96 type &= ~T_USER; 97 panic("trap"); 98 /*NOTREACHED*/ 99 100 case T_SEGNPFLT|T_USER: 101 case T_PROTFLT|T_USER: /* protection fault */ 102 copyfault: 103 ucode = code + BUS_SEGM_FAULT ; 104 i = SIGBUS; 105 break; 106 107 case T_PRIVINFLT|T_USER: /* privileged instruction fault */ 108 case T_RESADFLT|T_USER: /* reserved addressing fault */ 109 case T_RESOPFLT|T_USER: /* reserved operand fault */ 110 case T_FPOPFLT|T_USER: /* coprocessor operand fault */ 111 ucode = type &~ T_USER; 112 i = SIGILL; 113 break; 114 115 case T_ASTFLT|T_USER: /* Allow process switch */ 116 case T_ASTFLT: 117 astoff(); 118 if ((p->p_flag & SOWEUPC) && p->p_stats->p_prof.pr_scale) { 119 addupc(frame.tf_eip, &p->p_stats->p_prof, 1); 120 p->p_flag &= ~SOWEUPC; 121 } 122 goto out; 123 124 case T_DNA|T_USER: 125 #include "npx.h" 126 #if NNPX > 0 127 /* if a transparent fault (due to context switch "late") */ 128 if (npxdna()) return; 129 #endif 130 ucode = FPE_FPU_NP_TRAP; 131 i = SIGFPE; 132 break; 133 134 case T_BOUND|T_USER: 135 ucode = FPE_SUBRNG_TRAP; 136 i = SIGFPE; 137 break; 138 139 case T_OFLOW|T_USER: 140 ucode = FPE_INTOVF_TRAP; 141 i = SIGFPE; 142 break; 143 144 case T_DIVIDE|T_USER: 145 ucode = FPE_INTDIV_TRAP; 146 i = SIGFPE; 147 break; 148 149 case T_ARITHTRAP|T_USER: 150 ucode = code; 151 i = SIGFPE; 152 break; 153 154 case T_PAGEFLT: /* allow page faults in kernel mode */ 155 if (code & PGEX_P) goto we_re_toast; 156 157 /* fall into */ 158 case T_PAGEFLT|T_USER: /* page fault */ 159 { 160 register vm_offset_t va; 161 register struct vmspace *vm = p->p_vmspace; 162 register vm_map_t map; 163 int rv; 164 vm_prot_t ftype; 165 extern vm_map_t kernel_map; 166 unsigned nss,v; 167 168 va = trunc_page((vm_offset_t)eva); 169 /* 170 * It is only a kernel address space fault iff: 171 * 1. (type & T_USER) == 0 and 172 * 2. pcb_onfault not set or 173 * 3. pcb_onfault set but supervisor space fault 174 * The last can occur during an exec() copyin where the 175 * argument space is lazy-allocated. 176 */ 177 if (type == T_PAGEFLT && va >= 0xfe000000) 178 map = kernel_map; 179 else 180 map = &vm->vm_map; 181 if (code & PGEX_W) 182 ftype = VM_PROT_READ | VM_PROT_WRITE; 183 else 184 ftype = VM_PROT_READ; 185 186 #ifdef DEBUG 187 if (map == kernel_map && va == 0) { 188 printf("trap: bad kernel access at %x\n", va); 189 goto we_re_toast; 190 } 191 #endif 192 /* 193 * XXX: rude hack to make stack limits "work" 194 */ 195 nss = 0; 196 if ((caddr_t)va >= vm->vm_maxsaddr && map != kernel_map) { 197 nss = clrnd(btoc(USRSTACK-(unsigned)va)); 198 if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 199 rv = KERN_FAILURE; 200 goto nogo; 201 } 202 } 203 204 /* check if page table is mapped, if not, fault it first */ 205 #define pde_v(v) (PTD[((v)>>PD_SHIFT)&1023].pd_v) 206 if (!pde_v(va)) { 207 v = trunc_page(vtopte(va)); 208 rv = vm_fault(map, v, ftype, FALSE); 209 if (rv != KERN_SUCCESS) goto nogo; 210 /* check if page table fault, increment wiring */ 211 vm_map_pageable(map, v, round_page(v+1), FALSE); 212 } else v=0; 213 rv = vm_fault(map, va, ftype, FALSE); 214 if (rv == KERN_SUCCESS) { 215 /* 216 * XXX: continuation of rude stack hack 217 */ 218 if (nss > vm->vm_ssize) 219 vm->vm_ssize = nss; 220 va = trunc_page(vtopte(va)); 221 /* for page table, increment wiring 222 as long as not a page table fault as well */ 223 if (!v && type != T_PAGEFLT) 224 vm_map_pageable(map, va, round_page(va+1), FALSE); 225 if (type == T_PAGEFLT) 226 return; 227 goto out; 228 } 229 nogo: 230 if (type == T_PAGEFLT) { 231 if (curpcb->pcb_onfault) 232 goto copyfault; 233 printf("vm_fault(%x, %x, %x, 0) -> %x\n", 234 map, va, ftype, rv); 235 printf(" type %x, code %x\n", 236 type, code); 237 goto we_re_toast; 238 } 239 i = (rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV; 240 break; 241 } 242 243 case T_TRCTRAP: /* trace trap -- someone single stepping lcall's */ 244 frame.tf_eflags &= ~PSL_T; 245 246 /* Q: how do we turn it on again? */ 247 return; 248 249 case T_BPTFLT|T_USER: /* bpt instruction fault */ 250 case T_TRCTRAP|T_USER: /* trace trap */ 251 frame.tf_eflags &= ~PSL_T; 252 i = SIGTRAP; 253 break; 254 255 #include "isa.h" 256 #if NISA > 0 257 case T_NMI: 258 case T_NMI|T_USER: 259 /* machine/parity/power fail/"kitchen sink" faults */ 260 if(isa_nmi(code) == 0) return; 261 else goto we_re_toast; 262 #endif 263 } 264 265 trapsignal(p, i, ucode); 266 if ((type & T_USER) == 0) 267 return; 268 out: 269 while (i = CURSIG(p)) 270 psig(i); 271 p->p_pri = p->p_usrpri; 272 if (want_resched) { 273 int pl; 274 /* 275 * Since we are curproc, clock will normally just change 276 * our priority without moving us from one queue to another 277 * (since the running process is not on a queue.) 278 * If that happened after we setrq ourselves but before we 279 * swtch()'ed, we might not be on the queue indicated by 280 * our priority. 281 */ 282 pl = splclock(); 283 setrq(p); 284 p->p_stats->p_ru.ru_nivcsw++; 285 swtch(); 286 splx(pl); 287 while (i = CURSIG(p)) 288 psig(i); 289 } 290 if (p->p_stats->p_prof.pr_scale) { 291 int ticks; 292 struct timeval *tv = &p->p_stime; 293 294 ticks = ((tv->tv_sec - syst.tv_sec) * 1000 + 295 (tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000); 296 if (ticks) { 297 #ifdef PROFTIMER 298 extern int profscale; 299 addupc(frame.tf_eip, &p->p_stats->p_prof, 300 ticks * profscale); 301 #else 302 addupc(frame.tf_eip, &p->p_stats->p_prof, ticks); 303 #endif 304 } 305 } 306 curpri = p->p_pri; 307 curpcb->pcb_flags &= ~FM_TRAP; /* used by sendsig */ 308 } 309 310 /* 311 * syscall(frame): 312 * System call request from POSIX system call gate interface to kernel. 313 * Like trap(), argument is call by reference. 314 */ 315 /*ARGSUSED*/ 316 syscall(frame) 317 volatile struct syscframe frame; 318 { 319 register int *locr0 = ((int *)&frame); 320 register caddr_t params; 321 register int i; 322 register struct sysent *callp; 323 register struct proc *p = curproc; 324 struct timeval syst; 325 int error, opc; 326 int args[8], rval[2]; 327 unsigned int code; 328 329 #ifdef lint 330 r0 = 0; r0 = r0; r1 = 0; r1 = r1; 331 #endif 332 syst = p->p_stime; 333 if (ISPL(frame.sf_cs) != SEL_UPL) 334 panic("syscall"); 335 336 code = frame.sf_eax; 337 p->p_md.md_regs = (int *)&frame; 338 curpcb->pcb_flags &= ~FM_TRAP; /* used by sendsig */ 339 params = (caddr_t)frame.sf_esp + sizeof (int) ; 340 341 /* 342 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always. 343 */ 344 opc = frame.sf_eip - 7; 345 callp = (code >= nsysent) ? &sysent[63] : &sysent[code]; 346 if (callp == sysent) { 347 code = fuword(params); 348 params += sizeof (int); 349 callp = (code >= nsysent) ? &sysent[63] : &sysent[code]; 350 } 351 352 if ((i = callp->sy_narg * sizeof (int)) && 353 (error = copyin(params, (caddr_t)args, (u_int)i))) { 354 frame.sf_eax = error; 355 frame.sf_eflags |= PSL_C; /* carry bit */ 356 #ifdef KTRACE 357 if (KTRPOINT(p, KTR_SYSCALL)) 358 ktrsyscall(p->p_tracep, code, callp->sy_narg, &args); 359 #endif 360 goto done; 361 } 362 #ifdef KTRACE 363 if (KTRPOINT(p, KTR_SYSCALL)) 364 ktrsyscall(p->p_tracep, code, callp->sy_narg, &args); 365 #endif 366 rval[0] = 0; 367 rval[1] = frame.sf_edx; 368 error = (*callp->sy_call)(p, args, rval); 369 if (error == ERESTART) 370 frame.sf_eip = opc; 371 else if (error != EJUSTRETURN) { 372 if (error) { 373 frame.sf_eax = error; 374 frame.sf_eflags |= PSL_C; /* carry bit */ 375 } else { 376 frame.sf_eax = rval[0]; 377 frame.sf_edx = rval[1]; 378 frame.sf_eflags &= ~PSL_C; /* carry bit */ 379 } 380 } 381 /* else if (error == EJUSTRETURN) */ 382 /* nothing to do */ 383 done: 384 /* 385 * Reinitialize proc pointer `p' as it may be different 386 * if this is a child returning from fork syscall. 387 */ 388 p = curproc; 389 while (i = CURSIG(p)) 390 psig(i); 391 p->p_pri = p->p_usrpri; 392 if (want_resched) { 393 int pl; 394 /* 395 * Since we are curproc, clock will normally just change 396 * our priority without moving us from one queue to another 397 * (since the running process is not on a queue.) 398 * If that happened after we setrq ourselves but before we 399 * swtch()'ed, we might not be on the queue indicated by 400 * our priority. 401 */ 402 pl = splclock(); 403 setrq(p); 404 p->p_stats->p_ru.ru_nivcsw++; 405 swtch(); 406 splx(pl); 407 while (i = CURSIG(p)) 408 psig(i); 409 } 410 if (p->p_stats->p_prof.pr_scale) { 411 int ticks; 412 struct timeval *tv = &p->p_stime; 413 414 ticks = ((tv->tv_sec - syst.tv_sec) * 1000 + 415 (tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000); 416 if (ticks) { 417 #ifdef PROFTIMER 418 extern int profscale; 419 addupc(frame.sf_eip, &p->p_stats->p_prof, 420 ticks * profscale); 421 #else 422 addupc(frame.sf_eip, &p->p_stats->p_prof, ticks); 423 #endif 424 } 425 } 426 curpri = p->p_pri; 427 #ifdef KTRACE 428 if (KTRPOINT(p, KTR_SYSRET)) 429 ktrsysret(p->p_tracep, code, error, rval[0]); 430 #endif 431 } 432