1 /* $NetBSD: trap.c,v 1.128 2020/09/05 07:26:37 maxv Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2000, 2017 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, and by Maxime Villard.
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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1990 The Regents of the University of California.
34 * All rights reserved.
35 *
36 * This code is derived from software contributed to Berkeley by
37 * the University of Utah, and William Jolitz.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)trap.c 7.4 (Berkeley) 5/13/91
64 */
65
66 #include <sys/cdefs.h>
67 __KERNEL_RCSID(0, "$NetBSD: trap.c,v 1.128 2020/09/05 07:26:37 maxv Exp $");
68
69 #include "opt_ddb.h"
70 #include "opt_kgdb.h"
71 #include "opt_xen.h"
72 #include "opt_dtrace.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/proc.h>
77 #include <sys/acct.h>
78 #include <sys/kauth.h>
79 #include <sys/kernel.h>
80 #include <sys/kmem.h>
81 #include <sys/ras.h>
82 #include <sys/signal.h>
83 #include <sys/syscall.h>
84 #include <sys/cpu.h>
85 #include <sys/ucontext.h>
86 #include <sys/module_hook.h>
87 #include <sys/compat_stub.h>
88
89 #include <uvm/uvm_extern.h>
90
91 #include <machine/cpufunc.h>
92 #include <x86/fpu.h>
93 #include <x86/dbregs.h>
94 #include <machine/psl.h>
95 #include <machine/reg.h>
96 #include <machine/trap.h>
97 #include <machine/userret.h>
98 #include <machine/db_machdep.h>
99
100 #include <x86/nmi.h>
101
102 #ifndef XENPV
103 #include "isa.h"
104 #endif
105
106 #include <sys/kgdb.h>
107
108 #ifdef KDTRACE_HOOKS
109 #include <sys/dtrace_bsd.h>
110 /*
111 * This is a hook which is initialized by the dtrace module to handle traps
112 * which might occur during DTrace probe execution.
113 */
114 dtrace_trap_func_t dtrace_trap_func = NULL;
115 dtrace_doubletrap_func_t dtrace_doubletrap_func = NULL;
116 #endif
117
118 /*
119 * Module hook for amd64_oosyscall
120 */
121 struct amd64_oosyscall_hook_t amd64_oosyscall_hook;
122
123 void nmitrap(struct trapframe *);
124 void doubletrap(struct trapframe *);
125 void trap(struct trapframe *);
126
127 const char * const trap_type[] = {
128 "privileged instruction fault", /* 0 T_PRIVINFLT */
129 "breakpoint trap", /* 1 T_BPTFLT */
130 "arithmetic trap", /* 2 T_ARITHTRAP */
131 "asynchronous system trap", /* 3 T_ASTFLT */
132 "protection fault", /* 4 T_PROTFLT */
133 "trace trap", /* 5 T_TRCTRAP */
134 "page fault", /* 6 T_PAGEFLT */
135 "alignment fault", /* 7 T_ALIGNFLT */
136 "integer divide fault", /* 8 T_DIVIDE */
137 "non-maskable interrupt", /* 9 T_NMI */
138 "overflow trap", /* 10 T_OFLOW */
139 "bounds check fault", /* 11 T_BOUND */
140 "FPU not available fault", /* 12 T_DNA */
141 "double fault", /* 13 T_DOUBLEFLT */
142 "FPU operand fetch fault", /* 14 T_FPOPFLT */
143 "invalid TSS fault", /* 15 T_TSSFLT */
144 "segment not present fault", /* 16 T_SEGNPFLT */
145 "stack fault", /* 17 T_STKFLT */
146 "machine check fault", /* 18 T_MCA */
147 "SSE FP exception", /* 19 T_XMM */
148 "reserved trap", /* 20 T_RESERVED */
149 };
150 int trap_types = __arraycount(trap_type);
151
152 #ifdef TRAP_SIGDEBUG
153 static void sigdebug(const struct trapframe *, const ksiginfo_t *, int);
154 #define SIGDEBUG(a, b, c) sigdebug(a, b, c)
155 #else
156 #define SIGDEBUG(a, b, c)
157 #endif
158
159 static void
onfault_restore(struct trapframe * frame,void * onfault,int error)160 onfault_restore(struct trapframe *frame, void *onfault, int error)
161 {
162 frame->tf_rip = (uintptr_t)onfault;
163 frame->tf_rax = error;
164 }
165
166 static void *
onfault_handler(const struct pcb * pcb,const struct trapframe * tf)167 onfault_handler(const struct pcb *pcb, const struct trapframe *tf)
168 {
169 struct onfault_table {
170 uintptr_t start;
171 uintptr_t end;
172 void *handler;
173 };
174 extern const struct onfault_table onfault_table[];
175 const struct onfault_table *p;
176 uintptr_t pc;
177
178 if (pcb->pcb_onfault != NULL) {
179 return pcb->pcb_onfault;
180 }
181
182 pc = tf->tf_rip;
183 for (p = onfault_table; p->start; p++) {
184 if (p->start <= pc && pc < p->end) {
185 return p->handler;
186 }
187 }
188 return NULL;
189 }
190
191 static void
trap_print(const struct trapframe * frame,const lwp_t * l)192 trap_print(const struct trapframe *frame, const lwp_t *l)
193 {
194 const int type = frame->tf_trapno;
195
196 if (frame->tf_trapno < trap_types) {
197 printf("fatal %s", trap_type[type]);
198 } else {
199 printf("unknown trap %d", type);
200 }
201 printf(" in %s mode\n", (type & T_USER) ? "user" : "supervisor");
202
203 printf("trap type %d code %#lx rip %#lx cs %#lx rflags %#lx cr2 %#lx "
204 "ilevel %#x rsp %#lx\n",
205 type, frame->tf_err, (u_long)frame->tf_rip, frame->tf_cs,
206 frame->tf_rflags, rcr2(), curcpu()->ci_ilevel, frame->tf_rsp);
207
208 printf("curlwp %p pid %d.%d lowest kstack %p\n",
209 l, l->l_proc->p_pid, l->l_lid, KSTACK_LOWEST_ADDR(l));
210 }
211
212 void
nmitrap(struct trapframe * frame)213 nmitrap(struct trapframe *frame)
214 {
215 const int type = T_NMI;
216
217 if (nmi_dispatch(frame))
218 return;
219 /* NMI can be hooked up to a pushbutton for debugging */
220 if (kgdb_trap(type, frame))
221 return;
222 if (kdb_trap(type, 0, frame))
223 return;
224 /* machine/parity/power fail/"kitchen sink" faults */
225
226 x86_nmi();
227 }
228
229 void
doubletrap(struct trapframe * frame)230 doubletrap(struct trapframe *frame)
231 {
232 const int type = T_DOUBLEFLT;
233 struct lwp *l = curlwp;
234
235 trap_print(frame, l);
236
237 if (kdb_trap(type, 0, frame))
238 return;
239 if (kgdb_trap(type, frame))
240 return;
241
242 panic("double fault");
243 }
244
245 /*
246 * trap(frame): exception, fault, and trap interface to BSD kernel.
247 *
248 * This common code is called from assembly language IDT gate entry routines
249 * that prepare a suitable stack frame, and restore this frame after the
250 * exception has been processed. Note that the effect is as if the arguments
251 * were passed call by reference.
252 *
253 * Note that the fpu traps (07 T_DNA, 10 T_ARITHTRAP and 13 T_XMM)
254 * jump directly into the code in x86/fpu.c so they get processed
255 * without interrupts being enabled.
256 */
257 void
trap(struct trapframe * frame)258 trap(struct trapframe *frame)
259 {
260 struct lwp *l = curlwp;
261 struct proc *p;
262 struct pcb *pcb;
263 extern char kcopy_fault[];
264 ksiginfo_t ksi;
265 void *onfault;
266 int type, error;
267 uint64_t cr2;
268 bool pfail;
269
270 if (__predict_true(l != NULL)) {
271 pcb = lwp_getpcb(l);
272 p = l->l_proc;
273 } else {
274 /*
275 * This can happen eg on break points in early on boot.
276 */
277 pcb = NULL;
278 p = NULL;
279 }
280 type = frame->tf_trapno;
281
282 if (!KERNELMODE(frame->tf_cs)) {
283 type |= T_USER;
284 l->l_md.md_regs = frame;
285 LWP_CACHE_CREDS(l, p);
286 }
287
288 #ifdef KDTRACE_HOOKS
289 /*
290 * A trap can occur while DTrace executes a probe. Before
291 * executing the probe, DTrace blocks re-scheduling and sets
292 * a flag in its per-cpu flags to indicate that it doesn't
293 * want to fault. On returning from the probe, the no-fault
294 * flag is cleared and finally re-scheduling is enabled.
295 *
296 * If the DTrace kernel module has registered a trap handler,
297 * call it and if it returns non-zero, assume that it has
298 * handled the trap and modified the trap frame so that this
299 * function can return normally.
300 */
301 if ((type == T_PROTFLT || type == T_PAGEFLT) &&
302 dtrace_trap_func != NULL) {
303 if ((*dtrace_trap_func)(frame, type)) {
304 return;
305 }
306 }
307 #endif
308
309 switch (type) {
310
311 default:
312 we_re_toast:
313 trap_print(frame, l);
314
315 if (kdb_trap(type, 0, frame))
316 return;
317 if (kgdb_trap(type, frame))
318 return;
319 /*
320 * If this is a breakpoint, don't panic if we're not connected.
321 */
322 if (type == T_BPTFLT && kgdb_disconnected()) {
323 printf("kgdb: ignored %s\n", trap_type[type]);
324 return;
325 }
326 panic("trap");
327 /*NOTREACHED*/
328
329 case T_PROTFLT:
330 case T_SEGNPFLT:
331 case T_ALIGNFLT:
332 case T_STKFLT:
333 case T_TSSFLT:
334 if (p == NULL)
335 goto we_re_toast;
336
337 /* Check for copyin/copyout fault. */
338 onfault = onfault_handler(pcb, frame);
339 if (onfault != NULL) {
340 onfault_restore(frame, onfault, EFAULT);
341 return;
342 }
343
344 goto we_re_toast;
345
346 case T_PROTFLT|T_USER: /* protection fault */
347 { int hook_ret;
348
349 MODULE_HOOK_CALL(amd64_oosyscall_hook, (p, frame),
350 ENOSYS, hook_ret);
351 if (hook_ret == 0) {
352 /* Do the syscall */
353 p->p_md.md_syscall(frame);
354 goto out;
355 }
356 }
357 /* FALLTHROUGH */
358 case T_TSSFLT|T_USER:
359 case T_SEGNPFLT|T_USER:
360 case T_STKFLT|T_USER:
361 case T_ALIGNFLT|T_USER:
362 KSI_INIT_TRAP(&ksi);
363 ksi.ksi_trap = type & ~T_USER;
364 ksi.ksi_addr = (void *)frame->tf_rip;
365 switch (type) {
366 case T_SEGNPFLT|T_USER:
367 case T_STKFLT|T_USER:
368 ksi.ksi_signo = SIGBUS;
369 ksi.ksi_code = BUS_ADRERR;
370 break;
371 case T_TSSFLT|T_USER:
372 ksi.ksi_signo = SIGBUS;
373 ksi.ksi_code = BUS_OBJERR;
374 break;
375 case T_ALIGNFLT|T_USER:
376 ksi.ksi_signo = SIGBUS;
377 ksi.ksi_code = BUS_ADRALN;
378 break;
379 case T_PROTFLT|T_USER:
380 ksi.ksi_signo = SIGSEGV;
381 ksi.ksi_code = SEGV_ACCERR;
382 break;
383 default:
384 KASSERT(0);
385 break;
386 }
387 goto trapsignal;
388
389 case T_PRIVINFLT|T_USER: /* privileged instruction fault */
390 case T_FPOPFLT|T_USER: /* coprocessor operand fault */
391 KSI_INIT_TRAP(&ksi);
392 ksi.ksi_signo = SIGILL;
393 ksi.ksi_trap = type & ~T_USER;
394 ksi.ksi_addr = (void *) frame->tf_rip;
395 switch (type) {
396 case T_PRIVINFLT|T_USER:
397 ksi.ksi_code = ILL_PRVOPC;
398 break;
399 case T_FPOPFLT|T_USER:
400 ksi.ksi_code = ILL_COPROC;
401 break;
402 default:
403 KASSERT(0);
404 break;
405 }
406 goto trapsignal;
407
408 case T_ASTFLT|T_USER:
409 /* Allow process switch. */
410 //curcpu()->ci_data.cpu_nast++;
411 if (l->l_pflag & LP_OWEUPC) {
412 l->l_pflag &= ~LP_OWEUPC;
413 ADDUPROF(l);
414 }
415 goto out;
416
417 case T_BOUND|T_USER:
418 case T_OFLOW|T_USER:
419 case T_DIVIDE|T_USER:
420 KSI_INIT_TRAP(&ksi);
421 ksi.ksi_signo = SIGFPE;
422 ksi.ksi_trap = type & ~T_USER;
423 ksi.ksi_addr = (void *)frame->tf_rip;
424 switch (type) {
425 case T_BOUND|T_USER:
426 ksi.ksi_code = FPE_FLTSUB;
427 break;
428 case T_OFLOW|T_USER:
429 ksi.ksi_code = FPE_INTOVF;
430 break;
431 case T_DIVIDE|T_USER:
432 ksi.ksi_code = FPE_INTDIV;
433 break;
434 default:
435 KASSERT(0);
436 break;
437 }
438 goto trapsignal;
439
440 case T_PAGEFLT:
441 /* Allow page faults in kernel mode. */
442 if (__predict_false(l == NULL))
443 goto we_re_toast;
444
445 onfault = pcb->pcb_onfault;
446
447 if (cpu_intr_p() || (l->l_pflag & LP_INTR) != 0) {
448 goto we_re_toast;
449 }
450
451 cr2 = rcr2();
452
453 if (frame->tf_err & PGEX_I) {
454 /* SMEP might have brought us here */
455 if (cr2 < VM_MAXUSER_ADDRESS) {
456 printf("prevented execution of %p (SMEP)\n",
457 (void *)cr2);
458 goto we_re_toast;
459 }
460 }
461
462 if ((frame->tf_err & PGEX_P) &&
463 cr2 < VM_MAXUSER_ADDRESS) {
464 /* SMAP might have brought us here */
465 if (onfault_handler(pcb, frame) == NULL) {
466 printf("prevented access to %p (SMAP)\n",
467 (void *)cr2);
468 goto we_re_toast;
469 }
470 }
471
472 goto pagefltcommon;
473
474 case T_PAGEFLT|T_USER: {
475 register vaddr_t va;
476 register struct vmspace *vm;
477 register struct vm_map *map;
478 vm_prot_t ftype;
479 extern struct vm_map *kernel_map;
480
481 cr2 = rcr2();
482 if (p->p_emul->e_usertrap != NULL &&
483 (*p->p_emul->e_usertrap)(l, cr2, frame) != 0)
484 return;
485 pagefltcommon:
486 vm = p->p_vmspace;
487 if (__predict_false(vm == NULL)) {
488 goto we_re_toast;
489 }
490 pcb->pcb_cr2 = cr2;
491 va = trunc_page((vaddr_t)cr2);
492 /*
493 * It is only a kernel address space fault iff:
494 * 1. (type & T_USER) == 0 and
495 * 2. pcb_onfault not set or
496 * 3. pcb_onfault set but supervisor space fault
497 * The last can occur during an exec() copyin where the
498 * argument space is lazy-allocated.
499 */
500 if (type == T_PAGEFLT && va >= VM_MIN_KERNEL_ADDRESS)
501 map = kernel_map;
502 else
503 map = &vm->vm_map;
504 if (frame->tf_err & PGEX_W)
505 ftype = VM_PROT_WRITE;
506 else if (frame->tf_err & PGEX_I)
507 ftype = VM_PROT_EXECUTE;
508 else
509 ftype = VM_PROT_READ;
510
511 #ifdef DIAGNOSTIC
512 if (map == kernel_map && va == 0) {
513 printf("trap: bad kernel access at %lx\n", va);
514 goto we_re_toast;
515 }
516 #endif
517 /* Fault the original page in. */
518 onfault = pcb->pcb_onfault;
519 pcb->pcb_onfault = NULL;
520 error = uvm_fault(map, va, ftype);
521 pcb->pcb_onfault = onfault;
522 if (error == 0) {
523 if (map != kernel_map && (void *)va >= vm->vm_maxsaddr)
524 uvm_grow(p, va);
525
526 pfail = false;
527 while (type == T_PAGEFLT) {
528 /*
529 * we need to switch pmap now if we're in
530 * the middle of copyin/out.
531 *
532 * but we don't need to do so for kcopy as
533 * it never touch userspace.
534 */
535 kpreempt_disable();
536 if (curcpu()->ci_want_pmapload) {
537 onfault = onfault_handler(pcb, frame);
538 if (onfault != kcopy_fault) {
539 pmap_load();
540 }
541 }
542 /*
543 * We need to keep the pmap loaded and
544 * so avoid being preempted until back
545 * into the copy functions. Disable
546 * interrupts at the hardware level before
547 * re-enabling preemption. Interrupts
548 * will be re-enabled by 'iret' when
549 * returning back out of the trap stub.
550 * They'll only be re-enabled when the
551 * program counter is once again in
552 * the copy functions, and so visible
553 * to cpu_kpreempt_exit().
554 */
555 #ifndef XENPV
556 x86_disable_intr();
557 #endif
558 l->l_nopreempt--;
559 if (l->l_nopreempt > 0 || !l->l_dopreempt ||
560 pfail) {
561 return;
562 }
563 #ifndef XENPV
564 x86_enable_intr();
565 #endif
566 /*
567 * If preemption fails for some reason,
568 * don't retry it. The conditions won't
569 * change under our nose.
570 */
571 pfail = kpreempt(0);
572 }
573 goto out;
574 }
575
576 if (type == T_PAGEFLT) {
577 onfault = onfault_handler(pcb, frame);
578 if (onfault != NULL) {
579 onfault_restore(frame, onfault, error);
580 return;
581 }
582
583 printf("uvm_fault(%p, 0x%lx, %d) -> %x\n",
584 map, va, ftype, error);
585 goto we_re_toast;
586 }
587
588 KSI_INIT_TRAP(&ksi);
589 ksi.ksi_trap = type & ~T_USER;
590 ksi.ksi_addr = (void *)cr2;
591 switch (error) {
592 case EINVAL:
593 ksi.ksi_signo = SIGBUS;
594 ksi.ksi_code = BUS_ADRERR;
595 break;
596 case EACCES:
597 ksi.ksi_signo = SIGSEGV;
598 ksi.ksi_code = SEGV_ACCERR;
599 error = EFAULT;
600 break;
601 case ENOMEM:
602 ksi.ksi_signo = SIGKILL;
603 printf("UVM: pid %d.%d (%s), uid %d killed: "
604 "out of swap\n", p->p_pid, l->l_lid, p->p_comm,
605 l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1);
606 break;
607 default:
608 ksi.ksi_signo = SIGSEGV;
609 ksi.ksi_code = SEGV_MAPERR;
610 break;
611 }
612
613 SIGDEBUG(frame, &ksi, error);
614 (*p->p_emul->e_trapsignal)(l, &ksi);
615 break;
616 }
617
618 case T_TRCTRAP:
619 /*
620 * Ignore debug register trace traps due to
621 * accesses in the user's address space, which
622 * can happen under several conditions such as
623 * if a user sets a watchpoint on a buffer and
624 * then passes that buffer to a system call.
625 * We still want to get TRCTRAPS for addresses
626 * in kernel space because that is useful when
627 * debugging the kernel.
628 */
629 if (x86_dbregs_user_trap())
630 break;
631
632 goto we_re_toast;
633
634 case T_BPTFLT|T_USER: /* bpt instruction fault */
635 case T_TRCTRAP|T_USER: /* trace trap */
636 /*
637 * Don't go single-stepping into a RAS.
638 */
639 if (p->p_raslist == NULL ||
640 (ras_lookup(p, (void *)frame->tf_rip) == (void *)-1)) {
641 KSI_INIT_TRAP(&ksi);
642 ksi.ksi_signo = SIGTRAP;
643 ksi.ksi_trap = type & ~T_USER;
644 if (x86_dbregs_user_trap()) {
645 x86_dbregs_store_dr6(l);
646 ksi.ksi_code = TRAP_DBREG;
647 } else if (type == (T_BPTFLT|T_USER))
648 ksi.ksi_code = TRAP_BRKPT;
649 else
650 ksi.ksi_code = TRAP_TRACE;
651 (*p->p_emul->e_trapsignal)(l, &ksi);
652 }
653 break;
654 }
655
656 if ((type & T_USER) == 0)
657 return;
658 out:
659 userret(l);
660 return;
661 trapsignal:
662 SIGDEBUG(frame, &ksi, 0);
663 (*p->p_emul->e_trapsignal)(l, &ksi);
664 userret(l);
665 }
666
667 /*
668 * startlwp: start of a new LWP.
669 */
670 void
startlwp(void * arg)671 startlwp(void *arg)
672 {
673 ucontext_t *uc = arg;
674 lwp_t *l = curlwp;
675 int error __diagused;
676
677 error = cpu_setmcontext(l, &uc->uc_mcontext, uc->uc_flags);
678 KASSERT(error == 0);
679
680 kmem_free(uc, sizeof(ucontext_t));
681 userret(l);
682 }
683
684 #ifdef TRAP_SIGDEBUG
685 static void
frame_dump(const struct trapframe * tf,struct pcb * pcb)686 frame_dump(const struct trapframe *tf, struct pcb *pcb)
687 {
688
689 printf("trapframe %p\n", tf);
690 printf("rip %#018lx rsp %#018lx rfl %#018lx\n",
691 tf->tf_rip, tf->tf_rsp, tf->tf_rflags);
692 printf("rdi %#018lx rsi %#018lx rdx %#018lx\n",
693 tf->tf_rdi, tf->tf_rsi, tf->tf_rdx);
694 printf("rcx %#018lx r8 %#018lx r9 %#018lx\n",
695 tf->tf_rcx, tf->tf_r8, tf->tf_r9);
696 printf("r10 %#018lx r11 %#018lx r12 %#018lx\n",
697 tf->tf_r10, tf->tf_r11, tf->tf_r12);
698 printf("r13 %#018lx r14 %#018lx r15 %#018lx\n",
699 tf->tf_r13, tf->tf_r14, tf->tf_r15);
700 printf("rbp %#018lx rbx %#018lx rax %#018lx\n",
701 tf->tf_rbp, tf->tf_rbx, tf->tf_rax);
702 printf("cs %#04lx ds %#04lx es %#04lx "
703 "fs %#04lx gs %#04lx ss %#04lx\n",
704 tf->tf_cs & 0xffff, tf->tf_ds & 0xffff, tf->tf_es & 0xffff,
705 tf->tf_fs & 0xffff, tf->tf_gs & 0xffff, tf->tf_ss & 0xffff);
706 printf("fsbase %#018lx gsbase %#018lx\n", pcb->pcb_fs, pcb->pcb_gs);
707 printf("\n");
708 hexdump(printf, "Stack dump", tf, 256);
709 }
710
711 static void
sigdebug(const struct trapframe * tf,const ksiginfo_t * ksi,int e)712 sigdebug(const struct trapframe *tf, const ksiginfo_t *ksi, int e)
713 {
714 struct lwp *l = curlwp;
715 struct proc *p = l->l_proc;
716
717 printf("pid %d.%d (%s): signal %d code=%d (trap %#lx) "
718 "@rip %#lx addr %#lx error=%d\n",
719 p->p_pid, l->l_lid, p->p_comm, ksi->ksi_signo, ksi->ksi_code,
720 tf->tf_trapno, tf->tf_rip, rcr2(), e);
721 frame_dump(tf, lwp_getpcb(l));
722 }
723 #endif
724