summaryrefslogtreecommitdiffstats
path: root/sys/arch/powerpc64/powerpc64/trap.c
blob: 313d9b31066867bd67158149d342c5ad4b853ba7 (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
/*	$OpenBSD: trap.c,v 1.11 2020/06/22 16:58:20 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/user.h>
#include <sys/syscall.h>
#include <sys/syscall_mi.h>
#include <sys/systm.h>

#include <uvm/uvm_extern.h>

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

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

void
trap(struct trapframe *frame)
{
	struct cpu_info *ci = curcpu();
	struct proc *p = curproc;
	int type = frame->exc;
	struct vm_map *map;
	vaddr_t va;
	int ftype;
	int error;

	switch (type) {
	case EXC_DECR:
		decr_intr(frame);
		return;
	case EXC_HVI:
		hvi_intr(frame);
		return;
	}

	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 < VM_MAXUSER_ADDRESS) {
			map = &p->p_vmspace->vm_map;
		}
		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)
			printf("DSI onfault\n");

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

	case EXC_DSE:
		if (curpcb->pcb_onfault)
			printf("DSE onfault\n");

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

	case EXC_ISI|EXC_USER:
	case EXC_ISE|EXC_USER:
		intr_enable();
		map = &p->p_vmspace->vm_map;
		va = frame->srr0;
		ftype = PROT_READ | PROT_EXEC;
		error = uvm_fault(map, trunc_page(va), 0, ftype);
		KERNEL_UNLOCK();
		if (error)
			goto fatal;
		break;

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

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

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

	userret(p);
}