head.S 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 2021 Rain */
  2. /* This file is part of XNIX. */
  3. /*
  4. XNIX 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. 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. /*
  16. You should have received a copy of the GNU General Public License
  17. along with XNIX. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. .text
  20. .align 8
  21. .globl _start
  22. _start:
  23. /* we cannot use far-jmp in long mode. */
  24. /*
  25. * 'call' will push address of next code,
  26. * and 'ret' will pop it to %rip
  27. * so we can push address, 'ret' will
  28. * pop it and jump to it.
  29. */
  30. /* 64 bit code */
  31. /* return address */
  32. leaq init_done(%rip), %rax
  33. pushq %rax
  34. /* address will jump */
  35. leaq init(%rip), %rax
  36. pushq %rax
  37. retq
  38. init_done:
  39. /* andq %rax, %rax, but %rax won't change. */
  40. testq %rax, %rax
  41. jnz init_panic
  42. init_die:
  43. hlt
  44. jmp init_die
  45. init_panic:
  46. leaq panic_msg(%rip), %rdi
  47. callq print
  48. jmp init_die
  49. .globl print
  50. print:
  51. /* C stores the first argument in %rdi */
  52. movq %rdi, %rsi
  53. movq framebuffer(%rip), %rdi
  54. cld
  55. print_loop:
  56. lodsb
  57. testb %al, %al
  58. jz print_ret
  59. movb $0x07, %ah
  60. stosw
  61. jmp print_loop
  62. print_ret:
  63. /* update frame buffer */
  64. movq %rdi, framebuffer(%rip)
  65. ret
  66. .section .rodata
  67. .align 8
  68. message:
  69. .asciz "XNIX Kernel is starting..."
  70. panic_msg:
  71. .asciz "panic in init: halted"
  72. .data
  73. .globl framebuffer
  74. framebuffer:
  75. .quad 0x00000000000b8000