head.S 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. /* virtual base address */
  32. movq $0xffff800000000000, %rbp
  33. /* return address */
  34. leaq init_done(%rip), %rax
  35. addq %rbp, %rax
  36. pushq %rax
  37. /* address will jump */
  38. leaq init(%rip), %rax
  39. addq %rbp, %rax
  40. pushq %rax
  41. retq
  42. init_done:
  43. /* andq %rax, %rax, but %rax won't change. */
  44. testq %rax, %rax
  45. jnz init_panic
  46. init_die:
  47. hlt
  48. jmp init_die
  49. init_panic:
  50. leaq panic_msg(%rip), %rdi
  51. callq printf
  52. jmp init_die
  53. .section .rodata
  54. .align 8
  55. message:
  56. .asciz "XNIX Kernel is starting..."
  57. panic_msg:
  58. .asciz "panic in init: halted"