entry_64.S 44 KB

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