unaligned.c 42 KB

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