entry_32.S 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502
  1. /*
  2. *
  3. * Copyright (C) 1991, 1992 Linus Torvalds
  4. */
  5. /*
  6. * entry.S contains the system-call and fault low-level handling routines.
  7. * This also contains the timer-interrupt handler, as well as all interrupts
  8. * and faults that can result in a task-switch.
  9. *
  10. * NOTE: This code handles signal-recognition, which happens every time
  11. * after a timer-interrupt and after each system call.
  12. *
  13. * I changed all the .align's to 4 (16 byte alignment), as that's faster
  14. * on a 486.
  15. *
  16. * Stack layout in 'syscall_exit':
  17. * ptrace needs to have all regs on the stack.
  18. * if the order here is changed, it needs to be
  19. * updated in fork.c:copy_process, signal.c:do_signal,
  20. * ptrace.c and ptrace.h
  21. *
  22. * 0(%esp) - %ebx
  23. * 4(%esp) - %ecx
  24. * 8(%esp) - %edx
  25. * C(%esp) - %esi
  26. * 10(%esp) - %edi
  27. * 14(%esp) - %ebp
  28. * 18(%esp) - %eax
  29. * 1C(%esp) - %ds
  30. * 20(%esp) - %es
  31. * 24(%esp) - %fs
  32. * 28(%esp) - %gs saved iff !CONFIG_X86_32_LAZY_GS
  33. * 2C(%esp) - orig_eax
  34. * 30(%esp) - %eip
  35. * 34(%esp) - %cs
  36. * 38(%esp) - %eflags
  37. * 3C(%esp) - %oldesp
  38. * 40(%esp) - %oldss
  39. *
  40. * "current" is in register %ebx during any slow entries.
  41. */
  42. #include <linux/linkage.h>
  43. #include <linux/err.h>
  44. #include <asm/thread_info.h>
  45. #include <asm/irqflags.h>
  46. #include <asm/errno.h>
  47. #include <asm/segment.h>
  48. #include <asm/smp.h>
  49. #include <asm/page_types.h>
  50. #include <asm/percpu.h>
  51. #include <asm/dwarf2.h>
  52. #include <asm/processor-flags.h>
  53. #include <asm/ftrace.h>
  54. #include <asm/irq_vectors.h>
  55. #include <asm/cpufeature.h>
  56. #include <asm/alternative-asm.h>
  57. #include <asm/asm.h>
  58. #include <asm/smap.h>
  59. /* Avoid __ASSEMBLER__'ifying <linux/audit.h> just for this. */
  60. #include <linux/elf-em.h>
  61. #define AUDIT_ARCH_I386 (EM_386|__AUDIT_ARCH_LE)
  62. #define __AUDIT_ARCH_LE 0x40000000
  63. #ifndef CONFIG_AUDITSYSCALL
  64. #define sysenter_audit syscall_trace_entry
  65. #define sysexit_audit syscall_exit_work
  66. #endif
  67. .section .entry.text, "ax"
  68. /*
  69. * We use macros for low-level operations which need to be overridden
  70. * for paravirtualization. The following will never clobber any registers:
  71. * INTERRUPT_RETURN (aka. "iret")
  72. * GET_CR0_INTO_EAX (aka. "movl %cr0, %eax")
  73. * ENABLE_INTERRUPTS_SYSEXIT (aka "sti; sysexit").
  74. *
  75. * For DISABLE_INTERRUPTS/ENABLE_INTERRUPTS (aka "cli"/"sti"), you must
  76. * specify what registers can be overwritten (CLBR_NONE, CLBR_EAX/EDX/ECX/ANY).
  77. * Allowing a register to be clobbered can shrink the paravirt replacement
  78. * enough to patch inline, increasing performance.
  79. */
  80. #ifdef CONFIG_PREEMPT
  81. #define preempt_stop(clobbers) DISABLE_INTERRUPTS(clobbers); TRACE_IRQS_OFF
  82. #else
  83. #define preempt_stop(clobbers)
  84. #define resume_kernel restore_all
  85. #endif
  86. .macro TRACE_IRQS_IRET
  87. #ifdef CONFIG_TRACE_IRQFLAGS
  88. testl $X86_EFLAGS_IF,PT_EFLAGS(%esp) # interrupts off?
  89. jz 1f
  90. TRACE_IRQS_ON
  91. 1:
  92. #endif
  93. .endm
  94. /*
  95. * User gs save/restore
  96. *
  97. * %gs is used for userland TLS and kernel only uses it for stack
  98. * canary which is required to be at %gs:20 by gcc. Read the comment
  99. * at the top of stackprotector.h for more info.
  100. *
  101. * Local labels 98 and 99 are used.
  102. */
  103. #ifdef CONFIG_X86_32_LAZY_GS
  104. /* unfortunately push/pop can't be no-op */
  105. .macro PUSH_GS
  106. pushl_cfi $0
  107. .endm
  108. .macro POP_GS pop=0
  109. addl $(4 + \pop), %esp
  110. CFI_ADJUST_CFA_OFFSET -(4 + \pop)
  111. .endm
  112. .macro POP_GS_EX
  113. .endm
  114. /* all the rest are no-op */
  115. .macro PTGS_TO_GS
  116. .endm
  117. .macro PTGS_TO_GS_EX
  118. .endm
  119. .macro GS_TO_REG reg
  120. .endm
  121. .macro REG_TO_PTGS reg
  122. .endm
  123. .macro SET_KERNEL_GS reg
  124. .endm
  125. #else /* CONFIG_X86_32_LAZY_GS */
  126. .macro PUSH_GS
  127. pushl_cfi %gs
  128. /*CFI_REL_OFFSET gs, 0*/
  129. .endm
  130. .macro POP_GS pop=0
  131. 98: popl_cfi %gs
  132. /*CFI_RESTORE gs*/
  133. .if \pop <> 0
  134. add $\pop, %esp
  135. CFI_ADJUST_CFA_OFFSET -\pop
  136. .endif
  137. .endm
  138. .macro POP_GS_EX
  139. .pushsection .fixup, "ax"
  140. 99: movl $0, (%esp)
  141. jmp 98b
  142. .popsection
  143. _ASM_EXTABLE(98b,99b)
  144. .endm
  145. .macro PTGS_TO_GS
  146. 98: mov PT_GS(%esp), %gs
  147. .endm
  148. .macro PTGS_TO_GS_EX
  149. .pushsection .fixup, "ax"
  150. 99: movl $0, PT_GS(%esp)
  151. jmp 98b
  152. .popsection
  153. _ASM_EXTABLE(98b,99b)
  154. .endm
  155. .macro GS_TO_REG reg
  156. movl %gs, \reg
  157. /*CFI_REGISTER gs, \reg*/
  158. .endm
  159. .macro REG_TO_PTGS reg
  160. movl \reg, PT_GS(%esp)
  161. /*CFI_REL_OFFSET gs, PT_GS*/
  162. .endm
  163. .macro SET_KERNEL_GS reg
  164. movl $(__KERNEL_STACK_CANARY), \reg
  165. movl \reg, %gs
  166. .endm
  167. #endif /* CONFIG_X86_32_LAZY_GS */
  168. .macro SAVE_ALL
  169. cld
  170. PUSH_GS
  171. pushl_cfi %fs
  172. /*CFI_REL_OFFSET fs, 0;*/
  173. pushl_cfi %es
  174. /*CFI_REL_OFFSET es, 0;*/
  175. pushl_cfi %ds
  176. /*CFI_REL_OFFSET ds, 0;*/
  177. pushl_cfi %eax
  178. CFI_REL_OFFSET eax, 0
  179. pushl_cfi %ebp
  180. CFI_REL_OFFSET ebp, 0
  181. pushl_cfi %edi
  182. CFI_REL_OFFSET edi, 0
  183. pushl_cfi %esi
  184. CFI_REL_OFFSET esi, 0
  185. pushl_cfi %edx
  186. CFI_REL_OFFSET edx, 0
  187. pushl_cfi %ecx
  188. CFI_REL_OFFSET ecx, 0
  189. pushl_cfi %ebx
  190. CFI_REL_OFFSET ebx, 0
  191. movl $(__USER_DS), %edx
  192. movl %edx, %ds
  193. movl %edx, %es
  194. movl $(__KERNEL_PERCPU), %edx
  195. movl %edx, %fs
  196. SET_KERNEL_GS %edx
  197. .endm
  198. .macro RESTORE_INT_REGS
  199. popl_cfi %ebx
  200. CFI_RESTORE ebx
  201. popl_cfi %ecx
  202. CFI_RESTORE ecx
  203. popl_cfi %edx
  204. CFI_RESTORE edx
  205. popl_cfi %esi
  206. CFI_RESTORE esi
  207. popl_cfi %edi
  208. CFI_RESTORE edi
  209. popl_cfi %ebp
  210. CFI_RESTORE ebp
  211. popl_cfi %eax
  212. CFI_RESTORE eax
  213. .endm
  214. .macro RESTORE_REGS pop=0
  215. RESTORE_INT_REGS
  216. 1: popl_cfi %ds
  217. /*CFI_RESTORE ds;*/
  218. 2: popl_cfi %es
  219. /*CFI_RESTORE es;*/
  220. 3: popl_cfi %fs
  221. /*CFI_RESTORE fs;*/
  222. POP_GS \pop
  223. .pushsection .fixup, "ax"
  224. 4: movl $0, (%esp)
  225. jmp 1b
  226. 5: movl $0, (%esp)
  227. jmp 2b
  228. 6: movl $0, (%esp)
  229. jmp 3b
  230. .popsection
  231. _ASM_EXTABLE(1b,4b)
  232. _ASM_EXTABLE(2b,5b)
  233. _ASM_EXTABLE(3b,6b)
  234. POP_GS_EX
  235. .endm
  236. .macro RING0_INT_FRAME
  237. CFI_STARTPROC simple
  238. CFI_SIGNAL_FRAME
  239. CFI_DEF_CFA esp, 3*4
  240. /*CFI_OFFSET cs, -2*4;*/
  241. CFI_OFFSET eip, -3*4
  242. .endm
  243. .macro RING0_EC_FRAME
  244. CFI_STARTPROC simple
  245. CFI_SIGNAL_FRAME
  246. CFI_DEF_CFA esp, 4*4
  247. /*CFI_OFFSET cs, -2*4;*/
  248. CFI_OFFSET eip, -3*4
  249. .endm
  250. .macro RING0_PTREGS_FRAME
  251. CFI_STARTPROC simple
  252. CFI_SIGNAL_FRAME
  253. CFI_DEF_CFA esp, PT_OLDESP-PT_EBX
  254. /*CFI_OFFSET cs, PT_CS-PT_OLDESP;*/
  255. CFI_OFFSET eip, PT_EIP-PT_OLDESP
  256. /*CFI_OFFSET es, PT_ES-PT_OLDESP;*/
  257. /*CFI_OFFSET ds, PT_DS-PT_OLDESP;*/
  258. CFI_OFFSET eax, PT_EAX-PT_OLDESP
  259. CFI_OFFSET ebp, PT_EBP-PT_OLDESP
  260. CFI_OFFSET edi, PT_EDI-PT_OLDESP
  261. CFI_OFFSET esi, PT_ESI-PT_OLDESP
  262. CFI_OFFSET edx, PT_EDX-PT_OLDESP
  263. CFI_OFFSET ecx, PT_ECX-PT_OLDESP
  264. CFI_OFFSET ebx, PT_EBX-PT_OLDESP
  265. .endm
  266. ENTRY(ret_from_fork)
  267. CFI_STARTPROC
  268. pushl_cfi %eax
  269. call schedule_tail
  270. GET_THREAD_INFO(%ebp)
  271. popl_cfi %eax
  272. pushl_cfi $0x0202 # Reset kernel eflags
  273. popfl_cfi
  274. jmp syscall_exit
  275. CFI_ENDPROC
  276. END(ret_from_fork)
  277. ENTRY(ret_from_kernel_execve)
  278. movl %eax, %esp
  279. movl $0,PT_EAX(%esp)
  280. GET_THREAD_INFO(%ebp)
  281. jmp syscall_exit
  282. END(ret_from_kernel_execve)
  283. /*
  284. * Interrupt exit functions should be protected against kprobes
  285. */
  286. .pushsection .kprobes.text, "ax"
  287. /*
  288. * Return to user mode is not as complex as all this looks,
  289. * but we want the default path for a system call return to
  290. * go as quickly as possible which is why some of this is
  291. * less clear than it otherwise should be.
  292. */
  293. # userspace resumption stub bypassing syscall exit tracing
  294. ALIGN
  295. RING0_PTREGS_FRAME
  296. ret_from_exception:
  297. preempt_stop(CLBR_ANY)
  298. ret_from_intr:
  299. GET_THREAD_INFO(%ebp)
  300. #ifdef CONFIG_VM86
  301. movl PT_EFLAGS(%esp), %eax # mix EFLAGS and CS
  302. movb PT_CS(%esp), %al
  303. andl $(X86_EFLAGS_VM | SEGMENT_RPL_MASK), %eax
  304. #else
  305. /*
  306. * We can be coming here from child spawned by kernel_thread().
  307. */
  308. movl PT_CS(%esp), %eax
  309. andl $SEGMENT_RPL_MASK, %eax
  310. #endif
  311. cmpl $USER_RPL, %eax
  312. jb resume_kernel # not returning to v8086 or userspace
  313. ENTRY(resume_userspace)
  314. LOCKDEP_SYS_EXIT
  315. DISABLE_INTERRUPTS(CLBR_ANY) # make sure we don't miss an interrupt
  316. # setting need_resched or sigpending
  317. # between sampling and the iret
  318. TRACE_IRQS_OFF
  319. movl TI_flags(%ebp), %ecx
  320. andl $_TIF_WORK_MASK, %ecx # is there any work to be done on
  321. # int/exception return?
  322. jne work_pending
  323. jmp restore_all
  324. END(ret_from_exception)
  325. #ifdef CONFIG_PREEMPT
  326. ENTRY(resume_kernel)
  327. DISABLE_INTERRUPTS(CLBR_ANY)
  328. cmpl $0,TI_preempt_count(%ebp) # non-zero preempt_count ?
  329. jnz restore_all
  330. need_resched:
  331. movl TI_flags(%ebp), %ecx # need_resched set ?
  332. testb $_TIF_NEED_RESCHED, %cl
  333. jz restore_all
  334. testl $X86_EFLAGS_IF,PT_EFLAGS(%esp) # interrupts off (exception path) ?
  335. jz restore_all
  336. call preempt_schedule_irq
  337. jmp need_resched
  338. END(resume_kernel)
  339. #endif
  340. CFI_ENDPROC
  341. /*
  342. * End of kprobes section
  343. */
  344. .popsection
  345. /* SYSENTER_RETURN points to after the "sysenter" instruction in
  346. the vsyscall page. See vsyscall-sysentry.S, which defines the symbol. */
  347. # sysenter call handler stub
  348. ENTRY(ia32_sysenter_target)
  349. CFI_STARTPROC simple
  350. CFI_SIGNAL_FRAME
  351. CFI_DEF_CFA esp, 0
  352. CFI_REGISTER esp, ebp
  353. movl TSS_sysenter_sp0(%esp),%esp
  354. sysenter_past_esp:
  355. /*
  356. * Interrupts are disabled here, but we can't trace it until
  357. * enough kernel state to call TRACE_IRQS_OFF can be called - but
  358. * we immediately enable interrupts at that point anyway.
  359. */
  360. pushl_cfi $__USER_DS
  361. /*CFI_REL_OFFSET ss, 0*/
  362. pushl_cfi %ebp
  363. CFI_REL_OFFSET esp, 0
  364. pushfl_cfi
  365. orl $X86_EFLAGS_IF, (%esp)
  366. pushl_cfi $__USER_CS
  367. /*CFI_REL_OFFSET cs, 0*/
  368. /*
  369. * Push current_thread_info()->sysenter_return to the stack.
  370. * A tiny bit of offset fixup is necessary - 4*4 means the 4 words
  371. * pushed above; +8 corresponds to copy_thread's esp0 setting.
  372. */
  373. pushl_cfi ((TI_sysenter_return)-THREAD_SIZE+8+4*4)(%esp)
  374. CFI_REL_OFFSET eip, 0
  375. pushl_cfi %eax
  376. SAVE_ALL
  377. ENABLE_INTERRUPTS(CLBR_NONE)
  378. /*
  379. * Load the potential sixth argument from user stack.
  380. * Careful about security.
  381. */
  382. cmpl $__PAGE_OFFSET-3,%ebp
  383. jae syscall_fault
  384. ASM_STAC
  385. 1: movl (%ebp),%ebp
  386. ASM_CLAC
  387. movl %ebp,PT_EBP(%esp)
  388. _ASM_EXTABLE(1b,syscall_fault)
  389. GET_THREAD_INFO(%ebp)
  390. testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
  391. jnz sysenter_audit
  392. sysenter_do_call:
  393. cmpl $(NR_syscalls), %eax
  394. jae syscall_badsys
  395. call *sys_call_table(,%eax,4)
  396. movl %eax,PT_EAX(%esp)
  397. LOCKDEP_SYS_EXIT
  398. DISABLE_INTERRUPTS(CLBR_ANY)
  399. TRACE_IRQS_OFF
  400. movl TI_flags(%ebp), %ecx
  401. testl $_TIF_ALLWORK_MASK, %ecx
  402. jne sysexit_audit
  403. sysenter_exit:
  404. /* if something modifies registers it must also disable sysexit */
  405. movl PT_EIP(%esp), %edx
  406. movl PT_OLDESP(%esp), %ecx
  407. xorl %ebp,%ebp
  408. TRACE_IRQS_ON
  409. 1: mov PT_FS(%esp), %fs
  410. PTGS_TO_GS
  411. ENABLE_INTERRUPTS_SYSEXIT
  412. #ifdef CONFIG_AUDITSYSCALL
  413. sysenter_audit:
  414. testl $(_TIF_WORK_SYSCALL_ENTRY & ~_TIF_SYSCALL_AUDIT),TI_flags(%ebp)
  415. jnz syscall_trace_entry
  416. addl $4,%esp
  417. CFI_ADJUST_CFA_OFFSET -4
  418. /* %esi already in 8(%esp) 6th arg: 4th syscall arg */
  419. /* %edx already in 4(%esp) 5th arg: 3rd syscall arg */
  420. /* %ecx already in 0(%esp) 4th arg: 2nd syscall arg */
  421. movl %ebx,%ecx /* 3rd arg: 1st syscall arg */
  422. movl %eax,%edx /* 2nd arg: syscall number */
  423. movl $AUDIT_ARCH_I386,%eax /* 1st arg: audit arch */
  424. call __audit_syscall_entry
  425. pushl_cfi %ebx
  426. movl PT_EAX(%esp),%eax /* reload syscall number */
  427. jmp sysenter_do_call
  428. sysexit_audit:
  429. testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
  430. jne syscall_exit_work
  431. TRACE_IRQS_ON
  432. ENABLE_INTERRUPTS(CLBR_ANY)
  433. movl %eax,%edx /* second arg, syscall return value */
  434. cmpl $-MAX_ERRNO,%eax /* is it an error ? */
  435. setbe %al /* 1 if so, 0 if not */
  436. movzbl %al,%eax /* zero-extend that */
  437. call __audit_syscall_exit
  438. DISABLE_INTERRUPTS(CLBR_ANY)
  439. TRACE_IRQS_OFF
  440. movl TI_flags(%ebp), %ecx
  441. testl $(_TIF_ALLWORK_MASK & ~_TIF_SYSCALL_AUDIT), %ecx
  442. jne syscall_exit_work
  443. movl PT_EAX(%esp),%eax /* reload syscall return value */
  444. jmp sysenter_exit
  445. #endif
  446. CFI_ENDPROC
  447. .pushsection .fixup,"ax"
  448. 2: movl $0,PT_FS(%esp)
  449. jmp 1b
  450. .popsection
  451. _ASM_EXTABLE(1b,2b)
  452. PTGS_TO_GS_EX
  453. ENDPROC(ia32_sysenter_target)
  454. /*
  455. * syscall stub including irq exit should be protected against kprobes
  456. */
  457. .pushsection .kprobes.text, "ax"
  458. # system call handler stub
  459. ENTRY(system_call)
  460. RING0_INT_FRAME # can't unwind into user space anyway
  461. ASM_CLAC
  462. pushl_cfi %eax # save orig_eax
  463. SAVE_ALL
  464. GET_THREAD_INFO(%ebp)
  465. # system call tracing in operation / emulation
  466. testl $_TIF_WORK_SYSCALL_ENTRY,TI_flags(%ebp)
  467. jnz syscall_trace_entry
  468. cmpl $(NR_syscalls), %eax
  469. jae syscall_badsys
  470. syscall_call:
  471. call *sys_call_table(,%eax,4)
  472. movl %eax,PT_EAX(%esp) # store the return value
  473. syscall_exit:
  474. LOCKDEP_SYS_EXIT
  475. DISABLE_INTERRUPTS(CLBR_ANY) # make sure we don't miss an interrupt
  476. # setting need_resched or sigpending
  477. # between sampling and the iret
  478. TRACE_IRQS_OFF
  479. movl TI_flags(%ebp), %ecx
  480. testl $_TIF_ALLWORK_MASK, %ecx # current->work
  481. jne syscall_exit_work
  482. restore_all:
  483. TRACE_IRQS_IRET
  484. restore_all_notrace:
  485. movl PT_EFLAGS(%esp), %eax # mix EFLAGS, SS and CS
  486. # Warning: PT_OLDSS(%esp) contains the wrong/random values if we
  487. # are returning to the kernel.
  488. # See comments in process.c:copy_thread() for details.
  489. movb PT_OLDSS(%esp), %ah
  490. movb PT_CS(%esp), %al
  491. andl $(X86_EFLAGS_VM | (SEGMENT_TI_MASK << 8) | SEGMENT_RPL_MASK), %eax
  492. cmpl $((SEGMENT_LDT << 8) | USER_RPL), %eax
  493. CFI_REMEMBER_STATE
  494. je ldt_ss # returning to user-space with LDT SS
  495. restore_nocheck:
  496. RESTORE_REGS 4 # skip orig_eax/error_code
  497. irq_return:
  498. INTERRUPT_RETURN
  499. .section .fixup,"ax"
  500. ENTRY(iret_exc)
  501. pushl $0 # no error code
  502. pushl $do_iret_error
  503. jmp error_code
  504. .previous
  505. _ASM_EXTABLE(irq_return,iret_exc)
  506. CFI_RESTORE_STATE
  507. ldt_ss:
  508. larl PT_OLDSS(%esp), %eax
  509. jnz restore_nocheck
  510. testl $0x00400000, %eax # returning to 32bit stack?
  511. jnz restore_nocheck # allright, normal return
  512. #ifdef CONFIG_PARAVIRT
  513. /*
  514. * The kernel can't run on a non-flat stack if paravirt mode
  515. * is active. Rather than try to fixup the high bits of
  516. * ESP, bypass this code entirely. This may break DOSemu
  517. * and/or Wine support in a paravirt VM, although the option
  518. * is still available to implement the setting of the high
  519. * 16-bits in the INTERRUPT_RETURN paravirt-op.
  520. */
  521. cmpl $0, pv_info+PARAVIRT_enabled
  522. jne restore_nocheck
  523. #endif
  524. /*
  525. * Setup and switch to ESPFIX stack
  526. *
  527. * We're returning to userspace with a 16 bit stack. The CPU will not
  528. * restore the high word of ESP for us on executing iret... This is an
  529. * "official" bug of all the x86-compatible CPUs, which we can work
  530. * around to make dosemu and wine happy. We do this by preloading the
  531. * high word of ESP with the high word of the userspace ESP while
  532. * compensating for the offset by changing to the ESPFIX segment with
  533. * a base address that matches for the difference.
  534. */
  535. #define GDT_ESPFIX_SS PER_CPU_VAR(gdt_page) + (GDT_ENTRY_ESPFIX_SS * 8)
  536. mov %esp, %edx /* load kernel esp */
  537. mov PT_OLDESP(%esp), %eax /* load userspace esp */
  538. mov %dx, %ax /* eax: new kernel esp */
  539. sub %eax, %edx /* offset (low word is 0) */
  540. shr $16, %edx
  541. mov %dl, GDT_ESPFIX_SS + 4 /* bits 16..23 */
  542. mov %dh, GDT_ESPFIX_SS + 7 /* bits 24..31 */
  543. pushl_cfi $__ESPFIX_SS
  544. pushl_cfi %eax /* new kernel esp */
  545. /* Disable interrupts, but do not irqtrace this section: we
  546. * will soon execute iret and the tracer was already set to
  547. * the irqstate after the iret */
  548. DISABLE_INTERRUPTS(CLBR_EAX)
  549. lss (%esp), %esp /* switch to espfix segment */
  550. CFI_ADJUST_CFA_OFFSET -8
  551. jmp restore_nocheck
  552. CFI_ENDPROC
  553. ENDPROC(system_call)
  554. # perform work that needs to be done immediately before resumption
  555. ALIGN
  556. RING0_PTREGS_FRAME # can't unwind into user space anyway
  557. work_pending:
  558. testb $_TIF_NEED_RESCHED, %cl
  559. jz work_notifysig
  560. work_resched:
  561. call schedule
  562. LOCKDEP_SYS_EXIT
  563. DISABLE_INTERRUPTS(CLBR_ANY) # make sure we don't miss an interrupt
  564. # setting need_resched or sigpending
  565. # between sampling and the iret
  566. TRACE_IRQS_OFF
  567. movl TI_flags(%ebp), %ecx
  568. andl $_TIF_WORK_MASK, %ecx # is there any work to be done other
  569. # than syscall tracing?
  570. jz restore_all
  571. testb $_TIF_NEED_RESCHED, %cl
  572. jnz work_resched
  573. work_notifysig: # deal with pending signals and
  574. # notify-resume requests
  575. #ifdef CONFIG_VM86
  576. testl $X86_EFLAGS_VM, PT_EFLAGS(%esp)
  577. movl %esp, %eax
  578. jne work_notifysig_v86 # returning to kernel-space or
  579. # vm86-space
  580. 1:
  581. #else
  582. movl %esp, %eax
  583. #endif
  584. TRACE_IRQS_ON
  585. ENABLE_INTERRUPTS(CLBR_NONE)
  586. movb PT_CS(%esp), %bl
  587. andb $SEGMENT_RPL_MASK, %bl
  588. cmpb $USER_RPL, %bl
  589. jb resume_kernel
  590. xorl %edx, %edx
  591. call do_notify_resume
  592. jmp resume_userspace
  593. #ifdef CONFIG_VM86
  594. ALIGN
  595. work_notifysig_v86:
  596. pushl_cfi %ecx # save ti_flags for do_notify_resume
  597. call save_v86_state # %eax contains pt_regs pointer
  598. popl_cfi %ecx
  599. movl %eax, %esp
  600. jmp 1b
  601. #endif
  602. END(work_pending)
  603. # perform syscall exit tracing
  604. ALIGN
  605. syscall_trace_entry:
  606. movl $-ENOSYS,PT_EAX(%esp)
  607. movl %esp, %eax
  608. call syscall_trace_enter
  609. /* What it returned is what we'll actually use. */
  610. cmpl $(NR_syscalls), %eax
  611. jnae syscall_call
  612. jmp syscall_exit
  613. END(syscall_trace_entry)
  614. # perform syscall exit tracing
  615. ALIGN
  616. syscall_exit_work:
  617. testl $_TIF_WORK_SYSCALL_EXIT, %ecx
  618. jz work_pending
  619. TRACE_IRQS_ON
  620. ENABLE_INTERRUPTS(CLBR_ANY) # could let syscall_trace_leave() call
  621. # schedule() instead
  622. movl %esp, %eax
  623. call syscall_trace_leave
  624. jmp resume_userspace
  625. END(syscall_exit_work)
  626. CFI_ENDPROC
  627. RING0_INT_FRAME # can't unwind into user space anyway
  628. syscall_fault:
  629. ASM_CLAC
  630. GET_THREAD_INFO(%ebp)
  631. movl $-EFAULT,PT_EAX(%esp)
  632. jmp resume_userspace
  633. END(syscall_fault)
  634. syscall_badsys:
  635. movl $-ENOSYS,PT_EAX(%esp)
  636. jmp resume_userspace
  637. END(syscall_badsys)
  638. CFI_ENDPROC
  639. /*
  640. * End of kprobes section
  641. */
  642. .popsection
  643. /*
  644. * System calls that need a pt_regs pointer.
  645. */
  646. #define PTREGSCALL0(name) \
  647. ENTRY(ptregs_##name) ; \
  648. leal 4(%esp),%eax; \
  649. jmp sys_##name; \
  650. ENDPROC(ptregs_##name)
  651. #define PTREGSCALL1(name) \
  652. ENTRY(ptregs_##name) ; \
  653. leal 4(%esp),%edx; \
  654. movl (PT_EBX+4)(%esp),%eax; \
  655. jmp sys_##name; \
  656. ENDPROC(ptregs_##name)
  657. #define PTREGSCALL2(name) \
  658. ENTRY(ptregs_##name) ; \
  659. leal 4(%esp),%ecx; \
  660. movl (PT_ECX+4)(%esp),%edx; \
  661. movl (PT_EBX+4)(%esp),%eax; \
  662. jmp sys_##name; \
  663. ENDPROC(ptregs_##name)
  664. #define PTREGSCALL3(name) \
  665. ENTRY(ptregs_##name) ; \
  666. CFI_STARTPROC; \
  667. leal 4(%esp),%eax; \
  668. pushl_cfi %eax; \
  669. movl PT_EDX(%eax),%ecx; \
  670. movl PT_ECX(%eax),%edx; \
  671. movl PT_EBX(%eax),%eax; \
  672. call sys_##name; \
  673. addl $4,%esp; \
  674. CFI_ADJUST_CFA_OFFSET -4; \
  675. ret; \
  676. CFI_ENDPROC; \
  677. ENDPROC(ptregs_##name)
  678. PTREGSCALL1(iopl)
  679. PTREGSCALL0(fork)
  680. PTREGSCALL0(vfork)
  681. PTREGSCALL2(sigaltstack)
  682. PTREGSCALL0(sigreturn)
  683. PTREGSCALL0(rt_sigreturn)
  684. PTREGSCALL2(vm86)
  685. PTREGSCALL1(vm86old)
  686. /* Clone is an oddball. The 4th arg is in %edi */
  687. ENTRY(ptregs_clone)
  688. CFI_STARTPROC
  689. leal 4(%esp),%eax
  690. pushl_cfi %eax
  691. pushl_cfi PT_EDI(%eax)
  692. movl PT_EDX(%eax),%ecx
  693. movl PT_ECX(%eax),%edx
  694. movl PT_EBX(%eax),%eax
  695. call sys_clone
  696. addl $8,%esp
  697. CFI_ADJUST_CFA_OFFSET -8
  698. ret
  699. CFI_ENDPROC
  700. ENDPROC(ptregs_clone)
  701. .macro FIXUP_ESPFIX_STACK
  702. /*
  703. * Switch back for ESPFIX stack to the normal zerobased stack
  704. *
  705. * We can't call C functions using the ESPFIX stack. This code reads
  706. * the high word of the segment base from the GDT and swiches to the
  707. * normal stack and adjusts ESP with the matching offset.
  708. */
  709. /* fixup the stack */
  710. mov GDT_ESPFIX_SS + 4, %al /* bits 16..23 */
  711. mov GDT_ESPFIX_SS + 7, %ah /* bits 24..31 */
  712. shl $16, %eax
  713. addl %esp, %eax /* the adjusted stack pointer */
  714. pushl_cfi $__KERNEL_DS
  715. pushl_cfi %eax
  716. lss (%esp), %esp /* switch to the normal stack segment */
  717. CFI_ADJUST_CFA_OFFSET -8
  718. .endm
  719. .macro UNWIND_ESPFIX_STACK
  720. movl %ss, %eax
  721. /* see if on espfix stack */
  722. cmpw $__ESPFIX_SS, %ax
  723. jne 27f
  724. movl $__KERNEL_DS, %eax
  725. movl %eax, %ds
  726. movl %eax, %es
  727. /* switch to normal stack */
  728. FIXUP_ESPFIX_STACK
  729. 27:
  730. .endm
  731. /*
  732. * Build the entry stubs and pointer table with some assembler magic.
  733. * We pack 7 stubs into a single 32-byte chunk, which will fit in a
  734. * single cache line on all modern x86 implementations.
  735. */
  736. .section .init.rodata,"a"
  737. ENTRY(interrupt)
  738. .section .entry.text, "ax"
  739. .p2align 5
  740. .p2align CONFIG_X86_L1_CACHE_SHIFT
  741. ENTRY(irq_entries_start)
  742. RING0_INT_FRAME
  743. vector=FIRST_EXTERNAL_VECTOR
  744. .rept (NR_VECTORS-FIRST_EXTERNAL_VECTOR+6)/7
  745. .balign 32
  746. .rept 7
  747. .if vector < NR_VECTORS
  748. .if vector <> FIRST_EXTERNAL_VECTOR
  749. CFI_ADJUST_CFA_OFFSET -4
  750. .endif
  751. 1: pushl_cfi $(~vector+0x80) /* Note: always in signed byte range */
  752. .if ((vector-FIRST_EXTERNAL_VECTOR)%7) <> 6
  753. jmp 2f
  754. .endif
  755. .previous
  756. .long 1b
  757. .section .entry.text, "ax"
  758. vector=vector+1
  759. .endif
  760. .endr
  761. 2: jmp common_interrupt
  762. .endr
  763. END(irq_entries_start)
  764. .previous
  765. END(interrupt)
  766. .previous
  767. /*
  768. * the CPU automatically disables interrupts when executing an IRQ vector,
  769. * so IRQ-flags tracing has to follow that:
  770. */
  771. .p2align CONFIG_X86_L1_CACHE_SHIFT
  772. common_interrupt:
  773. ASM_CLAC
  774. addl $-0x80,(%esp) /* Adjust vector into the [-256,-1] range */
  775. SAVE_ALL
  776. TRACE_IRQS_OFF
  777. movl %esp,%eax
  778. call do_IRQ
  779. jmp ret_from_intr
  780. ENDPROC(common_interrupt)
  781. CFI_ENDPROC
  782. /*
  783. * Irq entries should be protected against kprobes
  784. */
  785. .pushsection .kprobes.text, "ax"
  786. #define BUILD_INTERRUPT3(name, nr, fn) \
  787. ENTRY(name) \
  788. RING0_INT_FRAME; \
  789. ASM_CLAC; \
  790. pushl_cfi $~(nr); \
  791. SAVE_ALL; \
  792. TRACE_IRQS_OFF \
  793. movl %esp,%eax; \
  794. call fn; \
  795. jmp ret_from_intr; \
  796. CFI_ENDPROC; \
  797. ENDPROC(name)
  798. #define BUILD_INTERRUPT(name, nr) BUILD_INTERRUPT3(name, nr, smp_##name)
  799. /* The include is where all of the SMP etc. interrupts come from */
  800. #include <asm/entry_arch.h>
  801. ENTRY(coprocessor_error)
  802. RING0_INT_FRAME
  803. ASM_CLAC
  804. pushl_cfi $0
  805. pushl_cfi $do_coprocessor_error
  806. jmp error_code
  807. CFI_ENDPROC
  808. END(coprocessor_error)
  809. ENTRY(simd_coprocessor_error)
  810. RING0_INT_FRAME
  811. ASM_CLAC
  812. pushl_cfi $0
  813. #ifdef CONFIG_X86_INVD_BUG
  814. /* AMD 486 bug: invd from userspace calls exception 19 instead of #GP */
  815. 661: pushl_cfi $do_general_protection
  816. 662:
  817. .section .altinstructions,"a"
  818. altinstruction_entry 661b, 663f, X86_FEATURE_XMM, 662b-661b, 664f-663f
  819. .previous
  820. .section .altinstr_replacement,"ax"
  821. 663: pushl $do_simd_coprocessor_error
  822. 664:
  823. .previous
  824. #else
  825. pushl_cfi $do_simd_coprocessor_error
  826. #endif
  827. jmp error_code
  828. CFI_ENDPROC
  829. END(simd_coprocessor_error)
  830. ENTRY(device_not_available)
  831. RING0_INT_FRAME
  832. ASM_CLAC
  833. pushl_cfi $-1 # mark this as an int
  834. pushl_cfi $do_device_not_available
  835. jmp error_code
  836. CFI_ENDPROC
  837. END(device_not_available)
  838. #ifdef CONFIG_PARAVIRT
  839. ENTRY(native_iret)
  840. iret
  841. _ASM_EXTABLE(native_iret, iret_exc)
  842. END(native_iret)
  843. ENTRY(native_irq_enable_sysexit)
  844. sti
  845. sysexit
  846. END(native_irq_enable_sysexit)
  847. #endif
  848. ENTRY(overflow)
  849. RING0_INT_FRAME
  850. ASM_CLAC
  851. pushl_cfi $0
  852. pushl_cfi $do_overflow
  853. jmp error_code
  854. CFI_ENDPROC
  855. END(overflow)
  856. ENTRY(bounds)
  857. RING0_INT_FRAME
  858. ASM_CLAC
  859. pushl_cfi $0
  860. pushl_cfi $do_bounds
  861. jmp error_code
  862. CFI_ENDPROC
  863. END(bounds)
  864. ENTRY(invalid_op)
  865. RING0_INT_FRAME
  866. ASM_CLAC
  867. pushl_cfi $0
  868. pushl_cfi $do_invalid_op
  869. jmp error_code
  870. CFI_ENDPROC
  871. END(invalid_op)
  872. ENTRY(coprocessor_segment_overrun)
  873. RING0_INT_FRAME
  874. ASM_CLAC
  875. pushl_cfi $0
  876. pushl_cfi $do_coprocessor_segment_overrun
  877. jmp error_code
  878. CFI_ENDPROC
  879. END(coprocessor_segment_overrun)
  880. ENTRY(invalid_TSS)
  881. RING0_EC_FRAME
  882. ASM_CLAC
  883. pushl_cfi $do_invalid_TSS
  884. jmp error_code
  885. CFI_ENDPROC
  886. END(invalid_TSS)
  887. ENTRY(segment_not_present)
  888. RING0_EC_FRAME
  889. ASM_CLAC
  890. pushl_cfi $do_segment_not_present
  891. jmp error_code
  892. CFI_ENDPROC
  893. END(segment_not_present)
  894. ENTRY(stack_segment)
  895. RING0_EC_FRAME
  896. ASM_CLAC
  897. pushl_cfi $do_stack_segment
  898. jmp error_code
  899. CFI_ENDPROC
  900. END(stack_segment)
  901. ENTRY(alignment_check)
  902. RING0_EC_FRAME
  903. ASM_CLAC
  904. pushl_cfi $do_alignment_check
  905. jmp error_code
  906. CFI_ENDPROC
  907. END(alignment_check)
  908. ENTRY(divide_error)
  909. RING0_INT_FRAME
  910. ASM_CLAC
  911. pushl_cfi $0 # no error code
  912. pushl_cfi $do_divide_error
  913. jmp error_code
  914. CFI_ENDPROC
  915. END(divide_error)
  916. #ifdef CONFIG_X86_MCE
  917. ENTRY(machine_check)
  918. RING0_INT_FRAME
  919. ASM_CLAC
  920. pushl_cfi $0
  921. pushl_cfi machine_check_vector
  922. jmp error_code
  923. CFI_ENDPROC
  924. END(machine_check)
  925. #endif
  926. ENTRY(spurious_interrupt_bug)
  927. RING0_INT_FRAME
  928. ASM_CLAC
  929. pushl_cfi $0
  930. pushl_cfi $do_spurious_interrupt_bug
  931. jmp error_code
  932. CFI_ENDPROC
  933. END(spurious_interrupt_bug)
  934. /*
  935. * End of kprobes section
  936. */
  937. .popsection
  938. ENTRY(ret_from_kernel_thread)
  939. CFI_STARTPROC
  940. pushl_cfi %eax
  941. call schedule_tail
  942. GET_THREAD_INFO(%ebp)
  943. popl_cfi %eax
  944. pushl_cfi $0x0202 # Reset kernel eflags
  945. popfl_cfi
  946. movl PT_EBP(%esp),%eax
  947. call *PT_EBX(%esp)
  948. call do_exit
  949. ud2 # padding for call trace
  950. CFI_ENDPROC
  951. ENDPROC(ret_from_kernel_thread)
  952. #ifdef CONFIG_XEN
  953. /* Xen doesn't set %esp to be precisely what the normal sysenter
  954. entrypoint expects, so fix it up before using the normal path. */
  955. ENTRY(xen_sysenter_target)
  956. RING0_INT_FRAME
  957. addl $5*4, %esp /* remove xen-provided frame */
  958. CFI_ADJUST_CFA_OFFSET -5*4
  959. jmp sysenter_past_esp
  960. CFI_ENDPROC
  961. ENTRY(xen_hypervisor_callback)
  962. CFI_STARTPROC
  963. pushl_cfi $0
  964. SAVE_ALL
  965. TRACE_IRQS_OFF
  966. /* Check to see if we got the event in the critical
  967. region in xen_iret_direct, after we've reenabled
  968. events and checked for pending events. This simulates
  969. iret instruction's behaviour where it delivers a
  970. pending interrupt when enabling interrupts. */
  971. movl PT_EIP(%esp),%eax
  972. cmpl $xen_iret_start_crit,%eax
  973. jb 1f
  974. cmpl $xen_iret_end_crit,%eax
  975. jae 1f
  976. jmp xen_iret_crit_fixup
  977. ENTRY(xen_do_upcall)
  978. 1: mov %esp, %eax
  979. call xen_evtchn_do_upcall
  980. jmp ret_from_intr
  981. CFI_ENDPROC
  982. ENDPROC(xen_hypervisor_callback)
  983. # Hypervisor uses this for application faults while it executes.
  984. # We get here for two reasons:
  985. # 1. Fault while reloading DS, ES, FS or GS
  986. # 2. Fault while executing IRET
  987. # Category 1 we fix up by reattempting the load, and zeroing the segment
  988. # register if the load fails.
  989. # Category 2 we fix up by jumping to do_iret_error. We cannot use the
  990. # normal Linux return path in this case because if we use the IRET hypercall
  991. # to pop the stack frame we end up in an infinite loop of failsafe callbacks.
  992. # We distinguish between categories by maintaining a status value in EAX.
  993. ENTRY(xen_failsafe_callback)
  994. CFI_STARTPROC
  995. pushl_cfi %eax
  996. movl $1,%eax
  997. 1: mov 4(%esp),%ds
  998. 2: mov 8(%esp),%es
  999. 3: mov 12(%esp),%fs
  1000. 4: mov 16(%esp),%gs
  1001. testl %eax,%eax
  1002. popl_cfi %eax
  1003. lea 16(%esp),%esp
  1004. CFI_ADJUST_CFA_OFFSET -16
  1005. jz 5f
  1006. addl $16,%esp
  1007. jmp iret_exc # EAX != 0 => Category 2 (Bad IRET)
  1008. 5: pushl_cfi $0 # EAX == 0 => Category 1 (Bad segment)
  1009. SAVE_ALL
  1010. jmp ret_from_exception
  1011. CFI_ENDPROC
  1012. .section .fixup,"ax"
  1013. 6: xorl %eax,%eax
  1014. movl %eax,4(%esp)
  1015. jmp 1b
  1016. 7: xorl %eax,%eax
  1017. movl %eax,8(%esp)
  1018. jmp 2b
  1019. 8: xorl %eax,%eax
  1020. movl %eax,12(%esp)
  1021. jmp 3b
  1022. 9: xorl %eax,%eax
  1023. movl %eax,16(%esp)
  1024. jmp 4b
  1025. .previous
  1026. _ASM_EXTABLE(1b,6b)
  1027. _ASM_EXTABLE(2b,7b)
  1028. _ASM_EXTABLE(3b,8b)
  1029. _ASM_EXTABLE(4b,9b)
  1030. ENDPROC(xen_failsafe_callback)
  1031. BUILD_INTERRUPT3(xen_hvm_callback_vector, XEN_HVM_EVTCHN_CALLBACK,
  1032. xen_evtchn_do_upcall)
  1033. #endif /* CONFIG_XEN */
  1034. #ifdef CONFIG_FUNCTION_TRACER
  1035. #ifdef CONFIG_DYNAMIC_FTRACE
  1036. ENTRY(mcount)
  1037. ret
  1038. END(mcount)
  1039. ENTRY(ftrace_caller)
  1040. cmpl $0, function_trace_stop
  1041. jne ftrace_stub
  1042. pushl %eax
  1043. pushl %ecx
  1044. pushl %edx
  1045. pushl $0 /* Pass NULL as regs pointer */
  1046. movl 4*4(%esp), %eax
  1047. movl 0x4(%ebp), %edx
  1048. leal function_trace_op, %ecx
  1049. subl $MCOUNT_INSN_SIZE, %eax
  1050. .globl ftrace_call
  1051. ftrace_call:
  1052. call ftrace_stub
  1053. addl $4,%esp /* skip NULL pointer */
  1054. popl %edx
  1055. popl %ecx
  1056. popl %eax
  1057. ftrace_ret:
  1058. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1059. .globl ftrace_graph_call
  1060. ftrace_graph_call:
  1061. jmp ftrace_stub
  1062. #endif
  1063. .globl ftrace_stub
  1064. ftrace_stub:
  1065. ret
  1066. END(ftrace_caller)
  1067. ENTRY(ftrace_regs_caller)
  1068. pushf /* push flags before compare (in cs location) */
  1069. cmpl $0, function_trace_stop
  1070. jne ftrace_restore_flags
  1071. /*
  1072. * i386 does not save SS and ESP when coming from kernel.
  1073. * Instead, to get sp, &regs->sp is used (see ptrace.h).
  1074. * Unfortunately, that means eflags must be at the same location
  1075. * as the current return ip is. We move the return ip into the
  1076. * ip location, and move flags into the return ip location.
  1077. */
  1078. pushl 4(%esp) /* save return ip into ip slot */
  1079. pushl $0 /* Load 0 into orig_ax */
  1080. pushl %gs
  1081. pushl %fs
  1082. pushl %es
  1083. pushl %ds
  1084. pushl %eax
  1085. pushl %ebp
  1086. pushl %edi
  1087. pushl %esi
  1088. pushl %edx
  1089. pushl %ecx
  1090. pushl %ebx
  1091. movl 13*4(%esp), %eax /* Get the saved flags */
  1092. movl %eax, 14*4(%esp) /* Move saved flags into regs->flags location */
  1093. /* clobbering return ip */
  1094. movl $__KERNEL_CS,13*4(%esp)
  1095. movl 12*4(%esp), %eax /* Load ip (1st parameter) */
  1096. subl $MCOUNT_INSN_SIZE, %eax /* Adjust ip */
  1097. movl 0x4(%ebp), %edx /* Load parent ip (2nd parameter) */
  1098. leal function_trace_op, %ecx /* Save ftrace_pos in 3rd parameter */
  1099. pushl %esp /* Save pt_regs as 4th parameter */
  1100. GLOBAL(ftrace_regs_call)
  1101. call ftrace_stub
  1102. addl $4, %esp /* Skip pt_regs */
  1103. movl 14*4(%esp), %eax /* Move flags back into cs */
  1104. movl %eax, 13*4(%esp) /* Needed to keep addl from modifying flags */
  1105. movl 12*4(%esp), %eax /* Get return ip from regs->ip */
  1106. movl %eax, 14*4(%esp) /* Put return ip back for ret */
  1107. popl %ebx
  1108. popl %ecx
  1109. popl %edx
  1110. popl %esi
  1111. popl %edi
  1112. popl %ebp
  1113. popl %eax
  1114. popl %ds
  1115. popl %es
  1116. popl %fs
  1117. popl %gs
  1118. addl $8, %esp /* Skip orig_ax and ip */
  1119. popf /* Pop flags at end (no addl to corrupt flags) */
  1120. jmp ftrace_ret
  1121. ftrace_restore_flags:
  1122. popf
  1123. jmp ftrace_stub
  1124. #else /* ! CONFIG_DYNAMIC_FTRACE */
  1125. ENTRY(mcount)
  1126. cmpl $0, function_trace_stop
  1127. jne ftrace_stub
  1128. cmpl $ftrace_stub, ftrace_trace_function
  1129. jnz trace
  1130. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1131. cmpl $ftrace_stub, ftrace_graph_return
  1132. jnz ftrace_graph_caller
  1133. cmpl $ftrace_graph_entry_stub, ftrace_graph_entry
  1134. jnz ftrace_graph_caller
  1135. #endif
  1136. .globl ftrace_stub
  1137. ftrace_stub:
  1138. ret
  1139. /* taken from glibc */
  1140. trace:
  1141. pushl %eax
  1142. pushl %ecx
  1143. pushl %edx
  1144. movl 0xc(%esp), %eax
  1145. movl 0x4(%ebp), %edx
  1146. subl $MCOUNT_INSN_SIZE, %eax
  1147. call *ftrace_trace_function
  1148. popl %edx
  1149. popl %ecx
  1150. popl %eax
  1151. jmp ftrace_stub
  1152. END(mcount)
  1153. #endif /* CONFIG_DYNAMIC_FTRACE */
  1154. #endif /* CONFIG_FUNCTION_TRACER */
  1155. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1156. ENTRY(ftrace_graph_caller)
  1157. pushl %eax
  1158. pushl %ecx
  1159. pushl %edx
  1160. movl 0xc(%esp), %edx
  1161. lea 0x4(%ebp), %eax
  1162. movl (%ebp), %ecx
  1163. subl $MCOUNT_INSN_SIZE, %edx
  1164. call prepare_ftrace_return
  1165. popl %edx
  1166. popl %ecx
  1167. popl %eax
  1168. ret
  1169. END(ftrace_graph_caller)
  1170. .globl return_to_handler
  1171. return_to_handler:
  1172. pushl %eax
  1173. pushl %edx
  1174. movl %ebp, %eax
  1175. call ftrace_return_to_handler
  1176. movl %eax, %ecx
  1177. popl %edx
  1178. popl %eax
  1179. jmp *%ecx
  1180. #endif
  1181. /*
  1182. * Some functions should be protected against kprobes
  1183. */
  1184. .pushsection .kprobes.text, "ax"
  1185. ENTRY(page_fault)
  1186. RING0_EC_FRAME
  1187. ASM_CLAC
  1188. pushl_cfi $do_page_fault
  1189. ALIGN
  1190. error_code:
  1191. /* the function address is in %gs's slot on the stack */
  1192. pushl_cfi %fs
  1193. /*CFI_REL_OFFSET fs, 0*/
  1194. pushl_cfi %es
  1195. /*CFI_REL_OFFSET es, 0*/
  1196. pushl_cfi %ds
  1197. /*CFI_REL_OFFSET ds, 0*/
  1198. pushl_cfi %eax
  1199. CFI_REL_OFFSET eax, 0
  1200. pushl_cfi %ebp
  1201. CFI_REL_OFFSET ebp, 0
  1202. pushl_cfi %edi
  1203. CFI_REL_OFFSET edi, 0
  1204. pushl_cfi %esi
  1205. CFI_REL_OFFSET esi, 0
  1206. pushl_cfi %edx
  1207. CFI_REL_OFFSET edx, 0
  1208. pushl_cfi %ecx
  1209. CFI_REL_OFFSET ecx, 0
  1210. pushl_cfi %ebx
  1211. CFI_REL_OFFSET ebx, 0
  1212. cld
  1213. movl $(__KERNEL_PERCPU), %ecx
  1214. movl %ecx, %fs
  1215. UNWIND_ESPFIX_STACK
  1216. GS_TO_REG %ecx
  1217. movl PT_GS(%esp), %edi # get the function address
  1218. movl PT_ORIG_EAX(%esp), %edx # get the error code
  1219. movl $-1, PT_ORIG_EAX(%esp) # no syscall to restart
  1220. REG_TO_PTGS %ecx
  1221. SET_KERNEL_GS %ecx
  1222. movl $(__USER_DS), %ecx
  1223. movl %ecx, %ds
  1224. movl %ecx, %es
  1225. TRACE_IRQS_OFF
  1226. movl %esp,%eax # pt_regs pointer
  1227. call *%edi
  1228. jmp ret_from_exception
  1229. CFI_ENDPROC
  1230. END(page_fault)
  1231. /*
  1232. * Debug traps and NMI can happen at the one SYSENTER instruction
  1233. * that sets up the real kernel stack. Check here, since we can't
  1234. * allow the wrong stack to be used.
  1235. *
  1236. * "TSS_sysenter_sp0+12" is because the NMI/debug handler will have
  1237. * already pushed 3 words if it hits on the sysenter instruction:
  1238. * eflags, cs and eip.
  1239. *
  1240. * We just load the right stack, and push the three (known) values
  1241. * by hand onto the new stack - while updating the return eip past
  1242. * the instruction that would have done it for sysenter.
  1243. */
  1244. .macro FIX_STACK offset ok label
  1245. cmpw $__KERNEL_CS, 4(%esp)
  1246. jne \ok
  1247. \label:
  1248. movl TSS_sysenter_sp0 + \offset(%esp), %esp
  1249. CFI_DEF_CFA esp, 0
  1250. CFI_UNDEFINED eip
  1251. pushfl_cfi
  1252. pushl_cfi $__KERNEL_CS
  1253. pushl_cfi $sysenter_past_esp
  1254. CFI_REL_OFFSET eip, 0
  1255. .endm
  1256. ENTRY(debug)
  1257. RING0_INT_FRAME
  1258. ASM_CLAC
  1259. cmpl $ia32_sysenter_target,(%esp)
  1260. jne debug_stack_correct
  1261. FIX_STACK 12, debug_stack_correct, debug_esp_fix_insn
  1262. debug_stack_correct:
  1263. pushl_cfi $-1 # mark this as an int
  1264. SAVE_ALL
  1265. TRACE_IRQS_OFF
  1266. xorl %edx,%edx # error code 0
  1267. movl %esp,%eax # pt_regs pointer
  1268. call do_debug
  1269. jmp ret_from_exception
  1270. CFI_ENDPROC
  1271. END(debug)
  1272. /*
  1273. * NMI is doubly nasty. It can happen _while_ we're handling
  1274. * a debug fault, and the debug fault hasn't yet been able to
  1275. * clear up the stack. So we first check whether we got an
  1276. * NMI on the sysenter entry path, but after that we need to
  1277. * check whether we got an NMI on the debug path where the debug
  1278. * fault happened on the sysenter path.
  1279. */
  1280. ENTRY(nmi)
  1281. RING0_INT_FRAME
  1282. ASM_CLAC
  1283. pushl_cfi %eax
  1284. movl %ss, %eax
  1285. cmpw $__ESPFIX_SS, %ax
  1286. popl_cfi %eax
  1287. je nmi_espfix_stack
  1288. cmpl $ia32_sysenter_target,(%esp)
  1289. je nmi_stack_fixup
  1290. pushl_cfi %eax
  1291. movl %esp,%eax
  1292. /* Do not access memory above the end of our stack page,
  1293. * it might not exist.
  1294. */
  1295. andl $(THREAD_SIZE-1),%eax
  1296. cmpl $(THREAD_SIZE-20),%eax
  1297. popl_cfi %eax
  1298. jae nmi_stack_correct
  1299. cmpl $ia32_sysenter_target,12(%esp)
  1300. je nmi_debug_stack_check
  1301. nmi_stack_correct:
  1302. /* We have a RING0_INT_FRAME here */
  1303. pushl_cfi %eax
  1304. SAVE_ALL
  1305. xorl %edx,%edx # zero error code
  1306. movl %esp,%eax # pt_regs pointer
  1307. call do_nmi
  1308. jmp restore_all_notrace
  1309. CFI_ENDPROC
  1310. nmi_stack_fixup:
  1311. RING0_INT_FRAME
  1312. FIX_STACK 12, nmi_stack_correct, 1
  1313. jmp nmi_stack_correct
  1314. nmi_debug_stack_check:
  1315. /* We have a RING0_INT_FRAME here */
  1316. cmpw $__KERNEL_CS,16(%esp)
  1317. jne nmi_stack_correct
  1318. cmpl $debug,(%esp)
  1319. jb nmi_stack_correct
  1320. cmpl $debug_esp_fix_insn,(%esp)
  1321. ja nmi_stack_correct
  1322. FIX_STACK 24, nmi_stack_correct, 1
  1323. jmp nmi_stack_correct
  1324. nmi_espfix_stack:
  1325. /* We have a RING0_INT_FRAME here.
  1326. *
  1327. * create the pointer to lss back
  1328. */
  1329. pushl_cfi %ss
  1330. pushl_cfi %esp
  1331. addl $4, (%esp)
  1332. /* copy the iret frame of 12 bytes */
  1333. .rept 3
  1334. pushl_cfi 16(%esp)
  1335. .endr
  1336. pushl_cfi %eax
  1337. SAVE_ALL
  1338. FIXUP_ESPFIX_STACK # %eax == %esp
  1339. xorl %edx,%edx # zero error code
  1340. call do_nmi
  1341. RESTORE_REGS
  1342. lss 12+4(%esp), %esp # back to espfix stack
  1343. CFI_ADJUST_CFA_OFFSET -24
  1344. jmp irq_return
  1345. CFI_ENDPROC
  1346. END(nmi)
  1347. ENTRY(int3)
  1348. RING0_INT_FRAME
  1349. ASM_CLAC
  1350. pushl_cfi $-1 # mark this as an int
  1351. SAVE_ALL
  1352. TRACE_IRQS_OFF
  1353. xorl %edx,%edx # zero error code
  1354. movl %esp,%eax # pt_regs pointer
  1355. call do_int3
  1356. jmp ret_from_exception
  1357. CFI_ENDPROC
  1358. END(int3)
  1359. ENTRY(general_protection)
  1360. RING0_EC_FRAME
  1361. pushl_cfi $do_general_protection
  1362. jmp error_code
  1363. CFI_ENDPROC
  1364. END(general_protection)
  1365. #ifdef CONFIG_KVM_GUEST
  1366. ENTRY(async_page_fault)
  1367. RING0_EC_FRAME
  1368. ASM_CLAC
  1369. pushl_cfi $do_async_page_fault
  1370. jmp error_code
  1371. CFI_ENDPROC
  1372. END(async_page_fault)
  1373. #endif
  1374. /*
  1375. * End of kprobes section
  1376. */
  1377. .popsection