head.S 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of Cunix. */
  3. /*
  4. Cunix is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) and later version.
  8. */
  9. /*
  10. Cunix 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. /*
  16. You should have received a copy of the GNU General Public License
  17. along with Cunix. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #include <arch/x86/paging.h>
  20. .align 8
  21. .text
  22. .globl _start
  23. /*
  24. * We can't use any symbol addresses before we setup PML4-entry.
  25. * Because linker use 0xffff8000 to fill all addresses (see kernel/kernel.lds).
  26. * So, we must setup PML4E first.
  27. */
  28. _start:
  29. movq $PML4_ADDR, %rax
  30. movq $PDPT_ADDR | PATT_USER, 2048(%rax)
  31. movq %rax, %cr3
  32. /* ok, now we can use addresses because we had been set PML4E */
  33. lgdt gdt_desc(%rip)
  34. lidt idt_desc(%rip)
  35. /* first, setup segment descriptor */
  36. leaq switch_segment(%rip), %rax
  37. pushq $0x08
  38. pushq %rax
  39. lretq
  40. switch_segment:
  41. /* segment registers are 16-bits, but use `rax` is faster than `ax` */
  42. movq $0x10, %rax
  43. movq %rax, %ds
  44. movq %rax, %es
  45. movq %rax, %fs
  46. movq %rax, %gs
  47. movq %rax, %ss
  48. movq $0x0000000000007c00, %rsp
  49. callq setup_idt
  50. /* enable interrupt in init() */
  51. leaq init(%rip), %rax
  52. pushq $0x08
  53. pushq %rax
  54. lretq
  55. /* setup paging */
  56. setup_paging:
  57. /* set attributes for 0 - 64MB pages */
  58. /* start address 0 */
  59. xorq %rax, %rax
  60. /* page address (first set in boot/x86/loader.asm) */
  61. movq $PTE_ADDR, %rdi
  62. /* count of entries will set */
  63. movq $64 * 1048576 / PAGE_SIZE, %rcx
  64. cld
  65. rep_spage:
  66. orq $PATT_KERNEL, %rax
  67. /* repeat while rcx > 0, then rcx-- */
  68. stosq
  69. addq $PAGE_SIZE, %rax
  70. loop rep_spage
  71. ret
  72. /* setup IDT entries */
  73. setup_idt:
  74. /* offset address of interrupt handler */
  75. leaq default_int(%rip), %rdx
  76. /* RAX is low 64 bit of IDT-entry */
  77. /* selector: 0x08 */
  78. movq $(0x08 << 16), %rax
  79. movw %dx, %ax
  80. /* 8E means present, DPL = 0, TYPE = E, and IST = 0 */
  81. movq $(0x8e00 << 32), %rcx
  82. addq %rcx, %rax
  83. /* offset low 32 bit to RCX */
  84. movl %edx, %ecx
  85. /* only high 16 bits */
  86. shrl $16, %ecx
  87. shlq $48, %rcx
  88. addq %rcx, %rax
  89. /* only high 32 bits */
  90. shrq $32, %rdx
  91. leaq idt_table(%rip), %rdi
  92. /* entries in IDT */
  93. movq $256, %rcx
  94. rep_sidt:
  95. movq %rax, (%rdi)
  96. movq %rdx, 8(%rdi)
  97. addq $0x10, %rdi
  98. dec %rcx
  99. jnz rep_sidt
  100. retq
  101. default_int:
  102. leaq int_msg(%rip), %rdi
  103. call printf
  104. default_int_die:
  105. hlt
  106. jmp default_int_die
  107. iretq
  108. .section .rodata
  109. .align 8
  110. int_msg: .asciz "unknow interrupt or fault"
  111. .data
  112. .globl gdt_table
  113. gdt_table:
  114. /* first entry must be a null descriptor */
  115. .quad 0x0000000000000000
  116. /* kernel code 64-bits */
  117. .quad 0x0020980000000000
  118. /* kernel data 64-bits */
  119. .quad 0x0000920000000000
  120. /* user code 64-bits */
  121. .quad 0x0020f80000000000
  122. /* user data 64-bits */
  123. .quad 0x0000f20000000000
  124. /* kernel code 32-bits */
  125. .quad 0x00cf9a000000ffff
  126. /* kernel data 32-bits */
  127. .quad 0x00cf92000000ffff
  128. .fill 10, 8, 0
  129. end_gdt:
  130. gdt_desc:
  131. .word end_gdt - gdt_table
  132. .quad gdt_table
  133. .globl idt_table
  134. idt_table:
  135. /* this will takes a warning */
  136. /* .fill 256, 16, 0 */
  137. .fill 512, 8, 0
  138. end_idt:
  139. idt_desc:
  140. .word end_idt - idt_table
  141. .quad idt_table