setup32.S 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* arch/x86/boot/setup32.S
  2. *
  3. * Copyright (C) 2021, 2022 intirain.
  4. *
  5. * This file is part of evx.
  6. *
  7. * evx is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * evx is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with evx. If not, see <https://www.gnu.org/licenses/>
  19. */
  20. .code16
  21. /* switch to the protected-mode, and to real system */
  22. .globl _start
  23. _start: xorw %ax, %ax
  24. movw %ax, %ds
  25. movw %ax, %es
  26. movw %ax, %ss
  27. /* yeah, we should move it again. */
  28. movw $0x7e00, %di
  29. movw $0x8000, %si
  30. movw $512, %cx
  31. cld
  32. rep
  33. movsb
  34. movw $0x7c00, %sp
  35. ljmp $0, $real_start
  36. real_start:
  37. /* open a20 */
  38. cli
  39. inb $0x92, %al
  40. orb $0x02, %al
  41. outb %al, $0x92
  42. /* setup idt and gdt */
  43. lgdt gdt32_descr
  44. movl %cr0, %eax
  45. orl $1, %eax
  46. /* that's it! */
  47. movl %eax, %cr0
  48. /* setup paging */
  49. movl $0, %edi
  50. movl $512, %ecx
  51. xorl %eax, %eax
  52. cld
  53. rsetpt: rep
  54. stosl
  55. movl $0x00001007, 0
  56. movl $0x00001007, 0xc00
  57. movl $0x1000, %edi
  58. movl $1024, %ecx
  59. movl $0x00000007, %eax
  60. rsetp: stosl
  61. addl $4096, %eax
  62. loop rsetp
  63. xorl %eax, %eax
  64. movl %eax, %cr3
  65. movl %cr0, %eax
  66. orl $0x80000000, %eax
  67. movl %eax, %cr0
  68. ljmp $0x08, $init_prot
  69. .code32
  70. init_prot:
  71. movl $0x10, %eax
  72. movl %eax, %ds
  73. movl %eax, %es
  74. movl %eax, %fs
  75. movl %eax, %gs
  76. movl %eax, %ss
  77. movl $0x7c00, %esp
  78. /* move it to 1mb */
  79. movl $1048576, %edi
  80. movl $0x8200, %esi
  81. movl $131072, %ecx
  82. cld
  83. rep
  84. movsl
  85. ljmp $0x08, $1048576
  86. .globl gdt32
  87. gdt32: .long 0x00000000, 0x00000000
  88. .long 0x0000ffff, 0x00cf9a00
  89. .long 0x0000ffff, 0x00cf9200
  90. .globl gdt32_descr
  91. gdt32_descr:
  92. .word . - gdt32
  93. .long gdt32