apu_boot.S 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "../common/asm.h"
  2. .balign 0x1000 // 按照4k对齐
  3. .text
  4. .code16
  5. ENTRY(_apu_boot_start)
  6. _apu_boot_base = .
  7. cli
  8. wbinvd // 将处理器缓存同步到内存中
  9. mov %cs, %ax
  10. mov %ax, %ds
  11. mov %ax, %es
  12. mov %ax, %ss
  13. mov %ax, %fs
  14. mov %ax, %gs
  15. // 设置栈指针
  16. movl $(_apu_boot_tmp_stack_end - _apu_boot_base), %esp
  17. // 计算ap处理器引导程序的基地址
  18. mov %cs, %ax
  19. movzx %ax, %esi
  20. shll $4, %esi
  21. // set gdt and 32bit/64bit code addr
  22. leal (_apu_code32 - _apu_boot_base)(%esi), %eax
  23. movl %eax, (_apu_code32_vector - _apu_boot_base)
  24. leal (_apu_code64 - _apu_boot_base)(%esi), %eax
  25. movl %eax, (_apu_code64_vector - _apu_boot_base)
  26. leal (_apu_tmp_gdt - _apu_boot_base)(%esi), %eax
  27. movl %eax, (_apu_tmp_gdt + 2 - _apu_boot_base)
  28. // 从实模式切换到保护模式
  29. lidtl _apu_tmp_idt - _apu_boot_base
  30. lgdtl _apu_tmp_gdt - _apu_boot_base
  31. // 操作cr0控制器,使能保护模式
  32. smsw %ax
  33. bts $0, %ax
  34. lmsw %ax
  35. // 转到保护模式
  36. ljmpl *(_apu_code32_vector - _apu_boot_base)
  37. .code32
  38. .balign 4
  39. _apu_code32:
  40. # 转到长模式
  41. mov $0x10, %ax
  42. mov %ax, %ds
  43. mov %ax, %es
  44. mov %ax, %ss
  45. mov %ax, %fs
  46. mov %ax, %gs
  47. // 设置栈指针
  48. leal (_apu_boot_tmp_stack_end - _apu_boot_base)(%esi), %eax
  49. movl %eax, %esp
  50. // open PAE
  51. movl %cr4, %eax
  52. bts $5, %eax
  53. movl %eax, %cr4
  54. // 设置页表
  55. movl $pml4, %eax // 复用bsp处理器初始化时的32位页表
  56. movl %eax, %cr3
  57. mov $0xC0000080, %ecx
  58. rdmsr
  59. or $(1<<8), %eax
  60. wrmsr
  61. // enable PE and paging 这里有问题
  62. mov %cr0, %eax
  63. or $(1<<31), %eax
  64. mov %eax, %cr0
  65. // 跳转到64位代码
  66. ljmp *(_apu_code64_vector - _apu_boot_base)(%esi)
  67. .code64
  68. .balign 4
  69. _apu_code64:
  70. movq $0x20, %rax
  71. movq %rax, %ds
  72. movq %rax, %es
  73. movq %rax, %ss
  74. movq %rax, %fs
  75. movq %rax, %gs
  76. //now enable SSE and the like
  77. movq %cr0, %rax
  78. and $0xFFFB, %ax //clear coprocessor emulation CR0.EM
  79. or $0x2, %ax //set coprocessor monitoring CR0.MP
  80. movq %rax, %cr0
  81. movq %cr4, %rax
  82. or $(3 << 9), %ax //set CR4.OSFXSR and CR4.OSXMMEXCPT at the same time
  83. movq %rax, %cr4
  84. hlt
  85. .balign 4
  86. _apu_tmp_idt:
  87. .word 0
  88. .word 0,0
  89. .balign 4
  90. _apu_tmp_gdt:
  91. .short _apu_tmp_gdt_end - _apu_tmp_gdt -1
  92. .long _apu_tmp_gdt - _apu_boot_base
  93. .short 0
  94. .quad 0x00cf9a000000ffff
  95. .quad 0x00cf92000000ffff
  96. .quad 0x0020980000000000
  97. .quad 0x0000920000000000
  98. _apu_tmp_gdt_end:
  99. .balign 4
  100. _apu_code32_vector:
  101. .long _apu_code32 - _apu_boot_base
  102. .word 0x08,0
  103. .balign 4
  104. _apu_code64_vector:
  105. .long _apu_code64 - _apu_boot_base
  106. .word 0x18,0
  107. .balign 4
  108. _apu_boot_tmp_stack_start:
  109. .org 0x400
  110. _apu_boot_tmp_stack_end:
  111. ENTRY(_apu_boot_end)