dl_helper.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* dl.c - arch-dependent part of loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2004,2005,2007,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/elf.h>
  21. #include <grub/misc.h>
  22. #include <grub/err.h>
  23. #include <grub/mm.h>
  24. #include <grub/i18n.h>
  25. #include <grub/ia64/reloc.h>
  26. #pragma GCC diagnostic ignored "-Wcast-align"
  27. #define MASK20 ((1 << 20) - 1)
  28. #define MASK3 (~(grub_addr_t) 3)
  29. void
  30. grub_ia64_set_immu64 (grub_addr_t addr, grub_uint64_t val)
  31. {
  32. /* Copied from binutils. */
  33. grub_uint64_t *ptr = ((grub_uint64_t *) (addr & MASK3));
  34. grub_uint64_t t0, t1;
  35. t0 = grub_le_to_cpu64 (ptr[0]);
  36. t1 = grub_le_to_cpu64 (ptr[1]);
  37. /* tmpl/s: bits 0.. 5 in t0
  38. slot 0: bits 5..45 in t0
  39. slot 1: bits 46..63 in t0, bits 0..22 in t1
  40. slot 2: bits 23..63 in t1 */
  41. /* First, clear the bits that form the 64 bit constant. */
  42. t0 &= ~(0x3ffffLL << 46);
  43. t1 &= ~(0x7fffffLL
  44. | (( (0x07fLL << 13) | (0x1ffLL << 27)
  45. | (0x01fLL << 22) | (0x001LL << 21)
  46. | (0x001LL << 36)) << 23));
  47. t0 |= ((val >> 22) & 0x03ffffLL) << 46; /* 18 lsbs of imm41 */
  48. t1 |= ((val >> 40) & 0x7fffffLL) << 0; /* 23 msbs of imm41 */
  49. t1 |= ( (((val >> 0) & 0x07f) << 13) /* imm7b */
  50. | (((val >> 7) & 0x1ff) << 27) /* imm9d */
  51. | (((val >> 16) & 0x01f) << 22) /* imm5c */
  52. | (((val >> 21) & 0x001) << 21) /* ic */
  53. | (((val >> 63) & 0x001) << 36)) << 23; /* i */
  54. ptr[0] = t0;
  55. ptr[1] = t1;
  56. }
  57. void
  58. grub_ia64_add_value_to_slot_20b (grub_addr_t addr, grub_uint32_t value)
  59. {
  60. grub_uint32_t val;
  61. switch (addr & 3)
  62. {
  63. case 0:
  64. val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
  65. (addr & MASK3) + 2)));
  66. val = (((((val & MASK20) + value) & MASK20) << 2)
  67. | (val & ~(MASK20 << 2)));
  68. grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 2),
  69. grub_cpu_to_le32 (val));
  70. break;
  71. case 1:
  72. val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
  73. (addr & MASK3) + 7)));
  74. val = ((((((val >> 3) & MASK20) + value) & MASK20) << 3)
  75. | (val & ~(MASK20 << 3)));
  76. grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 7),
  77. grub_cpu_to_le32 (val));
  78. break;
  79. case 2:
  80. val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
  81. (addr & MASK3) + 12)));
  82. val = ((((((val >> 4) & MASK20) + value) & MASK20) << 4)
  83. | (val & ~(MASK20 << 4)));
  84. grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 12),
  85. grub_cpu_to_le32 (val));
  86. break;
  87. }
  88. }
  89. #define MASKF21 ( ((1 << 23) - 1) & ~((1 << 7) | (1 << 8)) )
  90. static grub_uint32_t
  91. add_value_to_slot_21_real (grub_uint32_t a, grub_uint32_t value)
  92. {
  93. grub_uint32_t high, mid, low, c;
  94. low = (a & 0x00007f);
  95. mid = (a & 0x7fc000) >> 7;
  96. high = (a & 0x003e00) << 7;
  97. c = (low | mid | high) + value;
  98. return (c & 0x7f) | ((c << 7) & 0x7fc000) | ((c >> 7) & 0x0003e00); //0x003e00
  99. }
  100. void
  101. grub_ia64_add_value_to_slot_21 (grub_addr_t addr, grub_uint32_t value)
  102. {
  103. grub_uint32_t val;
  104. switch (addr & 3)
  105. {
  106. case 0:
  107. val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
  108. (addr & MASK3) + 2)));
  109. val = ((add_value_to_slot_21_real (((val >> 2) & MASKF21), value)
  110. & MASKF21) << 2) | (val & ~(MASKF21 << 2));
  111. grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 2),
  112. grub_cpu_to_le32 (val));
  113. break;
  114. case 1:
  115. val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
  116. (addr & MASK3) + 7)));
  117. val = ((add_value_to_slot_21_real (((val >> 3) & MASKF21), value)
  118. & MASKF21) << 3) | (val & ~(MASKF21 << 3));
  119. grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 7),
  120. grub_cpu_to_le32 (val));
  121. break;
  122. case 2:
  123. val = grub_le_to_cpu32 (grub_get_unaligned32 (((grub_uint8_t *)
  124. (addr & MASK3) + 12)));
  125. val = ((add_value_to_slot_21_real (((val >> 4) & MASKF21), value)
  126. & MASKF21) << 4) | (val & ~(MASKF21 << 4));
  127. grub_set_unaligned32 (((grub_uint8_t *) (addr & MASK3) + 12),
  128. grub_cpu_to_le32 (val));
  129. break;
  130. }
  131. }
  132. static const grub_uint8_t nopm[5] =
  133. {
  134. /* [MLX] nop.m 0x0 */
  135. 0x05, 0x00, 0x00, 0x00, 0x01
  136. };
  137. #ifdef GRUB_UTIL
  138. static grub_uint8_t jump[0x20] =
  139. {
  140. /* [MMI] add r15=r15,r1;; */
  141. 0x0b, 0x78, 0x3c, 0x02, 0x00, 0x20,
  142. /* ld8 r16=[r15],8 */
  143. 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0,
  144. /* mov r14=r1;; */
  145. 0x01, 0x08, 0x00, 0x84,
  146. /* [MIB] ld8 r1=[r15] */
  147. 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,
  148. /* mov b6=r16 */
  149. 0x60, 0x80, 0x04, 0x80, 0x03, 0x00,
  150. /* br.few b6;; */
  151. 0x60, 0x00, 0x80, 0x00
  152. };
  153. #else
  154. static const grub_uint8_t jump[0x20] =
  155. {
  156. /* ld8 r16=[r15],8 */
  157. 0x02, 0x80, 0x20, 0x1e, 0x18, 0x14,
  158. /* mov r14=r1;; */
  159. 0xe0, 0x00, 0x04, 0x00, 0x42, 0x00,
  160. /* nop.i 0x0 */
  161. 0x00, 0x00, 0x04, 0x00,
  162. /* ld8 r1=[r15] */
  163. 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10,
  164. /* mov b6=r16 */
  165. 0x60, 0x80, 0x04, 0x80, 0x03, 0x00,
  166. /* br.few b6;; */
  167. 0x60, 0x00, 0x80, 0x00
  168. };
  169. #endif
  170. void
  171. grub_ia64_make_trampoline (struct grub_ia64_trampoline *tr, grub_uint64_t addr)
  172. {
  173. grub_memcpy (tr->nop, nopm, sizeof (tr->nop));
  174. tr->addr_hi[0] = ((addr & 0xc00000) >> 16);
  175. tr->addr_hi[1] = (addr >> 24) & 0xff;
  176. tr->addr_hi[2] = (addr >> 32) & 0xff;
  177. tr->addr_hi[3] = (addr >> 40) & 0xff;
  178. tr->addr_hi[4] = (addr >> 48) & 0xff;
  179. tr->addr_hi[5] = (addr >> 56) & 0xff;
  180. tr->e0 = 0xe0;
  181. tr->addr_lo[0] = ((addr & 0x000f) << 4) | 0x01;
  182. tr->addr_lo[1] = (((addr & 0x0070) >> 4) | ((addr & 0x070000) >> 11)
  183. | ((addr & 0x200000) >> 17));
  184. tr->addr_lo[2] = ((addr & 0x1f80) >> 5) | ((addr & 0x180000) >> 19);
  185. tr->addr_lo[3] = ((addr & 0xe000) >> 13) | 0x60;
  186. grub_memcpy (tr->jump, jump, sizeof (tr->jump));
  187. }
  188. grub_err_t
  189. grub_ia64_dl_get_tramp_got_size (const void *ehdr, grub_size_t *tramp,
  190. grub_size_t *got)
  191. {
  192. const Elf64_Ehdr *e = ehdr;
  193. grub_size_t cntt = 0, cntg = 0;
  194. const Elf64_Shdr *s;
  195. unsigned i;
  196. for (i = 0, s = (Elf64_Shdr *) ((char *) e + grub_le_to_cpu64 (e->e_shoff));
  197. i < grub_le_to_cpu16 (e->e_shnum);
  198. i++, s = (Elf64_Shdr *) ((char *) s + grub_le_to_cpu16 (e->e_shentsize)))
  199. if (s->sh_type == grub_cpu_to_le32_compile_time (SHT_RELA))
  200. {
  201. const Elf64_Rela *rel, *max;
  202. for (rel = (Elf64_Rela *) ((char *) e + grub_le_to_cpu64 (s->sh_offset)),
  203. max = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_size));
  204. rel < max; rel = (const Elf64_Rela *) ((char *) rel + grub_le_to_cpu64 (s->sh_entsize)))
  205. switch (ELF64_R_TYPE (grub_le_to_cpu64 (rel->r_info)))
  206. {
  207. case R_IA64_PCREL21B:
  208. cntt++;
  209. break;
  210. case R_IA64_LTOFF_FPTR22:
  211. case R_IA64_LTOFF22X:
  212. case R_IA64_LTOFF22:
  213. cntg++;
  214. break;
  215. }
  216. }
  217. *tramp = cntt * sizeof (struct grub_ia64_trampoline);
  218. *got = cntg * sizeof (grub_uint64_t);
  219. return GRUB_ERR_NONE;
  220. }