1 // SPDX-License-Identifier: GPL-2.0
3 * unaligned.c: Unaligned load/store trap handling with special
4 * cases for the kernel to do them more quickly.
6 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
7 * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
11 #include <linux/kernel.h>
12 #include <linux/sched/signal.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>
20 #include <asm/setup.h>
25 load
, /* ld, ldd, ldh, ldsh */
26 store
, /* st, std, sth, stsh */
27 both
, /* Swap, ldstub, etc. */
33 static inline enum direction
decode_direction(unsigned int insn
)
35 unsigned long tmp
= (insn
>> 21) & 1;
40 if(((insn
>>19)&0x3f) == 15)
47 /* 8 = double-word, 4 = word, 2 = half-word */
48 static inline int decode_access_size(unsigned int insn
)
50 insn
= (insn
>> 19) & 3;
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. */
65 /* 0x400000 = signed, 0 = unsigned */
66 static inline int decode_signedness(unsigned int insn
)
68 return (insn
& 0x400000);
71 static inline void maybe_flush_windows(unsigned int rs1
, unsigned int rs2
,
74 if(rs2
>= 16 || rs1
>= 16 || rd
>= 16) {
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");
88 static inline int sign_extend_imm13(int imm
)
90 return imm
<< 19 >> 19;
93 static inline unsigned long fetch_reg(unsigned int reg
, struct pt_regs
*regs
)
95 struct reg_window32
*win
;
98 return (!reg
? 0 : regs
->u_regs
[reg
]);
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... */
105 static inline unsigned long safe_fetch_reg(unsigned int reg
, struct pt_regs
*regs
)
107 struct reg_window32 __user
*win
;
111 return (!reg
? 0 : regs
->u_regs
[reg
]);
113 /* Ho hum, the slightly complicated case. */
114 win
= (struct reg_window32 __user
*) regs
->u_regs
[UREG_FP
];
116 if ((unsigned long)win
& 3)
119 if (get_user(ret
, &win
->locals
[reg
- 16]))
125 static inline unsigned long *fetch_reg_addr(unsigned int reg
, struct pt_regs
*regs
)
127 struct reg_window32
*win
;
130 return ®s
->u_regs
[reg
];
131 win
= (struct reg_window32
*) regs
->u_regs
[UREG_FP
];
132 return &win
->locals
[reg
- 16];
135 static unsigned long compute_effective_address(struct pt_regs
*regs
,
138 unsigned int rs1
= (insn
>> 14) & 0x1f;
139 unsigned int rs2
= insn
& 0x1f;
140 unsigned int rd
= (insn
>> 25) & 0x1f;
143 maybe_flush_windows(rs1
, 0, rd
);
144 return (fetch_reg(rs1
, regs
) + sign_extend_imm13(insn
));
146 maybe_flush_windows(rs1
, rs2
, rd
);
147 return (fetch_reg(rs1
, regs
) + fetch_reg(rs2
, regs
));
151 unsigned long safe_compute_effective_address(struct pt_regs
*regs
,
154 unsigned int rs1
= (insn
>> 14) & 0x1f;
155 unsigned int rs2
= insn
& 0x1f;
156 unsigned int rd
= (insn
>> 25) & 0x1f;
159 maybe_flush_windows(rs1
, 0, rd
);
160 return (safe_fetch_reg(rs1
, regs
) + sign_extend_imm13(insn
));
162 maybe_flush_windows(rs1
, rs2
, rd
);
163 return (safe_fetch_reg(rs1
, regs
) + safe_fetch_reg(rs2
, regs
));
167 /* This is just to make gcc think panic does return... */
168 static void unaligned_panic(char *str
)
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
);
179 static int do_int_store(int reg_num
, int size
, unsigned long *dst_addr
,
180 struct pt_regs
*regs
)
182 unsigned long zero
[2] = { 0, 0 };
183 unsigned long *src_val
;
186 src_val
= fetch_reg_addr(reg_num
, regs
);
190 zero
[1] = fetch_reg(1, regs
);
192 return __do_int_store(dst_addr
, size
, src_val
);
195 extern void smp_capture(void);
196 extern void smp_release(void);
198 static inline void advance(struct pt_regs
*regs
)
200 regs
->pc
= regs
->npc
;
204 static inline int floating_point_load_or_store_p(unsigned int insn
)
206 return (insn
>> 24) & 1;
209 static inline int ok_for_kernel(unsigned int insn
)
211 return !floating_point_load_or_store_p(insn
);
214 static void kernel_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
)
216 unsigned long g2
= regs
->u_regs
[UREG_G2
];
217 unsigned long fixup
= search_extables_range(regs
->pc
, &g2
);
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");
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
);
236 regs
->npc
= regs
->pc
+ 4;
237 regs
->u_regs
[UREG_G2
] = g2
;
240 asmlinkage
void kernel_unaligned_trap(struct pt_regs
*regs
, unsigned int insn
)
242 enum direction dir
= decode_direction(insn
);
243 int size
= decode_access_size(insn
);
245 if(!ok_for_kernel(insn
) || dir
== both
) {
246 printk("Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
248 unaligned_panic("Wheee. Kernel does fpu/atomic unaligned load/store.");
250 unsigned long addr
= compute_effective_address(regs
, insn
);
253 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
, 1, regs
, addr
);
256 err
= do_int_load(fetch_reg_addr(((insn
>>25)&0x1f),
258 size
, (unsigned long *) addr
,
259 decode_signedness(insn
));
263 err
= do_int_store(((insn
>>25)&0x1f), size
,
264 (unsigned long *) addr
, regs
);
267 panic("Impossible kernel unaligned trap.");
271 kernel_mna_trap_fault(regs
, insn
);
277 static inline int ok_for_user(struct pt_regs
*regs
, unsigned int insn
,
281 int size
= ((insn
>> 19) & 3) == 3 ? 8 : 4;
283 if ((regs
->pc
| regs
->npc
) & 3)
286 /* Must access_ok() in all the necessary places. */
287 #define WINREG_ADDR(regnum) \
288 ((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
290 reg
= (insn
>> 25) & 0x1f;
292 if (!access_ok(WINREG_ADDR(reg
- 16), size
))
295 reg
= (insn
>> 14) & 0x1f;
297 if (!access_ok(WINREG_ADDR(reg
- 16), size
))
300 if (!(insn
& 0x2000)) {
303 if (!access_ok(WINREG_ADDR(reg
- 16), size
))
311 static void user_mna_trap_fault(struct pt_regs
*regs
, unsigned int insn
)
313 send_sig_fault(SIGBUS
, BUS_ADRALN
,
314 (void __user
*)safe_compute_effective_address(regs
, insn
),
318 asmlinkage
void user_unaligned_trap(struct pt_regs
*regs
, unsigned int insn
)
322 if(!(current
->thread
.flags
& SPARC_FLAG_UNALIGNED
) ||
323 (((insn
>> 30) & 3) != 3))
325 dir
= decode_direction(insn
);
326 if(!ok_for_user(regs
, insn
, dir
)) {
329 int err
, size
= decode_access_size(insn
);
332 if(floating_point_load_or_store_p(insn
)) {
333 printk("User FPU load/store unaligned unsupported.\n");
337 addr
= compute_effective_address(regs
, insn
);
338 perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS
, 1, regs
, addr
);
341 err
= do_int_load(fetch_reg_addr(((insn
>>25)&0x1f),
343 size
, (unsigned long *) addr
,
344 decode_signedness(insn
));
348 err
= do_int_store(((insn
>>25)&0x1f), size
,
349 (unsigned long *) addr
, regs
);
354 * This was supported in 2.4. However, we question
355 * the value of SWAP instruction across word boundaries.
357 printk("Unaligned SWAP unsupported.\n");
362 unaligned_panic("Impossible user unaligned trap.");
373 user_mna_trap_fault(regs
, insn
);