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