entry_64.S 46 KB

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