summaryrefslogtreecommitdiffstats
path: root/sys/arch/powerpc64/powerpc64/trap.c
blob: 46eecf2d8aa202ca9b93d22ad231b7a30e76362e (plain) (blame)
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*	$OpenBSD: trap.c,v 1.26 2020/07/10 18:34:24 kettenis Exp $	*/

/*
 * Copyright (c) 2020 Mark Kettenis <kettenis@openbsd.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/param.h>
#include <sys/proc.h>
#include <sys/signalvar.h>
#include <sys/user.h>
#include <sys/syscall.h>
#include <sys/syscall_mi.h>
#include <sys/systm.h>

#include <uvm/uvm_extern.h>

#include <machine/fpu.h>
#include <machine/pte.h>
#include <machine/trap.h>

#ifdef DDB
#include <machine/db_machdep.h>
#endif

void	decr_intr(struct trapframe *); /* clock.c */
void	hvi_intr(struct trapframe *);  /* intr.c */
void	syscall(struct trapframe *);   /* syscall.c */

void	dumpframe(struct trapframe *);

void
trap(struct trapframe *frame)
{
	struct cpu_info *ci = curcpu();
	struct proc *p = curproc;
	int type = frame->exc;
	union sigval sv;
	struct vm_map *map;
	struct slb_desc *slbd;
	pmap_t pm;
	vaddr_t va;
	int ftype;
	int error, sig, code;

	/* Disable access to floating-point and vector registers. */
	mtmsr(mfmsr() & ~(PSL_FP|PSL_VEC|PSL_VSX));

	switch (type) {
	case EXC_DECR:
		uvmexp.intrs++;
		ci->ci_idepth++;
		decr_intr(frame);
		ci->ci_idepth--;
		return;
	case EXC_HVI:
		uvmexp.intrs++;
		ci->ci_idepth++;
		hvi_intr(frame);
		ci->ci_idepth--;
		return;
	case EXC_SC:
		uvmexp.syscalls++;
		break;
	default:
		uvmexp.traps++;
		break;
	}

	if (frame->srr1 & PSL_EE)
		intr_enable();

	if (frame->srr1 & PSL_PR) {
		type |= EXC_USER;
		p->p_md.md_regs = frame;
		refreshcreds(p);
	}

	switch (type) {
#ifdef DDB
	case EXC_PGM:
		/* At a trap instruction, enter the debugger. */
		if (frame->srr1 & EXC_PGM_TRAP) {
			/* Return from db_enter(). */
			if (frame->srr0 == (register_t)db_enter)
				frame->srr0 = frame->lr;
			db_ktrap(T_BREAKPOINT, frame);
			return;
		}
		break;
	case EXC_TRC:
		db_ktrap(T_BREAKPOINT, frame); /* single-stepping */
		return;
#endif

	case EXC_DSI:
		map = kernel_map;
		va = frame->dar;
		if (curpcb->pcb_onfault &&
		    (va >> ADDR_ESID_SHIFT) == USER_ESID) {
			map = &p->p_vmspace->vm_map;
			va = curpcb->pcb_userva | (va & SEGMENT_MASK);
		}
		if (frame->dsisr & DSISR_STORE)
			ftype = PROT_READ | PROT_WRITE;
		else
			ftype = PROT_READ;
		KERNEL_LOCK();
		if (uvm_fault(map, trunc_page(va), 0, ftype) == 0) {
			KERNEL_UNLOCK();
			return;
		}
		KERNEL_UNLOCK();

		if (curpcb->pcb_onfault) {
			frame->srr0 = curpcb->pcb_onfault;
			return;
		}

		printf("dar 0x%lx dsisr 0x%lx\n", frame->dar, frame->dsisr);
		goto fatal;

	case EXC_DSE:
		/*
		 * If we sleep while handling a fault, we may lose our
		 * SLB entry.  Enter it again.
		 */
		va = frame->dar;
		if (curpcb->pcb_onfault &&
		    (va >> ADDR_ESID_SHIFT) == USER_ESID) {
			map = &p->p_vmspace->vm_map;
			va = curpcb->pcb_userva | (va & SEGMENT_MASK);
			if (pmap_set_user_slb(map->pmap, va, NULL, NULL) == 0)
				return;
		}

		if (curpcb->pcb_onfault) {
			frame->srr0 = curpcb->pcb_onfault;
			return;
		}

		printf("dar 0x%lx dsisr 0x%lx\n", frame->dar, frame->dsisr);
		goto fatal;

	case EXC_DSE|EXC_USER:
		pm = p->p_vmspace->vm_map.pmap;
		slbd = pmap_slbd_lookup(pm, frame->dar);
		if (slbd) {
			pmap_slbd_cache(pm, slbd);
			break;
		}
		frame->dsisr = 0;
		/* FALLTHROUGH */

	case EXC_DSI|EXC_USER:
		map = &p->p_vmspace->vm_map;
		va = frame->dar;
		if (frame->dsisr & DSISR_STORE)
			ftype = PROT_READ | PROT_WRITE;
		else
			ftype = PROT_READ;
		KERNEL_LOCK();
		error = uvm_fault(map, trunc_page(va), 0, ftype);
		KERNEL_UNLOCK();
		if (error) {
			printf("type %x dar 0x%lx dsisr 0x%lx %s\r\n",
			    type, frame->dar, frame->dsisr, p->p_p->ps_comm);
			dumpframe(frame);

			if (error == ENOMEM) {
				sig = SIGKILL;
				code = 0;
			} else if (error == EIO) {
				sig = SIGBUS;
				code = BUS_OBJERR;
			} else if (error == EACCES) {
				sig = SIGSEGV;
				code = SEGV_ACCERR;
			} else {
				sig = SIGSEGV;
				code = SEGV_MAPERR;
			}
			sv.sival_ptr = (void *)va;
			KERNEL_LOCK();
			trapsignal(p, sig, 0, code, sv);
			KERNEL_UNLOCK();
		}
		break;

	case EXC_ISE|EXC_USER:
		pm = p->p_vmspace->vm_map.pmap;
		slbd = pmap_slbd_lookup(pm, frame->srr0);
		if (slbd) {
			pmap_slbd_cache(pm, slbd);
			break;
		}
		/* FALLTHROUGH */

	case EXC_ISI|EXC_USER:
		map = &p->p_vmspace->vm_map;
		va = frame->srr0;
		ftype = PROT_READ | PROT_EXEC;
		KERNEL_LOCK();
		error = uvm_fault(map, trunc_page(va), 0, ftype);
		KERNEL_UNLOCK();
		if (error) {
			printf("type %x srr0 0x%lx %s\r\n",
			    type, frame->srr0, p->p_p->ps_comm);
			dumpframe(frame);

			if (error == ENOMEM) {
				sig = SIGKILL;
				code = 0;
			} else if (error == EIO) {
				sig = SIGBUS;
				code = BUS_OBJERR;
			} else if (error == EACCES) {
				sig = SIGSEGV;
				code = SEGV_ACCERR;
			} else {
				sig = SIGSEGV;
				code = SEGV_MAPERR;
			}
			sv.sival_ptr = (void *)va;
			KERNEL_LOCK();
			trapsignal(p, sig, 0, code, sv);
			KERNEL_UNLOCK();
		}
		break;

	case EXC_SC|EXC_USER:
		syscall(frame);
		return;

	case EXC_AST|EXC_USER:
		p->p_md.md_astpending = 0;
		uvmexp.softs++;
		mi_ast(p, ci->ci_want_resched);
		break;

	case EXC_ALI|EXC_USER:
		sv.sival_ptr = (void *)frame->dar;
		KERNEL_LOCK();
		trapsignal(p, SIGBUS, 0, BUS_ADRALN, sv);
		KERNEL_UNLOCK();
		break;

	case EXC_PGM|EXC_USER:
		printf("type %x srr0 0x%lx\r\n", type, frame->srr0);
		dumpframe(frame);

		sv.sival_ptr = (void *)frame->srr0;
		KERNEL_LOCK();
		trapsignal(p, SIGTRAP, 0, TRAP_BRKPT, sv);
		KERNEL_UNLOCK();
		break;

	case EXC_FPU|EXC_USER:
		restore_vsx(p);
		curpcb->pcb_flags |= PCB_FP;
		frame->srr1 |= PSL_FP;
		break;

	case EXC_VEC|EXC_USER:
		restore_vsx(p);
		curpcb->pcb_flags |= PCB_VEC;
		frame->srr1 |= PSL_VEC;
		break;

	default:
	fatal:
		panic("trap type %x srr1 %lx at %lx lr %lx",
		    type, frame->srr1, frame->srr0, frame->lr);
	}

	userret(p);
}

#include <machine/opal.h>

void
dumpframe(struct trapframe *frame)
{
	int i;

	for (i = 0; i < 32; i++)
		opal_printf("r%d 0x%lx\r\n", i, frame->fixreg[i]);
	opal_printf("ctr 0x%lx\r\n", frame->ctr);
	opal_printf("xer 0x%lx\r\n", frame->xer);
	opal_printf("cr 0x%lx\r\n", frame->cr);
	opal_printf("lr 0x%lx\r\n", frame->lr);
	opal_printf("srr0 0x%lx\r\n", frame->srr0);
	opal_printf("srr1 0x%lx\r\n", frame->srr1);
}