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