1170754Sdelphij/*-
2170754Sdelphij * Copyright (c) 2001 Jake Burkholder.
3170754Sdelphij * All rights reserved.
4170754Sdelphij *
5170754Sdelphij * Redistribution and use in source and binary forms, with or without
6170754Sdelphij * modification, are permitted provided that the following conditions
7170754Sdelphij * are met:
8170754Sdelphij * 1. Redistributions of source code must retain the above copyright
9170754Sdelphij *    notice, this list of conditions and the following disclaimer.
10170754Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11170754Sdelphij *    notice, this list of conditions and the following disclaimer in the
12170754Sdelphij *    documentation and/or other materials provided with the distribution.
13170754Sdelphij *
14170754Sdelphij * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15170754Sdelphij * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16170754Sdelphij * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17170754Sdelphij * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18170754Sdelphij * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19170754Sdelphij * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20170754Sdelphij * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21170754Sdelphij * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22170754Sdelphij * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23170754Sdelphij * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24170754Sdelphij * SUCH DAMAGE.
25170754Sdelphij */
26170754Sdelphij
27170754Sdelphij#include <sys/cdefs.h>
28170754Sdelphij__FBSDID("$FreeBSD: releng/10.3/lib/libc/sparc64/sys/__sparc_utrap.c 188031 2009-02-02 21:51:52Z rdivacky $");
29170754Sdelphij
30170754Sdelphij#include <sys/types.h>
31170754Sdelphij
32170754Sdelphij#include <machine/utrap.h>
33170754Sdelphij#include <machine/sysarch.h>
34170754Sdelphij
35170754Sdelphij#include <errno.h>
36170754Sdelphij#include <signal.h>
37170754Sdelphij#include <stdio.h>
38170754Sdelphij#include <stdlib.h>
39170754Sdelphij#include <string.h>
40170754Sdelphij#include <unistd.h>
41170754Sdelphij
42170754Sdelphij#include "fpu_extern.h"
43170754Sdelphij#include "__sparc_utrap_private.h"
44170754Sdelphij
45170754Sdelphijextern ssize_t __sys_write(int, const void *, size_t);
46170754Sdelphijextern int __sys_kill(pid_t, int);
47170754Sdelphijextern pid_t __sys_getpid(void);
48170754Sdelphij
49170754Sdelphijstatic const char *utrap_msg[] = {
50170754Sdelphij	"reserved",
51170754Sdelphij	"instruction access exception",
52170754Sdelphij	"instruction access error",
53170754Sdelphij	"instruction access protection",
54170754Sdelphij	"illtrap instruction",
55170754Sdelphij	"illegal instruction",
56170754Sdelphij	"privileged opcode",
57170754Sdelphij	"floating point disabled",
58170754Sdelphij	"floating point exception ieee 754",
59170754Sdelphij	"floating point exception other",
60170754Sdelphij	"tag overflow",
61170754Sdelphij	"division by zero",
62170754Sdelphij	"data access exception",
63170754Sdelphij	"data access error",
64170754Sdelphij	"data access protection",
65170754Sdelphij	"memory address not aligned",
66170754Sdelphij	"privileged action",
67170754Sdelphij	"async data error",
68170754Sdelphij	"trap instruction 16",
69170754Sdelphij	"trap instruction 17",
70170754Sdelphij	"trap instruction 18",
71170754Sdelphij	"trap instruction 19",
72170754Sdelphij	"trap instruction 20",
73170754Sdelphij	"trap instruction 21",
74170754Sdelphij	"trap instruction 22",
75170754Sdelphij	"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
86void
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
114void
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
124void
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
134void
135__utrap_panic(const char *msg)
136{
137
138	__utrap_write(msg);
139	__utrap_write("\n");
140	__utrap_kill_self(SIGKILL);
141}
142