entry.S 7.4 KB

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