entry_64.S 46 KB

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