xref: /freebsd/sys/kern/subr_trap.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
1 /*-
2  * Copyright (C) 1994, David Greenman
3  * Copyright (c) 1990, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * the University of Utah, and William Jolitz.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the University of
20  *	California, Berkeley and its contributors.
21  * 4. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	from: @(#)trap.c	7.4 (Berkeley) 5/13/91
38  *	$Id: trap.c,v 1.82 1996/09/01 10:10:12 davidg Exp $
39  */
40 
41 /*
42  * 386 Trap and System call handling
43  */
44 
45 #include "opt_ktrace.h"
46 #include "opt_ddb.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/acct.h>
52 #include <sys/kernel.h>
53 #include <sys/syscall.h>
54 #include <sys/sysent.h>
55 #include <sys/queue.h>
56 #include <sys/vmmeter.h>
57 #ifdef KTRACE
58 #include <sys/ktrace.h>
59 #endif
60 
61 #include <vm/vm.h>
62 #include <vm/vm_param.h>
63 #include <vm/vm_prot.h>
64 #include <vm/lock.h>
65 #include <vm/pmap.h>
66 #include <vm/vm_kern.h>
67 #include <vm/vm_map.h>
68 #include <vm/vm_page.h>
69 #include <vm/vm_extern.h>
70 
71 #include <sys/user.h>
72 
73 #include <machine/cpu.h>
74 #include <machine/md_var.h>
75 #include <machine/psl.h>
76 #include <machine/reg.h>
77 #include <machine/trap.h>
78 #include <machine/../isa/isa_device.h>
79 
80 #ifdef POWERFAIL_NMI
81 #include <sys/syslog.h>
82 #include <machine/clock.h>
83 #endif
84 
85 #include "isa.h"
86 #include "npx.h"
87 
88 int (*pmath_emulate) __P((struct trapframe *));
89 
90 extern void trap __P((struct trapframe frame));
91 extern int trapwrite __P((unsigned addr));
92 extern void syscall __P((struct trapframe frame));
93 
94 static int trap_pfault __P((struct trapframe *, int));
95 static void trap_fatal __P((struct trapframe *));
96 void dblfault_handler __P((void));
97 
98 extern inthand_t IDTVEC(syscall);
99 
100 #define MAX_TRAP_MSG		28
101 static char *trap_msg[] = {
102 	"",					/*  0 unused */
103 	"privileged instruction fault",		/*  1 T_PRIVINFLT */
104 	"",					/*  2 unused */
105 	"breakpoint instruction fault",		/*  3 T_BPTFLT */
106 	"",					/*  4 unused */
107 	"",					/*  5 unused */
108 	"arithmetic trap",			/*  6 T_ARITHTRAP */
109 	"system forced exception",		/*  7 T_ASTFLT */
110 	"",					/*  8 unused */
111 	"general protection fault",		/*  9 T_PROTFLT */
112 	"trace trap",				/* 10 T_TRCTRAP */
113 	"",					/* 11 unused */
114 	"page fault",				/* 12 T_PAGEFLT */
115 	"",					/* 13 unused */
116 	"alignment fault",			/* 14 T_ALIGNFLT */
117 	"",					/* 15 unused */
118 	"",					/* 16 unused */
119 	"",					/* 17 unused */
120 	"integer divide fault",			/* 18 T_DIVIDE */
121 	"non-maskable interrupt trap",		/* 19 T_NMI */
122 	"overflow trap",			/* 20 T_OFLOW */
123 	"FPU bounds check fault",		/* 21 T_BOUND */
124 	"FPU device not available",		/* 22 T_DNA */
125 	"double fault",				/* 23 T_DOUBLEFLT */
126 	"FPU operand fetch fault",		/* 24 T_FPOPFLT */
127 	"invalid TSS fault",			/* 25 T_TSSFLT */
128 	"segment not present fault",		/* 26 T_SEGNPFLT */
129 	"stack fault",				/* 27 T_STKFLT */
130 	"machine check trap",			/* 28 T_MCHK */
131 };
132 
133 static void userret __P((struct proc *p, struct trapframe *frame,
134 			 u_quad_t oticks));
135 
136 static inline void
137 userret(p, frame, oticks)
138 	struct proc *p;
139 	struct trapframe *frame;
140 	u_quad_t oticks;
141 {
142 	int sig, s;
143 
144 	while ((sig = CURSIG(p)) != 0)
145 		postsig(sig);
146 	p->p_priority = p->p_usrpri;
147 	if (want_resched) {
148 		/*
149 		 * Since we are curproc, clock will normally just change
150 		 * our priority without moving us from one queue to another
151 		 * (since the running process is not on a queue.)
152 		 * If that happened after we setrunqueue ourselves but before we
153 		 * mi_switch()'ed, we might not be on the queue indicated by
154 		 * our priority.
155 		 */
156 		s = splhigh();
157 		setrunqueue(p);
158 		p->p_stats->p_ru.ru_nivcsw++;
159 		mi_switch();
160 		splx(s);
161 		while ((sig = CURSIG(p)) != 0)
162 			postsig(sig);
163 	}
164 	/*
165 	 * Charge system time if profiling.
166 	 */
167 	if (p->p_flag & P_PROFIL)
168 		addupc_task(p, frame->tf_eip,
169 			    (u_int)(p->p_sticks - oticks) * psratio);
170 
171 	curpriority = p->p_priority;
172 }
173 
174 /*
175  * Exception, fault, and trap interface to the FreeBSD kernel.
176  * This common code is called from assembly language IDT gate entry
177  * routines that prepare a suitable stack frame, and restore this
178  * frame after the exception has been processed.
179  */
180 
181 void
182 trap(frame)
183 	struct trapframe frame;
184 {
185 	struct proc *p = curproc;
186 	u_quad_t sticks = 0;
187 	int i = 0, ucode = 0, type, code;
188 #ifdef DEBUG
189 	u_long eva;
190 #endif
191 
192 	type = frame.tf_trapno;
193 	code = frame.tf_err;
194 
195 	if (ISPL(frame.tf_cs) == SEL_UPL) {
196 		/* user trap */
197 
198 		sticks = p->p_sticks;
199 		p->p_md.md_regs = (int *)&frame;
200 
201 		switch (type) {
202 		case T_PRIVINFLT:	/* privileged instruction fault */
203 			ucode = type;
204 			i = SIGILL;
205 			break;
206 
207 		case T_BPTFLT:		/* bpt instruction fault */
208 		case T_TRCTRAP:		/* trace trap */
209 			frame.tf_eflags &= ~PSL_T;
210 			i = SIGTRAP;
211 			break;
212 
213 		case T_ARITHTRAP:	/* arithmetic trap */
214 			ucode = code;
215 			i = SIGFPE;
216 			break;
217 
218 		case T_ASTFLT:		/* Allow process switch */
219 			astoff();
220 			cnt.v_soft++;
221 			if (p->p_flag & P_OWEUPC) {
222 				p->p_flag &= ~P_OWEUPC;
223 				addupc_task(p, p->p_stats->p_prof.pr_addr,
224 					    p->p_stats->p_prof.pr_ticks);
225 			}
226 			goto out;
227 
228 		case T_PROTFLT:		/* general protection fault */
229 		case T_SEGNPFLT:	/* segment not present fault */
230 		case T_STKFLT:		/* stack fault */
231 		case T_TSSFLT:		/* invalid TSS fault */
232 		case T_DOUBLEFLT:	/* double fault */
233 		default:
234 			ucode = code + BUS_SEGM_FAULT ;
235 			i = SIGBUS;
236 			break;
237 
238 		case T_PAGEFLT:		/* page fault */
239 			i = trap_pfault(&frame, TRUE);
240 			if (i == -1)
241 				return;
242 			if (i == 0)
243 				goto out;
244 
245 			ucode = T_PAGEFLT;
246 			break;
247 
248 		case T_DIVIDE:		/* integer divide fault */
249 			ucode = FPE_INTDIV_TRAP;
250 			i = SIGFPE;
251 			break;
252 
253 #if NISA > 0
254 		case T_NMI:
255 #ifdef POWERFAIL_NMI
256 			goto handle_powerfail;
257 #else /* !POWERFAIL_NMI */
258 #ifdef DDB
259 			/* NMI can be hooked up to a pushbutton for debugging */
260 			printf ("NMI ... going to debugger\n");
261 			if (kdb_trap (type, 0, &frame))
262 				return;
263 #endif /* DDB */
264 			/* machine/parity/power fail/"kitchen sink" faults */
265 			if (isa_nmi(code) == 0) return;
266 			panic("NMI indicates hardware failure");
267 #endif /* POWERFAIL_NMI */
268 #endif /* NISA > 0 */
269 
270 		case T_OFLOW:		/* integer overflow fault */
271 			ucode = FPE_INTOVF_TRAP;
272 			i = SIGFPE;
273 			break;
274 
275 		case T_BOUND:		/* bounds check fault */
276 			ucode = FPE_SUBRNG_TRAP;
277 			i = SIGFPE;
278 			break;
279 
280 		case T_DNA:
281 #if NNPX > 0
282 			/* if a transparent fault (due to context switch "late") */
283 			if (npxdna())
284 				return;
285 #endif
286 			if (!pmath_emulate) {
287 				i = SIGFPE;
288 				ucode = FPE_FPU_NP_TRAP;
289 				break;
290 			}
291 			i = (*pmath_emulate)(&frame);
292 			if (i == 0) {
293 				if (!(frame.tf_eflags & PSL_T))
294 					return;
295 				frame.tf_eflags &= ~PSL_T;
296 				i = SIGTRAP;
297 			}
298 			/* else ucode = emulator_only_knows() XXX */
299 			break;
300 
301 		case T_FPOPFLT:		/* FPU operand fetch fault */
302 			ucode = T_FPOPFLT;
303 			i = SIGILL;
304 			break;
305 		}
306 	} else {
307 		/* kernel trap */
308 
309 		switch (type) {
310 		case T_PAGEFLT:			/* page fault */
311 			(void) trap_pfault(&frame, FALSE);
312 			return;
313 
314 		case T_DNA:
315 #if NNPX > 0
316 			/*
317 			 * The kernel is apparently using npx for copying.
318 			 * XXX this should be fatal unless the kernel has
319 			 * registered such use.
320 			 */
321 			if (npxdna())
322 				return;
323 #endif
324 			break;
325 
326 		case T_PROTFLT:		/* general protection fault */
327 		case T_SEGNPFLT:	/* segment not present fault */
328 			/*
329 			 * Invalid segment selectors and out of bounds
330 			 * %eip's and %esp's can be set up in user mode.
331 			 * This causes a fault in kernel mode when the
332 			 * kernel tries to return to user mode.  We want
333 			 * to get this fault so that we can fix the
334 			 * problem here and not have to check all the
335 			 * selectors and pointers when the user changes
336 			 * them.
337 			 */
338 #define	MAYBE_DORETI_FAULT(where, whereto)				\
339 	do {								\
340 		if (frame.tf_eip == (int)where) {			\
341 			frame.tf_eip = (int)whereto;			\
342 			return;						\
343 		}							\
344 	} while (0)
345 
346 			if (intr_nesting_level == 0) {
347 				MAYBE_DORETI_FAULT(doreti_iret,
348 						   doreti_iret_fault);
349 				MAYBE_DORETI_FAULT(doreti_popl_ds,
350 						   doreti_popl_ds_fault);
351 				MAYBE_DORETI_FAULT(doreti_popl_es,
352 						   doreti_popl_es_fault);
353 			}
354 			if (curpcb && curpcb->pcb_onfault) {
355 				frame.tf_eip = (int)curpcb->pcb_onfault;
356 				return;
357 			}
358 			break;
359 
360 		case T_TSSFLT:
361 			/*
362 			 * PSL_NT can be set in user mode and isn't cleared
363 			 * automatically when the kernel is entered.  This
364 			 * causes a TSS fault when the kernel attempts to
365 			 * `iret' because the TSS link is uninitialized.  We
366 			 * want to get this fault so that we can fix the
367 			 * problem here and not every time the kernel is
368 			 * entered.
369 			 */
370 			if (frame.tf_eflags & PSL_NT) {
371 				frame.tf_eflags &= ~PSL_NT;
372 				return;
373 			}
374 			break;
375 
376 		case T_TRCTRAP:	 /* trace trap */
377 			if (frame.tf_eip == (int)IDTVEC(syscall)) {
378 				/*
379 				 * We've just entered system mode via the
380 				 * syscall lcall.  Continue single stepping
381 				 * silently until the syscall handler has
382 				 * saved the flags.
383 				 */
384 				return;
385 			}
386 			if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
387 				/*
388 				 * The syscall handler has now saved the
389 				 * flags.  Stop single stepping it.
390 				 */
391 				frame.tf_eflags &= ~PSL_T;
392 				return;
393 			}
394 			/*
395 			 * Fall through.
396 			 */
397 		case T_BPTFLT:
398 			/*
399 			 * If DDB is enabled, let it handle the debugger trap.
400 			 * Otherwise, debugger traps "can't happen".
401 			 */
402 #ifdef DDB
403 			if (kdb_trap (type, 0, &frame))
404 				return;
405 #endif
406 			break;
407 
408 #if NISA > 0
409 		case T_NMI:
410 #ifdef POWERFAIL_NMI
411 #ifndef TIMER_FREQ
412 #  define TIMER_FREQ 1193182
413 #endif
414 	handle_powerfail:
415 		{
416 		  static unsigned lastalert = 0;
417 
418 		  if(time.tv_sec - lastalert > 10)
419 		    {
420 		      log(LOG_WARNING, "NMI: power fail\n");
421 		      sysbeep(TIMER_FREQ/880, hz);
422 		      lastalert = time.tv_sec;
423 		    }
424 		  return;
425 		}
426 #else /* !POWERFAIL_NMI */
427 #ifdef DDB
428 			/* NMI can be hooked up to a pushbutton for debugging */
429 			printf ("NMI ... going to debugger\n");
430 			if (kdb_trap (type, 0, &frame))
431 				return;
432 #endif /* DDB */
433 			/* machine/parity/power fail/"kitchen sink" faults */
434 			if (isa_nmi(code) == 0) return;
435 			/* FALL THROUGH */
436 #endif /* POWERFAIL_NMI */
437 #endif /* NISA > 0 */
438 		}
439 
440 		trap_fatal(&frame);
441 		return;
442 	}
443 
444 	trapsignal(p, i, ucode);
445 
446 #ifdef DEBUG
447 	eva = rcr2();
448 	if (type <= MAX_TRAP_MSG) {
449 		uprintf("fatal process exception: %s",
450 			trap_msg[type]);
451 		if ((type == T_PAGEFLT) || (type == T_PROTFLT))
452 			uprintf(", fault VA = 0x%x", eva);
453 		uprintf("\n");
454 	}
455 #endif
456 
457 out:
458 	userret(p, &frame, sticks);
459 }
460 
461 #ifdef notyet
462 /*
463  * This version doesn't allow a page fault to user space while
464  * in the kernel. The rest of the kernel needs to be made "safe"
465  * before this can be used. I think the only things remaining
466  * to be made safe are the iBCS2 code and the process tracing/
467  * debugging code.
468  */
469 static int
470 trap_pfault(frame, usermode)
471 	struct trapframe *frame;
472 	int usermode;
473 {
474 	vm_offset_t va;
475 	struct vmspace *vm = NULL;
476 	vm_map_t map = 0;
477 	int rv = 0;
478 	vm_prot_t ftype;
479 	int eva;
480 	struct proc *p = curproc;
481 
482 	if (frame->tf_err & PGEX_W)
483 		ftype = VM_PROT_READ | VM_PROT_WRITE;
484 	else
485 		ftype = VM_PROT_READ;
486 
487 	eva = rcr2();
488 	va = trunc_page((vm_offset_t)eva);
489 
490 	if (va < VM_MIN_KERNEL_ADDRESS) {
491 		vm_offset_t v;
492 		vm_page_t mpte;
493 
494 		if (p == NULL ||
495 		    (!usermode && va < VM_MAXUSER_ADDRESS &&
496 		    (curpcb == NULL || curpcb->pcb_onfault == NULL))) {
497 			trap_fatal(frame);
498 			return (-1);
499 		}
500 
501 		/*
502 		 * This is a fault on non-kernel virtual memory.
503 		 * vm is initialized above to NULL. If curproc is NULL
504 		 * or curproc->p_vmspace is NULL the fault is fatal.
505 		 */
506 		vm = p->p_vmspace;
507 		if (vm == NULL)
508 			goto nogo;
509 
510 		map = &vm->vm_map;
511 
512 		/*
513 		 * Keep swapout from messing with us during this
514 		 *	critical time.
515 		 */
516 		++p->p_lock;
517 
518 		/*
519 		 * Grow the stack if necessary
520 		 */
521 		if ((caddr_t)va > vm->vm_maxsaddr
522 		    && (caddr_t)va < (caddr_t)USRSTACK) {
523 			if (!grow(p, va)) {
524 				rv = KERN_FAILURE;
525 				--p->p_lock;
526 				goto nogo;
527 			}
528 		}
529 
530 		/* Fault in the user page: */
531 		rv = vm_fault(map, va, ftype, FALSE);
532 
533 		--p->p_lock;
534 	} else {
535 		/*
536 		 * Don't allow user-mode faults in kernel address space.
537 		 */
538 		if (usermode)
539 			goto nogo;
540 
541 		/*
542 		 * Since we know that kernel virtual address addresses
543 		 * always have pte pages mapped, we just have to fault
544 		 * the page.
545 		 */
546 		rv = vm_fault(kernel_map, va, ftype, FALSE);
547 	}
548 
549 	if (rv == KERN_SUCCESS)
550 		return (0);
551 nogo:
552 	if (!usermode) {
553 		if (curpcb && curpcb->pcb_onfault) {
554 			frame->tf_eip = (int)curpcb->pcb_onfault;
555 			return (0);
556 		}
557 		trap_fatal(frame);
558 		return (-1);
559 	}
560 
561 	/* kludge to pass faulting virtual address to sendsig */
562 	frame->tf_err = eva;
563 
564 	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
565 }
566 #endif
567 
568 int
569 trap_pfault(frame, usermode)
570 	struct trapframe *frame;
571 	int usermode;
572 {
573 	vm_offset_t va;
574 	struct vmspace *vm = NULL;
575 	vm_map_t map = 0;
576 	int rv = 0;
577 	vm_prot_t ftype;
578 	int eva;
579 	struct proc *p = curproc;
580 
581 	eva = rcr2();
582 	va = trunc_page((vm_offset_t)eva);
583 
584 	if (va >= KERNBASE) {
585 		/*
586 		 * Don't allow user-mode faults in kernel address space.
587 		 */
588 		if (usermode)
589 			goto nogo;
590 
591 		map = kernel_map;
592 	} else {
593 		/*
594 		 * This is a fault on non-kernel virtual memory.
595 		 * vm is initialized above to NULL. If curproc is NULL
596 		 * or curproc->p_vmspace is NULL the fault is fatal.
597 		 */
598 		if (p != NULL)
599 			vm = p->p_vmspace;
600 
601 		if (vm == NULL)
602 			goto nogo;
603 
604 		map = &vm->vm_map;
605 	}
606 
607 	if (frame->tf_err & PGEX_W)
608 		ftype = VM_PROT_READ | VM_PROT_WRITE;
609 	else
610 		ftype = VM_PROT_READ;
611 
612 	if (map != kernel_map) {
613 		/*
614 		 * Keep swapout from messing with us during this
615 		 *	critical time.
616 		 */
617 		++p->p_lock;
618 
619 		/*
620 		 * Grow the stack if necessary
621 		 */
622 		if ((caddr_t)va > vm->vm_maxsaddr
623 		    && (caddr_t)va < (caddr_t)USRSTACK) {
624 			if (!grow(p, va)) {
625 				rv = KERN_FAILURE;
626 				--p->p_lock;
627 				goto nogo;
628 			}
629 		}
630 
631 		/* Fault in the user page: */
632 		rv = vm_fault(map, va, ftype, FALSE);
633 
634 		--p->p_lock;
635 	} else {
636 		/*
637 		 * Since we know that kernel virtual address addresses
638 		 * always have pte pages mapped, we just have to fault
639 		 * the page.
640 		 */
641 		rv = vm_fault(map, va, ftype, FALSE);
642 	}
643 
644 	if (rv == KERN_SUCCESS)
645 		return (0);
646 nogo:
647 	if (!usermode) {
648 		if (curpcb && curpcb->pcb_onfault) {
649 			frame->tf_eip = (int)curpcb->pcb_onfault;
650 			return (0);
651 		}
652 		trap_fatal(frame);
653 		return (-1);
654 	}
655 
656 	/* kludge to pass faulting virtual address to sendsig */
657 	frame->tf_err = eva;
658 
659 	return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
660 }
661 
662 static void
663 trap_fatal(frame)
664 	struct trapframe *frame;
665 {
666 	int code, type, eva, ss, esp;
667 	struct soft_segment_descriptor softseg;
668 
669 	code = frame->tf_err;
670 	type = frame->tf_trapno;
671 	eva = rcr2();
672 	sdtossd(&gdt[IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
673 
674 	if (type <= MAX_TRAP_MSG)
675 		printf("\n\nFatal trap %d: %s while in %s mode\n",
676 			type, trap_msg[type],
677 			ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
678 	if (type == T_PAGEFLT) {
679 		printf("fault virtual address	= 0x%x\n", eva);
680 		printf("fault code		= %s %s, %s\n",
681 			code & PGEX_U ? "user" : "supervisor",
682 			code & PGEX_W ? "write" : "read",
683 			code & PGEX_P ? "protection violation" : "page not present");
684 	}
685 	printf("instruction pointer	= 0x%x:0x%x\n",
686 	       frame->tf_cs & 0xffff, frame->tf_eip);
687 	if (ISPL(frame->tf_cs) == SEL_UPL) {
688 		ss = frame->tf_ss & 0xffff;
689 		esp = frame->tf_esp;
690 	} else {
691 		ss = GSEL(GDATA_SEL, SEL_KPL);
692 		esp = (int)&frame->tf_esp;
693 	}
694 	printf("stack pointer	        = 0x%x:0x%x\n", ss, esp);
695 	printf("frame pointer	        = 0x%x:0x%x\n", ss, frame->tf_ebp);
696 	printf("code segment		= base 0x%x, limit 0x%x, type 0x%x\n",
697 	       softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
698 	printf("			= DPL %d, pres %d, def32 %d, gran %d\n",
699 	       softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
700 	       softseg.ssd_gran);
701 	printf("processor eflags	= ");
702 	if (frame->tf_eflags & PSL_T)
703 		printf("trace trap, ");
704 	if (frame->tf_eflags & PSL_I)
705 		printf("interrupt enabled, ");
706 	if (frame->tf_eflags & PSL_NT)
707 		printf("nested task, ");
708 	if (frame->tf_eflags & PSL_RF)
709 		printf("resume, ");
710 	if (frame->tf_eflags & PSL_VM)
711 		printf("vm86, ");
712 	printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
713 	printf("current process		= ");
714 	if (curproc) {
715 		printf("%lu (%s)\n",
716 		    (u_long)curproc->p_pid, curproc->p_comm ?
717 		    curproc->p_comm : "");
718 	} else {
719 		printf("Idle\n");
720 	}
721 	printf("interrupt mask		= ");
722 	if ((cpl & net_imask) == net_imask)
723 		printf("net ");
724 	if ((cpl & tty_imask) == tty_imask)
725 		printf("tty ");
726 	if ((cpl & bio_imask) == bio_imask)
727 		printf("bio ");
728 	if (cpl == 0)
729 		printf("none");
730 	printf("\n");
731 
732 #ifdef KDB
733 	if (kdb_trap(&psl))
734 		return;
735 #endif
736 #ifdef DDB
737 	if (kdb_trap (type, 0, frame))
738 		return;
739 #endif
740 	if (type <= MAX_TRAP_MSG)
741 		panic(trap_msg[type]);
742 	else
743 		panic("unknown/reserved trap");
744 }
745 
746 /*
747  * Double fault handler. Called when a fault occurs while writing
748  * a frame for a trap/exception onto the stack. This usually occurs
749  * when the stack overflows (such is the case with infinite recursion,
750  * for example).
751  *
752  * XXX Note that the current PTD gets replaced by IdlePTD when the
753  * task switch occurs. This means that the stack that was active at
754  * the time of the double fault is not available at <kstack> unless
755  * the machine was idle when the double fault occurred. The downside
756  * of this is that "trace <ebp>" in ddb won't work.
757  */
758 void
759 dblfault_handler()
760 {
761 	struct pcb *pcb = curpcb;
762 
763 	if (pcb != NULL) {
764 		printf("\nFatal double fault:\n");
765 		printf("eip = 0x%x\n", pcb->pcb_tss.tss_eip);
766 		printf("esp = 0x%x\n", pcb->pcb_tss.tss_esp);
767 		printf("ebp = 0x%x\n", pcb->pcb_tss.tss_ebp);
768 	}
769 
770 	panic("double fault");
771 }
772 
773 /*
774  * Compensate for 386 brain damage (missing URKR).
775  * This is a little simpler than the pagefault handler in trap() because
776  * it the page tables have already been faulted in and high addresses
777  * are thrown out early for other reasons.
778  */
779 int trapwrite(addr)
780 	unsigned addr;
781 {
782 	struct proc *p;
783 	vm_offset_t va, v;
784 	struct vmspace *vm;
785 	int rv;
786 
787 	va = trunc_page((vm_offset_t)addr);
788 	/*
789 	 * XXX - MAX is END.  Changed > to >= for temp. fix.
790 	 */
791 	if (va >= VM_MAXUSER_ADDRESS)
792 		return (1);
793 
794 	p = curproc;
795 	vm = p->p_vmspace;
796 
797 	++p->p_lock;
798 
799 	if ((caddr_t)va >= vm->vm_maxsaddr
800 	    && (caddr_t)va < (caddr_t)USRSTACK) {
801 		if (!grow(p, va)) {
802 			--p->p_lock;
803 			return (1);
804 		}
805 	}
806 
807 	v = trunc_page(vtopte(va));
808 
809 	/*
810 	 * fault the data page
811 	 */
812 	rv = vm_fault(&vm->vm_map, va, VM_PROT_READ|VM_PROT_WRITE, FALSE);
813 
814 	--p->p_lock;
815 
816 	if (rv != KERN_SUCCESS)
817 		return 1;
818 
819 	return (0);
820 }
821 
822 /*
823  * System call request from POSIX system call gate interface to kernel.
824  * Like trap(), argument is call by reference.
825  */
826 void
827 syscall(frame)
828 	struct trapframe frame;
829 {
830 	caddr_t params;
831 	int i;
832 	struct sysent *callp;
833 	struct proc *p = curproc;
834 	u_quad_t sticks;
835 	int error;
836 	int args[8], rval[2];
837 	u_int code;
838 
839 	sticks = p->p_sticks;
840 	if (ISPL(frame.tf_cs) != SEL_UPL)
841 		panic("syscall");
842 
843 	p->p_md.md_regs = (int *)&frame;
844 	params = (caddr_t)frame.tf_esp + sizeof(int);
845 	code = frame.tf_eax;
846 	if (p->p_sysent->sv_prepsyscall) {
847 		(*p->p_sysent->sv_prepsyscall)(&frame, args, &code, &params);
848 	} else {
849 		/*
850 		 * Need to check if this is a 32 bit or 64 bit syscall.
851 		 */
852 		if (code == SYS_syscall) {
853 			/*
854 			 * Code is first argument, followed by actual args.
855 			 */
856 			code = fuword(params);
857 			params += sizeof(int);
858 		} else if (code == SYS___syscall) {
859 			/*
860 			 * Like syscall, but code is a quad, so as to maintain
861 			 * quad alignment for the rest of the arguments.
862 			 */
863 			code = fuword(params);
864 			params += sizeof(quad_t);
865 		}
866 	}
867 
868  	if (p->p_sysent->sv_mask)
869  		code &= p->p_sysent->sv_mask;
870 
871  	if (code >= p->p_sysent->sv_size)
872  		callp = &p->p_sysent->sv_table[0];
873   	else
874  		callp = &p->p_sysent->sv_table[code];
875 
876 	if (params && (i = callp->sy_narg * sizeof(int)) &&
877 	    (error = copyin(params, (caddr_t)args, (u_int)i))) {
878 #ifdef KTRACE
879 		if (KTRPOINT(p, KTR_SYSCALL))
880 			ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
881 #endif
882 		goto bad;
883 	}
884 #ifdef KTRACE
885 	if (KTRPOINT(p, KTR_SYSCALL))
886 		ktrsyscall(p->p_tracep, code, callp->sy_narg, args);
887 #endif
888 	rval[0] = 0;
889 	rval[1] = frame.tf_edx;
890 
891 	error = (*callp->sy_call)(p, args, rval);
892 
893 	switch (error) {
894 
895 	case 0:
896 		/*
897 		 * Reinitialize proc pointer `p' as it may be different
898 		 * if this is a child returning from fork syscall.
899 		 */
900 		p = curproc;
901 		frame.tf_eax = rval[0];
902 		frame.tf_edx = rval[1];
903 		frame.tf_eflags &= ~PSL_C;
904 		break;
905 
906 	case ERESTART:
907 		/*
908 		 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
909 		 * int 0x80 is 2 bytes. We saved this in tf_err.
910 		 */
911 		frame.tf_eip -= frame.tf_err;
912 		break;
913 
914 	case EJUSTRETURN:
915 		break;
916 
917 	default:
918 bad:
919  		if (p->p_sysent->sv_errsize)
920  			if (error >= p->p_sysent->sv_errsize)
921   				error = -1;	/* XXX */
922    			else
923   				error = p->p_sysent->sv_errtbl[error];
924 		frame.tf_eax = error;
925 		frame.tf_eflags |= PSL_C;
926 		break;
927 	}
928 
929 	if (frame.tf_eflags & PSL_T) {
930 		/* Traced syscall. */
931 		frame.tf_eflags &= ~PSL_T;
932 		trapsignal(p, SIGTRAP, 0);
933 	}
934 
935 	userret(p, &frame, sticks);
936 
937 #ifdef KTRACE
938 	if (KTRPOINT(p, KTR_SYSRET))
939 		ktrsysret(p->p_tracep, code, error, rval[0]);
940 #endif
941 }
942