linux_trampoline.S 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/symbol.h>
  19. .p2align 4 /* force 16-byte alignment */
  20. VARIABLE(grub_linux_trampoline_start)
  21. cli
  22. /* %rdi contains protected memory start and %rsi
  23. contains real memory start. */
  24. mov %rsi, %rbx
  25. call base
  26. base:
  27. pop %rsi
  28. #ifdef APPLE_CC
  29. lea (cont1 - base) (%esi, 1), %rax
  30. mov %eax, (jump_vector - base) (%esi, 1)
  31. lea (gdt - base) (%esi, 1), %rax
  32. mov %rax, (gdtaddr - base) (%esi, 1)
  33. /* Switch to compatibility mode. */
  34. lidt (idtdesc - base) (%esi, 1)
  35. lgdt (gdtdesc - base) (%esi, 1)
  36. /* Update %cs. Thanks to David Miller for pointing this mistake out. */
  37. ljmp *(jump_vector - base) (%esi, 1)
  38. #else
  39. lea (cont1 - base) (%rsi, 1), %rax
  40. mov %eax, (jump_vector - base) (%rsi, 1)
  41. lea (gdt - base) (%rsi, 1), %rax
  42. mov %rax, (gdtaddr - base) (%rsi, 1)
  43. /* Switch to compatibility mode. */
  44. lidt (idtdesc - base) (%rsi, 1)
  45. lgdt (gdtdesc - base) (%rsi, 1)
  46. /* Update %cs. Thanks to David Miller for pointing this mistake out. */
  47. ljmp *(jump_vector - base) (%rsi, 1)
  48. #endif
  49. cont1:
  50. .code32
  51. /* Update other registers. */
  52. mov $0x18, %eax
  53. mov %eax, %ds
  54. mov %eax, %es
  55. mov %eax, %fs
  56. mov %eax, %gs
  57. mov %eax, %ss
  58. /* Disable paging. */
  59. mov %cr0, %eax
  60. and $0x7fffffff, %eax
  61. mov %eax, %cr0
  62. /* Disable amd64. */
  63. mov $0xc0000080, %ecx
  64. rdmsr
  65. and $0xfffffeff, %eax
  66. wrmsr
  67. /* Turn off PAE. */
  68. movl %cr4, %eax
  69. and $0xffffffcf, %eax
  70. mov %eax, %cr4
  71. jmp cont2
  72. cont2:
  73. .code32
  74. mov %ebx, %esi
  75. jmp *%edi
  76. /* GDT. */
  77. .p2align 4
  78. gdt:
  79. /* NULL. */
  80. .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  81. /* Reserved. */
  82. .byte 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  83. /* Code segment. */
  84. .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x9A, 0xCF, 0x00
  85. /* Data segment. */
  86. .byte 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x92, 0xCF, 0x00
  87. gdtdesc:
  88. .word 31
  89. gdtaddr:
  90. .quad gdt
  91. idtdesc:
  92. .word 0
  93. idtaddr:
  94. .quad 0
  95. .p2align 4
  96. jump_vector:
  97. /* Jump location. Is filled by the code */
  98. .long 0
  99. .long 0x10
  100. VARIABLE(grub_linux_trampoline_end)