relocator.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/err.h>
  22. #include <grub/term.h>
  23. #include <grub/xen.h>
  24. #include <grub/xen/relocator.h>
  25. #include <grub/relocator_private.h>
  26. typedef grub_addr_t grub_xen_reg_t;
  27. struct grub_relocator_xen_paging_area {
  28. grub_xen_reg_t start;
  29. grub_xen_reg_t size;
  30. } GRUB_PACKED;
  31. extern grub_uint8_t grub_relocator_xen_start;
  32. extern grub_uint8_t grub_relocator_xen_end;
  33. extern grub_uint8_t grub_relocator_xen_remap_start;
  34. extern grub_uint8_t grub_relocator_xen_remap_end;
  35. extern grub_xen_reg_t grub_relocator_xen_stack;
  36. extern grub_xen_reg_t grub_relocator_xen_start_info;
  37. extern grub_xen_reg_t grub_relocator_xen_entry_point;
  38. extern grub_xen_reg_t grub_relocator_xen_remapper_virt;
  39. extern grub_xen_reg_t grub_relocator_xen_remapper_virt2;
  40. extern grub_xen_reg_t grub_relocator_xen_remapper_map;
  41. extern grub_xen_reg_t grub_relocator_xen_mfn_list;
  42. extern struct grub_relocator_xen_paging_area
  43. grub_relocator_xen_paging_areas[XEN_MAX_MAPPINGS];
  44. extern grub_xen_reg_t grub_relocator_xen_remap_continue;
  45. #ifdef __i386__
  46. extern grub_xen_reg_t grub_relocator_xen_mmu_op_addr;
  47. extern grub_xen_reg_t grub_relocator_xen_paging_areas_addr;
  48. extern grub_xen_reg_t grub_relocator_xen_remapper_map_high;
  49. #endif
  50. extern mmuext_op_t grub_relocator_xen_mmu_op[3];
  51. #define RELOCATOR_SIZEOF(x) (&grub_relocator##x##_end - &grub_relocator##x##_start)
  52. grub_err_t
  53. grub_relocator_xen_boot (struct grub_relocator *rel,
  54. struct grub_relocator_xen_state state,
  55. grub_uint64_t remapper_pfn,
  56. grub_addr_t remapper_virt,
  57. grub_uint64_t trampoline_pfn,
  58. grub_addr_t trampoline_virt)
  59. {
  60. grub_err_t err;
  61. void *relst;
  62. int i;
  63. grub_relocator_chunk_t ch, ch_tramp;
  64. grub_xen_mfn_t *mfn_list =
  65. (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
  66. err = grub_relocator_alloc_chunk_addr (rel, &ch, remapper_pfn << 12,
  67. RELOCATOR_SIZEOF (_xen_remap));
  68. if (err)
  69. return err;
  70. err = grub_relocator_alloc_chunk_addr (rel, &ch_tramp, trampoline_pfn << 12,
  71. RELOCATOR_SIZEOF (_xen));
  72. if (err)
  73. return err;
  74. grub_relocator_xen_stack = state.stack;
  75. grub_relocator_xen_start_info = state.start_info;
  76. grub_relocator_xen_entry_point = state.entry_point;
  77. for (i = 0; i < XEN_MAX_MAPPINGS; i++)
  78. {
  79. grub_relocator_xen_paging_areas[i].start = state.paging_start[i];
  80. grub_relocator_xen_paging_areas[i].size = state.paging_size[i];
  81. }
  82. grub_relocator_xen_remapper_virt = remapper_virt;
  83. grub_relocator_xen_remapper_virt2 = remapper_virt;
  84. grub_relocator_xen_remap_continue = trampoline_virt;
  85. grub_relocator_xen_remapper_map = (mfn_list[remapper_pfn] << 12) | 5;
  86. #ifdef __i386__
  87. grub_relocator_xen_remapper_map_high = (mfn_list[remapper_pfn] >> 20);
  88. grub_relocator_xen_mmu_op_addr = (char *) &grub_relocator_xen_mmu_op
  89. - (char *) &grub_relocator_xen_remap_start + remapper_virt;
  90. grub_relocator_xen_paging_areas_addr =
  91. (char *) &grub_relocator_xen_paging_areas
  92. - (char *) &grub_relocator_xen_remap_start + remapper_virt;
  93. #endif
  94. grub_relocator_xen_mfn_list = state.mfn_list;
  95. grub_memset (grub_relocator_xen_mmu_op, 0,
  96. sizeof (grub_relocator_xen_mmu_op));
  97. #ifdef __i386__
  98. grub_relocator_xen_mmu_op[0].cmd = MMUEXT_PIN_L3_TABLE;
  99. #else
  100. grub_relocator_xen_mmu_op[0].cmd = MMUEXT_PIN_L4_TABLE;
  101. #endif
  102. grub_relocator_xen_mmu_op[0].arg1.mfn = mfn_list[state.paging_start[0]];
  103. grub_relocator_xen_mmu_op[1].cmd = MMUEXT_NEW_BASEPTR;
  104. grub_relocator_xen_mmu_op[1].arg1.mfn = mfn_list[state.paging_start[0]];
  105. grub_relocator_xen_mmu_op[2].cmd = MMUEXT_UNPIN_TABLE;
  106. grub_relocator_xen_mmu_op[2].arg1.mfn =
  107. mfn_list[grub_xen_start_page_addr->pt_base >> 12];
  108. grub_memmove (get_virtual_current_address (ch),
  109. &grub_relocator_xen_remap_start,
  110. RELOCATOR_SIZEOF (_xen_remap));
  111. grub_memmove (get_virtual_current_address (ch_tramp),
  112. &grub_relocator_xen_start, RELOCATOR_SIZEOF (_xen));
  113. err = grub_relocator_prepare_relocs (rel, get_physical_target_address (ch),
  114. &relst, NULL);
  115. if (err)
  116. return err;
  117. ((void (*)(void)) relst) ();
  118. /* Not reached. */
  119. return GRUB_ERR_NONE;
  120. }