]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/sparc64/sparc64/trap.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / sparc64 / sparc64 / trap.c
1 /*-
2  * Copyright (c) 2001, Jake Burkholder
3  * Copyright (C) 1994, David Greenman
4  * Copyright (c) 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * the University of Utah, and William Jolitz.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      from: @(#)trap.c        7.4 (Berkeley) 5/13/91
39  *      from: FreeBSD: src/sys/i386/i386/trap.c,v 1.197 2001/07/19
40  */
41
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include "opt_ddb.h"
46 #include "opt_ktr.h"
47 #include "opt_ktrace.h"
48
49 #include <sys/param.h>
50 #include <sys/kdb.h>
51 #include <sys/kernel.h>
52 #include <sys/bus.h>
53 #include <sys/interrupt.h>
54 #include <sys/ktr.h>
55 #include <sys/lock.h>
56 #include <sys/mutex.h>
57 #include <sys/systm.h>
58 #include <sys/pcpu.h>
59 #include <sys/pioctl.h>
60 #include <sys/ptrace.h>
61 #include <sys/proc.h>
62 #include <sys/smp.h>
63 #include <sys/signalvar.h>
64 #include <sys/syscall.h>
65 #include <sys/sysctl.h>
66 #include <sys/sysent.h>
67 #include <sys/vmmeter.h>
68 #ifdef KTRACE
69 #include <sys/uio.h>
70 #include <sys/ktrace.h>
71 #endif
72 #include <security/audit/audit.h>
73
74 #include <dev/ofw/openfirm.h>
75
76 #include <vm/vm.h>
77 #include <vm/pmap.h>
78 #include <vm/vm_extern.h>
79 #include <vm/vm_param.h>
80 #include <vm/vm_kern.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_page.h>
83
84 #include <machine/cpu.h>
85 #include <machine/frame.h>
86 #include <machine/intr_machdep.h>
87 #include <machine/ofw_machdep.h>
88 #include <machine/pcb.h>
89 #include <machine/smp.h>
90 #include <machine/trap.h>
91 #include <machine/tstate.h>
92 #include <machine/tte.h>
93 #include <machine/tlb.h>
94 #include <machine/tsb.h>
95 #include <machine/watch.h>
96
97 struct syscall_args {
98         u_long code;
99         struct sysent *callp;
100         register_t args[8];
101         register_t *argp;
102         int narg;
103 };
104
105 void trap(struct trapframe *tf);
106 void syscall(struct trapframe *tf);
107
108 static int fetch_syscall_args(struct thread *td, struct syscall_args *sa);
109 static int trap_cecc(void);
110 static int trap_pfault(struct thread *td, struct trapframe *tf);
111
112 extern char copy_fault[];
113 extern char copy_nofault_begin[];
114 extern char copy_nofault_end[];
115
116 extern char fs_fault[];
117 extern char fs_nofault_begin[];
118 extern char fs_nofault_end[];
119 extern char fs_nofault_intr_begin[];
120 extern char fs_nofault_intr_end[];
121
122 extern char fas_fault[];
123 extern char fas_nofault_begin[];
124 extern char fas_nofault_end[];
125
126 extern char *syscallnames[];
127
128 const char *const trap_msg[] = {
129         "reserved",
130         "instruction access exception",
131         "instruction access error",
132         "instruction access protection",
133         "illtrap instruction",
134         "illegal instruction",
135         "privileged opcode",
136         "floating point disabled",
137         "floating point exception ieee 754",
138         "floating point exception other",
139         "tag overflow",
140         "division by zero",
141         "data access exception",
142         "data access error",
143         "data access protection",
144         "memory address not aligned",
145         "privileged action",
146         "async data error",
147         "trap instruction 16",
148         "trap instruction 17",
149         "trap instruction 18",
150         "trap instruction 19",
151         "trap instruction 20",
152         "trap instruction 21",
153         "trap instruction 22",
154         "trap instruction 23",
155         "trap instruction 24",
156         "trap instruction 25",
157         "trap instruction 26",
158         "trap instruction 27",
159         "trap instruction 28",
160         "trap instruction 29",
161         "trap instruction 30",
162         "trap instruction 31",
163         "fast instruction access mmu miss",
164         "fast data access mmu miss",
165         "interrupt",
166         "physical address watchpoint",
167         "virtual address watchpoint",
168         "corrected ecc error",
169         "spill",
170         "fill",
171         "fill",
172         "breakpoint",
173         "clean window",
174         "range check",
175         "fix alignment",
176         "integer overflow",
177         "syscall",
178         "restore physical watchpoint",
179         "restore virtual watchpoint",
180         "kernel stack fault",
181 };
182
183 static const int trap_sig[] = {
184         SIGILL,                 /* reserved */
185         SIGILL,                 /* instruction access exception */
186         SIGILL,                 /* instruction access error */
187         SIGILL,                 /* instruction access protection */
188         SIGILL,                 /* illtrap instruction */
189         SIGILL,                 /* illegal instruction */
190         SIGBUS,                 /* privileged opcode */
191         SIGFPE,                 /* floating point disabled */
192         SIGFPE,                 /* floating point exception ieee 754 */
193         SIGFPE,                 /* floating point exception other */
194         SIGEMT,                 /* tag overflow */
195         SIGFPE,                 /* division by zero */
196         SIGILL,                 /* data access exception */
197         SIGILL,                 /* data access error */
198         SIGBUS,                 /* data access protection */
199         SIGBUS,                 /* memory address not aligned */
200         SIGBUS,                 /* privileged action */
201         SIGBUS,                 /* async data error */
202         SIGILL,                 /* trap instruction 16 */
203         SIGILL,                 /* trap instruction 17 */
204         SIGILL,                 /* trap instruction 18 */
205         SIGILL,                 /* trap instruction 19 */
206         SIGILL,                 /* trap instruction 20 */
207         SIGILL,                 /* trap instruction 21 */
208         SIGILL,                 /* trap instruction 22 */
209         SIGILL,                 /* trap instruction 23 */
210         SIGILL,                 /* trap instruction 24 */
211         SIGILL,                 /* trap instruction 25 */
212         SIGILL,                 /* trap instruction 26 */
213         SIGILL,                 /* trap instruction 27 */
214         SIGILL,                 /* trap instruction 28 */
215         SIGILL,                 /* trap instruction 29 */
216         SIGILL,                 /* trap instruction 30 */
217         SIGILL,                 /* trap instruction 31 */
218         SIGSEGV,                /* fast instruction access mmu miss */
219         SIGSEGV,                /* fast data access mmu miss */
220         -1,                     /* interrupt */
221         -1,                     /* physical address watchpoint */
222         -1,                     /* virtual address watchpoint */
223         -1,                     /* corrected ecc error */
224         SIGILL,                 /* spill */
225         SIGILL,                 /* fill */
226         SIGILL,                 /* fill */
227         SIGTRAP,                /* breakpoint */
228         SIGILL,                 /* clean window */
229         SIGILL,                 /* range check */
230         SIGILL,                 /* fix alignment */
231         SIGILL,                 /* integer overflow */
232         SIGSYS,                 /* syscall */
233         -1,                     /* restore physical watchpoint */
234         -1,                     /* restore virtual watchpoint */
235         -1,                     /* kernel stack fault */
236 };
237
238 CTASSERT(sizeof(struct trapframe) == 256);
239
240 int debugger_on_signal = 0;
241 SYSCTL_INT(_debug, OID_AUTO, debugger_on_signal, CTLFLAG_RW,
242     &debugger_on_signal, 0, "");
243
244 u_int corrected_ecc = 0;
245 SYSCTL_UINT(_machdep, OID_AUTO, corrected_ecc, CTLFLAG_RD, &corrected_ecc, 0,
246     "corrected ECC errors");
247
248 /*
249  * SUNW,set-trap-table allows to take over %tba from the PROM, which
250  * will turn off interrupts and handle outstanding ones while doing so,
251  * in a safe way.
252  */
253 void
254 sun4u_set_traptable(void *tba_addr)
255 {
256         static struct {
257                 cell_t name;
258                 cell_t nargs;
259                 cell_t nreturns;
260                 cell_t tba_addr;
261         } args = {
262                 (cell_t)"SUNW,set-trap-table",
263                 1,
264                 0,
265         };
266
267         args.tba_addr = (cell_t)tba_addr;
268         ofw_entry(&args);
269 }
270
271 void
272 trap(struct trapframe *tf)
273 {
274         struct thread *td;
275         struct proc *p;
276         int error;
277         int sig;
278         register_t addr;
279         ksiginfo_t ksi;
280
281         td = curthread;
282
283         CTR4(KTR_TRAP, "trap: %p type=%s (%s) pil=%#lx", td,
284             trap_msg[tf->tf_type & ~T_KERNEL],
285             (TRAPF_USERMODE(tf) ? "user" : "kernel"), rdpr(pil));
286
287         PCPU_INC(cnt.v_trap);
288
289         if ((tf->tf_tstate & TSTATE_PRIV) == 0) {
290                 KASSERT(td != NULL, ("trap: curthread NULL"));
291                 KASSERT(td->td_proc != NULL, ("trap: curproc NULL"));
292
293                 p = td->td_proc;
294                 td->td_pticks = 0;
295                 td->td_frame = tf;
296                 addr = tf->tf_tpc;
297                 if (td->td_ucred != p->p_ucred)
298                         cred_update_thread(td);
299
300                 switch (tf->tf_type) {
301                 case T_DATA_MISS:
302                 case T_DATA_PROTECTION:
303                         addr = tf->tf_sfar;
304                         /* FALLTHROUGH */
305                 case T_INSTRUCTION_MISS:
306                         sig = trap_pfault(td, tf);
307                         break;
308                 case T_FILL:
309                         sig = rwindow_load(td, tf, 2);
310                         break;
311                 case T_FILL_RET:
312                         sig = rwindow_load(td, tf, 1);
313                         break;
314                 case T_SPILL:
315                         sig = rwindow_save(td);
316                         break;
317                 case T_CORRECTED_ECC_ERROR:
318                         sig = trap_cecc();
319                         break;
320                 default:
321                         if (tf->tf_type < 0 || tf->tf_type >= T_MAX)
322                                 panic("trap: bad trap type %#lx (user)",
323                                     tf->tf_type);
324                         else if (trap_sig[tf->tf_type] == -1)
325                                 panic("trap: %s (user)",
326                                     trap_msg[tf->tf_type]);
327                         sig = trap_sig[tf->tf_type];
328                         break;
329                 }
330
331                 if (sig != 0) {
332                         /* Translate fault for emulators. */
333                         if (p->p_sysent->sv_transtrap != NULL) {
334                                 sig = p->p_sysent->sv_transtrap(sig,
335                                     tf->tf_type);
336                         }
337                         if (debugger_on_signal &&
338                             (sig == 4 || sig == 10 || sig == 11))
339                                 kdb_enter(KDB_WHY_TRAPSIG, "trapsig");
340                         ksiginfo_init_trap(&ksi);
341                         ksi.ksi_signo = sig;
342                         ksi.ksi_code = (int)tf->tf_type; /* XXX not POSIX */
343                         ksi.ksi_addr = (void *)addr;
344                         ksi.ksi_trapno = (int)tf->tf_type;
345                         trapsignal(td, &ksi);
346                 }
347
348                 userret(td, tf);
349                 mtx_assert(&Giant, MA_NOTOWNED);
350         } else {
351                 KASSERT((tf->tf_type & T_KERNEL) != 0,
352                     ("trap: kernel trap isn't"));
353
354                 if (kdb_active) {
355                         kdb_reenter();
356                         return;
357                 }
358
359                 switch (tf->tf_type & ~T_KERNEL) {
360                 case T_BREAKPOINT:
361                 case T_KSTACK_FAULT:
362                         error = (kdb_trap(tf->tf_type, 0, tf) == 0);
363                         TF_DONE(tf);
364                         break;
365 #ifdef notyet
366                 case T_PA_WATCHPOINT:
367                 case T_VA_WATCHPOINT:
368                         error = db_watch_trap(tf);
369                         break;
370 #endif
371                 case T_DATA_MISS:
372                 case T_DATA_PROTECTION:
373                 case T_INSTRUCTION_MISS:
374                         error = trap_pfault(td, tf);
375                         break;
376                 case T_DATA_EXCEPTION:
377                 case T_MEM_ADDRESS_NOT_ALIGNED:
378                         if ((tf->tf_sfsr & MMU_SFSR_FV) != 0 &&
379                             MMU_SFSR_GET_ASI(tf->tf_sfsr) == ASI_AIUP) {
380                                 if (tf->tf_tpc >= (u_long)copy_nofault_begin &&
381                                     tf->tf_tpc <= (u_long)copy_nofault_end) {
382                                         tf->tf_tpc = (u_long)copy_fault;
383                                         tf->tf_tnpc = tf->tf_tpc + 4;
384                                         error = 0;
385                                         break;
386                                 }
387                                 if (tf->tf_tpc >= (u_long)fs_nofault_begin &&
388                                     tf->tf_tpc <= (u_long)fs_nofault_end) {
389                                         tf->tf_tpc = (u_long)fs_fault;
390                                         tf->tf_tnpc = tf->tf_tpc + 4;
391                                         error = 0;
392                                         break;
393                                 }
394                         }
395                         error = 1;
396                         break;
397                 case T_DATA_ERROR:
398                         /*
399                          * Handle PCI poke/peek as per UltraSPARC IIi
400                          * User's Manual 16.2.1, modulo checking the
401                          * TPC as USIII CPUs generate a precise trap
402                          * instead of a special deferred one.
403                          */
404                         if (tf->tf_tpc > (u_long)fas_nofault_begin &&
405                             tf->tf_tpc < (u_long)fas_nofault_end) {
406                                 cache_flush();
407                                 cache_enable(PCPU_GET(impl));
408                                 tf->tf_tpc = (u_long)fas_fault;
409                                 tf->tf_tnpc = tf->tf_tpc + 4;
410                                 error = 0;
411                                 break;
412                         }
413                         error = 1;
414                         break;
415                 case T_CORRECTED_ECC_ERROR:
416                         error = trap_cecc();
417                         break;
418                 default:
419                         error = 1;
420                         break;
421                 }
422
423                 if (error != 0) {
424                         tf->tf_type &= ~T_KERNEL;
425                         if (tf->tf_type < 0 || tf->tf_type >= T_MAX)
426                                 panic("trap: bad trap type %#lx (kernel)",
427                                     tf->tf_type);
428                         else if (trap_sig[tf->tf_type] == -1)
429                                 panic("trap: %s (kernel)",
430                                     trap_msg[tf->tf_type]);
431                 }
432         }
433         CTR1(KTR_TRAP, "trap: td=%p return", td);
434 }
435
436 static int
437 trap_cecc(void)
438 {
439         u_long eee;
440
441         /*
442          * Turn off (non-)correctable error reporting while we're dealing
443          * with the error.
444          */
445         eee = ldxa(0, ASI_ESTATE_ERROR_EN_REG);
446         stxa_sync(0, ASI_ESTATE_ERROR_EN_REG, eee & ~(AA_ESTATE_NCEEN |
447             AA_ESTATE_CEEN));
448         /* Flush the caches in order ensure no corrupt data got installed. */
449         cache_flush();
450         /* Ensure the caches are still turned on (should be). */
451         cache_enable(PCPU_GET(impl));
452         /* Clear the the error from the AFSR. */
453         stxa_sync(0, ASI_AFSR, ldxa(0, ASI_AFSR));
454         corrected_ecc++;
455         printf("corrected ECC error\n");
456         /* Turn (non-)correctable error reporting back on. */
457         stxa_sync(0, ASI_ESTATE_ERROR_EN_REG, eee);
458         return (0);
459 }
460
461 static int
462 trap_pfault(struct thread *td, struct trapframe *tf)
463 {
464         struct vmspace *vm;
465         struct proc *p;
466         vm_offset_t va;
467         vm_prot_t prot;
468         vm_map_entry_t entry;
469         u_long ctx;
470         int flags;
471         int type;
472         int rv;
473
474         if (td == NULL)
475                 return (-1);
476         KASSERT(td->td_pcb != NULL, ("trap_pfault: pcb NULL"));
477         KASSERT(td->td_proc != NULL, ("trap_pfault: curproc NULL"));
478         KASSERT(td->td_proc->p_vmspace != NULL, ("trap_pfault: vmspace NULL"));
479
480         p = td->td_proc;
481
482         rv = KERN_SUCCESS;
483         ctx = TLB_TAR_CTX(tf->tf_tar);
484         type = tf->tf_type & ~T_KERNEL;
485         va = TLB_TAR_VA(tf->tf_tar);
486
487         CTR4(KTR_TRAP, "trap_pfault: td=%p pm_ctx=%#lx va=%#lx ctx=%#lx",
488             td, p->p_vmspace->vm_pmap.pm_context[curcpu], va, ctx);
489
490         if (type == T_DATA_PROTECTION) {
491                 prot = VM_PROT_WRITE;
492                 flags = VM_FAULT_DIRTY;
493         } else {
494                 if (type == T_DATA_MISS)
495                         prot = VM_PROT_READ;
496                 else
497                         prot = VM_PROT_READ | VM_PROT_EXECUTE;
498                 flags = VM_FAULT_NORMAL;
499         }
500
501         if (ctx != TLB_CTX_KERNEL) {
502                 if ((tf->tf_tstate & TSTATE_PRIV) != 0 &&
503                     (tf->tf_tpc >= (u_long)fs_nofault_intr_begin &&
504                     tf->tf_tpc <= (u_long)fs_nofault_intr_end)) {
505                         tf->tf_tpc = (u_long)fs_fault;
506                         tf->tf_tnpc = tf->tf_tpc + 4;
507                         return (0);
508                 }
509
510                 /*
511                  * This is a fault on non-kernel virtual memory.
512                  */
513                 vm = p->p_vmspace;
514
515                 /*
516                  * Keep swapout from messing with us during this
517                  * critical time.
518                  */
519                 PROC_LOCK(p);
520                 ++p->p_lock;
521                 PROC_UNLOCK(p);
522
523                 /* Fault in the user page. */
524                 rv = vm_fault(&vm->vm_map, va, prot, flags);
525
526                 /*
527                  * Now the process can be swapped again.
528                  */
529                 PROC_LOCK(p);
530                 --p->p_lock;
531                 PROC_UNLOCK(p);
532         } else {
533                 /*
534                  * This is a fault on kernel virtual memory.  Attempts to
535                  * access kernel memory from user mode cause privileged
536                  * action traps, not page fault.
537                  */
538                 KASSERT(tf->tf_tstate & TSTATE_PRIV,
539                     ("trap_pfault: fault on nucleus context from user mode"));
540
541                 if (tf->tf_tpc >= (u_long)copy_nofault_begin &&
542                     tf->tf_tpc <= (u_long)copy_nofault_end) {
543                         vm_map_lock_read(kernel_map);
544                         if (vm_map_lookup_entry(kernel_map, va, &entry) &&
545                             (entry->eflags & MAP_ENTRY_NOFAULT) != 0) {
546                                 tf->tf_tpc = (u_long)copy_fault;
547                                 tf->tf_tnpc = tf->tf_tpc + 4;
548                                 vm_map_unlock_read(kernel_map);
549                                 return (0);
550                         }
551                         vm_map_unlock_read(kernel_map);
552                 }
553
554                 /*
555                  * We don't have to worry about process locking or stacks in
556                  * the kernel.
557                  */
558                 rv = vm_fault(kernel_map, va, prot, VM_FAULT_NORMAL);
559         }
560
561         CTR3(KTR_TRAP, "trap_pfault: return td=%p va=%#lx rv=%d",
562             td, va, rv);
563         if (rv == KERN_SUCCESS)
564                 return (0);
565         if (ctx != TLB_CTX_KERNEL && (tf->tf_tstate & TSTATE_PRIV) != 0) {
566                 if (tf->tf_tpc >= (u_long)fs_nofault_begin &&
567                     tf->tf_tpc <= (u_long)fs_nofault_end) {
568                         tf->tf_tpc = (u_long)fs_fault;
569                         tf->tf_tnpc = tf->tf_tpc + 4;
570                         return (0);
571                 }
572                 if (tf->tf_tpc >= (u_long)copy_nofault_begin &&
573                     tf->tf_tpc <= (u_long)copy_nofault_end) {
574                         tf->tf_tpc = (u_long)copy_fault;
575                         tf->tf_tnpc = tf->tf_tpc + 4;
576                         return (0);
577                 }
578         }
579         return ((rv == KERN_PROTECTION_FAILURE) ? SIGBUS : SIGSEGV);
580 }
581
582 /* Maximum number of arguments that can be passed via the out registers. */
583 #define REG_MAXARGS     6
584
585 static int
586 fetch_syscall_args(struct thread *td, struct syscall_args *sa)
587 {
588         struct trapframe *tf;
589         struct proc *p;
590         int reg;
591         int regcnt;
592         int error;
593
594         p = td->td_proc;
595         tf = td->td_frame;
596         reg = 0;
597         regcnt = REG_MAXARGS;
598
599         sa->code = tf->tf_global[1];
600
601         if (p->p_sysent->sv_prepsyscall) {
602 #if 0
603                 (*p->p_sysent->sv_prepsyscall)(tf, sa->args, &sa->code,
604                     &params);
605 #endif
606         } else if (sa->code == SYS_syscall || sa->code == SYS___syscall) {
607                 sa->code = tf->tf_out[reg++];
608                 regcnt--;
609         }
610
611         if (p->p_sysent->sv_mask)
612                 sa->code &= p->p_sysent->sv_mask;
613
614         if (sa->code >= p->p_sysent->sv_size)
615                 sa->callp = &p->p_sysent->sv_table[0];
616         else
617                 sa->callp = &p->p_sysent->sv_table[sa->code];
618
619         sa->narg = sa->callp->sy_narg;
620         KASSERT(sa->narg <= sizeof(sa->args) / sizeof(sa->args[0]),
621             ("Too many syscall arguments!"));
622         error = 0;
623         sa->argp = sa->args;
624         bcopy(&tf->tf_out[reg], sa->args, sizeof(sa->args[0]) * regcnt);
625         if (sa->narg > regcnt)
626                 error = copyin((void *)(tf->tf_out[6] + SPOFF +
627                     offsetof(struct frame, fr_pad[6])), &sa->args[regcnt],
628                     (sa->narg - regcnt) * sizeof(sa->args[0]));
629
630         /*
631          * This may result in two records if debugger modified
632          * registers or memory during sleep at stop/ptrace point.
633          */
634 #ifdef KTRACE
635         if (KTRPOINT(td, KTR_SYSCALL))
636                 ktrsyscall(sa->code, sa->narg, sa->argp);
637 #endif
638         return (error);
639 }
640
641 /*
642  * Syscall handler
643  * The arguments to the syscall are passed in the out registers by the caller,
644  * and are saved in the trap frame.  The syscall number is passed in %g1 (and
645  * also saved in the trap frame).
646  */
647 void
648 syscall(struct trapframe *tf)
649 {
650         struct syscall_args sa;
651         struct thread *td;
652         struct proc *p;
653         int error;
654
655         td = curthread;
656         KASSERT(td != NULL, ("trap: curthread NULL"));
657         KASSERT(td->td_proc != NULL, ("trap: curproc NULL"));
658
659         PCPU_INC(cnt.v_syscall);
660         p = td->td_proc;
661         td->td_syscalls++;
662
663         td->td_pticks = 0;
664         td->td_frame = tf;
665         if (td->td_ucred != p->p_ucred)
666                 cred_update_thread(td);
667         if ((p->p_flag & P_TRACED) != 0) {
668                 PROC_LOCK(p);
669                 td->td_dbgflags &= ~TDB_USERWR;
670                 PROC_UNLOCK(p);
671         }
672
673         /*
674          * For syscalls, we don't want to retry the faulting instruction
675          * (usually), instead we need to advance one instruction.
676          */
677         td->td_pcb->pcb_tpc = tf->tf_tpc;
678         TF_DONE(tf);
679
680         error = fetch_syscall_args(td, &sa);
681         CTR5(KTR_SYSC, "syscall: td=%p %s(%#lx, %#lx, %#lx)", td,
682             syscallnames[sa.code], sa.argp[0], sa.argp[1], sa.argp[2]);
683
684         if (error == 0) {
685                 td->td_retval[0] = 0;
686                 td->td_retval[1] = 0;
687
688                 STOPEVENT(p, S_SCE, sa.narg);
689                 PTRACESTOP_SC(p, td, S_PT_SCE);
690                 if ((td->td_dbgflags & TDB_USERWR) != 0) {
691                         /*
692                          * Reread syscall number and arguments if
693                          * debugger modified registers or memory.
694                          */
695                         error = fetch_syscall_args(td, &sa);
696                         if (error != 0)
697                                 goto retval;
698                         td->td_retval[1] = 0;
699                 }
700
701                 AUDIT_SYSCALL_ENTER(sa.code, td);
702                 error = (*sa.callp->sy_call)(td, sa.argp);
703                 AUDIT_SYSCALL_EXIT(error, td);
704
705                 CTR5(KTR_SYSC, "syscall: p=%p error=%d %s return %#lx %#lx",
706                     p, error, syscallnames[sa.code], td->td_retval[0],
707                     td->td_retval[1]);
708         }
709  retval:
710         cpu_set_syscall_retval(td, error);
711
712         /*
713          * Check for misbehavior.
714          */
715         WITNESS_WARN(WARN_PANIC, NULL, "System call %s returning",
716             (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
717             syscallnames[sa.code] : "???");
718         KASSERT(td->td_critnest == 0,
719             ("System call %s returning in a critical section",
720             (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
721             syscallnames[sa.code] : "???"));
722         KASSERT(td->td_locks == 0,
723             ("System call %s returning with %d locks held",
724             (sa.code >= 0 && sa.code < SYS_MAXSYSCALL) ?
725             syscallnames[sa.code] : "???", td->td_locks));
726
727         /*
728          * Handle reschedule and other end-of-syscall issues.
729          */
730         userret(td, tf);
731
732 #ifdef KTRACE
733         if (KTRPOINT(td, KTR_SYSRET))
734                 ktrsysret(sa.code, error, td->td_retval[0]);
735 #endif
736         /*
737          * This works because errno is findable through the
738          * register set.  If we ever support an emulation where this
739          * is not the case, this code will need to be revisited.
740          */
741         STOPEVENT(p, S_SCX, sa.code);
742
743         PTRACESTOP_SC(p, td, S_PT_SCX);
744 }