Merge from vendor branch GCC:
[dragonfly.git] / sys / platform / vkernel / i386 / exception.c
1
2 /*
3  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
4  * 
5  * This code is derived from software contributed to The DragonFly Project
6  * by Matthew Dillon <dillon@backplane.com>
7  * 
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 
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
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  * 
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  * 
35  * $DragonFly: src/sys/platform/vkernel/i386/exception.c,v 1.3 2007/01/08 08:17:15 dillon Exp $
36  */
37
38 #include "opt_ddb.h"
39 #include <sys/types.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <ddb/ddb.h>
43 #include <machine/trap.h>
44 #include <machine/md_var.h>
45 #include <machine/segments.h>
46 #include <signal.h>
47
48 int _ucodesel = LSEL(LUCODE_SEL, SEL_UPL);
49 int _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
50
51 static void exc_segfault(int signo, siginfo_t *info, void *ctx);
52 #ifdef DDB
53 static void exc_debugger(int signo, siginfo_t *info, void *ctx);
54 #endif
55
56 void
57 init_exceptions(void)
58 {
59         struct sigaction sa;
60
61         bzero(&sa, sizeof(sa));
62         sa.sa_sigaction = exc_segfault;
63         sa.sa_flags |= SA_SIGINFO;
64         sigemptyset(&sa.sa_mask);
65         sigaction(SIGSEGV, &sa, NULL);
66         sigaction(SIGTRAP, &sa, NULL);
67 #ifdef DDB
68         sa.sa_sigaction = exc_debugger;
69         sigaction(SIGQUIT, &sa, NULL);
70 #endif
71 }
72
73 /*
74  * This function handles a segmentation fault.  
75  *
76  * XXX We assume that trapframe is a subset of ucontext.  It is as of
77  *     this writing.
78  */
79 static void
80 exc_segfault(int signo, siginfo_t *info, void *ctxp)
81 {
82         ucontext_t *ctx = ctxp;
83
84 #if 0
85         kprintf("CAUGHT SEGFAULT EIP %08x ERR %08x TRAPNO %d err %d\n",
86                 ctx->uc_mcontext.mc_eip,
87                 ctx->uc_mcontext.mc_err,
88                 ctx->uc_mcontext.mc_trapno & 0xFFFF,
89                 ctx->uc_mcontext.mc_trapno >> 16);
90 #endif
91         kern_trap((struct trapframe *)&ctx->uc_mcontext.mc_gs);
92         splz();
93 }
94
95 #ifdef DDB
96
97 static void
98 exc_debugger(int signo, siginfo_t *info, void *ctx)
99 {
100         Debugger("interrupt from console");
101 }
102
103 #endif