entry_64.S 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Rewritten by Cort Dougan (cort@cs.nmt.edu) for PReP
  5. * Copyright (C) 1996 Cort Dougan <cort@cs.nmt.edu>
  6. * Adapted for Power Macintosh by Paul Mackerras.
  7. * Low-level exception handlers and MMU support
  8. * rewritten by Paul Mackerras.
  9. * Copyright (C) 1996 Paul Mackerras.
  10. * MPC8xx modifications Copyright (C) 1997 Dan Malek (dmalek@jlc.net).
  11. *
  12. * This file contains the system call entry code, context switch
  13. * code, and exception/interrupt return code for PowerPC.
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/errno.h>
  21. #include <asm/unistd.h>
  22. #include <asm/processor.h>
  23. #include <asm/page.h>
  24. #include <asm/mmu.h>
  25. #include <asm/thread_info.h>
  26. #include <asm/ppc_asm.h>
  27. #include <asm/asm-offsets.h>
  28. #include <asm/cputable.h>
  29. #include <asm/firmware.h>
  30. #include <asm/bug.h>
  31. #include <asm/ptrace.h>
  32. #include <asm/irqflags.h>
  33. #include <asm/ftrace.h>
  34. /*
  35. * System calls.
  36. */
  37. .section ".toc","aw"
  38. .SYS_CALL_TABLE:
  39. .tc .sys_call_table[TC],.sys_call_table
  40. /* This value is used to mark exception frames on the stack. */
  41. exception_marker:
  42. .tc ID_EXC_MARKER[TC],STACK_FRAME_REGS_MARKER
  43. .section ".text"
  44. .align 7
  45. #undef SHOW_SYSCALLS
  46. .globl system_call_common
  47. system_call_common:
  48. andi. r10,r12,MSR_PR
  49. mr r10,r1
  50. addi r1,r1,-INT_FRAME_SIZE
  51. beq- 1f
  52. ld r1,PACAKSAVE(r13)
  53. 1: std r10,0(r1)
  54. std r11,_NIP(r1)
  55. std r12,_MSR(r1)
  56. std r0,GPR0(r1)
  57. std r10,GPR1(r1)
  58. ACCOUNT_CPU_USER_ENTRY(r10, r11)
  59. /*
  60. * This "crclr so" clears CR0.SO, which is the error indication on
  61. * return from this system call. There must be no cmp instruction
  62. * between it and the "mfcr r9" below, otherwise if XER.SO is set,
  63. * CR0.SO will get set, causing all system calls to appear to fail.
  64. */
  65. crclr so
  66. std r2,GPR2(r1)
  67. std r3,GPR3(r1)
  68. std r4,GPR4(r1)
  69. std r5,GPR5(r1)
  70. std r6,GPR6(r1)
  71. std r7,GPR7(r1)
  72. std r8,GPR8(r1)
  73. li r11,0
  74. std r11,GPR9(r1)
  75. std r11,GPR10(r1)
  76. std r11,GPR11(r1)
  77. std r11,GPR12(r1)
  78. std r9,GPR13(r1)
  79. mfcr r9
  80. mflr r10
  81. li r11,0xc01
  82. std r9,_CCR(r1)
  83. std r10,_LINK(r1)
  84. std r11,_TRAP(r1)
  85. mfxer r9
  86. mfctr r10
  87. std r9,_XER(r1)
  88. std r10,_CTR(r1)
  89. std r3,ORIG_GPR3(r1)
  90. ld r2,PACATOC(r13)
  91. addi r9,r1,STACK_FRAME_OVERHEAD
  92. ld r11,exception_marker@toc(r2)
  93. std r11,-16(r9) /* "regshere" marker */
  94. #if defined(CONFIG_VIRT_CPU_ACCOUNTING) && defined(CONFIG_PPC_SPLPAR)
  95. BEGIN_FW_FTR_SECTION
  96. beq 33f
  97. /* if from user, see if there are any DTL entries to process */
  98. ld r10,PACALPPACAPTR(r13) /* get ptr to VPA */
  99. ld r11,PACA_DTL_RIDX(r13) /* get log read index */
  100. ld r10,LPPACA_DTLIDX(r10) /* get log write index */
  101. cmpd cr1,r11,r10
  102. beq+ cr1,33f
  103. bl .accumulate_stolen_time
  104. REST_GPR(0,r1)
  105. REST_4GPRS(3,r1)
  106. REST_2GPRS(7,r1)
  107. addi r9,r1,STACK_FRAME_OVERHEAD
  108. 33:
  109. END_FW_FTR_SECTION_IFSET(FW_FEATURE_SPLPAR)
  110. #endif /* CONFIG_VIRT_CPU_ACCOUNTING && CONFIG_PPC_SPLPAR */
  111. #ifdef CONFIG_TRACE_IRQFLAGS
  112. bl .trace_hardirqs_on
  113. REST_GPR(0,r1)
  114. REST_4GPRS(3,r1)
  115. REST_2GPRS(7,r1)
  116. addi r9,r1,STACK_FRAME_OVERHEAD
  117. ld r12,_MSR(r1)
  118. #endif /* CONFIG_TRACE_IRQFLAGS */
  119. li r10,1
  120. stb r10,PACASOFTIRQEN(r13)
  121. stb r10,PACAHARDIRQEN(r13)
  122. std r10,SOFTE(r1)
  123. /* Hard enable interrupts */
  124. #ifdef CONFIG_PPC_BOOK3E
  125. wrteei 1
  126. #else
  127. mfmsr r11
  128. ori r11,r11,MSR_EE
  129. mtmsrd r11,1
  130. #endif /* CONFIG_PPC_BOOK3E */
  131. #ifdef SHOW_SYSCALLS
  132. bl .do_show_syscall
  133. REST_GPR(0,r1)
  134. REST_4GPRS(3,r1)
  135. REST_2GPRS(7,r1)
  136. addi r9,r1,STACK_FRAME_OVERHEAD
  137. #endif
  138. clrrdi r11,r1,THREAD_SHIFT
  139. ld r10,TI_FLAGS(r11)
  140. andi. r11,r10,_TIF_SYSCALL_T_OR_A
  141. bne- syscall_dotrace
  142. syscall_dotrace_cont:
  143. cmpldi 0,r0,NR_syscalls
  144. bge- syscall_enosys
  145. system_call: /* label this so stack traces look sane */
  146. /*
  147. * Need to vector to 32 Bit or default sys_call_table here,
  148. * based on caller's run-mode / personality.
  149. */
  150. ld r11,.SYS_CALL_TABLE@toc(2)
  151. andi. r10,r10,_TIF_32BIT
  152. beq 15f
  153. addi r11,r11,8 /* use 32-bit syscall entries */
  154. clrldi r3,r3,32
  155. clrldi r4,r4,32
  156. clrldi r5,r5,32
  157. clrldi r6,r6,32
  158. clrldi r7,r7,32
  159. clrldi r8,r8,32
  160. 15:
  161. slwi r0,r0,4
  162. ldx r10,r11,r0 /* Fetch system call handler [ptr] */
  163. mtctr r10
  164. bctrl /* Call handler */
  165. syscall_exit:
  166. std r3,RESULT(r1)
  167. #ifdef SHOW_SYSCALLS
  168. bl .do_show_syscall_exit
  169. ld r3,RESULT(r1)
  170. #endif
  171. clrrdi r12,r1,THREAD_SHIFT
  172. ld r8,_MSR(r1)
  173. #ifdef CONFIG_PPC_BOOK3S
  174. /* No MSR:RI on BookE */
  175. andi. r10,r8,MSR_RI
  176. beq- unrecov_restore
  177. #endif
  178. /* Disable interrupts so current_thread_info()->flags can't change,
  179. * and so that we don't get interrupted after loading SRR0/1.
  180. */
  181. #ifdef CONFIG_PPC_BOOK3E
  182. wrteei 0
  183. #else
  184. mfmsr r10
  185. rldicl r10,r10,48,1
  186. rotldi r10,r10,16
  187. mtmsrd r10,1
  188. #endif /* CONFIG_PPC_BOOK3E */
  189. ld r9,TI_FLAGS(r12)
  190. li r11,-_LAST_ERRNO
  191. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  192. bne- syscall_exit_work
  193. cmpld r3,r11
  194. ld r5,_CCR(r1)
  195. bge- syscall_error
  196. syscall_error_cont:
  197. ld r7,_NIP(r1)
  198. BEGIN_FTR_SECTION
  199. stdcx. r0,0,r1 /* to clear the reservation */
  200. END_FTR_SECTION_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  201. andi. r6,r8,MSR_PR
  202. ld r4,_LINK(r1)
  203. /*
  204. * Clear RI before restoring r13. If we are returning to
  205. * userspace and we take an exception after restoring r13,
  206. * we end up corrupting the userspace r13 value.
  207. */
  208. #ifdef CONFIG_PPC_BOOK3S
  209. /* No MSR:RI on BookE */
  210. li r12,MSR_RI
  211. andc r11,r10,r12
  212. mtmsrd r11,1 /* clear MSR.RI */
  213. #endif /* CONFIG_PPC_BOOK3S */
  214. beq- 1f
  215. ACCOUNT_CPU_USER_EXIT(r11, r12)
  216. ld r13,GPR13(r1) /* only restore r13 if returning to usermode */
  217. 1: ld r2,GPR2(r1)
  218. ld r1,GPR1(r1)
  219. mtlr r4
  220. mtcr r5
  221. mtspr SPRN_SRR0,r7
  222. mtspr SPRN_SRR1,r8
  223. RFI
  224. b . /* prevent speculative execution */
  225. syscall_error:
  226. oris r5,r5,0x1000 /* Set SO bit in CR */
  227. neg r3,r3
  228. std r5,_CCR(r1)
  229. b syscall_error_cont
  230. /* Traced system call support */
  231. syscall_dotrace:
  232. bl .save_nvgprs
  233. addi r3,r1,STACK_FRAME_OVERHEAD
  234. bl .do_syscall_trace_enter
  235. /*
  236. * Restore argument registers possibly just changed.
  237. * We use the return value of do_syscall_trace_enter
  238. * for the call number to look up in the table (r0).
  239. */
  240. mr r0,r3
  241. ld r3,GPR3(r1)
  242. ld r4,GPR4(r1)
  243. ld r5,GPR5(r1)
  244. ld r6,GPR6(r1)
  245. ld r7,GPR7(r1)
  246. ld r8,GPR8(r1)
  247. addi r9,r1,STACK_FRAME_OVERHEAD
  248. clrrdi r10,r1,THREAD_SHIFT
  249. ld r10,TI_FLAGS(r10)
  250. b syscall_dotrace_cont
  251. syscall_enosys:
  252. li r3,-ENOSYS
  253. b syscall_exit
  254. syscall_exit_work:
  255. /* If TIF_RESTOREALL is set, don't scribble on either r3 or ccr.
  256. If TIF_NOERROR is set, just save r3 as it is. */
  257. andi. r0,r9,_TIF_RESTOREALL
  258. beq+ 0f
  259. REST_NVGPRS(r1)
  260. b 2f
  261. 0: cmpld r3,r11 /* r10 is -LAST_ERRNO */
  262. blt+ 1f
  263. andi. r0,r9,_TIF_NOERROR
  264. bne- 1f
  265. ld r5,_CCR(r1)
  266. neg r3,r3
  267. oris r5,r5,0x1000 /* Set SO bit in CR */
  268. std r5,_CCR(r1)
  269. 1: std r3,GPR3(r1)
  270. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  271. beq 4f
  272. /* Clear per-syscall TIF flags if any are set. */
  273. li r11,_TIF_PERSYSCALL_MASK
  274. addi r12,r12,TI_FLAGS
  275. 3: ldarx r10,0,r12
  276. andc r10,r10,r11
  277. stdcx. r10,0,r12
  278. bne- 3b
  279. subi r12,r12,TI_FLAGS
  280. 4: /* Anything else left to do? */
  281. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  282. beq .ret_from_except_lite
  283. /* Re-enable interrupts */
  284. #ifdef CONFIG_PPC_BOOK3E
  285. wrteei 1
  286. #else
  287. mfmsr r10
  288. ori r10,r10,MSR_EE
  289. mtmsrd r10,1
  290. #endif /* CONFIG_PPC_BOOK3E */
  291. bl .save_nvgprs
  292. addi r3,r1,STACK_FRAME_OVERHEAD
  293. bl .do_syscall_trace_leave
  294. b .ret_from_except
  295. /* Save non-volatile GPRs, if not already saved. */
  296. _GLOBAL(save_nvgprs)
  297. ld r11,_TRAP(r1)
  298. andi. r0,r11,1
  299. beqlr-
  300. SAVE_NVGPRS(r1)
  301. clrrdi r0,r11,1
  302. std r0,_TRAP(r1)
  303. blr
  304. /*
  305. * The sigsuspend and rt_sigsuspend system calls can call do_signal
  306. * and thus put the process into the stopped state where we might
  307. * want to examine its user state with ptrace. Therefore we need
  308. * to save all the nonvolatile registers (r14 - r31) before calling
  309. * the C code. Similarly, fork, vfork and clone need the full
  310. * register state on the stack so that it can be copied to the child.
  311. */
  312. _GLOBAL(ppc_fork)
  313. bl .save_nvgprs
  314. bl .sys_fork
  315. b syscall_exit
  316. _GLOBAL(ppc_vfork)
  317. bl .save_nvgprs
  318. bl .sys_vfork
  319. b syscall_exit
  320. _GLOBAL(ppc_clone)
  321. bl .save_nvgprs
  322. bl .sys_clone
  323. b syscall_exit
  324. _GLOBAL(ppc32_swapcontext)
  325. bl .save_nvgprs
  326. bl .compat_sys_swapcontext
  327. b syscall_exit
  328. _GLOBAL(ppc64_swapcontext)
  329. bl .save_nvgprs
  330. bl .sys_swapcontext
  331. b syscall_exit
  332. _GLOBAL(ret_from_fork)
  333. bl .schedule_tail
  334. REST_NVGPRS(r1)
  335. li r3,0
  336. b syscall_exit
  337. /*
  338. * This routine switches between two different tasks. The process
  339. * state of one is saved on its kernel stack. Then the state
  340. * of the other is restored from its kernel stack. The memory
  341. * management hardware is updated to the second process's state.
  342. * Finally, we can return to the second process, via ret_from_except.
  343. * On entry, r3 points to the THREAD for the current task, r4
  344. * points to the THREAD for the new task.
  345. *
  346. * Note: there are two ways to get to the "going out" portion
  347. * of this code; either by coming in via the entry (_switch)
  348. * or via "fork" which must set up an environment equivalent
  349. * to the "_switch" path. If you change this you'll have to change
  350. * the fork code also.
  351. *
  352. * The code which creates the new task context is in 'copy_thread'
  353. * in arch/powerpc/kernel/process.c
  354. */
  355. .align 7
  356. _GLOBAL(_switch)
  357. mflr r0
  358. std r0,16(r1)
  359. stdu r1,-SWITCH_FRAME_SIZE(r1)
  360. /* r3-r13 are caller saved -- Cort */
  361. SAVE_8GPRS(14, r1)
  362. SAVE_10GPRS(22, r1)
  363. mflr r20 /* Return to switch caller */
  364. mfmsr r22
  365. li r0, MSR_FP
  366. #ifdef CONFIG_VSX
  367. BEGIN_FTR_SECTION
  368. oris r0,r0,MSR_VSX@h /* Disable VSX */
  369. END_FTR_SECTION_IFSET(CPU_FTR_VSX)
  370. #endif /* CONFIG_VSX */
  371. #ifdef CONFIG_ALTIVEC
  372. BEGIN_FTR_SECTION
  373. oris r0,r0,MSR_VEC@h /* Disable altivec */
  374. mfspr r24,SPRN_VRSAVE /* save vrsave register value */
  375. std r24,THREAD_VRSAVE(r3)
  376. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  377. #endif /* CONFIG_ALTIVEC */
  378. #ifdef CONFIG_PPC64
  379. BEGIN_FTR_SECTION
  380. mfspr r25,SPRN_DSCR
  381. std r25,THREAD_DSCR(r3)
  382. END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
  383. #endif
  384. and. r0,r0,r22
  385. beq+ 1f
  386. andc r22,r22,r0
  387. MTMSRD(r22)
  388. isync
  389. 1: std r20,_NIP(r1)
  390. mfcr r23
  391. std r23,_CCR(r1)
  392. std r1,KSP(r3) /* Set old stack pointer */
  393. #ifdef CONFIG_SMP
  394. /* We need a sync somewhere here to make sure that if the
  395. * previous task gets rescheduled on another CPU, it sees all
  396. * stores it has performed on this one.
  397. */
  398. sync
  399. #endif /* CONFIG_SMP */
  400. /*
  401. * If we optimise away the clear of the reservation in system
  402. * calls because we know the CPU tracks the address of the
  403. * reservation, then we need to clear it here to cover the
  404. * case that the kernel context switch path has no larx
  405. * instructions.
  406. */
  407. BEGIN_FTR_SECTION
  408. ldarx r6,0,r1
  409. END_FTR_SECTION_IFSET(CPU_FTR_STCX_CHECKS_ADDRESS)
  410. addi r6,r4,-THREAD /* Convert THREAD to 'current' */
  411. std r6,PACACURRENT(r13) /* Set new 'current' */
  412. ld r8,KSP(r4) /* new stack pointer */
  413. #ifdef CONFIG_PPC_BOOK3S
  414. BEGIN_FTR_SECTION
  415. BEGIN_FTR_SECTION_NESTED(95)
  416. clrrdi r6,r8,28 /* get its ESID */
  417. clrrdi r9,r1,28 /* get current sp ESID */
  418. FTR_SECTION_ELSE_NESTED(95)
  419. clrrdi r6,r8,40 /* get its 1T ESID */
  420. clrrdi r9,r1,40 /* get current sp 1T ESID */
  421. ALT_MMU_FTR_SECTION_END_NESTED_IFCLR(MMU_FTR_1T_SEGMENT, 95)
  422. FTR_SECTION_ELSE
  423. b 2f
  424. ALT_MMU_FTR_SECTION_END_IFSET(MMU_FTR_SLB)
  425. clrldi. r0,r6,2 /* is new ESID c00000000? */
  426. cmpd cr1,r6,r9 /* or is new ESID the same as current ESID? */
  427. cror eq,4*cr1+eq,eq
  428. beq 2f /* if yes, don't slbie it */
  429. /* Bolt in the new stack SLB entry */
  430. ld r7,KSP_VSID(r4) /* Get new stack's VSID */
  431. oris r0,r6,(SLB_ESID_V)@h
  432. ori r0,r0,(SLB_NUM_BOLTED-1)@l
  433. BEGIN_FTR_SECTION
  434. li r9,MMU_SEGSIZE_1T /* insert B field */
  435. oris r6,r6,(MMU_SEGSIZE_1T << SLBIE_SSIZE_SHIFT)@h
  436. rldimi r7,r9,SLB_VSID_SSIZE_SHIFT,0
  437. END_MMU_FTR_SECTION_IFSET(MMU_FTR_1T_SEGMENT)
  438. /* Update the last bolted SLB. No write barriers are needed
  439. * here, provided we only update the current CPU's SLB shadow
  440. * buffer.
  441. */
  442. ld r9,PACA_SLBSHADOWPTR(r13)
  443. li r12,0
  444. std r12,SLBSHADOW_STACKESID(r9) /* Clear ESID */
  445. std r7,SLBSHADOW_STACKVSID(r9) /* Save VSID */
  446. std r0,SLBSHADOW_STACKESID(r9) /* Save ESID */
  447. /* No need to check for MMU_FTR_NO_SLBIE_B here, since when
  448. * we have 1TB segments, the only CPUs known to have the errata
  449. * only support less than 1TB of system memory and we'll never
  450. * actually hit this code path.
  451. */
  452. slbie r6
  453. slbie r6 /* Workaround POWER5 < DD2.1 issue */
  454. slbmte r7,r0
  455. isync
  456. 2:
  457. #endif /* !CONFIG_PPC_BOOK3S */
  458. clrrdi r7,r8,THREAD_SHIFT /* base of new stack */
  459. /* Note: this uses SWITCH_FRAME_SIZE rather than INT_FRAME_SIZE
  460. because we don't need to leave the 288-byte ABI gap at the
  461. top of the kernel stack. */
  462. addi r7,r7,THREAD_SIZE-SWITCH_FRAME_SIZE
  463. mr r1,r8 /* start using new stack pointer */
  464. std r7,PACAKSAVE(r13)
  465. ld r6,_CCR(r1)
  466. mtcrf 0xFF,r6
  467. #ifdef CONFIG_ALTIVEC
  468. BEGIN_FTR_SECTION
  469. ld r0,THREAD_VRSAVE(r4)
  470. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  471. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  472. #endif /* CONFIG_ALTIVEC */
  473. #ifdef CONFIG_PPC64
  474. BEGIN_FTR_SECTION
  475. ld r0,THREAD_DSCR(r4)
  476. cmpd r0,r25
  477. beq 1f
  478. mtspr SPRN_DSCR,r0
  479. 1:
  480. END_FTR_SECTION_IFSET(CPU_FTR_DSCR)
  481. #endif
  482. /* r3-r13 are destroyed -- Cort */
  483. REST_8GPRS(14, r1)
  484. REST_10GPRS(22, r1)
  485. /* convert old thread to its task_struct for return value */
  486. addi r3,r3,-THREAD
  487. ld r7,_NIP(r1) /* Return to _switch caller in new task */
  488. mtlr r7
  489. addi r1,r1,SWITCH_FRAME_SIZE
  490. blr
  491. .align 7
  492. _GLOBAL(ret_from_except)
  493. ld r11,_TRAP(r1)
  494. andi. r0,r11,1
  495. bne .ret_from_except_lite
  496. REST_NVGPRS(r1)
  497. _GLOBAL(ret_from_except_lite)
  498. /*
  499. * Disable interrupts so that current_thread_info()->flags
  500. * can't change between when we test it and when we return
  501. * from the interrupt.
  502. */
  503. #ifdef CONFIG_PPC_BOOK3E
  504. wrteei 0
  505. #else
  506. mfmsr r10 /* Get current interrupt state */
  507. rldicl r9,r10,48,1 /* clear MSR_EE */
  508. rotldi r9,r9,16
  509. mtmsrd r9,1 /* Update machine state */
  510. #endif /* CONFIG_PPC_BOOK3E */
  511. #ifdef CONFIG_PREEMPT
  512. clrrdi r9,r1,THREAD_SHIFT /* current_thread_info() */
  513. li r0,_TIF_NEED_RESCHED /* bits to check */
  514. ld r3,_MSR(r1)
  515. ld r4,TI_FLAGS(r9)
  516. /* Move MSR_PR bit in r3 to _TIF_SIGPENDING position in r0 */
  517. rlwimi r0,r3,32+TIF_SIGPENDING-MSR_PR_LG,_TIF_SIGPENDING
  518. and. r0,r4,r0 /* check NEED_RESCHED and maybe SIGPENDING */
  519. bne do_work
  520. #else /* !CONFIG_PREEMPT */
  521. ld r3,_MSR(r1) /* Returning to user mode? */
  522. andi. r3,r3,MSR_PR
  523. beq restore /* if not, just restore regs and return */
  524. /* Check current_thread_info()->flags */
  525. clrrdi r9,r1,THREAD_SHIFT
  526. ld r4,TI_FLAGS(r9)
  527. andi. r0,r4,_TIF_USER_WORK_MASK
  528. bne do_work
  529. #endif /* !CONFIG_PREEMPT */
  530. restore:
  531. ld r5,SOFTE(r1)
  532. TRACE_AND_RESTORE_IRQ(r5);
  533. /* extract EE bit and use it to restore paca->hard_enabled */
  534. ld r3,_MSR(r1)
  535. rldicl r4,r3,49,63 /* r0 = (r3 >> 15) & 1 */
  536. stb r4,PACAHARDIRQEN(r13)
  537. #ifdef CONFIG_PPC_BOOK3E
  538. b .exception_return_book3e
  539. #else
  540. ld r4,_CTR(r1)
  541. ld r0,_LINK(r1)
  542. mtctr r4
  543. mtlr r0
  544. ld r4,_XER(r1)
  545. mtspr SPRN_XER,r4
  546. REST_8GPRS(5, r1)
  547. andi. r0,r3,MSR_RI
  548. beq- unrecov_restore
  549. /*
  550. * Clear the reservation. If we know the CPU tracks the address of
  551. * the reservation then we can potentially save some cycles and use
  552. * a larx. On POWER6 and POWER7 this is significantly faster.
  553. */
  554. BEGIN_FTR_SECTION
  555. stdcx. r0,0,r1 /* to clear the reservation */
  556. FTR_SECTION_ELSE
  557. ldarx r4,0,r1
  558. ALT_FTR_SECTION_END_IFCLR(CPU_FTR_STCX_CHECKS_ADDRESS)
  559. /*
  560. * Clear RI before restoring r13. If we are returning to
  561. * userspace and we take an exception after restoring r13,
  562. * we end up corrupting the userspace r13 value.
  563. */
  564. mfmsr r4
  565. andc r4,r4,r0 /* r0 contains MSR_RI here */
  566. mtmsrd r4,1
  567. /*
  568. * r13 is our per cpu area, only restore it if we are returning to
  569. * userspace
  570. */
  571. andi. r0,r3,MSR_PR
  572. beq 1f
  573. ACCOUNT_CPU_USER_EXIT(r2, r4)
  574. REST_GPR(13, r1)
  575. 1:
  576. mtspr SPRN_SRR1,r3
  577. ld r2,_CCR(r1)
  578. mtcrf 0xFF,r2
  579. ld r2,_NIP(r1)
  580. mtspr SPRN_SRR0,r2
  581. ld r0,GPR0(r1)
  582. ld r2,GPR2(r1)
  583. ld r3,GPR3(r1)
  584. ld r4,GPR4(r1)
  585. ld r1,GPR1(r1)
  586. rfid
  587. b . /* prevent speculative execution */
  588. #endif /* CONFIG_PPC_BOOK3E */
  589. do_work:
  590. #ifdef CONFIG_PREEMPT
  591. andi. r0,r3,MSR_PR /* Returning to user mode? */
  592. bne user_work
  593. /* Check that preempt_count() == 0 and interrupts are enabled */
  594. lwz r8,TI_PREEMPT(r9)
  595. cmpwi cr1,r8,0
  596. ld r0,SOFTE(r1)
  597. cmpdi r0,0
  598. crandc eq,cr1*4+eq,eq
  599. bne restore
  600. /* Here we are preempting the current task.
  601. *
  602. * Ensure interrupts are soft-disabled. We also properly mark
  603. * the PACA to reflect the fact that they are hard-disabled
  604. * and trace the change
  605. */
  606. li r0,0
  607. stb r0,PACASOFTIRQEN(r13)
  608. stb r0,PACAHARDIRQEN(r13)
  609. TRACE_DISABLE_INTS
  610. /* Call the scheduler with soft IRQs off */
  611. 1: bl .preempt_schedule_irq
  612. /* Hard-disable interrupts again (and update PACA) */
  613. #ifdef CONFIG_PPC_BOOK3E
  614. wrteei 0
  615. #else
  616. mfmsr r10
  617. rldicl r10,r10,48,1
  618. rotldi r10,r10,16
  619. mtmsrd r10,1
  620. #endif /* CONFIG_PPC_BOOK3E */
  621. li r0,0
  622. stb r0,PACAHARDIRQEN(r13)
  623. /* Re-test flags and eventually loop */
  624. clrrdi r9,r1,THREAD_SHIFT
  625. ld r4,TI_FLAGS(r9)
  626. andi. r0,r4,_TIF_NEED_RESCHED
  627. bne 1b
  628. b restore
  629. user_work:
  630. #endif /* CONFIG_PREEMPT */
  631. /* Enable interrupts */
  632. #ifdef CONFIG_PPC_BOOK3E
  633. wrteei 1
  634. #else
  635. ori r10,r10,MSR_EE
  636. mtmsrd r10,1
  637. #endif /* CONFIG_PPC_BOOK3E */
  638. andi. r0,r4,_TIF_NEED_RESCHED
  639. beq 1f
  640. li r5,1
  641. TRACE_AND_RESTORE_IRQ(r5);
  642. bl .schedule
  643. b .ret_from_except_lite
  644. 1: bl .save_nvgprs
  645. li r5,1
  646. TRACE_AND_RESTORE_IRQ(r5);
  647. addi r3,r1,STACK_FRAME_OVERHEAD
  648. bl .do_notify_resume
  649. b .ret_from_except
  650. unrecov_restore:
  651. addi r3,r1,STACK_FRAME_OVERHEAD
  652. bl .unrecoverable_exception
  653. b unrecov_restore
  654. #ifdef CONFIG_PPC_RTAS
  655. /*
  656. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  657. * called with the MMU off.
  658. *
  659. * In addition, we need to be in 32b mode, at least for now.
  660. *
  661. * Note: r3 is an input parameter to rtas, so don't trash it...
  662. */
  663. _GLOBAL(enter_rtas)
  664. mflr r0
  665. std r0,16(r1)
  666. stdu r1,-RTAS_FRAME_SIZE(r1) /* Save SP and create stack space. */
  667. /* Because RTAS is running in 32b mode, it clobbers the high order half
  668. * of all registers that it saves. We therefore save those registers
  669. * RTAS might touch to the stack. (r0, r3-r13 are caller saved)
  670. */
  671. SAVE_GPR(2, r1) /* Save the TOC */
  672. SAVE_GPR(13, r1) /* Save paca */
  673. SAVE_8GPRS(14, r1) /* Save the non-volatiles */
  674. SAVE_10GPRS(22, r1) /* ditto */
  675. mfcr r4
  676. std r4,_CCR(r1)
  677. mfctr r5
  678. std r5,_CTR(r1)
  679. mfspr r6,SPRN_XER
  680. std r6,_XER(r1)
  681. mfdar r7
  682. std r7,_DAR(r1)
  683. mfdsisr r8
  684. std r8,_DSISR(r1)
  685. /* Temporary workaround to clear CR until RTAS can be modified to
  686. * ignore all bits.
  687. */
  688. li r0,0
  689. mtcr r0
  690. #ifdef CONFIG_BUG
  691. /* There is no way it is acceptable to get here with interrupts enabled,
  692. * check it with the asm equivalent of WARN_ON
  693. */
  694. lbz r0,PACASOFTIRQEN(r13)
  695. 1: tdnei r0,0
  696. EMIT_BUG_ENTRY 1b,__FILE__,__LINE__,BUGFLAG_WARNING
  697. #endif
  698. /* Hard-disable interrupts */
  699. mfmsr r6
  700. rldicl r7,r6,48,1
  701. rotldi r7,r7,16
  702. mtmsrd r7,1
  703. /* Unfortunately, the stack pointer and the MSR are also clobbered,
  704. * so they are saved in the PACA which allows us to restore
  705. * our original state after RTAS returns.
  706. */
  707. std r1,PACAR1(r13)
  708. std r6,PACASAVEDMSR(r13)
  709. /* Setup our real return addr */
  710. LOAD_REG_ADDR(r4,.rtas_return_loc)
  711. clrldi r4,r4,2 /* convert to realmode address */
  712. mtlr r4
  713. li r0,0
  714. ori r0,r0,MSR_EE|MSR_SE|MSR_BE|MSR_RI
  715. andc r0,r6,r0
  716. li r9,1
  717. rldicr r9,r9,MSR_SF_LG,(63-MSR_SF_LG)
  718. ori r9,r9,MSR_IR|MSR_DR|MSR_FE0|MSR_FE1|MSR_FP|MSR_RI
  719. andc r6,r0,r9
  720. sync /* disable interrupts so SRR0/1 */
  721. mtmsrd r0 /* don't get trashed */
  722. LOAD_REG_ADDR(r4, rtas)
  723. ld r5,RTASENTRY(r4) /* get the rtas->entry value */
  724. ld r4,RTASBASE(r4) /* get the rtas->base value */
  725. mtspr SPRN_SRR0,r5
  726. mtspr SPRN_SRR1,r6
  727. rfid
  728. b . /* prevent speculative execution */
  729. _STATIC(rtas_return_loc)
  730. /* relocation is off at this point */
  731. GET_PACA(r4)
  732. clrldi r4,r4,2 /* convert to realmode address */
  733. bcl 20,31,$+4
  734. 0: mflr r3
  735. ld r3,(1f-0b)(r3) /* get &.rtas_restore_regs */
  736. mfmsr r6
  737. li r0,MSR_RI
  738. andc r6,r6,r0
  739. sync
  740. mtmsrd r6
  741. ld r1,PACAR1(r4) /* Restore our SP */
  742. ld r4,PACASAVEDMSR(r4) /* Restore our MSR */
  743. mtspr SPRN_SRR0,r3
  744. mtspr SPRN_SRR1,r4
  745. rfid
  746. b . /* prevent speculative execution */
  747. .align 3
  748. 1: .llong .rtas_restore_regs
  749. _STATIC(rtas_restore_regs)
  750. /* relocation is on at this point */
  751. REST_GPR(2, r1) /* Restore the TOC */
  752. REST_GPR(13, r1) /* Restore paca */
  753. REST_8GPRS(14, r1) /* Restore the non-volatiles */
  754. REST_10GPRS(22, r1) /* ditto */
  755. GET_PACA(r13)
  756. ld r4,_CCR(r1)
  757. mtcr r4
  758. ld r5,_CTR(r1)
  759. mtctr r5
  760. ld r6,_XER(r1)
  761. mtspr SPRN_XER,r6
  762. ld r7,_DAR(r1)
  763. mtdar r7
  764. ld r8,_DSISR(r1)
  765. mtdsisr r8
  766. addi r1,r1,RTAS_FRAME_SIZE /* Unstack our frame */
  767. ld r0,16(r1) /* get return address */
  768. mtlr r0
  769. blr /* return to caller */
  770. #endif /* CONFIG_PPC_RTAS */
  771. _GLOBAL(enter_prom)
  772. mflr r0
  773. std r0,16(r1)
  774. stdu r1,-PROM_FRAME_SIZE(r1) /* Save SP and create stack space */
  775. /* Because PROM is running in 32b mode, it clobbers the high order half
  776. * of all registers that it saves. We therefore save those registers
  777. * PROM might touch to the stack. (r0, r3-r13 are caller saved)
  778. */
  779. SAVE_GPR(2, r1)
  780. SAVE_GPR(13, r1)
  781. SAVE_8GPRS(14, r1)
  782. SAVE_10GPRS(22, r1)
  783. mfcr r10
  784. mfmsr r11
  785. std r10,_CCR(r1)
  786. std r11,_MSR(r1)
  787. /* Get the PROM entrypoint */
  788. mtlr r4
  789. /* Switch MSR to 32 bits mode
  790. */
  791. #ifdef CONFIG_PPC_BOOK3E
  792. rlwinm r11,r11,0,1,31
  793. mtmsr r11
  794. #else /* CONFIG_PPC_BOOK3E */
  795. mfmsr r11
  796. li r12,1
  797. rldicr r12,r12,MSR_SF_LG,(63-MSR_SF_LG)
  798. andc r11,r11,r12
  799. li r12,1
  800. rldicr r12,r12,MSR_ISF_LG,(63-MSR_ISF_LG)
  801. andc r11,r11,r12
  802. mtmsrd r11
  803. #endif /* CONFIG_PPC_BOOK3E */
  804. isync
  805. /* Enter PROM here... */
  806. blrl
  807. /* Just make sure that r1 top 32 bits didn't get
  808. * corrupt by OF
  809. */
  810. rldicl r1,r1,0,32
  811. /* Restore the MSR (back to 64 bits) */
  812. ld r0,_MSR(r1)
  813. MTMSRD(r0)
  814. isync
  815. /* Restore other registers */
  816. REST_GPR(2, r1)
  817. REST_GPR(13, r1)
  818. REST_8GPRS(14, r1)
  819. REST_10GPRS(22, r1)
  820. ld r4,_CCR(r1)
  821. mtcr r4
  822. addi r1,r1,PROM_FRAME_SIZE
  823. ld r0,16(r1)
  824. mtlr r0
  825. blr
  826. #ifdef CONFIG_FUNCTION_TRACER
  827. #ifdef CONFIG_DYNAMIC_FTRACE
  828. _GLOBAL(mcount)
  829. _GLOBAL(_mcount)
  830. blr
  831. _GLOBAL(ftrace_caller)
  832. /* Taken from output of objdump from lib64/glibc */
  833. mflr r3
  834. ld r11, 0(r1)
  835. stdu r1, -112(r1)
  836. std r3, 128(r1)
  837. ld r4, 16(r11)
  838. subi r3, r3, MCOUNT_INSN_SIZE
  839. .globl ftrace_call
  840. ftrace_call:
  841. bl ftrace_stub
  842. nop
  843. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  844. .globl ftrace_graph_call
  845. ftrace_graph_call:
  846. b ftrace_graph_stub
  847. _GLOBAL(ftrace_graph_stub)
  848. #endif
  849. ld r0, 128(r1)
  850. mtlr r0
  851. addi r1, r1, 112
  852. _GLOBAL(ftrace_stub)
  853. blr
  854. #else
  855. _GLOBAL(mcount)
  856. blr
  857. _GLOBAL(_mcount)
  858. /* Taken from output of objdump from lib64/glibc */
  859. mflr r3
  860. ld r11, 0(r1)
  861. stdu r1, -112(r1)
  862. std r3, 128(r1)
  863. ld r4, 16(r11)
  864. subi r3, r3, MCOUNT_INSN_SIZE
  865. LOAD_REG_ADDR(r5,ftrace_trace_function)
  866. ld r5,0(r5)
  867. ld r5,0(r5)
  868. mtctr r5
  869. bctrl
  870. nop
  871. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  872. b ftrace_graph_caller
  873. #endif
  874. ld r0, 128(r1)
  875. mtlr r0
  876. addi r1, r1, 112
  877. _GLOBAL(ftrace_stub)
  878. blr
  879. #endif /* CONFIG_DYNAMIC_FTRACE */
  880. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  881. _GLOBAL(ftrace_graph_caller)
  882. /* load r4 with local address */
  883. ld r4, 128(r1)
  884. subi r4, r4, MCOUNT_INSN_SIZE
  885. /* get the parent address */
  886. ld r11, 112(r1)
  887. addi r3, r11, 16
  888. bl .prepare_ftrace_return
  889. nop
  890. ld r0, 128(r1)
  891. mtlr r0
  892. addi r1, r1, 112
  893. blr
  894. _GLOBAL(return_to_handler)
  895. /* need to save return values */
  896. std r4, -24(r1)
  897. std r3, -16(r1)
  898. std r31, -8(r1)
  899. mr r31, r1
  900. stdu r1, -112(r1)
  901. bl .ftrace_return_to_handler
  902. nop
  903. /* return value has real return address */
  904. mtlr r3
  905. ld r1, 0(r1)
  906. ld r4, -24(r1)
  907. ld r3, -16(r1)
  908. ld r31, -8(r1)
  909. /* Jump back to real return address */
  910. blr
  911. _GLOBAL(mod_return_to_handler)
  912. /* need to save return values */
  913. std r4, -32(r1)
  914. std r3, -24(r1)
  915. /* save TOC */
  916. std r2, -16(r1)
  917. std r31, -8(r1)
  918. mr r31, r1
  919. stdu r1, -112(r1)
  920. /*
  921. * We are in a module using the module's TOC.
  922. * Switch to our TOC to run inside the core kernel.
  923. */
  924. ld r2, PACATOC(r13)
  925. bl .ftrace_return_to_handler
  926. nop
  927. /* return value has real return address */
  928. mtlr r3
  929. ld r1, 0(r1)
  930. ld r4, -32(r1)
  931. ld r3, -24(r1)
  932. ld r2, -16(r1)
  933. ld r31, -8(r1)
  934. /* Jump back to real return address */
  935. blr
  936. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  937. #endif /* CONFIG_FUNCTION_TRACER */