entry_64.S 42 KB

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