bios_initial_state.S 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* https://github.com/cirosantilli/x86-bare-metal-examples#bios-initial-state */
  2. #include "common.h"
  3. .macro INITIAL_STORE x
  4. mov %\()\x, \x
  5. .endm
  6. .macro INITIAL_DATA x
  7. \x: .skip 2
  8. \x\()s: .ascii "\x = \0"
  9. .endm
  10. .macro INITIAL_PRINT x
  11. PRINT_STRING $\x\()s
  12. PRINT_BYTES $\x, $2
  13. PRINT_NEWLINE
  14. .endm
  15. /* Indispensable initialization.
  16. * This initialization is a bit redundant with BEGIN,
  17. * and does dirty some registers, but I haven't found a better option.
  18. */
  19. .code16
  20. cli
  21. xor %ax, %ax
  22. mov %ax, %ds
  23. /* We want our data do be before STAGE2,
  24. * or it will get overwritten during the load.
  25. */
  26. jmp after_data
  27. .irp reg, ax, bx, cx, dx, cs, ds, es, fs, gs, ss
  28. INITIAL_DATA \reg
  29. .endr
  30. cr0: .long 0
  31. cr0s: .ascii "cr0 = \0"
  32. after_data:
  33. .irp reg, ax, bx, cx, dx, cs, ds, es, fs, gs, ss
  34. INITIAL_STORE \reg
  35. .endr
  36. /* Does not have a 16-bit mov version. */
  37. mov %cr0, %eax
  38. mov %eax, cr0
  39. /* We delay a full BEGIN as late as possible
  40. * to mess with less initial state.
  41. */
  42. BEGIN
  43. STAGE2
  44. .irp reg, ax, bx, cx, dx, cs, ds, es, fs, gs, ss
  45. INITIAL_PRINT \reg
  46. .endr
  47. PRINT_STRING $cr0s
  48. PRINT_BYTES cr0, $4
  49. PRINT_NEWLINE
  50. hlt