entry.S 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. #include"../common/asm.h"
  2. .code64
  3. //.section .text
  4. R15 = 0x00
  5. R14 = 0x08
  6. R13 = 0x10
  7. R12 = 0x18
  8. R11 = 0x20
  9. R10 = 0x28
  10. R9 = 0x30
  11. R8 = 0x38
  12. RBX = 0x40
  13. RCX = 0x48
  14. RDX = 0x50
  15. RSI = 0x58
  16. RDI = 0x60
  17. RBP = 0x68
  18. DS = 0x70
  19. ES = 0x78
  20. RAX = 0x80
  21. FUNC = 0x88
  22. ERRCODE = 0x90
  23. RIP = 0x98
  24. CS = 0xa0
  25. RFLAGS = 0xa8
  26. OLD_RSP = 0xb0
  27. OLDSS = 0xb8
  28. Restore_all:
  29. // === 恢复调用现场 ===
  30. popq %r15
  31. popq %r14
  32. popq %r13
  33. popq %r12
  34. popq %r11
  35. popq %r10
  36. popq %r9
  37. popq %r8
  38. popq %rbx
  39. popq %rcx
  40. popq %rdx
  41. popq %rsi
  42. popq %rdi
  43. popq %rbp
  44. popq %rax // 不允许直接pop到ds
  45. movq %rax, %ds
  46. popq %rax
  47. movq %rax, %es
  48. popq %rax
  49. addq $0x10, %rsp // 弹出变量FUNC和errcode
  50. iretq
  51. ret_from_exception:
  52. // === 从中断中返回 ===
  53. ENTRY(ret_from_intr)
  54. jmp Restore_all
  55. Err_Code:
  56. // ===== 有错误码的情况下,保存寄存器并跳转服务程序
  57. pushq %rax
  58. movq %es, %rax
  59. pushq %rax
  60. movq %ds, %rax
  61. pushq %rax
  62. xorq %rax, %rax
  63. pushq %rbp
  64. pushq %rdi
  65. pushq %rsi
  66. pushq %rdx
  67. pushq %rcx
  68. pushq %rbx
  69. pushq %r8
  70. pushq %r9
  71. pushq %r10
  72. pushq %r11
  73. pushq %r12
  74. pushq %r13
  75. pushq %r14
  76. pushq %r15
  77. cld
  78. movq ERRCODE(%rsp), %rsi // 把错误码装进rsi,作为函数的第二个参数
  79. movq FUNC(%rsp), %rdx
  80. movq $0x10, %rdi // 加载内核段的地址
  81. movq %rdi, %ds
  82. movq %rdi, %es
  83. movq %rsp, %rdi // 把栈指针装入rdi,作为函数的第一个的参数
  84. callq *%rdx //调用服务程序 带*号表示调用的是绝对地址
  85. jmp ret_from_exception
  86. // 系统调用入口
  87. // 保存寄存器
  88. ENTRY(system_call)
  89. // 由于sysenter指令会禁用中断,因此要在这里手动开启中断
  90. sti;
  91. subq $0x38, %rsp
  92. cld;
  93. pushq %rax
  94. movq %es, %rax
  95. pushq %rax
  96. movq %ds, %rax
  97. pushq %rax
  98. pushq %rbp
  99. pushq %rdi
  100. pushq %rsi
  101. pushq %rdx
  102. pushq %rcx
  103. pushq %rbx
  104. pushq %r8
  105. pushq %r9
  106. pushq %r10
  107. pushq %r11
  108. pushq %r12
  109. pushq %r13
  110. pushq %r14
  111. pushq %r15
  112. movq $0x10, %rdx
  113. movq %rdx, %ds
  114. movq %rdx, %es
  115. // 将rsp作为参数传递给system_call_function
  116. movq %rsp, %rdi
  117. callq system_call_function
  118. // 从系统调用中返回
  119. ENTRY(ret_from_system_call)
  120. movq %rax, 0x80(%rsp) // 将当前rax的值先存到栈中rax的位置
  121. popq %r15
  122. popq %r14
  123. popq %r13
  124. popq %r12
  125. popq %r11
  126. popq %r10
  127. popq %r9
  128. popq %r8
  129. popq %rbx
  130. popq %rcx
  131. popq %rdx
  132. popq %rsi
  133. popq %rdi
  134. popq %rbp
  135. popq %rax // 不允许直接pop到ds
  136. movq %rax, %ds
  137. popq %rax
  138. movq %rax, %es
  139. popq %rax
  140. addq $0x38, %rsp
  141. .byte 0x48
  142. sysexit
  143. // 0 #DE 除法错误
  144. ENTRY(divide_error)
  145. pushq $0 //由于#DE不会产生错误码,但是为了保持弹出结构的一致性,故也压入一个错误码0
  146. pushq %rax // 先将rax入栈
  147. leaq do_divide_error(%rip), %rax // 获取中断服务程序的地址
  148. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  149. jmp Err_Code
  150. // 1 #DB 调试异常
  151. ENTRY(debug)
  152. pushq $0
  153. pushq %rax
  154. leaq do_debug(%rip), %rax // 获取中断服务程序的地址
  155. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  156. jmp Err_Code
  157. // 2 不可屏蔽中断
  158. ENTRY(nmi)
  159. // 不可屏蔽中断不是异常,而是一个外部中断,不会产生错误码
  160. // 应执行中断处理流程
  161. pushq $0 //占位err_code
  162. pushq %rax
  163. leaq do_nmi(%rip), %rax
  164. xchgq %rax, (%rsp)
  165. jmp Err_Code
  166. // 3 #BP 断点异常
  167. ENTRY(int3)
  168. pushq $0
  169. pushq %rax
  170. leaq do_int3(%rip), %rax // 获取中断服务程序的地址
  171. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  172. jmp Err_Code
  173. // 4 #OF 溢出异常
  174. ENTRY(overflow)
  175. pushq $0
  176. pushq %rax
  177. leaq do_overflow(%rip), %rax // 获取中断服务程序的地址
  178. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  179. jmp Err_Code
  180. // 5 #BR 越界异常
  181. ENTRY(bounds)
  182. pushq $0
  183. pushq %rax
  184. leaq do_bounds(%rip), %rax // 获取中断服务程序的地址
  185. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  186. jmp Err_Code
  187. // 6 #UD 无效/未定义的机器码
  188. ENTRY(undefined_opcode)
  189. pushq $0
  190. pushq %rax
  191. leaq do_undefined_opcode(%rip), %rax // 获取中断服务程序的地址
  192. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  193. jmp Err_Code
  194. // 7 #NM 设备异常(FPU不存在)
  195. ENTRY(dev_not_avaliable)
  196. pushq $0
  197. pushq %rax
  198. leaq do_dev_not_avaliable(%rip), %rax // 获取中断服务程序的地址
  199. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  200. jmp Err_Code
  201. // 8 #DF 双重错误
  202. ENTRY(double_fault)
  203. pushq %rax
  204. leaq do_double_fault(%rip), %rax // 获取中断服务程序的地址
  205. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  206. jmp Err_Code
  207. // 9 协处理器越界(保留)
  208. ENTRY(coprocessor_segment_overrun)
  209. pushq $0
  210. pushq %rax
  211. leaq do_coprocessor_segment_overrun(%rip), %rax // 获取中断服务程序的地址
  212. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  213. jmp Err_Code
  214. // 10 #TS 无效的TSS段
  215. ENTRY(invalid_TSS)
  216. // === 不正确的任务状态段 #TS ==
  217. // 有错误码,处理器已经自动在异常处理程序栈中压入错误码
  218. pushq %rax
  219. leaq do_invalid_TSS(%rip), %rax
  220. xchgq %rax, (%rsp)
  221. jmp Err_Code
  222. // 11 #NP 段不存在
  223. ENTRY(segment_not_exists)
  224. pushq %rax
  225. leaq do_segment_not_exists(%rip), %rax // 获取中断服务程序的地址
  226. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  227. jmp Err_Code
  228. // 12 #SS 段错误
  229. ENTRY(stack_segment_fault)
  230. pushq %rax
  231. leaq do_stack_segment_fault(%rip), %rax // 获取中断服务程序的地址
  232. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  233. jmp Err_Code
  234. // 13 #GP 通用保护性异常
  235. ENTRY(general_protection)
  236. pushq %rax
  237. leaq do_general_protection(%rip), %rax // 获取中断服务程序的地址
  238. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  239. jmp Err_Code
  240. // 14 #PF 页错误
  241. ENTRY(page_fault)
  242. // === 页故障 #PF ==
  243. // 有错误码
  244. pushq %rax
  245. leaq do_page_fault(%rip), %rax
  246. xchgq %rax, (%rsp)
  247. jmp Err_Code
  248. // 15 Intel保留,请勿使用
  249. // 16 #MF X87 FPU错误(计算错误)
  250. ENTRY(x87_FPU_error)
  251. pushq $0
  252. pushq %rax
  253. leaq do_x87_FPU_error(%rip), %rax // 获取中断服务程序的地址
  254. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  255. jmp Err_Code
  256. // 17 #AC 对齐检测
  257. ENTRY(alignment_check)
  258. pushq %rax
  259. leaq do_alignment_check(%rip), %rax // 获取中断服务程序的地址
  260. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  261. jmp Err_Code
  262. // 18 #MC 机器检测
  263. ENTRY(machine_check)
  264. pushq $0
  265. pushq %rax
  266. leaq do_machine_check(%rip), %rax // 获取中断服务程序的地址
  267. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  268. jmp Err_Code
  269. // 19 #XM SIMD浮点异常
  270. ENTRY(SIMD_exception)
  271. pushq $0
  272. pushq %rax
  273. leaq do_SIMD_exception(%rip), %rax // 获取中断服务程序的地址
  274. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  275. jmp Err_Code
  276. // 20 #VE 虚拟化异常
  277. ENTRY(virtualization_exception)
  278. pushq $0
  279. pushq %rax
  280. leaq do_virtualization_exception(%rip), %rax // 获取中断服务程序的地址
  281. xchgq %rax, (%rsp) // 把FUNC的地址换入栈中
  282. jmp Err_Code
  283. ENTRY(_stack_start)
  284. .quad initial_proc_union + 32768