xref: /kvm-unit-tests/lib/arm64/processor.c (revision d95bd8f6c804027984dec6468a57cb47de556d19)
1 /*
2  * processor control and status functions
3  *
4  * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.
7  */
8 #include <libcflat.h>
9 #include <asm/ptrace.h>
10 #include <asm/processor.h>
11 #include <asm/esr.h>
12 #include <asm/thread_info.h>
13 
14 static const char *vector_names[] = {
15 	"el1t_sync",
16 	"el1t_irq",
17 	"el1t_fiq",
18 	"el1t_error",
19 	"el1h_sync",
20 	"el1h_irq",
21 	"el1h_fiq",
22 	"el1h_error",
23 	"el0_sync_64",
24 	"el0_irq_64",
25 	"el0_fiq_64",
26 	"el0_error_64",
27 	"el0_sync_32",
28 	"el0_irq_32",
29 	"el0_fiq_32",
30 	"el0_error_32",
31 };
32 
33 static const char *ec_names[EC_MAX] = {
34 	[ESR_EL1_EC_UNKNOWN]		= "UNKNOWN",
35 	[ESR_EL1_EC_WFI]		= "WFI",
36 	[ESR_EL1_EC_CP15_32]		= "CP15_32",
37 	[ESR_EL1_EC_CP15_64]		= "CP15_64",
38 	[ESR_EL1_EC_CP14_MR]		= "CP14_MR",
39 	[ESR_EL1_EC_CP14_LS]		= "CP14_LS",
40 	[ESR_EL1_EC_FP_ASIMD]		= "FP_ASMID",
41 	[ESR_EL1_EC_CP10_ID]		= "CP10_ID",
42 	[ESR_EL1_EC_CP14_64]		= "CP14_64",
43 	[ESR_EL1_EC_ILL_ISS]		= "ILL_ISS",
44 	[ESR_EL1_EC_SVC32]		= "SVC32",
45 	[ESR_EL1_EC_SVC64]		= "SVC64",
46 	[ESR_EL1_EC_SYS64]		= "SYS64",
47 	[ESR_EL1_EC_IABT_EL0]		= "IABT_EL0",
48 	[ESR_EL1_EC_IABT_EL1]		= "IABT_EL1",
49 	[ESR_EL1_EC_PC_ALIGN]		= "PC_ALIGN",
50 	[ESR_EL1_EC_DABT_EL0]		= "DABT_EL0",
51 	[ESR_EL1_EC_DABT_EL1]		= "DABT_EL1",
52 	[ESR_EL1_EC_SP_ALIGN]		= "SP_ALIGN",
53 	[ESR_EL1_EC_FP_EXC32]		= "FP_EXC32",
54 	[ESR_EL1_EC_FP_EXC64]		= "FP_EXC64",
55 	[ESR_EL1_EC_SERROR]		= "SERROR",
56 	[ESR_EL1_EC_BREAKPT_EL0]	= "BREAKPT_EL0",
57 	[ESR_EL1_EC_BREAKPT_EL1]	= "BREAKPT_EL1",
58 	[ESR_EL1_EC_SOFTSTP_EL0]	= "SOFTSTP_EL0",
59 	[ESR_EL1_EC_SOFTSTP_EL1]	= "SOFTSTP_EL1",
60 	[ESR_EL1_EC_WATCHPT_EL0]	= "WATCHPT_EL0",
61 	[ESR_EL1_EC_WATCHPT_EL1]	= "WATCHPT_EL1",
62 	[ESR_EL1_EC_BKPT32]		= "BKPT32",
63 	[ESR_EL1_EC_BRK64]		= "BRK64",
64 };
65 
66 void show_regs(struct pt_regs *regs)
67 {
68 	int i;
69 
70 	printf("pc : [<%016llx>] lr : [<%016llx>] pstate: %08llx\n",
71 			regs->pc, regs->regs[30], regs->pstate);
72 	printf("sp : %016llx\n", regs->sp);
73 
74 	for (i = 29; i >= 0; --i) {
75 		printf("x%-2d: %016llx ", i, regs->regs[i]);
76 		if (i % 2 == 0)
77 			printf("\n");
78 	}
79 	printf("\n");
80 }
81 
82 bool get_far(unsigned int esr, unsigned long *far)
83 {
84 	unsigned int ec = esr >> ESR_EL1_EC_SHIFT;
85 
86 	asm volatile("mrs %0, far_el1": "=r" (*far));
87 
88 	switch (ec) {
89 	case ESR_EL1_EC_IABT_EL0:
90 	case ESR_EL1_EC_IABT_EL1:
91 	case ESR_EL1_EC_PC_ALIGN:
92 	case ESR_EL1_EC_DABT_EL0:
93 	case ESR_EL1_EC_DABT_EL1:
94 	case ESR_EL1_EC_WATCHPT_EL0:
95 	case ESR_EL1_EC_WATCHPT_EL1:
96 		if ((esr & 0x3f /* DFSC */) != 0x10
97 				|| !(esr & 0x400 /* FnV */))
98 			return true;
99 	}
100 	return false;
101 }
102 
103 static void bad_exception(enum vector v, struct pt_regs *regs,
104 			  unsigned int esr, bool bad_vector)
105 {
106 	unsigned long far;
107 	bool far_valid = get_far(esr, &far);
108 	unsigned int ec = esr >> ESR_EL1_EC_SHIFT;
109 
110 	if (bad_vector) {
111 		if (v < VECTOR_MAX)
112 			printf("Unhandled vector %d (%s)\n", v,
113 					vector_names[v]);
114 		else
115 			printf("Got bad vector=%d\n", v);
116 	} else {
117 		if (ec_names[ec])
118 			printf("Unhandled exception ec=0x%x (%s)\n", ec,
119 					ec_names[ec]);
120 		else
121 			printf("Got bad ec=0x%x\n", ec);
122 	}
123 
124 	printf("Vector: %d (%s)\n", v, vector_names[v]);
125 	printf("ESR_EL1: %8s%08lx, ec=0x%x (%s)\n", "", esr, ec, ec_names[ec]);
126 	printf("FAR_EL1: %016lx (%svalid)\n", far, far_valid ? "" : "not ");
127 	printf("Exception frame registers:\n");
128 	show_regs(regs);
129 	abort();
130 }
131 
132 void install_exception_handler(enum vector v, unsigned int ec, exception_fn fn)
133 {
134 	struct thread_info *ti = current_thread_info();
135 
136 	if (v < VECTOR_MAX && ec < EC_MAX)
137 		ti->exception_handlers[v][ec] = fn;
138 }
139 
140 void default_vector_handler(enum vector v, struct pt_regs *regs,
141 			    unsigned int esr)
142 {
143 	struct thread_info *ti = thread_info_sp(regs->sp);
144 	unsigned int ec = esr >> ESR_EL1_EC_SHIFT;
145 
146 	if (ti->flags & TIF_USER_MODE) {
147 		if (ec < EC_MAX && ti->exception_handlers[v][ec]) {
148 			ti->exception_handlers[v][ec](regs, esr);
149 			return;
150 		}
151 		ti = current_thread_info();
152 	}
153 
154 	if (ec < EC_MAX && ti->exception_handlers[v][ec])
155 		ti->exception_handlers[v][ec](regs, esr);
156 	else
157 		bad_exception(v, regs, esr, false);
158 }
159 
160 void vector_handlers_default_init(vector_fn *handlers)
161 {
162 	handlers[EL1H_SYNC]	= default_vector_handler;
163 	handlers[EL1H_IRQ]	= default_vector_handler;
164 	handlers[EL0_SYNC_64]	= default_vector_handler;
165 	handlers[EL0_IRQ_64]	= default_vector_handler;
166 }
167 
168 void do_handle_exception(enum vector v, struct pt_regs *regs, unsigned int esr)
169 {
170 	struct thread_info *ti = thread_info_sp(regs->sp);
171 
172 	if (ti->flags & TIF_USER_MODE) {
173 		if (v < VECTOR_MAX && ti->vector_handlers[v]) {
174 			ti->vector_handlers[v](v, regs, esr);
175 			return;
176 		}
177 		ti = current_thread_info();
178 	}
179 
180 	if (v < VECTOR_MAX && ti->vector_handlers[v])
181 		ti->vector_handlers[v](v, regs, esr);
182 	else
183 		bad_exception(v, regs, esr, true);
184 }
185 
186 void install_vector_handler(enum vector v, vector_fn fn)
187 {
188 	struct thread_info *ti = current_thread_info();
189 
190 	if (v < VECTOR_MAX)
191 		ti->vector_handlers[v] = fn;
192 }
193 
194 void thread_info_init(struct thread_info *ti, unsigned int flags)
195 {
196 	memset(ti, 0, sizeof(struct thread_info));
197 	ti->cpu = mpidr_to_cpu(get_mpidr());
198 	ti->flags = flags;
199 	vector_handlers_default_init(ti->vector_handlers);
200 }
201 
202 void start_usr(void (*func)(void *arg), void *arg, unsigned long sp_usr)
203 {
204 	sp_usr &= (~15UL); /* stack ptr needs 16-byte alignment */
205 
206 	thread_info_init(thread_info_sp(sp_usr), TIF_USER_MODE);
207 
208 	asm volatile(
209 		"mov	x0, %0\n"
210 		"msr	sp_el0, %1\n"
211 		"msr	elr_el1, %2\n"
212 		"mov	x3, xzr\n"	/* clear and "set" PSR_MODE_EL0t */
213 		"msr	spsr_el1, x3\n"
214 		"eret\n"
215 	:: "r" (arg), "r" (sp_usr), "r" (func) : "x0", "x3");
216 }
217 
218 bool is_user(void)
219 {
220 	return current_thread_info()->flags & TIF_USER_MODE;
221 }
222