head32.S 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* arch/x86/init/head32.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. .code32
  21. .globl _start
  22. _start:
  23. /* we are now in the protected-mode, just
  24. * do some initializes, and call main */
  25. lgdt gdt_descr
  26. call init_idt
  27. movl $0xc0000000, %eax
  28. leal 1f, %ecx
  29. orl %ecx, %eax
  30. pushl %eax
  31. movl $0xc0000000, %eax
  32. leal init, %ecx
  33. orl %ecx, %eax
  34. pushl %eax
  35. retl
  36. 1: hlt
  37. jmp 1b
  38. init_idt:
  39. /* edx: high 32 eax: low 32 */
  40. leal default_int_handler, %edx
  41. /* KERNEL_CS */
  42. movl %edx, %eax
  43. andl $0x0000ffff, %eax
  44. addl $(8 << 16), %eax
  45. andl $0xffff0000, %edx
  46. addl $0x8e00, %edx
  47. movl $256, %ecx
  48. leal idt, %edi
  49. rsidt: movl %eax, (%edi)
  50. movl %edx, 4(%edi)
  51. addl $8, %edi
  52. decl %ecx
  53. jnz rsidt
  54. lidt idt_descr
  55. ret
  56. default_int_handler:
  57. incl 0xb8000
  58. iretl
  59. .globl ret_intr
  60. ret_intr:
  61. popl %eax
  62. movl %eax, %gs
  63. popl %eax
  64. movl %eax, %fs
  65. popl %eax
  66. movl %eax, %es
  67. popl %eax
  68. movl %eax, %ds
  69. popl %edi
  70. popl %esi
  71. popl %ebp
  72. popl %edx
  73. popl %ebx
  74. popl %ecx
  75. popl %eax
  76. addl $4, %esp
  77. iretl
  78. .globl __stack_chk_fail, __stack_chk_fail_local
  79. .globl die
  80. __stack_chk_fail_local:
  81. __stack_chk_fail:
  82. die:
  83. hlt
  84. jmp __stack_chk_fail
  85. .data
  86. .align 16
  87. .globl gdt
  88. gdt: .long 0x00000000, 0x00000000
  89. .long 0x0000ffff, 0x00cf9a00
  90. .long 0x0000ffff, 0x00cf9200
  91. .globl gdt_descr
  92. gdt_descr:
  93. .word . - gdt
  94. .long gdt
  95. .align 16
  96. .globl idt
  97. idt: .fill 256, 8, 0
  98. .globl idt_descr
  99. idt_descr:
  100. .word . - idt
  101. .long idt