relocator_common.S 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009,2010 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/symbol.h>
  19. #include <grub/i386/memory.h>
  20. #ifdef __x86_64__
  21. #define RAX %rax
  22. #define RSI %rsi
  23. #else
  24. #define RAX %eax
  25. #define RSI %esi
  26. #endif
  27. .macro DISABLE_PAGING
  28. movl %cr0, %eax
  29. andl $(~GRUB_MEMORY_CPU_CR0_PAGING_ON), %eax
  30. movl %eax, %cr0
  31. .endm
  32. .macro PREAMBLE
  33. LOCAL(base):
  34. /* %rax contains now our new 'base'. */
  35. mov RAX, RSI
  36. #if defined (__APPLE__) && defined (__x86_64__)
  37. leaq LOCAL(cont0) (%rip), RAX
  38. #elif defined (__APPLE__)
  39. LOCAL(cont0_offset) = LOCAL(cont0) - LOCAL(base)
  40. add $LOCAL(cont0_offset), RAX
  41. #else
  42. add $(LOCAL(cont0) - LOCAL(base)), RAX
  43. #endif
  44. jmp *RAX
  45. LOCAL(cont0):
  46. .endm
  47. .macro RELOAD_GDT
  48. #ifdef __APPLE__
  49. LOCAL(cont1_offset) = LOCAL(cont1) - LOCAL(base)
  50. LOCAL(jump_vector_offset) = LOCAL(jump_vector) - LOCAL(base)
  51. LOCAL(gdt_offset) = LOCAL(gdt) - LOCAL(base)
  52. LOCAL(gdt_addr_offset) = LOCAL(gdt_addr) - LOCAL(base)
  53. LOCAL(gdtdesc_offset) = LOCAL(gdtdesc) - LOCAL(base)
  54. lea LOCAL(cont1_offset) (RSI, 1), RAX
  55. movl %eax, LOCAL(jump_vector_offset) (RSI, 1)
  56. lea LOCAL(gdt_offset) (RSI, 1), RAX
  57. mov RAX, (LOCAL(gdt_addr_offset)) (RSI, 1)
  58. /* Switch to compatibility mode. */
  59. lgdt (LOCAL(gdtdesc_offset)) (RSI, 1)
  60. /* Update %cs. */
  61. ljmp *(LOCAL(jump_vector_offset)) (RSI, 1)
  62. .p2align 4
  63. LOCAL(gdtdesc):
  64. LOCAL(gdtsize) = LOCAL(gdt_end) - LOCAL(gdt)
  65. .word LOCAL(gdtsize)
  66. #else
  67. lea (LOCAL(cont1) - LOCAL(base)) (RSI, 1), RAX
  68. movl %eax, (LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
  69. lea (LOCAL(gdt) - LOCAL(base)) (RSI, 1), RAX
  70. mov RAX, (LOCAL(gdt_addr) - LOCAL(base)) (RSI, 1)
  71. /* Switch to compatibility mode. */
  72. lgdt (LOCAL(gdtdesc) - LOCAL(base)) (RSI, 1)
  73. /* Update %cs. */
  74. ljmp *(LOCAL(jump_vector) - LOCAL(base)) (RSI, 1)
  75. .p2align 4
  76. LOCAL(gdtdesc):
  77. .word LOCAL(gdt_end) - LOCAL(gdt)
  78. #endif
  79. LOCAL(gdt_addr):
  80. #ifdef __x86_64__
  81. /* Filled by the code. */
  82. .quad 0
  83. #else
  84. /* Filled by the code. */
  85. .long 0
  86. #endif
  87. .p2align 4
  88. LOCAL(jump_vector):
  89. /* Jump location. Is filled by the code */
  90. .long 0
  91. .long CODE_SEGMENT
  92. LOCAL(cont1):
  93. .endm