summaryrefslogtreecommitdiff
path: root/usr/src/uts/sparc/v9/syscall/install_utrap.c
blob: ba6c98598c244c32f2f9c5e3dd336a9dd982bb40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <sys/errno.h>
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/kmem.h>
#include <sys/machpcb.h>
#include <sys/utrap.h>
#include <sys/model.h>

int
install_utrap(utrap_entry_t type, utrap_handler_t new_handler,
	utrap_handler_t *old_handlerp)
{
	struct proc *p = curthread->t_procp;
	utrap_handler_t *ov, *nv, *pv, *sv, *tmp;
	caddr32_t nv32;
	int idx;

	/*
	 * Check trap number.
	 */
	switch (type) {
	case UTRAP_V8P_FP_DISABLED:
#ifdef SF_ERRATA_30 /* call causes fp-disabled */
		{
		extern int spitfire_call_bug;

		if (spitfire_call_bug)
			return ((int)set_errno(ENOSYS));
		}
#endif /* SF_ERRATA_30 */
		idx = UTRAP_V8P_FP_DISABLED;
		break;
	case UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED:
		idx = UTRAP_V8P_MEM_ADDRESS_NOT_ALIGNED;
		break;
	default:
		return ((int)set_errno(EINVAL));
	}
	if (get_udatamodel() == DATAMODEL_LP64)
		return ((int)set_errno(EINVAL));

	/*
	 * Be sure handler address is word aligned.  The uintptr_t casts are
	 * there to prevent warnings when using a certain compiler, and the
	 * temporary 32 bit variable is intended to ensure proper code
	 * generation and avoid a messy quadruple cast.
	 */
	nv32 = (caddr32_t)(uintptr_t)new_handler;
	nv = (utrap_handler_t *)(uintptr_t)nv32;
	if (nv != UTRAP_UTH_NOCHANGE) {
		if (((uintptr_t)nv) & 0x3)
			return ((int)set_errno(EINVAL));
	}
	/*
	 * Allocate proc space for saving the addresses to these user
	 * trap handlers, which must later be freed. Use atomic_cas_ptr to
	 * do this atomically.
	 */
	if (p->p_utraps == NULL) {
		pv = sv = kmem_zalloc((UT_PRECISE_MAXTRAPS+1) *
		    sizeof (utrap_handler_t *), KM_SLEEP);
		tmp = atomic_cas_ptr(&p->p_utraps, NULL, sv);
		if (tmp != NULL) {
			kmem_free(pv, (UT_PRECISE_MAXTRAPS+1) *
			    sizeof (utrap_handler_t *));
		}
	}
	ASSERT(p->p_utraps != NULL);

	/*
	 * Use atomic_cas_ptr to atomically install the handler.
	 */
	ov = p->p_utraps[idx];
	if (new_handler != (utrap_handler_t)UTRAP_UTH_NOCHANGE) {
		for (;;) {
			tmp = atomic_cas_ptr(&p->p_utraps[idx], ov, nv);
			if (ov == tmp)
				break;
			ov = tmp;
		}
	}
	if (old_handlerp != NULL) {
		if (suword32(old_handlerp, (uint32_t)(uintptr_t)ov) == -1)
			return ((int)set_errno(EINVAL));
	}
	return (0);
}

void
utrap_dup(struct proc *pp, struct proc *cp)
{
	if (pp->p_utraps != NULL) {
		cp->p_utraps = kmem_alloc((UT_PRECISE_MAXTRAPS+1) *
		    sizeof (utrap_handler_t *), KM_SLEEP);
		bcopy(pp->p_utraps, cp->p_utraps,
		    (UT_PRECISE_MAXTRAPS+1) * sizeof (utrap_handler_t *));
	} else {
		cp->p_utraps = NULL;
	}
}

void
utrap_free(struct proc *p)
{
	/* Free any kmem_alloc'ed space for user trap handlers. */
	if (p->p_utraps != NULL) {
		kmem_free(p->p_utraps, (UT_PRECISE_MAXTRAPS+1) *
		    sizeof (utrap_handler_t *));
		p->p_utraps = NULL;
	}
}

/*
 * The code below supports the set of user traps which are required and
 * "must be provided by all ABI-conforming implementations", according to
 * 3.3.3 User Traps of the SPARC V9 ABI SUPPLEMENT, Delta Document 1.38.
 * There is only 1 deferred trap in Ultra I&II, the asynchronous error
 * traps, which are not required, so the deferred args are not used.
 */
/*ARGSUSED*/
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)
{
	struct proc *p = curthread->t_procp;
	utrap_handler_t *ov, *nvp, *pv, *sv, *tmp;
	int idx;

	/*
	 * Check trap number.
	 */
	switch (type) {
	case UT_ILLTRAP_INSTRUCTION:
		idx = UT_ILLTRAP_INSTRUCTION;
		break;
	case UT_FP_DISABLED:
#ifdef SF_ERRATA_30 /* call causes fp-disabled */
		{
		extern int spitfire_call_bug;

		if (spitfire_call_bug)
			return ((int)set_errno(ENOSYS));
		}
#endif /* SF_ERRATA_30 */
		idx = UT_FP_DISABLED;
		break;
	case UT_FP_EXCEPTION_IEEE_754:
		idx = UT_FP_EXCEPTION_IEEE_754;
		break;
	case UT_TAG_OVERFLOW:
		idx = UT_TAG_OVERFLOW;
		break;
	case UT_DIVISION_BY_ZERO:
		idx = UT_DIVISION_BY_ZERO;
		break;
	case UT_MEM_ADDRESS_NOT_ALIGNED:
		idx = UT_MEM_ADDRESS_NOT_ALIGNED;
		break;
	case UT_PRIVILEGED_ACTION:
		idx = UT_PRIVILEGED_ACTION;
		break;
	case UT_TRAP_INSTRUCTION_16:
	case UT_TRAP_INSTRUCTION_17:
	case UT_TRAP_INSTRUCTION_18:
	case UT_TRAP_INSTRUCTION_19:
	case UT_TRAP_INSTRUCTION_20:
	case UT_TRAP_INSTRUCTION_21:
	case UT_TRAP_INSTRUCTION_22:
	case UT_TRAP_INSTRUCTION_23:
	case UT_TRAP_INSTRUCTION_24:
	case UT_TRAP_INSTRUCTION_25:
	case UT_TRAP_INSTRUCTION_26:
	case UT_TRAP_INSTRUCTION_27:
	case UT_TRAP_INSTRUCTION_28:
	case UT_TRAP_INSTRUCTION_29:
	case UT_TRAP_INSTRUCTION_30:
	case UT_TRAP_INSTRUCTION_31:
		idx = type;
		break;
	default:
		return ((int)set_errno(EINVAL));
	}

	if (get_udatamodel() == DATAMODEL_ILP32)
		return ((int)set_errno(EINVAL));

	/*
	 * Be sure handler address is word aligned.
	 * There are no deferred traps, so ignore them.
	 */
	nvp = (utrap_handler_t *)new_precise;
	if (nvp != UTRAP_UTH_NOCHANGE) {
		if (((uintptr_t)nvp) & 0x3)
			return ((int)set_errno(EINVAL));
	}

	/*
	 * Allocate proc space for saving the addresses to these user
	 * trap handlers, which must later be freed. Use atomic_cas_ptr to
	 * do this atomically.
	 */
	if (p->p_utraps == NULL) {
		pv = sv = kmem_zalloc((UT_PRECISE_MAXTRAPS+1) *
		    sizeof (utrap_handler_t *), KM_SLEEP);
		tmp = atomic_cas_ptr(&p->p_utraps, NULL, sv);
		if (tmp != NULL) {
			kmem_free(pv, (UT_PRECISE_MAXTRAPS+1) *
			    sizeof (utrap_handler_t *));
		}
	}
	ASSERT(p->p_utraps != NULL);

	/*
	 * Use atomic_cas_ptr to atomically install the handlers.
	 */
	ov = p->p_utraps[idx];
	if (new_precise != (utrap_handler_t)UTH_NOCHANGE) {
		for (;;) {
			tmp = atomic_cas_ptr(&p->p_utraps[idx], ov, nvp);
			if (ov == tmp)
				break;
			ov = tmp;
		}
	}
	if (old_precise != NULL) {
		if (suword64(old_precise, (uint64_t)ov) == -1)
			return ((int)set_errno(EINVAL));
	}
	return (0);
}