boot.S 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Declare constants used for creating a multiboot header.
  2. .set ALIGN, 1<<0 # align loaded modules on page boundaries
  3. .set MEMINFO, 1<<1 # provide memory map
  4. .set FLAGS, ALIGN | MEMINFO # this is the Multiboot 'flag' field
  5. .set MAGIC, 0x1BADB002 # 'magic number' lets bootloader find the header
  6. .set CHECKSUM, -(MAGIC + FLAGS) # checksum of above, to prove we are multiboot
  7. # Declare a header as in the Multiboot Standard. We put this into a special
  8. # section so we can force the header to be in the start of the final program.
  9. # You don't need to understand all these details as it is just magic values that
  10. # is documented in the multiboot standard. The bootloader will search for this
  11. # magic sequence and recognize us as a multiboot kernel.
  12. .section .multiboot
  13. .align 4
  14. .long MAGIC
  15. .long FLAGS
  16. .long CHECKSUM
  17. # Currently the stack pointer register (esp) points at anything and using it may
  18. # cause massive harm. Instead, we'll provide our own stack. We will allocate
  19. # room for a small temporary stack by creating a symbol at the bottom of it,
  20. # then allocating 16384 bytes for it, and finally creating a symbol at the top.
  21. .section .bootstrap_stack, "aw", @nobits
  22. stack_bottom:
  23. .skip 16384 # 16 KiB
  24. stack_top:
  25. # The linker script specifies _start as the entry point to the kernel and the
  26. # bootloader will jump to this position once the kernel has been loaded. It
  27. # doesn't make sense to return from this function as the bootloader is gone.
  28. .section .text
  29. .global _start
  30. .type _start, @function
  31. _start:
  32. # Welcome to kernel mode! We now have sufficient code for the bootloader to
  33. # load and run our operating system. It doesn't do anything interesting yet.
  34. # Perhaps we would like to call printf("Hello, World\n"). You should now
  35. # realize one of the profound truths about kernel mode: There is nothing
  36. # there unless you provide it yourself. There is no printf function. There
  37. # is no <stdio.h> header. If you want a function, you will have to code it
  38. # yourself. And that is one of the best things about kernel development:
  39. # you get to make the entire system yourself. You have absolute and complete
  40. # power over the machine, there are no security restrictions, no safe
  41. # guards, no debugging mechanisms, there is nothing but what you build.
  42. # By now, you are perhaps tired of assembly language. You realize some
  43. # things simply cannot be done in C, such as making the multiboot header in
  44. # the right section and setting up the stack. However, you would like to
  45. # write the operating system in a higher level language, such as C or C++.
  46. # To that end, the next task is preparing the processor for execution of
  47. # such code. C doesn't expect much at this point and we only need to set up
  48. # a stack. Note that the processor is not fully initialized yet and stuff
  49. # such as floating point instructions are not available yet.
  50. # To set up a stack, we simply set the esp register to point to the top of
  51. # our stack (as it grows downwards).
  52. movl $stack_top, %esp
  53. # We are now ready to actually execute C code. We cannot embed that in an
  54. # assembly file, so we'll create a kernel.c file in a moment. In that file,
  55. # we'll create a C entry point called kernel_main and call it here.
  56. call kernel_main
  57. # In case the function returns, we'll want to put the computer into an
  58. # infinite loop. To do that, we use the clear interrupt ('cli') instruction
  59. # to disable interrupts, the halt instruction ('hlt') to stop the CPU until
  60. # the next interrupt arrives, and jumping to the halt instruction if it ever
  61. # continues execution, just to be safe. We will create a local label rather
  62. # than real symbol and jump to there endlessly.
  63. cli
  64. hlt
  65. .Lhang:
  66. jmp .Lhang
  67. # Set the size of the _start symbol to the current location '.' minus its start.
  68. # This is useful when debugging or when you implement call tracing.
  69. .size _start, . - _start