xref: /original-bsd/sys/i386/i386/trap.c (revision 217010d6)
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.4 (Berkeley) 05/13/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 "seg.h"
26 #include "acct.h"
27 #include "kernel.h"
28 #ifdef KTRACE
29 #include "ktrace.h"
30 #endif
31 
32 #include "vm/vm_param.h"
33 #include "vm/pmap.h"
34 #include "vm/vm_map.h"
35 #include "sys/vmmeter.h"
36 
37 #include "machine/trap.h"
38 #include "machine/dbg.h"
39 
40 
41 struct	sysent sysent[];
42 int	nsysent;
43 unsigned rcr2();
44 extern short cpl;
45 
46 
47 /*
48  * trap(frame):
49  *	Exception, fault, and trap interface to BSD kernel. This
50  * common code is called from assembly language IDT gate entry
51  * routines that prepare a suitable stack frame, and restore this
52  * frame after the exception has been processed. Note that the
53  * effect is as if the arguments were passed call by reference.
54  */
55 
56 /*ARGSUSED*/
57 trap(frame)
58 	struct trapframe frame;
59 {
60 	register int i;
61 	register struct proc *p = curproc;
62 	struct timeval syst;
63 	int ucode, type, code, eva;
64 
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 		/*
273 		 * Since we are curproc, clock will normally just change
274 		 * our priority without moving us from one queue to another
275 		 * (since the running process is not on a queue.)
276 		 * If that happened after we setrq ourselves but before we
277 		 * swtch()'ed, we might not be on the queue indicated by
278 		 * our priority.
279 		 */
280 		(void) splclock();
281 		setrq(p);
282 		p->p_stats->p_ru.ru_nivcsw++;
283 		swtch();
284 		while (i = CURSIG(p))
285 			psig(i);
286 	}
287 	if (p->p_stats->p_prof.pr_scale) {
288 		int ticks;
289 		struct timeval *tv = &p->p_stime;
290 
291 		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
292 			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
293 		if (ticks) {
294 #ifdef PROFTIMER
295 			extern int profscale;
296 			addupc(frame.tf_eip, &p->p_stats->p_prof,
297 			    ticks * profscale);
298 #else
299 			addupc(frame.tf_eip, &p->p_stats->p_prof, ticks);
300 #endif
301 		}
302 	}
303 	curpri = p->p_pri;
304 	curpcb->pcb_flags &= ~FM_TRAP;	/* used by sendsig */
305 	spl0(); /*XXX*/
306 }
307 
308 /*
309  * syscall(frame):
310  *	System call request from POSIX system call gate interface to kernel.
311  * Like trap(), argument is call by reference.
312  */
313 /*ARGSUSED*/
314 syscall(frame)
315 	volatile struct syscframe frame;
316 {
317 	register int *locr0 = ((int *)&frame);
318 	register caddr_t params;
319 	register int i;
320 	register struct sysent *callp;
321 	register struct proc *p = curproc;
322 	struct timeval syst;
323 	int error, opc;
324 	int args[8], rval[2];
325 	int code;
326 
327 #ifdef lint
328 	r0 = 0; r0 = r0; r1 = 0; r1 = r1;
329 #endif
330 	syst = p->p_stime;
331 	if (ISPL(frame.sf_cs) != SEL_UPL)
332 		panic("syscall");
333 
334 	code = frame.sf_eax;
335 	p->p_regs = (int *)&frame;
336 	params = (caddr_t)frame.sf_esp + sizeof (int) ;
337 
338 	/*
339 	 * Reconstruct pc, assuming lcall $X,y is 7 bytes, as it is always.
340 	 */
341 	opc = frame.sf_eip - 7;
342 	callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
343 	if (callp == sysent) {
344 		i = fuword(params);
345 		params += sizeof (int);
346 		callp = (code >= nsysent) ? &sysent[63] : &sysent[code];
347 	}
348 
349 	if ((i = callp->sy_narg * sizeof (int)) &&
350 	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
351 		frame.sf_eax = error;
352 		frame.sf_eflags |= PSL_C;	/* carry bit */
353 #ifdef KTRACE
354 		if (KTRPOINT(p, KTR_SYSCALL))
355 			ktrsyscall(p->p_tracep, code, callp->sy_narg, &args);
356 #endif
357 		goto done;
358 	}
359 #ifdef KTRACE
360 	if (KTRPOINT(p, KTR_SYSCALL))
361 		ktrsyscall(p->p_tracep, code, callp->sy_narg, &args);
362 #endif
363 	rval[0] = 0;
364 	rval[1] = frame.sf_edx;
365 	error = (*callp->sy_call)(p, args, rval);
366 	if (error == ERESTART)
367 		frame.sf_eip = opc;
368 	else if (error != EJUSTRETURN) {
369 		if (error) {
370 			frame.sf_eax = error;
371 			frame.sf_eflags |= PSL_C;	/* carry bit */
372 		} else {
373 			frame.sf_eax = rval[0];
374 			frame.sf_edx = rval[1];
375 			frame.sf_eflags &= ~PSL_C;	/* carry bit */
376 		}
377 	}
378 	/* else if (error == EJUSTRETURN) */
379 		/* nothing to do */
380 done:
381 	/*
382 	 * Reinitialize proc pointer `p' as it may be different
383 	 * if this is a child returning from fork syscall.
384 	 */
385 	p = curproc;
386 	while (i = CURSIG(p))
387 		psig(i);
388 	p->p_pri = p->p_usrpri;
389 	if (want_resched) {
390 		/*
391 		 * Since we are curproc, clock will normally just change
392 		 * our priority without moving us from one queue to another
393 		 * (since the running process is not on a queue.)
394 		 * If that happened after we setrq ourselves but before we
395 		 * swtch()'ed, we might not be on the queue indicated by
396 		 * our priority.
397 		 */
398 		(void) splclock();
399 		setrq(p);
400 		p->p_stats->p_ru.ru_nivcsw++;
401 		swtch();
402 		while (i = CURSIG(p))
403 			psig(i);
404 	}
405 	if (p->p_stats->p_prof.pr_scale) {
406 		int ticks;
407 		struct timeval *tv = &p->p_stime;
408 
409 		ticks = ((tv->tv_sec - syst.tv_sec) * 1000 +
410 			(tv->tv_usec - syst.tv_usec) / 1000) / (tick / 1000);
411 		if (ticks) {
412 #ifdef PROFTIMER
413 			extern int profscale;
414 			addupc(frame.sf_eip, &p->p_stats->p_prof,
415 			    ticks * profscale);
416 #else
417 			addupc(frame.sf_eip, &p->p_stats->p_prof, ticks);
418 #endif
419 		}
420 	}
421 	curpri = p->p_pri;
422 #ifdef KTRACE
423 	if (KTRPOINT(p, KTR_SYSRET))
424 		ktrsysret(p->p_tracep, code, error, rval[0]);
425 #endif
426 }
427