relocator.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/mm.h>
  19. #include <grub/misc.h>
  20. #include <grub/types.h>
  21. #include <grub/types.h>
  22. #include <grub/err.h>
  23. #include <grub/cache.h>
  24. #include <grub/mips/relocator.h>
  25. #include <grub/relocator_private.h>
  26. /* Do we need mips64? */
  27. extern grub_uint8_t grub_relocator_forward_start;
  28. extern grub_uint8_t grub_relocator_forward_end;
  29. extern grub_uint8_t grub_relocator_backward_start;
  30. extern grub_uint8_t grub_relocator_backward_end;
  31. #define REGW_SIZEOF (2 * sizeof (grub_uint32_t))
  32. #define JUMP_SIZEOF (2 * sizeof (grub_uint32_t))
  33. #define RELOCATOR_SRC_SIZEOF(x) (&grub_relocator_##x##_end \
  34. - &grub_relocator_##x##_start)
  35. #define RELOCATOR_SIZEOF(x) (RELOCATOR_SRC_SIZEOF(x) \
  36. + REGW_SIZEOF * 3)
  37. grub_size_t grub_relocator_align = sizeof (grub_uint32_t);
  38. grub_size_t grub_relocator_forward_size;
  39. grub_size_t grub_relocator_backward_size;
  40. grub_size_t grub_relocator_jumper_size = JUMP_SIZEOF + REGW_SIZEOF;
  41. void
  42. grub_cpu_relocator_init (void)
  43. {
  44. grub_relocator_forward_size = RELOCATOR_SIZEOF(forward);
  45. grub_relocator_backward_size = RELOCATOR_SIZEOF(backward);
  46. }
  47. static void
  48. write_reg (int regn, grub_uint32_t val, void **target)
  49. {
  50. /* lui $r, (val+0x8000). */
  51. *(grub_uint32_t *) *target = ((0x3c00 | regn) << 16) | ((val + 0x8000) >> 16);
  52. *target = ((grub_uint32_t *) *target) + 1;
  53. /* addiu $r, $r, val. */
  54. *(grub_uint32_t *) *target = (((0x2400 | regn << 5 | regn) << 16)
  55. | (val & 0xffff));
  56. *target = ((grub_uint32_t *) *target) + 1;
  57. }
  58. static void
  59. write_jump (int regn, void **target)
  60. {
  61. /* j $r. */
  62. *(grub_uint32_t *) *target = (regn<<21) | 0x8;
  63. *target = ((grub_uint32_t *) *target) + 1;
  64. /* nop. */
  65. *(grub_uint32_t *) *target = 0;
  66. *target = ((grub_uint32_t *) *target) + 1;
  67. }
  68. void
  69. grub_cpu_relocator_jumper (void *rels, grub_addr_t addr)
  70. {
  71. write_reg (1, addr, &rels);
  72. write_jump (1, &rels);
  73. }
  74. void
  75. grub_cpu_relocator_backward (void *ptr0, void *src, void *dest,
  76. grub_size_t size)
  77. {
  78. void *ptr = ptr0;
  79. write_reg (8, (grub_uint32_t) src, &ptr);
  80. write_reg (9, (grub_uint32_t) dest, &ptr);
  81. write_reg (10, (grub_uint32_t) size, &ptr);
  82. grub_memcpy (ptr, &grub_relocator_backward_start,
  83. RELOCATOR_SRC_SIZEOF (backward));
  84. }
  85. void
  86. grub_cpu_relocator_forward (void *ptr0, void *src, void *dest,
  87. grub_size_t size)
  88. {
  89. void *ptr = ptr0;
  90. write_reg (8, (grub_uint32_t) src, &ptr);
  91. write_reg (9, (grub_uint32_t) dest, &ptr);
  92. write_reg (10, (grub_uint32_t) size, &ptr);
  93. grub_memcpy (ptr, &grub_relocator_forward_start,
  94. RELOCATOR_SRC_SIZEOF (forward));
  95. }
  96. grub_err_t
  97. grub_relocator32_boot (struct grub_relocator *rel,
  98. struct grub_relocator32_state state)
  99. {
  100. grub_relocator_chunk_t ch;
  101. void *ptr;
  102. grub_err_t err;
  103. void *relst;
  104. grub_size_t relsize;
  105. grub_size_t stateset_size = 31 * REGW_SIZEOF + JUMP_SIZEOF;
  106. unsigned i;
  107. grub_addr_t vtarget;
  108. err = grub_relocator_alloc_chunk_align (rel, &ch, 0, UP_TO_TOP32 (stateset_size),
  109. stateset_size, sizeof (grub_uint32_t),
  110. GRUB_RELOCATOR_PREFERENCE_NONE, 0);
  111. if (err)
  112. return err;
  113. ptr = get_virtual_current_address (ch);
  114. for (i = 1; i < 32; i++)
  115. write_reg (i, state.gpr[i], &ptr);
  116. write_jump (state.jumpreg, &ptr);
  117. vtarget = (grub_addr_t) grub_map_memory (get_physical_target_address (ch),
  118. stateset_size);
  119. err = grub_relocator_prepare_relocs (rel, vtarget, &relst, &relsize);
  120. if (err)
  121. return err;
  122. grub_arch_sync_caches ((void *) relst, relsize);
  123. ((void (*) (void)) relst) ();
  124. /* Not reached. */
  125. return GRUB_ERR_NONE;
  126. }