17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59df258acSjohnlev  * Common Development and Distribution License (the "License").
69df258acSjohnlev  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
229df258acSjohnlev  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/errno.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
307c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
317c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
327c478bd9Sstevel@tonic-gate #include <sys/utrap.h>
337c478bd9Sstevel@tonic-gate #include <sys/model.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate int
install_utrap(utrap_entry_t type,utrap_handler_t new_handler,utrap_handler_t * old_handlerp)367c478bd9Sstevel@tonic-gate install_utrap(utrap_entry_t type, utrap_handler_t new_handler,
377c478bd9Sstevel@tonic-gate 	utrap_handler_t *old_handlerp)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	struct proc *p = curthread->t_procp;
407c478bd9Sstevel@tonic-gate 	utrap_handler_t *ov, *nv, *pv, *sv, *tmp;
41aad98a6dSmathue 	caddr32_t nv32;
427c478bd9Sstevel@tonic-gate 	int idx;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	/*
457c478bd9Sstevel@tonic-gate 	 * Check trap number.
467c478bd9Sstevel@tonic-gate 	 */
477c478bd9Sstevel@tonic-gate 	switch (type) {
487c478bd9Sstevel@tonic-gate 	case UTRAP_V8P_FP_DISABLED:
497c478bd9Sstevel@tonic-gate #ifdef SF_ERRATA_30 /* call causes fp-disabled */
507c478bd9Sstevel@tonic-gate 		{
517c478bd9Sstevel@tonic-gate 		extern int spitfire_call_bug;
527c478bd9Sstevel@tonic-gate 
539df258acSjohnlev 		if (spitfire_call_bug)
547c478bd9Sstevel@tonic-gate 			return ((int)set_errno(ENOSYS));
557c478bd9Sstevel@tonic-gate 		}
567c478bd9Sstevel@tonic-gate #endif /* SF_ERRATA_30 */
577c478bd9Sstevel@tonic-gate 		idx = UTRAP_V8P_FP_DISABLED;
587c478bd9Sstevel@tonic-gate 		break;
597c478bd9Sstevel@tonic-gate 	case UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED:
607c478bd9Sstevel@tonic-gate 		idx = UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED;
617c478bd9Sstevel@tonic-gate 		break;
627c478bd9Sstevel@tonic-gate 	default:
637c478bd9Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
647c478bd9Sstevel@tonic-gate 	}
659df258acSjohnlev 	if (get_udatamodel() == DATAMODEL_LP64)
667c478bd9Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/*
69aad98a6dSmathue 	 * Be sure handler address is word aligned.  The uintptr_t casts are
70aad98a6dSmathue 	 * there to prevent warnings when using a certain compiler, and the
71aad98a6dSmathue 	 * temporary 32 bit variable is intended to ensure proper code
72aad98a6dSmathue 	 * generation and avoid a messy quadruple cast.
737c478bd9Sstevel@tonic-gate 	 */
74aad98a6dSmathue 	nv32 = (caddr32_t)(uintptr_t)new_handler;
75aad98a6dSmathue 	nv = (utrap_handler_t *)(uintptr_t)nv32;
767c478bd9Sstevel@tonic-gate 	if (nv != UTRAP_UTH_NOCHANGE) {
777c478bd9Sstevel@tonic-gate 		if (((uintptr_t)nv) & 0x3)
787c478bd9Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate 	/*
817c478bd9Sstevel@tonic-gate 	 * Allocate proc space for saving the addresses to these user
82*75d94465SJosef 'Jeff' Sipek 	 * trap handlers, which must later be freed. Use atomic_cas_ptr to
837c478bd9Sstevel@tonic-gate 	 * do this atomically.
847c478bd9Sstevel@tonic-gate 	 */
857c478bd9Sstevel@tonic-gate 	if (p->p_utraps == NULL) {
867c478bd9Sstevel@tonic-gate 		pv = sv = kmem_zalloc((UT_PRECISE_MAXTRAPS+1) *
877c478bd9Sstevel@tonic-gate 		    sizeof (utrap_handler_t *), KM_SLEEP);
88*75d94465SJosef 'Jeff' Sipek 		tmp = atomic_cas_ptr(&p->p_utraps, NULL, sv);
897c478bd9Sstevel@tonic-gate 		if (tmp != NULL) {
907c478bd9Sstevel@tonic-gate 			kmem_free(pv, (UT_PRECISE_MAXTRAPS+1) *
917c478bd9Sstevel@tonic-gate 			    sizeof (utrap_handler_t *));
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 	}
947c478bd9Sstevel@tonic-gate 	ASSERT(p->p_utraps != NULL);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/*
97*75d94465SJosef 'Jeff' Sipek 	 * Use atomic_cas_ptr to atomically install the handler.
987c478bd9Sstevel@tonic-gate 	 */
997c478bd9Sstevel@tonic-gate 	ov = p->p_utraps[idx];
1007c478bd9Sstevel@tonic-gate 	if (new_handler != (utrap_handler_t)UTRAP_UTH_NOCHANGE) {
1017c478bd9Sstevel@tonic-gate 		for (;;) {
102*75d94465SJosef 'Jeff' Sipek 			tmp = atomic_cas_ptr(&p->p_utraps[idx], ov, nv);
1037c478bd9Sstevel@tonic-gate 			if (ov == tmp)
1047c478bd9Sstevel@tonic-gate 				break;
1057c478bd9Sstevel@tonic-gate 			ov = tmp;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	if (old_handlerp != NULL) {
109aad98a6dSmathue 		if (suword32(old_handlerp, (uint32_t)(uintptr_t)ov) == -1)
1107c478bd9Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	return (0);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate void
utrap_dup(struct proc * pp,struct proc * cp)1167c478bd9Sstevel@tonic-gate utrap_dup(struct proc *pp, struct proc *cp)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	if (pp->p_utraps != NULL) {
1197c478bd9Sstevel@tonic-gate 		cp->p_utraps = kmem_alloc((UT_PRECISE_MAXTRAPS+1) *
1207c478bd9Sstevel@tonic-gate 		    sizeof (utrap_handler_t *), KM_SLEEP);
1217c478bd9Sstevel@tonic-gate 		bcopy(pp->p_utraps, cp->p_utraps,
1227c478bd9Sstevel@tonic-gate 		    (UT_PRECISE_MAXTRAPS+1) * sizeof (utrap_handler_t *));
1237c478bd9Sstevel@tonic-gate 	} else {
1247c478bd9Sstevel@tonic-gate 		cp->p_utraps = NULL;
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate void
utrap_free(struct proc * p)1297c478bd9Sstevel@tonic-gate utrap_free(struct proc *p)
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	/* Free any kmem_alloc'ed space for user trap handlers. */
1327c478bd9Sstevel@tonic-gate 	if (p->p_utraps != NULL) {
1337c478bd9Sstevel@tonic-gate 		kmem_free(p->p_utraps, (UT_PRECISE_MAXTRAPS+1) *
1347c478bd9Sstevel@tonic-gate 		    sizeof (utrap_handler_t *));
1357c478bd9Sstevel@tonic-gate 		p->p_utraps = NULL;
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate /*
1407c478bd9Sstevel@tonic-gate  * The code below supports the set of user traps which are required and
1417c478bd9Sstevel@tonic-gate  * "must be provided by all ABI-conforming implementations", according to
1427c478bd9Sstevel@tonic-gate  * 3.3.3 User Traps of the SPARC V9 ABI SUPPLEMENT, Delta Document 1.38.
1437c478bd9Sstevel@tonic-gate  * There is only 1 deferred trap in Ultra I&II, the asynchronous error
1447c478bd9Sstevel@tonic-gate  * traps, which are not required, so the deferred args are not used.
1457c478bd9Sstevel@tonic-gate  */
1467c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1477c478bd9Sstevel@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)1487c478bd9Sstevel@tonic-gate sparc_utrap_install(utrap_entry_t type,
1497c478bd9Sstevel@tonic-gate 	utrap_handler_t new_precise, utrap_handler_t new_deferred,
1507c478bd9Sstevel@tonic-gate 	utrap_handler_t *old_precise, utrap_handler_t *old_deferred)
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	struct proc *p = curthread->t_procp;
1537c478bd9Sstevel@tonic-gate 	utrap_handler_t *ov, *nvp, *pv, *sv, *tmp;
1547c478bd9Sstevel@tonic-gate 	int idx;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * Check trap number.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	switch (type) {
1607c478bd9Sstevel@tonic-gate 	case UT_ILLTRAP_INSTRUCTION:
1617c478bd9Sstevel@tonic-gate 		idx = UT_ILLTRAP_INSTRUCTION;
1627c478bd9Sstevel@tonic-gate 		break;
1637c478bd9Sstevel@tonic-gate 	case UT_FP_DISABLED:
1647c478bd9Sstevel@tonic-gate #ifdef SF_ERRATA_30 /* call causes fp-disabled */
1657c478bd9Sstevel@tonic-gate 		{
1667c478bd9Sstevel@tonic-gate 		extern int spitfire_call_bug;
1677c478bd9Sstevel@tonic-gate 
1689df258acSjohnlev 		if (spitfire_call_bug)
1697c478bd9Sstevel@tonic-gate 			return ((int)set_errno(ENOSYS));
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate #endif /* SF_ERRATA_30 */
1727c478bd9Sstevel@tonic-gate 		idx = UT_FP_DISABLED;
1737c478bd9Sstevel@tonic-gate 		break;
1747c478bd9Sstevel@tonic-gate 	case UT_FP_EXCEPTION_IEEE_754:
1757c478bd9Sstevel@tonic-gate 		idx = UT_FP_EXCEPTION_IEEE_754;
1767c478bd9Sstevel@tonic-gate 		break;
1777c478bd9Sstevel@tonic-gate 	case UT_TAG_OVERFLOW:
1787c478bd9Sstevel@tonic-gate 		idx = UT_TAG_OVERFLOW;
1797c478bd9Sstevel@tonic-gate 		break;
1807c478bd9Sstevel@tonic-gate 	case UT_DIVISION_BY_ZERO:
1817c478bd9Sstevel@tonic-gate 		idx = UT_DIVISION_BY_ZERO;
1827c478bd9Sstevel@tonic-gate 		break;
1837c478bd9Sstevel@tonic-gate 	case UT_MEM_ADDRESS_NOT_ALIGNED:
1847c478bd9Sstevel@tonic-gate 		idx = UT_MEM_ADDRESS_NOT_ALIGNED;
1857c478bd9Sstevel@tonic-gate 		break;
1867c478bd9Sstevel@tonic-gate 	case UT_PRIVILEGED_ACTION:
1877c478bd9Sstevel@tonic-gate 		idx = UT_PRIVILEGED_ACTION;
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_16:
1907c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_17:
1917c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_18:
1927c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_19:
1937c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_20:
1947c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_21:
1957c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_22:
1967c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_23:
1977c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_24:
1987c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_25:
1997c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_26:
2007c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_27:
2017c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_28:
2027c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_29:
2037c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_30:
2047c478bd9Sstevel@tonic-gate 	case UT_TRAP_INSTRUCTION_31:
2057c478bd9Sstevel@tonic-gate 		idx = type;
2067c478bd9Sstevel@tonic-gate 		break;
2077c478bd9Sstevel@tonic-gate 	default:
2087c478bd9Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
2097c478bd9Sstevel@tonic-gate 	}
2107c478bd9Sstevel@tonic-gate 
2119df258acSjohnlev 	if (get_udatamodel() == DATAMODEL_ILP32)
2127c478bd9Sstevel@tonic-gate 		return ((int)set_errno(EINVAL));
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * Be sure handler address is word aligned.
2167c478bd9Sstevel@tonic-gate 	 * There are no deferred traps, so ignore them.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate 	nvp = (utrap_handler_t *)new_precise;
2197c478bd9Sstevel@tonic-gate 	if (nvp != UTRAP_UTH_NOCHANGE) {
2207c478bd9Sstevel@tonic-gate 		if (((uintptr_t)nvp) & 0x3)
2217c478bd9Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	/*
2257c478bd9Sstevel@tonic-gate 	 * Allocate proc space for saving the addresses to these user
226*75d94465SJosef 'Jeff' Sipek 	 * trap handlers, which must later be freed. Use atomic_cas_ptr to
2277c478bd9Sstevel@tonic-gate 	 * do this atomically.
2287c478bd9Sstevel@tonic-gate 	 */
2297c478bd9Sstevel@tonic-gate 	if (p->p_utraps == NULL) {
2307c478bd9Sstevel@tonic-gate 		pv = sv = kmem_zalloc((UT_PRECISE_MAXTRAPS+1) *
2317c478bd9Sstevel@tonic-gate 		    sizeof (utrap_handler_t *), KM_SLEEP);
232*75d94465SJosef 'Jeff' Sipek 		tmp = atomic_cas_ptr(&p->p_utraps, NULL, sv);
2337c478bd9Sstevel@tonic-gate 		if (tmp != NULL) {
2347c478bd9Sstevel@tonic-gate 			kmem_free(pv, (UT_PRECISE_MAXTRAPS+1) *
2357c478bd9Sstevel@tonic-gate 			    sizeof (utrap_handler_t *));
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	ASSERT(p->p_utraps != NULL);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
241*75d94465SJosef 'Jeff' Sipek 	 * Use atomic_cas_ptr to atomically install the handlers.
2427c478bd9Sstevel@tonic-gate 	 */
2437c478bd9Sstevel@tonic-gate 	ov = p->p_utraps[idx];
2447c478bd9Sstevel@tonic-gate 	if (new_precise != (utrap_handler_t)UTH_NOCHANGE) {
2457c478bd9Sstevel@tonic-gate 		for (;;) {
246*75d94465SJosef 'Jeff' Sipek 			tmp = atomic_cas_ptr(&p->p_utraps[idx], ov, nvp);
2477c478bd9Sstevel@tonic-gate 			if (ov == tmp)
2487c478bd9Sstevel@tonic-gate 				break;
2497c478bd9Sstevel@tonic-gate 			ov = tmp;
2507c478bd9Sstevel@tonic-gate 		}
2517c478bd9Sstevel@tonic-gate 	}
2527c478bd9Sstevel@tonic-gate 	if (old_precise != NULL) {
2537c478bd9Sstevel@tonic-gate 		if (suword64(old_precise, (uint64_t)ov) == -1)
2547c478bd9Sstevel@tonic-gate 			return ((int)set_errno(EINVAL));
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 	return (0);
2577c478bd9Sstevel@tonic-gate }
258