setup64.S 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* arch/x86/boot/setup64.S
  2. *
  3. * This file is part of xnix.
  4. *
  5. * XNIX 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. * XNIX 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 XNIX. If not, see <https://www.gnu.org/licenses/>
  17. */
  18. .code16
  19. /* switch to the long-mode, and to real system */
  20. .globl _start
  21. _start: xorw %ax, %ax
  22. movw %ax, %ds
  23. movw %ax, %es
  24. movw %ax, %ss
  25. /* yeah, we should move it again. */
  26. movw $0x7e00, %di
  27. movw $0x8000, %si
  28. movw $512, %cx
  29. cld
  30. rep
  31. movsb
  32. movw $0x7c00, %sp
  33. ljmp $0, $real_start
  34. real_start:
  35. /* open a20 */
  36. cli
  37. inb $0x92, %al
  38. orb $0x02, %al
  39. outb %al, $0x92
  40. /* setup gdt */
  41. lgdt gdt32_descr
  42. movl %cr0, %eax
  43. orl $1, %eax
  44. /* that's it! */
  45. movl %eax, %cr0
  46. /* setup paging (for long mode) */
  47. movl $0, %edi
  48. movl $512, %ecx
  49. xorl %eax, %eax
  50. cld
  51. rsetpt: rep
  52. stosl
  53. movl $0x00001003, 0
  54. movl $0x00001003, 0x800
  55. movl $0x00002003, 0x1000
  56. movl $0x00003003, 0x2000
  57. movl $0x3000, %edi
  58. movl $0x00000083, %eax
  59. rsetp: movl %eax, (%edi)
  60. movl $0, 4(%edi)
  61. addl $8, %edi
  62. addl $4096, %eax
  63. cmpl $0x800000, %eax
  64. jbe rsetp
  65. ljmp $0x08, $init_prot
  66. .code32
  67. init_prot:
  68. movl $0x10, %eax
  69. movl %eax, %ds
  70. movl %eax, %es
  71. movl %eax, %fs
  72. movl %eax, %gs
  73. movl %eax, %ss
  74. movl $0x7c00, %esp
  75. /* move it to 1mb */
  76. movl $1048576, %edi
  77. movl $0x8200, %esi
  78. movl $131072, %ecx
  79. cld
  80. rep
  81. movsl
  82. to_long:
  83. lgdt gdt64_descr
  84. movl $0x000000a0, %eax
  85. movl %eax, %cr4
  86. xorl %eax, %eax
  87. movl %eax, %cr3
  88. movl $0xc0000080, %ecx
  89. rdmsr
  90. orl $0x100, %eax
  91. wrmsr
  92. /* PG and PE */
  93. movl %cr0, %eax
  94. orl $0x80000001, %eax
  95. movl %eax, %cr0
  96. ljmp $0x08, $init_lm
  97. .code64
  98. init_lm:
  99. movq $0x10, %rax
  100. movq %rax, %ds
  101. movq %rax, %es
  102. movq %rax, %fs
  103. movq %rax, %gs
  104. movq %rax, %ss
  105. movq $0x7c00, %rsp
  106. pushq $1048576
  107. retq
  108. .globl gdt32
  109. gdt32: .long 0x00000000, 0x00000000
  110. .long 0x0000ffff, 0x00cf9a00
  111. .long 0x0000ffff, 0x00cf9200
  112. .globl gdt32_descr
  113. gdt32_descr:
  114. .word . - gdt32
  115. .long gdt32
  116. gdt64: .quad 0x0000000000000000
  117. .quad 0x0020980000000000
  118. .quad 0x0000920000000000
  119. gdt64_descr:
  120. .word . - gdt64
  121. .long gdt64