entry_64.S 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. /*
  2. * linux/arch/x86_64/entry.S
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. * Copyright (C) 2000, 2001, 2002 Andi Kleen SuSE Labs
  6. * Copyright (C) 2000 Pavel Machek <pavel@suse.cz>
  7. */
  8. /*
  9. * entry.S contains the system-call and fault low-level handling routines.
  10. *
  11. * Some of this is documented in Documentation/x86/entry_64.txt
  12. *
  13. * NOTE: This code handles signal-recognition, which happens every time
  14. * after an interrupt and after each system call.
  15. *
  16. * Normal syscalls and interrupts don't save a full stack frame, this is
  17. * only done for syscall tracing, signals or fork/exec et.al.
  18. *
  19. * A note on terminology:
  20. * - top of stack: Architecture defined interrupt frame from SS to RIP
  21. * at the top of the kernel process stack.
  22. * - partial stack frame: partially saved registers up to R11.
  23. * - full stack frame: Like partial stack frame, but all register saved.
  24. *
  25. * Some macro usage:
  26. * - CFI macros are used to generate dwarf2 unwind information for better
  27. * backtraces. They don't change any code.
  28. * - SAVE_ALL/RESTORE_ALL - Save/restore all registers
  29. * - SAVE_ARGS/RESTORE_ARGS - Save/restore registers that C functions modify.
  30. * There are unfortunately lots of special cases where some registers
  31. * not touched. The macro is a big mess that should be cleaned up.
  32. * - SAVE_REST/RESTORE_REST - Handle the registers not saved by SAVE_ARGS.
  33. * Gives a full stack frame.
  34. * - ENTRY/END Define functions in the symbol table.
  35. * - FIXUP_TOP_OF_STACK/RESTORE_TOP_OF_STACK - Fix up the hardware stack
  36. * frame that is otherwise undefined after a SYSCALL
  37. * - TRACE_IRQ_* - Trace hard interrupt state for lock debugging.
  38. * - errorentry/paranoidentry/zeroentry - Define exception entry points.
  39. */
  40. #include <linux/linkage.h>
  41. #include <asm/segment.h>
  42. #include <asm/cache.h>
  43. #include <asm/errno.h>
  44. #include <asm/dwarf2.h>
  45. #include <asm/calling.h>
  46. #include <asm/asm-offsets.h>
  47. #include <asm/msr.h>
  48. #include <asm/unistd.h>
  49. #include <asm/thread_info.h>
  50. #include <asm/hw_irq.h>
  51. #include <asm/page_types.h>
  52. #include <asm/irqflags.h>
  53. #include <asm/paravirt.h>
  54. #include <asm/ftrace.h>
  55. #include <asm/percpu.h>
  56. #include <asm/asm.h>
  57. #include <asm/rcu.h>
  58. #include <linux/err.h>
  59. /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
  60. #include <linux/elf-em.h>
  61. #define AUDIT_ARCH_X86_64 (EM_X86_64|__AUDIT_ARCH_64BIT|__AUDIT_ARCH_LE)
  62. #define __AUDIT_ARCH_64BIT 0x80000000
  63. #define __AUDIT_ARCH_LE 0x40000000
  64. .code64
  65. .section .entry.text, "ax"
  66. #ifdef CONFIG_FUNCTION_TRACER
  67. #ifdef CONFIG_DYNAMIC_FTRACE
  68. ENTRY(mcount)
  69. retq
  70. END(mcount)
  71. ENTRY(ftrace_caller)
  72. cmpl $0, function_trace_stop
  73. jne ftrace_stub
  74. MCOUNT_SAVE_FRAME
  75. movq 0x38(%rsp), %rdi
  76. movq 8(%rbp), %rsi
  77. subq $MCOUNT_INSN_SIZE, %rdi
  78. GLOBAL(ftrace_call)
  79. call ftrace_stub
  80. MCOUNT_RESTORE_FRAME
  81. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  82. GLOBAL(ftrace_graph_call)
  83. jmp ftrace_stub
  84. #endif
  85. GLOBAL(ftrace_stub)
  86. retq
  87. END(ftrace_caller)
  88. #else /* ! CONFIG_DYNAMIC_FTRACE */
  89. ENTRY(mcount)
  90. cmpl $0, function_trace_stop
  91. jne ftrace_stub
  92. cmpq $ftrace_stub, ftrace_trace_function
  93. jnz trace
  94. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  95. cmpq $ftrace_stub, ftrace_graph_return
  96. jnz ftrace_graph_caller
  97. cmpq $ftrace_graph_entry_stub, ftrace_graph_entry
  98. jnz ftrace_graph_caller
  99. #endif
  100. GLOBAL(ftrace_stub)
  101. retq
  102. trace:
  103. MCOUNT_SAVE_FRAME
  104. movq 0x38(%rsp), %rdi
  105. movq 8(%rbp), %rsi
  106. subq $MCOUNT_INSN_SIZE, %rdi
  107. call *ftrace_trace_function
  108. MCOUNT_RESTORE_FRAME
  109. jmp ftrace_stub
  110. END(mcount)
  111. #endif /* CONFIG_DYNAMIC_FTRACE */
  112. #endif /* CONFIG_FUNCTION_TRACER */
  113. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  114. ENTRY(ftrace_graph_caller)
  115. cmpl $0, function_trace_stop
  116. jne ftrace_stub
  117. MCOUNT_SAVE_FRAME
  118. leaq 8(%rbp), %rdi
  119. movq 0x38(%rsp), %rsi
  120. movq (%rbp), %rdx
  121. subq $MCOUNT_INSN_SIZE, %rsi
  122. call prepare_ftrace_return
  123. MCOUNT_RESTORE_FRAME
  124. retq
  125. END(ftrace_graph_caller)
  126. GLOBAL(return_to_handler)
  127. subq $24, %rsp
  128. /* Save the return values */
  129. movq %rax, (%rsp)
  130. movq %rdx, 8(%rsp)
  131. movq %rbp, %rdi
  132. call ftrace_return_to_handler
  133. movq %rax, %rdi
  134. movq 8(%rsp), %rdx
  135. movq (%rsp), %rax
  136. addq $24, %rsp
  137. jmp *%rdi
  138. #endif
  139. #ifndef CONFIG_PREEMPT
  140. #define retint_kernel retint_restore_args
  141. #endif
  142. #ifdef CONFIG_PARAVIRT
  143. ENTRY(native_usergs_sysret64)
  144. swapgs
  145. sysretq
  146. ENDPROC(native_usergs_sysret64)
  147. #endif /* CONFIG_PARAVIRT */
  148. .macro TRACE_IRQS_IRETQ offset=ARGOFFSET
  149. #ifdef CONFIG_TRACE_IRQFLAGS
  150. bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
  151. jnc 1f
  152. TRACE_IRQS_ON
  153. 1:
  154. #endif
  155. .endm
  156. /*
  157. * When dynamic function tracer is enabled it will add a breakpoint
  158. * to all locations that it is about to modify, sync CPUs, update
  159. * all the code, sync CPUs, then remove the breakpoints. In this time
  160. * if lockdep is enabled, it might jump back into the debug handler
  161. * outside the updating of the IST protection. (TRACE_IRQS_ON/OFF).
  162. *
  163. * We need to change the IDT table before calling TRACE_IRQS_ON/OFF to
  164. * make sure the stack pointer does not get reset back to the top
  165. * of the debug stack, and instead just reuses the current stack.
  166. */
  167. #if defined(CONFIG_DYNAMIC_FTRACE) && defined(CONFIG_TRACE_IRQFLAGS)
  168. .macro TRACE_IRQS_OFF_DEBUG
  169. call debug_stack_set_zero
  170. TRACE_IRQS_OFF
  171. call debug_stack_reset
  172. .endm
  173. .macro TRACE_IRQS_ON_DEBUG
  174. call debug_stack_set_zero
  175. TRACE_IRQS_ON
  176. call debug_stack_reset
  177. .endm
  178. .macro TRACE_IRQS_IRETQ_DEBUG offset=ARGOFFSET
  179. bt $9,EFLAGS-\offset(%rsp) /* interrupts off? */
  180. jnc 1f
  181. TRACE_IRQS_ON_DEBUG
  182. 1:
  183. .endm
  184. #else
  185. # define TRACE_IRQS_OFF_DEBUG TRACE_IRQS_OFF
  186. # define TRACE_IRQS_ON_DEBUG TRACE_IRQS_ON
  187. # define TRACE_IRQS_IRETQ_DEBUG TRACE_IRQS_IRETQ
  188. #endif
  189. /*
  190. * C code is not supposed to know about undefined top of stack. Every time
  191. * a C function with an pt_regs argument is called from the SYSCALL based
  192. * fast path FIXUP_TOP_OF_STACK is needed.
  193. * RESTORE_TOP_OF_STACK syncs the syscall state after any possible ptregs
  194. * manipulation.
  195. */
  196. /* %rsp:at FRAMEEND */
  197. .macro FIXUP_TOP_OF_STACK tmp offset=0
  198. movq PER_CPU_VAR(old_rsp),\tmp
  199. movq \tmp,RSP+\offset(%rsp)
  200. movq $__USER_DS,SS+\offset(%rsp)
  201. movq $__USER_CS,CS+\offset(%rsp)
  202. movq $-1,RCX+\offset(%rsp)
  203. movq R11+\offset(%rsp),\tmp /* get eflags */
  204. movq \tmp,EFLAGS+\offset(%rsp)
  205. .endm
  206. .macro RESTORE_TOP_OF_STACK tmp offset=0
  207. movq RSP+\offset(%rsp),\tmp
  208. movq \tmp,PER_CPU_VAR(old_rsp)
  209. movq EFLAGS+\offset(%rsp),\tmp
  210. movq \tmp,R11+\offset(%rsp)
  211. .endm
  212. .macro FAKE_STACK_FRAME child_rip
  213. /* push in order ss, rsp, eflags, cs, rip */
  214. xorl %eax, %eax
  215. pushq_cfi $__KERNEL_DS /* ss */
  216. /*CFI_REL_OFFSET ss,0*/
  217. pushq_cfi %rax /* rsp */
  218. CFI_REL_OFFSET rsp,0
  219. pushq_cfi $(X86_EFLAGS_IF|X86_EFLAGS_BIT1) /* eflags - interrupts on */
  220. /*CFI_REL_OFFSET rflags,0*/
  221. pushq_cfi $__KERNEL_CS /* cs */
  222. /*CFI_REL_OFFSET cs,0*/
  223. pushq_cfi \child_rip /* rip */
  224. CFI_REL_OFFSET rip,0
  225. pushq_cfi %rax /* orig rax */
  226. .endm
  227. .macro UNFAKE_STACK_FRAME
  228. addq $8*6, %rsp
  229. CFI_ADJUST_CFA_OFFSET -(6*8)
  230. .endm
  231. /*
  232. * initial frame state for interrupts (and exceptions without error code)
  233. */
  234. .macro EMPTY_FRAME start=1 offset=0
  235. .if \start
  236. CFI_STARTPROC simple
  237. CFI_SIGNAL_FRAME
  238. CFI_DEF_CFA rsp,8+\offset
  239. .else
  240. CFI_DEF_CFA_OFFSET 8+\offset
  241. .endif
  242. .endm
  243. /*
  244. * initial frame state for interrupts (and exceptions without error code)
  245. */
  246. .macro INTR_FRAME start=1 offset=0
  247. EMPTY_FRAME \start, SS+8+\offset-RIP
  248. /*CFI_REL_OFFSET ss, SS+\offset-RIP*/
  249. CFI_REL_OFFSET rsp, RSP+\offset-RIP
  250. /*CFI_REL_OFFSET rflags, EFLAGS+\offset-RIP*/
  251. /*CFI_REL_OFFSET cs, CS+\offset-RIP*/
  252. CFI_REL_OFFSET rip, RIP+\offset-RIP
  253. .endm
  254. /*
  255. * initial frame state for exceptions with error code (and interrupts
  256. * with vector already pushed)
  257. */
  258. .macro XCPT_FRAME start=1 offset=0
  259. INTR_FRAME \start, RIP+\offset-ORIG_RAX
  260. /*CFI_REL_OFFSET orig_rax, ORIG_RAX-ORIG_RAX*/
  261. .endm
  262. /*
  263. * frame that enables calling into C.
  264. */
  265. .macro PARTIAL_FRAME start=1 offset=0
  266. XCPT_FRAME \start, ORIG_RAX+\offset-ARGOFFSET
  267. CFI_REL_OFFSET rdi, RDI+\offset-ARGOFFSET
  268. CFI_REL_OFFSET rsi, RSI+\offset-ARGOFFSET
  269. CFI_REL_OFFSET rdx, RDX+\offset-ARGOFFSET
  270. CFI_REL_OFFSET rcx, RCX+\offset-ARGOFFSET
  271. CFI_REL_OFFSET rax, RAX+\offset-ARGOFFSET
  272. CFI_REL_OFFSET r8, R8+\offset-ARGOFFSET
  273. CFI_REL_OFFSET r9, R9+\offset-ARGOFFSET
  274. CFI_REL_OFFSET r10, R10+\offset-ARGOFFSET
  275. CFI_REL_OFFSET r11, R11+\offset-ARGOFFSET
  276. .endm
  277. /*
  278. * frame that enables passing a complete pt_regs to a C function.
  279. */
  280. .macro DEFAULT_FRAME start=1 offset=0
  281. PARTIAL_FRAME \start, R11+\offset-R15
  282. CFI_REL_OFFSET rbx, RBX+\offset
  283. CFI_REL_OFFSET rbp, RBP+\offset
  284. CFI_REL_OFFSET r12, R12+\offset
  285. CFI_REL_OFFSET r13, R13+\offset
  286. CFI_REL_OFFSET r14, R14+\offset
  287. CFI_REL_OFFSET r15, R15+\offset
  288. .endm
  289. /* save partial stack frame */
  290. .macro SAVE_ARGS_IRQ
  291. cld
  292. /* start from rbp in pt_regs and jump over */
  293. movq_cfi rdi, RDI-RBP
  294. movq_cfi rsi, RSI-RBP
  295. movq_cfi rdx, RDX-RBP
  296. movq_cfi rcx, RCX-RBP
  297. movq_cfi rax, RAX-RBP
  298. movq_cfi r8, R8-RBP
  299. movq_cfi r9, R9-RBP
  300. movq_cfi r10, R10-RBP
  301. movq_cfi r11, R11-RBP
  302. /* Save rbp so that we can unwind from get_irq_regs() */
  303. movq_cfi rbp, 0
  304. /* Save previous stack value */
  305. movq %rsp, %rsi
  306. leaq -RBP(%rsp),%rdi /* arg1 for handler */
  307. testl $3, CS-RBP(%rsi)
  308. je 1f
  309. SWAPGS
  310. /*
  311. * irq_count is used to check if a CPU is already on an interrupt stack
  312. * or not. While this is essentially redundant with preempt_count it is
  313. * a little cheaper to use a separate counter in the PDA (short of
  314. * moving irq_enter into assembly, which would be too much work)
  315. */
  316. 1: incl PER_CPU_VAR(irq_count)
  317. cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
  318. CFI_DEF_CFA_REGISTER rsi
  319. /* Store previous stack value */
  320. pushq %rsi
  321. CFI_ESCAPE 0x0f /* DW_CFA_def_cfa_expression */, 6, \
  322. 0x77 /* DW_OP_breg7 */, 0, \
  323. 0x06 /* DW_OP_deref */, \
  324. 0x08 /* DW_OP_const1u */, SS+8-RBP, \
  325. 0x22 /* DW_OP_plus */
  326. /* We entered an interrupt context - irqs are off: */
  327. TRACE_IRQS_OFF
  328. .endm
  329. ENTRY(save_rest)
  330. PARTIAL_FRAME 1 REST_SKIP+8
  331. movq 5*8+16(%rsp), %r11 /* save return address */
  332. movq_cfi rbx, RBX+16
  333. movq_cfi rbp, RBP+16
  334. movq_cfi r12, R12+16
  335. movq_cfi r13, R13+16
  336. movq_cfi r14, R14+16
  337. movq_cfi r15, R15+16
  338. movq %r11, 8(%rsp) /* return address */
  339. FIXUP_TOP_OF_STACK %r11, 16
  340. ret
  341. CFI_ENDPROC
  342. END(save_rest)
  343. /* save complete stack frame */
  344. .pushsection .kprobes.text, "ax"
  345. ENTRY(save_paranoid)
  346. XCPT_FRAME 1 RDI+8
  347. cld
  348. movq_cfi rdi, RDI+8
  349. movq_cfi rsi, RSI+8
  350. movq_cfi rdx, RDX+8
  351. movq_cfi rcx, RCX+8
  352. movq_cfi rax, RAX+8
  353. movq_cfi r8, R8+8
  354. movq_cfi r9, R9+8
  355. movq_cfi r10, R10+8
  356. movq_cfi r11, R11+8
  357. movq_cfi rbx, RBX+8
  358. movq_cfi rbp, RBP+8
  359. movq_cfi r12, R12+8
  360. movq_cfi r13, R13+8
  361. movq_cfi r14, R14+8
  362. movq_cfi r15, R15+8
  363. movl $1,%ebx
  364. movl $MSR_GS_BASE,%ecx
  365. rdmsr
  366. testl %edx,%edx
  367. js 1f /* negative -> in kernel */
  368. SWAPGS
  369. xorl %ebx,%ebx
  370. 1: ret
  371. CFI_ENDPROC
  372. END(save_paranoid)
  373. .popsection
  374. /*
  375. * A newly forked process directly context switches into this address.
  376. *
  377. * rdi: prev task we switched from
  378. */
  379. ENTRY(ret_from_fork)
  380. DEFAULT_FRAME
  381. LOCK ; btr $TIF_FORK,TI_flags(%r8)
  382. pushq_cfi kernel_eflags(%rip)
  383. popfq_cfi # reset kernel eflags
  384. call schedule_tail # rdi: 'prev' task parameter
  385. GET_THREAD_INFO(%rcx)
  386. RESTORE_REST
  387. testl $3, CS-ARGOFFSET(%rsp) # from kernel_thread?
  388. jz retint_restore_args
  389. testl $_TIF_IA32, TI_flags(%rcx) # 32-bit compat task needs IRET
  390. jnz int_ret_from_sys_call
  391. RESTORE_TOP_OF_STACK %rdi, -ARGOFFSET
  392. jmp ret_from_sys_call # go to the SYSRET fastpath
  393. CFI_ENDPROC
  394. END(ret_from_fork)
  395. /*
  396. * System call entry. Up to 6 arguments in registers are supported.
  397. *
  398. * SYSCALL does not save anything on the stack and does not change the
  399. * stack pointer.
  400. */
  401. /*
  402. * Register setup:
  403. * rax system call number
  404. * rdi arg0
  405. * rcx return address for syscall/sysret, C arg3
  406. * rsi arg1
  407. * rdx arg2
  408. * r10 arg3 (--> moved to rcx for C)
  409. * r8 arg4
  410. * r9 arg5
  411. * r11 eflags for syscall/sysret, temporary for C
  412. * r12-r15,rbp,rbx saved by C code, not touched.
  413. *
  414. * Interrupts are off on entry.
  415. * Only called from user space.
  416. *
  417. * XXX if we had a free scratch register we could save the RSP into the stack frame
  418. * and report it properly in ps. Unfortunately we haven't.
  419. *
  420. * When user can change the frames always force IRET. That is because
  421. * it deals with uncanonical addresses better. SYSRET has trouble
  422. * with them due to bugs in both AMD and Intel CPUs.
  423. */
  424. ENTRY(system_call)
  425. CFI_STARTPROC simple
  426. CFI_SIGNAL_FRAME
  427. CFI_DEF_CFA rsp,KERNEL_STACK_OFFSET
  428. CFI_REGISTER rip,rcx
  429. /*CFI_REGISTER rflags,r11*/
  430. SWAPGS_UNSAFE_STACK
  431. /*
  432. * A hypervisor implementation might want to use a label
  433. * after the swapgs, so that it can do the swapgs
  434. * for the guest and jump here on syscall.
  435. */
  436. GLOBAL(system_call_after_swapgs)
  437. movq %rsp,PER_CPU_VAR(old_rsp)
  438. movq PER_CPU_VAR(kernel_stack),%rsp
  439. /*
  440. * No need to follow this irqs off/on section - it's straight
  441. * and short:
  442. */
  443. ENABLE_INTERRUPTS(CLBR_NONE)
  444. SAVE_ARGS 8,0
  445. movq %rax,ORIG_RAX-ARGOFFSET(%rsp)
  446. movq %rcx,RIP-ARGOFFSET(%rsp)
  447. CFI_REL_OFFSET rip,RIP-ARGOFFSET
  448. testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
  449. jnz tracesys
  450. system_call_fastpath:
  451. #if __SYSCALL_MASK == ~0
  452. cmpq $__NR_syscall_max,%rax
  453. #else
  454. andl $__SYSCALL_MASK,%eax
  455. cmpl $__NR_syscall_max,%eax
  456. #endif
  457. ja badsys
  458. movq %r10,%rcx
  459. call *sys_call_table(,%rax,8) # XXX: rip relative
  460. movq %rax,RAX-ARGOFFSET(%rsp)
  461. /*
  462. * Syscall return path ending with SYSRET (fast path)
  463. * Has incomplete stack frame and undefined top of stack.
  464. */
  465. ret_from_sys_call:
  466. movl $_TIF_ALLWORK_MASK,%edi
  467. /* edi: flagmask */
  468. sysret_check:
  469. LOCKDEP_SYS_EXIT
  470. DISABLE_INTERRUPTS(CLBR_NONE)
  471. TRACE_IRQS_OFF
  472. movl TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET),%edx
  473. andl %edi,%edx
  474. jnz sysret_careful
  475. CFI_REMEMBER_STATE
  476. /*
  477. * sysretq will re-enable interrupts:
  478. */
  479. TRACE_IRQS_ON
  480. movq RIP-ARGOFFSET(%rsp),%rcx
  481. CFI_REGISTER rip,rcx
  482. RESTORE_ARGS 1,-ARG_SKIP,0
  483. /*CFI_REGISTER rflags,r11*/
  484. movq PER_CPU_VAR(old_rsp), %rsp
  485. USERGS_SYSRET64
  486. CFI_RESTORE_STATE
  487. /* Handle reschedules */
  488. /* edx: work, edi: workmask */
  489. sysret_careful:
  490. bt $TIF_NEED_RESCHED,%edx
  491. jnc sysret_signal
  492. TRACE_IRQS_ON
  493. ENABLE_INTERRUPTS(CLBR_NONE)
  494. pushq_cfi %rdi
  495. SCHEDULE_USER
  496. popq_cfi %rdi
  497. jmp sysret_check
  498. /* Handle a signal */
  499. sysret_signal:
  500. TRACE_IRQS_ON
  501. ENABLE_INTERRUPTS(CLBR_NONE)
  502. #ifdef CONFIG_AUDITSYSCALL
  503. bt $TIF_SYSCALL_AUDIT,%edx
  504. jc sysret_audit
  505. #endif
  506. /*
  507. * We have a signal, or exit tracing or single-step.
  508. * These all wind up with the iret return path anyway,
  509. * so just join that path right now.
  510. */
  511. FIXUP_TOP_OF_STACK %r11, -ARGOFFSET
  512. jmp int_check_syscall_exit_work
  513. badsys:
  514. movq $-ENOSYS,RAX-ARGOFFSET(%rsp)
  515. jmp ret_from_sys_call
  516. #ifdef CONFIG_AUDITSYSCALL
  517. /*
  518. * Fast path for syscall audit without full syscall trace.
  519. * We just call __audit_syscall_entry() directly, and then
  520. * jump back to the normal fast path.
  521. */
  522. auditsys:
  523. movq %r10,%r9 /* 6th arg: 4th syscall arg */
  524. movq %rdx,%r8 /* 5th arg: 3rd syscall arg */
  525. movq %rsi,%rcx /* 4th arg: 2nd syscall arg */
  526. movq %rdi,%rdx /* 3rd arg: 1st syscall arg */
  527. movq %rax,%rsi /* 2nd arg: syscall number */
  528. movl $AUDIT_ARCH_X86_64,%edi /* 1st arg: audit arch */
  529. call __audit_syscall_entry
  530. LOAD_ARGS 0 /* reload call-clobbered registers */
  531. jmp system_call_fastpath
  532. /*
  533. * Return fast path for syscall audit. Call __audit_syscall_exit()
  534. * directly and then jump back to the fast path with TIF_SYSCALL_AUDIT
  535. * masked off.
  536. */
  537. sysret_audit:
  538. movq RAX-ARGOFFSET(%rsp),%rsi /* second arg, syscall return value */
  539. cmpq $-MAX_ERRNO,%rsi /* is it < -MAX_ERRNO? */
  540. setbe %al /* 1 if so, 0 if not */
  541. movzbl %al,%edi /* zero-extend that into %edi */
  542. call __audit_syscall_exit
  543. movl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT),%edi
  544. jmp sysret_check
  545. #endif /* CONFIG_AUDITSYSCALL */
  546. /* Do syscall tracing */
  547. tracesys:
  548. #ifdef CONFIG_AUDITSYSCALL
  549. testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags+THREAD_INFO(%rsp,RIP-ARGOFFSET)
  550. jz auditsys
  551. #endif
  552. SAVE_REST
  553. movq $-ENOSYS,RAX(%rsp) /* ptrace can change this for a bad syscall */
  554. FIXUP_TOP_OF_STACK %rdi
  555. movq %rsp,%rdi
  556. call syscall_trace_enter
  557. /*
  558. * Reload arg registers from stack in case ptrace changed them.
  559. * We don't reload %rax because syscall_trace_enter() returned
  560. * the value it wants us to use in the table lookup.
  561. */
  562. LOAD_ARGS ARGOFFSET, 1
  563. RESTORE_REST
  564. #if __SYSCALL_MASK == ~0
  565. cmpq $__NR_syscall_max,%rax
  566. #else
  567. andl $__SYSCALL_MASK,%eax
  568. cmpl $__NR_syscall_max,%eax
  569. #endif
  570. ja int_ret_from_sys_call /* RAX(%rsp) set to -ENOSYS above */
  571. movq %r10,%rcx /* fixup for C */
  572. call *sys_call_table(,%rax,8)
  573. movq %rax,RAX-ARGOFFSET(%rsp)
  574. /* Use IRET because user could have changed frame */
  575. /*
  576. * Syscall return path ending with IRET.
  577. * Has correct top of stack, but partial stack frame.
  578. */
  579. GLOBAL(int_ret_from_sys_call)
  580. DISABLE_INTERRUPTS(CLBR_NONE)
  581. TRACE_IRQS_OFF
  582. movl $_TIF_ALLWORK_MASK,%edi
  583. /* edi: mask to check */
  584. GLOBAL(int_with_check)
  585. LOCKDEP_SYS_EXIT_IRQ
  586. GET_THREAD_INFO(%rcx)
  587. movl TI_flags(%rcx),%edx
  588. andl %edi,%edx
  589. jnz int_careful
  590. andl $~TS_COMPAT,TI_status(%rcx)
  591. jmp retint_swapgs
  592. /* Either reschedule or signal or syscall exit tracking needed. */
  593. /* First do a reschedule test. */
  594. /* edx: work, edi: workmask */
  595. int_careful:
  596. bt $TIF_NEED_RESCHED,%edx
  597. jnc int_very_careful
  598. TRACE_IRQS_ON
  599. ENABLE_INTERRUPTS(CLBR_NONE)
  600. pushq_cfi %rdi
  601. SCHEDULE_USER
  602. popq_cfi %rdi
  603. DISABLE_INTERRUPTS(CLBR_NONE)
  604. TRACE_IRQS_OFF
  605. jmp int_with_check
  606. /* handle signals and tracing -- both require a full stack frame */
  607. int_very_careful:
  608. TRACE_IRQS_ON
  609. ENABLE_INTERRUPTS(CLBR_NONE)
  610. int_check_syscall_exit_work:
  611. SAVE_REST
  612. /* Check for syscall exit trace */
  613. testl $_TIF_WORK_SYSCALL_EXIT,%edx
  614. jz int_signal
  615. pushq_cfi %rdi
  616. leaq 8(%rsp),%rdi # &ptregs -> arg1
  617. call syscall_trace_leave
  618. popq_cfi %rdi
  619. andl $~(_TIF_WORK_SYSCALL_EXIT|_TIF_SYSCALL_EMU),%edi
  620. jmp int_restore_rest
  621. int_signal:
  622. testl $_TIF_DO_NOTIFY_MASK,%edx
  623. jz 1f
  624. movq %rsp,%rdi # &ptregs -> arg1
  625. xorl %esi,%esi # oldset -> arg2
  626. call do_notify_resume
  627. 1: movl $_TIF_WORK_MASK,%edi
  628. int_restore_rest:
  629. RESTORE_REST
  630. DISABLE_INTERRUPTS(CLBR_NONE)
  631. TRACE_IRQS_OFF
  632. jmp int_with_check
  633. CFI_ENDPROC
  634. END(system_call)
  635. /*
  636. * Certain special system calls that need to save a complete full stack frame.
  637. */
  638. .macro PTREGSCALL label,func,arg
  639. ENTRY(\label)
  640. PARTIAL_FRAME 1 8 /* offset 8: return address */
  641. subq $REST_SKIP, %rsp
  642. CFI_ADJUST_CFA_OFFSET REST_SKIP
  643. call save_rest
  644. DEFAULT_FRAME 0 8 /* offset 8: return address */
  645. leaq 8(%rsp), \arg /* pt_regs pointer */
  646. call \func
  647. jmp ptregscall_common
  648. CFI_ENDPROC
  649. END(\label)
  650. .endm
  651. PTREGSCALL stub_clone, sys_clone, %r8
  652. PTREGSCALL stub_fork, sys_fork, %rdi
  653. PTREGSCALL stub_vfork, sys_vfork, %rdi
  654. PTREGSCALL stub_sigaltstack, sys_sigaltstack, %rdx
  655. PTREGSCALL stub_iopl, sys_iopl, %rsi
  656. ENTRY(ptregscall_common)
  657. DEFAULT_FRAME 1 8 /* offset 8: return address */
  658. RESTORE_TOP_OF_STACK %r11, 8
  659. movq_cfi_restore R15+8, r15
  660. movq_cfi_restore R14+8, r14
  661. movq_cfi_restore R13+8, r13
  662. movq_cfi_restore R12+8, r12
  663. movq_cfi_restore RBP+8, rbp
  664. movq_cfi_restore RBX+8, rbx
  665. ret $REST_SKIP /* pop extended registers */
  666. CFI_ENDPROC
  667. END(ptregscall_common)
  668. ENTRY(stub_execve)
  669. CFI_STARTPROC
  670. addq $8, %rsp
  671. PARTIAL_FRAME 0
  672. SAVE_REST
  673. FIXUP_TOP_OF_STACK %r11
  674. movq %rsp, %rcx
  675. call sys_execve
  676. RESTORE_TOP_OF_STACK %r11
  677. movq %rax,RAX(%rsp)
  678. RESTORE_REST
  679. jmp int_ret_from_sys_call
  680. CFI_ENDPROC
  681. END(stub_execve)
  682. /*
  683. * sigreturn is special because it needs to restore all registers on return.
  684. * This cannot be done with SYSRET, so use the IRET return path instead.
  685. */
  686. ENTRY(stub_rt_sigreturn)
  687. CFI_STARTPROC
  688. addq $8, %rsp
  689. PARTIAL_FRAME 0
  690. SAVE_REST
  691. movq %rsp,%rdi
  692. FIXUP_TOP_OF_STACK %r11
  693. call sys_rt_sigreturn
  694. movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
  695. RESTORE_REST
  696. jmp int_ret_from_sys_call
  697. CFI_ENDPROC
  698. END(stub_rt_sigreturn)
  699. #ifdef CONFIG_X86_X32_ABI
  700. PTREGSCALL stub_x32_sigaltstack, sys32_sigaltstack, %rdx
  701. ENTRY(stub_x32_rt_sigreturn)
  702. CFI_STARTPROC
  703. addq $8, %rsp
  704. PARTIAL_FRAME 0
  705. SAVE_REST
  706. movq %rsp,%rdi
  707. FIXUP_TOP_OF_STACK %r11
  708. call sys32_x32_rt_sigreturn
  709. movq %rax,RAX(%rsp) # fixme, this could be done at the higher layer
  710. RESTORE_REST
  711. jmp int_ret_from_sys_call
  712. CFI_ENDPROC
  713. END(stub_x32_rt_sigreturn)
  714. ENTRY(stub_x32_execve)
  715. CFI_STARTPROC
  716. addq $8, %rsp
  717. PARTIAL_FRAME 0
  718. SAVE_REST
  719. FIXUP_TOP_OF_STACK %r11
  720. movq %rsp, %rcx
  721. call sys32_execve
  722. RESTORE_TOP_OF_STACK %r11
  723. movq %rax,RAX(%rsp)
  724. RESTORE_REST
  725. jmp int_ret_from_sys_call
  726. CFI_ENDPROC
  727. END(stub_x32_execve)
  728. #endif
  729. /*
  730. * Build the entry stubs and pointer table with some assembler magic.
  731. * We pack 7 stubs into a single 32-byte chunk, which will fit in a
  732. * single cache line on all modern x86 implementations.
  733. */
  734. .section .init.rodata,"a"
  735. ENTRY(interrupt)
  736. .section .entry.text
  737. .p2align 5
  738. .p2align CONFIG_X86_L1_CACHE_SHIFT
  739. ENTRY(irq_entries_start)
  740. INTR_FRAME
  741. vector=FIRST_EXTERNAL_VECTOR
  742. .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
  743. .balign 32
  744. .rept 7
  745. .if vector < NR_VECTORS
  746. .if vector <> FIRST_EXTERNAL_VECTOR
  747. CFI_ADJUST_CFA_OFFSET -8
  748. .endif
  749. 1: pushq_cfi $(~vector+0x80) /* Note: always in signed byte range */
  750. .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
  751. jmp 2f
  752. .endif
  753. .previous
  754. .quad 1b
  755. .section .entry.text
  756. vector=vector+1
  757. .endif
  758. .endr
  759. 2: jmp common_interrupt
  760. .endr
  761. CFI_ENDPROC
  762. END(irq_entries_start)
  763. .previous
  764. END(interrupt)
  765. .previous
  766. /*
  767. * Interrupt entry/exit.
  768. *
  769. * Interrupt entry points save only callee clobbered registers in fast path.
  770. *
  771. * Entry runs with interrupts off.
  772. */
  773. /* 0(%rsp): ~(interrupt number) */
  774. .macro interrupt func
  775. /* reserve pt_regs for scratch regs and rbp */
  776. subq $ORIG_RAX-RBP, %rsp
  777. CFI_ADJUST_CFA_OFFSET ORIG_RAX-RBP
  778. SAVE_ARGS_IRQ
  779. call \func
  780. .endm
  781. /*
  782. * Interrupt entry/exit should be protected against kprobes
  783. */
  784. .pushsection .kprobes.text, "ax"
  785. /*
  786. * The interrupt stubs push (~vector+0x80) onto the stack and
  787. * then jump to common_interrupt.
  788. */
  789. .p2align CONFIG_X86_L1_CACHE_SHIFT
  790. common_interrupt:
  791. XCPT_FRAME
  792. addq $-0x80,(%rsp) /* Adjust vector to [-256,-1] range */
  793. interrupt do_IRQ
  794. /* 0(%rsp): old_rsp-ARGOFFSET */
  795. ret_from_intr:
  796. DISABLE_INTERRUPTS(CLBR_NONE)
  797. TRACE_IRQS_OFF
  798. decl PER_CPU_VAR(irq_count)
  799. /* Restore saved previous stack */
  800. popq %rsi
  801. CFI_DEF_CFA rsi,SS+8-RBP /* reg/off reset after def_cfa_expr */
  802. leaq ARGOFFSET-RBP(%rsi), %rsp
  803. CFI_DEF_CFA_REGISTER rsp
  804. CFI_ADJUST_CFA_OFFSET RBP-ARGOFFSET
  805. exit_intr:
  806. GET_THREAD_INFO(%rcx)
  807. testl $3,CS-ARGOFFSET(%rsp)
  808. je retint_kernel
  809. /* Interrupt came from user space */
  810. /*
  811. * Has a correct top of stack, but a partial stack frame
  812. * %rcx: thread info. Interrupts off.
  813. */
  814. retint_with_reschedule:
  815. movl $_TIF_WORK_MASK,%edi
  816. retint_check:
  817. LOCKDEP_SYS_EXIT_IRQ
  818. movl TI_flags(%rcx),%edx
  819. andl %edi,%edx
  820. CFI_REMEMBER_STATE
  821. jnz retint_careful
  822. retint_swapgs: /* return to user-space */
  823. /*
  824. * The iretq could re-enable interrupts:
  825. */
  826. DISABLE_INTERRUPTS(CLBR_ANY)
  827. TRACE_IRQS_IRETQ
  828. SWAPGS
  829. jmp restore_args
  830. retint_restore_args: /* return to kernel space */
  831. DISABLE_INTERRUPTS(CLBR_ANY)
  832. /*
  833. * The iretq could re-enable interrupts:
  834. */
  835. TRACE_IRQS_IRETQ
  836. restore_args:
  837. RESTORE_ARGS 1,8,1
  838. irq_return:
  839. INTERRUPT_RETURN
  840. _ASM_EXTABLE(irq_return, bad_iret)
  841. #ifdef CONFIG_PARAVIRT
  842. ENTRY(native_iret)
  843. iretq
  844. _ASM_EXTABLE(native_iret, bad_iret)
  845. #endif
  846. .section .fixup,"ax"
  847. bad_iret:
  848. /*
  849. * The iret traps when the %cs or %ss being restored is bogus.
  850. * We've lost the original trap vector and error code.
  851. * #GPF is the most likely one to get for an invalid selector.
  852. * So pretend we completed the iret and took the #GPF in user mode.
  853. *
  854. * We are now running with the kernel GS after exception recovery.
  855. * But error_entry expects us to have user GS to match the user %cs,
  856. * so swap back.
  857. */
  858. pushq $0
  859. SWAPGS
  860. jmp general_protection
  861. .previous
  862. /* edi: workmask, edx: work */
  863. retint_careful:
  864. CFI_RESTORE_STATE
  865. bt $TIF_NEED_RESCHED,%edx
  866. jnc retint_signal
  867. TRACE_IRQS_ON
  868. ENABLE_INTERRUPTS(CLBR_NONE)
  869. pushq_cfi %rdi
  870. SCHEDULE_USER
  871. popq_cfi %rdi
  872. GET_THREAD_INFO(%rcx)
  873. DISABLE_INTERRUPTS(CLBR_NONE)
  874. TRACE_IRQS_OFF
  875. jmp retint_check
  876. retint_signal:
  877. testl $_TIF_DO_NOTIFY_MASK,%edx
  878. jz retint_swapgs
  879. TRACE_IRQS_ON
  880. ENABLE_INTERRUPTS(CLBR_NONE)
  881. SAVE_REST
  882. movq $-1,ORIG_RAX(%rsp)
  883. xorl %esi,%esi # oldset
  884. movq %rsp,%rdi # &pt_regs
  885. call do_notify_resume
  886. RESTORE_REST
  887. DISABLE_INTERRUPTS(CLBR_NONE)
  888. TRACE_IRQS_OFF
  889. GET_THREAD_INFO(%rcx)
  890. jmp retint_with_reschedule
  891. #ifdef CONFIG_PREEMPT
  892. /* Returning to kernel space. Check if we need preemption */
  893. /* rcx: threadinfo. interrupts off. */
  894. ENTRY(retint_kernel)
  895. cmpl $0,TI_preempt_count(%rcx)
  896. jnz retint_restore_args
  897. bt $TIF_NEED_RESCHED,TI_flags(%rcx)
  898. jnc retint_restore_args
  899. bt $9,EFLAGS-ARGOFFSET(%rsp) /* interrupts off? */
  900. jnc retint_restore_args
  901. call preempt_schedule_irq
  902. jmp exit_intr
  903. #endif
  904. CFI_ENDPROC
  905. END(common_interrupt)
  906. /*
  907. * End of kprobes section
  908. */
  909. .popsection
  910. /*
  911. * APIC interrupts.
  912. */
  913. .macro apicinterrupt num sym do_sym
  914. ENTRY(\sym)
  915. INTR_FRAME
  916. pushq_cfi $~(\num)
  917. .Lcommon_\sym:
  918. interrupt \do_sym
  919. jmp ret_from_intr
  920. CFI_ENDPROC
  921. END(\sym)
  922. .endm
  923. #ifdef CONFIG_SMP
  924. apicinterrupt IRQ_MOVE_CLEANUP_VECTOR \
  925. irq_move_cleanup_interrupt smp_irq_move_cleanup_interrupt
  926. apicinterrupt REBOOT_VECTOR \
  927. reboot_interrupt smp_reboot_interrupt
  928. #endif
  929. #ifdef CONFIG_X86_UV
  930. apicinterrupt UV_BAU_MESSAGE \
  931. uv_bau_message_intr1 uv_bau_message_interrupt
  932. #endif
  933. apicinterrupt LOCAL_TIMER_VECTOR \
  934. apic_timer_interrupt smp_apic_timer_interrupt
  935. apicinterrupt X86_PLATFORM_IPI_VECTOR \
  936. x86_platform_ipi smp_x86_platform_ipi
  937. apicinterrupt THRESHOLD_APIC_VECTOR \
  938. threshold_interrupt smp_threshold_interrupt
  939. apicinterrupt THERMAL_APIC_VECTOR \
  940. thermal_interrupt smp_thermal_interrupt
  941. #ifdef CONFIG_SMP
  942. apicinterrupt CALL_FUNCTION_SINGLE_VECTOR \
  943. call_function_single_interrupt smp_call_function_single_interrupt
  944. apicinterrupt CALL_FUNCTION_VECTOR \
  945. call_function_interrupt smp_call_function_interrupt
  946. apicinterrupt RESCHEDULE_VECTOR \
  947. reschedule_interrupt smp_reschedule_interrupt
  948. #endif
  949. apicinterrupt ERROR_APIC_VECTOR \
  950. error_interrupt smp_error_interrupt
  951. apicinterrupt SPURIOUS_APIC_VECTOR \
  952. spurious_interrupt smp_spurious_interrupt
  953. #ifdef CONFIG_IRQ_WORK
  954. apicinterrupt IRQ_WORK_VECTOR \
  955. irq_work_interrupt smp_irq_work_interrupt
  956. #endif
  957. /*
  958. * Exception entry points.
  959. */
  960. .macro zeroentry sym do_sym
  961. ENTRY(\sym)
  962. INTR_FRAME
  963. PARAVIRT_ADJUST_EXCEPTION_FRAME
  964. pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
  965. subq $ORIG_RAX-R15, %rsp
  966. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  967. call error_entry
  968. DEFAULT_FRAME 0
  969. movq %rsp,%rdi /* pt_regs pointer */
  970. xorl %esi,%esi /* no error code */
  971. call \do_sym
  972. jmp error_exit /* %ebx: no swapgs flag */
  973. CFI_ENDPROC
  974. END(\sym)
  975. .endm
  976. .macro paranoidzeroentry sym do_sym
  977. ENTRY(\sym)
  978. INTR_FRAME
  979. PARAVIRT_ADJUST_EXCEPTION_FRAME
  980. pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
  981. subq $ORIG_RAX-R15, %rsp
  982. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  983. call save_paranoid
  984. TRACE_IRQS_OFF
  985. movq %rsp,%rdi /* pt_regs pointer */
  986. xorl %esi,%esi /* no error code */
  987. call \do_sym
  988. jmp paranoid_exit /* %ebx: no swapgs flag */
  989. CFI_ENDPROC
  990. END(\sym)
  991. .endm
  992. #define INIT_TSS_IST(x) PER_CPU_VAR(init_tss) + (TSS_ist + ((x) - 1) * 8)
  993. .macro paranoidzeroentry_ist sym do_sym ist
  994. ENTRY(\sym)
  995. INTR_FRAME
  996. PARAVIRT_ADJUST_EXCEPTION_FRAME
  997. pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
  998. subq $ORIG_RAX-R15, %rsp
  999. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  1000. call save_paranoid
  1001. TRACE_IRQS_OFF_DEBUG
  1002. movq %rsp,%rdi /* pt_regs pointer */
  1003. xorl %esi,%esi /* no error code */
  1004. subq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
  1005. call \do_sym
  1006. addq $EXCEPTION_STKSZ, INIT_TSS_IST(\ist)
  1007. jmp paranoid_exit /* %ebx: no swapgs flag */
  1008. CFI_ENDPROC
  1009. END(\sym)
  1010. .endm
  1011. .macro errorentry sym do_sym
  1012. ENTRY(\sym)
  1013. XCPT_FRAME
  1014. PARAVIRT_ADJUST_EXCEPTION_FRAME
  1015. subq $ORIG_RAX-R15, %rsp
  1016. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  1017. call error_entry
  1018. DEFAULT_FRAME 0
  1019. movq %rsp,%rdi /* pt_regs pointer */
  1020. movq ORIG_RAX(%rsp),%rsi /* get error code */
  1021. movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
  1022. call \do_sym
  1023. jmp error_exit /* %ebx: no swapgs flag */
  1024. CFI_ENDPROC
  1025. END(\sym)
  1026. .endm
  1027. /* error code is on the stack already */
  1028. .macro paranoiderrorentry sym do_sym
  1029. ENTRY(\sym)
  1030. XCPT_FRAME
  1031. PARAVIRT_ADJUST_EXCEPTION_FRAME
  1032. subq $ORIG_RAX-R15, %rsp
  1033. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  1034. call save_paranoid
  1035. DEFAULT_FRAME 0
  1036. TRACE_IRQS_OFF
  1037. movq %rsp,%rdi /* pt_regs pointer */
  1038. movq ORIG_RAX(%rsp),%rsi /* get error code */
  1039. movq $-1,ORIG_RAX(%rsp) /* no syscall to restart */
  1040. call \do_sym
  1041. jmp paranoid_exit /* %ebx: no swapgs flag */
  1042. CFI_ENDPROC
  1043. END(\sym)
  1044. .endm
  1045. zeroentry divide_error do_divide_error
  1046. zeroentry overflow do_overflow
  1047. zeroentry bounds do_bounds
  1048. zeroentry invalid_op do_invalid_op
  1049. zeroentry device_not_available do_device_not_available
  1050. paranoiderrorentry double_fault do_double_fault
  1051. zeroentry coprocessor_segment_overrun do_coprocessor_segment_overrun
  1052. errorentry invalid_TSS do_invalid_TSS
  1053. errorentry segment_not_present do_segment_not_present
  1054. zeroentry spurious_interrupt_bug do_spurious_interrupt_bug
  1055. zeroentry coprocessor_error do_coprocessor_error
  1056. errorentry alignment_check do_alignment_check
  1057. zeroentry simd_coprocessor_error do_simd_coprocessor_error
  1058. /* Reload gs selector with exception handling */
  1059. /* edi: new selector */
  1060. ENTRY(native_load_gs_index)
  1061. CFI_STARTPROC
  1062. pushfq_cfi
  1063. DISABLE_INTERRUPTS(CLBR_ANY & ~CLBR_RDI)
  1064. SWAPGS
  1065. gs_change:
  1066. movl %edi,%gs
  1067. 2: mfence /* workaround */
  1068. SWAPGS
  1069. popfq_cfi
  1070. ret
  1071. CFI_ENDPROC
  1072. END(native_load_gs_index)
  1073. _ASM_EXTABLE(gs_change,bad_gs)
  1074. .section .fixup,"ax"
  1075. /* running with kernelgs */
  1076. bad_gs:
  1077. SWAPGS /* switch back to user gs */
  1078. xorl %eax,%eax
  1079. movl %eax,%gs
  1080. jmp 2b
  1081. .previous
  1082. ENTRY(kernel_thread_helper)
  1083. pushq $0 # fake return address
  1084. CFI_STARTPROC
  1085. /*
  1086. * Here we are in the child and the registers are set as they were
  1087. * at kernel_thread() invocation in the parent.
  1088. */
  1089. call *%rsi
  1090. # exit
  1091. mov %eax, %edi
  1092. call do_exit
  1093. ud2 # padding for call trace
  1094. CFI_ENDPROC
  1095. END(kernel_thread_helper)
  1096. /*
  1097. * execve(). This function needs to use IRET, not SYSRET, to set up all state properly.
  1098. *
  1099. * C extern interface:
  1100. * extern long execve(const char *name, char **argv, char **envp)
  1101. *
  1102. * asm input arguments:
  1103. * rdi: name, rsi: argv, rdx: envp
  1104. *
  1105. * We want to fallback into:
  1106. * extern long sys_execve(const char *name, char **argv,char **envp, struct pt_regs *regs)
  1107. *
  1108. * do_sys_execve asm fallback arguments:
  1109. * rdi: name, rsi: argv, rdx: envp, rcx: fake frame on the stack
  1110. */
  1111. ENTRY(kernel_execve)
  1112. CFI_STARTPROC
  1113. FAKE_STACK_FRAME $0
  1114. SAVE_ALL
  1115. movq %rsp,%rcx
  1116. call sys_execve
  1117. movq %rax, RAX(%rsp)
  1118. RESTORE_REST
  1119. testq %rax,%rax
  1120. je int_ret_from_sys_call
  1121. RESTORE_ARGS
  1122. UNFAKE_STACK_FRAME
  1123. ret
  1124. CFI_ENDPROC
  1125. END(kernel_execve)
  1126. /* Call softirq on interrupt stack. Interrupts are off. */
  1127. ENTRY(call_softirq)
  1128. CFI_STARTPROC
  1129. pushq_cfi %rbp
  1130. CFI_REL_OFFSET rbp,0
  1131. mov %rsp,%rbp
  1132. CFI_DEF_CFA_REGISTER rbp
  1133. incl PER_CPU_VAR(irq_count)
  1134. cmove PER_CPU_VAR(irq_stack_ptr),%rsp
  1135. push %rbp # backlink for old unwinder
  1136. call __do_softirq
  1137. leaveq
  1138. CFI_RESTORE rbp
  1139. CFI_DEF_CFA_REGISTER rsp
  1140. CFI_ADJUST_CFA_OFFSET -8
  1141. decl PER_CPU_VAR(irq_count)
  1142. ret
  1143. CFI_ENDPROC
  1144. END(call_softirq)
  1145. #ifdef CONFIG_XEN
  1146. zeroentry xen_hypervisor_callback xen_do_hypervisor_callback
  1147. /*
  1148. * A note on the "critical region" in our callback handler.
  1149. * We want to avoid stacking callback handlers due to events occurring
  1150. * during handling of the last event. To do this, we keep events disabled
  1151. * until we've done all processing. HOWEVER, we must enable events before
  1152. * popping the stack frame (can't be done atomically) and so it would still
  1153. * be possible to get enough handler activations to overflow the stack.
  1154. * Although unlikely, bugs of that kind are hard to track down, so we'd
  1155. * like to avoid the possibility.
  1156. * So, on entry to the handler we detect whether we interrupted an
  1157. * existing activation in its critical region -- if so, we pop the current
  1158. * activation and restart the handler using the previous one.
  1159. */
  1160. ENTRY(xen_do_hypervisor_callback) # do_hypervisor_callback(struct *pt_regs)
  1161. CFI_STARTPROC
  1162. /*
  1163. * Since we don't modify %rdi, evtchn_do_upall(struct *pt_regs) will
  1164. * see the correct pointer to the pt_regs
  1165. */
  1166. movq %rdi, %rsp # we don't return, adjust the stack frame
  1167. CFI_ENDPROC
  1168. DEFAULT_FRAME
  1169. 11: incl PER_CPU_VAR(irq_count)
  1170. movq %rsp,%rbp
  1171. CFI_DEF_CFA_REGISTER rbp
  1172. cmovzq PER_CPU_VAR(irq_stack_ptr),%rsp
  1173. pushq %rbp # backlink for old unwinder
  1174. call xen_evtchn_do_upcall
  1175. popq %rsp
  1176. CFI_DEF_CFA_REGISTER rsp
  1177. decl PER_CPU_VAR(irq_count)
  1178. jmp error_exit
  1179. CFI_ENDPROC
  1180. END(xen_do_hypervisor_callback)
  1181. /*
  1182. * Hypervisor uses this for application faults while it executes.
  1183. * We get here for two reasons:
  1184. * 1. Fault while reloading DS, ES, FS or GS
  1185. * 2. Fault while executing IRET
  1186. * Category 1 we do not need to fix up as Xen has already reloaded all segment
  1187. * registers that could be reloaded and zeroed the others.
  1188. * Category 2 we fix up by killing the current process. We cannot use the
  1189. * normal Linux return path in this case because if we use the IRET hypercall
  1190. * to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  1191. * We distinguish between categories by comparing each saved segment register
  1192. * with its current contents: any discrepancy means we in category 1.
  1193. */
  1194. ENTRY(xen_failsafe_callback)
  1195. INTR_FRAME 1 (6*8)
  1196. /*CFI_REL_OFFSET gs,GS*/
  1197. /*CFI_REL_OFFSET fs,FS*/
  1198. /*CFI_REL_OFFSET es,ES*/
  1199. /*CFI_REL_OFFSET ds,DS*/
  1200. CFI_REL_OFFSET r11,8
  1201. CFI_REL_OFFSET rcx,0
  1202. movw %ds,%cx
  1203. cmpw %cx,0x10(%rsp)
  1204. CFI_REMEMBER_STATE
  1205. jne 1f
  1206. movw %es,%cx
  1207. cmpw %cx,0x18(%rsp)
  1208. jne 1f
  1209. movw %fs,%cx
  1210. cmpw %cx,0x20(%rsp)
  1211. jne 1f
  1212. movw %gs,%cx
  1213. cmpw %cx,0x28(%rsp)
  1214. jne 1f
  1215. /* All segments match their saved values => Category 2 (Bad IRET). */
  1216. movq (%rsp),%rcx
  1217. CFI_RESTORE rcx
  1218. movq 8(%rsp),%r11
  1219. CFI_RESTORE r11
  1220. addq $0x30,%rsp
  1221. CFI_ADJUST_CFA_OFFSET -0x30
  1222. pushq_cfi $0 /* RIP */
  1223. pushq_cfi %r11
  1224. pushq_cfi %rcx
  1225. jmp general_protection
  1226. CFI_RESTORE_STATE
  1227. 1: /* Segment mismatch => Category 1 (Bad segment). Retry the IRET. */
  1228. movq (%rsp),%rcx
  1229. CFI_RESTORE rcx
  1230. movq 8(%rsp),%r11
  1231. CFI_RESTORE r11
  1232. addq $0x30,%rsp
  1233. CFI_ADJUST_CFA_OFFSET -0x30
  1234. pushq_cfi $0
  1235. SAVE_ALL
  1236. jmp error_exit
  1237. CFI_ENDPROC
  1238. END(xen_failsafe_callback)
  1239. apicinterrupt XEN_HVM_EVTCHN_CALLBACK \
  1240. xen_hvm_callback_vector xen_evtchn_do_upcall
  1241. #endif /* CONFIG_XEN */
  1242. /*
  1243. * Some functions should be protected against kprobes
  1244. */
  1245. .pushsection .kprobes.text, "ax"
  1246. paranoidzeroentry_ist debug do_debug DEBUG_STACK
  1247. paranoidzeroentry_ist int3 do_int3 DEBUG_STACK
  1248. paranoiderrorentry stack_segment do_stack_segment
  1249. #ifdef CONFIG_XEN
  1250. zeroentry xen_debug do_debug
  1251. zeroentry xen_int3 do_int3
  1252. errorentry xen_stack_segment do_stack_segment
  1253. #endif
  1254. errorentry general_protection do_general_protection
  1255. errorentry page_fault do_page_fault
  1256. #ifdef CONFIG_KVM_GUEST
  1257. errorentry async_page_fault do_async_page_fault
  1258. #endif
  1259. #ifdef CONFIG_X86_MCE
  1260. paranoidzeroentry machine_check *machine_check_vector(%rip)
  1261. #endif
  1262. /*
  1263. * "Paranoid" exit path from exception stack.
  1264. * Paranoid because this is used by NMIs and cannot take
  1265. * any kernel state for granted.
  1266. * We don't do kernel preemption checks here, because only
  1267. * NMI should be common and it does not enable IRQs and
  1268. * cannot get reschedule ticks.
  1269. *
  1270. * "trace" is 0 for the NMI handler only, because irq-tracing
  1271. * is fundamentally NMI-unsafe. (we cannot change the soft and
  1272. * hard flags at once, atomically)
  1273. */
  1274. /* ebx: no swapgs flag */
  1275. ENTRY(paranoid_exit)
  1276. DEFAULT_FRAME
  1277. DISABLE_INTERRUPTS(CLBR_NONE)
  1278. TRACE_IRQS_OFF_DEBUG
  1279. testl %ebx,%ebx /* swapgs needed? */
  1280. jnz paranoid_restore
  1281. testl $3,CS(%rsp)
  1282. jnz paranoid_userspace
  1283. paranoid_swapgs:
  1284. TRACE_IRQS_IRETQ 0
  1285. SWAPGS_UNSAFE_STACK
  1286. RESTORE_ALL 8
  1287. jmp irq_return
  1288. paranoid_restore:
  1289. TRACE_IRQS_IRETQ_DEBUG 0
  1290. RESTORE_ALL 8
  1291. jmp irq_return
  1292. paranoid_userspace:
  1293. GET_THREAD_INFO(%rcx)
  1294. movl TI_flags(%rcx),%ebx
  1295. andl $_TIF_WORK_MASK,%ebx
  1296. jz paranoid_swapgs
  1297. movq %rsp,%rdi /* &pt_regs */
  1298. call sync_regs
  1299. movq %rax,%rsp /* switch stack for scheduling */
  1300. testl $_TIF_NEED_RESCHED,%ebx
  1301. jnz paranoid_schedule
  1302. movl %ebx,%edx /* arg3: thread flags */
  1303. TRACE_IRQS_ON
  1304. ENABLE_INTERRUPTS(CLBR_NONE)
  1305. xorl %esi,%esi /* arg2: oldset */
  1306. movq %rsp,%rdi /* arg1: &pt_regs */
  1307. call do_notify_resume
  1308. DISABLE_INTERRUPTS(CLBR_NONE)
  1309. TRACE_IRQS_OFF
  1310. jmp paranoid_userspace
  1311. paranoid_schedule:
  1312. TRACE_IRQS_ON
  1313. ENABLE_INTERRUPTS(CLBR_ANY)
  1314. SCHEDULE_USER
  1315. DISABLE_INTERRUPTS(CLBR_ANY)
  1316. TRACE_IRQS_OFF
  1317. jmp paranoid_userspace
  1318. CFI_ENDPROC
  1319. END(paranoid_exit)
  1320. /*
  1321. * Exception entry point. This expects an error code/orig_rax on the stack.
  1322. * returns in "no swapgs flag" in %ebx.
  1323. */
  1324. ENTRY(error_entry)
  1325. XCPT_FRAME
  1326. CFI_ADJUST_CFA_OFFSET 15*8
  1327. /* oldrax contains error code */
  1328. cld
  1329. movq_cfi rdi, RDI+8
  1330. movq_cfi rsi, RSI+8
  1331. movq_cfi rdx, RDX+8
  1332. movq_cfi rcx, RCX+8
  1333. movq_cfi rax, RAX+8
  1334. movq_cfi r8, R8+8
  1335. movq_cfi r9, R9+8
  1336. movq_cfi r10, R10+8
  1337. movq_cfi r11, R11+8
  1338. movq_cfi rbx, RBX+8
  1339. movq_cfi rbp, RBP+8
  1340. movq_cfi r12, R12+8
  1341. movq_cfi r13, R13+8
  1342. movq_cfi r14, R14+8
  1343. movq_cfi r15, R15+8
  1344. xorl %ebx,%ebx
  1345. testl $3,CS+8(%rsp)
  1346. je error_kernelspace
  1347. error_swapgs:
  1348. SWAPGS
  1349. error_sti:
  1350. TRACE_IRQS_OFF
  1351. ret
  1352. /*
  1353. * There are two places in the kernel that can potentially fault with
  1354. * usergs. Handle them here. The exception handlers after iret run with
  1355. * kernel gs again, so don't set the user space flag. B stepping K8s
  1356. * sometimes report an truncated RIP for IRET exceptions returning to
  1357. * compat mode. Check for these here too.
  1358. */
  1359. error_kernelspace:
  1360. incl %ebx
  1361. leaq irq_return(%rip),%rcx
  1362. cmpq %rcx,RIP+8(%rsp)
  1363. je error_swapgs
  1364. movl %ecx,%eax /* zero extend */
  1365. cmpq %rax,RIP+8(%rsp)
  1366. je bstep_iret
  1367. cmpq $gs_change,RIP+8(%rsp)
  1368. je error_swapgs
  1369. jmp error_sti
  1370. bstep_iret:
  1371. /* Fix truncated RIP */
  1372. movq %rcx,RIP+8(%rsp)
  1373. jmp error_swapgs
  1374. CFI_ENDPROC
  1375. END(error_entry)
  1376. /* ebx: no swapgs flag (1: don't need swapgs, 0: need it) */
  1377. ENTRY(error_exit)
  1378. DEFAULT_FRAME
  1379. movl %ebx,%eax
  1380. RESTORE_REST
  1381. DISABLE_INTERRUPTS(CLBR_NONE)
  1382. TRACE_IRQS_OFF
  1383. GET_THREAD_INFO(%rcx)
  1384. testl %eax,%eax
  1385. jne retint_kernel
  1386. LOCKDEP_SYS_EXIT_IRQ
  1387. movl TI_flags(%rcx),%edx
  1388. movl $_TIF_WORK_MASK,%edi
  1389. andl %edi,%edx
  1390. jnz retint_careful
  1391. jmp retint_swapgs
  1392. CFI_ENDPROC
  1393. END(error_exit)
  1394. /*
  1395. * Test if a given stack is an NMI stack or not.
  1396. */
  1397. .macro test_in_nmi reg stack nmi_ret normal_ret
  1398. cmpq %\reg, \stack
  1399. ja \normal_ret
  1400. subq $EXCEPTION_STKSZ, %\reg
  1401. cmpq %\reg, \stack
  1402. jb \normal_ret
  1403. jmp \nmi_ret
  1404. .endm
  1405. /* runs on exception stack */
  1406. ENTRY(nmi)
  1407. INTR_FRAME
  1408. PARAVIRT_ADJUST_EXCEPTION_FRAME
  1409. /*
  1410. * We allow breakpoints in NMIs. If a breakpoint occurs, then
  1411. * the iretq it performs will take us out of NMI context.
  1412. * This means that we can have nested NMIs where the next
  1413. * NMI is using the top of the stack of the previous NMI. We
  1414. * can't let it execute because the nested NMI will corrupt the
  1415. * stack of the previous NMI. NMI handlers are not re-entrant
  1416. * anyway.
  1417. *
  1418. * To handle this case we do the following:
  1419. * Check the a special location on the stack that contains
  1420. * a variable that is set when NMIs are executing.
  1421. * The interrupted task's stack is also checked to see if it
  1422. * is an NMI stack.
  1423. * If the variable is not set and the stack is not the NMI
  1424. * stack then:
  1425. * o Set the special variable on the stack
  1426. * o Copy the interrupt frame into a "saved" location on the stack
  1427. * o Copy the interrupt frame into a "copy" location on the stack
  1428. * o Continue processing the NMI
  1429. * If the variable is set or the previous stack is the NMI stack:
  1430. * o Modify the "copy" location to jump to the repeate_nmi
  1431. * o return back to the first NMI
  1432. *
  1433. * Now on exit of the first NMI, we first clear the stack variable
  1434. * The NMI stack will tell any nested NMIs at that point that it is
  1435. * nested. Then we pop the stack normally with iret, and if there was
  1436. * a nested NMI that updated the copy interrupt stack frame, a
  1437. * jump will be made to the repeat_nmi code that will handle the second
  1438. * NMI.
  1439. */
  1440. /* Use %rdx as out temp variable throughout */
  1441. pushq_cfi %rdx
  1442. CFI_REL_OFFSET rdx, 0
  1443. /*
  1444. * If %cs was not the kernel segment, then the NMI triggered in user
  1445. * space, which means it is definitely not nested.
  1446. */
  1447. cmpl $__KERNEL_CS, 16(%rsp)
  1448. jne first_nmi
  1449. /*
  1450. * Check the special variable on the stack to see if NMIs are
  1451. * executing.
  1452. */
  1453. cmpl $1, -8(%rsp)
  1454. je nested_nmi
  1455. /*
  1456. * Now test if the previous stack was an NMI stack.
  1457. * We need the double check. We check the NMI stack to satisfy the
  1458. * race when the first NMI clears the variable before returning.
  1459. * We check the variable because the first NMI could be in a
  1460. * breakpoint routine using a breakpoint stack.
  1461. */
  1462. lea 6*8(%rsp), %rdx
  1463. test_in_nmi rdx, 4*8(%rsp), nested_nmi, first_nmi
  1464. CFI_REMEMBER_STATE
  1465. nested_nmi:
  1466. /*
  1467. * Do nothing if we interrupted the fixup in repeat_nmi.
  1468. * It's about to repeat the NMI handler, so we are fine
  1469. * with ignoring this one.
  1470. */
  1471. movq $repeat_nmi, %rdx
  1472. cmpq 8(%rsp), %rdx
  1473. ja 1f
  1474. movq $end_repeat_nmi, %rdx
  1475. cmpq 8(%rsp), %rdx
  1476. ja nested_nmi_out
  1477. 1:
  1478. /* Set up the interrupted NMIs stack to jump to repeat_nmi */
  1479. leaq -6*8(%rsp), %rdx
  1480. movq %rdx, %rsp
  1481. CFI_ADJUST_CFA_OFFSET 6*8
  1482. pushq_cfi $__KERNEL_DS
  1483. pushq_cfi %rdx
  1484. pushfq_cfi
  1485. pushq_cfi $__KERNEL_CS
  1486. pushq_cfi $repeat_nmi
  1487. /* Put stack back */
  1488. addq $(11*8), %rsp
  1489. CFI_ADJUST_CFA_OFFSET -11*8
  1490. nested_nmi_out:
  1491. popq_cfi %rdx
  1492. CFI_RESTORE rdx
  1493. /* No need to check faults here */
  1494. INTERRUPT_RETURN
  1495. CFI_RESTORE_STATE
  1496. first_nmi:
  1497. /*
  1498. * Because nested NMIs will use the pushed location that we
  1499. * stored in rdx, we must keep that space available.
  1500. * Here's what our stack frame will look like:
  1501. * +-------------------------+
  1502. * | original SS |
  1503. * | original Return RSP |
  1504. * | original RFLAGS |
  1505. * | original CS |
  1506. * | original RIP |
  1507. * +-------------------------+
  1508. * | temp storage for rdx |
  1509. * +-------------------------+
  1510. * | NMI executing variable |
  1511. * +-------------------------+
  1512. * | Saved SS |
  1513. * | Saved Return RSP |
  1514. * | Saved RFLAGS |
  1515. * | Saved CS |
  1516. * | Saved RIP |
  1517. * +-------------------------+
  1518. * | copied SS |
  1519. * | copied Return RSP |
  1520. * | copied RFLAGS |
  1521. * | copied CS |
  1522. * | copied RIP |
  1523. * +-------------------------+
  1524. * | pt_regs |
  1525. * +-------------------------+
  1526. *
  1527. * The saved stack frame is used to fix up the copied stack frame
  1528. * that a nested NMI may change to make the interrupted NMI iret jump
  1529. * to the repeat_nmi. The original stack frame and the temp storage
  1530. * is also used by nested NMIs and can not be trusted on exit.
  1531. */
  1532. /* Do not pop rdx, nested NMIs will corrupt that part of the stack */
  1533. movq (%rsp), %rdx
  1534. CFI_RESTORE rdx
  1535. /* Set the NMI executing variable on the stack. */
  1536. pushq_cfi $1
  1537. /* Copy the stack frame to the Saved frame */
  1538. .rept 5
  1539. pushq_cfi 6*8(%rsp)
  1540. .endr
  1541. CFI_DEF_CFA_OFFSET SS+8-RIP
  1542. /* Everything up to here is safe from nested NMIs */
  1543. /*
  1544. * If there was a nested NMI, the first NMI's iret will return
  1545. * here. But NMIs are still enabled and we can take another
  1546. * nested NMI. The nested NMI checks the interrupted RIP to see
  1547. * if it is between repeat_nmi and end_repeat_nmi, and if so
  1548. * it will just return, as we are about to repeat an NMI anyway.
  1549. * This makes it safe to copy to the stack frame that a nested
  1550. * NMI will update.
  1551. */
  1552. repeat_nmi:
  1553. /*
  1554. * Update the stack variable to say we are still in NMI (the update
  1555. * is benign for the non-repeat case, where 1 was pushed just above
  1556. * to this very stack slot).
  1557. */
  1558. movq $1, 5*8(%rsp)
  1559. /* Make another copy, this one may be modified by nested NMIs */
  1560. .rept 5
  1561. pushq_cfi 4*8(%rsp)
  1562. .endr
  1563. CFI_DEF_CFA_OFFSET SS+8-RIP
  1564. end_repeat_nmi:
  1565. /*
  1566. * Everything below this point can be preempted by a nested
  1567. * NMI if the first NMI took an exception and reset our iret stack
  1568. * so that we repeat another NMI.
  1569. */
  1570. pushq_cfi $-1 /* ORIG_RAX: no syscall to restart */
  1571. subq $ORIG_RAX-R15, %rsp
  1572. CFI_ADJUST_CFA_OFFSET ORIG_RAX-R15
  1573. /*
  1574. * Use save_paranoid to handle SWAPGS, but no need to use paranoid_exit
  1575. * as we should not be calling schedule in NMI context.
  1576. * Even with normal interrupts enabled. An NMI should not be
  1577. * setting NEED_RESCHED or anything that normal interrupts and
  1578. * exceptions might do.
  1579. */
  1580. call save_paranoid
  1581. DEFAULT_FRAME 0
  1582. /*
  1583. * Save off the CR2 register. If we take a page fault in the NMI then
  1584. * it could corrupt the CR2 value. If the NMI preempts a page fault
  1585. * handler before it was able to read the CR2 register, and then the
  1586. * NMI itself takes a page fault, the page fault that was preempted
  1587. * will read the information from the NMI page fault and not the
  1588. * origin fault. Save it off and restore it if it changes.
  1589. * Use the r12 callee-saved register.
  1590. */
  1591. movq %cr2, %r12
  1592. /* paranoidentry do_nmi, 0; without TRACE_IRQS_OFF */
  1593. movq %rsp,%rdi
  1594. movq $-1,%rsi
  1595. call do_nmi
  1596. /* Did the NMI take a page fault? Restore cr2 if it did */
  1597. movq %cr2, %rcx
  1598. cmpq %rcx, %r12
  1599. je 1f
  1600. movq %r12, %cr2
  1601. 1:
  1602. testl %ebx,%ebx /* swapgs needed? */
  1603. jnz nmi_restore
  1604. nmi_swapgs:
  1605. SWAPGS_UNSAFE_STACK
  1606. nmi_restore:
  1607. RESTORE_ALL 8
  1608. /* Clear the NMI executing stack variable */
  1609. movq $0, 10*8(%rsp)
  1610. jmp irq_return
  1611. CFI_ENDPROC
  1612. END(nmi)
  1613. ENTRY(ignore_sysret)
  1614. CFI_STARTPROC
  1615. mov $-ENOSYS,%eax
  1616. sysret
  1617. CFI_ENDPROC
  1618. END(ignore_sysret)
  1619. /*
  1620. * End of kprobes section
  1621. */
  1622. .popsection