entry-armv.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /*
  2. * linux/arch/arm/kernel/entry-armv.S
  3. *
  4. * Copyright (C) 1996,1997,1998 Russell King.
  5. * ARM700 fix by Matthew Godbolt (linux-user@willothewisp.demon.co.uk)
  6. * nommu support by Hyok S. Choi (hyok.choi@samsung.com)
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * Low-level vector interface routines
  13. *
  14. * Note: there is a StrongARM bug in the STMIA rn, {regs}^ instruction that causes
  15. * it to save wrong values... Be aware!
  16. */
  17. #include <linux/config.h>
  18. #include <asm/memory.h>
  19. #include <asm/glue.h>
  20. #include <asm/vfpmacros.h>
  21. #include <asm/arch/entry-macro.S>
  22. #include "entry-header.S"
  23. /*
  24. * Interrupt handling. Preserves r7, r8, r9
  25. */
  26. .macro irq_handler
  27. 1: get_irqnr_and_base r0, r6, r5, lr
  28. movne r1, sp
  29. @
  30. @ routine called with r0 = irq number, r1 = struct pt_regs *
  31. @
  32. adrne lr, 1b
  33. bne asm_do_IRQ
  34. #ifdef CONFIG_SMP
  35. /*
  36. * XXX
  37. *
  38. * this macro assumes that irqstat (r6) and base (r5) are
  39. * preserved from get_irqnr_and_base above
  40. */
  41. test_for_ipi r0, r6, r5, lr
  42. movne r0, sp
  43. adrne lr, 1b
  44. bne do_IPI
  45. #ifdef CONFIG_LOCAL_TIMERS
  46. test_for_ltirq r0, r6, r5, lr
  47. movne r0, sp
  48. adrne lr, 1b
  49. bne do_local_timer
  50. #endif
  51. #endif
  52. .endm
  53. /*
  54. * Invalid mode handlers
  55. */
  56. .macro inv_entry, reason
  57. sub sp, sp, #S_FRAME_SIZE
  58. stmib sp, {r1 - lr}
  59. mov r1, #\reason
  60. .endm
  61. __pabt_invalid:
  62. inv_entry BAD_PREFETCH
  63. b common_invalid
  64. __dabt_invalid:
  65. inv_entry BAD_DATA
  66. b common_invalid
  67. __irq_invalid:
  68. inv_entry BAD_IRQ
  69. b common_invalid
  70. __und_invalid:
  71. inv_entry BAD_UNDEFINSTR
  72. @
  73. @ XXX fall through to common_invalid
  74. @
  75. @
  76. @ common_invalid - generic code for failed exception (re-entrant version of handlers)
  77. @
  78. common_invalid:
  79. zero_fp
  80. ldmia r0, {r4 - r6}
  81. add r0, sp, #S_PC @ here for interlock avoidance
  82. mov r7, #-1 @ "" "" "" ""
  83. str r4, [sp] @ save preserved r0
  84. stmia r0, {r5 - r7} @ lr_<exception>,
  85. @ cpsr_<exception>, "old_r0"
  86. mov r0, sp
  87. and r2, r6, #0x1f
  88. b bad_mode
  89. /*
  90. * SVC mode handlers
  91. */
  92. .macro svc_entry
  93. sub sp, sp, #S_FRAME_SIZE
  94. stmib sp, {r1 - r12}
  95. ldmia r0, {r1 - r3}
  96. add r5, sp, #S_SP @ here for interlock avoidance
  97. mov r4, #-1 @ "" "" "" ""
  98. add r0, sp, #S_FRAME_SIZE @ "" "" "" ""
  99. str r1, [sp] @ save the "real" r0 copied
  100. @ from the exception stack
  101. mov r1, lr
  102. @
  103. @ We are now ready to fill in the remaining blanks on the stack:
  104. @
  105. @ r0 - sp_svc
  106. @ r1 - lr_svc
  107. @ r2 - lr_<exception>, already fixed up for correct return/restart
  108. @ r3 - spsr_<exception>
  109. @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
  110. @
  111. stmia r5, {r0 - r4}
  112. .endm
  113. .align 5
  114. __dabt_svc:
  115. svc_entry
  116. @
  117. @ get ready to re-enable interrupts if appropriate
  118. @
  119. mrs r9, cpsr
  120. tst r3, #PSR_I_BIT
  121. biceq r9, r9, #PSR_I_BIT
  122. @
  123. @ Call the processor-specific abort handler:
  124. @
  125. @ r2 - aborted context pc
  126. @ r3 - aborted context cpsr
  127. @
  128. @ The abort handler must return the aborted address in r0, and
  129. @ the fault status register in r1. r9 must be preserved.
  130. @
  131. #ifdef MULTI_ABORT
  132. ldr r4, .LCprocfns
  133. mov lr, pc
  134. ldr pc, [r4]
  135. #else
  136. bl CPU_ABORT_HANDLER
  137. #endif
  138. @
  139. @ set desired IRQ state, then call main handler
  140. @
  141. msr cpsr_c, r9
  142. mov r2, sp
  143. bl do_DataAbort
  144. @
  145. @ IRQs off again before pulling preserved data off the stack
  146. @
  147. disable_irq
  148. @
  149. @ restore SPSR and restart the instruction
  150. @
  151. ldr r0, [sp, #S_PSR]
  152. msr spsr_cxsf, r0
  153. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  154. .align 5
  155. __irq_svc:
  156. svc_entry
  157. #ifdef CONFIG_PREEMPT
  158. get_thread_info tsk
  159. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  160. add r7, r8, #1 @ increment it
  161. str r7, [tsk, #TI_PREEMPT]
  162. #endif
  163. irq_handler
  164. #ifdef CONFIG_PREEMPT
  165. ldr r0, [tsk, #TI_FLAGS] @ get flags
  166. tst r0, #_TIF_NEED_RESCHED
  167. blne svc_preempt
  168. preempt_return:
  169. ldr r0, [tsk, #TI_PREEMPT] @ read preempt value
  170. str r8, [tsk, #TI_PREEMPT] @ restore preempt count
  171. teq r0, r7
  172. strne r0, [r0, -r0] @ bug()
  173. #endif
  174. ldr r0, [sp, #S_PSR] @ irqs are already disabled
  175. msr spsr_cxsf, r0
  176. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  177. .ltorg
  178. #ifdef CONFIG_PREEMPT
  179. svc_preempt:
  180. teq r8, #0 @ was preempt count = 0
  181. ldreq r6, .LCirq_stat
  182. movne pc, lr @ no
  183. ldr r0, [r6, #4] @ local_irq_count
  184. ldr r1, [r6, #8] @ local_bh_count
  185. adds r0, r0, r1
  186. movne pc, lr
  187. mov r7, #0 @ preempt_schedule_irq
  188. str r7, [tsk, #TI_PREEMPT] @ expects preempt_count == 0
  189. 1: bl preempt_schedule_irq @ irq en/disable is done inside
  190. ldr r0, [tsk, #TI_FLAGS] @ get new tasks TI_FLAGS
  191. tst r0, #_TIF_NEED_RESCHED
  192. beq preempt_return @ go again
  193. b 1b
  194. #endif
  195. .align 5
  196. __und_svc:
  197. svc_entry
  198. @
  199. @ call emulation code, which returns using r9 if it has emulated
  200. @ the instruction, or the more conventional lr if we are to treat
  201. @ this as a real undefined instruction
  202. @
  203. @ r0 - instruction
  204. @
  205. ldr r0, [r2, #-4]
  206. adr r9, 1f
  207. bl call_fpe
  208. mov r0, sp @ struct pt_regs *regs
  209. bl do_undefinstr
  210. @
  211. @ IRQs off again before pulling preserved data off the stack
  212. @
  213. 1: disable_irq
  214. @
  215. @ restore SPSR and restart the instruction
  216. @
  217. ldr lr, [sp, #S_PSR] @ Get SVC cpsr
  218. msr spsr_cxsf, lr
  219. ldmia sp, {r0 - pc}^ @ Restore SVC registers
  220. .align 5
  221. __pabt_svc:
  222. svc_entry
  223. @
  224. @ re-enable interrupts if appropriate
  225. @
  226. mrs r9, cpsr
  227. tst r3, #PSR_I_BIT
  228. biceq r9, r9, #PSR_I_BIT
  229. msr cpsr_c, r9
  230. @
  231. @ set args, then call main handler
  232. @
  233. @ r0 - address of faulting instruction
  234. @ r1 - pointer to registers on stack
  235. @
  236. mov r0, r2 @ address (pc)
  237. mov r1, sp @ regs
  238. bl do_PrefetchAbort @ call abort handler
  239. @
  240. @ IRQs off again before pulling preserved data off the stack
  241. @
  242. disable_irq
  243. @
  244. @ restore SPSR and restart the instruction
  245. @
  246. ldr r0, [sp, #S_PSR]
  247. msr spsr_cxsf, r0
  248. ldmia sp, {r0 - pc}^ @ load r0 - pc, cpsr
  249. .align 5
  250. .LCcralign:
  251. .word cr_alignment
  252. #ifdef MULTI_ABORT
  253. .LCprocfns:
  254. .word processor
  255. #endif
  256. .LCfp:
  257. .word fp_enter
  258. #ifdef CONFIG_PREEMPT
  259. .LCirq_stat:
  260. .word irq_stat
  261. #endif
  262. /*
  263. * User mode handlers
  264. */
  265. .macro usr_entry
  266. sub sp, sp, #S_FRAME_SIZE
  267. stmib sp, {r1 - r12}
  268. ldmia r0, {r1 - r3}
  269. add r0, sp, #S_PC @ here for interlock avoidance
  270. mov r4, #-1 @ "" "" "" ""
  271. str r1, [sp] @ save the "real" r0 copied
  272. @ from the exception stack
  273. #if __LINUX_ARM_ARCH__ < 6 && !defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
  274. @ make sure our user space atomic helper is aborted
  275. cmp r2, #TASK_SIZE
  276. bichs r3, r3, #PSR_Z_BIT
  277. #endif
  278. @
  279. @ We are now ready to fill in the remaining blanks on the stack:
  280. @
  281. @ r2 - lr_<exception>, already fixed up for correct return/restart
  282. @ r3 - spsr_<exception>
  283. @ r4 - orig_r0 (see pt_regs definition in ptrace.h)
  284. @
  285. @ Also, separately save sp_usr and lr_usr
  286. @
  287. stmia r0, {r2 - r4}
  288. stmdb r0, {sp, lr}^
  289. @
  290. @ Enable the alignment trap while in kernel mode
  291. @
  292. alignment_trap r0
  293. @
  294. @ Clear FP to mark the first stack frame
  295. @
  296. zero_fp
  297. .endm
  298. .align 5
  299. __dabt_usr:
  300. usr_entry
  301. @
  302. @ Call the processor-specific abort handler:
  303. @
  304. @ r2 - aborted context pc
  305. @ r3 - aborted context cpsr
  306. @
  307. @ The abort handler must return the aborted address in r0, and
  308. @ the fault status register in r1.
  309. @
  310. #ifdef MULTI_ABORT
  311. ldr r4, .LCprocfns
  312. mov lr, pc
  313. ldr pc, [r4]
  314. #else
  315. bl CPU_ABORT_HANDLER
  316. #endif
  317. @
  318. @ IRQs on, then call the main handler
  319. @
  320. enable_irq
  321. mov r2, sp
  322. adr lr, ret_from_exception
  323. b do_DataAbort
  324. .align 5
  325. __irq_usr:
  326. usr_entry
  327. get_thread_info tsk
  328. #ifdef CONFIG_PREEMPT
  329. ldr r8, [tsk, #TI_PREEMPT] @ get preempt count
  330. add r7, r8, #1 @ increment it
  331. str r7, [tsk, #TI_PREEMPT]
  332. #endif
  333. irq_handler
  334. #ifdef CONFIG_PREEMPT
  335. ldr r0, [tsk, #TI_PREEMPT]
  336. str r8, [tsk, #TI_PREEMPT]
  337. teq r0, r7
  338. strne r0, [r0, -r0]
  339. #endif
  340. mov why, #0
  341. b ret_to_user
  342. .ltorg
  343. .align 5
  344. __und_usr:
  345. usr_entry
  346. tst r3, #PSR_T_BIT @ Thumb mode?
  347. bne fpundefinstr @ ignore FP
  348. sub r4, r2, #4
  349. @
  350. @ fall through to the emulation code, which returns using r9 if
  351. @ it has emulated the instruction, or the more conventional lr
  352. @ if we are to treat this as a real undefined instruction
  353. @
  354. @ r0 - instruction
  355. @
  356. 1: ldrt r0, [r4]
  357. adr r9, ret_from_exception
  358. adr lr, fpundefinstr
  359. @
  360. @ fallthrough to call_fpe
  361. @
  362. /*
  363. * The out of line fixup for the ldrt above.
  364. */
  365. .section .fixup, "ax"
  366. 2: mov pc, r9
  367. .previous
  368. .section __ex_table,"a"
  369. .long 1b, 2b
  370. .previous
  371. /*
  372. * Check whether the instruction is a co-processor instruction.
  373. * If yes, we need to call the relevant co-processor handler.
  374. *
  375. * Note that we don't do a full check here for the co-processor
  376. * instructions; all instructions with bit 27 set are well
  377. * defined. The only instructions that should fault are the
  378. * co-processor instructions. However, we have to watch out
  379. * for the ARM6/ARM7 SWI bug.
  380. *
  381. * Emulators may wish to make use of the following registers:
  382. * r0 = instruction opcode.
  383. * r2 = PC+4
  384. * r10 = this threads thread_info structure.
  385. */
  386. call_fpe:
  387. tst r0, #0x08000000 @ only CDP/CPRT/LDC/STC have bit 27
  388. #if defined(CONFIG_CPU_ARM610) || defined(CONFIG_CPU_ARM710)
  389. and r8, r0, #0x0f000000 @ mask out op-code bits
  390. teqne r8, #0x0f000000 @ SWI (ARM6/7 bug)?
  391. #endif
  392. moveq pc, lr
  393. get_thread_info r10 @ get current thread
  394. and r8, r0, #0x00000f00 @ mask out CP number
  395. mov r7, #1
  396. add r6, r10, #TI_USED_CP
  397. strb r7, [r6, r8, lsr #8] @ set appropriate used_cp[]
  398. #ifdef CONFIG_IWMMXT
  399. @ Test if we need to give access to iWMMXt coprocessors
  400. ldr r5, [r10, #TI_FLAGS]
  401. rsbs r7, r8, #(1 << 8) @ CP 0 or 1 only
  402. movcss r7, r5, lsr #(TIF_USING_IWMMXT + 1)
  403. bcs iwmmxt_task_enable
  404. #endif
  405. enable_irq
  406. add pc, pc, r8, lsr #6
  407. mov r0, r0
  408. mov pc, lr @ CP#0
  409. b do_fpe @ CP#1 (FPE)
  410. b do_fpe @ CP#2 (FPE)
  411. mov pc, lr @ CP#3
  412. mov pc, lr @ CP#4
  413. mov pc, lr @ CP#5
  414. mov pc, lr @ CP#6
  415. mov pc, lr @ CP#7
  416. mov pc, lr @ CP#8
  417. mov pc, lr @ CP#9
  418. #ifdef CONFIG_VFP
  419. b do_vfp @ CP#10 (VFP)
  420. b do_vfp @ CP#11 (VFP)
  421. #else
  422. mov pc, lr @ CP#10 (VFP)
  423. mov pc, lr @ CP#11 (VFP)
  424. #endif
  425. mov pc, lr @ CP#12
  426. mov pc, lr @ CP#13
  427. mov pc, lr @ CP#14 (Debug)
  428. mov pc, lr @ CP#15 (Control)
  429. do_fpe:
  430. ldr r4, .LCfp
  431. add r10, r10, #TI_FPSTATE @ r10 = workspace
  432. ldr pc, [r4] @ Call FP module USR entry point
  433. /*
  434. * The FP module is called with these registers set:
  435. * r0 = instruction
  436. * r2 = PC+4
  437. * r9 = normal "successful" return address
  438. * r10 = FP workspace
  439. * lr = unrecognised FP instruction return address
  440. */
  441. .data
  442. ENTRY(fp_enter)
  443. .word fpundefinstr
  444. .text
  445. fpundefinstr:
  446. mov r0, sp
  447. adr lr, ret_from_exception
  448. b do_undefinstr
  449. .align 5
  450. __pabt_usr:
  451. usr_entry
  452. enable_irq @ Enable interrupts
  453. mov r0, r2 @ address (pc)
  454. mov r1, sp @ regs
  455. bl do_PrefetchAbort @ call abort handler
  456. /* fall through */
  457. /*
  458. * This is the return code to user mode for abort handlers
  459. */
  460. ENTRY(ret_from_exception)
  461. get_thread_info tsk
  462. mov why, #0
  463. b ret_to_user
  464. /*
  465. * Register switch for ARMv3 and ARMv4 processors
  466. * r0 = previous task_struct, r1 = previous thread_info, r2 = next thread_info
  467. * previous and next are guaranteed not to be the same.
  468. */
  469. ENTRY(__switch_to)
  470. add ip, r1, #TI_CPU_SAVE
  471. ldr r3, [r2, #TI_TP_VALUE]
  472. stmia ip!, {r4 - sl, fp, sp, lr} @ Store most regs on stack
  473. #ifndef CONFIG_MMU
  474. add r2, r2, #TI_CPU_DOMAIN
  475. #else
  476. ldr r6, [r2, #TI_CPU_DOMAIN]!
  477. #endif
  478. #if __LINUX_ARM_ARCH__ >= 6
  479. #ifdef CONFIG_CPU_MPCORE
  480. clrex
  481. #else
  482. strex r5, r4, [ip] @ Clear exclusive monitor
  483. #endif
  484. #endif
  485. #if defined(CONFIG_CPU_XSCALE) && !defined(CONFIG_IWMMXT)
  486. mra r4, r5, acc0
  487. stmia ip, {r4, r5}
  488. #endif
  489. #if defined(CONFIG_HAS_TLS_REG)
  490. mcr p15, 0, r3, c13, c0, 3 @ set TLS register
  491. #elif !defined(CONFIG_TLS_REG_EMUL)
  492. mov r4, #0xffff0fff
  493. str r3, [r4, #-15] @ TLS val at 0xffff0ff0
  494. #endif
  495. #ifdef CONFIG_MMU
  496. mcr p15, 0, r6, c3, c0, 0 @ Set domain register
  497. #endif
  498. #ifdef CONFIG_VFP
  499. @ Always disable VFP so we can lazily save/restore the old
  500. @ state. This occurs in the context of the previous thread.
  501. VFPFMRX r4, FPEXC
  502. bic r4, r4, #FPEXC_ENABLE
  503. VFPFMXR FPEXC, r4
  504. #endif
  505. #if defined(CONFIG_IWMMXT)
  506. bl iwmmxt_task_switch
  507. #elif defined(CONFIG_CPU_XSCALE)
  508. add r4, r2, #40 @ cpu_context_save->extra
  509. ldmib r4, {r4, r5}
  510. mar acc0, r4, r5
  511. #endif
  512. ldmib r2, {r4 - sl, fp, sp, pc} @ Load all regs saved previously
  513. __INIT
  514. /*
  515. * User helpers.
  516. *
  517. * These are segment of kernel provided user code reachable from user space
  518. * at a fixed address in kernel memory. This is used to provide user space
  519. * with some operations which require kernel help because of unimplemented
  520. * native feature and/or instructions in many ARM CPUs. The idea is for
  521. * this code to be executed directly in user mode for best efficiency but
  522. * which is too intimate with the kernel counter part to be left to user
  523. * libraries. In fact this code might even differ from one CPU to another
  524. * depending on the available instruction set and restrictions like on
  525. * SMP systems. In other words, the kernel reserves the right to change
  526. * this code as needed without warning. Only the entry points and their
  527. * results are guaranteed to be stable.
  528. *
  529. * Each segment is 32-byte aligned and will be moved to the top of the high
  530. * vector page. New segments (if ever needed) must be added in front of
  531. * existing ones. This mechanism should be used only for things that are
  532. * really small and justified, and not be abused freely.
  533. *
  534. * User space is expected to implement those things inline when optimizing
  535. * for a processor that has the necessary native support, but only if such
  536. * resulting binaries are already to be incompatible with earlier ARM
  537. * processors due to the use of unsupported instructions other than what
  538. * is provided here. In other words don't make binaries unable to run on
  539. * earlier processors just for the sake of not using these kernel helpers
  540. * if your compiled code is not going to use the new instructions for other
  541. * purpose.
  542. */
  543. .align 5
  544. .globl __kuser_helper_start
  545. __kuser_helper_start:
  546. /*
  547. * Reference prototype:
  548. *
  549. * void __kernel_memory_barrier(void)
  550. *
  551. * Input:
  552. *
  553. * lr = return address
  554. *
  555. * Output:
  556. *
  557. * none
  558. *
  559. * Clobbered:
  560. *
  561. * the Z flag might be lost
  562. *
  563. * Definition and user space usage example:
  564. *
  565. * typedef void (__kernel_dmb_t)(void);
  566. * #define __kernel_dmb (*(__kernel_dmb_t *)0xffff0fa0)
  567. *
  568. * Apply any needed memory barrier to preserve consistency with data modified
  569. * manually and __kuser_cmpxchg usage.
  570. *
  571. * This could be used as follows:
  572. *
  573. * #define __kernel_dmb() \
  574. * asm volatile ( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #95" \
  575. * : : : "lr","cc" )
  576. */
  577. __kuser_memory_barrier: @ 0xffff0fa0
  578. #if __LINUX_ARM_ARCH__ >= 6 && defined(CONFIG_SMP)
  579. mcr p15, 0, r0, c7, c10, 5 @ dmb
  580. #endif
  581. mov pc, lr
  582. .align 5
  583. /*
  584. * Reference prototype:
  585. *
  586. * int __kernel_cmpxchg(int oldval, int newval, int *ptr)
  587. *
  588. * Input:
  589. *
  590. * r0 = oldval
  591. * r1 = newval
  592. * r2 = ptr
  593. * lr = return address
  594. *
  595. * Output:
  596. *
  597. * r0 = returned value (zero or non-zero)
  598. * C flag = set if r0 == 0, clear if r0 != 0
  599. *
  600. * Clobbered:
  601. *
  602. * r3, ip, flags
  603. *
  604. * Definition and user space usage example:
  605. *
  606. * typedef int (__kernel_cmpxchg_t)(int oldval, int newval, int *ptr);
  607. * #define __kernel_cmpxchg (*(__kernel_cmpxchg_t *)0xffff0fc0)
  608. *
  609. * Atomically store newval in *ptr if *ptr is equal to oldval for user space.
  610. * Return zero if *ptr was changed or non-zero if no exchange happened.
  611. * The C flag is also set if *ptr was changed to allow for assembly
  612. * optimization in the calling code.
  613. *
  614. * Note: this routine already includes memory barriers as needed.
  615. *
  616. * For example, a user space atomic_add implementation could look like this:
  617. *
  618. * #define atomic_add(ptr, val) \
  619. * ({ register unsigned int *__ptr asm("r2") = (ptr); \
  620. * register unsigned int __result asm("r1"); \
  621. * asm volatile ( \
  622. * "1: @ atomic_add\n\t" \
  623. * "ldr r0, [r2]\n\t" \
  624. * "mov r3, #0xffff0fff\n\t" \
  625. * "add lr, pc, #4\n\t" \
  626. * "add r1, r0, %2\n\t" \
  627. * "add pc, r3, #(0xffff0fc0 - 0xffff0fff)\n\t" \
  628. * "bcc 1b" \
  629. * : "=&r" (__result) \
  630. * : "r" (__ptr), "rIL" (val) \
  631. * : "r0","r3","ip","lr","cc","memory" ); \
  632. * __result; })
  633. */
  634. __kuser_cmpxchg: @ 0xffff0fc0
  635. #if defined(CONFIG_NEEDS_SYSCALL_FOR_CMPXCHG)
  636. /*
  637. * Poor you. No fast solution possible...
  638. * The kernel itself must perform the operation.
  639. * A special ghost syscall is used for that (see traps.c).
  640. */
  641. swi #0x9ffff0
  642. mov pc, lr
  643. #elif __LINUX_ARM_ARCH__ < 6
  644. /*
  645. * Theory of operation:
  646. *
  647. * We set the Z flag before loading oldval. If ever an exception
  648. * occurs we can not be sure the loaded value will still be the same
  649. * when the exception returns, therefore the user exception handler
  650. * will clear the Z flag whenever the interrupted user code was
  651. * actually from the kernel address space (see the usr_entry macro).
  652. *
  653. * The post-increment on the str is used to prevent a race with an
  654. * exception happening just after the str instruction which would
  655. * clear the Z flag although the exchange was done.
  656. */
  657. teq ip, ip @ set Z flag
  658. ldr ip, [r2] @ load current val
  659. add r3, r2, #1 @ prepare store ptr
  660. teqeq ip, r0 @ compare with oldval if still allowed
  661. streq r1, [r3, #-1]! @ store newval if still allowed
  662. subs r0, r2, r3 @ if r2 == r3 the str occured
  663. mov pc, lr
  664. #else
  665. #ifdef CONFIG_SMP
  666. mcr p15, 0, r0, c7, c10, 5 @ dmb
  667. #endif
  668. ldrex r3, [r2]
  669. subs r3, r3, r0
  670. strexeq r3, r1, [r2]
  671. rsbs r0, r3, #0
  672. #ifdef CONFIG_SMP
  673. mcr p15, 0, r0, c7, c10, 5 @ dmb
  674. #endif
  675. mov pc, lr
  676. #endif
  677. .align 5
  678. /*
  679. * Reference prototype:
  680. *
  681. * int __kernel_get_tls(void)
  682. *
  683. * Input:
  684. *
  685. * lr = return address
  686. *
  687. * Output:
  688. *
  689. * r0 = TLS value
  690. *
  691. * Clobbered:
  692. *
  693. * the Z flag might be lost
  694. *
  695. * Definition and user space usage example:
  696. *
  697. * typedef int (__kernel_get_tls_t)(void);
  698. * #define __kernel_get_tls (*(__kernel_get_tls_t *)0xffff0fe0)
  699. *
  700. * Get the TLS value as previously set via the __ARM_NR_set_tls syscall.
  701. *
  702. * This could be used as follows:
  703. *
  704. * #define __kernel_get_tls() \
  705. * ({ register unsigned int __val asm("r0"); \
  706. * asm( "mov r0, #0xffff0fff; mov lr, pc; sub pc, r0, #31" \
  707. * : "=r" (__val) : : "lr","cc" ); \
  708. * __val; })
  709. */
  710. __kuser_get_tls: @ 0xffff0fe0
  711. #if !defined(CONFIG_HAS_TLS_REG) && !defined(CONFIG_TLS_REG_EMUL)
  712. ldr r0, [pc, #(16 - 8)] @ TLS stored at 0xffff0ff0
  713. mov pc, lr
  714. #else
  715. mrc p15, 0, r0, c13, c0, 3 @ read TLS register
  716. mov pc, lr
  717. #endif
  718. .rep 5
  719. .word 0 @ pad up to __kuser_helper_version
  720. .endr
  721. /*
  722. * Reference declaration:
  723. *
  724. * extern unsigned int __kernel_helper_version;
  725. *
  726. * Definition and user space usage example:
  727. *
  728. * #define __kernel_helper_version (*(unsigned int *)0xffff0ffc)
  729. *
  730. * User space may read this to determine the curent number of helpers
  731. * available.
  732. */
  733. __kuser_helper_version: @ 0xffff0ffc
  734. .word ((__kuser_helper_end - __kuser_helper_start) >> 5)
  735. .globl __kuser_helper_end
  736. __kuser_helper_end:
  737. /*
  738. * Vector stubs.
  739. *
  740. * This code is copied to 0xffff0200 so we can use branches in the
  741. * vectors, rather than ldr's. Note that this code must not
  742. * exceed 0x300 bytes.
  743. *
  744. * Common stub entry macro:
  745. * Enter in IRQ mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
  746. *
  747. * SP points to a minimal amount of processor-private memory, the address
  748. * of which is copied into r0 for the mode specific abort handler.
  749. */
  750. .macro vector_stub, name, mode, correction=0
  751. .align 5
  752. vector_\name:
  753. .if \correction
  754. sub lr, lr, #\correction
  755. .endif
  756. @
  757. @ Save r0, lr_<exception> (parent PC) and spsr_<exception>
  758. @ (parent CPSR)
  759. @
  760. stmia sp, {r0, lr} @ save r0, lr
  761. mrs lr, spsr
  762. str lr, [sp, #8] @ save spsr
  763. @
  764. @ Prepare for SVC32 mode. IRQs remain disabled.
  765. @
  766. mrs r0, cpsr
  767. eor r0, r0, #(\mode ^ SVC_MODE)
  768. msr spsr_cxsf, r0
  769. @
  770. @ the branch table must immediately follow this code
  771. @
  772. and lr, lr, #0x0f
  773. mov r0, sp
  774. ldr lr, [pc, lr, lsl #2]
  775. movs pc, lr @ branch to handler in SVC mode
  776. .endm
  777. .globl __stubs_start
  778. __stubs_start:
  779. /*
  780. * Interrupt dispatcher
  781. */
  782. vector_stub irq, IRQ_MODE, 4
  783. .long __irq_usr @ 0 (USR_26 / USR_32)
  784. .long __irq_invalid @ 1 (FIQ_26 / FIQ_32)
  785. .long __irq_invalid @ 2 (IRQ_26 / IRQ_32)
  786. .long __irq_svc @ 3 (SVC_26 / SVC_32)
  787. .long __irq_invalid @ 4
  788. .long __irq_invalid @ 5
  789. .long __irq_invalid @ 6
  790. .long __irq_invalid @ 7
  791. .long __irq_invalid @ 8
  792. .long __irq_invalid @ 9
  793. .long __irq_invalid @ a
  794. .long __irq_invalid @ b
  795. .long __irq_invalid @ c
  796. .long __irq_invalid @ d
  797. .long __irq_invalid @ e
  798. .long __irq_invalid @ f
  799. /*
  800. * Data abort dispatcher
  801. * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
  802. */
  803. vector_stub dabt, ABT_MODE, 8
  804. .long __dabt_usr @ 0 (USR_26 / USR_32)
  805. .long __dabt_invalid @ 1 (FIQ_26 / FIQ_32)
  806. .long __dabt_invalid @ 2 (IRQ_26 / IRQ_32)
  807. .long __dabt_svc @ 3 (SVC_26 / SVC_32)
  808. .long __dabt_invalid @ 4
  809. .long __dabt_invalid @ 5
  810. .long __dabt_invalid @ 6
  811. .long __dabt_invalid @ 7
  812. .long __dabt_invalid @ 8
  813. .long __dabt_invalid @ 9
  814. .long __dabt_invalid @ a
  815. .long __dabt_invalid @ b
  816. .long __dabt_invalid @ c
  817. .long __dabt_invalid @ d
  818. .long __dabt_invalid @ e
  819. .long __dabt_invalid @ f
  820. /*
  821. * Prefetch abort dispatcher
  822. * Enter in ABT mode, spsr = USR CPSR, lr = USR PC
  823. */
  824. vector_stub pabt, ABT_MODE, 4
  825. .long __pabt_usr @ 0 (USR_26 / USR_32)
  826. .long __pabt_invalid @ 1 (FIQ_26 / FIQ_32)
  827. .long __pabt_invalid @ 2 (IRQ_26 / IRQ_32)
  828. .long __pabt_svc @ 3 (SVC_26 / SVC_32)
  829. .long __pabt_invalid @ 4
  830. .long __pabt_invalid @ 5
  831. .long __pabt_invalid @ 6
  832. .long __pabt_invalid @ 7
  833. .long __pabt_invalid @ 8
  834. .long __pabt_invalid @ 9
  835. .long __pabt_invalid @ a
  836. .long __pabt_invalid @ b
  837. .long __pabt_invalid @ c
  838. .long __pabt_invalid @ d
  839. .long __pabt_invalid @ e
  840. .long __pabt_invalid @ f
  841. /*
  842. * Undef instr entry dispatcher
  843. * Enter in UND mode, spsr = SVC/USR CPSR, lr = SVC/USR PC
  844. */
  845. vector_stub und, UND_MODE
  846. .long __und_usr @ 0 (USR_26 / USR_32)
  847. .long __und_invalid @ 1 (FIQ_26 / FIQ_32)
  848. .long __und_invalid @ 2 (IRQ_26 / IRQ_32)
  849. .long __und_svc @ 3 (SVC_26 / SVC_32)
  850. .long __und_invalid @ 4
  851. .long __und_invalid @ 5
  852. .long __und_invalid @ 6
  853. .long __und_invalid @ 7
  854. .long __und_invalid @ 8
  855. .long __und_invalid @ 9
  856. .long __und_invalid @ a
  857. .long __und_invalid @ b
  858. .long __und_invalid @ c
  859. .long __und_invalid @ d
  860. .long __und_invalid @ e
  861. .long __und_invalid @ f
  862. .align 5
  863. /*=============================================================================
  864. * Undefined FIQs
  865. *-----------------------------------------------------------------------------
  866. * Enter in FIQ mode, spsr = ANY CPSR, lr = ANY PC
  867. * MUST PRESERVE SVC SPSR, but need to switch to SVC mode to show our msg.
  868. * Basically to switch modes, we *HAVE* to clobber one register... brain
  869. * damage alert! I don't think that we can execute any code in here in any
  870. * other mode than FIQ... Ok you can switch to another mode, but you can't
  871. * get out of that mode without clobbering one register.
  872. */
  873. vector_fiq:
  874. disable_fiq
  875. subs pc, lr, #4
  876. /*=============================================================================
  877. * Address exception handler
  878. *-----------------------------------------------------------------------------
  879. * These aren't too critical.
  880. * (they're not supposed to happen, and won't happen in 32-bit data mode).
  881. */
  882. vector_addrexcptn:
  883. b vector_addrexcptn
  884. /*
  885. * We group all the following data together to optimise
  886. * for CPUs with separate I & D caches.
  887. */
  888. .align 5
  889. .LCvswi:
  890. .word vector_swi
  891. .globl __stubs_end
  892. __stubs_end:
  893. .equ stubs_offset, __vectors_start + 0x200 - __stubs_start
  894. .globl __vectors_start
  895. __vectors_start:
  896. swi SYS_ERROR0
  897. b vector_und + stubs_offset
  898. ldr pc, .LCvswi + stubs_offset
  899. b vector_pabt + stubs_offset
  900. b vector_dabt + stubs_offset
  901. b vector_addrexcptn + stubs_offset
  902. b vector_irq + stubs_offset
  903. b vector_fiq + stubs_offset
  904. .globl __vectors_end
  905. __vectors_end:
  906. .data
  907. .globl cr_alignment
  908. .globl cr_no_alignment
  909. cr_alignment:
  910. .space 4
  911. cr_no_alignment:
  912. .space 4