entry_64.S 25 KB

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