entry-armv.S 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * linux/arch/arm/kernel/entry-armv.S
  4. *
  5. * Copyright (C) 1996,1997,1998 Russell King.
  6. * ARM700 fix by Matthew Godbolt (linux-user@willothewisp.demon.co.uk)
  7. * nommu support by Hyok S. Choi (hyok.choi@samsung.com)
  8. *
  9. * Low-level vector interface routines
  10. *
  11. * Note: there is a StrongARM bug in the STMIA rn, {regs}^ instruction
  12. * that causes it to save wrong values... Be aware!
  13. */
  14. #include <linux/init.h>
  15. #include <asm/assembler.h>
  16. #include <asm/page.h>
  17. #include <asm/glue-df.h>
  18. #include <asm/glue-pf.h>
  19. #include <asm/vfpmacros.h>
  20. #include <asm/thread_notify.h>
  21. #include <asm/unwind.h>
  22. #include <asm/unistd.h>
  23. #include <asm/tls.h>
  24. #include <asm/system_info.h>
  25. #include <asm/uaccess-asm.h>
  26. #include <asm/kasan_def.h>
  27. #include "entry-header.S"
  28. #include <asm/probes.h>
  29. #ifdef CONFIG_HAVE_LD_DEAD_CODE_DATA_ELIMINATION
  30. #define RELOC_TEXT_NONE .reloc .text, R_ARM_NONE, .
  31. #else
  32. #define RELOC_TEXT_NONE
  33. #endif
  34. /*
  35. * Interrupt handling.
  36. */
  37. .macro irq_handler, from_user:req
  38. mov r1, sp
  39. ldr_this_cpu r2, irq_stack_ptr, r2, r3
  40. .if \from_user == 0
  41. @
  42. @ If we took the interrupt while running in the kernel, we may already
  43. @ be using the IRQ stack, so revert to the original value in that case.
  44. @
  45. subs r3, r2, r1 @ SP above bottom of IRQ stack?
  46. rsbscs r3, r3, #THREAD_SIZE @ ... and below the top?
  47. #ifdef CONFIG_VMAP_STACK
  48. ldr_va r3, high_memory, cc @ End of the linear region
  49. cmpcc r3, r1 @ Stack pointer was below it?
  50. #endif
  51. bcc 0f @ If not, switch to the IRQ stack
  52. mov r0, r1
  53. bl generic_handle_arch_irq
  54. b 1f
  55. 0:
  56. .endif
  57. mov_l r0, generic_handle_arch_irq
  58. bl call_with_stack
  59. 1:
  60. .endm
  61. .macro pabt_helper
  62. @ PABORT handler takes pt_regs in r2, fault address in r4 and psr in r5
  63. #ifdef MULTI_PABORT
  64. ldr_va ip, processor, offset=PROCESSOR_PABT_FUNC
  65. bl_r ip
  66. #else
  67. bl CPU_PABORT_HANDLER
  68. #endif
  69. .endm
  70. .macro dabt_helper
  71. @
  72. @ Call the processor-specific abort handler:
  73. @
  74. @ r2 - pt_regs
  75. @ r4 - aborted context pc
  76. @ r5 - aborted context psr
  77. @
  78. @ The abort handler must return the aborted address in r0, and
  79. @ the fault status register in r1. r9 must be preserved.
  80. @
  81. #ifdef MULTI_DABORT
  82. ldr_va ip, processor, offset=PROCESSOR_DABT_FUNC
  83. bl_r ip
  84. #else
  85. bl CPU_DABORT_HANDLER
  86. #endif
  87. .endm
  88. .section .entry.text,"ax",%progbits
  89. /*
  90. * Invalid mode handlers
  91. */
  92. .macro inv_entry, reason
  93. sub sp, sp, #PT_REGS_SIZE
  94. ARM( stmib sp, {r1 - lr} )
  95. THUMB( stmia sp, {r0 - r12} )
  96. THUMB( str sp, [sp, #S_SP] )
  97. THUMB( str lr, [sp, #S_LR] )
  98. mov r1, #\reason
  99. .endm
  100. __pabt_invalid:
  101. inv_entry BAD_PREFETCH
  102. b common_invalid
  103. ENDPROC(__pabt_invalid)
  104. __dabt_invalid:
  105. inv_entry BAD_DATA
  106. b common_invalid
  107. ENDPROC(__dabt_invalid)
  108. __irq_invalid:
  109. inv_entry BAD_IRQ
  110. b common_invalid
  111. ENDPROC(__irq_invalid)
  112. __und_invalid:
  113. inv_entry BAD_UNDEFINSTR
  114. @
  115. @ XXX fall through to common_invalid
  116. @
  117. @
  118. @ common_invalid - generic code for failed exception (re-entrant version of handlers)
  119. @
  120. common_invalid:
  121. zero_fp
  122. ldmia r0, {r4 - r6}
  123. add r0, sp, #S_PC @ here for interlock avoidance
  124. mov r7, #-1 @ "" "" "" ""
  125. str r4, [sp] @ save preserved r0
  126. stmia r0, {r5 - r7} @ lr_<exception>,
  127. @ cpsr_<exception>, "old_r0"
  128. mov r0, sp
  129. b bad_mode
  130. ENDPROC(__und_invalid)
  131. /*
  132. * SVC mode handlers
  133. */
  134. #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5)
  135. #define SPFIX(code...) code
  136. #else
  137. #define SPFIX(code...)
  138. #endif
  139. .macro svc_entry, stack_hole=0, trace=1, uaccess=1, overflow_check=1
  140. UNWIND(.fnstart )
  141. sub sp, sp, #(SVC_REGS_SIZE + \stack_hole)
  142. THUMB( add sp, r1 ) @ get SP in a GPR without
  143. THUMB( sub r1, sp, r1 ) @ using a temp register
  144. .if \overflow_check
  145. UNWIND(.save {r0 - pc} )
  146. do_overflow_check (SVC_REGS_SIZE + \stack_hole)
  147. .endif
  148. #ifdef CONFIG_THUMB2_KERNEL
  149. tst r1, #4 @ test stack pointer alignment
  150. sub r1, sp, r1 @ restore original R1
  151. sub sp, r1 @ restore original SP
  152. #else
  153. SPFIX( tst sp, #4 )
  154. #endif
  155. SPFIX( subne sp, sp, #4 )
  156. ARM( stmib sp, {r1 - r12} )
  157. THUMB( stmia sp, {r0 - r12} ) @ No STMIB in Thumb-2
  158. ldmia r0, {r3 - r5}
  159. add r7, sp, #S_SP @ here for interlock avoidance
  160. mov r6, #-1 @ "" "" "" ""
  161. add r2, sp, #(SVC_REGS_SIZE + \stack_hole)
  162. SPFIX( addne r2, r2, #4 )
  163. str r3, [sp] @ save the "real" r0 copied
  164. @ from the exception stack
  165. mov r3, lr
  166. @
  167. @ We are now ready to fill in the remaining blanks on the stack:
  168. @
  169. @ r2 - sp_svc
  170. @ r3 - lr_svc
  171. @ r4 - lr_<exception>, already fixed up for correct return/restart
  172. @ r5 - spsr_<exception>
  173. @ r6 - orig_r0 (see pt_regs definition in ptrace.h)
  174. @
  175. stmia r7, {r2 - r6}
  176. get_thread_info tsk
  177. uaccess_entry tsk, r0, r1, r2, \uaccess
  178. .if \trace
  179. #ifdef CONFIG_TRACE_IRQFLAGS
  180. bl trace_hardirqs_off
  181. #endif
  182. .endif
  183. .endm
  184. .align 5
  185. __dabt_svc:
  186. svc_entry uaccess=0
  187. mov r2, sp
  188. dabt_helper
  189. THUMB( ldr r5, [sp, #S_PSR] ) @ potentially updated CPSR
  190. svc_exit r5 @ return from exception
  191. UNWIND(.fnend )
  192. ENDPROC(__dabt_svc)
  193. .align 5
  194. __irq_svc:
  195. svc_entry
  196. irq_handler from_user=0
  197. #ifdef CONFIG_PREEMPTION
  198. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  199. ldr r0, [tsk, #TI_FLAGS] @ get flags
  200. teq r8, #0 @ if preempt count != 0
  201. movne r0, #0 @ force flags to 0
  202. tst r0, #_TIF_NEED_RESCHED
  203. blne svc_preempt
  204. #endif
  205. svc_exit r5, irq = 1 @ return from exception
  206. UNWIND(.fnend )
  207. ENDPROC(__irq_svc)
  208. .ltorg
  209. #ifdef CONFIG_PREEMPTION
  210. svc_preempt:
  211. mov r8, lr
  212. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  213. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  214. tst r0, #_TIF_NEED_RESCHED
  215. reteq r8 @ go again
  216. b 1b
  217. #endif
  218. __und_fault:
  219. @ Correct the PC such that it is pointing at the instruction
  220. @ which caused the fault. If the faulting instruction was ARM
  221. @ the PC will be pointing at the next instruction, and have to
  222. @ subtract 4. Otherwise, it is Thumb, and the PC will be
  223. @ pointing at the second half of the Thumb instruction. We
  224. @ have to subtract 2.
  225. ldr r2, [r0, #S_PC]
  226. sub r2, r2, r1
  227. str r2, [r0, #S_PC]
  228. b do_undefinstr
  229. ENDPROC(__und_fault)
  230. .align 5
  231. __und_svc:
  232. #ifdef CONFIG_KPROBES
  233. @ If a kprobe is about to simulate a "stmdb sp..." instruction,
  234. @ it obviously needs free stack space which then will belong to
  235. @ the saved context.
  236. svc_entry MAX_STACK_SIZE
  237. #else
  238. svc_entry
  239. #endif
  240. mov r1, #4 @ PC correction to apply
  241. THUMB( tst r5, #PSR_T_BIT ) @ exception taken in Thumb mode?
  242. THUMB( movne r1, #2 ) @ if so, fix up PC correction
  243. mov r0, sp @ struct pt_regs *regs
  244. bl __und_fault
  245. __und_svc_finish:
  246. get_thread_info tsk
  247. ldr r5, [sp, #S_PSR] @ Get SVC cpsr
  248. svc_exit r5 @ return from exception
  249. UNWIND(.fnend )
  250. ENDPROC(__und_svc)
  251. .align 5
  252. __pabt_svc:
  253. svc_entry
  254. mov r2, sp @ regs
  255. pabt_helper
  256. svc_exit r5 @ return from exception
  257. UNWIND(.fnend )
  258. ENDPROC(__pabt_svc)
  259. .align 5
  260. __fiq_svc:
  261. svc_entry trace=0
  262. mov r0, sp @ struct pt_regs *regs
  263. bl handle_fiq_as_nmi
  264. svc_exit_via_fiq
  265. UNWIND(.fnend )
  266. ENDPROC(__fiq_svc)
  267. /*
  268. * Abort mode handlers
  269. */
  270. @
  271. @ Taking a FIQ in abort mode is similar to taking a FIQ in SVC mode
  272. @ and reuses the same macros. However in abort mode we must also
  273. @ save/restore lr_abt and spsr_abt to make nested aborts safe.
  274. @
  275. .align 5
  276. __fiq_abt:
  277. svc_entry trace=0
  278. ARM( msr cpsr_c, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
  279. THUMB( mov r0, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
  280. THUMB( msr cpsr_c, r0 )
  281. mov r1, lr @ Save lr_abt
  282. mrs r2, spsr @ Save spsr_abt, abort is now safe
  283. ARM( msr cpsr_c, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
  284. THUMB( mov r0, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
  285. THUMB( msr cpsr_c, r0 )
  286. stmfd sp!, {r1 - r2}
  287. add r0, sp, #8 @ struct pt_regs *regs
  288. bl handle_fiq_as_nmi
  289. ldmfd sp!, {r1 - r2}
  290. ARM( msr cpsr_c, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
  291. THUMB( mov r0, #ABT_MODE | PSR_I_BIT | PSR_F_BIT )
  292. THUMB( msr cpsr_c, r0 )
  293. mov lr, r1 @ Restore lr_abt, abort is unsafe
  294. msr spsr_cxsf, r2 @ Restore spsr_abt
  295. ARM( msr cpsr_c, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
  296. THUMB( mov r0, #SVC_MODE | PSR_I_BIT | PSR_F_BIT )
  297. THUMB( msr cpsr_c, r0 )
  298. svc_exit_via_fiq
  299. UNWIND(.fnend )
  300. ENDPROC(__fiq_abt)
  301. /*
  302. * User mode handlers
  303. *
  304. * EABI note: sp_svc is always 64-bit aligned here, so should PT_REGS_SIZE
  305. */
  306. #if defined(CONFIG_AEABI) && (__LINUX_ARM_ARCH__ >= 5) && (PT_REGS_SIZE & 7)
  307. #error "sizeof(struct pt_regs) must be a multiple of 8"
  308. #endif
  309. .macro usr_entry, trace=1, uaccess=1
  310. UNWIND(.fnstart )
  311. UNWIND(.cantunwind ) @ don't unwind the user space
  312. sub sp, sp, #PT_REGS_SIZE
  313. ARM( stmib sp, {r1 - r12} )
  314. THUMB( stmia sp, {r0 - r12} )
  315. ATRAP( mrc p15, 0, r7, c1, c0, 0)
  316. ATRAP( ldr_va r8, cr_alignment)
  317. ldmia r0, {r3 - r5}
  318. add r0, sp, #S_PC @ here for interlock avoidance
  319. mov r6, #-1 @ "" "" "" ""
  320. str r3, [sp] @ save the "real" r0 copied
  321. @ from the exception stack
  322. @
  323. @ We are now ready to fill in the remaining blanks on the stack:
  324. @
  325. @ r4 - lr_<exception>, already fixed up for correct return/restart
  326. @ r5 - spsr_<exception>
  327. @ r6 - orig_r0 (see pt_regs definition in ptrace.h)
  328. @
  329. @ Also, separately save sp_usr and lr_usr
  330. @
  331. stmia r0, {r4 - r6}
  332. ARM( stmdb r0, {sp, lr}^ )
  333. THUMB( store_user_sp_lr r0, r1, S_SP - S_PC )
  334. .if \uaccess
  335. uaccess_disable ip
  336. .endif
  337. @ Enable the alignment trap while in kernel mode
  338. ATRAP( teq r8, r7)
  339. ATRAP( mcrne p15, 0, r8, c1, c0, 0)
  340. reload_current r7, r8
  341. @
  342. @ Clear FP to mark the first stack frame
  343. @
  344. zero_fp
  345. .if \trace
  346. #ifdef CONFIG_TRACE_IRQFLAGS
  347. bl trace_hardirqs_off
  348. #endif
  349. ct_user_exit save = 0
  350. .endif
  351. .endm
  352. .macro kuser_cmpxchg_check
  353. #if !defined(CONFIG_CPU_32v6K) && defined(CONFIG_KUSER_HELPERS)
  354. #ifndef CONFIG_MMU
  355. #warning "NPTL on non MMU needs fixing"
  356. #else
  357. @ Make sure our user space atomic helper is restarted
  358. @ if it was interrupted in a critical region. Here we
  359. @ perform a quick test inline since it should be false
  360. @ 99.9999% of the time. The rest is done out of line.
  361. ldr r0, =TASK_SIZE
  362. cmp r4, r0
  363. blhs kuser_cmpxchg64_fixup
  364. #endif
  365. #endif
  366. .endm
  367. .align 5
  368. __dabt_usr:
  369. usr_entry uaccess=0
  370. kuser_cmpxchg_check
  371. mov r2, sp
  372. dabt_helper
  373. b ret_from_exception
  374. UNWIND(.fnend )
  375. ENDPROC(__dabt_usr)
  376. .align 5
  377. __irq_usr:
  378. usr_entry
  379. kuser_cmpxchg_check
  380. irq_handler from_user=1
  381. get_thread_info tsk
  382. mov why, #0
  383. b ret_to_user_from_irq
  384. UNWIND(.fnend )
  385. ENDPROC(__irq_usr)
  386. .ltorg
  387. .align 5
  388. __und_usr:
  389. usr_entry uaccess=0
  390. @ IRQs must be enabled before attempting to read the instruction from
  391. @ user space since that could cause a page/translation fault if the
  392. @ page table was modified by another CPU.
  393. enable_irq
  394. tst r5, #PSR_T_BIT @ Thumb mode?
  395. mov r1, #2 @ set insn size to 2 for Thumb
  396. bne 0f @ handle as Thumb undef exception
  397. #ifdef CONFIG_FPE_NWFPE
  398. adr r9, ret_from_exception
  399. bl call_fpe @ returns via R9 on success
  400. #endif
  401. mov r1, #4 @ set insn size to 4 for ARM
  402. 0: mov r0, sp
  403. uaccess_disable ip
  404. bl __und_fault
  405. b ret_from_exception
  406. UNWIND(.fnend)
  407. ENDPROC(__und_usr)
  408. .align 5
  409. __pabt_usr:
  410. usr_entry
  411. mov r2, sp @ regs
  412. pabt_helper
  413. UNWIND(.fnend )
  414. /* fall through */
  415. /*
  416. * This is the return code to user mode for abort handlers
  417. */
  418. ENTRY(ret_from_exception)
  419. UNWIND(.fnstart )
  420. UNWIND(.cantunwind )
  421. get_thread_info tsk
  422. mov why, #0
  423. b ret_to_user
  424. UNWIND(.fnend )
  425. ENDPROC(__pabt_usr)
  426. ENDPROC(ret_from_exception)
  427. .align 5
  428. __fiq_usr:
  429. usr_entry trace=0
  430. kuser_cmpxchg_check
  431. mov r0, sp @ struct pt_regs *regs
  432. bl handle_fiq_as_nmi
  433. get_thread_info tsk
  434. restore_user_regs fast = 0, offset = 0
  435. UNWIND(.fnend )
  436. ENDPROC(__fiq_usr)
  437. /*
  438. * Register switch for ARMv3 and ARMv4 processors
  439. * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
  440. * previous and next are guaranteed not to be the same.
  441. */
  442. ENTRY(__switch_to)
  443. UNWIND(.fnstart )
  444. UNWIND(.cantunwind )
  445. add ip, r1, #TI_CPU_SAVE
  446. ARM( stmia ip!, {r4 - sl, fp, sp, lr} ) @ Store most regs on stack
  447. THUMB( stmia ip!, {r4 - sl, fp} ) @ Store most regs on stack
  448. THUMB( str sp, [ip], #4 )
  449. THUMB( str lr, [ip], #4 )
  450. ldr r4, [r2, #TI_TP_VALUE]
  451. ldr r5, [r2, #TI_TP_VALUE + 4]
  452. #ifdef CONFIG_CPU_USE_DOMAINS
  453. mrc p15, 0, r6, c3, c0, 0 @ Get domain register
  454. str r6, [r1, #TI_CPU_DOMAIN] @ Save old domain register
  455. ldr r6, [r2, #TI_CPU_DOMAIN]
  456. #endif
  457. switch_tls r1, r4, r5, r3, r7
  458. #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
  459. !defined(CONFIG_STACKPROTECTOR_PER_TASK)
  460. ldr r8, =__stack_chk_guard
  461. .if (TSK_STACK_CANARY > IMM12_MASK)
  462. add r9, r2, #TSK_STACK_CANARY & ~IMM12_MASK
  463. ldr r9, [r9, #TSK_STACK_CANARY & IMM12_MASK]
  464. .else
  465. ldr r9, [r2, #TSK_STACK_CANARY & IMM12_MASK]
  466. .endif
  467. #endif
  468. mov r7, r2 @ Preserve 'next'
  469. #ifdef CONFIG_CPU_USE_DOMAINS
  470. mcr p15, 0, r6, c3, c0, 0 @ Set domain register
  471. #endif
  472. mov r5, r0
  473. add r4, r2, #TI_CPU_SAVE
  474. ldr r0, =thread_notify_head
  475. mov r1, #THREAD_NOTIFY_SWITCH
  476. bl atomic_notifier_call_chain
  477. #if defined(CONFIG_STACKPROTECTOR) && !defined(CONFIG_SMP) && \
  478. !defined(CONFIG_STACKPROTECTOR_PER_TASK)
  479. str r9, [r8]
  480. #endif
  481. mov r0, r5
  482. #if !defined(CONFIG_THUMB2_KERNEL) && !defined(CONFIG_VMAP_STACK)
  483. set_current r7, r8
  484. ldmia r4, {r4 - sl, fp, sp, pc} @ Load all regs saved previously
  485. #else
  486. mov r1, r7
  487. ldmia r4, {r4 - sl, fp, ip, lr} @ Load all regs saved previously
  488. #ifdef CONFIG_VMAP_STACK
  489. @
  490. @ Do a dummy read from the new stack while running from the old one so
  491. @ that we can rely on do_translation_fault() to fix up any stale PMD
  492. @ entries covering the vmalloc region.
  493. @
  494. ldr r2, [ip]
  495. #ifdef CONFIG_KASAN_VMALLOC
  496. @ Also dummy read from the KASAN shadow memory for the new stack if we
  497. @ are using KASAN
  498. mov_l r2, KASAN_SHADOW_OFFSET
  499. add r2, r2, ip, lsr #KASAN_SHADOW_SCALE_SHIFT
  500. ldr r2, [r2]
  501. #endif
  502. #endif
  503. @ When CONFIG_THREAD_INFO_IN_TASK=n, the update of SP itself is what
  504. @ effectuates the task switch, as that is what causes the observable
  505. @ values of current and current_thread_info to change. When
  506. @ CONFIG_THREAD_INFO_IN_TASK=y, setting current (and therefore
  507. @ current_thread_info) is done explicitly, and the update of SP just
  508. @ switches us to another stack, with few other side effects. In order
  509. @ to prevent this distinction from causing any inconsistencies, let's
  510. @ keep the 'set_current' call as close as we can to the update of SP.
  511. set_current r1, r2
  512. mov sp, ip
  513. ret lr
  514. #endif
  515. UNWIND(.fnend )
  516. ENDPROC(__switch_to)
  517. #ifdef CONFIG_VMAP_STACK
  518. .text
  519. .align 2
  520. __bad_stack:
  521. @
  522. @ We've just detected an overflow. We need to load the address of this
  523. @ CPU's overflow stack into the stack pointer register. We have only one
  524. @ scratch register so let's use a sequence of ADDs including one
  525. @ involving the PC, and decorate them with PC-relative group
  526. @ relocations. As these are ARM only, switch to ARM mode first.
  527. @
  528. @ We enter here with IP clobbered and its value stashed on the mode
  529. @ stack.
  530. @
  531. THUMB( bx pc )
  532. THUMB( nop )
  533. THUMB( .arm )
  534. ldr_this_cpu_armv6 ip, overflow_stack_ptr
  535. str sp, [ip, #-4]! @ Preserve original SP value
  536. mov sp, ip @ Switch to overflow stack
  537. pop {ip} @ Original SP in IP
  538. #if defined(CONFIG_UNWINDER_FRAME_POINTER) && defined(CONFIG_CC_IS_GCC)
  539. mov ip, ip @ mov expected by unwinder
  540. push {fp, ip, lr, pc} @ GCC flavor frame record
  541. #else
  542. str ip, [sp, #-8]! @ store original SP
  543. push {fpreg, lr} @ Clang flavor frame record
  544. #endif
  545. UNWIND( ldr ip, [r0, #4] ) @ load exception LR
  546. UNWIND( str ip, [sp, #12] ) @ store in the frame record
  547. ldr ip, [r0, #12] @ reload IP
  548. @ Store the original GPRs to the new stack.
  549. svc_entry uaccess=0, overflow_check=0
  550. UNWIND( .save {sp, pc} )
  551. UNWIND( .save {fpreg, lr} )
  552. UNWIND( .setfp fpreg, sp )
  553. ldr fpreg, [sp, #S_SP] @ Add our frame record
  554. @ to the linked list
  555. #if defined(CONFIG_UNWINDER_FRAME_POINTER) && defined(CONFIG_CC_IS_GCC)
  556. ldr r1, [fp, #4] @ reload SP at entry
  557. add fp, fp, #12
  558. #else
  559. ldr r1, [fpreg, #8]
  560. #endif
  561. str r1, [sp, #S_SP] @ store in pt_regs
  562. @ Stash the regs for handle_bad_stack
  563. mov r0, sp
  564. @ Time to die
  565. bl handle_bad_stack
  566. nop
  567. UNWIND( .fnend )
  568. ENDPROC(__bad_stack)
  569. #endif
  570. __INIT
  571. /*
  572. * User helpers.
  573. *
  574. * Each segment is 32-byte aligned and will be moved to the top of the high
  575. * vector page. New segments (if ever needed) must be added in front of
  576. * existing ones. This mechanism should be used only for things that are
  577. * really small and justified, and not be abused freely.
  578. *
  579. * See Documentation/arch/arm/kernel_user_helpers.rst for formal definitions.
  580. */
  581. THUMB( .arm )
  582. .macro usr_ret, reg
  583. #ifdef CONFIG_ARM_THUMB
  584. bx \reg
  585. #else
  586. ret \reg
  587. #endif
  588. .endm
  589. .macro kuser_pad, sym, size
  590. .if (. - \sym) & 3
  591. .rept 4 - (. - \sym) & 3
  592. .byte 0
  593. .endr
  594. .endif
  595. .rept (\size - (. - \sym)) / 4
  596. .word 0xe7fddef1
  597. .endr
  598. .endm
  599. #ifdef CONFIG_KUSER_HELPERS
  600. .align 5
  601. .globl __kuser_helper_start
  602. __kuser_helper_start:
  603. /*
  604. * Due to the length of some sequences, __kuser_cmpxchg64 spans 2 regular
  605. * kuser "slots", therefore 0xffff0f80 is not used as a valid entry point.
  606. */
  607. __kuser_cmpxchg64: @ 0xffff0f60
  608. #if defined(CONFIG_CPU_32v6K)
  609. stmfd sp!, {r4, r5, r6, r7}
  610. ldrd r4, r5, [r0] @ load old val
  611. ldrd r6, r7, [r1] @ load new val
  612. smp_dmb arm
  613. 1: ldrexd r0, r1, [r2] @ load current val
  614. eors r3, r0, r4 @ compare with oldval (1)
  615. eorseq r3, r1, r5 @ compare with oldval (2)
  616. strexdeq r3, r6, r7, [r2] @ store newval if eq
  617. teqeq r3, #1 @ success?
  618. beq 1b @ if no then retry
  619. smp_dmb arm
  620. rsbs r0, r3, #0 @ set returned val and C flag
  621. ldmfd sp!, {r4, r5, r6, r7}
  622. usr_ret lr
  623. #elif !defined(CONFIG_SMP)
  624. #ifdef CONFIG_MMU
  625. /*
  626. * The only thing that can break atomicity in this cmpxchg64
  627. * implementation is either an IRQ or a data abort exception
  628. * causing another process/thread to be scheduled in the middle of
  629. * the critical sequence. The same strategy as for cmpxchg is used.
  630. */
  631. stmfd sp!, {r4, r5, r6, lr}
  632. ldmia r0, {r4, r5} @ load old val
  633. ldmia r1, {r6, lr} @ load new val
  634. 1: ldmia r2, {r0, r1} @ load current val
  635. eors r3, r0, r4 @ compare with oldval (1)
  636. eorseq r3, r1, r5 @ compare with oldval (2)
  637. 2: stmiaeq r2, {r6, lr} @ store newval if eq
  638. rsbs r0, r3, #0 @ set return val and C flag
  639. ldmfd sp!, {r4, r5, r6, pc}
  640. .text
  641. kuser_cmpxchg64_fixup:
  642. @ Called from kuser_cmpxchg_fixup.
  643. @ r4 = address of interrupted insn (must be preserved).
  644. @ sp = saved regs. r7 and r8 are clobbered.
  645. @ 1b = first critical insn, 2b = last critical insn.
  646. @ If r4 >= 1b and r4 <= 2b then saved pc_usr is set to 1b.
  647. mov r7, #0xffff0fff
  648. sub r7, r7, #(0xffff0fff - (0xffff0f60 + (1b - __kuser_cmpxchg64)))
  649. subs r8, r4, r7
  650. rsbscs r8, r8, #(2b - 1b)
  651. strcs r7, [sp, #S_PC]
  652. #if __LINUX_ARM_ARCH__ < 6
  653. bcc kuser_cmpxchg32_fixup
  654. #endif
  655. ret lr
  656. .previous
  657. #else
  658. #warning "NPTL on non MMU needs fixing"
  659. mov r0, #-1
  660. adds r0, r0, #0
  661. usr_ret lr
  662. #endif
  663. #else
  664. #error "incoherent kernel configuration"
  665. #endif
  666. kuser_pad __kuser_cmpxchg64, 64
  667. __kuser_memory_barrier: @ 0xffff0fa0
  668. smp_dmb arm
  669. usr_ret lr
  670. kuser_pad __kuser_memory_barrier, 32
  671. __kuser_cmpxchg: @ 0xffff0fc0
  672. #if __LINUX_ARM_ARCH__ < 6
  673. #ifdef CONFIG_MMU
  674. /*
  675. * The only thing that can break atomicity in this cmpxchg
  676. * implementation is either an IRQ or a data abort exception
  677. * causing another process/thread to be scheduled in the middle
  678. * of the critical sequence. To prevent this, code is added to
  679. * the IRQ and data abort exception handlers to set the pc back
  680. * to the beginning of the critical section if it is found to be
  681. * within that critical section (see kuser_cmpxchg_fixup).
  682. */
  683. 1: ldr r3, [r2] @ load current val
  684. subs r3, r3, r0 @ compare with oldval
  685. 2: streq r1, [r2] @ store newval if eq
  686. rsbs r0, r3, #0 @ set return val and C flag
  687. usr_ret lr
  688. .text
  689. kuser_cmpxchg32_fixup:
  690. @ Called from kuser_cmpxchg_check macro.
  691. @ r4 = address of interrupted insn (must be preserved).
  692. @ sp = saved regs. r7 and r8 are clobbered.
  693. @ 1b = first critical insn, 2b = last critical insn.
  694. @ If r4 >= 1b and r4 <= 2b then saved pc_usr is set to 1b.
  695. mov r7, #0xffff0fff
  696. sub r7, r7, #(0xffff0fff - (0xffff0fc0 + (1b - __kuser_cmpxchg)))
  697. subs r8, r4, r7
  698. rsbscs r8, r8, #(2b - 1b)
  699. strcs r7, [sp, #S_PC]
  700. ret lr
  701. .previous
  702. #else
  703. #warning "NPTL on non MMU needs fixing"
  704. mov r0, #-1
  705. adds r0, r0, #0
  706. usr_ret lr
  707. #endif
  708. #else
  709. smp_dmb arm
  710. 1: ldrex r3, [r2]
  711. subs r3, r3, r0
  712. strexeq r3, r1, [r2]
  713. teqeq r3, #1
  714. beq 1b
  715. rsbs r0, r3, #0
  716. /* beware -- each __kuser slot must be 8 instructions max */
  717. ALT_SMP(b __kuser_memory_barrier)
  718. ALT_UP(usr_ret lr)
  719. #endif
  720. kuser_pad __kuser_cmpxchg, 32
  721. __kuser_get_tls: @ 0xffff0fe0
  722. ldr r0, [pc, #(16 - 8)] @ read TLS, set in kuser_get_tls_init
  723. usr_ret lr
  724. mrc p15, 0, r0, c13, c0, 3 @ 0xffff0fe8 hardware TLS code
  725. kuser_pad __kuser_get_tls, 16
  726. .rep 3
  727. .word 0 @ 0xffff0ff0 software TLS value, then
  728. .endr @ pad up to __kuser_helper_version
  729. __kuser_helper_version: @ 0xffff0ffc
  730. .word ((__kuser_helper_end - __kuser_helper_start) >> 5)
  731. .globl __kuser_helper_end
  732. __kuser_helper_end:
  733. #endif
  734. THUMB( .thumb )
  735. /*
  736. * Vector stubs.
  737. *
  738. * This code is copied to 0xffff1000 so we can use branches in the
  739. * vectors, rather than ldr's. Note that this code must not exceed
  740. * a page size.
  741. *
  742. * Common stub entry macro:
  743. * Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
  744. *
  745. * SP points to a minimal amount of processor-private memory, the address
  746. * of which is copied into r0 for the mode specific abort handler.
  747. */
  748. .macro vector_stub, name, mode, correction=0
  749. .align 5
  750. #ifdef CONFIG_HARDEN_BRANCH_HISTORY
  751. vector_bhb_bpiall_\name:
  752. mcr p15, 0, r0, c7, c5, 6 @ BPIALL
  753. @ isb not needed due to "movs pc, lr" in the vector stub
  754. @ which gives a "context synchronisation".
  755. #endif
  756. vector_\name:
  757. .if \correction
  758. sub lr, lr, #\correction
  759. .endif
  760. @ Save r0, lr_<exception> (parent PC)
  761. stmia sp, {r0, lr} @ save r0, lr
  762. @ Save spsr_<exception> (parent CPSR)
  763. .Lvec_\name:
  764. mrs lr, spsr
  765. str lr, [sp, #8] @ save spsr
  766. @
  767. @ Prepare for SVC32 mode. IRQs remain disabled.
  768. @
  769. mrs r0, cpsr
  770. eor r0, r0, #(\mode ^ SVC_MODE | PSR_ISETSTATE)
  771. msr spsr_cxsf, r0
  772. @
  773. @ the branch table must immediately follow this code
  774. @
  775. and lr, lr, #0x0f
  776. THUMB( adr r0, 1f )
  777. THUMB( ldr lr, [r0, lr, lsl #2] )
  778. mov r0, sp
  779. ARM( ldr lr, [pc, lr, lsl #2] )
  780. movs pc, lr @ branch to handler in SVC mode
  781. ENDPROC(vector_\name)
  782. #ifdef CONFIG_HARDEN_BRANCH_HISTORY
  783. .subsection 1
  784. .align 5
  785. vector_bhb_loop8_\name:
  786. .if \correction
  787. sub lr, lr, #\correction
  788. .endif
  789. @ Save r0, lr_<exception> (parent PC)
  790. stmia sp, {r0, lr}
  791. @ bhb workaround
  792. mov r0, #8
  793. 3: W(b) . + 4
  794. subs r0, r0, #1
  795. bne 3b
  796. dsb nsh
  797. @ isb not needed due to "movs pc, lr" in the vector stub
  798. @ which gives a "context synchronisation".
  799. b .Lvec_\name
  800. ENDPROC(vector_bhb_loop8_\name)
  801. .previous
  802. #endif
  803. .align 2
  804. @ handler addresses follow this label
  805. 1:
  806. .endm
  807. .section .stubs, "ax", %progbits
  808. @ These need to remain at the start of the section so that
  809. @ they are in range of the 'SWI' entries in the vector tables
  810. @ located 4k down.
  811. .L__vector_swi:
  812. .word vector_swi
  813. #ifdef CONFIG_HARDEN_BRANCH_HISTORY
  814. .L__vector_bhb_loop8_swi:
  815. .word vector_bhb_loop8_swi
  816. .L__vector_bhb_bpiall_swi:
  817. .word vector_bhb_bpiall_swi
  818. #endif
  819. vector_rst:
  820. ARM( swi SYS_ERROR0 )
  821. THUMB( svc #0 )
  822. THUMB( nop )
  823. b vector_und
  824. /*
  825. * Interrupt dispatcher
  826. */
  827. vector_stub irq, IRQ_MODE, 4
  828. .long __irq_usr @ 0 (USR_26 / USR_32)
  829. .long __irq_invalid @ 1 (FIQ_26 / FIQ_32)
  830. .long __irq_invalid @ 2 (IRQ_26 / IRQ_32)
  831. .long __irq_svc @ 3 (SVC_26 / SVC_32)
  832. .long __irq_invalid @ 4
  833. .long __irq_invalid @ 5
  834. .long __irq_invalid @ 6
  835. .long __irq_invalid @ 7
  836. .long __irq_invalid @ 8
  837. .long __irq_invalid @ 9
  838. .long __irq_invalid @ a
  839. .long __irq_invalid @ b
  840. .long __irq_invalid @ c
  841. .long __irq_invalid @ d
  842. .long __irq_invalid @ e
  843. .long __irq_invalid @ f
  844. /*
  845. * Data abort dispatcher
  846. * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
  847. */
  848. vector_stub dabt, ABT_MODE, 8
  849. .long __dabt_usr @ 0 (USR_26 / USR_32)
  850. .long __dabt_invalid @ 1 (FIQ_26 / FIQ_32)
  851. .long __dabt_invalid @ 2 (IRQ_26 / IRQ_32)
  852. .long __dabt_svc @ 3 (SVC_26 / SVC_32)
  853. .long __dabt_invalid @ 4
  854. .long __dabt_invalid @ 5
  855. .long __dabt_invalid @ 6
  856. .long __dabt_invalid @ 7
  857. .long __dabt_invalid @ 8
  858. .long __dabt_invalid @ 9
  859. .long __dabt_invalid @ a
  860. .long __dabt_invalid @ b
  861. .long __dabt_invalid @ c
  862. .long __dabt_invalid @ d
  863. .long __dabt_invalid @ e
  864. .long __dabt_invalid @ f
  865. /*
  866. * Prefetch abort dispatcher
  867. * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
  868. */
  869. vector_stub pabt, ABT_MODE, 4
  870. .long __pabt_usr @ 0 (USR_26 / USR_32)
  871. .long __pabt_invalid @ 1 (FIQ_26 / FIQ_32)
  872. .long __pabt_invalid @ 2 (IRQ_26 / IRQ_32)
  873. .long __pabt_svc @ 3 (SVC_26 / SVC_32)
  874. .long __pabt_invalid @ 4
  875. .long __pabt_invalid @ 5
  876. .long __pabt_invalid @ 6
  877. .long __pabt_invalid @ 7
  878. .long __pabt_invalid @ 8
  879. .long __pabt_invalid @ 9
  880. .long __pabt_invalid @ a
  881. .long __pabt_invalid @ b
  882. .long __pabt_invalid @ c
  883. .long __pabt_invalid @ d
  884. .long __pabt_invalid @ e
  885. .long __pabt_invalid @ f
  886. /*
  887. * Undef instr entry dispatcher
  888. * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
  889. */
  890. vector_stub und, UND_MODE
  891. .long __und_usr @ 0 (USR_26 / USR_32)
  892. .long __und_invalid @ 1 (FIQ_26 / FIQ_32)
  893. .long __und_invalid @ 2 (IRQ_26 / IRQ_32)
  894. .long __und_svc @ 3 (SVC_26 / SVC_32)
  895. .long __und_invalid @ 4
  896. .long __und_invalid @ 5
  897. .long __und_invalid @ 6
  898. .long __und_invalid @ 7
  899. .long __und_invalid @ 8
  900. .long __und_invalid @ 9
  901. .long __und_invalid @ a
  902. .long __und_invalid @ b
  903. .long __und_invalid @ c
  904. .long __und_invalid @ d
  905. .long __und_invalid @ e
  906. .long __und_invalid @ f
  907. .align 5
  908. /*=============================================================================
  909. * Address exception handler
  910. *-----------------------------------------------------------------------------
  911. * These aren't too critical.
  912. * (they're not supposed to happen, and won't happen in 32-bit data mode).
  913. */
  914. vector_addrexcptn:
  915. b vector_addrexcptn
  916. /*=============================================================================
  917. * FIQ "NMI" handler
  918. *-----------------------------------------------------------------------------
  919. * Handle a FIQ using the SVC stack allowing FIQ act like NMI on x86
  920. * systems. This must be the last vector stub, so lets place it in its own
  921. * subsection.
  922. */
  923. .subsection 2
  924. vector_stub fiq, FIQ_MODE, 4
  925. .long __fiq_usr @ 0 (USR_26 / USR_32)
  926. .long __fiq_svc @ 1 (FIQ_26 / FIQ_32)
  927. .long __fiq_svc @ 2 (IRQ_26 / IRQ_32)
  928. .long __fiq_svc @ 3 (SVC_26 / SVC_32)
  929. .long __fiq_svc @ 4
  930. .long __fiq_svc @ 5
  931. .long __fiq_svc @ 6
  932. .long __fiq_abt @ 7
  933. .long __fiq_svc @ 8
  934. .long __fiq_svc @ 9
  935. .long __fiq_svc @ a
  936. .long __fiq_svc @ b
  937. .long __fiq_svc @ c
  938. .long __fiq_svc @ d
  939. .long __fiq_svc @ e
  940. .long __fiq_svc @ f
  941. .globl vector_fiq
  942. .section .vectors, "ax", %progbits
  943. RELOC_TEXT_NONE
  944. W(b) vector_rst
  945. W(b) vector_und
  946. ARM( .reloc ., R_ARM_LDR_PC_G0, .L__vector_swi )
  947. THUMB( .reloc ., R_ARM_THM_PC12, .L__vector_swi )
  948. W(ldr) pc, .
  949. W(b) vector_pabt
  950. W(b) vector_dabt
  951. W(b) vector_addrexcptn
  952. W(b) vector_irq
  953. W(b) vector_fiq
  954. #ifdef CONFIG_HARDEN_BRANCH_HISTORY
  955. .section .vectors.bhb.loop8, "ax", %progbits
  956. RELOC_TEXT_NONE
  957. W(b) vector_rst
  958. W(b) vector_bhb_loop8_und
  959. ARM( .reloc ., R_ARM_LDR_PC_G0, .L__vector_bhb_loop8_swi )
  960. THUMB( .reloc ., R_ARM_THM_PC12, .L__vector_bhb_loop8_swi )
  961. W(ldr) pc, .
  962. W(b) vector_bhb_loop8_pabt
  963. W(b) vector_bhb_loop8_dabt
  964. W(b) vector_addrexcptn
  965. W(b) vector_bhb_loop8_irq
  966. W(b) vector_bhb_loop8_fiq
  967. .section .vectors.bhb.bpiall, "ax", %progbits
  968. RELOC_TEXT_NONE
  969. W(b) vector_rst
  970. W(b) vector_bhb_bpiall_und
  971. ARM( .reloc ., R_ARM_LDR_PC_G0, .L__vector_bhb_bpiall_swi )
  972. THUMB( .reloc ., R_ARM_THM_PC12, .L__vector_bhb_bpiall_swi )
  973. W(ldr) pc, .
  974. W(b) vector_bhb_bpiall_pabt
  975. W(b) vector_bhb_bpiall_dabt
  976. W(b) vector_addrexcptn
  977. W(b) vector_bhb_bpiall_irq
  978. W(b) vector_bhb_bpiall_fiq
  979. #endif
  980. .data
  981. .align 2
  982. .globl cr_alignment
  983. cr_alignment:
  984. .space 4