xref: /openbmc/linux/arch/parisc/kernel/unaligned.c (revision d7955ce4)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *    Unaligned memory access handler
4  *
5  *    Copyright (C) 2001 Randolph Chung <tausq@debian.org>
6  *    Copyright (C) 2022 Helge Deller <deller@gmx.de>
7  *    Significantly tweaked by LaMont Jones <lamont@debian.org>
8  */
9 
10 #include <linux/sched/signal.h>
11 #include <linux/signal.h>
12 #include <linux/ratelimit.h>
13 #include <linux/uaccess.h>
14 #include <asm/unaligned.h>
15 #include <asm/hardirq.h>
16 #include <asm/traps.h>
17 
18 /* #define DEBUG_UNALIGNED 1 */
19 
20 #ifdef DEBUG_UNALIGNED
21 #define DPRINTF(fmt, args...) do { printk(KERN_DEBUG "%s:%d:%s ", __FILE__, __LINE__, __func__ ); printk(KERN_DEBUG fmt, ##args ); } while (0)
22 #else
23 #define DPRINTF(fmt, args...)
24 #endif
25 
26 #define RFMT "%#08lx"
27 
28 /* 1111 1100 0000 0000 0001 0011 1100 0000 */
29 #define OPCODE1(a,b,c)	((a)<<26|(b)<<12|(c)<<6)
30 #define OPCODE2(a,b)	((a)<<26|(b)<<1)
31 #define OPCODE3(a,b)	((a)<<26|(b)<<2)
32 #define OPCODE4(a)	((a)<<26)
33 #define OPCODE1_MASK	OPCODE1(0x3f,1,0xf)
34 #define OPCODE2_MASK 	OPCODE2(0x3f,1)
35 #define OPCODE3_MASK	OPCODE3(0x3f,1)
36 #define OPCODE4_MASK    OPCODE4(0x3f)
37 
38 /* skip LDB - never unaligned (index) */
39 #define OPCODE_LDH_I	OPCODE1(0x03,0,0x1)
40 #define OPCODE_LDW_I	OPCODE1(0x03,0,0x2)
41 #define OPCODE_LDD_I	OPCODE1(0x03,0,0x3)
42 #define OPCODE_LDDA_I	OPCODE1(0x03,0,0x4)
43 #define OPCODE_LDCD_I	OPCODE1(0x03,0,0x5)
44 #define OPCODE_LDWA_I	OPCODE1(0x03,0,0x6)
45 #define OPCODE_LDCW_I	OPCODE1(0x03,0,0x7)
46 /* skip LDB - never unaligned (short) */
47 #define OPCODE_LDH_S	OPCODE1(0x03,1,0x1)
48 #define OPCODE_LDW_S	OPCODE1(0x03,1,0x2)
49 #define OPCODE_LDD_S	OPCODE1(0x03,1,0x3)
50 #define OPCODE_LDDA_S	OPCODE1(0x03,1,0x4)
51 #define OPCODE_LDCD_S	OPCODE1(0x03,1,0x5)
52 #define OPCODE_LDWA_S	OPCODE1(0x03,1,0x6)
53 #define OPCODE_LDCW_S	OPCODE1(0x03,1,0x7)
54 /* skip STB - never unaligned */
55 #define OPCODE_STH	OPCODE1(0x03,1,0x9)
56 #define OPCODE_STW	OPCODE1(0x03,1,0xa)
57 #define OPCODE_STD	OPCODE1(0x03,1,0xb)
58 /* skip STBY - never unaligned */
59 /* skip STDBY - never unaligned */
60 #define OPCODE_STWA	OPCODE1(0x03,1,0xe)
61 #define OPCODE_STDA	OPCODE1(0x03,1,0xf)
62 
63 #define OPCODE_FLDWX	OPCODE1(0x09,0,0x0)
64 #define OPCODE_FLDWXR	OPCODE1(0x09,0,0x1)
65 #define OPCODE_FSTWX	OPCODE1(0x09,0,0x8)
66 #define OPCODE_FSTWXR	OPCODE1(0x09,0,0x9)
67 #define OPCODE_FLDWS	OPCODE1(0x09,1,0x0)
68 #define OPCODE_FLDWSR	OPCODE1(0x09,1,0x1)
69 #define OPCODE_FSTWS	OPCODE1(0x09,1,0x8)
70 #define OPCODE_FSTWSR	OPCODE1(0x09,1,0x9)
71 #define OPCODE_FLDDX	OPCODE1(0x0b,0,0x0)
72 #define OPCODE_FSTDX	OPCODE1(0x0b,0,0x8)
73 #define OPCODE_FLDDS	OPCODE1(0x0b,1,0x0)
74 #define OPCODE_FSTDS	OPCODE1(0x0b,1,0x8)
75 
76 #define OPCODE_LDD_L	OPCODE2(0x14,0)
77 #define OPCODE_FLDD_L	OPCODE2(0x14,1)
78 #define OPCODE_STD_L	OPCODE2(0x1c,0)
79 #define OPCODE_FSTD_L	OPCODE2(0x1c,1)
80 
81 #define OPCODE_LDW_M	OPCODE3(0x17,1)
82 #define OPCODE_FLDW_L	OPCODE3(0x17,0)
83 #define OPCODE_FSTW_L	OPCODE3(0x1f,0)
84 #define OPCODE_STW_M	OPCODE3(0x1f,1)
85 
86 #define OPCODE_LDH_L    OPCODE4(0x11)
87 #define OPCODE_LDW_L    OPCODE4(0x12)
88 #define OPCODE_LDWM     OPCODE4(0x13)
89 #define OPCODE_STH_L    OPCODE4(0x19)
90 #define OPCODE_STW_L    OPCODE4(0x1A)
91 #define OPCODE_STWM     OPCODE4(0x1B)
92 
93 #define MAJOR_OP(i) (((i)>>26)&0x3f)
94 #define R1(i) (((i)>>21)&0x1f)
95 #define R2(i) (((i)>>16)&0x1f)
96 #define R3(i) ((i)&0x1f)
97 #define FR3(i) ((((i)&0x1f)<<1)|(((i)>>6)&1))
98 #define IM(i,n) (((i)>>1&((1<<(n-1))-1))|((i)&1?((0-1L)<<(n-1)):0))
99 #define IM5_2(i) IM((i)>>16,5)
100 #define IM5_3(i) IM((i),5)
101 #define IM14(i) IM((i),14)
102 
103 #define ERR_NOTHANDLED	-1
104 
105 int unaligned_enabled __read_mostly = 1;
106 
107 static int emulate_ldh(struct pt_regs *regs, int toreg)
108 {
109 	unsigned long saddr = regs->ior;
110 	unsigned long val = 0, temp1;
111 	ASM_EXCEPTIONTABLE_VAR(ret);
112 
113 	DPRINTF("load " RFMT ":" RFMT " to r%d for 2 bytes\n",
114 		regs->isr, regs->ior, toreg);
115 
116 	__asm__ __volatile__  (
117 "	mtsp	%4, %%sr1\n"
118 "1:	ldbs	0(%%sr1,%3), %2\n"
119 "2:	ldbs	1(%%sr1,%3), %0\n"
120 "	depw	%2, 23, 24, %0\n"
121 "3:	\n"
122 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b)
123 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b)
124 	: "+r" (val), "+r" (ret), "=&r" (temp1)
125 	: "r" (saddr), "r" (regs->isr) );
126 
127 	DPRINTF("val = " RFMT "\n", val);
128 
129 	if (toreg)
130 		regs->gr[toreg] = val;
131 
132 	return ret;
133 }
134 
135 static int emulate_ldw(struct pt_regs *regs, int toreg, int flop)
136 {
137 	unsigned long saddr = regs->ior;
138 	unsigned long val = 0, temp1, temp2;
139 	ASM_EXCEPTIONTABLE_VAR(ret);
140 
141 	DPRINTF("load " RFMT ":" RFMT " to r%d for 4 bytes\n",
142 		regs->isr, regs->ior, toreg);
143 
144 	__asm__ __volatile__  (
145 "	zdep	%4,28,2,%2\n"		/* r19=(ofs&3)*8 */
146 "	mtsp	%5, %%sr1\n"
147 "	depw	%%r0,31,2,%4\n"
148 "1:	ldw	0(%%sr1,%4),%0\n"
149 "2:	ldw	4(%%sr1,%4),%3\n"
150 "	subi	32,%2,%2\n"
151 "	mtctl	%2,11\n"
152 "	vshd	%0,%3,%0\n"
153 "3:	\n"
154 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b)
155 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b)
156 	: "+r" (val), "+r" (ret), "=&r" (temp1), "=&r" (temp2)
157 	: "r" (saddr), "r" (regs->isr) );
158 
159 	DPRINTF("val = " RFMT "\n", val);
160 
161 	if (flop)
162 		((__u32*)(regs->fr))[toreg] = val;
163 	else if (toreg)
164 		regs->gr[toreg] = val;
165 
166 	return ret;
167 }
168 static int emulate_ldd(struct pt_regs *regs, int toreg, int flop)
169 {
170 	unsigned long saddr = regs->ior;
171 	__u64 val = 0;
172 	ASM_EXCEPTIONTABLE_VAR(ret);
173 
174 	DPRINTF("load " RFMT ":" RFMT " to r%d for 8 bytes\n",
175 		regs->isr, regs->ior, toreg);
176 
177 	if (!IS_ENABLED(CONFIG_64BIT) && !flop)
178 		return ERR_NOTHANDLED;
179 
180 #ifdef CONFIG_64BIT
181 	__asm__ __volatile__  (
182 "	depd,z	%3,60,3,%%r19\n"		/* r19=(ofs&7)*8 */
183 "	mtsp	%4, %%sr1\n"
184 "	depd	%%r0,63,3,%3\n"
185 "1:	ldd	0(%%sr1,%3),%0\n"
186 "2:	ldd	8(%%sr1,%3),%%r20\n"
187 "	subi	64,%%r19,%%r19\n"
188 "	mtsar	%%r19\n"
189 "	shrpd	%0,%%r20,%%sar,%0\n"
190 "3:	\n"
191 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b)
192 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b)
193 	: "=r" (val), "+r" (ret)
194 	: "0" (val), "r" (saddr), "r" (regs->isr)
195 	: "r19", "r20" );
196 #else
197     {
198 	unsigned long shift, temp1;
199 	__asm__ __volatile__  (
200 "	zdep	%2,29,2,%3\n"		/* r19=(ofs&3)*8 */
201 "	mtsp	%5, %%sr1\n"
202 "	dep	%%r0,31,2,%2\n"
203 "1:	ldw	0(%%sr1,%2),%0\n"
204 "2:	ldw	4(%%sr1,%2),%R0\n"
205 "3:	ldw	8(%%sr1,%2),%4\n"
206 "	subi	32,%3,%3\n"
207 "	mtsar	%3\n"
208 "	vshd	%0,%R0,%0\n"
209 "	vshd	%R0,%4,%R0\n"
210 "4:	\n"
211 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 4b)
212 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 4b)
213 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(3b, 4b)
214 	: "+r" (val), "+r" (ret), "+r" (saddr), "=&r" (shift), "=&r" (temp1)
215 	: "r" (regs->isr) );
216     }
217 #endif
218 
219 	DPRINTF("val = 0x%llx\n", val);
220 
221 	if (flop)
222 		regs->fr[toreg] = val;
223 	else if (toreg)
224 		regs->gr[toreg] = val;
225 
226 	return ret;
227 }
228 
229 static int emulate_sth(struct pt_regs *regs, int frreg)
230 {
231 	unsigned long val = regs->gr[frreg], temp1;
232 	ASM_EXCEPTIONTABLE_VAR(ret);
233 
234 	if (!frreg)
235 		val = 0;
236 
237 	DPRINTF("store r%d (" RFMT ") to " RFMT ":" RFMT " for 2 bytes\n", frreg,
238 		val, regs->isr, regs->ior);
239 
240 	__asm__ __volatile__ (
241 "	mtsp %4, %%sr1\n"
242 "	extrw,u %2, 23, 8, %1\n"
243 "1:	stb %1, 0(%%sr1, %3)\n"
244 "2:	stb %2, 1(%%sr1, %3)\n"
245 "3:	\n"
246 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b)
247 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b)
248 	: "+r" (ret), "=&r" (temp1)
249 	: "r" (val), "r" (regs->ior), "r" (regs->isr) );
250 
251 	return ret;
252 }
253 
254 static int emulate_stw(struct pt_regs *regs, int frreg, int flop)
255 {
256 	unsigned long val;
257 	ASM_EXCEPTIONTABLE_VAR(ret);
258 
259 	if (flop)
260 		val = ((__u32*)(regs->fr))[frreg];
261 	else if (frreg)
262 		val = regs->gr[frreg];
263 	else
264 		val = 0;
265 
266 	DPRINTF("store r%d (" RFMT ") to " RFMT ":" RFMT " for 4 bytes\n", frreg,
267 		val, regs->isr, regs->ior);
268 
269 
270 	__asm__ __volatile__ (
271 "	mtsp %3, %%sr1\n"
272 "	zdep	%2, 28, 2, %%r19\n"
273 "	dep	%%r0, 31, 2, %2\n"
274 "	mtsar	%%r19\n"
275 "	depwi,z	-2, %%sar, 32, %%r19\n"
276 "1:	ldw	0(%%sr1,%2),%%r20\n"
277 "2:	ldw	4(%%sr1,%2),%%r21\n"
278 "	vshd	%%r0, %1, %%r22\n"
279 "	vshd	%1, %%r0, %%r1\n"
280 "	and	%%r20, %%r19, %%r20\n"
281 "	andcm	%%r21, %%r19, %%r21\n"
282 "	or	%%r22, %%r20, %%r20\n"
283 "	or	%%r1, %%r21, %%r21\n"
284 "	stw	%%r20,0(%%sr1,%2)\n"
285 "	stw	%%r21,4(%%sr1,%2)\n"
286 "3:	\n"
287 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 3b)
288 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 3b)
289 	: "+r" (ret)
290 	: "r" (val), "r" (regs->ior), "r" (regs->isr)
291 	: "r19", "r20", "r21", "r22", "r1" );
292 
293 	return ret;
294 }
295 static int emulate_std(struct pt_regs *regs, int frreg, int flop)
296 {
297 	__u64 val;
298 	ASM_EXCEPTIONTABLE_VAR(ret);
299 
300 	if (flop)
301 		val = regs->fr[frreg];
302 	else if (frreg)
303 		val = regs->gr[frreg];
304 	else
305 		val = 0;
306 
307 	DPRINTF("store r%d (0x%016llx) to " RFMT ":" RFMT " for 8 bytes\n", frreg,
308 		val,  regs->isr, regs->ior);
309 
310 	if (!IS_ENABLED(CONFIG_64BIT) && !flop)
311 		return ERR_NOTHANDLED;
312 
313 #ifdef CONFIG_64BIT
314 	__asm__ __volatile__ (
315 "	mtsp %3, %%sr1\n"
316 "	depd,z	%2, 60, 3, %%r19\n"
317 "	depd	%%r0, 63, 3, %2\n"
318 "	mtsar	%%r19\n"
319 "	depdi,z	-2, %%sar, 64, %%r19\n"
320 "1:	ldd	0(%%sr1,%2),%%r20\n"
321 "2:	ldd	8(%%sr1,%2),%%r21\n"
322 "	shrpd	%%r0, %1, %%sar, %%r22\n"
323 "	shrpd	%1, %%r0, %%sar, %%r1\n"
324 "	and	%%r20, %%r19, %%r20\n"
325 "	andcm	%%r21, %%r19, %%r21\n"
326 "	or	%%r22, %%r20, %%r20\n"
327 "	or	%%r1, %%r21, %%r21\n"
328 "3:	std	%%r20,0(%%sr1,%2)\n"
329 "4:	std	%%r21,8(%%sr1,%2)\n"
330 "5:	\n"
331 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 5b)
332 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 5b)
333 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(3b, 5b)
334 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(4b, 5b)
335 	: "+r" (ret)
336 	: "r" (val), "r" (regs->ior), "r" (regs->isr)
337 	: "r19", "r20", "r21", "r22", "r1" );
338 #else
339     {
340 	unsigned long valh=(val>>32),vall=(val&0xffffffffl);
341 	__asm__ __volatile__ (
342 "	mtsp	%4, %%sr1\n"
343 "	zdep	%2, 29, 2, %%r19\n"
344 "	dep	%%r0, 31, 2, %3\n"
345 "	mtsar	%%r19\n"
346 "	zvdepi	-2, 32, %%r19\n"
347 "1:	ldw	0(%%sr1,%3),%%r20\n"
348 "2:	ldw	8(%%sr1,%3),%%r21\n"
349 "	vshd	%1, %2, %%r1\n"
350 "	vshd	%%r0, %1, %1\n"
351 "	vshd	%2, %%r0, %2\n"
352 "	and	%%r20, %%r19, %%r20\n"
353 "	andcm	%%r21, %%r19, %%r21\n"
354 "	or	%1, %%r20, %1\n"
355 "	or	%2, %%r21, %2\n"
356 "3:	stw	%1,0(%%sr1,%3)\n"
357 "4:	stw	%%r1,4(%%sr1,%3)\n"
358 "5:	stw	%2,8(%%sr1,%3)\n"
359 "6:	\n"
360 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 6b)
361 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 6b)
362 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(3b, 6b)
363 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(4b, 6b)
364 	ASM_EXCEPTIONTABLE_ENTRY_EFAULT(5b, 6b)
365 	: "+r" (ret)
366 	: "r" (valh), "r" (vall), "r" (regs->ior), "r" (regs->isr)
367 	: "r19", "r20", "r21", "r1" );
368     }
369 #endif
370 
371 	return ret;
372 }
373 
374 void handle_unaligned(struct pt_regs *regs)
375 {
376 	static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 5);
377 	unsigned long newbase = R1(regs->iir)?regs->gr[R1(regs->iir)]:0;
378 	int modify = 0;
379 	int ret = ERR_NOTHANDLED;
380 
381 	__inc_irq_stat(irq_unaligned_count);
382 
383 	/* log a message with pacing */
384 	if (user_mode(regs)) {
385 		if (current->thread.flags & PARISC_UAC_SIGBUS) {
386 			goto force_sigbus;
387 		}
388 
389 		if (!(current->thread.flags & PARISC_UAC_NOPRINT) &&
390 			__ratelimit(&ratelimit)) {
391 			printk(KERN_WARNING "%s(%d): unaligned access to " RFMT
392 				" at ip " RFMT " (iir " RFMT ")\n",
393 				current->comm, task_pid_nr(current), regs->ior,
394 				regs->iaoq[0], regs->iir);
395 #ifdef DEBUG_UNALIGNED
396 			show_regs(regs);
397 #endif
398 		}
399 
400 		if (!unaligned_enabled)
401 			goto force_sigbus;
402 	}
403 
404 	/* handle modification - OK, it's ugly, see the instruction manual */
405 	switch (MAJOR_OP(regs->iir))
406 	{
407 	case 0x03:
408 	case 0x09:
409 	case 0x0b:
410 		if (regs->iir&0x20)
411 		{
412 			modify = 1;
413 			if (regs->iir&0x1000)		/* short loads */
414 				if (regs->iir&0x200)
415 					newbase += IM5_3(regs->iir);
416 				else
417 					newbase += IM5_2(regs->iir);
418 			else if (regs->iir&0x2000)	/* scaled indexed */
419 			{
420 				int shift=0;
421 				switch (regs->iir & OPCODE1_MASK)
422 				{
423 				case OPCODE_LDH_I:
424 					shift= 1; break;
425 				case OPCODE_LDW_I:
426 					shift= 2; break;
427 				case OPCODE_LDD_I:
428 				case OPCODE_LDDA_I:
429 					shift= 3; break;
430 				}
431 				newbase += (R2(regs->iir)?regs->gr[R2(regs->iir)]:0)<<shift;
432 			} else				/* simple indexed */
433 				newbase += (R2(regs->iir)?regs->gr[R2(regs->iir)]:0);
434 		}
435 		break;
436 	case 0x13:
437 	case 0x1b:
438 		modify = 1;
439 		newbase += IM14(regs->iir);
440 		break;
441 	case 0x14:
442 	case 0x1c:
443 		if (regs->iir&8)
444 		{
445 			modify = 1;
446 			newbase += IM14(regs->iir&~0xe);
447 		}
448 		break;
449 	case 0x16:
450 	case 0x1e:
451 		modify = 1;
452 		newbase += IM14(regs->iir&6);
453 		break;
454 	case 0x17:
455 	case 0x1f:
456 		if (regs->iir&4)
457 		{
458 			modify = 1;
459 			newbase += IM14(regs->iir&~4);
460 		}
461 		break;
462 	}
463 
464 	/* TODO: make this cleaner... */
465 	switch (regs->iir & OPCODE1_MASK)
466 	{
467 	case OPCODE_LDH_I:
468 	case OPCODE_LDH_S:
469 		ret = emulate_ldh(regs, R3(regs->iir));
470 		break;
471 
472 	case OPCODE_LDW_I:
473 	case OPCODE_LDWA_I:
474 	case OPCODE_LDW_S:
475 	case OPCODE_LDWA_S:
476 		ret = emulate_ldw(regs, R3(regs->iir),0);
477 		break;
478 
479 	case OPCODE_STH:
480 		ret = emulate_sth(regs, R2(regs->iir));
481 		break;
482 
483 	case OPCODE_STW:
484 	case OPCODE_STWA:
485 		ret = emulate_stw(regs, R2(regs->iir),0);
486 		break;
487 
488 #ifdef CONFIG_64BIT
489 	case OPCODE_LDD_I:
490 	case OPCODE_LDDA_I:
491 	case OPCODE_LDD_S:
492 	case OPCODE_LDDA_S:
493 		ret = emulate_ldd(regs, R3(regs->iir),0);
494 		break;
495 
496 	case OPCODE_STD:
497 	case OPCODE_STDA:
498 		ret = emulate_std(regs, R2(regs->iir),0);
499 		break;
500 #endif
501 
502 	case OPCODE_FLDWX:
503 	case OPCODE_FLDWS:
504 	case OPCODE_FLDWXR:
505 	case OPCODE_FLDWSR:
506 		ret = emulate_ldw(regs,FR3(regs->iir),1);
507 		break;
508 
509 	case OPCODE_FLDDX:
510 	case OPCODE_FLDDS:
511 		ret = emulate_ldd(regs,R3(regs->iir),1);
512 		break;
513 
514 	case OPCODE_FSTWX:
515 	case OPCODE_FSTWS:
516 	case OPCODE_FSTWXR:
517 	case OPCODE_FSTWSR:
518 		ret = emulate_stw(regs,FR3(regs->iir),1);
519 		break;
520 
521 	case OPCODE_FSTDX:
522 	case OPCODE_FSTDS:
523 		ret = emulate_std(regs,R3(regs->iir),1);
524 		break;
525 
526 	case OPCODE_LDCD_I:
527 	case OPCODE_LDCW_I:
528 	case OPCODE_LDCD_S:
529 	case OPCODE_LDCW_S:
530 		ret = ERR_NOTHANDLED;	/* "undefined", but lets kill them. */
531 		break;
532 	}
533 	switch (regs->iir & OPCODE2_MASK)
534 	{
535 	case OPCODE_FLDD_L:
536 		ret = emulate_ldd(regs,R2(regs->iir),1);
537 		break;
538 	case OPCODE_FSTD_L:
539 		ret = emulate_std(regs, R2(regs->iir),1);
540 		break;
541 #ifdef CONFIG_64BIT
542 	case OPCODE_LDD_L:
543 		ret = emulate_ldd(regs, R2(regs->iir),0);
544 		break;
545 	case OPCODE_STD_L:
546 		ret = emulate_std(regs, R2(regs->iir),0);
547 		break;
548 #endif
549 	}
550 	switch (regs->iir & OPCODE3_MASK)
551 	{
552 	case OPCODE_FLDW_L:
553 		ret = emulate_ldw(regs, R2(regs->iir), 1);
554 		break;
555 	case OPCODE_LDW_M:
556 		ret = emulate_ldw(regs, R2(regs->iir), 0);
557 		break;
558 
559 	case OPCODE_FSTW_L:
560 		ret = emulate_stw(regs, R2(regs->iir),1);
561 		break;
562 	case OPCODE_STW_M:
563 		ret = emulate_stw(regs, R2(regs->iir),0);
564 		break;
565 	}
566 	switch (regs->iir & OPCODE4_MASK)
567 	{
568 	case OPCODE_LDH_L:
569 		ret = emulate_ldh(regs, R2(regs->iir));
570 		break;
571 	case OPCODE_LDW_L:
572 	case OPCODE_LDWM:
573 		ret = emulate_ldw(regs, R2(regs->iir),0);
574 		break;
575 	case OPCODE_STH_L:
576 		ret = emulate_sth(regs, R2(regs->iir));
577 		break;
578 	case OPCODE_STW_L:
579 	case OPCODE_STWM:
580 		ret = emulate_stw(regs, R2(regs->iir),0);
581 		break;
582 	}
583 
584 	if (ret == 0 && modify && R1(regs->iir))
585 		regs->gr[R1(regs->iir)] = newbase;
586 
587 
588 	if (ret == ERR_NOTHANDLED)
589 		printk(KERN_CRIT "Not-handled unaligned insn 0x%08lx\n", regs->iir);
590 
591 	DPRINTF("ret = %d\n", ret);
592 
593 	if (ret)
594 	{
595 		/*
596 		 * The unaligned handler failed.
597 		 * If we were called by __get_user() or __put_user() jump
598 		 * to it's exception fixup handler instead of crashing.
599 		 */
600 		if (!user_mode(regs) && fixup_exception(regs))
601 			return;
602 
603 		printk(KERN_CRIT "Unaligned handler failed, ret = %d\n", ret);
604 		die_if_kernel("Unaligned data reference", regs, 28);
605 
606 		if (ret == -EFAULT)
607 		{
608 			force_sig_fault(SIGSEGV, SEGV_MAPERR,
609 					(void __user *)regs->ior);
610 		}
611 		else
612 		{
613 force_sigbus:
614 			/* couldn't handle it ... */
615 			force_sig_fault(SIGBUS, BUS_ADRALN,
616 					(void __user *)regs->ior);
617 		}
618 
619 		return;
620 	}
621 
622 	/* else we handled it, let life go on. */
623 	regs->gr[0]|=PSW_N;
624 }
625 
626 /*
627  * NB: check_unaligned() is only used for PCXS processors right
628  * now, so we only check for PA1.1 encodings at this point.
629  */
630 
631 int
632 check_unaligned(struct pt_regs *regs)
633 {
634 	unsigned long align_mask;
635 
636 	/* Get alignment mask */
637 
638 	align_mask = 0UL;
639 	switch (regs->iir & OPCODE1_MASK) {
640 
641 	case OPCODE_LDH_I:
642 	case OPCODE_LDH_S:
643 	case OPCODE_STH:
644 		align_mask = 1UL;
645 		break;
646 
647 	case OPCODE_LDW_I:
648 	case OPCODE_LDWA_I:
649 	case OPCODE_LDW_S:
650 	case OPCODE_LDWA_S:
651 	case OPCODE_STW:
652 	case OPCODE_STWA:
653 		align_mask = 3UL;
654 		break;
655 
656 	default:
657 		switch (regs->iir & OPCODE4_MASK) {
658 		case OPCODE_LDH_L:
659 		case OPCODE_STH_L:
660 			align_mask = 1UL;
661 			break;
662 		case OPCODE_LDW_L:
663 		case OPCODE_LDWM:
664 		case OPCODE_STW_L:
665 		case OPCODE_STWM:
666 			align_mask = 3UL;
667 			break;
668 		}
669 		break;
670 	}
671 
672 	return (int)(regs->ior & align_mask);
673 }
674 
675