xref: /original-bsd/sys/i386/i386/trap.c (revision 817cfbae)
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.6 (Berkeley) 11/20/91
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_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 #ifdef	NPX
126 		/* if a transparent fault (due to context switch "late") */
127 		if (npxdna()) return;
128 #endif
129 		ucode = FPE_FPU_NP_TRAP;
130 		i = SIGFPE;
131 		break;
132 
133 	case T_BOUND|T_USER:
134 		ucode = FPE_SUBRNG_TRAP;
135 		i = SIGFPE;
136 		break;
137 
138 	case T_OFLOW|T_USER:
139 		ucode = FPE_INTOVF_TRAP;
140 		i = SIGFPE;
141 		break;
142 
143 	case T_DIVIDE|T_USER:
144 		ucode = FPE_INTDIV_TRAP;
145 		i = SIGFPE;
146 		break;
147 
148 	case T_ARITHTRAP|T_USER:
149 		ucode = code;
150 		i = SIGFPE;
151 		break;
152 
153 	case T_PAGEFLT:			/* allow page faults in kernel mode */
154 		if (code & PGEX_P) goto we_re_toast;
155 
156 		/* fall into */
157 	case T_PAGEFLT|T_USER:		/* page fault */
158 	    {
159 		register vm_offset_t va;
160 		register struct vmspace *vm = p->p_vmspace;
161 		register vm_map_t map;
162 		int rv;
163 		vm_prot_t ftype;
164 		extern vm_map_t kernel_map;
165 		unsigned nss,v;
166 
167 		va = trunc_page((vm_offset_t)eva);
168 		/*
169 		 * It is only a kernel address space fault iff:
170 		 * 	1. (type & T_USER) == 0  and
171 		 * 	2. pcb_onfault not set or
172 		 *	3. pcb_onfault set but supervisor space fault
173 		 * The last can occur during an exec() copyin where the
174 		 * argument space is lazy-allocated.
175 		 */
176 		if (type == T_PAGEFLT && va >= 0xfe000000)
177 			map = kernel_map;
178 		else
179 			map = &vm->vm_map;
180 		if (code & PGEX_W)
181 			ftype = VM_PROT_READ | VM_PROT_WRITE;
182 		else
183 			ftype = VM_PROT_READ;
184 
185 #ifdef DEBUG
186 		if (map == kernel_map && va == 0) {
187 			printf("trap: bad kernel access at %x\n", va);
188 			goto we_re_toast;
189 		}
190 #endif
191 		/*
192 		 * XXX: rude hack to make stack limits "work"
193 		 */
194 		nss = 0;
195 		if ((caddr_t)va >= vm->vm_maxsaddr && map != kernel_map) {
196 			nss = clrnd(btoc(USRSTACK-(unsigned)va));
197 			if (nss > btoc(p->p_rlimit[RLIMIT_STACK].rlim_cur)) {
198 				rv = KERN_FAILURE;
199 				goto nogo;
200 			}
201 		}
202 
203 		/* check if page table is mapped, if not, fault it first */
204 #define pde_v(v) (PTD[((v)>>PD_SHIFT)&1023].pd_v)
205 		if (!pde_v(va)) {
206 			v = trunc_page(vtopte(va));
207 			rv = vm_fault(map, v, ftype, FALSE);
208 			if (rv != KERN_SUCCESS) goto nogo;
209 			/* check if page table fault, increment wiring */
210 			vm_map_pageable(map, v, round_page(v+1), FALSE);
211 		} else v=0;
212 		rv = vm_fault(map, va, ftype, FALSE);
213 		if (rv == KERN_SUCCESS) {
214 			/*
215 			 * XXX: continuation of rude stack hack
216 			 */
217 			if (nss > vm->vm_ssize)
218 				vm->vm_ssize = nss;
219 			va = trunc_page(vtopte(va));
220 			/* for page table, increment wiring
221 			   as long as not a page table fault as well */
222 			if (!v && type != T_PAGEFLT)
223 			  vm_map_pageable(map, va, round_page(va+1), FALSE);
224 			if (type == T_PAGEFLT)
225 				return;
226 			goto out;
227 		}
228 nogo:
229 		if (type == T_PAGEFLT) {
230 			if (curpcb->pcb_onfault)
231 				goto copyfault;
232 			printf("vm_fault(%x, %x, %x, 0) -> %x\n",
233 			       map, va, ftype, rv);
234 			printf("  type %x, code %x\n",
235 			       type, code);
236 			goto we_re_toast;
237 		}
238 		i = (rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV;
239 		break;
240 	    }
241 
242 	case T_TRCTRAP:	 /* trace trap -- someone single stepping lcall's */
243 		frame.tf_eflags &= ~PSL_T;
244 
245 			/* Q: how do we turn it on again? */
246 		return;
247 
248 	case T_BPTFLT|T_USER:		/* bpt instruction fault */
249 	case T_TRCTRAP|T_USER:		/* trace trap */
250 		frame.tf_eflags &= ~PSL_T;
251 		i = SIGTRAP;
252 		break;
253 
254 #include "isa.h"
255 #if	NISA > 0
256 	case T_NMI:
257 	case T_NMI|T_USER:
258 		/* machine/parity/power fail/"kitchen sink" faults */
259 		if(isa_nmi(code) == 0) return;
260 		else goto we_re_toast;
261 #endif
262 	}
263 
264 	trapsignal(p, i, ucode);
265 	if ((type & T_USER) == 0)
266 		return;
267 out:
268 	while (i = CURSIG(p))
269 		psig(i);
270 	p->p_pri = p->p_usrpri;
271 	if (want_resched) {
272 		int pl;
273 		/*
274 		 * Since we are curproc, clock will normally just change
275 		 * our priority without moving us from one queue to another
276 		 * (since the running process is not on a queue.)
277 		 * If that happened after we setrq ourselves but before we
278 		 * swtch()'ed, we might not be on the queue indicated by
279 		 * our priority.
280 		 */
281 		pl = splclock();
282 		setrq(p);
283 		p->p_stats->p_ru.ru_nivcsw++;
284 		swtch();
285 		splx(pl);
286 		while (i = CURSIG(p))
287 			psig(i);
288 	}
289 	if (p->p_stats->p_prof.pr_scale) {
290 		int ticks;
291 		struct timeval *tv = &p->p_stime;
292 
293 		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
294 			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
295 		if (ticks) {
296 #ifdef PROFTIMER
297 			extern int profscale;
298 			addupc(frame.tf_eip, &p->p_stats->p_prof,
299 			    ticks * profscale);
300 #else
301 			addupc(frame.tf_eip, &p->p_stats->p_prof, ticks);
302 #endif
303 		}
304 	}
305 	curpri = p->p_pri;
306 	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
307 }
308 
309 /*
310  * syscall(frame):
311  *	System call request from POSIX system call gate interface to kernel.
312  * Like trap(), argument is call by reference.
313  */
314 /*ARGSUSED*/
315 syscall(frame)
316 	volatile struct syscframe frame;
317 {
318 	register int *locr0 = ((int *)&frame);
319 	register caddr_t params;
320 	register int i;
321 	register struct sysent *callp;
322 	register struct proc *p = curproc;
323 	struct timeval syst;
324 	int error, opc;
325 	int args[8], rval[2];
326 	int code;
327 
328 #ifdef lint
329 	r0 = 0; r0 = r0; r1 = 0; r1 = r1;
330 #endif
331 	syst = p->p_stime;
332 	if (ISPL(frame.sf_cs) != SEL_UPL)
333 		panic("syscall");
334 
335 	code = frame.sf_eax;
336 	p->p_regs = (int *)&frame;
337 	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
338 	params = (caddr_t)frame.sf_esp + sizeof (int) ;
339 
340 	/*
341 	 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always.
342 	 */
343 	opc = frame.sf_eip - 7;
344 	callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
345 	if (callp == sysent) {
346 		i = fuword(params);
347 		params += sizeof (int);
348 		callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
349 	}
350 
351 	if ((i = callp->sy_narg * sizeof (int)) &&
352 	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
353 		frame.sf_eax = error;
354 		frame.sf_eflags |= PSL_C;	/* carry bit */
355 #ifdef KTRACE
356 		if (KTRPOINT(p, KTR_SYSCALL))
357 			ktrsyscall(p->p_tracep, code, callp->sy_narg, &args);
358 #endif
359 		goto done;
360 	}
361 #ifdef KTRACE
362 	if (KTRPOINT(p, KTR_SYSCALL))
363 		ktrsyscall(p->p_tracep, code, callp->sy_narg, &args);
364 #endif
365 	rval[0] = 0;
366 	rval[1] = frame.sf_edx;
367 	error = (*callp->sy_call)(p, args, rval);
368 	if (error == ERESTART)
369 		frame.sf_eip = opc;
370 	else if (error != EJUSTRETURN) {
371 		if (error) {
372 			frame.sf_eax = error;
373 			frame.sf_eflags |= PSL_C;	/* carry bit */
374 		} else {
375 			frame.sf_eax = rval[0];
376 			frame.sf_edx = rval[1];
377 			frame.sf_eflags &= ~PSL_C;	/* carry bit */
378 		}
379 	}
380 	/* else if (error == EJUSTRETURN) */
381 		/* nothing to do */
382 done:
383 	/*
384 	 * Reinitialize proc pointer `p' as it may be different
385 	 * if this is a child returning from fork syscall.
386 	 */
387 	p = curproc;
388 	while (i = CURSIG(p))
389 		psig(i);
390 	p->p_pri = p->p_usrpri;
391 	if (want_resched) {
392 		int pl;
393 		/*
394 		 * Since we are curproc, clock will normally just change
395 		 * our priority without moving us from one queue to another
396 		 * (since the running process is not on a queue.)
397 		 * If that happened after we setrq ourselves but before we
398 		 * swtch()'ed, we might not be on the queue indicated by
399 		 * our priority.
400 		 */
401 		pl = splclock();
402 		setrq(p);
403 		p->p_stats->p_ru.ru_nivcsw++;
404 		swtch();
405 		splx(pl);
406 		while (i = CURSIG(p))
407 			psig(i);
408 	}
409 	if (p->p_stats->p_prof.pr_scale) {
410 		int ticks;
411 		struct timeval *tv = &p->p_stime;
412 
413 		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
414 			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
415 		if (ticks) {
416 #ifdef PROFTIMER
417 			extern int profscale;
418 			addupc(frame.sf_eip, &p->p_stats->p_prof,
419 			    ticks * profscale);
420 #else
421 			addupc(frame.sf_eip, &p->p_stats->p_prof, ticks);
422 #endif
423 		}
424 	}
425 	curpri = p->p_pri;
426 #ifdef KTRACE
427 	if (KTRPOINT(p, KTR_SYSRET))
428 		ktrsysret(p->p_tracep, code, error, rval[0]);
429 #endif
430 }
431