Blame view

arch/arm/mm/alignment.c 25.4 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
  /*
   *  linux/arch/arm/mm/alignment.c
   *
   *  Copyright (C) 1995  Linus Torvalds
   *  Modifications for ARM processor (c) 1995-2001 Russell King
6cbdc8c53   Simon Arlott   [ARM] spelling fixes
6
   *  Thumb alignment fault fixups (c) 2004 MontaVista Software, Inc.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
9
10
11
12
13
   *  - Adapted from gdb/sim/arm/thumbemu.c -- Thumb instruction emulation.
   *    Copyright (C) 1996, Cygnus Software Technologies Ltd.
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   */
d944d549a   Russell King   ARM: allow alignm...
14
  #include <linux/moduleparam.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
16
17
18
  #include <linux/compiler.h>
  #include <linux/kernel.h>
  #include <linux/errno.h>
  #include <linux/string.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include <linux/proc_fs.h>
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
20
  #include <linux/seq_file.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
  #include <linux/init.h>
87c52578b   Russell King   [ARM] Remove linu...
22
  #include <linux/sched.h>
33fa9b132   Russell King   [ARM] Convert asm...
23
  #include <linux/uaccess.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24

2102a65e6   Dave Martin   ARM: 7008/1: alig...
25
  #include <asm/system.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <asm/unaligned.h>
  
  #include "fault.h"
  
  /*
   * 32-bit misaligned trap handler (c) 1998 San Mehat (CCC) -July 1998
   * /proc/sys/debug/alignment, modified and integrated into
   * Linux 2.1 by Russell King
   *
   * Speed optimisations and better fault handling by Russell King.
   *
   * *** NOTE ***
   * This code is not portable to processors with late data abort handling.
   */
  #define CODING_BITS(i)	(i & 0x0e000000)
  
  #define LDST_I_BIT(i)	(i & (1 << 26))		/* Immediate constant	*/
  #define LDST_P_BIT(i)	(i & (1 << 24))		/* Preindex		*/
  #define LDST_U_BIT(i)	(i & (1 << 23))		/* Add offset		*/
  #define LDST_W_BIT(i)	(i & (1 << 21))		/* Writeback		*/
  #define LDST_L_BIT(i)	(i & (1 << 20))		/* Load			*/
  
  #define LDST_P_EQ_U(i)	((((i) ^ ((i) >> 1)) & (1 << 23)) == 0)
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
49
  #define LDSTHD_I_BIT(i)	(i & (1 << 22))		/* double/half-word immed */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  #define LDM_S_BIT(i)	(i & (1 << 22))		/* write CPSR from SPSR	*/
  
  #define RN_BITS(i)	((i >> 16) & 15)	/* Rn			*/
  #define RD_BITS(i)	((i >> 12) & 15)	/* Rd			*/
  #define RM_BITS(i)	(i & 15)		/* Rm			*/
  
  #define REGMASK_BITS(i)	(i & 0xffff)
  #define OFFSET_BITS(i)	(i & 0x0fff)
  
  #define IS_SHIFT(i)	(i & 0x0ff0)
  #define SHIFT_BITS(i)	((i >> 7) & 0x1f)
  #define SHIFT_TYPE(i)	(i & 0x60)
  #define SHIFT_LSL	0x00
  #define SHIFT_LSR	0x20
  #define SHIFT_ASR	0x40
  #define SHIFT_RORRRX	0x60
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
66
67
68
69
70
  #define BAD_INSTR 	0xdeadc0de
  
  /* Thumb-2 32 bit format per ARMv7 DDI0406A A6.3, either f800h,e800h,f800h */
  #define IS_T32(hi16) \
  	(((hi16) & 0xe000) == 0xe000 && ((hi16) & 0x1800))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
  static unsigned long ai_user;
  static unsigned long ai_sys;
  static unsigned long ai_skipped;
  static unsigned long ai_half;
  static unsigned long ai_word;
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
76
  static unsigned long ai_dword;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
  static unsigned long ai_multi;
  static int ai_usermode;
d944d549a   Russell King   ARM: allow alignm...
79
  core_param(alignment, ai_usermode, int, 0600);
baa745a33   Russell King   [ARM] Fix alignme...
80
81
82
  #define UM_WARN		(1 << 0)
  #define UM_FIXUP	(1 << 1)
  #define UM_SIGNAL	(1 << 2)
088c01f1e   Dave Martin   ARM: 7007/1: alig...
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
  /* Return true if and only if the ARMv6 unaligned access model is in use. */
  static bool cpu_is_v6_unaligned(void)
  {
  	return cpu_architecture() >= CPU_ARCH_ARMv6 && (cr_alignment & CR_U);
  }
  
  static int safe_usermode(int new_usermode, bool warn)
  {
  	/*
  	 * ARMv6 and later CPUs can perform unaligned accesses for
  	 * most single load and store instructions up to word size.
  	 * LDM, STM, LDRD and STRD still need to be handled.
  	 *
  	 * Ignoring the alignment fault is not an option on these
  	 * CPUs since we spin re-faulting the instruction without
  	 * making any progress.
  	 */
  	if (cpu_is_v6_unaligned() && !(new_usermode & (UM_FIXUP | UM_SIGNAL))) {
  		new_usermode |= UM_FIXUP;
  
  		if (warn)
  			printk(KERN_WARNING "alignment: ignoring faults is unsafe on this CPU.  Defaulting to fixup mode.
  ");
  	}
  
  	return new_usermode;
  }
ffc660c51   Arnd Bergmann   ARM: allow buildi...
110
111
112
113
114
115
116
117
118
  #ifdef CONFIG_PROC_FS
  static const char *usermode_action[] = {
  	"ignored",
  	"warn",
  	"fixup",
  	"fixup+warn",
  	"signal",
  	"signal+warn"
  };
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
119
  static int alignment_proc_show(struct seq_file *m, void *v)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
  {
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
121
122
123
124
125
126
127
128
129
130
  	seq_printf(m, "User:\t\t%lu
  ", ai_user);
  	seq_printf(m, "System:\t\t%lu
  ", ai_sys);
  	seq_printf(m, "Skipped:\t%lu
  ", ai_skipped);
  	seq_printf(m, "Half:\t\t%lu
  ", ai_half);
  	seq_printf(m, "Word:\t\t%lu
  ", ai_word);
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
131
  	if (cpu_architecture() >= CPU_ARCH_ARMv5TE)
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
132
133
134
135
136
137
  		seq_printf(m, "DWord:\t\t%lu
  ", ai_dword);
  	seq_printf(m, "Multi:\t\t%lu
  ", ai_multi);
  	seq_printf(m, "User faults:\t%i (%s)
  ", ai_usermode,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
138
  			usermode_action[ai_usermode]);
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
139
140
  	return 0;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141

b7072c63c   Alexey Dobriyan   ARM: convert /pro...
142
143
144
  static int alignment_proc_open(struct inode *inode, struct file *file)
  {
  	return single_open(file, alignment_proc_show, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  }
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
146
147
  static ssize_t alignment_proc_write(struct file *file, const char __user *buffer,
  				    size_t count, loff_t *pos)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
152
153
154
  {
  	char mode;
  
  	if (count > 0) {
  		if (get_user(mode, buffer))
  			return -EFAULT;
  		if (mode >= '0' && mode <= '5')
088c01f1e   Dave Martin   ARM: 7007/1: alig...
155
  			ai_usermode = safe_usermode(mode - '0', true);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
156
157
158
  	}
  	return count;
  }
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
159
160
161
162
163
164
165
  static const struct file_operations alignment_proc_fops = {
  	.open		= alignment_proc_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  	.write		= alignment_proc_write,
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  #endif /* CONFIG_PROC_FS */
  
  union offset_union {
  	unsigned long un;
  	  signed long sn;
  };
  
  #define TYPE_ERROR	0
  #define TYPE_FAULT	1
  #define TYPE_LDST	2
  #define TYPE_DONE	3
  
  #ifdef __ARMEB__
  #define BE		1
  #define FIRST_BYTE_16	"mov	%1, %1, ror #8
  "
  #define FIRST_BYTE_32	"mov	%1, %1, ror #24
  "
  #define NEXT_BYTE	"ror #24"
  #else
  #define BE		0
  #define FIRST_BYTE_16
  #define FIRST_BYTE_32
  #define NEXT_BYTE	"lsr #8"
  #endif
  
  #define __get8_unaligned_check(ins,val,addr,err)	\
  	__asm__(					\
347c8b70b   Catalin Marinas   Thumb-2: Implemen...
194
195
196
197
198
199
   ARM(	"1:	"ins"	%1, [%2], #1
  "	)		\
   THUMB(	"1:	"ins"	%1, [%2]
  "	)		\
   THUMB(	"	add	%2, %2, #1
  "	)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
201
  	"2:
  "						\
4260415f6   Russell King   ARM: fix build er...
202
203
  	"	.pushsection .fixup,\"ax\"
  "		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
204
205
206
207
208
209
  	"	.align	2
  "				\
  	"3:	mov	%0, #1
  "			\
  	"	b	2b
  "				\
4260415f6   Russell King   ARM: fix build er...
210
211
212
213
  	"	.popsection
  "				\
  	"	.pushsection __ex_table,\"a\"
  "	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
214
215
216
217
  	"	.align	3
  "				\
  	"	.long	1b, 3b
  "			\
4260415f6   Russell King   ARM: fix build er...
218
219
  	"	.popsection
  "				\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  	: "=r" (err), "=&r" (val), "=r" (addr)		\
  	: "0" (err), "2" (addr))
  
  #define __get16_unaligned_check(ins,val,addr)			\
  	do {							\
  		unsigned int err = 0, v, a = addr;		\
  		__get8_unaligned_check(ins,v,a,err);		\
  		val =  v << ((BE) ? 8 : 0);			\
  		__get8_unaligned_check(ins,v,a,err);		\
  		val |= v << ((BE) ? 0 : 8);			\
  		if (err)					\
  			goto fault;				\
  	} while (0)
  
  #define get16_unaligned_check(val,addr) \
  	__get16_unaligned_check("ldrb",val,addr)
  
  #define get16t_unaligned_check(val,addr) \
  	__get16_unaligned_check("ldrbt",val,addr)
  
  #define __get32_unaligned_check(ins,val,addr)			\
  	do {							\
  		unsigned int err = 0, v, a = addr;		\
  		__get8_unaligned_check(ins,v,a,err);		\
  		val =  v << ((BE) ? 24 :  0);			\
  		__get8_unaligned_check(ins,v,a,err);		\
  		val |= v << ((BE) ? 16 :  8);			\
  		__get8_unaligned_check(ins,v,a,err);		\
  		val |= v << ((BE) ?  8 : 16);			\
  		__get8_unaligned_check(ins,v,a,err);		\
  		val |= v << ((BE) ?  0 : 24);			\
  		if (err)					\
  			goto fault;				\
  	} while (0)
  
  #define get32_unaligned_check(val,addr) \
  	__get32_unaligned_check("ldrb",val,addr)
  
  #define get32t_unaligned_check(val,addr) \
  	__get32_unaligned_check("ldrbt",val,addr)
  
  #define __put16_unaligned_check(ins,val,addr)			\
  	do {							\
  		unsigned int err = 0, v = val, a = addr;	\
  		__asm__( FIRST_BYTE_16				\
347c8b70b   Catalin Marinas   Thumb-2: Implemen...
265
266
267
268
269
270
  	 ARM(	"1:	"ins"	%1, [%2], #1
  "	)		\
  	 THUMB(	"1:	"ins"	%1, [%2]
  "	)		\
  	 THUMB(	"	add	%2, %2, #1
  "	)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
271
272
273
274
275
276
  		"	mov	%1, %1, "NEXT_BYTE"
  "		\
  		"2:	"ins"	%1, [%2]
  "			\
  		"3:
  "						\
4260415f6   Russell King   ARM: fix build er...
277
278
  		"	.pushsection .fixup,\"ax\"
  "		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
284
  		"	.align	2
  "				\
  		"4:	mov	%0, #1
  "			\
  		"	b	3b
  "				\
4260415f6   Russell King   ARM: fix build er...
285
286
287
288
  		"	.popsection
  "				\
  		"	.pushsection __ex_table,\"a\"
  "	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
  		"	.align	3
  "				\
  		"	.long	1b, 4b
  "			\
  		"	.long	2b, 4b
  "			\
4260415f6   Russell King   ARM: fix build er...
295
296
  		"	.popsection
  "				\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
  		: "=r" (err), "=&r" (v), "=&r" (a)		\
  		: "0" (err), "1" (v), "2" (a));			\
  		if (err)					\
  			goto fault;				\
  	} while (0)
  
  #define put16_unaligned_check(val,addr)  \
  	__put16_unaligned_check("strb",val,addr)
  
  #define put16t_unaligned_check(val,addr) \
  	__put16_unaligned_check("strbt",val,addr)
  
  #define __put32_unaligned_check(ins,val,addr)			\
  	do {							\
  		unsigned int err = 0, v = val, a = addr;	\
  		__asm__( FIRST_BYTE_32				\
347c8b70b   Catalin Marinas   Thumb-2: Implemen...
313
314
315
316
317
318
  	 ARM(	"1:	"ins"	%1, [%2], #1
  "	)		\
  	 THUMB(	"1:	"ins"	%1, [%2]
  "	)		\
  	 THUMB(	"	add	%2, %2, #1
  "	)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
  		"	mov	%1, %1, "NEXT_BYTE"
  "		\
347c8b70b   Catalin Marinas   Thumb-2: Implemen...
321
322
323
324
325
326
  	 ARM(	"2:	"ins"	%1, [%2], #1
  "	)		\
  	 THUMB(	"2:	"ins"	%1, [%2]
  "	)		\
  	 THUMB(	"	add	%2, %2, #1
  "	)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
  		"	mov	%1, %1, "NEXT_BYTE"
  "		\
347c8b70b   Catalin Marinas   Thumb-2: Implemen...
329
330
331
332
333
334
  	 ARM(	"3:	"ins"	%1, [%2], #1
  "	)		\
  	 THUMB(	"3:	"ins"	%1, [%2]
  "	)		\
  	 THUMB(	"	add	%2, %2, #1
  "	)		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
335
336
337
338
339
340
  		"	mov	%1, %1, "NEXT_BYTE"
  "		\
  		"4:	"ins"	%1, [%2]
  "			\
  		"5:
  "						\
4260415f6   Russell King   ARM: fix build er...
341
342
  		"	.pushsection .fixup,\"ax\"
  "		\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
343
344
345
346
347
348
  		"	.align	2
  "				\
  		"6:	mov	%0, #1
  "			\
  		"	b	5b
  "				\
4260415f6   Russell King   ARM: fix build er...
349
350
351
352
  		"	.popsection
  "				\
  		"	.pushsection __ex_table,\"a\"
  "	\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
353
354
355
356
357
358
359
360
361
362
  		"	.align	3
  "				\
  		"	.long	1b, 6b
  "			\
  		"	.long	2b, 6b
  "			\
  		"	.long	3b, 6b
  "			\
  		"	.long	4b, 6b
  "			\
4260415f6   Russell King   ARM: fix build er...
363
364
  		"	.popsection
  "				\
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
368
369
  		: "=r" (err), "=&r" (v), "=&r" (a)		\
  		: "0" (err), "1" (v), "2" (a));			\
  		if (err)					\
  			goto fault;				\
  	} while (0)
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
370
  #define put32_unaligned_check(val,addr) \
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
  	__put32_unaligned_check("strb", val, addr)
  
  #define put32t_unaligned_check(val,addr) \
  	__put32_unaligned_check("strbt", val, addr)
  
  static void
  do_alignment_finish_ldst(unsigned long addr, unsigned long instr, struct pt_regs *regs, union offset_union offset)
  {
  	if (!LDST_U_BIT(instr))
  		offset.un = -offset.un;
  
  	if (!LDST_P_BIT(instr))
  		addr += offset.un;
  
  	if (!LDST_P_BIT(instr) || LDST_W_BIT(instr))
  		regs->uregs[RN_BITS(instr)] = addr;
  }
  
  static int
  do_alignment_ldrhstrh(unsigned long addr, unsigned long instr, struct pt_regs *regs)
  {
  	unsigned int rd = RD_BITS(instr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  	ai_half += 1;
  
  	if (user_mode(regs))
  		goto user;
  
  	if (LDST_L_BIT(instr)) {
  		unsigned long val;
  		get16_unaligned_check(val, addr);
  
  		/* signed half-word? */
  		if (instr & 0x40)
  			val = (signed long)((signed short) val);
  
  		regs->uregs[rd] = val;
  	} else
  		put16_unaligned_check(regs->uregs[rd], addr);
  
  	return TYPE_LDST;
  
   user:
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
413
414
415
  	if (LDST_L_BIT(instr)) {
  		unsigned long val;
  		get16t_unaligned_check(val, addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
416

737d0bb77   George G. Davis   [ARM] 2969/1: mis...
417
418
419
  		/* signed half-word? */
  		if (instr & 0x40)
  			val = (signed long)((signed short) val);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
420

737d0bb77   George G. Davis   [ARM] 2969/1: mis...
421
422
423
  		regs->uregs[rd] = val;
  	} else
  		put16t_unaligned_check(regs->uregs[rd], addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
424

737d0bb77   George G. Davis   [ARM] 2969/1: mis...
425
  	return TYPE_LDST;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
426

f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
427
428
429
430
431
432
433
434
435
   fault:
  	return TYPE_FAULT;
  }
  
  static int
  do_alignment_ldrdstrd(unsigned long addr, unsigned long instr,
  		      struct pt_regs *regs)
  {
  	unsigned int rd = RD_BITS(instr);
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
436
437
438
439
440
441
442
443
  	unsigned int rd2;
  	int load;
  
  	if ((instr & 0xfe000000) == 0xe8000000) {
  		/* ARMv7 Thumb-2 32-bit LDRD/STRD */
  		rd2 = (instr >> 8) & 0xf;
  		load = !!(LDST_L_BIT(instr));
  	} else if (((rd & 1) == 1) || (rd == 14))
19da83f63   George G. Davis   [ARM] 2959/1: Add...
444
  		goto bad;
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
445
446
447
448
  	else {
  		load = ((instr & 0xf0) == 0xd0);
  		rd2 = rd + 1;
  	}
19da83f63   George G. Davis   [ARM] 2959/1: Add...
449

f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
450
451
452
453
  	ai_dword += 1;
  
  	if (user_mode(regs))
  		goto user;
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
454
  	if (load) {
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
455
456
457
  		unsigned long val;
  		get32_unaligned_check(val, addr);
  		regs->uregs[rd] = val;
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
458
  		get32_unaligned_check(val, addr + 4);
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
459
  		regs->uregs[rd2] = val;
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
460
461
  	} else {
  		put32_unaligned_check(regs->uregs[rd], addr);
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
462
  		put32_unaligned_check(regs->uregs[rd2], addr + 4);
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
463
464
465
466
467
  	}
  
  	return TYPE_LDST;
  
   user:
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
468
  	if (load) {
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
469
470
471
  		unsigned long val;
  		get32t_unaligned_check(val, addr);
  		regs->uregs[rd] = val;
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
472
  		get32t_unaligned_check(val, addr + 4);
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
473
  		regs->uregs[rd2] = val;
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
474
475
  	} else {
  		put32t_unaligned_check(regs->uregs[rd], addr);
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
476
  		put32t_unaligned_check(regs->uregs[rd2], addr + 4);
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
477
478
479
  	}
  
  	return TYPE_LDST;
19da83f63   George G. Davis   [ARM] 2959/1: Add...
480
481
   bad:
  	return TYPE_ERROR;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
   fault:
  	return TYPE_FAULT;
  }
  
  static int
  do_alignment_ldrstr(unsigned long addr, unsigned long instr, struct pt_regs *regs)
  {
  	unsigned int rd = RD_BITS(instr);
  
  	ai_word += 1;
  
  	if ((!LDST_P_BIT(instr) && LDST_W_BIT(instr)) || user_mode(regs))
  		goto trans;
  
  	if (LDST_L_BIT(instr)) {
  		unsigned int val;
  		get32_unaligned_check(val, addr);
  		regs->uregs[rd] = val;
  	} else
  		put32_unaligned_check(regs->uregs[rd], addr);
  	return TYPE_LDST;
  
   trans:
  	if (LDST_L_BIT(instr)) {
  		unsigned int val;
  		get32t_unaligned_check(val, addr);
  		regs->uregs[rd] = val;
  	} else
  		put32t_unaligned_check(regs->uregs[rd], addr);
  	return TYPE_LDST;
  
   fault:
  	return TYPE_FAULT;
  }
  
  /*
   * LDM/STM alignment handler.
   *
   * There are 4 variants of this instruction:
   *
   * B = rn pointer before instruction, A = rn pointer after instruction
   *              ------ increasing address ----->
   *	        |    | r0 | r1 | ... | rx |    |
   * PU = 01             B                    A
   * PU = 11        B                    A
   * PU = 00        A                    B
   * PU = 10             A                    B
   */
  static int
  do_alignment_ldmstm(unsigned long addr, unsigned long instr, struct pt_regs *regs)
  {
  	unsigned int rd, rn, correction, nr_regs, regbits;
  	unsigned long eaddr, newaddr;
  
  	if (LDM_S_BIT(instr))
  		goto bad;
  
  	correction = 4; /* processor implementation defined */
  	regs->ARM_pc += correction;
  
  	ai_multi += 1;
  
  	/* count the number of registers in the mask to be transferred */
  	nr_regs = hweight16(REGMASK_BITS(instr)) * 4;
  
  	rn = RN_BITS(instr);
  	newaddr = eaddr = regs->uregs[rn];
  
  	if (!LDST_U_BIT(instr))
  		nr_regs = -nr_regs;
  	newaddr += nr_regs;
  	if (!LDST_U_BIT(instr))
  		eaddr = newaddr;
  
  	if (LDST_P_EQ_U(instr))	/* U = P */
  		eaddr += 4;
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
558
  	/*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  	 * For alignment faults on the ARM922T/ARM920T the MMU  makes
  	 * the FSR (and hence addr) equal to the updated base address
  	 * of the multiple access rather than the restored value.
  	 * Switch this message off if we've got a ARM92[02], otherwise
  	 * [ls]dm alignment faults are noisy!
  	 */
  #if !(defined CONFIG_CPU_ARM922T)  && !(defined CONFIG_CPU_ARM920T)
  	/*
  	 * This is a "hint" - we already have eaddr worked out by the
  	 * processor for us.
  	 */
  	if (addr != eaddr) {
  		printk(KERN_ERR "LDMSTM: PC = %08lx, instr = %08lx, "
  			"addr = %08lx, eaddr = %08lx
  ",
  			 instruction_pointer(regs), instr, addr, eaddr);
  		show_regs(regs);
  	}
  #endif
  
  	if (user_mode(regs)) {
  		for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  		     regbits >>= 1, rd += 1)
  			if (regbits & 1) {
  				if (LDST_L_BIT(instr)) {
  					unsigned int val;
  					get32t_unaligned_check(val, eaddr);
  					regs->uregs[rd] = val;
  				} else
  					put32t_unaligned_check(regs->uregs[rd], eaddr);
  				eaddr += 4;
  			}
  	} else {
  		for (regbits = REGMASK_BITS(instr), rd = 0; regbits;
  		     regbits >>= 1, rd += 1)
  			if (regbits & 1) {
  				if (LDST_L_BIT(instr)) {
  					unsigned int val;
  					get32_unaligned_check(val, eaddr);
  					regs->uregs[rd] = val;
  				} else
  					put32_unaligned_check(regs->uregs[rd], eaddr);
  				eaddr += 4;
  			}
  	}
  
  	if (LDST_W_BIT(instr))
  		regs->uregs[rn] = newaddr;
  	if (!LDST_L_BIT(instr) || !(REGMASK_BITS(instr) & (1 << 15)))
  		regs->ARM_pc -= correction;
  	return TYPE_DONE;
  
  fault:
  	regs->ARM_pc -= correction;
  	return TYPE_FAULT;
  
  bad:
  	printk(KERN_ERR "Alignment trap: not handling ldm with s-bit set
  ");
  	return TYPE_ERROR;
  }
  
  /*
   * Convert Thumb ld/st instruction forms to equivalent ARM instructions so
   * we can reuse ARM userland alignment fault fixups for Thumb.
   *
   * This implementation was initially based on the algorithm found in
   * gdb/sim/arm/thumbemu.c. It is basically just a code reduction of same
   * to convert only Thumb ld/st instruction forms to equivalent ARM forms.
   *
   * NOTES:
   * 1. Comments below refer to ARM ARM DDI0100E Thumb Instruction sections.
   * 2. If for some reason we're passed an non-ld/st Thumb instruction to
   *    decode, we return 0xdeadc0de. This should never happen under normal
   *    circumstances but if it does, we've got other problems to deal with
   *    elsewhere and we obviously can't fix those problems here.
   */
  
  static unsigned long
  thumb2arm(u16 tinstr)
  {
  	u32 L = (tinstr & (1<<11)) >> 11;
  
  	switch ((tinstr & 0xf800) >> 11) {
  	/* 6.5.1 Format 1: */
  	case 0x6000 >> 11:				/* 7.1.52 STR(1) */
  	case 0x6800 >> 11:				/* 7.1.26 LDR(1) */
  	case 0x7000 >> 11:				/* 7.1.55 STRB(1) */
  	case 0x7800 >> 11:				/* 7.1.30 LDRB(1) */
  		return 0xe5800000 |
  			((tinstr & (1<<12)) << (22-12)) |	/* fixup */
  			(L<<20) |				/* L==1? */
  			((tinstr & (7<<0)) << (12-0)) |		/* Rd */
  			((tinstr & (7<<3)) << (16-3)) |		/* Rn */
  			((tinstr & (31<<6)) >>			/* immed_5 */
  				(6 - ((tinstr & (1<<12)) ? 0 : 2)));
  	case 0x8000 >> 11:				/* 7.1.57 STRH(1) */
  	case 0x8800 >> 11:				/* 7.1.32 LDRH(1) */
  		return 0xe1c000b0 |
  			(L<<20) |				/* L==1? */
  			((tinstr & (7<<0)) << (12-0)) |		/* Rd */
  			((tinstr & (7<<3)) << (16-3)) |		/* Rn */
  			((tinstr & (7<<6)) >> (6-1)) |	 /* immed_5[2:0] */
  			((tinstr & (3<<9)) >> (9-8));	 /* immed_5[4:3] */
  
  	/* 6.5.1 Format 2: */
  	case 0x5000 >> 11:
  	case 0x5800 >> 11:
  		{
  			static const u32 subset[8] = {
  				0xe7800000,		/* 7.1.53 STR(2) */
  				0xe18000b0,		/* 7.1.58 STRH(2) */
  				0xe7c00000,		/* 7.1.56 STRB(2) */
  				0xe19000d0,		/* 7.1.34 LDRSB */
  				0xe7900000,		/* 7.1.27 LDR(2) */
  				0xe19000b0,		/* 7.1.33 LDRH(2) */
  				0xe7d00000,		/* 7.1.31 LDRB(2) */
  				0xe19000f0		/* 7.1.35 LDRSH */
  			};
  			return subset[(tinstr & (7<<9)) >> 9] |
  			    ((tinstr & (7<<0)) << (12-0)) |	/* Rd */
  			    ((tinstr & (7<<3)) << (16-3)) |	/* Rn */
  			    ((tinstr & (7<<6)) >> (6-0));	/* Rm */
  		}
  
  	/* 6.5.1 Format 3: */
  	case 0x4800 >> 11:				/* 7.1.28 LDR(3) */
  		/* NOTE: This case is not technically possible. We're
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
687
  		 *	 loading 32-bit memory data via PC relative
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
  		 *	 addressing mode. So we can and should eliminate
  		 *	 this case. But I'll leave it here for now.
  		 */
  		return 0xe59f0000 |
  		    ((tinstr & (7<<8)) << (12-8)) |		/* Rd */
  		    ((tinstr & 255) << (2-0));			/* immed_8 */
  
  	/* 6.5.1 Format 4: */
  	case 0x9000 >> 11:				/* 7.1.54 STR(3) */
  	case 0x9800 >> 11:				/* 7.1.29 LDR(4) */
  		return 0xe58d0000 |
  			(L<<20) |				/* L==1? */
  			((tinstr & (7<<8)) << (12-8)) |		/* Rd */
  			((tinstr & 255) << 2);			/* immed_8 */
  
  	/* 6.6.1 Format 1: */
  	case 0xc000 >> 11:				/* 7.1.51 STMIA */
  	case 0xc800 >> 11:				/* 7.1.25 LDMIA */
  		{
  			u32 Rn = (tinstr & (7<<8)) >> 8;
  			u32 W = ((L<<Rn) & (tinstr&255)) ? 0 : 1<<21;
  
  			return 0xe8800000 | W | (L<<20) | (Rn<<16) |
  				(tinstr&255);
  		}
  
  	/* 6.6.1 Format 2: */
  	case 0xb000 >> 11:				/* 7.1.48 PUSH */
  	case 0xb800 >> 11:				/* 7.1.47 POP */
  		if ((tinstr & (3 << 9)) == 0x0400) {
  			static const u32 subset[4] = {
  				0xe92d0000,	/* STMDB sp!,{registers} */
  				0xe92d4000,	/* STMDB sp!,{registers,lr} */
  				0xe8bd0000,	/* LDMIA sp!,{registers} */
  				0xe8bd8000	/* LDMIA sp!,{registers,pc} */
  			};
  			return subset[(L<<1) | ((tinstr & (1<<8)) >> 8)] |
  			    (tinstr & 255);		/* register_list */
  		}
  		/* Else fall through for illegal instruction case */
  
  	default:
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
  		return BAD_INSTR;
  	}
  }
  
  /*
   * Convert Thumb-2 32 bit LDM, STM, LDRD, STRD to equivalent instruction
   * handlable by ARM alignment handler, also find the corresponding handler,
   * so that we can reuse ARM userland alignment fault fixups for Thumb.
   *
   * @pinstr: original Thumb-2 instruction; returns new handlable instruction
   * @regs: register context.
   * @poffset: return offset from faulted addr for later writeback
   *
   * NOTES:
   * 1. Comments below refer to ARMv7 DDI0406A Thumb Instruction sections.
   * 2. Register name Rt from ARMv7 is same as Rd from ARMv6 (Rd is Rt)
   */
  static void *
  do_alignment_t32_to_handler(unsigned long *pinstr, struct pt_regs *regs,
  			    union offset_union *poffset)
  {
  	unsigned long instr = *pinstr;
  	u16 tinst1 = (instr >> 16) & 0xffff;
  	u16 tinst2 = instr & 0xffff;
  	poffset->un = 0;
  
  	switch (tinst1 & 0xffe0) {
  	/* A6.3.5 Load/Store multiple */
  	case 0xe880:		/* STM/STMIA/STMEA,LDM/LDMIA, PUSH/POP T2 */
  	case 0xe8a0:		/* ...above writeback version */
  	case 0xe900:		/* STMDB/STMFD, LDMDB/LDMEA */
  	case 0xe920:		/* ...above writeback version */
  		/* no need offset decision since handler calculates it */
  		return do_alignment_ldmstm;
  
  	case 0xf840:		/* POP/PUSH T3 (single register) */
  		if (RN_BITS(instr) == 13 && (tinst2 & 0x09ff) == 0x0904) {
  			u32 L = !!(LDST_L_BIT(instr));
  			const u32 subset[2] = {
  				0xe92d0000,	/* STMDB sp!,{registers} */
  				0xe8bd0000,	/* LDMIA sp!,{registers} */
  			};
  			*pinstr = subset[L] | (1<<RD_BITS(instr));
  			return do_alignment_ldmstm;
  		}
  		/* Else fall through for illegal instruction case */
  		break;
  
  	/* A6.3.6 Load/store double, STRD/LDRD(immed, lit, reg) */
  	case 0xe860:
  	case 0xe960:
  	case 0xe8e0:
  	case 0xe9e0:
  		poffset->un = (tinst2 & 0xff) << 2;
  	case 0xe940:
  	case 0xe9c0:
  		return do_alignment_ldrdstrd;
  
  	/*
  	 * No need to handle load/store instructions up to word size
  	 * since ARMv6 and later CPUs can perform unaligned accesses.
  	 */
  	default:
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
794
  	}
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
795
  	return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
796
797
798
799
800
801
802
803
804
805
806
807
  }
  
  static int
  do_alignment(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  {
  	union offset_union offset;
  	unsigned long instr = 0, instrptr;
  	int (*handler)(unsigned long addr, unsigned long instr, struct pt_regs *regs);
  	unsigned int type;
  	mm_segment_t fs;
  	unsigned int fault;
  	u16 tinstr = 0;
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
808
809
  	int isize = 4;
  	int thumb2_32b = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
810

02fe2845d   Russell King   ARM: entry: avoid...
811
812
  	if (interrupts_enabled(regs))
  		local_irq_enable();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
813
814
815
816
  	instrptr = instruction_pointer(regs);
  
  	fs = get_fs();
  	set_fs(KERNEL_DS);
f83436856   Yoann Padioleau   parse errors in i...
817
  	if (thumb_mode(regs)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
818
  		fault = __get_user(tinstr, (u16 *)(instrptr & ~1));
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
819
820
821
822
823
824
825
826
827
828
829
830
831
  		if (!fault) {
  			if (cpu_architecture() >= CPU_ARCH_ARMv7 &&
  			    IS_T32(tinstr)) {
  				/* Thumb-2 32-bit */
  				u16 tinst2 = 0;
  				fault = __get_user(tinst2, (u16 *)(instrptr+2));
  				instr = (tinstr << 16) | tinst2;
  				thumb2_32b = 1;
  			} else {
  				isize = 2;
  				instr = thumb2arm(tinstr);
  			}
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
832
833
834
835
836
837
  	} else
  		fault = __get_user(instr, (u32 *)instrptr);
  	set_fs(fs);
  
  	if (fault) {
  		type = TYPE_FAULT;
737d0bb77   George G. Davis   [ARM] 2969/1: mis...
838
  		goto bad_or_fault;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
839
840
841
842
843
844
845
846
  	}
  
  	if (user_mode(regs))
  		goto user;
  
  	ai_sys += 1;
  
   fixup:
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
847
  	regs->ARM_pc += isize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
848
849
  
  	switch (CODING_BITS(instr)) {
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
850
851
  	case 0x00000000:	/* 3.13.4 load/store instruction extensions */
  		if (LDSTHD_I_BIT(instr))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
852
853
854
  			offset.un = (instr & 0xf00) >> 4 | (instr & 15);
  		else
  			offset.un = regs->uregs[RM_BITS(instr)];
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
855
856
857
858
859
860
861
  
  		if ((instr & 0x000000f0) == 0x000000b0 || /* LDRH, STRH */
  		    (instr & 0x001000f0) == 0x001000f0)   /* LDRSH */
  			handler = do_alignment_ldrhstrh;
  		else if ((instr & 0x001000f0) == 0x000000d0 || /* LDRD */
  			 (instr & 0x001000f0) == 0x000000f0)   /* STRD */
  			handler = do_alignment_ldrdstrd;
19da83f63   George G. Davis   [ARM] 2959/1: Add...
862
863
  		else if ((instr & 0x01f00ff0) == 0x01000090) /* SWP */
  			goto swp;
f21ee2d42   Steve Longerbeam   [ARM] 2867/2: una...
864
865
  		else
  			goto bad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
  		break;
  
  	case 0x04000000:	/* ldr or str immediate */
  		offset.un = OFFSET_BITS(instr);
  		handler = do_alignment_ldrstr;
  		break;
  
  	case 0x06000000:	/* ldr or str register */
  		offset.un = regs->uregs[RM_BITS(instr)];
  
  		if (IS_SHIFT(instr)) {
  			unsigned int shiftval = SHIFT_BITS(instr);
  
  			switch(SHIFT_TYPE(instr)) {
  			case SHIFT_LSL:
  				offset.un <<= shiftval;
  				break;
  
  			case SHIFT_LSR:
  				offset.un >>= shiftval;
  				break;
  
  			case SHIFT_ASR:
  				offset.sn >>= shiftval;
  				break;
  
  			case SHIFT_RORRRX:
  				if (shiftval == 0) {
  					offset.un >>= 1;
  					if (regs->ARM_cpsr & PSR_C_BIT)
  						offset.un |= 1 << 31;
  				} else
  					offset.un = offset.un >> shiftval |
  							  offset.un << (32 - shiftval);
  				break;
  			}
  		}
  		handler = do_alignment_ldrstr;
  		break;
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
905
906
907
908
909
  	case 0x08000000:	/* ldm or stm, or thumb-2 32bit instruction */
  		if (thumb2_32b)
  			handler = do_alignment_t32_to_handler(&instr, regs, &offset);
  		else
  			handler = do_alignment_ldmstm;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
910
911
912
913
914
  		break;
  
  	default:
  		goto bad;
  	}
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
915
916
  	if (!handler)
  		goto bad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
917
  	type = handler(addr, instr, regs);
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
918
919
  	if (type == TYPE_ERROR || type == TYPE_FAULT) {
  		regs->ARM_pc -= isize;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
920
  		goto bad_or_fault;
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
921
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
922
923
924
925
926
927
928
929
930
  
  	if (type == TYPE_LDST)
  		do_alignment_finish_ldst(addr, instr, regs, offset);
  
  	return 0;
  
   bad_or_fault:
  	if (type == TYPE_ERROR)
  		goto bad;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
931
932
933
  	/*
  	 * We got a fault - fix it up, or die.
  	 */
e5beac371   Russell King   [ARM] do_bad_area...
934
  	do_bad_area(addr, fsr, regs);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
935
  	return 0;
19da83f63   George G. Davis   [ARM] 2959/1: Add...
936
937
938
   swp:
  	printk(KERN_ERR "Alignment trap: not handling swp instruction
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
939
940
941
942
943
944
945
   bad:
  	/*
  	 * Oops, we didn't handle the instruction.
  	 */
  	printk(KERN_ERR "Alignment trap: not handling instruction "
  		"%0*lx at [<%08lx>]
  ",
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
946
947
  		isize << 1,
  		isize == 2 ? tinstr : instr, instrptr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
948
949
950
951
952
  	ai_skipped += 1;
  	return 1;
  
   user:
  	ai_user += 1;
baa745a33   Russell King   [ARM] Fix alignme...
953
  	if (ai_usermode & UM_WARN)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
954
955
956
  		printk("Alignment trap: %s (%d) PC=0x%08lx Instr=0x%0*lx "
  		       "Address=0x%08lx FSR 0x%03x
  ", current->comm,
19c5870c0   Alexey Dobriyan   Use helpers to ob...
957
  			task_pid_nr(current), instrptr,
c2860d43f   George G. Davis   [ARM] 5540/1: 32-...
958
959
  			isize << 1,
  			isize == 2 ? tinstr : instr,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
960
  		        addr, fsr);
baa745a33   Russell King   [ARM] Fix alignme...
961
  	if (ai_usermode & UM_FIXUP)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
962
  		goto fixup;
2102a65e6   Dave Martin   ARM: 7008/1: alig...
963
964
965
966
967
968
969
970
971
972
  	if (ai_usermode & UM_SIGNAL) {
  		siginfo_t si;
  
  		si.si_signo = SIGBUS;
  		si.si_errno = 0;
  		si.si_code = BUS_ADRALN;
  		si.si_addr = (void __user *)addr;
  
  		force_sig_info(si.si_signo, &si, current);
  	} else {
2f27bf834   Nicolas Pitre   ARM: 6401/1: plug...
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
  		/*
  		 * We're about to disable the alignment trap and return to
  		 * user space.  But if an interrupt occurs before actually
  		 * reaching user space, then the IRQ vector entry code will
  		 * notice that we were still in kernel space and therefore
  		 * the alignment trap won't be re-enabled in that case as it
  		 * is presumed to be always on from kernel space.
  		 * Let's prevent that race by disabling interrupts here (they
  		 * are disabled on the way back to user space anyway in
  		 * entry-common.S) and disable the alignment trap only if
  		 * there is no work pending for this thread.
  		 */
  		raw_local_irq_disable();
  		if (!(current_thread_info()->flags & _TIF_WORK_MASK))
  			set_cr(cr_no_alignment);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
  
  	return 0;
  }
  
  /*
   * This needs to be done after sysctl_init, otherwise sys/ will be
   * overwritten.  Actually, this shouldn't be in sys/ at all since
   * it isn't a sysctl, and it doesn't contain sysctl information.
   * We now locate it in /proc/cpu/alignment instead.
   */
  static int __init alignment_init(void)
  {
  #ifdef CONFIG_PROC_FS
  	struct proc_dir_entry *res;
b7072c63c   Alexey Dobriyan   ARM: convert /pro...
1003
1004
  	res = proc_create("cpu/alignment", S_IWUSR | S_IRUGO, NULL,
  			  &alignment_proc_fops);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1005
1006
  	if (!res)
  		return -ENOMEM;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1007
  #endif
088c01f1e   Dave Martin   ARM: 7007/1: alig...
1008
  	if (cpu_is_v6_unaligned()) {
baa745a33   Russell King   [ARM] Fix alignme...
1009
1010
1011
  		cr_alignment &= ~CR_A;
  		cr_no_alignment &= ~CR_A;
  		set_cr(cr_alignment);
088c01f1e   Dave Martin   ARM: 7007/1: alig...
1012
  		ai_usermode = safe_usermode(ai_usermode, false);
baa745a33   Russell King   [ARM] Fix alignme...
1013
  	}
f7b8156d1   Catalin Marinas   ARM: LPAE: Add fa...
1014
  	hook_fault_code(FAULT_CODE_ALIGNMENT, do_alignment, SIGBUS, BUS_ADRALN,
6338a6aa7   Kirill A. Shutemov   ARM: 6269/1: Add ...
1015
  			"alignment exception");
b8ab5397b   Kirill A. Shutemov   ARM: 6268/1: ARMv...
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
  
  	/*
  	 * ARMv6K and ARMv7 use fault status 3 (0b00011) as Access Flag section
  	 * fault, not as alignment error.
  	 *
  	 * TODO: handle ARMv6K properly. Runtime check for 'K' extension is
  	 * needed.
  	 */
  	if (cpu_architecture() <= CPU_ARCH_ARMv6) {
  		hook_fault_code(3, do_alignment, SIGBUS, BUS_ADRALN,
  				"alignment exception");
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1028
1029
1030
1031
1032
  
  	return 0;
  }
  
  fs_initcall(alignment_init);