entry_64.S 46 KB

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