unaligned.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. /*
  2. * Architecture-specific unaligned trap handling.
  3. *
  4. * Copyright (C) 1999-2002, 2004 Hewlett-Packard Co
  5. * Stephane Eranian <eranian@hpl.hp.com>
  6. * David Mosberger-Tang <davidm@hpl.hp.com>
  7. *
  8. * 2002/12/09 Fix rotating register handling (off-by-1 error, missing fr-rotation). Fix
  9. * get_rse_reg() to not leak kernel bits to user-level (reading an out-of-frame
  10. * stacked register returns an undefined value; it does NOT trigger a
  11. * "rsvd register fault").
  12. * 2001/10/11 Fix unaligned access to rotating registers in s/w pipelined loops.
  13. * 2001/08/13 Correct size of extended floats (float_fsz) from 16 to 10 bytes.
  14. * 2001/01/17 Add support emulation of unaligned kernel accesses.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/tty.h>
  19. #include <asm/intrinsics.h>
  20. #include <asm/processor.h>
  21. #include <asm/rse.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/unaligned.h>
  24. extern void die_if_kernel(char *str, struct pt_regs *regs, long err);
  25. #undef DEBUG_UNALIGNED_TRAP
  26. #ifdef DEBUG_UNALIGNED_TRAP
  27. # define DPRINT(a...) do { printk("%s %u: ", __FUNCTION__, __LINE__); printk (a); } while (0)
  28. # define DDUMP(str,vp,len) dump(str, vp, len)
  29. static void
  30. dump (const char *str, void *vp, size_t len)
  31. {
  32. unsigned char *cp = vp;
  33. int i;
  34. printk("%s", str);
  35. for (i = 0; i < len; ++i)
  36. printk (" %02x", *cp++);
  37. printk("\n");
  38. }
  39. #else
  40. # define DPRINT(a...)
  41. # define DDUMP(str,vp,len)
  42. #endif
  43. #define IA64_FIRST_STACKED_GR 32
  44. #define IA64_FIRST_ROTATING_FR 32
  45. #define SIGN_EXT9 0xffffffffffffff00ul
  46. /*
  47. * sysctl settable hook which tells the kernel whether to honor the
  48. * IA64_THREAD_UAC_NOPRINT prctl. Because this is user settable, we want
  49. * to allow the super user to enable/disable this for security reasons
  50. * (i.e. don't allow attacker to fill up logs with unaligned accesses).
  51. */
  52. int no_unaligned_warning;
  53. static int noprint_warning;
  54. /*
  55. * For M-unit:
  56. *
  57. * opcode | m | x6 |
  58. * --------|------|---------|
  59. * [40-37] | [36] | [35:30] |
  60. * --------|------|---------|
  61. * 4 | 1 | 6 | = 11 bits
  62. * --------------------------
  63. * However bits [31:30] are not directly useful to distinguish between
  64. * load/store so we can use [35:32] instead, which gives the following
  65. * mask ([40:32]) using 9 bits. The 'e' comes from the fact that we defer
  66. * checking the m-bit until later in the load/store emulation.
  67. */
  68. #define IA64_OPCODE_MASK 0x1ef
  69. #define IA64_OPCODE_SHIFT 32
  70. /*
  71. * Table C-28 Integer Load/Store
  72. *
  73. * We ignore [35:32]= 0x6, 0x7, 0xE, 0xF
  74. *
  75. * ld8.fill, st8.fill MUST be aligned because the RNATs are based on
  76. * the address (bits [8:3]), so we must failed.
  77. */
  78. #define LD_OP 0x080
  79. #define LDS_OP 0x081
  80. #define LDA_OP 0x082
  81. #define LDSA_OP 0x083
  82. #define LDBIAS_OP 0x084
  83. #define LDACQ_OP 0x085
  84. /* 0x086, 0x087 are not relevant */
  85. #define LDCCLR_OP 0x088
  86. #define LDCNC_OP 0x089
  87. #define LDCCLRACQ_OP 0x08a
  88. #define ST_OP 0x08c
  89. #define STREL_OP 0x08d
  90. /* 0x08e,0x8f are not relevant */
  91. /*
  92. * Table C-29 Integer Load +Reg
  93. *
  94. * we use the ld->m (bit [36:36]) field to determine whether or not we have
  95. * a load/store of this form.
  96. */
  97. /*
  98. * Table C-30 Integer Load/Store +Imm
  99. *
  100. * We ignore [35:32]= 0x6, 0x7, 0xE, 0xF
  101. *
  102. * ld8.fill, st8.fill must be aligned because the Nat register are based on
  103. * the address, so we must fail and the program must be fixed.
  104. */
  105. #define LD_IMM_OP 0x0a0
  106. #define LDS_IMM_OP 0x0a1
  107. #define LDA_IMM_OP 0x0a2
  108. #define LDSA_IMM_OP 0x0a3
  109. #define LDBIAS_IMM_OP 0x0a4
  110. #define LDACQ_IMM_OP 0x0a5
  111. /* 0x0a6, 0xa7 are not relevant */
  112. #define LDCCLR_IMM_OP 0x0a8
  113. #define LDCNC_IMM_OP 0x0a9
  114. #define LDCCLRACQ_IMM_OP 0x0aa
  115. #define ST_IMM_OP 0x0ac
  116. #define STREL_IMM_OP 0x0ad
  117. /* 0x0ae,0xaf are not relevant */
  118. /*
  119. * Table C-32 Floating-point Load/Store
  120. */
  121. #define LDF_OP 0x0c0
  122. #define LDFS_OP 0x0c1
  123. #define LDFA_OP 0x0c2
  124. #define LDFSA_OP 0x0c3
  125. /* 0x0c6 is irrelevant */
  126. #define LDFCCLR_OP 0x0c8
  127. #define LDFCNC_OP 0x0c9
  128. /* 0x0cb is irrelevant */
  129. #define STF_OP 0x0cc
  130. /*
  131. * Table C-33 Floating-point Load +Reg
  132. *
  133. * we use the ld->m (bit [36:36]) field to determine whether or not we have
  134. * a load/store of this form.
  135. */
  136. /*
  137. * Table C-34 Floating-point Load/Store +Imm
  138. */
  139. #define LDF_IMM_OP 0x0e0
  140. #define LDFS_IMM_OP 0x0e1
  141. #define LDFA_IMM_OP 0x0e2
  142. #define LDFSA_IMM_OP 0x0e3
  143. /* 0x0e6 is irrelevant */
  144. #define LDFCCLR_IMM_OP 0x0e8
  145. #define LDFCNC_IMM_OP 0x0e9
  146. #define STF_IMM_OP 0x0ec
  147. typedef struct {
  148. unsigned long qp:6; /* [0:5] */
  149. unsigned long r1:7; /* [6:12] */
  150. unsigned long imm:7; /* [13:19] */
  151. unsigned long r3:7; /* [20:26] */
  152. unsigned long x:1; /* [27:27] */
  153. unsigned long hint:2; /* [28:29] */
  154. unsigned long x6_sz:2; /* [30:31] */
  155. unsigned long x6_op:4; /* [32:35], x6 = x6_sz|x6_op */
  156. unsigned long m:1; /* [36:36] */
  157. unsigned long op:4; /* [37:40] */
  158. unsigned long pad:23; /* [41:63] */
  159. } load_store_t;
  160. typedef enum {
  161. UPD_IMMEDIATE, /* ldXZ r1=[r3],imm(9) */
  162. UPD_REG /* ldXZ r1=[r3],r2 */
  163. } update_t;
  164. /*
  165. * We use tables to keep track of the offsets of registers in the saved state.
  166. * This way we save having big switch/case statements.
  167. *
  168. * We use bit 0 to indicate switch_stack or pt_regs.
  169. * The offset is simply shifted by 1 bit.
  170. * A 2-byte value should be enough to hold any kind of offset
  171. *
  172. * In case the calling convention changes (and thus pt_regs/switch_stack)
  173. * simply use RSW instead of RPT or vice-versa.
  174. */
  175. #define RPO(x) ((size_t) &((struct pt_regs *)0)->x)
  176. #define RSO(x) ((size_t) &((struct switch_stack *)0)->x)
  177. #define RPT(x) (RPO(x) << 1)
  178. #define RSW(x) (1| RSO(x)<<1)
  179. #define GR_OFFS(x) (gr_info[x]>>1)
  180. #define GR_IN_SW(x) (gr_info[x] & 0x1)
  181. #define FR_OFFS(x) (fr_info[x]>>1)
  182. #define FR_IN_SW(x) (fr_info[x] & 0x1)
  183. static u16 gr_info[32]={
  184. 0, /* r0 is read-only : WE SHOULD NEVER GET THIS */
  185. RPT(r1), RPT(r2), RPT(r3),
  186. RSW(r4), RSW(r5), RSW(r6), RSW(r7),
  187. RPT(r8), RPT(r9), RPT(r10), RPT(r11),
  188. RPT(r12), RPT(r13), RPT(r14), RPT(r15),
  189. RPT(r16), RPT(r17), RPT(r18), RPT(r19),
  190. RPT(r20), RPT(r21), RPT(r22), RPT(r23),
  191. RPT(r24), RPT(r25), RPT(r26), RPT(r27),
  192. RPT(r28), RPT(r29), RPT(r30), RPT(r31)
  193. };
  194. static u16 fr_info[32]={
  195. 0, /* constant : WE SHOULD NEVER GET THIS */
  196. 0, /* constant : WE SHOULD NEVER GET THIS */
  197. RSW(f2), RSW(f3), RSW(f4), RSW(f5),
  198. RPT(f6), RPT(f7), RPT(f8), RPT(f9),
  199. RPT(f10), RPT(f11),
  200. RSW(f12), RSW(f13), RSW(f14),
  201. RSW(f15), RSW(f16), RSW(f17), RSW(f18), RSW(f19),
  202. RSW(f20), RSW(f21), RSW(f22), RSW(f23), RSW(f24),
  203. RSW(f25), RSW(f26), RSW(f27), RSW(f28), RSW(f29),
  204. RSW(f30), RSW(f31)
  205. };
  206. /* Invalidate ALAT entry for integer register REGNO. */
  207. static void
  208. invala_gr (int regno)
  209. {
  210. # define F(reg) case reg: ia64_invala_gr(reg); break
  211. switch (regno) {
  212. F( 0); F( 1); F( 2); F( 3); F( 4); F( 5); F( 6); F( 7);
  213. F( 8); F( 9); F( 10); F( 11); F( 12); F( 13); F( 14); F( 15);
  214. F( 16); F( 17); F( 18); F( 19); F( 20); F( 21); F( 22); F( 23);
  215. F( 24); F( 25); F( 26); F( 27); F( 28); F( 29); F( 30); F( 31);
  216. F( 32); F( 33); F( 34); F( 35); F( 36); F( 37); F( 38); F( 39);
  217. F( 40); F( 41); F( 42); F( 43); F( 44); F( 45); F( 46); F( 47);
  218. F( 48); F( 49); F( 50); F( 51); F( 52); F( 53); F( 54); F( 55);
  219. F( 56); F( 57); F( 58); F( 59); F( 60); F( 61); F( 62); F( 63);
  220. F( 64); F( 65); F( 66); F( 67); F( 68); F( 69); F( 70); F( 71);
  221. F( 72); F( 73); F( 74); F( 75); F( 76); F( 77); F( 78); F( 79);
  222. F( 80); F( 81); F( 82); F( 83); F( 84); F( 85); F( 86); F( 87);
  223. F( 88); F( 89); F( 90); F( 91); F( 92); F( 93); F( 94); F( 95);
  224. F( 96); F( 97); F( 98); F( 99); F(100); F(101); F(102); F(103);
  225. F(104); F(105); F(106); F(107); F(108); F(109); F(110); F(111);
  226. F(112); F(113); F(114); F(115); F(116); F(117); F(118); F(119);
  227. F(120); F(121); F(122); F(123); F(124); F(125); F(126); F(127);
  228. }
  229. # undef F
  230. }
  231. /* Invalidate ALAT entry for floating-point register REGNO. */
  232. static void
  233. invala_fr (int regno)
  234. {
  235. # define F(reg) case reg: ia64_invala_fr(reg); break
  236. switch (regno) {
  237. F( 0); F( 1); F( 2); F( 3); F( 4); F( 5); F( 6); F( 7);
  238. F( 8); F( 9); F( 10); F( 11); F( 12); F( 13); F( 14); F( 15);
  239. F( 16); F( 17); F( 18); F( 19); F( 20); F( 21); F( 22); F( 23);
  240. F( 24); F( 25); F( 26); F( 27); F( 28); F( 29); F( 30); F( 31);
  241. F( 32); F( 33); F( 34); F( 35); F( 36); F( 37); F( 38); F( 39);
  242. F( 40); F( 41); F( 42); F( 43); F( 44); F( 45); F( 46); F( 47);
  243. F( 48); F( 49); F( 50); F( 51); F( 52); F( 53); F( 54); F( 55);
  244. F( 56); F( 57); F( 58); F( 59); F( 60); F( 61); F( 62); F( 63);
  245. F( 64); F( 65); F( 66); F( 67); F( 68); F( 69); F( 70); F( 71);
  246. F( 72); F( 73); F( 74); F( 75); F( 76); F( 77); F( 78); F( 79);
  247. F( 80); F( 81); F( 82); F( 83); F( 84); F( 85); F( 86); F( 87);
  248. F( 88); F( 89); F( 90); F( 91); F( 92); F( 93); F( 94); F( 95);
  249. F( 96); F( 97); F( 98); F( 99); F(100); F(101); F(102); F(103);
  250. F(104); F(105); F(106); F(107); F(108); F(109); F(110); F(111);
  251. F(112); F(113); F(114); F(115); F(116); F(117); F(118); F(119);
  252. F(120); F(121); F(122); F(123); F(124); F(125); F(126); F(127);
  253. }
  254. # undef F
  255. }
  256. static inline unsigned long
  257. rotate_reg (unsigned long sor, unsigned long rrb, unsigned long reg)
  258. {
  259. reg += rrb;
  260. if (reg >= sor)
  261. reg -= sor;
  262. return reg;
  263. }
  264. static void
  265. set_rse_reg (struct pt_regs *regs, unsigned long r1, unsigned long val, int nat)
  266. {
  267. struct switch_stack *sw = (struct switch_stack *) regs - 1;
  268. unsigned long *bsp, *bspstore, *addr, *rnat_addr, *ubs_end;
  269. unsigned long *kbs = (void *) current + IA64_RBS_OFFSET;
  270. unsigned long rnats, nat_mask;
  271. unsigned long on_kbs;
  272. long sof = (regs->cr_ifs) & 0x7f;
  273. long sor = 8 * ((regs->cr_ifs >> 14) & 0xf);
  274. long rrb_gr = (regs->cr_ifs >> 18) & 0x7f;
  275. long ridx = r1 - 32;
  276. if (ridx >= sof) {
  277. /* this should never happen, as the "rsvd register fault" has higher priority */
  278. DPRINT("ignoring write to r%lu; only %lu registers are allocated!\n", r1, sof);
  279. return;
  280. }
  281. if (ridx < sor)
  282. ridx = rotate_reg(sor, rrb_gr, ridx);
  283. DPRINT("r%lu, sw.bspstore=%lx pt.bspstore=%lx sof=%ld sol=%ld ridx=%ld\n",
  284. r1, sw->ar_bspstore, regs->ar_bspstore, sof, (regs->cr_ifs >> 7) & 0x7f, ridx);
  285. on_kbs = ia64_rse_num_regs(kbs, (unsigned long *) sw->ar_bspstore);
  286. addr = ia64_rse_skip_regs((unsigned long *) sw->ar_bspstore, -sof + ridx);
  287. if (addr >= kbs) {
  288. /* the register is on the kernel backing store: easy... */
  289. rnat_addr = ia64_rse_rnat_addr(addr);
  290. if ((unsigned long) rnat_addr >= sw->ar_bspstore)
  291. rnat_addr = &sw->ar_rnat;
  292. nat_mask = 1UL << ia64_rse_slot_num(addr);
  293. *addr = val;
  294. if (nat)
  295. *rnat_addr |= nat_mask;
  296. else
  297. *rnat_addr &= ~nat_mask;
  298. return;
  299. }
  300. if (!user_stack(current, regs)) {
  301. DPRINT("ignoring kernel write to r%lu; register isn't on the kernel RBS!", r1);
  302. return;
  303. }
  304. bspstore = (unsigned long *)regs->ar_bspstore;
  305. ubs_end = ia64_rse_skip_regs(bspstore, on_kbs);
  306. bsp = ia64_rse_skip_regs(ubs_end, -sof);
  307. addr = ia64_rse_skip_regs(bsp, ridx);
  308. DPRINT("ubs_end=%p bsp=%p addr=%p\n", (void *) ubs_end, (void *) bsp, (void *) addr);
  309. ia64_poke(current, sw, (unsigned long) ubs_end, (unsigned long) addr, val);
  310. rnat_addr = ia64_rse_rnat_addr(addr);
  311. ia64_peek(current, sw, (unsigned long) ubs_end, (unsigned long) rnat_addr, &rnats);
  312. DPRINT("rnat @%p = 0x%lx nat=%d old nat=%ld\n",
  313. (void *) rnat_addr, rnats, nat, (rnats >> ia64_rse_slot_num(addr)) & 1);
  314. nat_mask = 1UL << ia64_rse_slot_num(addr);
  315. if (nat)
  316. rnats |= nat_mask;
  317. else
  318. rnats &= ~nat_mask;
  319. ia64_poke(current, sw, (unsigned long) ubs_end, (unsigned long) rnat_addr, rnats);
  320. DPRINT("rnat changed to @%p = 0x%lx\n", (void *) rnat_addr, rnats);
  321. }
  322. static void
  323. get_rse_reg (struct pt_regs *regs, unsigned long r1, unsigned long *val, int *nat)
  324. {
  325. struct switch_stack *sw = (struct switch_stack *) regs - 1;
  326. unsigned long *bsp, *addr, *rnat_addr, *ubs_end, *bspstore;
  327. unsigned long *kbs = (void *) current + IA64_RBS_OFFSET;
  328. unsigned long rnats, nat_mask;
  329. unsigned long on_kbs;
  330. long sof = (regs->cr_ifs) & 0x7f;
  331. long sor = 8 * ((regs->cr_ifs >> 14) & 0xf);
  332. long rrb_gr = (regs->cr_ifs >> 18) & 0x7f;
  333. long ridx = r1 - 32;
  334. if (ridx >= sof) {
  335. /* read of out-of-frame register returns an undefined value; 0 in our case. */
  336. DPRINT("ignoring read from r%lu; only %lu registers are allocated!\n", r1, sof);
  337. goto fail;
  338. }
  339. if (ridx < sor)
  340. ridx = rotate_reg(sor, rrb_gr, ridx);
  341. DPRINT("r%lu, sw.bspstore=%lx pt.bspstore=%lx sof=%ld sol=%ld ridx=%ld\n",
  342. r1, sw->ar_bspstore, regs->ar_bspstore, sof, (regs->cr_ifs >> 7) & 0x7f, ridx);
  343. on_kbs = ia64_rse_num_regs(kbs, (unsigned long *) sw->ar_bspstore);
  344. addr = ia64_rse_skip_regs((unsigned long *) sw->ar_bspstore, -sof + ridx);
  345. if (addr >= kbs) {
  346. /* the register is on the kernel backing store: easy... */
  347. *val = *addr;
  348. if (nat) {
  349. rnat_addr = ia64_rse_rnat_addr(addr);
  350. if ((unsigned long) rnat_addr >= sw->ar_bspstore)
  351. rnat_addr = &sw->ar_rnat;
  352. nat_mask = 1UL << ia64_rse_slot_num(addr);
  353. *nat = (*rnat_addr & nat_mask) != 0;
  354. }
  355. return;
  356. }
  357. if (!user_stack(current, regs)) {
  358. DPRINT("ignoring kernel read of r%lu; register isn't on the RBS!", r1);
  359. goto fail;
  360. }
  361. bspstore = (unsigned long *)regs->ar_bspstore;
  362. ubs_end = ia64_rse_skip_regs(bspstore, on_kbs);
  363. bsp = ia64_rse_skip_regs(ubs_end, -sof);
  364. addr = ia64_rse_skip_regs(bsp, ridx);
  365. DPRINT("ubs_end=%p bsp=%p addr=%p\n", (void *) ubs_end, (void *) bsp, (void *) addr);
  366. ia64_peek(current, sw, (unsigned long) ubs_end, (unsigned long) addr, val);
  367. if (nat) {
  368. rnat_addr = ia64_rse_rnat_addr(addr);
  369. nat_mask = 1UL << ia64_rse_slot_num(addr);
  370. DPRINT("rnat @%p = 0x%lx\n", (void *) rnat_addr, rnats);
  371. ia64_peek(current, sw, (unsigned long) ubs_end, (unsigned long) rnat_addr, &rnats);
  372. *nat = (rnats & nat_mask) != 0;
  373. }
  374. return;
  375. fail:
  376. *val = 0;
  377. if (nat)
  378. *nat = 0;
  379. return;
  380. }
  381. static void
  382. setreg (unsigned long regnum, unsigned long val, int nat, struct pt_regs *regs)
  383. {
  384. struct switch_stack *sw = (struct switch_stack *) regs - 1;
  385. unsigned long addr;
  386. unsigned long bitmask;
  387. unsigned long *unat;
  388. /*
  389. * First takes care of stacked registers
  390. */
  391. if (regnum >= IA64_FIRST_STACKED_GR) {
  392. set_rse_reg(regs, regnum, val, nat);
  393. return;
  394. }
  395. /*
  396. * Using r0 as a target raises a General Exception fault which has higher priority
  397. * than the Unaligned Reference fault.
  398. */
  399. /*
  400. * Now look at registers in [0-31] range and init correct UNAT
  401. */
  402. if (GR_IN_SW(regnum)) {
  403. addr = (unsigned long)sw;
  404. unat = &sw->ar_unat;
  405. } else {
  406. addr = (unsigned long)regs;
  407. unat = &sw->caller_unat;
  408. }
  409. DPRINT("tmp_base=%lx switch_stack=%s offset=%d\n",
  410. addr, unat==&sw->ar_unat ? "yes":"no", GR_OFFS(regnum));
  411. /*
  412. * add offset from base of struct
  413. * and do it !
  414. */
  415. addr += GR_OFFS(regnum);
  416. *(unsigned long *)addr = val;
  417. /*
  418. * We need to clear the corresponding UNAT bit to fully emulate the load
  419. * UNAT bit_pos = GR[r3]{8:3} form EAS-2.4
  420. */
  421. bitmask = 1UL << (addr >> 3 & 0x3f);
  422. DPRINT("*0x%lx=0x%lx NaT=%d prev_unat @%p=%lx\n", addr, val, nat, (void *) unat, *unat);
  423. if (nat) {
  424. *unat |= bitmask;
  425. } else {
  426. *unat &= ~bitmask;
  427. }
  428. DPRINT("*0x%lx=0x%lx NaT=%d new unat: %p=%lx\n", addr, val, nat, (void *) unat,*unat);
  429. }
  430. /*
  431. * Return the (rotated) index for floating point register REGNUM (REGNUM must be in the
  432. * range from 32-127, result is in the range from 0-95.
  433. */
  434. static inline unsigned long
  435. fph_index (struct pt_regs *regs, long regnum)
  436. {
  437. unsigned long rrb_fr = (regs->cr_ifs >> 25) & 0x7f;
  438. return rotate_reg(96, rrb_fr, (regnum - IA64_FIRST_ROTATING_FR));
  439. }
  440. static void
  441. setfpreg (unsigned long regnum, struct ia64_fpreg *fpval, struct pt_regs *regs)
  442. {
  443. struct switch_stack *sw = (struct switch_stack *)regs - 1;
  444. unsigned long addr;
  445. /*
  446. * From EAS-2.5: FPDisableFault has higher priority than Unaligned
  447. * Fault. Thus, when we get here, we know the partition is enabled.
  448. * To update f32-f127, there are three choices:
  449. *
  450. * (1) save f32-f127 to thread.fph and update the values there
  451. * (2) use a gigantic switch statement to directly access the registers
  452. * (3) generate code on the fly to update the desired register
  453. *
  454. * For now, we are using approach (1).
  455. */
  456. if (regnum >= IA64_FIRST_ROTATING_FR) {
  457. ia64_sync_fph(current);
  458. current->thread.fph[fph_index(regs, regnum)] = *fpval;
  459. } else {
  460. /*
  461. * pt_regs or switch_stack ?
  462. */
  463. if (FR_IN_SW(regnum)) {
  464. addr = (unsigned long)sw;
  465. } else {
  466. addr = (unsigned long)regs;
  467. }
  468. DPRINT("tmp_base=%lx offset=%d\n", addr, FR_OFFS(regnum));
  469. addr += FR_OFFS(regnum);
  470. *(struct ia64_fpreg *)addr = *fpval;
  471. /*
  472. * mark the low partition as being used now
  473. *
  474. * It is highly unlikely that this bit is not already set, but
  475. * let's do it for safety.
  476. */
  477. regs->cr_ipsr |= IA64_PSR_MFL;
  478. }
  479. }
  480. /*
  481. * Those 2 inline functions generate the spilled versions of the constant floating point
  482. * registers which can be used with stfX
  483. */
  484. static inline void
  485. float_spill_f0 (struct ia64_fpreg *final)
  486. {
  487. ia64_stf_spill(final, 0);
  488. }
  489. static inline void
  490. float_spill_f1 (struct ia64_fpreg *final)
  491. {
  492. ia64_stf_spill(final, 1);
  493. }
  494. static void
  495. getfpreg (unsigned long regnum, struct ia64_fpreg *fpval, struct pt_regs *regs)
  496. {
  497. struct switch_stack *sw = (struct switch_stack *) regs - 1;
  498. unsigned long addr;
  499. /*
  500. * From EAS-2.5: FPDisableFault has higher priority than
  501. * Unaligned Fault. Thus, when we get here, we know the partition is
  502. * enabled.
  503. *
  504. * When regnum > 31, the register is still live and we need to force a save
  505. * to current->thread.fph to get access to it. See discussion in setfpreg()
  506. * for reasons and other ways of doing this.
  507. */
  508. if (regnum >= IA64_FIRST_ROTATING_FR) {
  509. ia64_flush_fph(current);
  510. *fpval = current->thread.fph[fph_index(regs, regnum)];
  511. } else {
  512. /*
  513. * f0 = 0.0, f1= 1.0. Those registers are constant and are thus
  514. * not saved, we must generate their spilled form on the fly
  515. */
  516. switch(regnum) {
  517. case 0:
  518. float_spill_f0(fpval);
  519. break;
  520. case 1:
  521. float_spill_f1(fpval);
  522. break;
  523. default:
  524. /*
  525. * pt_regs or switch_stack ?
  526. */
  527. addr = FR_IN_SW(regnum) ? (unsigned long)sw
  528. : (unsigned long)regs;
  529. DPRINT("is_sw=%d tmp_base=%lx offset=0x%x\n",
  530. FR_IN_SW(regnum), addr, FR_OFFS(regnum));
  531. addr += FR_OFFS(regnum);
  532. *fpval = *(struct ia64_fpreg *)addr;
  533. }
  534. }
  535. }
  536. static void
  537. getreg (unsigned long regnum, unsigned long *val, int *nat, struct pt_regs *regs)
  538. {
  539. struct switch_stack *sw = (struct switch_stack *) regs - 1;
  540. unsigned long addr, *unat;
  541. if (regnum >= IA64_FIRST_STACKED_GR) {
  542. get_rse_reg(regs, regnum, val, nat);
  543. return;
  544. }
  545. /*
  546. * take care of r0 (read-only always evaluate to 0)
  547. */
  548. if (regnum == 0) {
  549. *val = 0;
  550. if (nat)
  551. *nat = 0;
  552. return;
  553. }
  554. /*
  555. * Now look at registers in [0-31] range and init correct UNAT
  556. */
  557. if (GR_IN_SW(regnum)) {
  558. addr = (unsigned long)sw;
  559. unat = &sw->ar_unat;
  560. } else {
  561. addr = (unsigned long)regs;
  562. unat = &sw->caller_unat;
  563. }
  564. DPRINT("addr_base=%lx offset=0x%x\n", addr, GR_OFFS(regnum));
  565. addr += GR_OFFS(regnum);
  566. *val = *(unsigned long *)addr;
  567. /*
  568. * do it only when requested
  569. */
  570. if (nat)
  571. *nat = (*unat >> (addr >> 3 & 0x3f)) & 0x1UL;
  572. }
  573. static void
  574. emulate_load_updates (update_t type, load_store_t ld, struct pt_regs *regs, unsigned long ifa)
  575. {
  576. /*
  577. * IMPORTANT:
  578. * Given the way we handle unaligned speculative loads, we should
  579. * not get to this point in the code but we keep this sanity check,
  580. * just in case.
  581. */
  582. if (ld.x6_op == 1 || ld.x6_op == 3) {
  583. printk(KERN_ERR "%s: register update on speculative load, error\n", __FUNCTION__);
  584. die_if_kernel("unaligned reference on speculative load with register update\n",
  585. regs, 30);
  586. }
  587. /*
  588. * at this point, we know that the base register to update is valid i.e.,
  589. * it's not r0
  590. */
  591. if (type == UPD_IMMEDIATE) {
  592. unsigned long imm;
  593. /*
  594. * Load +Imm: ldXZ r1=[r3],imm(9)
  595. *
  596. *
  597. * form imm9: [13:19] contain the first 7 bits
  598. */
  599. imm = ld.x << 7 | ld.imm;
  600. /*
  601. * sign extend (1+8bits) if m set
  602. */
  603. if (ld.m) imm |= SIGN_EXT9;
  604. /*
  605. * ifa == r3 and we know that the NaT bit on r3 was clear so
  606. * we can directly use ifa.
  607. */
  608. ifa += imm;
  609. setreg(ld.r3, ifa, 0, regs);
  610. DPRINT("ld.x=%d ld.m=%d imm=%ld r3=0x%lx\n", ld.x, ld.m, imm, ifa);
  611. } else if (ld.m) {
  612. unsigned long r2;
  613. int nat_r2;
  614. /*
  615. * Load +Reg Opcode: ldXZ r1=[r3],r2
  616. *
  617. * Note: that we update r3 even in the case of ldfX.a
  618. * (where the load does not happen)
  619. *
  620. * The way the load algorithm works, we know that r3 does not
  621. * have its NaT bit set (would have gotten NaT consumption
  622. * before getting the unaligned fault). So we can use ifa
  623. * which equals r3 at this point.
  624. *
  625. * IMPORTANT:
  626. * The above statement holds ONLY because we know that we
  627. * never reach this code when trying to do a ldX.s.
  628. * If we ever make it to here on an ldfX.s then
  629. */
  630. getreg(ld.imm, &r2, &nat_r2, regs);
  631. ifa += r2;
  632. /*
  633. * propagate Nat r2 -> r3
  634. */
  635. setreg(ld.r3, ifa, nat_r2, regs);
  636. DPRINT("imm=%d r2=%ld r3=0x%lx nat_r2=%d\n",ld.imm, r2, ifa, nat_r2);
  637. }
  638. }
  639. static int
  640. emulate_load_int (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
  641. {
  642. unsigned int len = 1 << ld.x6_sz;
  643. unsigned long val = 0;
  644. /*
  645. * r0, as target, doesn't need to be checked because Illegal Instruction
  646. * faults have higher priority than unaligned faults.
  647. *
  648. * r0 cannot be found as the base as it would never generate an
  649. * unaligned reference.
  650. */
  651. /*
  652. * ldX.a we will emulate load and also invalidate the ALAT entry.
  653. * See comment below for explanation on how we handle ldX.a
  654. */
  655. if (len != 2 && len != 4 && len != 8) {
  656. DPRINT("unknown size: x6=%d\n", ld.x6_sz);
  657. return -1;
  658. }
  659. /* this assumes little-endian byte-order: */
  660. if (copy_from_user(&val, (void __user *) ifa, len))
  661. return -1;
  662. setreg(ld.r1, val, 0, regs);
  663. /*
  664. * check for updates on any kind of loads
  665. */
  666. if (ld.op == 0x5 || ld.m)
  667. emulate_load_updates(ld.op == 0x5 ? UPD_IMMEDIATE: UPD_REG, ld, regs, ifa);
  668. /*
  669. * handling of various loads (based on EAS2.4):
  670. *
  671. * ldX.acq (ordered load):
  672. * - acquire semantics would have been used, so force fence instead.
  673. *
  674. * ldX.c.clr (check load and clear):
  675. * - if we get to this handler, it's because the entry was not in the ALAT.
  676. * Therefore the operation reverts to a normal load
  677. *
  678. * ldX.c.nc (check load no clear):
  679. * - same as previous one
  680. *
  681. * ldX.c.clr.acq (ordered check load and clear):
  682. * - same as above for c.clr part. The load needs to have acquire semantics. So
  683. * we use the fence semantics which is stronger and thus ensures correctness.
  684. *
  685. * ldX.a (advanced load):
  686. * - suppose ldX.a r1=[r3]. If we get to the unaligned trap it's because the
  687. * address doesn't match requested size alignment. This means that we would
  688. * possibly need more than one load to get the result.
  689. *
  690. * The load part can be handled just like a normal load, however the difficult
  691. * part is to get the right thing into the ALAT. The critical piece of information
  692. * in the base address of the load & size. To do that, a ld.a must be executed,
  693. * clearly any address can be pushed into the table by using ld1.a r1=[r3]. Now
  694. * if we use the same target register, we will be okay for the check.a instruction.
  695. * If we look at the store, basically a stX [r3]=r1 checks the ALAT for any entry
  696. * which would overlap within [r3,r3+X] (the size of the load was store in the
  697. * ALAT). If such an entry is found the entry is invalidated. But this is not good
  698. * enough, take the following example:
  699. * r3=3
  700. * ld4.a r1=[r3]
  701. *
  702. * Could be emulated by doing:
  703. * ld1.a r1=[r3],1
  704. * store to temporary;
  705. * ld1.a r1=[r3],1
  706. * store & shift to temporary;
  707. * ld1.a r1=[r3],1
  708. * store & shift to temporary;
  709. * ld1.a r1=[r3]
  710. * store & shift to temporary;
  711. * r1=temporary
  712. *
  713. * So in this case, you would get the right value is r1 but the wrong info in
  714. * the ALAT. Notice that you could do it in reverse to finish with address 3
  715. * but you would still get the size wrong. To get the size right, one needs to
  716. * execute exactly the same kind of load. You could do it from a aligned
  717. * temporary location, but you would get the address wrong.
  718. *
  719. * So no matter what, it is not possible to emulate an advanced load
  720. * correctly. But is that really critical ?
  721. *
  722. * We will always convert ld.a into a normal load with ALAT invalidated. This
  723. * will enable compiler to do optimization where certain code path after ld.a
  724. * is not required to have ld.c/chk.a, e.g., code path with no intervening stores.
  725. *
  726. * If there is a store after the advanced load, one must either do a ld.c.* or
  727. * chk.a.* to reuse the value stored in the ALAT. Both can "fail" (meaning no
  728. * entry found in ALAT), and that's perfectly ok because:
  729. *
  730. * - ld.c.*, if the entry is not present a normal load is executed
  731. * - chk.a.*, if the entry is not present, execution jumps to recovery code
  732. *
  733. * In either case, the load can be potentially retried in another form.
  734. *
  735. * ALAT must be invalidated for the register (so that chk.a or ld.c don't pick
  736. * up a stale entry later). The register base update MUST also be performed.
  737. */
  738. /*
  739. * when the load has the .acq completer then
  740. * use ordering fence.
  741. */
  742. if (ld.x6_op == 0x5 || ld.x6_op == 0xa)
  743. mb();
  744. /*
  745. * invalidate ALAT entry in case of advanced load
  746. */
  747. if (ld.x6_op == 0x2)
  748. invala_gr(ld.r1);
  749. return 0;
  750. }
  751. static int
  752. emulate_store_int (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
  753. {
  754. unsigned long r2;
  755. unsigned int len = 1 << ld.x6_sz;
  756. /*
  757. * if we get to this handler, Nat bits on both r3 and r2 have already
  758. * been checked. so we don't need to do it
  759. *
  760. * extract the value to be stored
  761. */
  762. getreg(ld.imm, &r2, NULL, regs);
  763. /*
  764. * we rely on the macros in unaligned.h for now i.e.,
  765. * we let the compiler figure out how to read memory gracefully.
  766. *
  767. * We need this switch/case because the way the inline function
  768. * works. The code is optimized by the compiler and looks like
  769. * a single switch/case.
  770. */
  771. DPRINT("st%d [%lx]=%lx\n", len, ifa, r2);
  772. if (len != 2 && len != 4 && len != 8) {
  773. DPRINT("unknown size: x6=%d\n", ld.x6_sz);
  774. return -1;
  775. }
  776. /* this assumes little-endian byte-order: */
  777. if (copy_to_user((void __user *) ifa, &r2, len))
  778. return -1;
  779. /*
  780. * stX [r3]=r2,imm(9)
  781. *
  782. * NOTE:
  783. * ld.r3 can never be r0, because r0 would not generate an
  784. * unaligned access.
  785. */
  786. if (ld.op == 0x5) {
  787. unsigned long imm;
  788. /*
  789. * form imm9: [12:6] contain first 7bits
  790. */
  791. imm = ld.x << 7 | ld.r1;
  792. /*
  793. * sign extend (8bits) if m set
  794. */
  795. if (ld.m) imm |= SIGN_EXT9;
  796. /*
  797. * ifa == r3 (NaT is necessarily cleared)
  798. */
  799. ifa += imm;
  800. DPRINT("imm=%lx r3=%lx\n", imm, ifa);
  801. setreg(ld.r3, ifa, 0, regs);
  802. }
  803. /*
  804. * we don't have alat_invalidate_multiple() so we need
  805. * to do the complete flush :-<<
  806. */
  807. ia64_invala();
  808. /*
  809. * stX.rel: use fence instead of release
  810. */
  811. if (ld.x6_op == 0xd)
  812. mb();
  813. return 0;
  814. }
  815. /*
  816. * floating point operations sizes in bytes
  817. */
  818. static const unsigned char float_fsz[4]={
  819. 10, /* extended precision (e) */
  820. 8, /* integer (8) */
  821. 4, /* single precision (s) */
  822. 8 /* double precision (d) */
  823. };
  824. static inline void
  825. mem2float_extended (struct ia64_fpreg *init, struct ia64_fpreg *final)
  826. {
  827. ia64_ldfe(6, init);
  828. ia64_stop();
  829. ia64_stf_spill(final, 6);
  830. }
  831. static inline void
  832. mem2float_integer (struct ia64_fpreg *init, struct ia64_fpreg *final)
  833. {
  834. ia64_ldf8(6, init);
  835. ia64_stop();
  836. ia64_stf_spill(final, 6);
  837. }
  838. static inline void
  839. mem2float_single (struct ia64_fpreg *init, struct ia64_fpreg *final)
  840. {
  841. ia64_ldfs(6, init);
  842. ia64_stop();
  843. ia64_stf_spill(final, 6);
  844. }
  845. static inline void
  846. mem2float_double (struct ia64_fpreg *init, struct ia64_fpreg *final)
  847. {
  848. ia64_ldfd(6, init);
  849. ia64_stop();
  850. ia64_stf_spill(final, 6);
  851. }
  852. static inline void
  853. float2mem_extended (struct ia64_fpreg *init, struct ia64_fpreg *final)
  854. {
  855. ia64_ldf_fill(6, init);
  856. ia64_stop();
  857. ia64_stfe(final, 6);
  858. }
  859. static inline void
  860. float2mem_integer (struct ia64_fpreg *init, struct ia64_fpreg *final)
  861. {
  862. ia64_ldf_fill(6, init);
  863. ia64_stop();
  864. ia64_stf8(final, 6);
  865. }
  866. static inline void
  867. float2mem_single (struct ia64_fpreg *init, struct ia64_fpreg *final)
  868. {
  869. ia64_ldf_fill(6, init);
  870. ia64_stop();
  871. ia64_stfs(final, 6);
  872. }
  873. static inline void
  874. float2mem_double (struct ia64_fpreg *init, struct ia64_fpreg *final)
  875. {
  876. ia64_ldf_fill(6, init);
  877. ia64_stop();
  878. ia64_stfd(final, 6);
  879. }
  880. static int
  881. emulate_load_floatpair (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
  882. {
  883. struct ia64_fpreg fpr_init[2];
  884. struct ia64_fpreg fpr_final[2];
  885. unsigned long len = float_fsz[ld.x6_sz];
  886. /*
  887. * fr0 & fr1 don't need to be checked because Illegal Instruction faults have
  888. * higher priority than unaligned faults.
  889. *
  890. * r0 cannot be found as the base as it would never generate an unaligned
  891. * reference.
  892. */
  893. /*
  894. * make sure we get clean buffers
  895. */
  896. memset(&fpr_init, 0, sizeof(fpr_init));
  897. memset(&fpr_final, 0, sizeof(fpr_final));
  898. /*
  899. * ldfpX.a: we don't try to emulate anything but we must
  900. * invalidate the ALAT entry and execute updates, if any.
  901. */
  902. if (ld.x6_op != 0x2) {
  903. /*
  904. * This assumes little-endian byte-order. Note that there is no "ldfpe"
  905. * instruction:
  906. */
  907. if (copy_from_user(&fpr_init[0], (void __user *) ifa, len)
  908. || copy_from_user(&fpr_init[1], (void __user *) (ifa + len), len))
  909. return -1;
  910. DPRINT("ld.r1=%d ld.imm=%d x6_sz=%d\n", ld.r1, ld.imm, ld.x6_sz);
  911. DDUMP("frp_init =", &fpr_init, 2*len);
  912. /*
  913. * XXX fixme
  914. * Could optimize inlines by using ldfpX & 2 spills
  915. */
  916. switch( ld.x6_sz ) {
  917. case 0:
  918. mem2float_extended(&fpr_init[0], &fpr_final[0]);
  919. mem2float_extended(&fpr_init[1], &fpr_final[1]);
  920. break;
  921. case 1:
  922. mem2float_integer(&fpr_init[0], &fpr_final[0]);
  923. mem2float_integer(&fpr_init[1], &fpr_final[1]);
  924. break;
  925. case 2:
  926. mem2float_single(&fpr_init[0], &fpr_final[0]);
  927. mem2float_single(&fpr_init[1], &fpr_final[1]);
  928. break;
  929. case 3:
  930. mem2float_double(&fpr_init[0], &fpr_final[0]);
  931. mem2float_double(&fpr_init[1], &fpr_final[1]);
  932. break;
  933. }
  934. DDUMP("fpr_final =", &fpr_final, 2*len);
  935. /*
  936. * XXX fixme
  937. *
  938. * A possible optimization would be to drop fpr_final and directly
  939. * use the storage from the saved context i.e., the actual final
  940. * destination (pt_regs, switch_stack or thread structure).
  941. */
  942. setfpreg(ld.r1, &fpr_final[0], regs);
  943. setfpreg(ld.imm, &fpr_final[1], regs);
  944. }
  945. /*
  946. * Check for updates: only immediate updates are available for this
  947. * instruction.
  948. */
  949. if (ld.m) {
  950. /*
  951. * the immediate is implicit given the ldsz of the operation:
  952. * single: 8 (2x4) and for all others it's 16 (2x8)
  953. */
  954. ifa += len<<1;
  955. /*
  956. * IMPORTANT:
  957. * the fact that we force the NaT of r3 to zero is ONLY valid
  958. * as long as we don't come here with a ldfpX.s.
  959. * For this reason we keep this sanity check
  960. */
  961. if (ld.x6_op == 1 || ld.x6_op == 3)
  962. printk(KERN_ERR "%s: register update on speculative load pair, error\n",
  963. __FUNCTION__);
  964. setreg(ld.r3, ifa, 0, regs);
  965. }
  966. /*
  967. * Invalidate ALAT entries, if any, for both registers.
  968. */
  969. if (ld.x6_op == 0x2) {
  970. invala_fr(ld.r1);
  971. invala_fr(ld.imm);
  972. }
  973. return 0;
  974. }
  975. static int
  976. emulate_load_float (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
  977. {
  978. struct ia64_fpreg fpr_init;
  979. struct ia64_fpreg fpr_final;
  980. unsigned long len = float_fsz[ld.x6_sz];
  981. /*
  982. * fr0 & fr1 don't need to be checked because Illegal Instruction
  983. * faults have higher priority than unaligned faults.
  984. *
  985. * r0 cannot be found as the base as it would never generate an
  986. * unaligned reference.
  987. */
  988. /*
  989. * make sure we get clean buffers
  990. */
  991. memset(&fpr_init,0, sizeof(fpr_init));
  992. memset(&fpr_final,0, sizeof(fpr_final));
  993. /*
  994. * ldfX.a we don't try to emulate anything but we must
  995. * invalidate the ALAT entry.
  996. * See comments in ldX for descriptions on how the various loads are handled.
  997. */
  998. if (ld.x6_op != 0x2) {
  999. if (copy_from_user(&fpr_init, (void __user *) ifa, len))
  1000. return -1;
  1001. DPRINT("ld.r1=%d x6_sz=%d\n", ld.r1, ld.x6_sz);
  1002. DDUMP("fpr_init =", &fpr_init, len);
  1003. /*
  1004. * we only do something for x6_op={0,8,9}
  1005. */
  1006. switch( ld.x6_sz ) {
  1007. case 0:
  1008. mem2float_extended(&fpr_init, &fpr_final);
  1009. break;
  1010. case 1:
  1011. mem2float_integer(&fpr_init, &fpr_final);
  1012. break;
  1013. case 2:
  1014. mem2float_single(&fpr_init, &fpr_final);
  1015. break;
  1016. case 3:
  1017. mem2float_double(&fpr_init, &fpr_final);
  1018. break;
  1019. }
  1020. DDUMP("fpr_final =", &fpr_final, len);
  1021. /*
  1022. * XXX fixme
  1023. *
  1024. * A possible optimization would be to drop fpr_final and directly
  1025. * use the storage from the saved context i.e., the actual final
  1026. * destination (pt_regs, switch_stack or thread structure).
  1027. */
  1028. setfpreg(ld.r1, &fpr_final, regs);
  1029. }
  1030. /*
  1031. * check for updates on any loads
  1032. */
  1033. if (ld.op == 0x7 || ld.m)
  1034. emulate_load_updates(ld.op == 0x7 ? UPD_IMMEDIATE: UPD_REG, ld, regs, ifa);
  1035. /*
  1036. * invalidate ALAT entry in case of advanced floating point loads
  1037. */
  1038. if (ld.x6_op == 0x2)
  1039. invala_fr(ld.r1);
  1040. return 0;
  1041. }
  1042. static int
  1043. emulate_store_float (unsigned long ifa, load_store_t ld, struct pt_regs *regs)
  1044. {
  1045. struct ia64_fpreg fpr_init;
  1046. struct ia64_fpreg fpr_final;
  1047. unsigned long len = float_fsz[ld.x6_sz];
  1048. /*
  1049. * make sure we get clean buffers
  1050. */
  1051. memset(&fpr_init,0, sizeof(fpr_init));
  1052. memset(&fpr_final,0, sizeof(fpr_final));
  1053. /*
  1054. * if we get to this handler, Nat bits on both r3 and r2 have already
  1055. * been checked. so we don't need to do it
  1056. *
  1057. * extract the value to be stored
  1058. */
  1059. getfpreg(ld.imm, &fpr_init, regs);
  1060. /*
  1061. * during this step, we extract the spilled registers from the saved
  1062. * context i.e., we refill. Then we store (no spill) to temporary
  1063. * aligned location
  1064. */
  1065. switch( ld.x6_sz ) {
  1066. case 0:
  1067. float2mem_extended(&fpr_init, &fpr_final);
  1068. break;
  1069. case 1:
  1070. float2mem_integer(&fpr_init, &fpr_final);
  1071. break;
  1072. case 2:
  1073. float2mem_single(&fpr_init, &fpr_final);
  1074. break;
  1075. case 3:
  1076. float2mem_double(&fpr_init, &fpr_final);
  1077. break;
  1078. }
  1079. DPRINT("ld.r1=%d x6_sz=%d\n", ld.r1, ld.x6_sz);
  1080. DDUMP("fpr_init =", &fpr_init, len);
  1081. DDUMP("fpr_final =", &fpr_final, len);
  1082. if (copy_to_user((void __user *) ifa, &fpr_final, len))
  1083. return -1;
  1084. /*
  1085. * stfX [r3]=r2,imm(9)
  1086. *
  1087. * NOTE:
  1088. * ld.r3 can never be r0, because r0 would not generate an
  1089. * unaligned access.
  1090. */
  1091. if (ld.op == 0x7) {
  1092. unsigned long imm;
  1093. /*
  1094. * form imm9: [12:6] contain first 7bits
  1095. */
  1096. imm = ld.x << 7 | ld.r1;
  1097. /*
  1098. * sign extend (8bits) if m set
  1099. */
  1100. if (ld.m)
  1101. imm |= SIGN_EXT9;
  1102. /*
  1103. * ifa == r3 (NaT is necessarily cleared)
  1104. */
  1105. ifa += imm;
  1106. DPRINT("imm=%lx r3=%lx\n", imm, ifa);
  1107. setreg(ld.r3, ifa, 0, regs);
  1108. }
  1109. /*
  1110. * we don't have alat_invalidate_multiple() so we need
  1111. * to do the complete flush :-<<
  1112. */
  1113. ia64_invala();
  1114. return 0;
  1115. }
  1116. /*
  1117. * Make sure we log the unaligned access, so that user/sysadmin can notice it and
  1118. * eventually fix the program. However, we don't want to do that for every access so we
  1119. * pace it with jiffies. This isn't really MP-safe, but it doesn't really have to be
  1120. * either...
  1121. */
  1122. static int
  1123. within_logging_rate_limit (void)
  1124. {
  1125. static unsigned long count, last_time;
  1126. if (jiffies - last_time > 5*HZ)
  1127. count = 0;
  1128. if (count < 5) {
  1129. last_time = jiffies;
  1130. count++;
  1131. return 1;
  1132. }
  1133. return 0;
  1134. }
  1135. void
  1136. ia64_handle_unaligned (unsigned long ifa, struct pt_regs *regs)
  1137. {
  1138. struct ia64_psr *ipsr = ia64_psr(regs);
  1139. mm_segment_t old_fs = get_fs();
  1140. unsigned long bundle[2];
  1141. unsigned long opcode;
  1142. struct siginfo si;
  1143. const struct exception_table_entry *eh = NULL;
  1144. union {
  1145. unsigned long l;
  1146. load_store_t insn;
  1147. } u;
  1148. int ret = -1;
  1149. if (ia64_psr(regs)->be) {
  1150. /* we don't support big-endian accesses */
  1151. die_if_kernel("big-endian unaligned accesses are not supported", regs, 0);
  1152. goto force_sigbus;
  1153. }
  1154. /*
  1155. * Treat kernel accesses for which there is an exception handler entry the same as
  1156. * user-level unaligned accesses. Otherwise, a clever program could trick this
  1157. * handler into reading an arbitrary kernel addresses...
  1158. */
  1159. if (!user_mode(regs))
  1160. eh = search_exception_tables(regs->cr_iip + ia64_psr(regs)->ri);
  1161. if (user_mode(regs) || eh) {
  1162. if ((current->thread.flags & IA64_THREAD_UAC_SIGBUS) != 0)
  1163. goto force_sigbus;
  1164. if (!no_unaligned_warning &&
  1165. !(current->thread.flags & IA64_THREAD_UAC_NOPRINT) &&
  1166. within_logging_rate_limit())
  1167. {
  1168. char buf[200]; /* comm[] is at most 16 bytes... */
  1169. size_t len;
  1170. len = sprintf(buf, "%s(%d): unaligned access to 0x%016lx, "
  1171. "ip=0x%016lx\n\r", current->comm, current->pid,
  1172. ifa, regs->cr_iip + ipsr->ri);
  1173. /*
  1174. * Don't call tty_write_message() if we're in the kernel; we might
  1175. * be holding locks...
  1176. */
  1177. if (user_mode(regs))
  1178. tty_write_message(current->signal->tty, buf);
  1179. buf[len-1] = '\0'; /* drop '\r' */
  1180. /* watch for command names containing %s */
  1181. printk(KERN_WARNING "%s", buf);
  1182. } else {
  1183. if (no_unaligned_warning && !noprint_warning) {
  1184. noprint_warning = 1;
  1185. printk(KERN_WARNING "%s(%d) encountered an "
  1186. "unaligned exception which required\n"
  1187. "kernel assistance, which degrades "
  1188. "the performance of the application.\n"
  1189. "Unaligned exception warnings have "
  1190. "been disabled by the system "
  1191. "administrator\n"
  1192. "echo 0 > /proc/sys/kernel/ignore-"
  1193. "unaligned-usertrap to re-enable\n",
  1194. current->comm, current->pid);
  1195. }
  1196. }
  1197. } else {
  1198. if (within_logging_rate_limit())
  1199. printk(KERN_WARNING "kernel unaligned access to 0x%016lx, ip=0x%016lx\n",
  1200. ifa, regs->cr_iip + ipsr->ri);
  1201. set_fs(KERNEL_DS);
  1202. }
  1203. DPRINT("iip=%lx ifa=%lx isr=%lx (ei=%d, sp=%d)\n",
  1204. regs->cr_iip, ifa, regs->cr_ipsr, ipsr->ri, ipsr->it);
  1205. if (__copy_from_user(bundle, (void __user *) regs->cr_iip, 16))
  1206. goto failure;
  1207. /*
  1208. * extract the instruction from the bundle given the slot number
  1209. */
  1210. switch (ipsr->ri) {
  1211. case 0: u.l = (bundle[0] >> 5); break;
  1212. case 1: u.l = (bundle[0] >> 46) | (bundle[1] << 18); break;
  1213. case 2: u.l = (bundle[1] >> 23); break;
  1214. }
  1215. opcode = (u.l >> IA64_OPCODE_SHIFT) & IA64_OPCODE_MASK;
  1216. DPRINT("opcode=%lx ld.qp=%d ld.r1=%d ld.imm=%d ld.r3=%d ld.x=%d ld.hint=%d "
  1217. "ld.x6=0x%x ld.m=%d ld.op=%d\n", opcode, u.insn.qp, u.insn.r1, u.insn.imm,
  1218. u.insn.r3, u.insn.x, u.insn.hint, u.insn.x6_sz, u.insn.m, u.insn.op);
  1219. /*
  1220. * IMPORTANT:
  1221. * Notice that the switch statement DOES not cover all possible instructions
  1222. * that DO generate unaligned references. This is made on purpose because for some
  1223. * instructions it DOES NOT make sense to try and emulate the access. Sometimes it
  1224. * is WRONG to try and emulate. Here is a list of instruction we don't emulate i.e.,
  1225. * the program will get a signal and die:
  1226. *
  1227. * load/store:
  1228. * - ldX.spill
  1229. * - stX.spill
  1230. * Reason: RNATs are based on addresses
  1231. * - ld16
  1232. * - st16
  1233. * Reason: ld16 and st16 are supposed to occur in a single
  1234. * memory op
  1235. *
  1236. * synchronization:
  1237. * - cmpxchg
  1238. * - fetchadd
  1239. * - xchg
  1240. * Reason: ATOMIC operations cannot be emulated properly using multiple
  1241. * instructions.
  1242. *
  1243. * speculative loads:
  1244. * - ldX.sZ
  1245. * Reason: side effects, code must be ready to deal with failure so simpler
  1246. * to let the load fail.
  1247. * ---------------------------------------------------------------------------------
  1248. * XXX fixme
  1249. *
  1250. * I would like to get rid of this switch case and do something
  1251. * more elegant.
  1252. */
  1253. switch (opcode) {
  1254. case LDS_OP:
  1255. case LDSA_OP:
  1256. if (u.insn.x)
  1257. /* oops, really a semaphore op (cmpxchg, etc) */
  1258. goto failure;
  1259. /* no break */
  1260. case LDS_IMM_OP:
  1261. case LDSA_IMM_OP:
  1262. case LDFS_OP:
  1263. case LDFSA_OP:
  1264. case LDFS_IMM_OP:
  1265. /*
  1266. * The instruction will be retried with deferred exceptions turned on, and
  1267. * we should get Nat bit installed
  1268. *
  1269. * IMPORTANT: When PSR_ED is set, the register & immediate update forms
  1270. * are actually executed even though the operation failed. So we don't
  1271. * need to take care of this.
  1272. */
  1273. DPRINT("forcing PSR_ED\n");
  1274. regs->cr_ipsr |= IA64_PSR_ED;
  1275. goto done;
  1276. case LD_OP:
  1277. case LDA_OP:
  1278. case LDBIAS_OP:
  1279. case LDACQ_OP:
  1280. case LDCCLR_OP:
  1281. case LDCNC_OP:
  1282. case LDCCLRACQ_OP:
  1283. if (u.insn.x)
  1284. /* oops, really a semaphore op (cmpxchg, etc) */
  1285. goto failure;
  1286. /* no break */
  1287. case LD_IMM_OP:
  1288. case LDA_IMM_OP:
  1289. case LDBIAS_IMM_OP:
  1290. case LDACQ_IMM_OP:
  1291. case LDCCLR_IMM_OP:
  1292. case LDCNC_IMM_OP:
  1293. case LDCCLRACQ_IMM_OP:
  1294. ret = emulate_load_int(ifa, u.insn, regs);
  1295. break;
  1296. case ST_OP:
  1297. case STREL_OP:
  1298. if (u.insn.x)
  1299. /* oops, really a semaphore op (cmpxchg, etc) */
  1300. goto failure;
  1301. /* no break */
  1302. case ST_IMM_OP:
  1303. case STREL_IMM_OP:
  1304. ret = emulate_store_int(ifa, u.insn, regs);
  1305. break;
  1306. case LDF_OP:
  1307. case LDFA_OP:
  1308. case LDFCCLR_OP:
  1309. case LDFCNC_OP:
  1310. case LDF_IMM_OP:
  1311. case LDFA_IMM_OP:
  1312. case LDFCCLR_IMM_OP:
  1313. case LDFCNC_IMM_OP:
  1314. if (u.insn.x)
  1315. ret = emulate_load_floatpair(ifa, u.insn, regs);
  1316. else
  1317. ret = emulate_load_float(ifa, u.insn, regs);
  1318. break;
  1319. case STF_OP:
  1320. case STF_IMM_OP:
  1321. ret = emulate_store_float(ifa, u.insn, regs);
  1322. break;
  1323. default:
  1324. goto failure;
  1325. }
  1326. DPRINT("ret=%d\n", ret);
  1327. if (ret)
  1328. goto failure;
  1329. if (ipsr->ri == 2)
  1330. /*
  1331. * given today's architecture this case is not likely to happen because a
  1332. * memory access instruction (M) can never be in the last slot of a
  1333. * bundle. But let's keep it for now.
  1334. */
  1335. regs->cr_iip += 16;
  1336. ipsr->ri = (ipsr->ri + 1) & 0x3;
  1337. DPRINT("ipsr->ri=%d iip=%lx\n", ipsr->ri, regs->cr_iip);
  1338. done:
  1339. set_fs(old_fs); /* restore original address limit */
  1340. return;
  1341. failure:
  1342. /* something went wrong... */
  1343. if (!user_mode(regs)) {
  1344. if (eh) {
  1345. ia64_handle_exception(regs, eh);
  1346. goto done;
  1347. }
  1348. die_if_kernel("error during unaligned kernel access\n", regs, ret);
  1349. /* NOT_REACHED */
  1350. }
  1351. force_sigbus:
  1352. si.si_signo = SIGBUS;
  1353. si.si_errno = 0;
  1354. si.si_code = BUS_ADRALN;
  1355. si.si_addr = (void __user *) ifa;
  1356. si.si_flags = 0;
  1357. si.si_isr = 0;
  1358. si.si_imm = 0;
  1359. force_sig_info(SIGBUS, &si, current);
  1360. goto done;
  1361. }