17 #include <linux/kernel.h>
18 #include <linux/sched.h>
19 #include <linux/tty.h>
22 #include <asm/intrinsics.h>
23 #include <asm/processor.h>
25 #include <asm/uaccess.h>
26 #include <asm/unaligned.h>
30 #undef DEBUG_UNALIGNED_TRAP
32 #ifdef DEBUG_UNALIGNED_TRAP
33 # define DPRINT(a...) do { printk("%s %u: ", __func__, __LINE__); printk (a); } while (0)
34 # define DDUMP(str,vp,len) dump(str, vp, len)
37 dump (
const char *
str,
void *vp,
size_t len)
39 unsigned char *
cp = vp;
43 for (i = 0; i < len; ++
i)
49 # define DDUMP(str,vp,len)
52 #define IA64_FIRST_STACKED_GR 32
53 #define IA64_FIRST_ROTATING_FR 32
54 #define SIGN_EXT9 0xffffffffffffff00ul
79 #define IA64_OPCODE_MASK 0x1ef
80 #define IA64_OPCODE_SHIFT 32
94 #define LDBIAS_OP 0x084
95 #define LDACQ_OP 0x085
97 #define LDCCLR_OP 0x088
98 #define LDCNC_OP 0x089
99 #define LDCCLRACQ_OP 0x08a
101 #define STREL_OP 0x08d
119 #define LD_IMM_OP 0x0a0
120 #define LDS_IMM_OP 0x0a1
121 #define LDA_IMM_OP 0x0a2
122 #define LDSA_IMM_OP 0x0a3
123 #define LDBIAS_IMM_OP 0x0a4
124 #define LDACQ_IMM_OP 0x0a5
126 #define LDCCLR_IMM_OP 0x0a8
127 #define LDCNC_IMM_OP 0x0a9
128 #define LDCCLRACQ_IMM_OP 0x0aa
129 #define ST_IMM_OP 0x0ac
130 #define STREL_IMM_OP 0x0ad
137 #define LDFS_OP 0x0c1
138 #define LDFA_OP 0x0c2
139 #define LDFSA_OP 0x0c3
141 #define LDFCCLR_OP 0x0c8
142 #define LDFCNC_OP 0x0c9
156 #define LDF_IMM_OP 0x0e0
157 #define LDFS_IMM_OP 0x0e1
158 #define LDFA_IMM_OP 0x0e2
159 #define LDFSA_IMM_OP 0x0e3
161 #define LDFCCLR_IMM_OP 0x0e8
162 #define LDFCNC_IMM_OP 0x0e9
163 #define STF_IMM_OP 0x0ec
171 unsigned long hint:2;
172 unsigned long x6_sz:2;
173 unsigned long x6_op:4;
197 #define RPO(x) ((size_t) &((struct pt_regs *)0)->x)
198 #define RSO(x) ((size_t) &((struct switch_stack *)0)->x)
200 #define RPT(x) (RPO(x) << 1)
201 #define RSW(x) (1| RSO(x)<<1)
203 #define GR_OFFS(x) (gr_info[x]>>1)
204 #define GR_IN_SW(x) (gr_info[x] & 0x1)
206 #define FR_OFFS(x) (fr_info[x]>>1)
207 #define FR_IN_SW(x) (fr_info[x] & 0x1)
209 static u16 gr_info[32]={
225 static u16 fr_info[32]={
243 invala_gr (
int regno)
245 # define F(reg) case reg: ia64_invala_gr(reg); break
248 F( 0);
F( 1);
F( 2);
F( 3);
F( 4);
F( 5);
F( 6);
F( 7);
249 F( 8);
F( 9);
F( 10);
F( 11);
F( 12);
F( 13);
F( 14);
F( 15);
250 F( 16);
F( 17);
F( 18);
F( 19);
F( 20);
F( 21);
F( 22);
F( 23);
251 F( 24);
F( 25);
F( 26);
F( 27);
F( 28);
F( 29);
F( 30);
F( 31);
252 F( 32);
F( 33);
F( 34);
F( 35);
F( 36);
F( 37);
F( 38);
F( 39);
253 F( 40);
F( 41);
F( 42);
F( 43);
F( 44);
F( 45);
F( 46);
F( 47);
254 F( 48);
F( 49);
F( 50);
F( 51);
F( 52);
F( 53);
F( 54);
F( 55);
255 F( 56);
F( 57);
F( 58);
F( 59);
F( 60);
F( 61);
F( 62);
F( 63);
256 F( 64);
F( 65);
F( 66);
F( 67);
F( 68);
F( 69);
F( 70);
F( 71);
257 F( 72);
F( 73);
F( 74);
F( 75);
F( 76);
F( 77);
F( 78);
F( 79);
258 F( 80);
F( 81);
F( 82);
F( 83);
F( 84);
F( 85);
F( 86);
F( 87);
259 F( 88);
F( 89);
F( 90);
F( 91);
F( 92);
F( 93);
F( 94);
F( 95);
260 F( 96);
F( 97);
F( 98);
F( 99);
F(100);
F(101);
F(102);
F(103);
261 F(104);
F(105);
F(106);
F(107);
F(108);
F(109);
F(110);
F(111);
262 F(112);
F(113);
F(114);
F(115);
F(116);
F(117);
F(118);
F(119);
263 F(120);
F(121);
F(122);
F(123);
F(124);
F(125);
F(126);
F(127);
270 invala_fr (
int regno)
272 # define F(reg) case reg: ia64_invala_fr(reg); break
275 F( 0);
F( 1);
F( 2);
F( 3);
F( 4);
F( 5);
F( 6);
F( 7);
276 F( 8);
F( 9);
F( 10);
F( 11);
F( 12);
F( 13);
F( 14);
F( 15);
277 F( 16);
F( 17);
F( 18);
F( 19);
F( 20);
F( 21);
F( 22);
F( 23);
278 F( 24);
F( 25);
F( 26);
F( 27);
F( 28);
F( 29);
F( 30);
F( 31);
279 F( 32);
F( 33);
F( 34);
F( 35);
F( 36);
F( 37);
F( 38);
F( 39);
280 F( 40);
F( 41);
F( 42);
F( 43);
F( 44);
F( 45);
F( 46);
F( 47);
281 F( 48);
F( 49);
F( 50);
F( 51);
F( 52);
F( 53);
F( 54);
F( 55);
282 F( 56);
F( 57);
F( 58);
F( 59);
F( 60);
F( 61);
F( 62);
F( 63);
283 F( 64);
F( 65);
F( 66);
F( 67);
F( 68);
F( 69);
F( 70);
F( 71);
284 F( 72);
F( 73);
F( 74);
F( 75);
F( 76);
F( 77);
F( 78);
F( 79);
285 F( 80);
F( 81);
F( 82);
F( 83);
F( 84);
F( 85);
F( 86);
F( 87);
286 F( 88);
F( 89);
F( 90);
F( 91);
F( 92);
F( 93);
F( 94);
F( 95);
287 F( 96);
F( 97);
F( 98);
F( 99);
F(100);
F(101);
F(102);
F(103);
288 F(104);
F(105);
F(106);
F(107);
F(108);
F(109);
F(110);
F(111);
289 F(112);
F(113);
F(114);
F(115);
F(116);
F(117);
F(118);
F(119);
290 F(120);
F(121);
F(122);
F(123);
F(124);
F(125);
F(126);
F(127);
295 static inline unsigned long
296 rotate_reg (
unsigned long sor,
unsigned long rrb,
unsigned long reg)
308 unsigned long *bsp, *bspstore, *
addr, *rnat_addr, *ubs_end;
310 unsigned long rnats, nat_mask;
311 unsigned long on_kbs;
312 long sof = (regs->
cr_ifs) & 0x7f;
313 long sor = 8 * ((regs->
cr_ifs >> 14) & 0xf);
314 long rrb_gr = (regs->
cr_ifs >> 18) & 0x7f;
319 DPRINT(
"ignoring write to r%lu; only %lu registers are allocated!\n", r1, sof);
324 ridx = rotate_reg(sor, rrb_gr, ridx);
326 DPRINT(
"r%lu, sw.bspstore=%lx pt.bspstore=%lx sof=%ld sol=%ld ridx=%ld\n",
329 on_kbs = ia64_rse_num_regs(kbs, (
unsigned long *) sw->
ar_bspstore);
330 addr = ia64_rse_skip_regs((
unsigned long *) sw->
ar_bspstore, -sof + ridx);
333 rnat_addr = ia64_rse_rnat_addr(addr);
336 nat_mask = 1
UL << ia64_rse_slot_num(addr);
340 *rnat_addr |= nat_mask;
342 *rnat_addr &= ~nat_mask;
347 DPRINT(
"ignoring kernel write to r%lu; register isn't on the kernel RBS!", r1);
352 ubs_end = ia64_rse_skip_regs(bspstore, on_kbs);
353 bsp = ia64_rse_skip_regs(ubs_end, -sof);
354 addr = ia64_rse_skip_regs(bsp, ridx);
356 DPRINT(
"ubs_end=%p bsp=%p addr=%p\n", (
void *) ubs_end, (
void *) bsp, (
void *) addr);
360 rnat_addr = ia64_rse_rnat_addr(addr);
362 ia64_peek(
current, sw, (
unsigned long) ubs_end, (
unsigned long) rnat_addr, &rnats);
363 DPRINT(
"rnat @%p = 0x%lx nat=%d old nat=%ld\n",
364 (
void *) rnat_addr, rnats, nat, (rnats >> ia64_rse_slot_num(addr)) & 1);
366 nat_mask = 1
UL << ia64_rse_slot_num(addr);
371 ia64_poke(
current, sw, (
unsigned long) ubs_end, (
unsigned long) rnat_addr, rnats);
373 DPRINT(
"rnat changed to @%p = 0x%lx\n", (
void *) rnat_addr, rnats);
378 get_rse_reg (
struct pt_regs *regs,
unsigned long r1,
unsigned long *val,
int *nat)
381 unsigned long *bsp, *
addr, *rnat_addr, *ubs_end, *bspstore;
383 unsigned long rnats, nat_mask;
384 unsigned long on_kbs;
385 long sof = (regs->
cr_ifs) & 0x7f;
386 long sor = 8 * ((regs->
cr_ifs >> 14) & 0xf);
387 long rrb_gr = (regs->
cr_ifs >> 18) & 0x7f;
392 DPRINT(
"ignoring read from r%lu; only %lu registers are allocated!\n", r1, sof);
397 ridx = rotate_reg(sor, rrb_gr, ridx);
399 DPRINT(
"r%lu, sw.bspstore=%lx pt.bspstore=%lx sof=%ld sol=%ld ridx=%ld\n",
402 on_kbs = ia64_rse_num_regs(kbs, (
unsigned long *) sw->
ar_bspstore);
403 addr = ia64_rse_skip_regs((
unsigned long *) sw->
ar_bspstore, -sof + ridx);
408 rnat_addr = ia64_rse_rnat_addr(addr);
411 nat_mask = 1
UL << ia64_rse_slot_num(addr);
412 *nat = (*rnat_addr & nat_mask) != 0;
418 DPRINT(
"ignoring kernel read of r%lu; register isn't on the RBS!", r1);
423 ubs_end = ia64_rse_skip_regs(bspstore, on_kbs);
424 bsp = ia64_rse_skip_regs(ubs_end, -sof);
425 addr = ia64_rse_skip_regs(bsp, ridx);
427 DPRINT(
"ubs_end=%p bsp=%p addr=%p\n", (
void *) ubs_end, (
void *) bsp, (
void *) addr);
432 rnat_addr = ia64_rse_rnat_addr(addr);
433 nat_mask = 1
UL << ia64_rse_slot_num(addr);
435 DPRINT(
"rnat @%p = 0x%lx\n", (
void *) rnat_addr, rnats);
437 ia64_peek(
current, sw, (
unsigned long) ubs_end, (
unsigned long) rnat_addr, &rnats);
438 *nat = (rnats & nat_mask) != 0;
451 setreg (
unsigned long regnum,
unsigned long val,
int nat,
struct pt_regs *regs)
475 addr = (
unsigned long)sw;
478 addr = (
unsigned long)regs;
481 DPRINT(
"tmp_base=%lx switch_stack=%s offset=%d\n",
489 *(
unsigned long *)addr = val;
495 bitmask = 1UL << (addr >> 3 & 0x3f);
496 DPRINT(
"*0x%lx=0x%lx NaT=%d prev_unat @%p=%lx\n", addr, val, nat, (
void *) unat, *unat);
502 DPRINT(
"*0x%lx=0x%lx NaT=%d new unat: %p=%lx\n", addr, val, nat, (
void *) unat,*unat);
509 static inline unsigned long
510 fph_index (
struct pt_regs *regs,
long regnum)
512 unsigned long rrb_fr = (regs->
cr_ifs >> 25) & 0x7f;
535 current->thread.fph[fph_index(regs, regnum)] = *fpval;
541 addr = (
unsigned long)sw;
543 addr = (
unsigned long)regs;
594 *fpval =
current->thread.fph[fph_index(regs, regnum)];
602 float_spill_f0(fpval);
605 float_spill_f1(fpval);
612 : (
unsigned long)
regs;
614 DPRINT(
"is_sw=%d tmp_base=%lx offset=0x%x\n",
625 getreg (
unsigned long regnum,
unsigned long *val,
int *nat,
struct pt_regs *regs)
628 unsigned long addr, *unat;
631 get_rse_reg(regs, regnum, val, nat);
649 addr = (
unsigned long)sw;
652 addr = (
unsigned long)regs;
656 DPRINT(
"addr_base=%lx offset=0x%x\n", addr,
GR_OFFS(regnum));
660 *val = *(
unsigned long *)addr;
666 *nat = (*unat >> (addr >> 3 & 0x3f)) & 0x1
UL;
679 printk(
KERN_ERR "%s: register update on speculative load, error\n", __func__);
680 if (
die_if_kernel(
"unaligned reference on speculative load with register update\n",
699 imm = ld.
x << 7 | ld.
imm;
714 DPRINT(
"ld.x=%d ld.m=%d imm=%ld r3=0x%lx\n", ld.
x, ld.
m, imm, ifa);
736 getreg(ld.
imm, &r2, &nat_r2, regs);
745 DPRINT(
"imm=%d r2=%ld r3=0x%lx nat_r2=%d\n",ld.
imm, r2, ifa, nat_r2);
753 unsigned int len = 1 << ld.
x6_sz;
754 unsigned long val = 0;
769 if (len != 2 && len != 4 && len != 8) {
781 if (ld.
op == 0x5 || ld.
m)
875 unsigned int len = 1 << ld.
x6_sz;
883 getreg(ld.
imm, &r2,
NULL, regs);
893 DPRINT(
"st%d [%lx]=%lx\n", len, ifa, r2);
895 if (len != 2 && len != 4 && len != 8) {
917 imm = ld.
x << 7 | ld.
r1;
927 DPRINT(
"imm=%lx r3=%lx\n", imm, ifa);
949 static const unsigned char float_fsz[4]={
1025 unsigned long len = float_fsz[ld.
x6_sz];
1038 memset(&fpr_init, 0,
sizeof(fpr_init));
1039 memset(&fpr_final, 0,
sizeof(fpr_final));
1045 if (ld.
x6_op != 0x2) {
1051 ||
copy_from_user(&fpr_init[1], (
void __user *) (ifa + len), len))
1055 DDUMP(
"frp_init =", &fpr_init, 2*len);
1060 switch( ld.
x6_sz ) {
1062 mem2float_extended(&fpr_init[0], &fpr_final[0]);
1063 mem2float_extended(&fpr_init[1], &fpr_final[1]);
1066 mem2float_integer(&fpr_init[0], &fpr_final[0]);
1067 mem2float_integer(&fpr_init[1], &fpr_final[1]);
1070 mem2float_single(&fpr_init[0], &fpr_final[0]);
1071 mem2float_single(&fpr_init[1], &fpr_final[1]);
1074 mem2float_double(&fpr_init[0], &fpr_final[0]);
1075 mem2float_double(&fpr_init[1], &fpr_final[1]);
1078 DDUMP(
"fpr_final =", &fpr_final, 2*len);
1108 printk(
KERN_ERR "%s: register update on speculative load pair, error\n",
1117 if (ld.
x6_op == 0x2) {
1130 unsigned long len = float_fsz[ld.
x6_sz];
1143 memset(&fpr_init,0,
sizeof(fpr_init));
1144 memset(&fpr_final,0,
sizeof(fpr_final));
1151 if (ld.
x6_op != 0x2) {
1156 DDUMP(
"fpr_init =", &fpr_init, len);
1160 switch( ld.
x6_sz ) {
1162 mem2float_extended(&fpr_init, &fpr_final);
1165 mem2float_integer(&fpr_init, &fpr_final);
1168 mem2float_single(&fpr_init, &fpr_final);
1171 mem2float_double(&fpr_init, &fpr_final);
1174 DDUMP(
"fpr_final =", &fpr_final, len);
1188 if (ld.
op == 0x7 || ld.
m)
1194 if (ld.
x6_op == 0x2)
1206 unsigned long len = float_fsz[ld.
x6_sz];
1211 memset(&fpr_init,0,
sizeof(fpr_init));
1212 memset(&fpr_final,0,
sizeof(fpr_final));
1226 switch( ld.
x6_sz ) {
1228 float2mem_extended(&fpr_init, &fpr_final);
1231 float2mem_integer(&fpr_init, &fpr_final);
1234 float2mem_single(&fpr_init, &fpr_final);
1237 float2mem_double(&fpr_init, &fpr_final);
1241 DDUMP(
"fpr_init =", &fpr_init, len);
1242 DDUMP(
"fpr_final =", &fpr_final, len);
1244 if (
copy_to_user((
void __user *) ifa, &fpr_final, len))
1260 imm = ld.
x << 7 | ld.
r1;
1271 DPRINT(
"imm=%lx r3=%lx\n", imm, ifa);
1296 unsigned long bundle[2];
1308 if (
die_if_kernel(
"big-endian unaligned accesses are not supported", regs, 0))
1324 if (!no_unaligned_warning &&
1331 len =
sprintf(buf,
"%s(%d): unaligned access to 0x%016lx, "
1332 "ip=0x%016lx\n\r",
current->comm,
1345 if (no_unaligned_warning) {
1347 "unaligned exception which required\n"
1348 "kernel assistance, which degrades "
1349 "the performance of the application.\n"
1350 "Unaligned exception warnings have "
1351 "been disabled by the system "
1353 "echo 0 > /proc/sys/kernel/ignore-"
1354 "unaligned-usertrap to re-enable\n",
1362 if (unaligned_dump_stack)
1368 DPRINT(
"iip=%lx ifa=%lx isr=%lx (ei=%d, sp=%d)\n",
1378 case 0:
u.l = (bundle[0] >> 5);
break;
1379 case 1:
u.l = (bundle[0] >> 46) | (bundle[1] << 18);
break;
1380 case 2:
u.l = (bundle[1] >> 23);
break;
1384 DPRINT(
"opcode=%lx ld.qp=%d ld.r1=%d ld.imm=%d ld.r3=%d ld.x=%d ld.hint=%d "
1385 "ld.x6=0x%x ld.m=%d ld.op=%d\n", opcode,
u.insn.qp,
u.insn.r1,
u.insn.imm,
1386 u.insn.r3,
u.insn.x,
u.insn.hint,
u.insn.x6_sz,
u.insn.m,
u.insn.op);
1442 DPRINT(
"forcing PSR_ED\n");
1464 ret = emulate_load_int(ifa,
u.insn, regs);
1475 ret = emulate_store_int(ifa,
u.insn, regs);
1483 ret = emulate_load_floatpair(ifa,
u.insn, regs);
1485 ret = emulate_load_float(ifa,
u.insn, regs);
1492 ret = emulate_load_float(ifa,
u.insn, regs);
1497 ret = emulate_store_float(ifa,
u.insn, regs);
1514 ipsr->
ri = (ipsr->
ri + 1) & 0x3;
1528 if (
die_if_kernel(
"error during unaligned kernel access\n", regs, ret))
1536 si.si_addr = (
void __user *) ifa;