1
0

bios.S 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # The linker script specifies _start as the entry point to the kernel and the
  18. # bootloader will jump to this position once the kernel has been loaded. It
  19. # doesn't make sense to return from this function as the bootloader is gone.
  20. .section .text
  21. .global _start
  22. .type _start, @function
  23. _start:
  24. # Welcome to kernel mode! We now have sufficient code for the bootloader to
  25. # load and run our operating system. It doesn't do anything interesting yet.
  26. # Perhaps we would like to call printf("Hello, World\n"). You should now
  27. # realize one of the profound truths about kernel mode: There is nothing
  28. # there unless you provide it yourself. There is no printf function. There
  29. # is no <stdio.h> header. If you want a function, you will have to code it
  30. # yourself. And that is one of the best things about kernel development:
  31. # you get to make the entire system yourself. You have absolute and complete
  32. # power over the machine, there are no security restrictions, no safe
  33. # guards, no debugging mechanisms, there is nothing but what you build.
  34. # By now, you are perhaps tired of assembly language. You realize some
  35. # things simply cannot be done in C, such as making the multiboot header in
  36. # the right section and setting up the stack. However, you would like to
  37. # write the operating system in a higher level language, such as C or C++.
  38. # To that end, the next task is preparing the processor for execution of
  39. # such code. C doesn't expect much at this point and we only need to set up
  40. # a stack. Note that the processor is not fully initialized yet and stuff
  41. # such as floating point instructions are not available yet.
  42. # To set up a stack, we simply set the esp register to point to the top of
  43. # our stack (as it grows downwards).
  44. movl $0x200000, %esp
  45. movw $0xf41, %si
  46. movw %si, (0xb8000)
  47. movw %si, (0xb8002)
  48. movw %si, (0xb8004)
  49. movw %si, (0xb8006)
  50. movw %si, (0xb8008)
  51. # We are now ready to actually execute C code. We cannot embed that in an
  52. # assembly file, so we'll create a kernel.c file in a moment. In that file,
  53. # we'll create a C entry point called kernel_main and call it here.
  54. call kernel_main
  55. # In case the function returns, we'll want to put the computer into an
  56. # infinite loop. To do that, we use the clear interrupt ('cli') instruction
  57. # to disable interrupts, the halt instruction ('hlt') to stop the CPU until
  58. # the next interrupt arrives, and jumping to the halt instruction if it ever
  59. # continues execution, just to be safe. We will create a local label rather
  60. # than real symbol and jump to there endlessly.
  61. cli
  62. hlt
  63. .Lhang:
  64. jmp .Lhang
  65. # Set the size of the _start symbol to the current location '.' minus its start.
  66. # This is useful when debugging or when you implement call tracing.
  67. .size _start, . - _start