Rename functions to avoid conflicts with libc.
[dragonfly.git] / sys / i386 / i386 / trap.c
CommitLineData
984263bc
MD
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 * $FreeBSD: src/sys/i386/i386/trap.c,v 1.147.2.11 2003/02/27 19:09:59 luoqi Exp $
84204577 39 * $DragonFly: src/sys/i386/i386/Attic/trap.c,v 1.79 2006/09/03 18:29:16 dillon Exp $
984263bc
MD
40 */
41
42/*
43 * 386 Trap and System call handling
44 */
45
1f2de5d4
MD
46#include "use_isa.h"
47#include "use_npx.h"
48
984263bc
MD
49#include "opt_cpu.h"
50#include "opt_ddb.h"
51#include "opt_ktrace.h"
52#include "opt_clock.h"
53#include "opt_trap.h"
54
55#include <sys/param.h>
56#include <sys/systm.h>
57#include <sys/proc.h>
58#include <sys/pioctl.h>
59#include <sys/kernel.h>
60#include <sys/resourcevar.h>
61#include <sys/signalvar.h>
62#include <sys/syscall.h>
63#include <sys/sysctl.h>
64#include <sys/sysent.h>
65#include <sys/uio.h>
66#include <sys/vmmeter.h>
4fd10eb6 67#include <sys/malloc.h>
984263bc
MD
68#ifdef KTRACE
69#include <sys/ktrace.h>
70#endif
a722be49 71#include <sys/upcall.h>
a64ba182
MD
72#include <sys/sysproto.h>
73#include <sys/sysunion.h>
984263bc
MD
74
75#include <vm/vm.h>
76#include <vm/vm_param.h>
77#include <sys/lock.h>
78#include <vm/pmap.h>
79#include <vm/vm_kern.h>
80#include <vm/vm_map.h>
81#include <vm/vm_page.h>
82#include <vm/vm_extern.h>
83
84#include <machine/cpu.h>
85#include <machine/ipl.h>
86#include <machine/md_var.h>
87#include <machine/pcb.h>
984263bc 88#include <machine/smp.h>
984263bc 89#include <machine/tss.h>
85100692 90#include <machine/globaldata.h>
984263bc
MD
91
92#include <i386/isa/intr_machdep.h>
93
94#ifdef POWERFAIL_NMI
95#include <sys/syslog.h>
96#include <machine/clock.h>
97#endif
98
99#include <machine/vm86.h>
100
101#include <ddb/ddb.h>
245e4f17 102#include <sys/msgport2.h>
41c20dac 103#include <sys/thread2.h>
984263bc 104
9753ab6c
MD
105#ifdef SMP
106
107#define MAKEMPSAFE(have_mplock) \
108 if (have_mplock == 0) { \
109 get_mplock(); \
110 have_mplock = 1; \
111 }
112
113#else
114
115#define MAKEMPSAFE(have_mplock)
116
117#endif
118
3ae0cd58 119int (*pmath_emulate) (struct trapframe *);
984263bc 120
3ae0cd58
RG
121extern void trap (struct trapframe frame);
122extern int trapwrite (unsigned addr);
123extern void syscall2 (struct trapframe frame);
984263bc 124
3ae0cd58
RG
125static int trap_pfault (struct trapframe *, int, vm_offset_t);
126static void trap_fatal (struct trapframe *, vm_offset_t);
127void dblfault_handler (void);
984263bc
MD
128
129extern inthand_t IDTVEC(syscall);
130
131#define MAX_TRAP_MSG 28
132static char *trap_msg[] = {
133 "", /* 0 unused */
134 "privileged instruction fault", /* 1 T_PRIVINFLT */
135 "", /* 2 unused */
136 "breakpoint instruction fault", /* 3 T_BPTFLT */
137 "", /* 4 unused */
138 "", /* 5 unused */
139 "arithmetic trap", /* 6 T_ARITHTRAP */
140 "system forced exception", /* 7 T_ASTFLT */
141 "", /* 8 unused */
142 "general protection fault", /* 9 T_PROTFLT */
143 "trace trap", /* 10 T_TRCTRAP */
144 "", /* 11 unused */
145 "page fault", /* 12 T_PAGEFLT */
146 "", /* 13 unused */
147 "alignment fault", /* 14 T_ALIGNFLT */
148 "", /* 15 unused */
149 "", /* 16 unused */
150 "", /* 17 unused */
151 "integer divide fault", /* 18 T_DIVIDE */
152 "non-maskable interrupt trap", /* 19 T_NMI */
153 "overflow trap", /* 20 T_OFLOW */
154 "FPU bounds check fault", /* 21 T_BOUND */
155 "FPU device not available", /* 22 T_DNA */
156 "double fault", /* 23 T_DOUBLEFLT */
157 "FPU operand fetch fault", /* 24 T_FPOPFLT */
158 "invalid TSS fault", /* 25 T_TSSFLT */
159 "segment not present fault", /* 26 T_SEGNPFLT */
160 "stack fault", /* 27 T_STKFLT */
161 "machine check trap", /* 28 T_MCHK */
162};
163
984263bc
MD
164#if defined(I586_CPU) && !defined(NO_F00F_HACK)
165extern int has_f00f_bug;
166#endif
167
168#ifdef DDB
169static int ddb_on_nmi = 1;
170SYSCTL_INT(_machdep, OID_AUTO, ddb_on_nmi, CTLFLAG_RW,
171 &ddb_on_nmi, 0, "Go to DDB on NMI");
172#endif
173static int panic_on_nmi = 1;
174SYSCTL_INT(_machdep, OID_AUTO, panic_on_nmi, CTLFLAG_RW,
175 &panic_on_nmi, 0, "Panic on NMI");
d9eea1a5
MD
176static int fast_release;
177SYSCTL_INT(_machdep, OID_AUTO, fast_release, CTLFLAG_RW,
178 &fast_release, 0, "Passive Release was optimal");
179static int slow_release;
180SYSCTL_INT(_machdep, OID_AUTO, slow_release, CTLFLAG_RW,
181 &slow_release, 0, "Passive Release was nonoptimal");
0e6beaa3 182#ifdef SMP
270ac911
MD
183static int syscall_mpsafe = 0;
184SYSCTL_INT(_kern, OID_AUTO, syscall_mpsafe, CTLFLAG_RW,
185 &syscall_mpsafe, 0, "Allow MPSAFE marked syscalls to run without BGL");
186TUNABLE_INT("kern.syscall_mpsafe", &syscall_mpsafe);
9753ab6c
MD
187static int trap_mpsafe = 0;
188SYSCTL_INT(_kern, OID_AUTO, trap_mpsafe, CTLFLAG_RW,
189 &trap_mpsafe, 0, "Allow traps to mostly run without the BGL");
190TUNABLE_INT("kern.trap_mpsafe", &trap_mpsafe);
0e6beaa3 191#endif
984263bc 192
4fd10eb6 193MALLOC_DEFINE(M_SYSMSG, "sysmsg", "sysmsg structure");
7062f5b4 194extern int max_sysmsg;
4fd10eb6 195
26a0694b 196/*
8ec60c3f
MD
197 * Passive USER->KERNEL transition. This only occurs if we block in the
198 * kernel while still holding our userland priority. We have to fixup our
199 * priority in order to avoid potential deadlocks before we allow the system
200 * to switch us to another thread.
26a0694b
MD
201 */
202static void
a2a5ad0d 203passive_release(struct thread *td)
26a0694b 204{
553ea3c8 205 struct lwp *lp = td->td_lwp;
26a0694b 206
0b0ee71f 207 td->td_release = NULL;
8ec60c3f 208 lwkt_setpri_self(TDPRI_KERN_USER);
553ea3c8 209 lp->lwp_proc->p_usched->release_curproc(lp);
26a0694b
MD
210}
211
212/*
d81ccc3e
MD
213 * userenter() passively intercepts the thread switch function to increase
214 * the thread priority from a user priority to a kernel priority, reducing
215 * syscall and trap overhead for the case where no switch occurs.
26a0694b 216 */
efd3c4c3 217
26a0694b 218static __inline void
7966cb69 219userenter(struct thread *curtd)
26a0694b 220{
7966cb69 221 curtd->td_release = passive_release;
26a0694b
MD
222}
223
0a3f9b47 224/*
8ec60c3f
MD
225 * Handle signals, upcalls, profiling, and other AST's and/or tasks that
226 * must be completed before we can return to or try to return to userland.
0a3f9b47 227 *
8ec60c3f
MD
228 * Note that td_sticks is a 64 bit quantity, but there's no point doing 64
229 * arithmatic on the delta calculation so the absolute tick values are
230 * truncated to an integer.
37af14fe 231 */
a2a5ad0d 232static void
81eea9f4 233userret(struct lwp *lp, struct trapframe *frame, int sticks)
a2a5ad0d 234{
81eea9f4 235 struct proc *p = lp->lwp_proc;
a2a5ad0d
MD
236 int sig;
237
344ad853
MD
238 /*
239 * Charge system time if profiling. Note: times are in microseconds.
240 * This may do a copyout and block, so do it first even though it
241 * means some system time will be charged as user time.
242 */
243 if (p->p_flag & P_PROFIL) {
244 addupc_task(p, frame->tf_eip,
245 (u_int)((int)p->p_thread->td_sticks - sticks));
246 }
247
248recheck:
249 /*
250 * Block here if we are in a stopped state.
251 */
252 if (p->p_flag & P_STOPPED) {
144ce500 253 get_mplock();
344ad853 254 tstop(p);
144ce500 255 rel_mplock();
344ad853
MD
256 goto recheck;
257 }
258
a722be49
MD
259 /*
260 * Post any pending upcalls
261 */
262 if (p->p_flag & P_UPCALLPEND) {
263 p->p_flag &= ~P_UPCALLPEND;
144ce500 264 get_mplock();
81eea9f4 265 postupcall(lp);
144ce500 266 rel_mplock();
344ad853 267 goto recheck;
a722be49
MD
268 }
269
a2a5ad0d
MD
270 /*
271 * Post any pending signals
272 */
344ad853 273 if ((sig = CURSIG(p)) != 0) {
144ce500 274 get_mplock();
a2a5ad0d 275 postsig(sig);
144ce500 276 rel_mplock();
344ad853 277 goto recheck;
a2a5ad0d 278 }
26a0694b 279
984263bc 280 /*
344ad853
MD
281 * block here if we are swapped out, but still process signals
282 * (such as SIGKILL). proc0 (the swapin scheduler) is already
283 * aware of our situation, we do not have to wake it up.
984263bc 284 */
344ad853 285 if (p->p_flag & P_SWAPPEDOUT) {
144ce500 286 get_mplock();
344ad853
MD
287 p->p_flag |= P_SWAPWAIT;
288 swapin_request();
289 if (p->p_flag & P_SWAPWAIT)
290 tsleep(p, PCATCH, "SWOUT", 0);
291 p->p_flag &= ~P_SWAPWAIT;
144ce500 292 rel_mplock();
344ad853 293 goto recheck;
984263bc 294 }
984263bc
MD
295}
296
8ec60c3f
MD
297/*
298 * Cleanup from userenter and any passive release that might have occured.
299 * We must reclaim the current-process designation before we can return
300 * to usermode. We also handle both LWKT and USER reschedule requests.
301 */
302static __inline void
81eea9f4 303userexit(struct lwp *lp)
8ec60c3f 304{
81eea9f4 305 struct thread *td = lp->lwp_thread;
8ec60c3f
MD
306 globaldata_t gd = td->td_gd;
307
308#if 0
309 /*
310 * If a user reschedule is requested force a new process to be
311 * chosen by releasing the current process. Our process will only
312 * be chosen again if it has a considerably better priority.
313 */
314 if (user_resched_wanted())
553ea3c8 315 lp->lwp_proc->p_usched->release_curproc(lp);
8ec60c3f
MD
316#endif
317
8ec60c3f
MD
318 /*
319 * Handle a LWKT reschedule request first. Since our passive release
320 * is still in place we do not have to do anything special.
321 */
322 if (lwkt_resched_wanted())
323 lwkt_switch();
324
325 /*
52eedfb5
MD
326 * Acquire the current process designation for this user scheduler
327 * on this cpu. This will also handle any user-reschedule requests.
8ec60c3f 328 */
52eedfb5
MD
329 lp->lwp_proc->p_usched->acquire_curproc(lp);
330 /* We may have switched cpus on acquisition */
331 gd = td->td_gd;
8ec60c3f
MD
332
333 /*
334 * Reduce our priority in preparation for a return to userland. If
335 * our passive release function was still in place, our priority was
336 * never raised and does not need to be reduced.
337 */
338 if (td->td_release == NULL)
339 lwkt_setpri_self(TDPRI_USER_NORM);
340 td->td_release = NULL;
341
342 /*
343 * After reducing our priority there might be other kernel-level
344 * LWKTs that now have a greater priority. Run them as necessary.
345 * We don't have to worry about losing cpu to userland because
346 * we still control the current-process designation and we no longer
347 * have a passive release function installed.
348 */
349 if (lwkt_checkpri_self())
350 lwkt_switch();
8ec60c3f
MD
351}
352
984263bc 353/*
7517cb03 354 * Exception, fault, and trap interface to the kernel.
984263bc
MD
355 * This common code is called from assembly language IDT gate entry
356 * routines that prepare a suitable stack frame, and restore this
357 * frame after the exception has been processed.
a2a5ad0d
MD
358 *
359 * This function is also called from doreti in an interlock to handle ASTs.
360 * For example: hardwareint->INTROUTINE->(set ast)->doreti->trap
361 *
362 * NOTE! We have to retrieve the fault address prior to obtaining the
363 * MP lock because get_mplock() may switch out. YYY cr2 really ought
364 * to be retrieved by the assembly code, not here.
27e88a6e
MD
365 *
366 * XXX gd_trap_nesting_level currently prevents lwkt_switch() from panicing
367 * if an attempt is made to switch from a fast interrupt or IPI. This is
368 * necessary to properly take fatal kernel traps on SMP machines if
369 * get_mplock() has to block.
984263bc 370 */
9753ab6c 371
984263bc
MD
372void
373trap(frame)
374 struct trapframe frame;
375{
27e88a6e
MD
376 struct globaldata *gd = mycpu;
377 struct thread *td = gd->gd_curthread;
81eea9f4 378 struct lwp *lp = td->td_lwp;
7966cb69 379 struct proc *p;
37af14fe 380 int sticks = 0;
984263bc 381 int i = 0, ucode = 0, type, code;
9753ab6c
MD
382#ifdef SMP
383 int have_mplock = 0;
7527bc81
MD
384#endif
385#ifdef INVARIANTS
386 int crit_count = td->td_pri & ~TDPRI_MASK;
9753ab6c 387#endif
984263bc
MD
388 vm_offset_t eva;
389
7966cb69 390 p = td->td_proc;
984263bc
MD
391#ifdef DDB
392 if (db_active) {
393 eva = (frame.tf_trapno == T_PAGEFLT ? rcr2() : 0);
27e88a6e 394 ++gd->gd_trap_nesting_level;
9753ab6c 395 MAKEMPSAFE(have_mplock);
984263bc 396 trap_fatal(&frame, eva);
27e88a6e 397 --gd->gd_trap_nesting_level;
8a8d5d85 398 goto out2;
984263bc
MD
399 }
400#endif
401
a2a5ad0d 402 eva = 0;
27e88a6e 403 ++gd->gd_trap_nesting_level;
a2a5ad0d
MD
404 if (frame.tf_trapno == T_PAGEFLT) {
405 /*
406 * For some Cyrix CPUs, %cr2 is clobbered by interrupts.
407 * This problem is worked around by using an interrupt
408 * gate for the pagefault handler. We are finally ready
409 * to read %cr2 and then must reenable interrupts.
410 *
411 * XXX this should be in the switch statement, but the
412 * NO_FOOF_HACK and VM86 goto and ifdefs obfuscate the
413 * flow of control too much for this to be obviously
414 * correct.
415 */
416 eva = rcr2();
a2a5ad0d 417 cpu_enable_intr();
a2a5ad0d 418 }
9753ab6c
MD
419#ifdef SMP
420 if (trap_mpsafe == 0)
421 MAKEMPSAFE(have_mplock);
422#endif
423
27e88a6e 424 --gd->gd_trap_nesting_level;
a2a5ad0d 425
984263bc
MD
426 if (!(frame.tf_eflags & PSL_I)) {
427 /*
428 * Buggy application or kernel code has disabled interrupts
429 * and then trapped. Enabling interrupts now is wrong, but
430 * it is better than running with interrupts disabled until
431 * they are accidentally enabled later.
432 */
433 type = frame.tf_trapno;
a2a5ad0d 434 if (ISPL(frame.tf_cs)==SEL_UPL || (frame.tf_eflags & PSL_VM)) {
9753ab6c 435 MAKEMPSAFE(have_mplock);
984263bc
MD
436 printf(
437 "pid %ld (%s): trap %d with interrupts disabled\n",
438 (long)curproc->p_pid, curproc->p_comm, type);
a2a5ad0d 439 } else if (type != T_BPTFLT && type != T_TRCTRAP) {
984263bc
MD
440 /*
441 * XXX not quite right, since this may be for a
442 * multiple fault in user mode.
443 */
9753ab6c 444 MAKEMPSAFE(have_mplock);
984263bc
MD
445 printf("kernel trap %d with interrupts disabled\n",
446 type);
a2a5ad0d 447 }
8a8d5d85 448 cpu_enable_intr();
984263bc
MD
449 }
450
984263bc
MD
451#if defined(I586_CPU) && !defined(NO_F00F_HACK)
452restart:
453#endif
454 type = frame.tf_trapno;
455 code = frame.tf_err;
456
457 if (in_vm86call) {
9753ab6c 458 ASSERT_MP_LOCK_HELD(curthread);
984263bc
MD
459 if (frame.tf_eflags & PSL_VM &&
460 (type == T_PROTFLT || type == T_STKFLT)) {
96728c05 461#ifdef SMP
37af14fe 462 KKASSERT(td->td_mpcount > 0);
96728c05 463#endif
984263bc 464 i = vm86_emulate((struct vm86frame *)&frame);
96728c05 465#ifdef SMP
37af14fe 466 KKASSERT(td->td_mpcount > 0);
96728c05 467#endif
8a8d5d85 468 if (i != 0) {
984263bc
MD
469 /*
470 * returns to original process
471 */
d3d32139
MD
472#ifdef SMP
473 vm86_trap((struct vm86frame *)&frame,
474 have_mplock);
475#else
476 vm86_trap((struct vm86frame *)&frame, 0);
477#endif
478 KKASSERT(0); /* NOT REACHED */
8a8d5d85
MD
479 }
480 goto out2;
984263bc
MD
481 }
482 switch (type) {
483 /*
484 * these traps want either a process context, or
485 * assume a normal userspace trap.
486 */
487 case T_PROTFLT:
488 case T_SEGNPFLT:
489 trap_fatal(&frame, eva);
8a8d5d85 490 goto out2;
984263bc
MD
491 case T_TRCTRAP:
492 type = T_BPTFLT; /* kernel breakpoint */
493 /* FALL THROUGH */
494 }
495 goto kernel_trap; /* normal kernel trap handling */
496 }
497
498 if ((ISPL(frame.tf_cs) == SEL_UPL) || (frame.tf_eflags & PSL_VM)) {
499 /* user trap */
500
7966cb69 501 userenter(td);
26a0694b 502
37af14fe 503 sticks = (int)td->td_sticks;
81eea9f4 504 lp->lwp_md.md_regs = &frame;
984263bc
MD
505
506 switch (type) {
507 case T_PRIVINFLT: /* privileged instruction fault */
508 ucode = type;
509 i = SIGILL;
510 break;
511
512 case T_BPTFLT: /* bpt instruction fault */
513 case T_TRCTRAP: /* trace trap */
514 frame.tf_eflags &= ~PSL_T;
515 i = SIGTRAP;
516 break;
517
518 case T_ARITHTRAP: /* arithmetic trap */
519 ucode = code;
520 i = SIGFPE;
521 break;
522
523 case T_ASTFLT: /* Allow process switch */
12e4aaff 524 mycpu->gd_cnt.v_soft++;
235957ed
MD
525 if (mycpu->gd_reqflags & RQF_AST_OWEUPC) {
526 atomic_clear_int_nonlocked(&mycpu->gd_reqflags,
527 RQF_AST_OWEUPC);
93328593
SS
528 addupc_task(p, p->p_prof.pr_addr,
529 p->p_prof.pr_ticks);
984263bc
MD
530 }
531 goto out;
532
533 /*
534 * The following two traps can happen in
535 * vm86 mode, and, if so, we want to handle
536 * them specially.
537 */
538 case T_PROTFLT: /* general protection fault */
539 case T_STKFLT: /* stack fault */
540 if (frame.tf_eflags & PSL_VM) {
541 i = vm86_emulate((struct vm86frame *)&frame);
542 if (i == 0)
543 goto out;
544 break;
545 }
546 /* FALL THROUGH */
547
548 case T_SEGNPFLT: /* segment not present fault */
549 case T_TSSFLT: /* invalid TSS fault */
550 case T_DOUBLEFLT: /* double fault */
551 default:
552 ucode = code + BUS_SEGM_FAULT ;
553 i = SIGBUS;
554 break;
555
556 case T_PAGEFLT: /* page fault */
9753ab6c 557 MAKEMPSAFE(have_mplock);
984263bc
MD
558 i = trap_pfault(&frame, TRUE, eva);
559 if (i == -1)
d81ccc3e 560 goto out;
984263bc
MD
561#if defined(I586_CPU) && !defined(NO_F00F_HACK)
562 if (i == -2)
563 goto restart;
564#endif
565 if (i == 0)
566 goto out;
567
568 ucode = T_PAGEFLT;
569 break;
570
571 case T_DIVIDE: /* integer divide fault */
572 ucode = FPE_INTDIV;
573 i = SIGFPE;
574 break;
575
576#if NISA > 0
577 case T_NMI:
9753ab6c 578 MAKEMPSAFE(have_mplock);
984263bc
MD
579#ifdef POWERFAIL_NMI
580 goto handle_powerfail;
581#else /* !POWERFAIL_NMI */
582 /* machine/parity/power fail/"kitchen sink" faults */
583 if (isa_nmi(code) == 0) {
584#ifdef DDB
585 /*
586 * NMI can be hooked up to a pushbutton
587 * for debugging.
588 */
589 if (ddb_on_nmi) {
590 printf ("NMI ... going to debugger\n");
591 kdb_trap (type, 0, &frame);
592 }
593#endif /* DDB */
8a8d5d85 594 goto out2;
984263bc
MD
595 } else if (panic_on_nmi)
596 panic("NMI indicates hardware failure");
597 break;
598#endif /* POWERFAIL_NMI */
599#endif /* NISA > 0 */
600
601 case T_OFLOW: /* integer overflow fault */
602 ucode = FPE_INTOVF;
603 i = SIGFPE;
604 break;
605
606 case T_BOUND: /* bounds check fault */
607 ucode = FPE_FLTSUB;
608 i = SIGFPE;
609 break;
610
611 case T_DNA:
612#if NNPX > 0
a02705a9
MD
613 /*
614 * The kernel may have switched out the FP unit's
615 * state, causing the user process to take a fault
616 * when it tries to use the FP unit. Restore the
617 * state here
618 */
984263bc 619 if (npxdna())
d81ccc3e 620 goto out;
984263bc
MD
621#endif
622 if (!pmath_emulate) {
623 i = SIGFPE;
624 ucode = FPE_FPU_NP_TRAP;
625 break;
626 }
627 i = (*pmath_emulate)(&frame);
628 if (i == 0) {
629 if (!(frame.tf_eflags & PSL_T))
8a8d5d85 630 goto out2;
984263bc
MD
631 frame.tf_eflags &= ~PSL_T;
632 i = SIGTRAP;
633 }
634 /* else ucode = emulator_only_knows() XXX */
635 break;
636
637 case T_FPOPFLT: /* FPU operand fetch fault */
638 ucode = T_FPOPFLT;
639 i = SIGILL;
640 break;
641
642 case T_XMMFLT: /* SIMD floating-point exception */
643 ucode = 0; /* XXX */
644 i = SIGFPE;
645 break;
646 }
647 } else {
648kernel_trap:
649 /* kernel trap */
650
651 switch (type) {
652 case T_PAGEFLT: /* page fault */
9753ab6c 653 MAKEMPSAFE(have_mplock);
09e6e929 654 trap_pfault(&frame, FALSE, eva);
8a8d5d85 655 goto out2;
984263bc
MD
656
657 case T_DNA:
658#if NNPX > 0
659 /*
a02705a9
MD
660 * The kernel may be using npx for copying or other
661 * purposes.
984263bc
MD
662 */
663 if (npxdna())
8a8d5d85 664 goto out2;
984263bc
MD
665#endif
666 break;
667
668 case T_PROTFLT: /* general protection fault */
669 case T_SEGNPFLT: /* segment not present fault */
670 /*
671 * Invalid segment selectors and out of bounds
672 * %eip's and %esp's can be set up in user mode.
673 * This causes a fault in kernel mode when the
674 * kernel tries to return to user mode. We want
675 * to get this fault so that we can fix the
676 * problem here and not have to check all the
677 * selectors and pointers when the user changes
678 * them.
679 */
680#define MAYBE_DORETI_FAULT(where, whereto) \
681 do { \
682 if (frame.tf_eip == (int)where) { \
683 frame.tf_eip = (int)whereto; \
8a8d5d85 684 goto out2; \
984263bc
MD
685 } \
686 } while (0)
fe8c5e17
MD
687 /*
688 * Since we don't save %gs across an interrupt
689 * frame this check must occur outside the intr
690 * nesting level check.
691 */
692 if (frame.tf_eip == (int)cpu_switch_load_gs) {
37af14fe 693 td->td_pcb->pcb_gs = 0;
9753ab6c 694 MAKEMPSAFE(have_mplock);
84204577 695 ksignal(p, SIGBUS);
fe8c5e17
MD
696 goto out2;
697 }
ef0fdad1 698 if (mycpu->gd_intr_nesting_level == 0) {
984263bc
MD
699 /*
700 * Invalid %fs's and %gs's can be created using
701 * procfs or PT_SETREGS or by invalidating the
702 * underlying LDT entry. This causes a fault
703 * in kernel mode when the kernel attempts to
704 * switch contexts. Lose the bad context
705 * (XXX) so that we can continue, and generate
706 * a signal.
707 */
984263bc
MD
708 MAYBE_DORETI_FAULT(doreti_iret,
709 doreti_iret_fault);
710 MAYBE_DORETI_FAULT(doreti_popl_ds,
711 doreti_popl_ds_fault);
712 MAYBE_DORETI_FAULT(doreti_popl_es,
713 doreti_popl_es_fault);
714 MAYBE_DORETI_FAULT(doreti_popl_fs,
715 doreti_popl_fs_fault);
37af14fe
MD
716 if (td->td_pcb->pcb_onfault) {
717 frame.tf_eip =
718 (register_t)td->td_pcb->pcb_onfault;
8a8d5d85 719 goto out2;
984263bc
MD
720 }
721 }
722 break;
723
724 case T_TSSFLT:
725 /*
726 * PSL_NT can be set in user mode and isn't cleared
727 * automatically when the kernel is entered. This
728 * causes a TSS fault when the kernel attempts to
729 * `iret' because the TSS link is uninitialized. We
730 * want to get this fault so that we can fix the
731 * problem here and not every time the kernel is
732 * entered.
733 */
734 if (frame.tf_eflags & PSL_NT) {
735 frame.tf_eflags &= ~PSL_NT;
8a8d5d85 736 goto out2;
984263bc
MD
737 }
738 break;
739
740 case T_TRCTRAP: /* trace trap */
741 if (frame.tf_eip == (int)IDTVEC(syscall)) {
742 /*
743 * We've just entered system mode via the
744 * syscall lcall. Continue single stepping
745 * silently until the syscall handler has
746 * saved the flags.
747 */
8a8d5d85 748 goto out2;
984263bc
MD
749 }
750 if (frame.tf_eip == (int)IDTVEC(syscall) + 1) {
751 /*
752 * The syscall handler has now saved the
753 * flags. Stop single stepping it.
754 */
755 frame.tf_eflags &= ~PSL_T;
8a8d5d85 756 goto out2;
984263bc
MD
757 }
758 /*
759 * Ignore debug register trace traps due to
760 * accesses in the user's address space, which
761 * can happen under several conditions such as
762 * if a user sets a watchpoint on a buffer and
763 * then passes that buffer to a system call.
764 * We still want to get TRCTRAPS for addresses
765 * in kernel space because that is useful when
766 * debugging the kernel.
767 */
768 if (user_dbreg_trap()) {
769 /*
770 * Reset breakpoint bits because the
771 * processor doesn't
772 */
773 load_dr6(rdr6() & 0xfffffff0);
8a8d5d85 774 goto out2;
984263bc
MD
775 }
776 /*
777 * Fall through (TRCTRAP kernel mode, kernel address)
778 */
779 case T_BPTFLT:
780 /*
781 * If DDB is enabled, let it handle the debugger trap.
782 * Otherwise, debugger traps "can't happen".
783 */
784#ifdef DDB
9753ab6c 785 MAKEMPSAFE(have_mplock);
984263bc 786 if (kdb_trap (type, 0, &frame))
8a8d5d85 787 goto out2;
984263bc
MD
788#endif
789 break;
790
791#if NISA > 0
792 case T_NMI:
9753ab6c 793 MAKEMPSAFE(have_mplock);
984263bc
MD
794#ifdef POWERFAIL_NMI
795#ifndef TIMER_FREQ
796# define TIMER_FREQ 1193182
797#endif
798 handle_powerfail:
799 {
800 static unsigned lastalert = 0;
801
802 if(time_second - lastalert > 10)
803 {
804 log(LOG_WARNING, "NMI: power fail\n");
805 sysbeep(TIMER_FREQ/880, hz);
806 lastalert = time_second;
807 }
8a8d5d85
MD
808 /* YYY mp count */
809 goto out2;
984263bc
MD
810 }
811#else /* !POWERFAIL_NMI */
812 /* machine/parity/power fail/"kitchen sink" faults */
813 if (isa_nmi(code) == 0) {
814#ifdef DDB
815 /*
816 * NMI can be hooked up to a pushbutton
817 * for debugging.
818 */
819 if (ddb_on_nmi) {
820 printf ("NMI ... going to debugger\n");
821 kdb_trap (type, 0, &frame);
822 }
823#endif /* DDB */
8a8d5d85 824 goto out2;
984263bc 825 } else if (panic_on_nmi == 0)
8a8d5d85 826 goto out2;
984263bc
MD
827 /* FALL THROUGH */
828#endif /* POWERFAIL_NMI */
829#endif /* NISA > 0 */
830 }
831
9753ab6c 832 MAKEMPSAFE(have_mplock);
984263bc 833 trap_fatal(&frame, eva);
8a8d5d85 834 goto out2;
984263bc
MD
835 }
836
837 /* Translate fault for emulators (e.g. Linux) */
838 if (*p->p_sysent->sv_transtrap)
839 i = (*p->p_sysent->sv_transtrap)(i, type);
840
9753ab6c 841 MAKEMPSAFE(have_mplock);
984263bc
MD
842 trapsignal(p, i, ucode);
843
844#ifdef DEBUG
845 if (type <= MAX_TRAP_MSG) {
846 uprintf("fatal process exception: %s",
847 trap_msg[type]);
848 if ((type == T_PAGEFLT) || (type == T_PROTFLT))
849 uprintf(", fault VA = 0x%lx", (u_long)eva);
850 uprintf("\n");
851 }
852#endif
853
854out:
8a8d5d85
MD
855#ifdef SMP
856 if (ISPL(frame.tf_cs) == SEL_UPL)
9753ab6c 857 KASSERT(td->td_mpcount == have_mplock, ("badmpcount trap/end from %p", (void *)frame.tf_eip));
8a8d5d85 858#endif
81eea9f4
SS
859 userret(lp, &frame, sticks);
860 userexit(lp);
9753ab6c 861out2: ;
96728c05 862#ifdef SMP
9753ab6c
MD
863 if (have_mplock)
864 rel_mplock();
96728c05 865#endif
7527bc81
MD
866#ifdef INVARIANTS
867 KASSERT(crit_count == (td->td_pri & ~TDPRI_MASK),
868 ("syscall: critical section count mismatch! %d/%d",
869 crit_count / TDPRI_CRIT, td->td_pri / TDPRI_CRIT));
870#endif
984263bc
MD
871}
872
873#ifdef notyet
874/*
875 * This version doesn't allow a page fault to user space while
876 * in the kernel. The rest of the kernel needs to be made "safe"
877 * before this can be used. I think the only things remaining
d511d5d5 878 * to be made safe is the process tracing/debugging code.
984263bc
MD
879 */
880static int
881trap_pfault(frame, usermode, eva)
882 struct trapframe *frame;
883 int usermode;
884 vm_offset_t eva;
885{
886 vm_offset_t va;
887 struct vmspace *vm = NULL;
888 vm_map_t map = 0;
889 int rv = 0;
890 vm_prot_t ftype;
37af14fe
MD
891 thread_t td = curthread;
892 struct proc *p = td->td_proc; /* may be NULL */
984263bc
MD
893
894 if (frame->tf_err & PGEX_W)
895 ftype = VM_PROT_WRITE;
896 else
897 ftype = VM_PROT_READ;
898
899 va = trunc_page(eva);
900 if (va < VM_MIN_KERNEL_ADDRESS) {
901 vm_offset_t v;
902 vm_page_t mpte;
903
904 if (p == NULL ||
905 (!usermode && va < VM_MAXUSER_ADDRESS &&
37af14fe
MD
906 (td->td_gd->gd_intr_nesting_level != 0 ||
907 td->td_pcb->pcb_onfault == NULL))) {
984263bc
MD
908 trap_fatal(frame, eva);
909 return (-1);
910 }
911
912 /*
913 * This is a fault on non-kernel virtual memory.
914 * vm is initialized above to NULL. If curproc is NULL
915 * or curproc->p_vmspace is NULL the fault is fatal.
916 */
917 vm = p->p_vmspace;
918 if (vm == NULL)
919 goto nogo;
920
921 map = &vm->vm_map;
922
923 /*
924 * Keep swapout from messing with us during this
925 * critical time.
926 */
927 ++p->p_lock;
928
929 /*
930 * Grow the stack if necessary
931 */
932 /* grow_stack returns false only if va falls into
933 * a growable stack region and the stack growth
934 * fails. It returns true if va was not within
935 * a growable stack region, or if the stack
936 * growth succeeded.
937 */
938 if (!grow_stack (p, va)) {
939 rv = KERN_FAILURE;
940 --p->p_lock;
941 goto nogo;
942 }
943
944 /* Fault in the user page: */
945 rv = vm_fault(map, va, ftype,
946 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
947 : VM_FAULT_NORMAL);
948
949 --p->p_lock;
950 } else {
951 /*
952 * Don't allow user-mode faults in kernel address space.
953 */
954 if (usermode)
955 goto nogo;
956
957 /*
958 * Since we know that kernel virtual address addresses
959 * always have pte pages mapped, we just have to fault
960 * the page.
961 */
962 rv = vm_fault(kernel_map, va, ftype, VM_FAULT_NORMAL);
963 }
964
965 if (rv == KERN_SUCCESS)
966 return (0);
967nogo:
968 if (!usermode) {
37af14fe
MD
969 if (mtd->td_gd->gd_intr_nesting_level == 0 &&
970 td->td_pcb->pcb_onfault) {
971 frame->tf_eip = (register_t)td->td_pcb->pcb_onfault;
984263bc
MD
972 return (0);
973 }
974 trap_fatal(frame, eva);
975 return (-1);
976 }
977
978 /* kludge to pass faulting virtual address to sendsig */
979 frame->tf_err = eva;
980
981 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
982}
983#endif
984
985int
986trap_pfault(frame, usermode, eva)
987 struct trapframe *frame;
988 int usermode;
989 vm_offset_t eva;
990{
991 vm_offset_t va;
992 struct vmspace *vm = NULL;
993 vm_map_t map = 0;
994 int rv = 0;
995 vm_prot_t ftype;
37af14fe
MD
996 thread_t td = curthread;
997 struct proc *p = td->td_proc;
984263bc
MD
998
999 va = trunc_page(eva);
1000 if (va >= KERNBASE) {
1001 /*
1002 * Don't allow user-mode faults in kernel address space.
1003 * An exception: if the faulting address is the invalid
1004 * instruction entry in the IDT, then the Intel Pentium
1005 * F00F bug workaround was triggered, and we need to
1006 * treat it is as an illegal instruction, and not a page
1007 * fault.
1008 */
1009#if defined(I586_CPU) && !defined(NO_F00F_HACK)
1010 if ((eva == (unsigned int)&idt[6]) && has_f00f_bug) {
1011 frame->tf_trapno = T_PRIVINFLT;
1012 return -2;
1013 }
1014#endif
1015 if (usermode)
1016 goto nogo;
1017
1018 map = kernel_map;
1019 } else {
1020 /*
1021 * This is a fault on non-kernel virtual memory.
1022 * vm is initialized above to NULL. If curproc is NULL
1023 * or curproc->p_vmspace is NULL the fault is fatal.
1024 */
1025 if (p != NULL)
1026 vm = p->p_vmspace;
1027
1028 if (vm == NULL)
1029 goto nogo;
1030
1031 map = &vm->vm_map;
1032 }
1033
1034 if (frame->tf_err & PGEX_W)
1035 ftype = VM_PROT_WRITE;
1036 else
1037 ftype = VM_PROT_READ;
1038
1039 if (map != kernel_map) {
1040 /*
1041 * Keep swapout from messing with us during this
1042 * critical time.
1043 */
1044 ++p->p_lock;
1045
1046 /*
1047 * Grow the stack if necessary
1048 */
1049 /* grow_stack returns false only if va falls into
1050 * a growable stack region and the stack growth
1051 * fails. It returns true if va was not within
1052 * a growable stack region, or if the stack
1053 * growth succeeded.
1054 */
1055 if (!grow_stack (p, va)) {
1056 rv = KERN_FAILURE;
1057 --p->p_lock;
1058 goto nogo;
1059 }
1060
1061 /* Fault in the user page: */
1062 rv = vm_fault(map, va, ftype,
1063 (ftype & VM_PROT_WRITE) ? VM_FAULT_DIRTY
1064 : VM_FAULT_NORMAL);
1065
1066 --p->p_lock;
1067 } else {
1068 /*
1069 * Don't have to worry about process locking or stacks in the kernel.
1070 */
1071 rv = vm_fault(map, va, ftype, VM_FAULT_NORMAL);
1072 }
1073
1074 if (rv == KERN_SUCCESS)
1075 return (0);
1076nogo:
1077 if (!usermode) {
37af14fe
MD
1078 if (td->td_gd->gd_intr_nesting_level == 0 &&
1079 td->td_pcb->pcb_onfault) {
1080 frame->tf_eip = (register_t)td->td_pcb->pcb_onfault;
984263bc
MD
1081 return (0);
1082 }
1083 trap_fatal(frame, eva);
1084 return (-1);
1085 }
1086
1087 /* kludge to pass faulting virtual address to sendsig */
1088 frame->tf_err = eva;
1089
1090 return((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
1091}
1092
1093static void
1094trap_fatal(frame, eva)
1095 struct trapframe *frame;
1096 vm_offset_t eva;
1097{
1098 int code, type, ss, esp;
1099 struct soft_segment_descriptor softseg;
1100
1101 code = frame->tf_err;
1102 type = frame->tf_trapno;
3951a45f 1103 sdtossd(&gdt[mycpu->gd_cpuid * NGDT + IDXSEL(frame->tf_cs & 0xffff)].sd, &softseg);
984263bc
MD
1104
1105 if (type <= MAX_TRAP_MSG)
1106 printf("\n\nFatal trap %d: %s while in %s mode\n",
1107 type, trap_msg[type],
1108 frame->tf_eflags & PSL_VM ? "vm86" :
1109 ISPL(frame->tf_cs) == SEL_UPL ? "user" : "kernel");
1110#ifdef SMP
7a44d1cb 1111 /* three separate prints in case of a trap on an unmapped page */
984263bc 1112 printf("mp_lock = %08x; ", mp_lock);
8a8d5d85 1113 printf("cpuid = %d; ", mycpu->gd_cpuid);
984263bc
MD
1114 printf("lapic.id = %08x\n", lapic.id);
1115#endif
1116 if (type == T_PAGEFLT) {
1117 printf("fault virtual address = 0x%x\n", eva);
1118 printf("fault code = %s %s, %s\n",
1119 code & PGEX_U ? "user" : "supervisor",
1120 code & PGEX_W ? "write" : "read",
1121 code & PGEX_P ? "protection violation" : "page not present");
1122 }
1123 printf("instruction pointer = 0x%x:0x%x\n",
1124 frame->tf_cs & 0xffff, frame->tf_eip);
1125 if ((ISPL(frame->tf_cs) == SEL_UPL) || (frame->tf_eflags & PSL_VM)) {
1126 ss = frame->tf_ss & 0xffff;
1127 esp = frame->tf_esp;
1128 } else {
1129 ss = GSEL(GDATA_SEL, SEL_KPL);
1130 esp = (int)&frame->tf_esp;
1131 }
1132 printf("stack pointer = 0x%x:0x%x\n", ss, esp);
1133 printf("frame pointer = 0x%x:0x%x\n", ss, frame->tf_ebp);
1134 printf("code segment = base 0x%x, limit 0x%x, type 0x%x\n",
1135 softseg.ssd_base, softseg.ssd_limit, softseg.ssd_type);
1136 printf(" = DPL %d, pres %d, def32 %d, gran %d\n",
1137 softseg.ssd_dpl, softseg.ssd_p, softseg.ssd_def32,
1138 softseg.ssd_gran);
1139 printf("processor eflags = ");
1140 if (frame->tf_eflags & PSL_T)
1141 printf("trace trap, ");
1142 if (frame->tf_eflags & PSL_I)
1143 printf("interrupt enabled, ");
1144 if (frame->tf_eflags & PSL_NT)
1145 printf("nested task, ");
1146 if (frame->tf_eflags & PSL_RF)
1147 printf("resume, ");
1148 if (frame->tf_eflags & PSL_VM)
1149 printf("vm86, ");
1150 printf("IOPL = %d\n", (frame->tf_eflags & PSL_IOPL) >> 12);
1151 printf("current process = ");
1152 if (curproc) {
1153 printf("%lu (%s)\n",
1154 (u_long)curproc->p_pid, curproc->p_comm ?
1155 curproc->p_comm : "");
1156 } else {
1157 printf("Idle\n");
1158 }
f1d1c3fa
MD
1159 printf("current thread = pri %d ", curthread->td_pri);
1160 if (curthread->td_pri >= TDPRI_CRIT)
1161 printf("(CRIT)");
1162 printf("\n");
984263bc
MD
1163#ifdef SMP
1164/**
1165 * XXX FIXME:
1166 * we probably SHOULD have stopped the other CPUs before now!
1167 * another CPU COULD have been touching cpl at this moment...
1168 */
1169 printf(" <- SMP: XXX");
1170#endif
1171 printf("\n");
1172
1173#ifdef KDB
1174 if (kdb_trap(&psl))
1175 return;
1176#endif
1177#ifdef DDB
f7bc9806 1178 if ((debugger_on_panic || db_active) && kdb_trap(type, code, frame))
984263bc
MD
1179 return;
1180#endif
1181 printf("trap number = %d\n", type);
1182 if (type <= MAX_TRAP_MSG)
1183 panic("%s", trap_msg[type]);
1184 else
1185 panic("unknown/reserved trap");
1186}
1187
1188/*
1189 * Double fault handler. Called when a fault occurs while writing
1190 * a frame for a trap/exception onto the stack. This usually occurs
1191 * when the stack overflows (such is the case with infinite recursion,
1192 * for example).
1193 *
1194 * XXX Note that the current PTD gets replaced by IdlePTD when the
1195 * task switch occurs. This means that the stack that was active at
1196 * the time of the double fault is not available at <kstack> unless
1197 * the machine was idle when the double fault occurred. The downside
1198 * of this is that "trace <ebp>" in ddb won't work.
1199 */
1200void
1201dblfault_handler()
1202{
85100692 1203 struct mdglobaldata *gd = mdcpu;
17a9f566 1204
984263bc 1205 printf("\nFatal double fault:\n");
17a9f566
MD
1206 printf("eip = 0x%x\n", gd->gd_common_tss.tss_eip);
1207 printf("esp = 0x%x\n", gd->gd_common_tss.tss_esp);
1208 printf("ebp = 0x%x\n", gd->gd_common_tss.tss_ebp);
984263bc 1209#ifdef SMP
7a44d1cb 1210 /* three separate prints in case of a trap on an unmapped page */
984263bc 1211 printf("mp_lock = %08x; ", mp_lock);
8a8d5d85 1212 printf("cpuid = %d; ", mycpu->gd_cpuid);
984263bc
MD
1213 printf("lapic.id = %08x\n", lapic.id);
1214#endif
1215 panic("double fault");
1216}
1217
1218/*
1219 * Compensate for 386 brain damage (missing URKR).
1220 * This is a little simpler than the pagefault handler in trap() because
1221 * it the page tables have already been faulted in and high addresses
1222 * are thrown out early for other reasons.
1223 */
1224int trapwrite(addr)
1225 unsigned addr;
1226{
1227 struct proc *p;
1228 vm_offset_t va;
1229 struct vmspace *vm;
1230 int rv;
1231
1232 va = trunc_page((vm_offset_t)addr);
1233 /*
1234 * XXX - MAX is END. Changed > to >= for temp. fix.
1235 */
1236 if (va >= VM_MAXUSER_ADDRESS)
1237 return (1);
1238
1239 p = curproc;
1240 vm = p->p_vmspace;
1241
1242 ++p->p_lock;
1243
1244 if (!grow_stack (p, va)) {
1245 --p->p_lock;
1246 return (1);
1247 }
1248
1249 /*
1250 * fault the data page
1251 */
1252 rv = vm_fault(&vm->vm_map, va, VM_PROT_WRITE, VM_FAULT_DIRTY);
1253
1254 --p->p_lock;
1255
1256 if (rv != KERN_SUCCESS)
1257 return 1;
1258
1259 return (0);
1260}
1261
1262/*
1263 * syscall2 - MP aware system call request C handler
1264 *
1265 * A system call is essentially treated as a trap except that the
1266 * MP lock is not held on entry or return. We are responsible for
1267 * obtaining the MP lock if necessary and for handling ASTs
1268 * (e.g. a task switch) prior to return.
1269 *
1270 * In general, only simple access and manipulation of curproc and
1271 * the current stack is allowed without having to hold MP lock.
270ac911
MD
1272 *
1273 * MPSAFE - note that large sections of this routine are run without
1274 * the MP lock.
984263bc 1275 */
270ac911 1276
984263bc 1277void
a64ba182 1278syscall2(struct trapframe frame)
984263bc 1279{
dadab5e9
MD
1280 struct thread *td = curthread;
1281 struct proc *p = td->td_proc;
81eea9f4 1282 struct lwp *lp = td->td_lwp;
984263bc 1283 caddr_t params;
984263bc 1284 struct sysent *callp;
984263bc 1285 register_t orig_tf_eflags;
37af14fe 1286 int sticks;
984263bc
MD
1287 int error;
1288 int narg;
7527bc81
MD
1289#ifdef INVARIANTS
1290 int crit_count = td->td_pri & ~TDPRI_MASK;
1291#endif
270ac911
MD
1292#ifdef SMP
1293 int have_mplock = 0;
1294#endif
984263bc 1295 u_int code;
a64ba182 1296 union sysunion args;
984263bc
MD
1297
1298#ifdef DIAGNOSTIC
1299 if (ISPL(frame.tf_cs) != SEL_UPL) {
1300 get_mplock();
1301 panic("syscall");
1302 /* NOT REACHED */
1303 }
1304#endif
1305
8a8d5d85 1306#ifdef SMP
9753ab6c 1307 KASSERT(td->td_mpcount == 0, ("badmpcount syscall2 from %p", (void *)frame.tf_eip));
270ac911
MD
1308 if (syscall_mpsafe == 0)
1309 MAKEMPSAFE(have_mplock);
8a8d5d85 1310#endif
37af14fe
MD
1311 userenter(td); /* lazy raise our priority */
1312
1313 sticks = (int)td->td_sticks;
984263bc 1314
81eea9f4 1315 lp->lwp_md.md_regs = &frame;
984263bc
MD
1316 params = (caddr_t)frame.tf_esp + sizeof(int);
1317 code = frame.tf_eax;
1318 orig_tf_eflags = frame.tf_eflags;
1319
1320 if (p->p_sysent->sv_prepsyscall) {
270ac911 1321 (*p->p_sysent->sv_prepsyscall)(
f9a13fc4 1322 &frame, (int *)(&args.nosys.sysmsg + 1),
270ac911 1323 &code, &params);
984263bc
MD
1324 } else {
1325 /*
1326 * Need to check if this is a 32 bit or 64 bit syscall.
1327 * fuword is MP aware.
1328 */
1329 if (code == SYS_syscall) {
1330 /*
1331 * Code is first argument, followed by actual args.
1332 */
1333 code = fuword(params);
1334 params += sizeof(int);
1335 } else if (code == SYS___syscall) {
1336 /*
1337 * Like syscall, but code is a quad, so as to maintain
1338 * quad alignment for the rest of the arguments.
1339 */
1340 code = fuword(params);
1341 params += sizeof(quad_t);
1342 }
1343 }
1344
8ec60c3f 1345 code &= p->p_sysent->sv_mask;
7062f5b4
EN
1346 if (code >= p->p_sysent->sv_size)
1347 callp = &p->p_sysent->sv_table[0];
1348 else
1349 callp = &p->p_sysent->sv_table[code];
984263bc
MD
1350
1351 narg = callp->sy_narg & SYF_ARGMASK;
1352
1353 /*
1354 * copyin is MP aware, but the tracing code is not
1355 */
8ec60c3f 1356 if (narg && params) {
f9a13fc4 1357 error = copyin(params, (caddr_t)(&args.nosys.sysmsg + 1),
8ec60c3f
MD
1358 narg * sizeof(register_t));
1359 if (error) {
984263bc 1360#ifdef KTRACE
270ac911
MD
1361 if (KTRPOINT(td, KTR_SYSCALL)) {
1362 MAKEMPSAFE(have_mplock);
1363
a9b80e23 1364 ktrsyscall(p, code, narg,
f9a13fc4 1365 (void *)(&args.nosys.sysmsg + 1));
270ac911 1366 }
984263bc 1367#endif
8ec60c3f
MD
1368 goto bad;
1369 }
984263bc
MD
1370 }
1371
984263bc 1372#ifdef KTRACE
dadab5e9 1373 if (KTRPOINT(td, KTR_SYSCALL)) {
270ac911 1374 MAKEMPSAFE(have_mplock);
f9a13fc4 1375 ktrsyscall(p, code, narg, (void *)(&args.nosys.sysmsg + 1));
984263bc
MD
1376 }
1377#endif
01dce7bb
MD
1378
1379 /*
1380 * For traditional syscall code edx is left untouched when 32 bit
1381 * results are returned. Since edx is loaded from fds[1] when the
1382 * system call returns we pre-set it here.
1383 */
c7114eea
MD
1384 args.sysmsg_fds[0] = 0;
1385 args.sysmsg_fds[1] = frame.tf_edx;
984263bc
MD
1386
1387 STOPEVENT(p, S_SCE, narg); /* MP aware */
1388
270ac911
MD
1389#ifdef SMP
1390 /*
1391 * Try to run the syscall without the MP lock if the syscall
1392 * is MP safe. We have to obtain the MP lock no matter what if
1393 * we are ktracing
1394 */
1395 if ((callp->sy_narg & SYF_MPSAFE) == 0)
1396 MAKEMPSAFE(have_mplock);
1397#endif
1398
a64ba182 1399 error = (*callp->sy_call)(&args);
984263bc
MD
1400
1401 /*
1402 * MP SAFE (we may or may not have the MP lock at this point)
1403 */
1404 switch (error) {
1405 case 0:
1406 /*
1407 * Reinitialize proc pointer `p' as it may be different
1408 * if this is a child returning from fork syscall.
1409 */
1410 p = curproc;
81eea9f4 1411 lp = curthread->td_lwp;
c7114eea
MD
1412 frame.tf_eax = args.sysmsg_fds[0];
1413 frame.tf_edx = args.sysmsg_fds[1];
984263bc
MD
1414 frame.tf_eflags &= ~PSL_C;
1415 break;
984263bc
MD
1416 case ERESTART:
1417 /*
1418 * Reconstruct pc, assuming lcall $X,y is 7 bytes,
1419 * int 0x80 is 2 bytes. We saved this in tf_err.
1420 */
1421 frame.tf_eip -= frame.tf_err;
1422 break;
984263bc
MD
1423 case EJUSTRETURN:
1424 break;
245e4f17
MD
1425 case EASYNC:
1426 panic("Unexpected EASYNC return value (for now)");
984263bc
MD
1427 default:
1428bad:
7062f5b4
EN
1429 if (p->p_sysent->sv_errsize) {
1430 if (error >= p->p_sysent->sv_errsize)
1431 error = -1; /* XXX */
1432 else
1433 error = p->p_sysent->sv_errtbl[error];
984263bc
MD
1434 }
1435 frame.tf_eax = error;
1436 frame.tf_eflags |= PSL_C;
1437 break;
1438 }
1439
1440 /*
1441 * Traced syscall. trapsignal() is not MP aware.
1442 */
1443 if ((orig_tf_eflags & PSL_T) && !(orig_tf_eflags & PSL_VM)) {
270ac911 1444 MAKEMPSAFE(have_mplock);
984263bc
MD
1445 frame.tf_eflags &= ~PSL_T;
1446 trapsignal(p, SIGTRAP, 0);
1447 }
1448
1449 /*
1450 * Handle reschedule and other end-of-syscall issues
1451 */
81eea9f4 1452 userret(lp, &frame, sticks);
984263bc
MD
1453
1454#ifdef KTRACE
dadab5e9 1455 if (KTRPOINT(td, KTR_SYSRET)) {
270ac911 1456 MAKEMPSAFE(have_mplock);
a9b80e23 1457 ktrsysret(p, code, error, args.sysmsg_result);
984263bc
MD
1458 }
1459#endif
1460
1461 /*
1462 * This works because errno is findable through the
1463 * register set. If we ever support an emulation where this
1464 * is not the case, this code will need to be revisited.
1465 */
1466 STOPEVENT(p, S_SCX, code);
1467
81eea9f4 1468 userexit(lp);
8a8d5d85 1469#ifdef SMP
984263bc
MD
1470 /*
1471 * Release the MP lock if we had to get it
1472 */
270ac911 1473 KASSERT(td->td_mpcount == have_mplock,
9753ab6c 1474 ("badmpcount syscall2/end from %p", (void *)frame.tf_eip));
270ac911
MD
1475 if (have_mplock)
1476 rel_mplock();
8a8d5d85 1477#endif
7527bc81
MD
1478#ifdef INVARIANTS
1479 KASSERT(crit_count == (td->td_pri & ~TDPRI_MASK),
1480 ("syscall: critical section count mismatch! %d/%d",
1481 crit_count / TDPRI_CRIT, td->td_pri / TDPRI_CRIT));
1482#endif
984263bc
MD
1483}
1484
1485/*
1486 * Simplified back end of syscall(), used when returning from fork()
8a8d5d85
MD
1487 * directly into user mode. MP lock is held on entry and should be
1488 * released on return. This code will return back into the fork
1489 * trampoline code which then runs doreti.
984263bc
MD
1490 */
1491void
1492fork_return(p, frame)
1493 struct proc *p;
1494 struct trapframe frame;
1495{
81eea9f4
SS
1496 struct lwp *lp;
1497
1498 KKASSERT(p->p_nthreads == 1);
1499
1500 lp = LIST_FIRST(&p->p_lwps);
1501
984263bc
MD
1502 frame.tf_eax = 0; /* Child returns zero */
1503 frame.tf_eflags &= ~PSL_C; /* success */
1504 frame.tf_edx = 1;
1505
3a57cd9c
MD
1506 /*
1507 * Newly forked processes are given a kernel priority. We have to
1508 * adjust the priority to a normal user priority and fake entry
1509 * into the kernel (call userenter()) to install a passive release
1510 * function just in case userret() decides to stop the process. This
1511 * can occur when ^Z races a fork. If we do not install the passive
1512 * release function the current process designation will not be
1513 * released when the thread goes to sleep.
1514 */
1515 lwkt_setpri_self(TDPRI_USER_NORM);
81eea9f4
SS
1516 userenter(lp->lwp_thread);
1517 userret(lp, &frame, 0);
984263bc 1518#ifdef KTRACE
81eea9f4 1519 if (KTRPOINT(lp->lwp_thread, KTR_SYSRET))
a9b80e23 1520 ktrsysret(p, SYS_fork, 0, 0);
984263bc 1521#endif
d9eea1a5 1522 p->p_flag |= P_PASSIVE_ACQ;
81eea9f4 1523 userexit(lp);
d9eea1a5 1524 p->p_flag &= ~P_PASSIVE_ACQ;
8a8d5d85 1525#ifdef SMP
81eea9f4 1526 KKASSERT(lp->lwp_thread->td_mpcount == 1);
8a8d5d85
MD
1527 rel_mplock();
1528#endif
984263bc 1529}