xref: /onnv-gate/usr/src/uts/sparc/v9/syscall/install_utrap.c (revision 2778:188aca97d7fb)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2778Sjohnlev  * Common Development and Distribution License (the "License").
6*2778Sjohnlev  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2778Sjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/errno.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/atomic.h>
320Sstevel@tonic-gate #include <sys/kmem.h>
330Sstevel@tonic-gate #include <sys/machpcb.h>
340Sstevel@tonic-gate #include <sys/utrap.h>
350Sstevel@tonic-gate #include <sys/model.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate int
install_utrap(utrap_entry_t type,utrap_handler_t new_handler,utrap_handler_t * old_handlerp)380Sstevel@tonic-gate install_utrap(utrap_entry_t type, utrap_handler_t new_handler,
390Sstevel@tonic-gate 	utrap_handler_t *old_handlerp)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	struct proc *p = curthread->t_procp;
420Sstevel@tonic-gate 	utrap_handler_t *ov, *nv, *pv, *sv, *tmp;
431030Smathue 	caddr32_t nv32;
440Sstevel@tonic-gate 	int idx;
450Sstevel@tonic-gate 
460Sstevel@tonic-gate 	/*
470Sstevel@tonic-gate 	 * Check trap number.
480Sstevel@tonic-gate 	 */
490Sstevel@tonic-gate 	switch (type) {
500Sstevel@tonic-gate 	case UTRAP_V8P_FP_DISABLED:
510Sstevel@tonic-gate #ifdef SF_ERRATA_30 /* call causes fp-disabled */
520Sstevel@tonic-gate 		{
530Sstevel@tonic-gate 		extern int spitfire_call_bug;
540Sstevel@tonic-gate 
55*2778Sjohnlev 		if (spitfire_call_bug)
560Sstevel@tonic-gate 			return ((int)set_errno(ENOSYS));
570Sstevel@tonic-gate 		}
580Sstevel@tonic-gate #endif /* SF_ERRATA_30 */
590Sstevel@tonic-gate 		idx = UTRAP_V8P_FP_DISABLED;
600Sstevel@tonic-gate 		break;
610Sstevel@tonic-gate 	case UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED:
620Sstevel@tonic-gate 		idx = UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED;
630Sstevel@tonic-gate 		break;
640Sstevel@tonic-gate 	default:
650Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
660Sstevel@tonic-gate 	}
67*2778Sjohnlev 	if (get_udatamodel() == DATAMODEL_LP64)
680Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	/*
711030Smathue 	 * Be sure handler address is word aligned.  The uintptr_t casts are
721030Smathue 	 * there to prevent warnings when using a certain compiler, and the
731030Smathue 	 * temporary 32 bit variable is intended to ensure proper code
741030Smathue 	 * generation and avoid a messy quadruple cast.
750Sstevel@tonic-gate 	 */
761030Smathue 	nv32 = (caddr32_t)(uintptr_t)new_handler;
771030Smathue 	nv = (utrap_handler_t *)(uintptr_t)nv32;
780Sstevel@tonic-gate 	if (nv != UTRAP_UTH_NOCHANGE) {
790Sstevel@tonic-gate 		if (((uintptr_t)nv) & 0x3)
800Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 	/*
830Sstevel@tonic-gate 	 * Allocate proc space for saving the addresses to these user
840Sstevel@tonic-gate 	 * trap handlers, which must later be freed. Use casptr to
850Sstevel@tonic-gate 	 * do this atomically.
860Sstevel@tonic-gate 	 */
870Sstevel@tonic-gate 	if (p->p_utraps == NULL) {
880Sstevel@tonic-gate 		pv = sv = kmem_zalloc((UT_PRECISE_MAXTRAPS+1) *
890Sstevel@tonic-gate 		    sizeof (utrap_handler_t *), KM_SLEEP);
900Sstevel@tonic-gate 		tmp = casptr(&p->p_utraps, NULL, sv);
910Sstevel@tonic-gate 		if (tmp != NULL) {
920Sstevel@tonic-gate 			kmem_free(pv, (UT_PRECISE_MAXTRAPS+1) *
930Sstevel@tonic-gate 			    sizeof (utrap_handler_t *));
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	ASSERT(p->p_utraps != NULL);
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * Use casptr to atomically install the handler.
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	ov = p->p_utraps[idx];
1020Sstevel@tonic-gate 	if (new_handler != (utrap_handler_t)UTRAP_UTH_NOCHANGE) {
1030Sstevel@tonic-gate 		for (;;) {
1040Sstevel@tonic-gate 			tmp = casptr(&p->p_utraps[idx], ov, nv);
1050Sstevel@tonic-gate 			if (ov == tmp)
1060Sstevel@tonic-gate 				break;
1070Sstevel@tonic-gate 			ov = tmp;
1080Sstevel@tonic-gate 		}
1090Sstevel@tonic-gate 	}
1100Sstevel@tonic-gate 	if (old_handlerp != NULL) {
1111030Smathue 		if (suword32(old_handlerp, (uint32_t)(uintptr_t)ov) == -1)
1120Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	return (0);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate void
utrap_dup(struct proc * pp,struct proc * cp)1180Sstevel@tonic-gate utrap_dup(struct proc *pp, struct proc *cp)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	if (pp->p_utraps != NULL) {
1210Sstevel@tonic-gate 		cp->p_utraps = kmem_alloc((UT_PRECISE_MAXTRAPS+1) *
1220Sstevel@tonic-gate 		    sizeof (utrap_handler_t *), KM_SLEEP);
1230Sstevel@tonic-gate 		bcopy(pp->p_utraps, cp->p_utraps,
1240Sstevel@tonic-gate 		    (UT_PRECISE_MAXTRAPS+1) * sizeof (utrap_handler_t *));
1250Sstevel@tonic-gate 	} else {
1260Sstevel@tonic-gate 		cp->p_utraps = NULL;
1270Sstevel@tonic-gate 	}
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate void
utrap_free(struct proc * p)1310Sstevel@tonic-gate utrap_free(struct proc *p)
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	/* Free any kmem_alloc'ed space for user trap handlers. */
1340Sstevel@tonic-gate 	if (p->p_utraps != NULL) {
1350Sstevel@tonic-gate 		kmem_free(p->p_utraps, (UT_PRECISE_MAXTRAPS+1) *
1360Sstevel@tonic-gate 		    sizeof (utrap_handler_t *));
1370Sstevel@tonic-gate 		p->p_utraps = NULL;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate }
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate /*
1420Sstevel@tonic-gate  * The code below supports the set of user traps which are required and
1430Sstevel@tonic-gate  * "must be provided by all ABI-conforming implementations", according to
1440Sstevel@tonic-gate  * 3.3.3 User Traps of the SPARC V9 ABI SUPPLEMENT, Delta Document 1.38.
1450Sstevel@tonic-gate  * There is only 1 deferred trap in Ultra I&II, the asynchronous error
1460Sstevel@tonic-gate  * traps, which are not required, so the deferred args are not used.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate /*ARGSUSED*/
1490Sstevel@tonic-gate int
sparc_utrap_install(utrap_entry_t type,utrap_handler_t new_precise,utrap_handler_t new_deferred,utrap_handler_t * old_precise,utrap_handler_t * old_deferred)1500Sstevel@tonic-gate sparc_utrap_install(utrap_entry_t type,
1510Sstevel@tonic-gate 	utrap_handler_t new_precise, utrap_handler_t new_deferred,
1520Sstevel@tonic-gate 	utrap_handler_t *old_precise, utrap_handler_t *old_deferred)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	struct proc *p = curthread->t_procp;
1550Sstevel@tonic-gate 	utrap_handler_t *ov, *nvp, *pv, *sv, *tmp;
1560Sstevel@tonic-gate 	int idx;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/*
1590Sstevel@tonic-gate 	 * Check trap number.
1600Sstevel@tonic-gate 	 */
1610Sstevel@tonic-gate 	switch (type) {
1620Sstevel@tonic-gate 	case UT_ILLTRAP_INSTRUCTION:
1630Sstevel@tonic-gate 		idx = UT_ILLTRAP_INSTRUCTION;
1640Sstevel@tonic-gate 		break;
1650Sstevel@tonic-gate 	case UT_FP_DISABLED:
1660Sstevel@tonic-gate #ifdef SF_ERRATA_30 /* call causes fp-disabled */
1670Sstevel@tonic-gate 		{
1680Sstevel@tonic-gate 		extern int spitfire_call_bug;
1690Sstevel@tonic-gate 
170*2778Sjohnlev 		if (spitfire_call_bug)
1710Sstevel@tonic-gate 			return ((int)set_errno(ENOSYS));
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate #endif /* SF_ERRATA_30 */
1740Sstevel@tonic-gate 		idx = UT_FP_DISABLED;
1750Sstevel@tonic-gate 		break;
1760Sstevel@tonic-gate 	case UT_FP_EXCEPTION_IEEE_754:
1770Sstevel@tonic-gate 		idx = UT_FP_EXCEPTION_IEEE_754;
1780Sstevel@tonic-gate 		break;
1790Sstevel@tonic-gate 	case UT_TAG_OVERFLOW:
1800Sstevel@tonic-gate 		idx = UT_TAG_OVERFLOW;
1810Sstevel@tonic-gate 		break;
1820Sstevel@tonic-gate 	case UT_DIVISION_BY_ZERO:
1830Sstevel@tonic-gate 		idx = UT_DIVISION_BY_ZERO;
1840Sstevel@tonic-gate 		break;
1850Sstevel@tonic-gate 	case UT_MEM_ADDRESS_NOT_ALIGNED:
1860Sstevel@tonic-gate 		idx = UT_MEM_ADDRESS_NOT_ALIGNED;
1870Sstevel@tonic-gate 		break;
1880Sstevel@tonic-gate 	case UT_PRIVILEGED_ACTION:
1890Sstevel@tonic-gate 		idx = UT_PRIVILEGED_ACTION;
1900Sstevel@tonic-gate 		break;
1910Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_16:
1920Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_17:
1930Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_18:
1940Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_19:
1950Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_20:
1960Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_21:
1970Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_22:
1980Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_23:
1990Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_24:
2000Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_25:
2010Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_26:
2020Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_27:
2030Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_28:
2040Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_29:
2050Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_30:
2060Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_31:
2070Sstevel@tonic-gate 		idx = type;
2080Sstevel@tonic-gate 		break;
2090Sstevel@tonic-gate 	default:
2100Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 
213*2778Sjohnlev 	if (get_udatamodel() == DATAMODEL_ILP32)
2140Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2170Sstevel@tonic-gate 	 * Be sure handler address is word aligned.
2180Sstevel@tonic-gate 	 * There are no deferred traps, so ignore them.
2190Sstevel@tonic-gate 	 */
2200Sstevel@tonic-gate 	nvp = (utrap_handler_t *)new_precise;
2210Sstevel@tonic-gate 	if (nvp != UTRAP_UTH_NOCHANGE) {
2220Sstevel@tonic-gate 		if (((uintptr_t)nvp) & 0x3)
2230Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	/*
2270Sstevel@tonic-gate 	 * Allocate proc space for saving the addresses to these user
2280Sstevel@tonic-gate 	 * trap handlers, which must later be freed. Use casptr to
2290Sstevel@tonic-gate 	 * do this atomically.
2300Sstevel@tonic-gate 	 */
2310Sstevel@tonic-gate 	if (p->p_utraps == NULL) {
2320Sstevel@tonic-gate 		pv = sv = kmem_zalloc((UT_PRECISE_MAXTRAPS+1) *
2330Sstevel@tonic-gate 		    sizeof (utrap_handler_t *), KM_SLEEP);
2340Sstevel@tonic-gate 		tmp = casptr(&p->p_utraps, NULL, sv);
2350Sstevel@tonic-gate 		if (tmp != NULL) {
2360Sstevel@tonic-gate 			kmem_free(pv, (UT_PRECISE_MAXTRAPS+1) *
2370Sstevel@tonic-gate 			    sizeof (utrap_handler_t *));
2380Sstevel@tonic-gate 		}
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 	ASSERT(p->p_utraps != NULL);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Use casptr to atomically install the handlers.
2440Sstevel@tonic-gate 	 */
2450Sstevel@tonic-gate 	ov = p->p_utraps[idx];
2460Sstevel@tonic-gate 	if (new_precise != (utrap_handler_t)UTH_NOCHANGE) {
2470Sstevel@tonic-gate 		for (;;) {
2480Sstevel@tonic-gate 			tmp = casptr(&p->p_utraps[idx], ov, nvp);
2490Sstevel@tonic-gate 			if (ov == tmp)
2500Sstevel@tonic-gate 				break;
2510Sstevel@tonic-gate 			ov = tmp;
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	if (old_precise != NULL) {
2550Sstevel@tonic-gate 		if (suword64(old_precise, (uint64_t)ov) == -1)
2560Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 	return (0);
2590Sstevel@tonic-gate }
260