xref: /openbmc/linux/arch/sparc/kernel/unaligned_32.c (revision 82e6fdd6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * unaligned.c: Unaligned load/store trap handling with special
4  *              cases for the kernel to do them more quickly.
5  *
6  * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
7  * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
8  */
9 
10 
11 #include <linux/kernel.h>
12 #include <linux/sched/signal.h>
13 #include <linux/mm.h>
14 #include <asm/ptrace.h>
15 #include <asm/processor.h>
16 #include <linux/uaccess.h>
17 #include <linux/smp.h>
18 #include <linux/perf_event.h>
19 
20 #include <asm/setup.h>
21 
22 #include "kernel.h"
23 
24 enum direction {
25 	load,    /* ld, ldd, ldh, ldsh */
26 	store,   /* st, std, sth, stsh */
27 	both,    /* Swap, ldstub, etc. */
28 	fpload,
29 	fpstore,
30 	invalid,
31 };
32 
33 static inline enum direction decode_direction(unsigned int insn)
34 {
35 	unsigned long tmp = (insn >> 21) & 1;
36 
37 	if(!tmp)
38 		return load;
39 	else {
40 		if(((insn>>19)&0x3f) == 15)
41 			return both;
42 		else
43 			return store;
44 	}
45 }
46 
47 /* 8 = double-word, 4 = word, 2 = half-word */
48 static inline int decode_access_size(unsigned int insn)
49 {
50 	insn = (insn >> 19) & 3;
51 
52 	if(!insn)
53 		return 4;
54 	else if(insn == 3)
55 		return 8;
56 	else if(insn == 2)
57 		return 2;
58 	else {
59 		printk("Impossible unaligned trap. insn=%08x\n", insn);
60 		die_if_kernel("Byte sized unaligned access?!?!", current->thread.kregs);
61 		return 4; /* just to keep gcc happy. */
62 	}
63 }
64 
65 /* 0x400000 = signed, 0 = unsigned */
66 static inline int decode_signedness(unsigned int insn)
67 {
68 	return (insn & 0x400000);
69 }
70 
71 static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
72 				       unsigned int rd)
73 {
74 	if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
75 		/* Wheee... */
76 		__asm__ __volatile__("save %sp, -0x40, %sp\n\t"
77 				     "save %sp, -0x40, %sp\n\t"
78 				     "save %sp, -0x40, %sp\n\t"
79 				     "save %sp, -0x40, %sp\n\t"
80 				     "save %sp, -0x40, %sp\n\t"
81 				     "save %sp, -0x40, %sp\n\t"
82 				     "save %sp, -0x40, %sp\n\t"
83 				     "restore; restore; restore; restore;\n\t"
84 				     "restore; restore; restore;\n\t");
85 	}
86 }
87 
88 static inline int sign_extend_imm13(int imm)
89 {
90 	return imm << 19 >> 19;
91 }
92 
93 static inline unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
94 {
95 	struct reg_window32 *win;
96 
97 	if(reg < 16)
98 		return (!reg ? 0 : regs->u_regs[reg]);
99 
100 	/* Ho hum, the slightly complicated case. */
101 	win = (struct reg_window32 *) regs->u_regs[UREG_FP];
102 	return win->locals[reg - 16]; /* yes, I know what this does... */
103 }
104 
105 static inline unsigned long safe_fetch_reg(unsigned int reg, struct pt_regs *regs)
106 {
107 	struct reg_window32 __user *win;
108 	unsigned long ret;
109 
110 	if (reg < 16)
111 		return (!reg ? 0 : regs->u_regs[reg]);
112 
113 	/* Ho hum, the slightly complicated case. */
114 	win = (struct reg_window32 __user *) regs->u_regs[UREG_FP];
115 
116 	if ((unsigned long)win & 3)
117 		return -1;
118 
119 	if (get_user(ret, &win->locals[reg - 16]))
120 		return -1;
121 
122 	return ret;
123 }
124 
125 static inline unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
126 {
127 	struct reg_window32 *win;
128 
129 	if(reg < 16)
130 		return &regs->u_regs[reg];
131 	win = (struct reg_window32 *) regs->u_regs[UREG_FP];
132 	return &win->locals[reg - 16];
133 }
134 
135 static unsigned long compute_effective_address(struct pt_regs *regs,
136 					       unsigned int insn)
137 {
138 	unsigned int rs1 = (insn >> 14) & 0x1f;
139 	unsigned int rs2 = insn & 0x1f;
140 	unsigned int rd = (insn >> 25) & 0x1f;
141 
142 	if(insn & 0x2000) {
143 		maybe_flush_windows(rs1, 0, rd);
144 		return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
145 	} else {
146 		maybe_flush_windows(rs1, rs2, rd);
147 		return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
148 	}
149 }
150 
151 unsigned long safe_compute_effective_address(struct pt_regs *regs,
152 					     unsigned int insn)
153 {
154 	unsigned int rs1 = (insn >> 14) & 0x1f;
155 	unsigned int rs2 = insn & 0x1f;
156 	unsigned int rd = (insn >> 25) & 0x1f;
157 
158 	if(insn & 0x2000) {
159 		maybe_flush_windows(rs1, 0, rd);
160 		return (safe_fetch_reg(rs1, regs) + sign_extend_imm13(insn));
161 	} else {
162 		maybe_flush_windows(rs1, rs2, rd);
163 		return (safe_fetch_reg(rs1, regs) + safe_fetch_reg(rs2, regs));
164 	}
165 }
166 
167 /* This is just to make gcc think panic does return... */
168 static void unaligned_panic(char *str)
169 {
170 	panic("%s", str);
171 }
172 
173 /* una_asm.S */
174 extern int do_int_load(unsigned long *dest_reg, int size,
175 		       unsigned long *saddr, int is_signed);
176 extern int __do_int_store(unsigned long *dst_addr, int size,
177 			  unsigned long *src_val);
178 
179 static int do_int_store(int reg_num, int size, unsigned long *dst_addr,
180 			struct pt_regs *regs)
181 {
182 	unsigned long zero[2] = { 0, 0 };
183 	unsigned long *src_val;
184 
185 	if (reg_num)
186 		src_val = fetch_reg_addr(reg_num, regs);
187 	else {
188 		src_val = &zero[0];
189 		if (size == 8)
190 			zero[1] = fetch_reg(1, regs);
191 	}
192 	return __do_int_store(dst_addr, size, src_val);
193 }
194 
195 extern void smp_capture(void);
196 extern void smp_release(void);
197 
198 static inline void advance(struct pt_regs *regs)
199 {
200 	regs->pc   = regs->npc;
201 	regs->npc += 4;
202 }
203 
204 static inline int floating_point_load_or_store_p(unsigned int insn)
205 {
206 	return (insn >> 24) & 1;
207 }
208 
209 static inline int ok_for_kernel(unsigned int insn)
210 {
211 	return !floating_point_load_or_store_p(insn);
212 }
213 
214 static void kernel_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
215 {
216 	unsigned long g2 = regs->u_regs [UREG_G2];
217 	unsigned long fixup = search_extables_range(regs->pc, &g2);
218 
219 	if (!fixup) {
220 		unsigned long address = compute_effective_address(regs, insn);
221         	if(address < PAGE_SIZE) {
222                 	printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference in mna handler");
223         	} else
224                 	printk(KERN_ALERT "Unable to handle kernel paging request in mna handler");
225 	        printk(KERN_ALERT " at virtual address %08lx\n",address);
226 		printk(KERN_ALERT "current->{mm,active_mm}->context = %08lx\n",
227 			(current->mm ? current->mm->context :
228 			current->active_mm->context));
229 		printk(KERN_ALERT "current->{mm,active_mm}->pgd = %08lx\n",
230 			(current->mm ? (unsigned long) current->mm->pgd :
231 			(unsigned long) current->active_mm->pgd));
232 	        die_if_kernel("Oops", regs);
233 		/* Not reached */
234 	}
235 	regs->pc = fixup;
236 	regs->npc = regs->pc + 4;
237 	regs->u_regs [UREG_G2] = g2;
238 }
239 
240 asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
241 {
242 	enum direction dir = decode_direction(insn);
243 	int size = decode_access_size(insn);
244 
245 	if(!ok_for_kernel(insn) || dir == both) {
246 		printk("Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
247 		       regs->pc);
248 		unaligned_panic("Wheee. Kernel does fpu/atomic unaligned load/store.");
249 	} else {
250 		unsigned long addr = compute_effective_address(regs, insn);
251 		int err;
252 
253 		perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
254 		switch (dir) {
255 		case load:
256 			err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
257 							 regs),
258 					  size, (unsigned long *) addr,
259 					  decode_signedness(insn));
260 			break;
261 
262 		case store:
263 			err = do_int_store(((insn>>25)&0x1f), size,
264 					   (unsigned long *) addr, regs);
265 			break;
266 		default:
267 			panic("Impossible kernel unaligned trap.");
268 			/* Not reached... */
269 		}
270 		if (err)
271 			kernel_mna_trap_fault(regs, insn);
272 		else
273 			advance(regs);
274 	}
275 }
276 
277 static inline int ok_for_user(struct pt_regs *regs, unsigned int insn,
278 			      enum direction dir)
279 {
280 	unsigned int reg;
281 	int check = (dir == load) ? VERIFY_READ : VERIFY_WRITE;
282 	int size = ((insn >> 19) & 3) == 3 ? 8 : 4;
283 
284 	if ((regs->pc | regs->npc) & 3)
285 		return 0;
286 
287 	/* Must access_ok() in all the necessary places. */
288 #define WINREG_ADDR(regnum) \
289 	((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
290 
291 	reg = (insn >> 25) & 0x1f;
292 	if (reg >= 16) {
293 		if (!access_ok(check, WINREG_ADDR(reg - 16), size))
294 			return -EFAULT;
295 	}
296 	reg = (insn >> 14) & 0x1f;
297 	if (reg >= 16) {
298 		if (!access_ok(check, WINREG_ADDR(reg - 16), size))
299 			return -EFAULT;
300 	}
301 	if (!(insn & 0x2000)) {
302 		reg = (insn & 0x1f);
303 		if (reg >= 16) {
304 			if (!access_ok(check, WINREG_ADDR(reg - 16), size))
305 				return -EFAULT;
306 		}
307 	}
308 #undef WINREG_ADDR
309 	return 0;
310 }
311 
312 static void user_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
313 {
314 	siginfo_t info;
315 
316 	info.si_signo = SIGBUS;
317 	info.si_errno = 0;
318 	info.si_code = BUS_ADRALN;
319 	info.si_addr = (void __user *)safe_compute_effective_address(regs, insn);
320 	info.si_trapno = 0;
321 	send_sig_info(SIGBUS, &info, current);
322 }
323 
324 asmlinkage void user_unaligned_trap(struct pt_regs *regs, unsigned int insn)
325 {
326 	enum direction dir;
327 
328 	if(!(current->thread.flags & SPARC_FLAG_UNALIGNED) ||
329 	   (((insn >> 30) & 3) != 3))
330 		goto kill_user;
331 	dir = decode_direction(insn);
332 	if(!ok_for_user(regs, insn, dir)) {
333 		goto kill_user;
334 	} else {
335 		int err, size = decode_access_size(insn);
336 		unsigned long addr;
337 
338 		if(floating_point_load_or_store_p(insn)) {
339 			printk("User FPU load/store unaligned unsupported.\n");
340 			goto kill_user;
341 		}
342 
343 		addr = compute_effective_address(regs, insn);
344 		perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
345 		switch(dir) {
346 		case load:
347 			err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
348 							 regs),
349 					  size, (unsigned long *) addr,
350 					  decode_signedness(insn));
351 			break;
352 
353 		case store:
354 			err = do_int_store(((insn>>25)&0x1f), size,
355 					   (unsigned long *) addr, regs);
356 			break;
357 
358 		case both:
359 			/*
360 			 * This was supported in 2.4. However, we question
361 			 * the value of SWAP instruction across word boundaries.
362 			 */
363 			printk("Unaligned SWAP unsupported.\n");
364 			err = -EFAULT;
365 			break;
366 
367 		default:
368 			unaligned_panic("Impossible user unaligned trap.");
369 			goto out;
370 		}
371 		if (err)
372 			goto kill_user;
373 		else
374 			advance(regs);
375 		goto out;
376 	}
377 
378 kill_user:
379 	user_mna_trap_fault(regs, insn);
380 out:
381 	;
382 }
383