entry_64.S 47 KB

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