unaligned.c 42 KB

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