188794Sjake/*-
288794Sjake * Copyright (c) 2001 Jake Burkholder.
388794Sjake * All rights reserved.
488794Sjake *
588794Sjake * Redistribution and use in source and binary forms, with or without
688794Sjake * modification, are permitted provided that the following conditions
788794Sjake * are met:
888794Sjake * 1. Redistributions of source code must retain the above copyright
988794Sjake *    notice, this list of conditions and the following disclaimer.
1088794Sjake * 2. Redistributions in binary form must reproduce the above copyright
1188794Sjake *    notice, this list of conditions and the following disclaimer in the
1288794Sjake *    documentation and/or other materials provided with the distribution.
1388794Sjake *
1488794Sjake * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1588794Sjake * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1688794Sjake * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1788794Sjake * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1888794Sjake * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1988794Sjake * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2088794Sjake * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2188794Sjake * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2288794Sjake * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2388794Sjake * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2488794Sjake * SUCH DAMAGE.
2588794Sjake */
2688794Sjake
2788794Sjake#include <sys/cdefs.h>
2888794Sjake__FBSDID("$FreeBSD$");
2988794Sjake
3088794Sjake#include <sys/types.h>
3188794Sjake
3288794Sjake#include <machine/utrap.h>
3388794Sjake#include <machine/sysarch.h>
3488794Sjake
3595587Sjake#include <errno.h>
3695587Sjake#include <signal.h>
3788794Sjake#include <stdio.h>
3888794Sjake#include <stdlib.h>
39124182Snectar#include <string.h>
4095587Sjake#include <unistd.h>
4188794Sjake
42124182Snectar#include "fpu_extern.h"
4388794Sjake#include "__sparc_utrap_private.h"
4488794Sjake
45124722Snectarextern ssize_t __sys_write(int, const void *, size_t);
46124722Snectarextern int __sys_kill(pid_t, int);
47124722Snectarextern pid_t __sys_getpid(void);
48124722Snectar
4988794Sjakestatic const char *utrap_msg[] = {
5088794Sjake	"reserved",
5188794Sjake	"instruction access exception",
5288794Sjake	"instruction access error",
5388794Sjake	"instruction access protection",
5488794Sjake	"illtrap instruction",
5588794Sjake	"illegal instruction",
5688794Sjake	"privileged opcode",
5788794Sjake	"floating point disabled",
5888794Sjake	"floating point exception ieee 754",
5988794Sjake	"floating point exception other",
6088794Sjake	"tag overflow",
6188794Sjake	"division by zero",
6288794Sjake	"data access exception",
6388794Sjake	"data access error",
6488794Sjake	"data access protection",
6588794Sjake	"memory address not aligned",
6688794Sjake	"privileged action",
6788794Sjake	"async data error",
6888794Sjake	"trap instruction 16",
6988794Sjake	"trap instruction 17",
7088794Sjake	"trap instruction 18",
7188794Sjake	"trap instruction 19",
7288794Sjake	"trap instruction 20",
7388794Sjake	"trap instruction 21",
7488794Sjake	"trap instruction 22",
7588794Sjake	"trap instruction 23",
7688794Sjake	"trap instruction 24",
7788794Sjake	"trap instruction 25",
7888794Sjake	"trap instruction 26",
7988794Sjake	"trap instruction 27",
8088794Sjake	"trap instruction 28",
8188794Sjake	"trap instruction 29",
8288794Sjake	"trap instruction 30",
8388794Sjake	"trap instruction 31",
8488794Sjake};
8588794Sjake
8688794Sjakevoid
8788794Sjake__sparc_utrap(struct utrapframe *uf)
8888794Sjake{
8995587Sjake	int sig;
9088794Sjake
9188794Sjake	switch (uf->uf_type) {
9288794Sjake	case UT_FP_EXCEPTION_IEEE_754:
9388794Sjake	case UT_FP_EXCEPTION_OTHER:
9495587Sjake		sig = __fpu_exception(uf);
9595587Sjake		break;
9688794Sjake	case UT_ILLEGAL_INSTRUCTION:
9795587Sjake		sig = __emul_insn(uf);
9895587Sjake		break;
9988794Sjake	case UT_MEM_ADDRESS_NOT_ALIGNED:
10096492Sjake		sig = __unaligned_fixup(uf);
10188794Sjake		break;
10288794Sjake	default:
10388794Sjake		break;
10488794Sjake	}
10595587Sjake	if (sig) {
10695587Sjake		__utrap_write("__sparc_utrap: fatal ");
10795587Sjake		__utrap_write(utrap_msg[uf->uf_type]);
10895587Sjake		__utrap_write("\n");
10995587Sjake		__utrap_kill_self(sig);
11095587Sjake	}
11195587Sjake	UF_DONE(uf);
11288794Sjake}
11395587Sjake
11495587Sjakevoid
11595587Sjake__utrap_write(const char *str)
11695587Sjake{
11795587Sjake	int berrno;
11895587Sjake
11995587Sjake	berrno = errno;
12095587Sjake	__sys_write(STDERR_FILENO, str, strlen(str));
12195587Sjake	errno = berrno;
12295587Sjake}
12395587Sjake
12495587Sjakevoid
125188031Srdivacky__utrap_kill_self(int sig)
12695587Sjake{
12795587Sjake	int berrno;
12895587Sjake
12995587Sjake	berrno = errno;
13095587Sjake	__sys_kill(__sys_getpid(), sig);
13195587Sjake	errno = berrno;
13295587Sjake}
13395587Sjake
13495587Sjakevoid
13595587Sjake__utrap_panic(const char *msg)
13695587Sjake{
13795587Sjake
13895587Sjake	__utrap_write(msg);
13995587Sjake	__utrap_write("\n");
14095587Sjake	__utrap_kill_self(SIGKILL);
14195587Sjake}
142