1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/jiffies.h>
18#include <linux/kernel.h>
19#include <linux/sched/signal.h>
20#include <linux/tty.h>
21#include <linux/extable.h>
22#include <linux/ratelimit.h>
23#include <linux/uaccess.h>
24
25#include <asm/intrinsics.h>
26#include <asm/processor.h>
27#include <asm/rse.h>
28#include <asm/exception.h>
29#include <asm/unaligned.h>
30
31extern int die_if_kernel(char *str, struct pt_regs *regs, long err);
32
33#undef DEBUG_UNALIGNED_TRAP
34
35#ifdef DEBUG_UNALIGNED_TRAP
36# define DPRINT(a...) do { printk("%s %u: ", __func__, __LINE__); printk (a); } while (0)
37# define DDUMP(str,vp,len) dump(str, vp, len)
38
39static void
40dump (const char *str, void *vp, size_t len)
41{
42 unsigned char *cp = vp;
43 int i;
44
45 printk("%s", str);
46 for (i = 0; i < len; ++i)
47 printk (" %02x", *cp++);
48 printk("\n");
49}
50#else
51# define DPRINT(a...)
52# define DDUMP(str,vp,len)
53#endif
54
55#define IA64_FIRST_STACKED_GR 32
56#define IA64_FIRST_ROTATING_FR 32
57#define SIGN_EXT9 0xffffffffffffff00ul
58
59
60
61
62
63
64
65int no_unaligned_warning;
66int unaligned_dump_stack;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82#define IA64_OPCODE_MASK 0x1ef
83#define IA64_OPCODE_SHIFT 32
84
85
86
87
88
89
90
91
92
93#define LD_OP 0x080
94#define LDS_OP 0x081
95#define LDA_OP 0x082
96#define LDSA_OP 0x083
97#define LDBIAS_OP 0x084
98#define LDACQ_OP 0x085
99
100#define LDCCLR_OP 0x088
101#define LDCNC_OP 0x089
102#define LDCCLRACQ_OP 0x08a
103#define ST_OP 0x08c
104#define STREL_OP 0x08d
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122#define LD_IMM_OP 0x0a0
123#define LDS_IMM_OP 0x0a1
124#define LDA_IMM_OP 0x0a2
125#define LDSA_IMM_OP 0x0a3
126#define LDBIAS_IMM_OP 0x0a4
127#define LDACQ_IMM_OP 0x0a5
128
129#define LDCCLR_IMM_OP 0x0a8
130#define LDCNC_IMM_OP 0x0a9
131#define LDCCLRACQ_IMM_OP 0x0aa
132#define ST_IMM_OP 0x0ac
133#define STREL_IMM_OP 0x0ad
134
135
136
137
138
139#define LDF_OP 0x0c0
140#define LDFS_OP 0x0c1
141#define LDFA_OP 0x0c2
142#define LDFSA_OP 0x0c3
143
144#define LDFCCLR_OP 0x0c8
145#define LDFCNC_OP 0x0c9
146
147#define STF_OP 0x0cc
148
149
150
151
152
153
154
155
156
157
158
159#define LDF_IMM_OP 0x0e0
160#define LDFS_IMM_OP 0x0e1
161#define LDFA_IMM_OP 0x0e2
162#define LDFSA_IMM_OP 0x0e3
163
164#define LDFCCLR_IMM_OP 0x0e8
165#define LDFCNC_IMM_OP 0x0e9
166#define STF_IMM_OP 0x0ec
167
168typedef struct {
169 unsigned long qp:6;
170 unsigned long r1:7;
171 unsigned long imm:7;
172 unsigned long r3:7;
173 unsigned long x:1;
174 unsigned long hint:2;
175 unsigned long x6_sz:2;
176 unsigned long x6_op:4;
177 unsigned long m:1;
178 unsigned long op:4;
179 unsigned long pad:23;
180} load_store_t;
181
182
183typedef enum {
184 UPD_IMMEDIATE,
185 UPD_REG
186} update_t;
187
188
189
190
191
192
193
194
195
196
197
198
199
200#define RPO(x) ((size_t) &((struct pt_regs *)0)->x)
201#define RSO(x) ((size_t) &((struct switch_stack *)0)->x)
202
203#define RPT(x) (RPO(x) << 1)
204#define RSW(x) (1| RSO(x)<<1)
205
206#define GR_OFFS(x) (gr_info[x]>>1)
207#define GR_IN_SW(x) (gr_info[x] & 0x1)
208
209#define FR_OFFS(x) (fr_info[x]>>1)
210#define FR_IN_SW(x) (fr_info[x] & 0x1)
211
212static u16 gr_info[32]={
213 0,
214
215 RPT(r1), RPT(r2), RPT(r3),
216
217 RSW(r4), RSW(r5), RSW(r6), RSW(r7),
218
219 RPT(r8), RPT(r9), RPT(r10), RPT(r11),
220 RPT(r12), RPT(r13), RPT(r14), RPT(r15),
221
222 RPT(r16), RPT(r17), RPT(r18), RPT(r19),
223 RPT(r20), RPT(r21), RPT(r22), RPT(r23),
224 RPT(r24), RPT(r25), RPT(r26), RPT(r27),
225 RPT(r28), RPT(r29), RPT(r30), RPT(r31)
226};
227
228static u16 fr_info[32]={
229 0,
230 0,
231
232 RSW(f2), RSW(f3), RSW(f4), RSW(f5),
233
234 RPT(f6), RPT(f7), RPT(f8), RPT(f9),
235 RPT(f10), RPT(f11),
236
237 RSW(f12), RSW(f13), RSW(f14),
238 RSW(f15), RSW(f16), RSW(f17), RSW(f18), RSW(f19),
239 RSW(f20), RSW(f21), RSW(f22), RSW(f23), RSW(f24),
240 RSW(f25), RSW(f26), RSW(f27), RSW(f28), RSW(f29),
241 RSW(f30), RSW(f31)
242};
243
244
245static void
246invala_gr (int regno)
247{
248# define F(reg) case reg: ia64_invala_gr(reg); break
249
250 switch (regno) {
251 F( 0); F( 1); F( 2); F( 3); F( 4); F( 5); F( 6); F( 7);
252 F( 8); F( 9); F( 10); F( 11); F( 12); F( 13); F( 14); F( 15);
253 F( 16); F( 17); F( 18); F( 19); F( 20); F( 21); F( 22); F( 23);
254 F( 24); F( 25); F( 26); F( 27); F( 28); F( 29); F( 30); F( 31);
255 F( 32); F( 33); F( 34); F( 35); F( 36); F( 37); F( 38); F( 39);
256 F( 40); F( 41); F( 42); F( 43); F( 44); F( 45); F( 46); F( 47);
257 F( 48); F( 49); F( 50); F( 51); F( 52); F( 53); F( 54); F( 55);
258 F( 56); F( 57); F( 58); F( 59); F( 60); F( 61); F( 62); F( 63);
259 F( 64); F( 65); F( 66); F( 67); F( 68); F( 69); F( 70); F( 71);
260 F( 72); F( 73); F( 74); F( 75); F( 76); F( 77); F( 78); F( 79);
261 F( 80); F( 81); F( 82); F( 83); F( 84); F( 85); F( 86); F( 87);
262 F( 88); F( 89); F( 90); F( 91); F( 92); F( 93); F( 94); F( 95);
263 F( 96); F( 97); F( 98); F( 99); F(100); F(101); F(102); F(103);
264 F(104); F(105); F(106); F(107); F(108); F(109); F(110); F(111);
265 F(112); F(113); F(114); F(115); F(116); F(117); F(118); F(119);
266 F(120); F(121); F(122); F(123); F(124); F(125); F(126); F(127);
267 }
268# undef F
269}
270
271
272static void
273invala_fr (int regno)
274{
275# define F(reg) case reg: ia64_invala_fr(reg); break
276
277 switch (regno) {
278 F( 0); F( 1); F( 2); F( 3); F( 4); F( 5); F( 6); F( 7);
279 F( 8); F( 9); F( 10); F( 11); F( 12); F( 13); F( 14); F( 15);
280 F( 16); F( 17); F( 18); F( 19); F( 20); F( 21); F( 22); F( 23);
281 F( 24); F( 25); F( 26); F( 27); F( 28); F( 29); F( 30); F( 31);
282 F( 32); F( 33); F( 34); F( 35); F( 36); F( 37); F( 38); F( 39);
283 F( 40); F( 41); F( 42); F( 43); F( 44); F( 45); F( 46); F( 47);
284 F( 48); F( 49); F( 50); F( 51); F( 52); F( 53); F( 54); F( 55);
285 F( 56); F( 57); F( 58); F( 59); F( 60); F( 61); F( 62); F( 63);
286 F( 64); F( 65); F( 66); F( 67); F( 68); F( 69); F( 70); F( 71);
287 F( 72); F( 73); F( 74); F( 75); F( 76); F( 77); F( 78); F( 79);
288 F( 80); F( 81); F( 82); F( 83); F( 84); F( 85); F( 86); F( 87);
289 F( 88); F( 89); F( 90); F( 91); F( 92); F( 93); F( 94); F( 95);
290 F( 96); F( 97); F( 98); F( 99); F(100); F(101); F(102); F(103);
291 F(104); F(105); F(106); F(107); F(108); F(109); F(110); F(111);
292 F(112); F(113); F(114); F(115); F(116); F(117); F(118); F(119);
293 F(120); F(121); F(122); F(123); F(124); F(125); F(126); F(127);
294 }
295# undef F
296}
297
298static inline unsigned long
299rotate_reg (unsigned long sor, unsigned long rrb, unsigned long reg)
300{
301 reg += rrb;
302 if (reg >= sor)
303 reg -= sor;
304 return reg;
305}
306
307static void
308set_rse_reg (struct pt_regs *regs, unsigned long r1, unsigned long val, int nat)
309{
310 struct switch_stack *sw = (struct switch_stack *) regs - 1;
311 unsigned long *bsp, *bspstore, *addr, *rnat_addr, *ubs_end;
312 unsigned long *kbs = (void *) current + IA64_RBS_OFFSET;
313 unsigned long rnats, nat_mask;
314 unsigned long on_kbs;
315 long sof = (regs->cr_ifs) & 0x7f;
316 long sor = 8 * ((regs->cr_ifs >> 14) & 0xf);
317 long rrb_gr = (regs->cr_ifs >> 18) & 0x7f;
318 long ridx = r1 - 32;
319
320 if (ridx >= sof) {
321
322 DPRINT("ignoring write to r%lu; only %lu registers are allocated!\n", r1, sof);
323 return;
324 }
325
326 if (ridx < sor)
327 ridx = rotate_reg(sor, rrb_gr, ridx);
328
329 DPRINT("r%lu, sw.bspstore=%lx pt.bspstore=%lx sof=%ld sol=%ld ridx=%ld\n",
330 r1, sw->ar_bspstore, regs->ar_bspstore, sof, (regs->cr_ifs >> 7) & 0x7f, ridx);
331
332 on_kbs = ia64_rse_num_regs(kbs, (unsigned long *) sw->ar_bspstore);
333 addr = ia64_rse_skip_regs((unsigned long *) sw->ar_bspstore, -sof + ridx);
334 if (addr >= kbs) {
335
336 rnat_addr = ia64_rse_rnat_addr(addr);
337 if ((unsigned long) rnat_addr >= sw->ar_bspstore)
338 rnat_addr = &sw->ar_rnat;
339 nat_mask = 1UL << ia64_rse_slot_num(addr);
340
341 *addr = val;
342 if (nat)
343 *rnat_addr |= nat_mask;
344 else
345 *rnat_addr &= ~nat_mask;
346 return;
347 }
348
349 if (!user_stack(current, regs)) {
350 DPRINT("ignoring kernel write to r%lu; register isn't on the kernel RBS!", r1);
351 return;
352 }
353
354 bspstore = (unsigned long *)regs->ar_bspstore;
355 ubs_end = ia64_rse_skip_regs(bspstore, on_kbs);
356 bsp = ia64_rse_skip_regs(ubs_end, -sof);
357 addr = ia64_rse_skip_regs(bsp, ridx);
358
359 DPRINT("ubs_end=%p bsp=%p addr=%p\n", (void *) ubs_end, (void *) bsp, (void *) addr);
360
361 ia64_poke(current, sw, (unsigned long) ubs_end, (unsigned long) addr, val);
362
363 rnat_addr = ia64_rse_rnat_addr(addr);
364
365 ia64_peek(current, sw, (unsigned long) ubs_end, (unsigned long) rnat_addr, &rnats);
366 DPRINT("rnat @%p = 0x%lx nat=%d old nat=%ld\n",
367 (void *) rnat_addr, rnats, nat, (rnats >> ia64_rse_slot_num(addr)) & 1);
368
369 nat_mask = 1UL << ia64_rse_slot_num(addr);
370 if (nat)
371 rnats |= nat_mask;
372 else
373 rnats &= ~nat_mask;
374 ia64_poke(current, sw, (unsigned long) ubs_end, (unsigned long) rnat_addr, rnats);
375
376 DPRINT("rnat changed to @%p = 0x%lx\n", (void *) rnat_addr, rnats);
377}
378
379
380static void
381get_rse_reg (struct pt_regs *regs, unsigned long r1, unsigned long *val, int *nat)
382{
383 struct switch_stack *sw = (struct switch_stack *) regs - 1;
384 unsigned long *bsp, *addr, *rnat_addr, *ubs_end, *bspstore;
385 unsigned long *kbs = (void *) current + IA64_RBS_OFFSET;
386 unsigned long rnats, nat_mask;
387 unsigned long on_kbs;
388 long sof = (regs->cr_ifs) & 0x7f;
389 long sor = 8 * ((regs->cr_ifs >> 14) & 0xf);
390 long rrb_gr = (regs->cr_ifs >> 18) & 0x7f;
391 long ridx = r1 - 32;
392
393 if (ridx >= sof) {
394
395 DPRINT("ignoring read from r%lu; only %lu registers are allocated!\n", r1, sof);
396 goto fail;
397 }
398
399 if (ridx < sor)
400 ridx = rotate_reg(sor, rrb_gr, ridx);
401
402 DPRINT("r%lu, sw.bspstore=%lx pt.bspstore=%lx sof=%ld sol=%ld ridx=%ld\n",
403 r1, sw->ar_bspstore, regs->ar_bspstore, sof, (regs->cr_ifs >> 7) & 0x7f, ridx);
404
405 on_kbs = ia64_rse_num_regs(kbs, (unsigned long *) sw->ar_bspstore);
406 addr = ia64_rse_skip_regs((unsigned long *) sw->ar_bspstore, -sof + ridx);
407 if (addr >= kbs) {
408
409 *val = *addr;
410 if (nat) {
411 rnat_addr = ia64_rse_rnat_addr(addr);
412 if ((unsigned long) rnat_addr >= sw->ar_bspstore)
413 rnat_addr = &sw->ar_rnat;
414 nat_mask = 1UL << ia64_rse_slot_num(addr);
415 *nat = (*rnat_addr & nat_mask) != 0;
416 }
417 return;
418 }
419
420 if (!user_stack(current, regs)) {
421 DPRINT("ignoring kernel read of r%lu; register isn't on the RBS!", r1);
422 goto fail;
423 }
424
425 bspstore = (unsigned long *)regs->ar_bspstore;
426 ubs_end = ia64_rse_skip_regs(bspstore, on_kbs);
427 bsp = ia64_rse_skip_regs(ubs_end, -sof);
428 addr = ia64_rse_skip_regs(bsp, ridx);
429
430 DPRINT("ubs_end=%p bsp=%p addr=%p\n", (void *) ubs_end, (void *) bsp, (void *) addr);
431
432 ia64_peek(current, sw, (unsigned long) ubs_end, (unsigned long) addr, val);
433
434 if (nat) {
435 rnat_addr = ia64_rse_rnat_addr(addr);
436 nat_mask = 1UL << ia64_rse_slot_num(addr);
437
438 DPRINT("rnat @%p = 0x%lx\n", (void *) rnat_addr, rnats);
439
440 ia64_peek(current, sw, (unsigned long) ubs_end, (unsigned long) rnat_addr, &rnats);
441 *nat = (rnats & nat_mask) != 0;
442 }
443 return;
444
445 fail:
446 *val = 0;
447 if (nat)
448 *nat = 0;
449 return;
450}
451
452
453static void
454setreg (unsigned long regnum, unsigned long val, int nat, struct pt_regs *regs)
455{
456 struct switch_stack *sw = (struct switch_stack *) regs - 1;
457 unsigned long addr;
458 unsigned long bitmask;
459 unsigned long *unat;
460
461
462
463
464 if (regnum >= IA64_FIRST_STACKED_GR) {
465 set_rse_reg(regs, regnum, val, nat);
466 return;
467 }
468
469
470
471
472
473
474
475
476
477 if (GR_IN_SW(regnum)) {
478 addr = (unsigned long)sw;
479 unat = &sw->ar_unat;
480 } else {
481 addr = (unsigned long)regs;
482 unat = &sw->caller_unat;
483 }
484 DPRINT("tmp_base=%lx switch_stack=%s offset=%d\n",
485 addr, unat==&sw->ar_unat ? "yes":"no", GR_OFFS(regnum));
486
487
488
489
490 addr += GR_OFFS(regnum);
491
492 *(unsigned long *)addr = val;
493
494
495
496
497
498 bitmask = 1UL << (addr >> 3 & 0x3f);
499 DPRINT("*0x%lx=0x%lx NaT=%d prev_unat @%p=%lx\n", addr, val, nat, (void *) unat, *unat);
500 if (nat) {
501 *unat |= bitmask;
502 } else {
503 *unat &= ~bitmask;
504 }
505 DPRINT("*0x%lx=0x%lx NaT=%d new unat: %p=%lx\n", addr, val, nat, (void *) unat,*unat);
506}
507
508
509
510
511
512static inline unsigned long
513fph_index (struct pt_regs *regs, long regnum)
514{
515 unsigned long rrb_fr = (regs->cr_ifs >> 25) & 0x7f;
516 return rotate_reg(96, rrb_fr, (regnum - IA64_FIRST_ROTATING_FR));
517}
518
519static void
520setfpreg (unsigned long regnum, struct ia64_fpreg *fpval, struct pt_regs *regs)
521{
522 struct switch_stack *sw = (struct switch_stack *)regs - 1;
523 unsigned long addr;
524
525
526
527
528
529
530
531
532
533
534
535
536 if (regnum >= IA64_FIRST_ROTATING_FR) {
537 ia64_sync_fph(current);
538 current->thread.fph[fph_index(regs, regnum)] = *fpval;
539 } else {
540
541
542
543 if (FR_IN_SW(regnum)) {
544 addr = (unsigned long)sw;
545 } else {
546 addr = (unsigned long)regs;
547 }
548
549 DPRINT("tmp_base=%lx offset=%d\n", addr, FR_OFFS(regnum));
550
551 addr += FR_OFFS(regnum);
552 *(struct ia64_fpreg *)addr = *fpval;
553
554
555
556
557
558
559
560 regs->cr_ipsr |= IA64_PSR_MFL;
561 }
562}
563
564
565
566
567
568static inline void
569float_spill_f0 (struct ia64_fpreg *final)
570{
571 ia64_stf_spill(final, 0);
572}
573
574static inline void
575float_spill_f1 (struct ia64_fpreg *final)
576{
577 ia64_stf_spill(final, 1);
578}
579
580static void
581getfpreg (unsigned long regnum, struct ia64_fpreg *fpval, struct pt_regs *regs)
582{
583 struct switch_stack *sw = (struct switch_stack *) regs - 1;
584 unsigned long addr;
585
586
587
588
589
590
591
592
593
594
595 if (regnum >= IA64_FIRST_ROTATING_FR) {
596 ia64_flush_fph(current);
597 *fpval = current->thread.fph[fph_index(regs, regnum)];
598 } else {
599
600
601
602
603 switch(regnum) {
604 case 0:
605 float_spill_f0(fpval);
606 break;
607 case 1:
608 float_spill_f1(fpval);
609 break;
610 default:
611
612
613
614 addr = FR_IN_SW(regnum) ? (unsigned long)sw
615 : (unsigned long)regs;
616
617 DPRINT("is_sw=%d tmp_base=%lx offset=0x%x\n",
618 FR_IN_SW(regnum), addr, FR_OFFS(regnum));
619
620 addr += FR_OFFS(regnum);
621 *fpval = *(struct ia64_fpreg *)addr;
622 }
623 }
624}
625
626
627static void
628getreg (unsigned long regnum, unsigned long *val, int *nat, struct pt_regs *regs)
629{
630 struct switch_stack *sw = (struct switch_stack *) regs - 1;
631 unsigned long addr, *unat;
632
633 if (regnum >= IA64_FIRST_STACKED_GR) {
634 get_rse_reg(regs, regnum, val, nat);
635 return;
636 }
637
638
639
640
641 if (regnum == 0) {
642 *val = 0;
643 if (nat)
644 *nat = 0;
645 return;
646 }
647
648
649
650
651 if (GR_IN_SW(regnum)) {
652 addr = (unsigned long)sw;
653 unat = &sw->ar_unat;
654 } else {
655 addr = (unsigned long)regs;
656 unat = &sw->caller_unat;
657 }
658
659 DPRINT("addr_base=%lx offset=0x%x\n", addr, GR_OFFS(regnum));
660
661 addr += GR_OFFS(regnum);
662
663 *val = *(unsigned long *)addr;
664
665
666
667
668 if (nat)
669 *nat = (*unat >> (addr >> 3 & 0x3f)) & 0x1UL;
670}
671
672static void
673emulate_load_updates (update_t type, load_store_t ld, struct pt_regs *regs, unsigned long ifa)
674{
675
676
677
678
679
680
681 if (ld.x6_op == 1 || ld.x6_op == 3) {
682 printk(KERN_ERR "%s: register update on speculative load, error\n", __func__);
683 if (die_if_kernel("unaligned reference on speculative load with register update\n",
684 regs, 30))
685 return;
686 }
687
688
689
690
691
692
693 if (type == UPD_IMMEDIATE) {
694 unsigned long imm;
695
696
697
698
699
700
701
702 imm = ld.x << 7 | ld.imm;
703
704
705
706
707 if (ld.m) imm |= SIGN_EXT9;
708
709
710
711
712
713 ifa += imm;
714
715 setreg(ld.r3, ifa, 0, regs);
716
717 DPRINT("ld.x=%d ld.m=%d imm=%ld r3=0x%lx\n", ld.x, ld.m, imm, ifa);
718
719 } else if (ld.m) {
720 unsigned long r2;
721 int nat_r2;
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739 getreg(ld.imm, &r2, &nat_r2, regs);
740
741 ifa += r2;
742
743
744
745
746 setreg(ld.r3, ifa, nat_r2, regs);
747
748 DPRINT("imm=%d r2=%ld r3=0x%lx nat_r2=%d\n",ld.imm, r2, ifa, nat_r2);
749 }
750}
751
752
753static int
754emulate_load_int (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
755{
756 unsigned int len = 1 << ld.x6_sz;
757 unsigned long val = 0;
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772 if (len != 2 && len != 4 && len != 8) {
773 DPRINT("unknown size: x6=%d\n", ld.x6_sz);
774 return -1;
775 }
776
777 if (copy_from_user(&val, (void __user *) ifa, len))
778 return -1;
779 setreg(ld.r1, val, 0, regs);
780
781
782
783
784 if (ld.op == 0x5 || ld.m)
785 emulate_load_updates(ld.op == 0x5 ? UPD_IMMEDIATE: UPD_REG, ld, regs, ifa);
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862 if (ld.x6_op == 0x5 || ld.x6_op == 0xa)
863 mb();
864
865
866
867
868 if (ld.x6_op == 0x2)
869 invala_gr(ld.r1);
870
871 return 0;
872}
873
874static int
875emulate_store_int (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
876{
877 unsigned long r2;
878 unsigned int len = 1 << ld.x6_sz;
879
880
881
882
883
884
885
886 getreg(ld.imm, &r2, NULL, regs);
887
888
889
890
891
892
893
894
895
896 DPRINT("st%d [%lx]=%lx\n", len, ifa, r2);
897
898 if (len != 2 && len != 4 && len != 8) {
899 DPRINT("unknown size: x6=%d\n", ld.x6_sz);
900 return -1;
901 }
902
903
904 if (copy_to_user((void __user *) ifa, &r2, len))
905 return -1;
906
907
908
909
910
911
912
913
914 if (ld.op == 0x5) {
915 unsigned long imm;
916
917
918
919
920 imm = ld.x << 7 | ld.r1;
921
922
923
924 if (ld.m) imm |= SIGN_EXT9;
925
926
927
928 ifa += imm;
929
930 DPRINT("imm=%lx r3=%lx\n", imm, ifa);
931
932 setreg(ld.r3, ifa, 0, regs);
933 }
934
935
936
937
938 ia64_invala();
939
940
941
942
943 if (ld.x6_op == 0xd)
944 mb();
945
946 return 0;
947}
948
949
950
951
952static const unsigned char float_fsz[4]={
953 10,
954 8,
955 4,
956 8
957};
958
959static inline void
960mem2float_extended (struct ia64_fpreg *init, struct ia64_fpreg *final)
961{
962 ia64_ldfe(6, init);
963 ia64_stop();
964 ia64_stf_spill(final, 6);
965}
966
967static inline void
968mem2float_integer (struct ia64_fpreg *init, struct ia64_fpreg *final)
969{
970 ia64_ldf8(6, init);
971 ia64_stop();
972 ia64_stf_spill(final, 6);
973}
974
975static inline void
976mem2float_single (struct ia64_fpreg *init, struct ia64_fpreg *final)
977{
978 ia64_ldfs(6, init);
979 ia64_stop();
980 ia64_stf_spill(final, 6);
981}
982
983static inline void
984mem2float_double (struct ia64_fpreg *init, struct ia64_fpreg *final)
985{
986 ia64_ldfd(6, init);
987 ia64_stop();
988 ia64_stf_spill(final, 6);
989}
990
991static inline void
992float2mem_extended (struct ia64_fpreg *init, struct ia64_fpreg *final)
993{
994 ia64_ldf_fill(6, init);
995 ia64_stop();
996 ia64_stfe(final, 6);
997}
998
999static inline void
1000float2mem_integer (struct ia64_fpreg *init, struct ia64_fpreg *final)
1001{
1002 ia64_ldf_fill(6, init);
1003 ia64_stop();
1004 ia64_stf8(final, 6);
1005}
1006
1007static inline void
1008float2mem_single (struct ia64_fpreg *init, struct ia64_fpreg *final)
1009{
1010 ia64_ldf_fill(6, init);
1011 ia64_stop();
1012 ia64_stfs(final, 6);
1013}
1014
1015static inline void
1016float2mem_double (struct ia64_fpreg *init, struct ia64_fpreg *final)
1017{
1018 ia64_ldf_fill(6, init);
1019 ia64_stop();
1020 ia64_stfd(final, 6);
1021}
1022
1023static int
1024emulate_load_floatpair (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
1025{
1026 struct ia64_fpreg fpr_init[2];
1027 struct ia64_fpreg fpr_final[2];
1028 unsigned long len = float_fsz[ld.x6_sz];
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041 memset(&fpr_init, 0, sizeof(fpr_init));
1042 memset(&fpr_final, 0, sizeof(fpr_final));
1043
1044
1045
1046
1047
1048 if (ld.x6_op != 0x2) {
1049
1050
1051
1052
1053 if (copy_from_user(&fpr_init[0], (void __user *) ifa, len)
1054 || copy_from_user(&fpr_init[1], (void __user *) (ifa + len), len))
1055 return -1;
1056
1057 DPRINT("ld.r1=%d ld.imm=%d x6_sz=%d\n", ld.r1, ld.imm, ld.x6_sz);
1058 DDUMP("frp_init =", &fpr_init, 2*len);
1059
1060
1061
1062
1063 switch( ld.x6_sz ) {
1064 case 0:
1065 mem2float_extended(&fpr_init[0], &fpr_final[0]);
1066 mem2float_extended(&fpr_init[1], &fpr_final[1]);
1067 break;
1068 case 1:
1069 mem2float_integer(&fpr_init[0], &fpr_final[0]);
1070 mem2float_integer(&fpr_init[1], &fpr_final[1]);
1071 break;
1072 case 2:
1073 mem2float_single(&fpr_init[0], &fpr_final[0]);
1074 mem2float_single(&fpr_init[1], &fpr_final[1]);
1075 break;
1076 case 3:
1077 mem2float_double(&fpr_init[0], &fpr_final[0]);
1078 mem2float_double(&fpr_init[1], &fpr_final[1]);
1079 break;
1080 }
1081 DDUMP("fpr_final =", &fpr_final, 2*len);
1082
1083
1084
1085
1086
1087
1088
1089 setfpreg(ld.r1, &fpr_final[0], regs);
1090 setfpreg(ld.imm, &fpr_final[1], regs);
1091 }
1092
1093
1094
1095
1096
1097 if (ld.m) {
1098
1099
1100
1101
1102 ifa += len<<1;
1103
1104
1105
1106
1107
1108
1109
1110 if (ld.x6_op == 1 || ld.x6_op == 3)
1111 printk(KERN_ERR "%s: register update on speculative load pair, error\n",
1112 __func__);
1113
1114 setreg(ld.r3, ifa, 0, regs);
1115 }
1116
1117
1118
1119
1120 if (ld.x6_op == 0x2) {
1121 invala_fr(ld.r1);
1122 invala_fr(ld.imm);
1123 }
1124 return 0;
1125}
1126
1127
1128static int
1129emulate_load_float (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
1130{
1131 struct ia64_fpreg fpr_init;
1132 struct ia64_fpreg fpr_final;
1133 unsigned long len = float_fsz[ld.x6_sz];
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 memset(&fpr_init,0, sizeof(fpr_init));
1147 memset(&fpr_final,0, sizeof(fpr_final));
1148
1149
1150
1151
1152
1153
1154 if (ld.x6_op != 0x2) {
1155 if (copy_from_user(&fpr_init, (void __user *) ifa, len))
1156 return -1;
1157
1158 DPRINT("ld.r1=%d x6_sz=%d\n", ld.r1, ld.x6_sz);
1159 DDUMP("fpr_init =", &fpr_init, len);
1160
1161
1162
1163 switch( ld.x6_sz ) {
1164 case 0:
1165 mem2float_extended(&fpr_init, &fpr_final);
1166 break;
1167 case 1:
1168 mem2float_integer(&fpr_init, &fpr_final);
1169 break;
1170 case 2:
1171 mem2float_single(&fpr_init, &fpr_final);
1172 break;
1173 case 3:
1174 mem2float_double(&fpr_init, &fpr_final);
1175 break;
1176 }
1177 DDUMP("fpr_final =", &fpr_final, len);
1178
1179
1180
1181
1182
1183
1184
1185 setfpreg(ld.r1, &fpr_final, regs);
1186 }
1187
1188
1189
1190
1191 if (ld.op == 0x7 || ld.m)
1192 emulate_load_updates(ld.op == 0x7 ? UPD_IMMEDIATE: UPD_REG, ld, regs, ifa);
1193
1194
1195
1196
1197 if (ld.x6_op == 0x2)
1198 invala_fr(ld.r1);
1199
1200 return 0;
1201}
1202
1203
1204static int
1205emulate_store_float (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
1206{
1207 struct ia64_fpreg fpr_init;
1208 struct ia64_fpreg fpr_final;
1209 unsigned long len = float_fsz[ld.x6_sz];
1210
1211
1212
1213
1214 memset(&fpr_init,0, sizeof(fpr_init));
1215 memset(&fpr_final,0, sizeof(fpr_final));
1216
1217
1218
1219
1220
1221
1222
1223 getfpreg(ld.imm, &fpr_init, regs);
1224
1225
1226
1227
1228
1229 switch( ld.x6_sz ) {
1230 case 0:
1231 float2mem_extended(&fpr_init, &fpr_final);
1232 break;
1233 case 1:
1234 float2mem_integer(&fpr_init, &fpr_final);
1235 break;
1236 case 2:
1237 float2mem_single(&fpr_init, &fpr_final);
1238 break;
1239 case 3:
1240 float2mem_double(&fpr_init, &fpr_final);
1241 break;
1242 }
1243 DPRINT("ld.r1=%d x6_sz=%d\n", ld.r1, ld.x6_sz);
1244 DDUMP("fpr_init =", &fpr_init, len);
1245 DDUMP("fpr_final =", &fpr_final, len);
1246
1247 if (copy_to_user((void __user *) ifa, &fpr_final, len))
1248 return -1;
1249
1250
1251
1252
1253
1254
1255
1256
1257 if (ld.op == 0x7) {
1258 unsigned long imm;
1259
1260
1261
1262
1263 imm = ld.x << 7 | ld.r1;
1264
1265
1266
1267 if (ld.m)
1268 imm |= SIGN_EXT9;
1269
1270
1271
1272 ifa += imm;
1273
1274 DPRINT("imm=%lx r3=%lx\n", imm, ifa);
1275
1276 setreg(ld.r3, ifa, 0, regs);
1277 }
1278
1279
1280
1281
1282 ia64_invala();
1283
1284 return 0;
1285}
1286
1287
1288
1289
1290
1291
1292static DEFINE_RATELIMIT_STATE(logging_rate_limit, 5 * HZ, 5);
1293
1294void
1295ia64_handle_unaligned (unsigned long ifa, struct pt_regs *regs)
1296{
1297 struct ia64_psr *ipsr = ia64_psr(regs);
1298 mm_segment_t old_fs = get_fs();
1299 unsigned long bundle[2];
1300 unsigned long opcode;
1301 struct siginfo si;
1302 const struct exception_table_entry *eh = NULL;
1303 union {
1304 unsigned long l;
1305 load_store_t insn;
1306 } u;
1307 int ret = -1;
1308
1309 if (ia64_psr(regs)->be) {
1310
1311 if (die_if_kernel("big-endian unaligned accesses are not supported", regs, 0))
1312 return;
1313 goto force_sigbus;
1314 }
1315
1316
1317
1318
1319
1320
1321 if (!user_mode(regs))
1322 eh = search_exception_tables(regs->cr_iip + ia64_psr(regs)->ri);
1323 if (user_mode(regs) || eh) {
1324 if ((current->thread.flags & IA64_THREAD_UAC_SIGBUS) != 0)
1325 goto force_sigbus;
1326
1327 if (!no_unaligned_warning &&
1328 !(current->thread.flags & IA64_THREAD_UAC_NOPRINT) &&
1329 __ratelimit(&logging_rate_limit))
1330 {
1331 char buf[200];
1332 size_t len;
1333
1334 len = sprintf(buf, "%s(%d): unaligned access to 0x%016lx, "
1335 "ip=0x%016lx\n\r", current->comm,
1336 task_pid_nr(current),
1337 ifa, regs->cr_iip + ipsr->ri);
1338
1339
1340
1341
1342 if (user_mode(regs)) {
1343 struct tty_struct *tty = get_current_tty();
1344 tty_write_message(tty, buf);
1345 tty_kref_put(tty);
1346 }
1347 buf[len-1] = '\0';
1348
1349 printk(KERN_WARNING "%s", buf);
1350 } else {
1351 if (no_unaligned_warning) {
1352 printk_once(KERN_WARNING "%s(%d) encountered an "
1353 "unaligned exception which required\n"
1354 "kernel assistance, which degrades "
1355 "the performance of the application.\n"
1356 "Unaligned exception warnings have "
1357 "been disabled by the system "
1358 "administrator\n"
1359 "echo 0 > /proc/sys/kernel/ignore-"
1360 "unaligned-usertrap to re-enable\n",
1361 current->comm, task_pid_nr(current));
1362 }
1363 }
1364 } else {
1365 if (__ratelimit(&logging_rate_limit)) {
1366 printk(KERN_WARNING "kernel unaligned access to 0x%016lx, ip=0x%016lx\n",
1367 ifa, regs->cr_iip + ipsr->ri);
1368 if (unaligned_dump_stack)
1369 dump_stack();
1370 }
1371 set_fs(KERNEL_DS);
1372 }
1373
1374 DPRINT("iip=%lx ifa=%lx isr=%lx (ei=%d, sp=%d)\n",
1375 regs->cr_iip, ifa, regs->cr_ipsr, ipsr->ri, ipsr->it);
1376
1377 if (__copy_from_user(bundle, (void __user *) regs->cr_iip, 16))
1378 goto failure;
1379
1380
1381
1382
1383 switch (ipsr->ri) {
1384 default:
1385 case 0: u.l = (bundle[0] >> 5); break;
1386 case 1: u.l = (bundle[0] >> 46) | (bundle[1] << 18); break;
1387 case 2: u.l = (bundle[1] >> 23); break;
1388 }
1389 opcode = (u.l >> IA64_OPCODE_SHIFT) & IA64_OPCODE_MASK;
1390
1391 DPRINT("opcode=%lx ld.qp=%d ld.r1=%d ld.imm=%d ld.r3=%d ld.x=%d ld.hint=%d "
1392 "ld.x6=0x%x ld.m=%d ld.op=%d\n", opcode, u.insn.qp, u.insn.r1, u.insn.imm,
1393 u.insn.r3, u.insn.x, u.insn.hint, u.insn.x6_sz, u.insn.m, u.insn.op);
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429 switch (opcode) {
1430 case LDS_OP:
1431 case LDSA_OP:
1432 if (u.insn.x)
1433
1434 goto failure;
1435
1436 case LDS_IMM_OP:
1437 case LDSA_IMM_OP:
1438 case LDFS_OP:
1439 case LDFSA_OP:
1440 case LDFS_IMM_OP:
1441
1442
1443
1444
1445
1446
1447
1448
1449 DPRINT("forcing PSR_ED\n");
1450 regs->cr_ipsr |= IA64_PSR_ED;
1451 goto done;
1452
1453 case LD_OP:
1454 case LDA_OP:
1455 case LDBIAS_OP:
1456 case LDACQ_OP:
1457 case LDCCLR_OP:
1458 case LDCNC_OP:
1459 case LDCCLRACQ_OP:
1460 if (u.insn.x)
1461
1462 goto failure;
1463
1464 case LD_IMM_OP:
1465 case LDA_IMM_OP:
1466 case LDBIAS_IMM_OP:
1467 case LDACQ_IMM_OP:
1468 case LDCCLR_IMM_OP:
1469 case LDCNC_IMM_OP:
1470 case LDCCLRACQ_IMM_OP:
1471 ret = emulate_load_int(ifa, u.insn, regs);
1472 break;
1473
1474 case ST_OP:
1475 case STREL_OP:
1476 if (u.insn.x)
1477
1478 goto failure;
1479
1480 case ST_IMM_OP:
1481 case STREL_IMM_OP:
1482 ret = emulate_store_int(ifa, u.insn, regs);
1483 break;
1484
1485 case LDF_OP:
1486 case LDFA_OP:
1487 case LDFCCLR_OP:
1488 case LDFCNC_OP:
1489 if (u.insn.x)
1490 ret = emulate_load_floatpair(ifa, u.insn, regs);
1491 else
1492 ret = emulate_load_float(ifa, u.insn, regs);
1493 break;
1494
1495 case LDF_IMM_OP:
1496 case LDFA_IMM_OP:
1497 case LDFCCLR_IMM_OP:
1498 case LDFCNC_IMM_OP:
1499 ret = emulate_load_float(ifa, u.insn, regs);
1500 break;
1501
1502 case STF_OP:
1503 case STF_IMM_OP:
1504 ret = emulate_store_float(ifa, u.insn, regs);
1505 break;
1506
1507 default:
1508 goto failure;
1509 }
1510 DPRINT("ret=%d\n", ret);
1511 if (ret)
1512 goto failure;
1513
1514 if (ipsr->ri == 2)
1515
1516
1517
1518
1519
1520 regs->cr_iip += 16;
1521 ipsr->ri = (ipsr->ri + 1) & 0x3;
1522
1523 DPRINT("ipsr->ri=%d iip=%lx\n", ipsr->ri, regs->cr_iip);
1524 done:
1525 set_fs(old_fs);
1526 return;
1527
1528 failure:
1529
1530 if (!user_mode(regs)) {
1531 if (eh) {
1532 ia64_handle_exception(regs, eh);
1533 goto done;
1534 }
1535 if (die_if_kernel("error during unaligned kernel access\n", regs, ret))
1536 return;
1537
1538 }
1539 force_sigbus:
1540 clear_siginfo(&si);
1541 si.si_signo = SIGBUS;
1542 si.si_errno = 0;
1543 si.si_code = BUS_ADRALN;
1544 si.si_addr = (void __user *) ifa;
1545 si.si_flags = 0;
1546 si.si_isr = 0;
1547 si.si_imm = 0;
1548 force_sig_info(SIGBUS, &si, current);
1549 goto done;
1550}
1551