xref: /NextBSD/lib/libc/sparc64/sys/__sparc_utrap.c (revision eb1a5f8de9f7ea602c373a710f531abbf81141c4)
1 /*-
2  * Copyright (c) 2001 Jake Burkholder.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 
32 #include <machine/utrap.h>
33 #include <machine/sysarch.h>
34 
35 #include <errno.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include "fpu_extern.h"
43 #include "__sparc_utrap_private.h"
44 
45 extern ssize_t __sys_write(int, const void *, size_t);
46 extern int __sys_kill(pid_t, int);
47 extern pid_t __sys_getpid(void);
48 
49 static const char *utrap_msg[] = {
50 	"reserved",
51 	"instruction access exception",
52 	"instruction access error",
53 	"instruction access protection",
54 	"illtrap instruction",
55 	"illegal instruction",
56 	"privileged opcode",
57 	"floating point disabled",
58 	"floating point exception ieee 754",
59 	"floating point exception other",
60 	"tag overflow",
61 	"division by zero",
62 	"data access exception",
63 	"data access error",
64 	"data access protection",
65 	"memory address not aligned",
66 	"privileged action",
67 	"async data error",
68 	"trap instruction 16",
69 	"trap instruction 17",
70 	"trap instruction 18",
71 	"trap instruction 19",
72 	"trap instruction 20",
73 	"trap instruction 21",
74 	"trap instruction 22",
75 	"trap instruction 23",
76 	"trap instruction 24",
77 	"trap instruction 25",
78 	"trap instruction 26",
79 	"trap instruction 27",
80 	"trap instruction 28",
81 	"trap instruction 29",
82 	"trap instruction 30",
83 	"trap instruction 31",
84 };
85 
86 void
__sparc_utrap(struct utrapframe * uf)87 __sparc_utrap(struct utrapframe *uf)
88 {
89 	int sig;
90 
91 	switch (uf->uf_type) {
92 	case UT_FP_EXCEPTION_IEEE_754:
93 	case UT_FP_EXCEPTION_OTHER:
94 		sig = __fpu_exception(uf);
95 		break;
96 	case UT_ILLEGAL_INSTRUCTION:
97 		sig = __emul_insn(uf);
98 		break;
99 	case UT_MEM_ADDRESS_NOT_ALIGNED:
100 		sig = __unaligned_fixup(uf);
101 		break;
102 	default:
103 		break;
104 	}
105 	if (sig) {
106 		__utrap_write("__sparc_utrap: fatal ");
107 		__utrap_write(utrap_msg[uf->uf_type]);
108 		__utrap_write("\n");
109 		__utrap_kill_self(sig);
110 	}
111 	UF_DONE(uf);
112 }
113 
114 void
__utrap_write(const char * str)115 __utrap_write(const char *str)
116 {
117 	int berrno;
118 
119 	berrno = errno;
120 	__sys_write(STDERR_FILENO, str, strlen(str));
121 	errno = berrno;
122 }
123 
124 void
__utrap_kill_self(int sig)125 __utrap_kill_self(int sig)
126 {
127 	int berrno;
128 
129 	berrno = errno;
130 	__sys_kill(__sys_getpid(), sig);
131 	errno = berrno;
132 }
133 
134 void
__utrap_panic(const char * msg)135 __utrap_panic(const char *msg)
136 {
137 
138 	__utrap_write(msg);
139 	__utrap_write("\n");
140 	__utrap_kill_self(SIGKILL);
141 }
142