entry_32.S 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  1. /*
  2. * PowerPC version
  3. * Copyright (C) 1995-1996 Gary Thomas (gdt@linuxppc.org)
  4. * Rewritten by Cort Dougan (cort@fsmlabs.com) for PReP
  5. * Copyright (C) 1996 Cort Dougan <cort@fsmlabs.com>
  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. */
  21. #include <linux/errno.h>
  22. #include <linux/err.h>
  23. #include <linux/sys.h>
  24. #include <linux/threads.h>
  25. #include <asm/reg.h>
  26. #include <asm/page.h>
  27. #include <asm/mmu.h>
  28. #include <asm/cputable.h>
  29. #include <asm/thread_info.h>
  30. #include <asm/ppc_asm.h>
  31. #include <asm/asm-offsets.h>
  32. #include <asm/unistd.h>
  33. #include <asm/ftrace.h>
  34. #include <asm/ptrace.h>
  35. #include <asm/export.h>
  36. /*
  37. * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
  38. */
  39. #if MSR_KERNEL >= 0x10000
  40. #define LOAD_MSR_KERNEL(r, x) lis r,(x)@h; ori r,r,(x)@l
  41. #else
  42. #define LOAD_MSR_KERNEL(r, x) li r,(x)
  43. #endif
  44. #ifdef CONFIG_BOOKE
  45. .globl mcheck_transfer_to_handler
  46. mcheck_transfer_to_handler:
  47. mfspr r0,SPRN_DSRR0
  48. stw r0,_DSRR0(r11)
  49. mfspr r0,SPRN_DSRR1
  50. stw r0,_DSRR1(r11)
  51. /* fall through */
  52. .globl debug_transfer_to_handler
  53. debug_transfer_to_handler:
  54. mfspr r0,SPRN_CSRR0
  55. stw r0,_CSRR0(r11)
  56. mfspr r0,SPRN_CSRR1
  57. stw r0,_CSRR1(r11)
  58. /* fall through */
  59. .globl crit_transfer_to_handler
  60. crit_transfer_to_handler:
  61. #ifdef CONFIG_PPC_BOOK3E_MMU
  62. mfspr r0,SPRN_MAS0
  63. stw r0,MAS0(r11)
  64. mfspr r0,SPRN_MAS1
  65. stw r0,MAS1(r11)
  66. mfspr r0,SPRN_MAS2
  67. stw r0,MAS2(r11)
  68. mfspr r0,SPRN_MAS3
  69. stw r0,MAS3(r11)
  70. mfspr r0,SPRN_MAS6
  71. stw r0,MAS6(r11)
  72. #ifdef CONFIG_PHYS_64BIT
  73. mfspr r0,SPRN_MAS7
  74. stw r0,MAS7(r11)
  75. #endif /* CONFIG_PHYS_64BIT */
  76. #endif /* CONFIG_PPC_BOOK3E_MMU */
  77. #ifdef CONFIG_44x
  78. mfspr r0,SPRN_MMUCR
  79. stw r0,MMUCR(r11)
  80. #endif
  81. mfspr r0,SPRN_SRR0
  82. stw r0,_SRR0(r11)
  83. mfspr r0,SPRN_SRR1
  84. stw r0,_SRR1(r11)
  85. /* set the stack limit to the current stack
  86. * and set the limit to protect the thread_info
  87. * struct
  88. */
  89. mfspr r8,SPRN_SPRG_THREAD
  90. lwz r0,KSP_LIMIT(r8)
  91. stw r0,SAVED_KSP_LIMIT(r11)
  92. rlwimi r0,r1,0,0,(31-THREAD_SHIFT)
  93. stw r0,KSP_LIMIT(r8)
  94. /* fall through */
  95. #endif
  96. #ifdef CONFIG_40x
  97. .globl crit_transfer_to_handler
  98. crit_transfer_to_handler:
  99. lwz r0,crit_r10@l(0)
  100. stw r0,GPR10(r11)
  101. lwz r0,crit_r11@l(0)
  102. stw r0,GPR11(r11)
  103. mfspr r0,SPRN_SRR0
  104. stw r0,crit_srr0@l(0)
  105. mfspr r0,SPRN_SRR1
  106. stw r0,crit_srr1@l(0)
  107. /* set the stack limit to the current stack
  108. * and set the limit to protect the thread_info
  109. * struct
  110. */
  111. mfspr r8,SPRN_SPRG_THREAD
  112. lwz r0,KSP_LIMIT(r8)
  113. stw r0,saved_ksp_limit@l(0)
  114. rlwimi r0,r1,0,0,(31-THREAD_SHIFT)
  115. stw r0,KSP_LIMIT(r8)
  116. /* fall through */
  117. #endif
  118. /*
  119. * This code finishes saving the registers to the exception frame
  120. * and jumps to the appropriate handler for the exception, turning
  121. * on address translation.
  122. * Note that we rely on the caller having set cr0.eq iff the exception
  123. * occurred in kernel mode (i.e. MSR:PR = 0).
  124. */
  125. .globl transfer_to_handler_full
  126. transfer_to_handler_full:
  127. SAVE_NVGPRS(r11)
  128. /* fall through */
  129. .globl transfer_to_handler
  130. transfer_to_handler:
  131. stw r2,GPR2(r11)
  132. stw r12,_NIP(r11)
  133. stw r9,_MSR(r11)
  134. andi. r2,r9,MSR_PR
  135. mfctr r12
  136. mfspr r2,SPRN_XER
  137. stw r12,_CTR(r11)
  138. stw r2,_XER(r11)
  139. mfspr r12,SPRN_SPRG_THREAD
  140. addi r2,r12,-THREAD
  141. tovirt(r2,r2) /* set r2 to current */
  142. beq 2f /* if from user, fix up THREAD.regs */
  143. addi r11,r1,STACK_FRAME_OVERHEAD
  144. stw r11,PT_REGS(r12)
  145. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  146. /* Check to see if the dbcr0 register is set up to debug. Use the
  147. internal debug mode bit to do this. */
  148. lwz r12,THREAD_DBCR0(r12)
  149. andis. r12,r12,DBCR0_IDM@h
  150. beq+ 3f
  151. /* From user and task is ptraced - load up global dbcr0 */
  152. li r12,-1 /* clear all pending debug events */
  153. mtspr SPRN_DBSR,r12
  154. lis r11,global_dbcr0@ha
  155. tophys(r11,r11)
  156. addi r11,r11,global_dbcr0@l
  157. #ifdef CONFIG_SMP
  158. CURRENT_THREAD_INFO(r9, r1)
  159. lwz r9,TI_CPU(r9)
  160. slwi r9,r9,3
  161. add r11,r11,r9
  162. #endif
  163. lwz r12,0(r11)
  164. mtspr SPRN_DBCR0,r12
  165. lwz r12,4(r11)
  166. addi r12,r12,-1
  167. stw r12,4(r11)
  168. #endif
  169. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  170. CURRENT_THREAD_INFO(r9, r1)
  171. tophys(r9, r9)
  172. ACCOUNT_CPU_USER_ENTRY(r9, r11, r12)
  173. #endif
  174. b 3f
  175. 2: /* if from kernel, check interrupted DOZE/NAP mode and
  176. * check for stack overflow
  177. */
  178. lwz r9,KSP_LIMIT(r12)
  179. cmplw r1,r9 /* if r1 <= ksp_limit */
  180. ble- stack_ovf /* then the kernel stack overflowed */
  181. 5:
  182. #if defined(CONFIG_6xx) || defined(CONFIG_E500)
  183. CURRENT_THREAD_INFO(r9, r1)
  184. tophys(r9,r9) /* check local flags */
  185. lwz r12,TI_LOCAL_FLAGS(r9)
  186. mtcrf 0x01,r12
  187. bt- 31-TLF_NAPPING,4f
  188. bt- 31-TLF_SLEEPING,7f
  189. #endif /* CONFIG_6xx || CONFIG_E500 */
  190. .globl transfer_to_handler_cont
  191. transfer_to_handler_cont:
  192. 3:
  193. mflr r9
  194. lwz r11,0(r9) /* virtual address of handler */
  195. lwz r9,4(r9) /* where to go when done */
  196. #ifdef CONFIG_TRACE_IRQFLAGS
  197. lis r12,reenable_mmu@h
  198. ori r12,r12,reenable_mmu@l
  199. mtspr SPRN_SRR0,r12
  200. mtspr SPRN_SRR1,r10
  201. SYNC
  202. RFI
  203. reenable_mmu: /* re-enable mmu so we can */
  204. mfmsr r10
  205. lwz r12,_MSR(r1)
  206. xor r10,r10,r12
  207. andi. r10,r10,MSR_EE /* Did EE change? */
  208. beq 1f
  209. /*
  210. * The trace_hardirqs_off will use CALLER_ADDR0 and CALLER_ADDR1.
  211. * If from user mode there is only one stack frame on the stack, and
  212. * accessing CALLER_ADDR1 will cause oops. So we need create a dummy
  213. * stack frame to make trace_hardirqs_off happy.
  214. *
  215. * This is handy because we also need to save a bunch of GPRs,
  216. * r3 can be different from GPR3(r1) at this point, r9 and r11
  217. * contains the old MSR and handler address respectively,
  218. * r4 & r5 can contain page fault arguments that need to be passed
  219. * along as well. r12, CCR, CTR, XER etc... are left clobbered as
  220. * they aren't useful past this point (aren't syscall arguments),
  221. * the rest is restored from the exception frame.
  222. */
  223. stwu r1,-32(r1)
  224. stw r9,8(r1)
  225. stw r11,12(r1)
  226. stw r3,16(r1)
  227. stw r4,20(r1)
  228. stw r5,24(r1)
  229. bl trace_hardirqs_off
  230. lwz r5,24(r1)
  231. lwz r4,20(r1)
  232. lwz r3,16(r1)
  233. lwz r11,12(r1)
  234. lwz r9,8(r1)
  235. addi r1,r1,32
  236. lwz r0,GPR0(r1)
  237. lwz r6,GPR6(r1)
  238. lwz r7,GPR7(r1)
  239. lwz r8,GPR8(r1)
  240. 1: mtctr r11
  241. mtlr r9
  242. bctr /* jump to handler */
  243. #else /* CONFIG_TRACE_IRQFLAGS */
  244. mtspr SPRN_SRR0,r11
  245. mtspr SPRN_SRR1,r10
  246. mtlr r9
  247. SYNC
  248. RFI /* jump to handler, enable MMU */
  249. #endif /* CONFIG_TRACE_IRQFLAGS */
  250. #if defined (CONFIG_6xx) || defined(CONFIG_E500)
  251. 4: rlwinm r12,r12,0,~_TLF_NAPPING
  252. stw r12,TI_LOCAL_FLAGS(r9)
  253. b power_save_ppc32_restore
  254. 7: rlwinm r12,r12,0,~_TLF_SLEEPING
  255. stw r12,TI_LOCAL_FLAGS(r9)
  256. lwz r9,_MSR(r11) /* if sleeping, clear MSR.EE */
  257. rlwinm r9,r9,0,~MSR_EE
  258. lwz r12,_LINK(r11) /* and return to address in LR */
  259. b fast_exception_return
  260. #endif
  261. /*
  262. * On kernel stack overflow, load up an initial stack pointer
  263. * and call StackOverflow(regs), which should not return.
  264. */
  265. stack_ovf:
  266. /* sometimes we use a statically-allocated stack, which is OK. */
  267. lis r12,_end@h
  268. ori r12,r12,_end@l
  269. cmplw r1,r12
  270. ble 5b /* r1 <= &_end is OK */
  271. SAVE_NVGPRS(r11)
  272. addi r3,r1,STACK_FRAME_OVERHEAD
  273. lis r1,init_thread_union@ha
  274. addi r1,r1,init_thread_union@l
  275. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  276. lis r9,StackOverflow@ha
  277. addi r9,r9,StackOverflow@l
  278. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  279. FIX_SRR1(r10,r12)
  280. mtspr SPRN_SRR0,r9
  281. mtspr SPRN_SRR1,r10
  282. SYNC
  283. RFI
  284. /*
  285. * Handle a system call.
  286. */
  287. .stabs "arch/powerpc/kernel/",N_SO,0,0,0f
  288. .stabs "entry_32.S",N_SO,0,0,0f
  289. 0:
  290. _GLOBAL(DoSyscall)
  291. stw r3,ORIG_GPR3(r1)
  292. li r12,0
  293. stw r12,RESULT(r1)
  294. lwz r11,_CCR(r1) /* Clear SO bit in CR */
  295. rlwinm r11,r11,0,4,2
  296. stw r11,_CCR(r1)
  297. #ifdef CONFIG_TRACE_IRQFLAGS
  298. /* Return from syscalls can (and generally will) hard enable
  299. * interrupts. You aren't supposed to call a syscall with
  300. * interrupts disabled in the first place. However, to ensure
  301. * that we get it right vs. lockdep if it happens, we force
  302. * that hard enable here with appropriate tracing if we see
  303. * that we have been called with interrupts off
  304. */
  305. mfmsr r11
  306. andi. r12,r11,MSR_EE
  307. bne+ 1f
  308. /* We came in with interrupts disabled, we enable them now */
  309. bl trace_hardirqs_on
  310. mfmsr r11
  311. lwz r0,GPR0(r1)
  312. lwz r3,GPR3(r1)
  313. lwz r4,GPR4(r1)
  314. ori r11,r11,MSR_EE
  315. lwz r5,GPR5(r1)
  316. lwz r6,GPR6(r1)
  317. lwz r7,GPR7(r1)
  318. lwz r8,GPR8(r1)
  319. mtmsr r11
  320. 1:
  321. #endif /* CONFIG_TRACE_IRQFLAGS */
  322. CURRENT_THREAD_INFO(r10, r1)
  323. lwz r11,TI_FLAGS(r10)
  324. andi. r11,r11,_TIF_SYSCALL_DOTRACE
  325. bne- syscall_dotrace
  326. syscall_dotrace_cont:
  327. cmplwi 0,r0,NR_syscalls
  328. lis r10,sys_call_table@h
  329. ori r10,r10,sys_call_table@l
  330. slwi r0,r0,2
  331. bge- 66f
  332. lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
  333. mtlr r10
  334. addi r9,r1,STACK_FRAME_OVERHEAD
  335. PPC440EP_ERR42
  336. blrl /* Call handler */
  337. .globl ret_from_syscall
  338. ret_from_syscall:
  339. mr r6,r3
  340. CURRENT_THREAD_INFO(r12, r1)
  341. /* disable interrupts so current_thread_info()->flags can't change */
  342. LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
  343. /* Note: We don't bother telling lockdep about it */
  344. SYNC
  345. MTMSRD(r10)
  346. lwz r9,TI_FLAGS(r12)
  347. li r8,-MAX_ERRNO
  348. andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  349. bne- syscall_exit_work
  350. cmplw 0,r3,r8
  351. blt+ syscall_exit_cont
  352. lwz r11,_CCR(r1) /* Load CR */
  353. neg r3,r3
  354. oris r11,r11,0x1000 /* Set SO bit in CR */
  355. stw r11,_CCR(r1)
  356. syscall_exit_cont:
  357. lwz r8,_MSR(r1)
  358. #ifdef CONFIG_TRACE_IRQFLAGS
  359. /* If we are going to return from the syscall with interrupts
  360. * off, we trace that here. It shouldn't happen though but we
  361. * want to catch the bugger if it does right ?
  362. */
  363. andi. r10,r8,MSR_EE
  364. bne+ 1f
  365. stw r3,GPR3(r1)
  366. bl trace_hardirqs_off
  367. lwz r3,GPR3(r1)
  368. 1:
  369. #endif /* CONFIG_TRACE_IRQFLAGS */
  370. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  371. /* If the process has its own DBCR0 value, load it up. The internal
  372. debug mode bit tells us that dbcr0 should be loaded. */
  373. lwz r0,THREAD+THREAD_DBCR0(r2)
  374. andis. r10,r0,DBCR0_IDM@h
  375. bnel- load_dbcr0
  376. #endif
  377. #ifdef CONFIG_44x
  378. BEGIN_MMU_FTR_SECTION
  379. lis r4,icache_44x_need_flush@ha
  380. lwz r5,icache_44x_need_flush@l(r4)
  381. cmplwi cr0,r5,0
  382. bne- 2f
  383. 1:
  384. END_MMU_FTR_SECTION_IFCLR(MMU_FTR_TYPE_47x)
  385. #endif /* CONFIG_44x */
  386. BEGIN_FTR_SECTION
  387. lwarx r7,0,r1
  388. END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
  389. stwcx. r0,0,r1 /* to clear the reservation */
  390. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  391. andi. r4,r8,MSR_PR
  392. beq 3f
  393. CURRENT_THREAD_INFO(r4, r1)
  394. ACCOUNT_CPU_USER_EXIT(r4, r5, r7)
  395. 3:
  396. #endif
  397. lwz r4,_LINK(r1)
  398. lwz r5,_CCR(r1)
  399. mtlr r4
  400. mtcr r5
  401. lwz r7,_NIP(r1)
  402. FIX_SRR1(r8, r0)
  403. lwz r2,GPR2(r1)
  404. lwz r1,GPR1(r1)
  405. mtspr SPRN_SRR0,r7
  406. mtspr SPRN_SRR1,r8
  407. SYNC
  408. RFI
  409. #ifdef CONFIG_44x
  410. 2: li r7,0
  411. iccci r0,r0
  412. stw r7,icache_44x_need_flush@l(r4)
  413. b 1b
  414. #endif /* CONFIG_44x */
  415. 66: li r3,-ENOSYS
  416. b ret_from_syscall
  417. .globl ret_from_fork
  418. ret_from_fork:
  419. REST_NVGPRS(r1)
  420. bl schedule_tail
  421. li r3,0
  422. b ret_from_syscall
  423. .globl ret_from_kernel_thread
  424. ret_from_kernel_thread:
  425. REST_NVGPRS(r1)
  426. bl schedule_tail
  427. mtlr r14
  428. mr r3,r15
  429. PPC440EP_ERR42
  430. blrl
  431. li r3,0
  432. b ret_from_syscall
  433. /* Traced system call support */
  434. syscall_dotrace:
  435. SAVE_NVGPRS(r1)
  436. li r0,0xc00
  437. stw r0,_TRAP(r1)
  438. addi r3,r1,STACK_FRAME_OVERHEAD
  439. bl do_syscall_trace_enter
  440. /*
  441. * Restore argument registers possibly just changed.
  442. * We use the return value of do_syscall_trace_enter
  443. * for call number to look up in the table (r0).
  444. */
  445. mr r0,r3
  446. lwz r3,GPR3(r1)
  447. lwz r4,GPR4(r1)
  448. lwz r5,GPR5(r1)
  449. lwz r6,GPR6(r1)
  450. lwz r7,GPR7(r1)
  451. lwz r8,GPR8(r1)
  452. REST_NVGPRS(r1)
  453. cmplwi r0,NR_syscalls
  454. /* Return code is already in r3 thanks to do_syscall_trace_enter() */
  455. bge- ret_from_syscall
  456. b syscall_dotrace_cont
  457. syscall_exit_work:
  458. andi. r0,r9,_TIF_RESTOREALL
  459. beq+ 0f
  460. REST_NVGPRS(r1)
  461. b 2f
  462. 0: cmplw 0,r3,r8
  463. blt+ 1f
  464. andi. r0,r9,_TIF_NOERROR
  465. bne- 1f
  466. lwz r11,_CCR(r1) /* Load CR */
  467. neg r3,r3
  468. oris r11,r11,0x1000 /* Set SO bit in CR */
  469. stw r11,_CCR(r1)
  470. 1: stw r6,RESULT(r1) /* Save result */
  471. stw r3,GPR3(r1) /* Update return value */
  472. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  473. beq 4f
  474. /* Clear per-syscall TIF flags if any are set. */
  475. li r11,_TIF_PERSYSCALL_MASK
  476. addi r12,r12,TI_FLAGS
  477. 3: lwarx r8,0,r12
  478. andc r8,r8,r11
  479. #ifdef CONFIG_IBM405_ERR77
  480. dcbt 0,r12
  481. #endif
  482. stwcx. r8,0,r12
  483. bne- 3b
  484. subi r12,r12,TI_FLAGS
  485. 4: /* Anything which requires enabling interrupts? */
  486. andi. r0,r9,(_TIF_SYSCALL_DOTRACE|_TIF_SINGLESTEP)
  487. beq ret_from_except
  488. /* Re-enable interrupts. There is no need to trace that with
  489. * lockdep as we are supposed to have IRQs on at this point
  490. */
  491. ori r10,r10,MSR_EE
  492. SYNC
  493. MTMSRD(r10)
  494. /* Save NVGPRS if they're not saved already */
  495. lwz r4,_TRAP(r1)
  496. andi. r4,r4,1
  497. beq 5f
  498. SAVE_NVGPRS(r1)
  499. li r4,0xc00
  500. stw r4,_TRAP(r1)
  501. 5:
  502. addi r3,r1,STACK_FRAME_OVERHEAD
  503. bl do_syscall_trace_leave
  504. b ret_from_except_full
  505. /*
  506. * The fork/clone functions need to copy the full register set into
  507. * the child process. Therefore we need to save all the nonvolatile
  508. * registers (r13 - r31) before calling the C code.
  509. */
  510. .globl ppc_fork
  511. ppc_fork:
  512. SAVE_NVGPRS(r1)
  513. lwz r0,_TRAP(r1)
  514. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  515. stw r0,_TRAP(r1) /* register set saved */
  516. b sys_fork
  517. .globl ppc_vfork
  518. ppc_vfork:
  519. SAVE_NVGPRS(r1)
  520. lwz r0,_TRAP(r1)
  521. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  522. stw r0,_TRAP(r1) /* register set saved */
  523. b sys_vfork
  524. .globl ppc_clone
  525. ppc_clone:
  526. SAVE_NVGPRS(r1)
  527. lwz r0,_TRAP(r1)
  528. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  529. stw r0,_TRAP(r1) /* register set saved */
  530. b sys_clone
  531. .globl ppc_swapcontext
  532. ppc_swapcontext:
  533. SAVE_NVGPRS(r1)
  534. lwz r0,_TRAP(r1)
  535. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  536. stw r0,_TRAP(r1) /* register set saved */
  537. b sys_swapcontext
  538. /*
  539. * Top-level page fault handling.
  540. * This is in assembler because if do_page_fault tells us that
  541. * it is a bad kernel page fault, we want to save the non-volatile
  542. * registers before calling bad_page_fault.
  543. */
  544. .globl handle_page_fault
  545. handle_page_fault:
  546. stw r4,_DAR(r1)
  547. addi r3,r1,STACK_FRAME_OVERHEAD
  548. bl do_page_fault
  549. cmpwi r3,0
  550. beq+ ret_from_except
  551. SAVE_NVGPRS(r1)
  552. lwz r0,_TRAP(r1)
  553. clrrwi r0,r0,1
  554. stw r0,_TRAP(r1)
  555. mr r5,r3
  556. addi r3,r1,STACK_FRAME_OVERHEAD
  557. lwz r4,_DAR(r1)
  558. bl bad_page_fault
  559. b ret_from_except_full
  560. /*
  561. * This routine switches between two different tasks. The process
  562. * state of one is saved on its kernel stack. Then the state
  563. * of the other is restored from its kernel stack. The memory
  564. * management hardware is updated to the second process's state.
  565. * Finally, we can return to the second process.
  566. * On entry, r3 points to the THREAD for the current task, r4
  567. * points to the THREAD for the new task.
  568. *
  569. * This routine is always called with interrupts disabled.
  570. *
  571. * Note: there are two ways to get to the "going out" portion
  572. * of this code; either by coming in via the entry (_switch)
  573. * or via "fork" which must set up an environment equivalent
  574. * to the "_switch" path. If you change this , you'll have to
  575. * change the fork code also.
  576. *
  577. * The code which creates the new task context is in 'copy_thread'
  578. * in arch/ppc/kernel/process.c
  579. */
  580. _GLOBAL(_switch)
  581. stwu r1,-INT_FRAME_SIZE(r1)
  582. mflr r0
  583. stw r0,INT_FRAME_SIZE+4(r1)
  584. /* r3-r12 are caller saved -- Cort */
  585. SAVE_NVGPRS(r1)
  586. stw r0,_NIP(r1) /* Return to switch caller */
  587. mfmsr r11
  588. li r0,MSR_FP /* Disable floating-point */
  589. #ifdef CONFIG_ALTIVEC
  590. BEGIN_FTR_SECTION
  591. oris r0,r0,MSR_VEC@h /* Disable altivec */
  592. mfspr r12,SPRN_VRSAVE /* save vrsave register value */
  593. stw r12,THREAD+THREAD_VRSAVE(r2)
  594. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  595. #endif /* CONFIG_ALTIVEC */
  596. #ifdef CONFIG_SPE
  597. BEGIN_FTR_SECTION
  598. oris r0,r0,MSR_SPE@h /* Disable SPE */
  599. mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
  600. stw r12,THREAD+THREAD_SPEFSCR(r2)
  601. END_FTR_SECTION_IFSET(CPU_FTR_SPE)
  602. #endif /* CONFIG_SPE */
  603. and. r0,r0,r11 /* FP or altivec or SPE enabled? */
  604. beq+ 1f
  605. andc r11,r11,r0
  606. MTMSRD(r11)
  607. isync
  608. 1: stw r11,_MSR(r1)
  609. mfcr r10
  610. stw r10,_CCR(r1)
  611. stw r1,KSP(r3) /* Set old stack pointer */
  612. #ifdef CONFIG_SMP
  613. /* We need a sync somewhere here to make sure that if the
  614. * previous task gets rescheduled on another CPU, it sees all
  615. * stores it has performed on this one.
  616. */
  617. sync
  618. #endif /* CONFIG_SMP */
  619. tophys(r0,r4)
  620. mtspr SPRN_SPRG_THREAD,r0 /* Update current THREAD phys addr */
  621. lwz r1,KSP(r4) /* Load new stack pointer */
  622. /* save the old current 'last' for return value */
  623. mr r3,r2
  624. addi r2,r4,-THREAD /* Update current */
  625. #ifdef CONFIG_ALTIVEC
  626. BEGIN_FTR_SECTION
  627. lwz r0,THREAD+THREAD_VRSAVE(r2)
  628. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  629. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  630. #endif /* CONFIG_ALTIVEC */
  631. #ifdef CONFIG_SPE
  632. BEGIN_FTR_SECTION
  633. lwz r0,THREAD+THREAD_SPEFSCR(r2)
  634. mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
  635. END_FTR_SECTION_IFSET(CPU_FTR_SPE)
  636. #endif /* CONFIG_SPE */
  637. lwz r0,_CCR(r1)
  638. mtcrf 0xFF,r0
  639. /* r3-r12 are destroyed -- Cort */
  640. REST_NVGPRS(r1)
  641. lwz r4,_NIP(r1) /* Return to _switch caller in new task */
  642. mtlr r4
  643. addi r1,r1,INT_FRAME_SIZE
  644. blr
  645. .globl fast_exception_return
  646. fast_exception_return:
  647. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  648. andi. r10,r9,MSR_RI /* check for recoverable interrupt */
  649. beq 1f /* if not, we've got problems */
  650. #endif
  651. 2: REST_4GPRS(3, r11)
  652. lwz r10,_CCR(r11)
  653. REST_GPR(1, r11)
  654. mtcr r10
  655. lwz r10,_LINK(r11)
  656. mtlr r10
  657. REST_GPR(10, r11)
  658. mtspr SPRN_SRR1,r9
  659. mtspr SPRN_SRR0,r12
  660. REST_GPR(9, r11)
  661. REST_GPR(12, r11)
  662. lwz r11,GPR11(r11)
  663. SYNC
  664. RFI
  665. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  666. /* check if the exception happened in a restartable section */
  667. 1: lis r3,exc_exit_restart_end@ha
  668. addi r3,r3,exc_exit_restart_end@l
  669. cmplw r12,r3
  670. bge 3f
  671. lis r4,exc_exit_restart@ha
  672. addi r4,r4,exc_exit_restart@l
  673. cmplw r12,r4
  674. blt 3f
  675. lis r3,fee_restarts@ha
  676. tophys(r3,r3)
  677. lwz r5,fee_restarts@l(r3)
  678. addi r5,r5,1
  679. stw r5,fee_restarts@l(r3)
  680. mr r12,r4 /* restart at exc_exit_restart */
  681. b 2b
  682. .section .bss
  683. .align 2
  684. fee_restarts:
  685. .space 4
  686. .previous
  687. /* aargh, a nonrecoverable interrupt, panic */
  688. /* aargh, we don't know which trap this is */
  689. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  690. 3:
  691. BEGIN_FTR_SECTION
  692. b 2b
  693. END_FTR_SECTION_IFSET(CPU_FTR_601)
  694. li r10,-1
  695. stw r10,_TRAP(r11)
  696. addi r3,r1,STACK_FRAME_OVERHEAD
  697. lis r10,MSR_KERNEL@h
  698. ori r10,r10,MSR_KERNEL@l
  699. bl transfer_to_handler_full
  700. .long nonrecoverable_exception
  701. .long ret_from_except
  702. #endif
  703. .globl ret_from_except_full
  704. ret_from_except_full:
  705. REST_NVGPRS(r1)
  706. /* fall through */
  707. .globl ret_from_except
  708. ret_from_except:
  709. /* Hard-disable interrupts so that current_thread_info()->flags
  710. * can't change between when we test it and when we return
  711. * from the interrupt. */
  712. /* Note: We don't bother telling lockdep about it */
  713. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  714. SYNC /* Some chip revs have problems here... */
  715. MTMSRD(r10) /* disable interrupts */
  716. lwz r3,_MSR(r1) /* Returning to user mode? */
  717. andi. r0,r3,MSR_PR
  718. beq resume_kernel
  719. user_exc_return: /* r10 contains MSR_KERNEL here */
  720. /* Check current_thread_info()->flags */
  721. CURRENT_THREAD_INFO(r9, r1)
  722. lwz r9,TI_FLAGS(r9)
  723. andi. r0,r9,_TIF_USER_WORK_MASK
  724. bne do_work
  725. restore_user:
  726. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  727. /* Check whether this process has its own DBCR0 value. The internal
  728. debug mode bit tells us that dbcr0 should be loaded. */
  729. lwz r0,THREAD+THREAD_DBCR0(r2)
  730. andis. r10,r0,DBCR0_IDM@h
  731. bnel- load_dbcr0
  732. #endif
  733. #ifdef CONFIG_VIRT_CPU_ACCOUNTING_NATIVE
  734. CURRENT_THREAD_INFO(r9, r1)
  735. ACCOUNT_CPU_USER_EXIT(r9, r10, r11)
  736. #endif
  737. b restore
  738. /* N.B. the only way to get here is from the beq following ret_from_except. */
  739. resume_kernel:
  740. /* check current_thread_info, _TIF_EMULATE_STACK_STORE */
  741. CURRENT_THREAD_INFO(r9, r1)
  742. lwz r8,TI_FLAGS(r9)
  743. andis. r0,r8,_TIF_EMULATE_STACK_STORE@h
  744. beq+ 1f
  745. addi r8,r1,INT_FRAME_SIZE /* Get the kprobed function entry */
  746. lwz r3,GPR1(r1)
  747. subi r3,r3,INT_FRAME_SIZE /* dst: Allocate a trampoline exception frame */
  748. mr r4,r1 /* src: current exception frame */
  749. mr r1,r3 /* Reroute the trampoline frame to r1 */
  750. /* Copy from the original to the trampoline. */
  751. li r5,INT_FRAME_SIZE/4 /* size: INT_FRAME_SIZE */
  752. li r6,0 /* start offset: 0 */
  753. mtctr r5
  754. 2: lwzx r0,r6,r4
  755. stwx r0,r6,r3
  756. addi r6,r6,4
  757. bdnz 2b
  758. /* Do real store operation to complete stwu */
  759. lwz r5,GPR1(r1)
  760. stw r8,0(r5)
  761. /* Clear _TIF_EMULATE_STACK_STORE flag */
  762. lis r11,_TIF_EMULATE_STACK_STORE@h
  763. addi r5,r9,TI_FLAGS
  764. 0: lwarx r8,0,r5
  765. andc r8,r8,r11
  766. #ifdef CONFIG_IBM405_ERR77
  767. dcbt 0,r5
  768. #endif
  769. stwcx. r8,0,r5
  770. bne- 0b
  771. 1:
  772. #ifdef CONFIG_PREEMPT
  773. /* check current_thread_info->preempt_count */
  774. lwz r0,TI_PREEMPT(r9)
  775. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  776. bne restore
  777. andi. r8,r8,_TIF_NEED_RESCHED
  778. beq+ restore
  779. lwz r3,_MSR(r1)
  780. andi. r0,r3,MSR_EE /* interrupts off? */
  781. beq restore /* don't schedule if so */
  782. #ifdef CONFIG_TRACE_IRQFLAGS
  783. /* Lockdep thinks irqs are enabled, we need to call
  784. * preempt_schedule_irq with IRQs off, so we inform lockdep
  785. * now that we -did- turn them off already
  786. */
  787. bl trace_hardirqs_off
  788. #endif
  789. 1: bl preempt_schedule_irq
  790. CURRENT_THREAD_INFO(r9, r1)
  791. lwz r3,TI_FLAGS(r9)
  792. andi. r0,r3,_TIF_NEED_RESCHED
  793. bne- 1b
  794. #ifdef CONFIG_TRACE_IRQFLAGS
  795. /* And now, to properly rebalance the above, we tell lockdep they
  796. * are being turned back on, which will happen when we return
  797. */
  798. bl trace_hardirqs_on
  799. #endif
  800. #endif /* CONFIG_PREEMPT */
  801. /* interrupts are hard-disabled at this point */
  802. restore:
  803. #ifdef CONFIG_44x
  804. BEGIN_MMU_FTR_SECTION
  805. b 1f
  806. END_MMU_FTR_SECTION_IFSET(MMU_FTR_TYPE_47x)
  807. lis r4,icache_44x_need_flush@ha
  808. lwz r5,icache_44x_need_flush@l(r4)
  809. cmplwi cr0,r5,0
  810. beq+ 1f
  811. li r6,0
  812. iccci r0,r0
  813. stw r6,icache_44x_need_flush@l(r4)
  814. 1:
  815. #endif /* CONFIG_44x */
  816. lwz r9,_MSR(r1)
  817. #ifdef CONFIG_TRACE_IRQFLAGS
  818. /* Lockdep doesn't know about the fact that IRQs are temporarily turned
  819. * off in this assembly code while peeking at TI_FLAGS() and such. However
  820. * we need to inform it if the exception turned interrupts off, and we
  821. * are about to trun them back on.
  822. *
  823. * The problem here sadly is that we don't know whether the exceptions was
  824. * one that turned interrupts off or not. So we always tell lockdep about
  825. * turning them on here when we go back to wherever we came from with EE
  826. * on, even if that may meen some redudant calls being tracked. Maybe later
  827. * we could encode what the exception did somewhere or test the exception
  828. * type in the pt_regs but that sounds overkill
  829. */
  830. andi. r10,r9,MSR_EE
  831. beq 1f
  832. /*
  833. * Since the ftrace irqsoff latency trace checks CALLER_ADDR1,
  834. * which is the stack frame here, we need to force a stack frame
  835. * in case we came from user space.
  836. */
  837. stwu r1,-32(r1)
  838. mflr r0
  839. stw r0,4(r1)
  840. stwu r1,-32(r1)
  841. bl trace_hardirqs_on
  842. lwz r1,0(r1)
  843. lwz r1,0(r1)
  844. lwz r9,_MSR(r1)
  845. 1:
  846. #endif /* CONFIG_TRACE_IRQFLAGS */
  847. lwz r0,GPR0(r1)
  848. lwz r2,GPR2(r1)
  849. REST_4GPRS(3, r1)
  850. REST_2GPRS(7, r1)
  851. lwz r10,_XER(r1)
  852. lwz r11,_CTR(r1)
  853. mtspr SPRN_XER,r10
  854. mtctr r11
  855. PPC405_ERR77(0,r1)
  856. BEGIN_FTR_SECTION
  857. lwarx r11,0,r1
  858. END_FTR_SECTION_IFSET(CPU_FTR_NEED_PAIRED_STWCX)
  859. stwcx. r0,0,r1 /* to clear the reservation */
  860. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  861. andi. r10,r9,MSR_RI /* check if this exception occurred */
  862. beql nonrecoverable /* at a bad place (MSR:RI = 0) */
  863. lwz r10,_CCR(r1)
  864. lwz r11,_LINK(r1)
  865. mtcrf 0xFF,r10
  866. mtlr r11
  867. /*
  868. * Once we put values in SRR0 and SRR1, we are in a state
  869. * where exceptions are not recoverable, since taking an
  870. * exception will trash SRR0 and SRR1. Therefore we clear the
  871. * MSR:RI bit to indicate this. If we do take an exception,
  872. * we can't return to the point of the exception but we
  873. * can restart the exception exit path at the label
  874. * exc_exit_restart below. -- paulus
  875. */
  876. LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
  877. SYNC
  878. MTMSRD(r10) /* clear the RI bit */
  879. .globl exc_exit_restart
  880. exc_exit_restart:
  881. lwz r12,_NIP(r1)
  882. FIX_SRR1(r9,r10)
  883. mtspr SPRN_SRR0,r12
  884. mtspr SPRN_SRR1,r9
  885. REST_4GPRS(9, r1)
  886. lwz r1,GPR1(r1)
  887. .globl exc_exit_restart_end
  888. exc_exit_restart_end:
  889. SYNC
  890. RFI
  891. #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
  892. /*
  893. * This is a bit different on 4xx/Book-E because it doesn't have
  894. * the RI bit in the MSR.
  895. * The TLB miss handler checks if we have interrupted
  896. * the exception exit path and restarts it if so
  897. * (well maybe one day it will... :).
  898. */
  899. lwz r11,_LINK(r1)
  900. mtlr r11
  901. lwz r10,_CCR(r1)
  902. mtcrf 0xff,r10
  903. REST_2GPRS(9, r1)
  904. .globl exc_exit_restart
  905. exc_exit_restart:
  906. lwz r11,_NIP(r1)
  907. lwz r12,_MSR(r1)
  908. exc_exit_start:
  909. mtspr SPRN_SRR0,r11
  910. mtspr SPRN_SRR1,r12
  911. REST_2GPRS(11, r1)
  912. lwz r1,GPR1(r1)
  913. .globl exc_exit_restart_end
  914. exc_exit_restart_end:
  915. PPC405_ERR77_SYNC
  916. rfi
  917. b . /* prevent prefetch past rfi */
  918. /*
  919. * Returning from a critical interrupt in user mode doesn't need
  920. * to be any different from a normal exception. For a critical
  921. * interrupt in the kernel, we just return (without checking for
  922. * preemption) since the interrupt may have happened at some crucial
  923. * place (e.g. inside the TLB miss handler), and because we will be
  924. * running with r1 pointing into critical_stack, not the current
  925. * process's kernel stack (and therefore current_thread_info() will
  926. * give the wrong answer).
  927. * We have to restore various SPRs that may have been in use at the
  928. * time of the critical interrupt.
  929. *
  930. */
  931. #ifdef CONFIG_40x
  932. #define PPC_40x_TURN_OFF_MSR_DR \
  933. /* avoid any possible TLB misses here by turning off MSR.DR, we \
  934. * assume the instructions here are mapped by a pinned TLB entry */ \
  935. li r10,MSR_IR; \
  936. mtmsr r10; \
  937. isync; \
  938. tophys(r1, r1);
  939. #else
  940. #define PPC_40x_TURN_OFF_MSR_DR
  941. #endif
  942. #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
  943. REST_NVGPRS(r1); \
  944. lwz r3,_MSR(r1); \
  945. andi. r3,r3,MSR_PR; \
  946. LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
  947. bne user_exc_return; \
  948. lwz r0,GPR0(r1); \
  949. lwz r2,GPR2(r1); \
  950. REST_4GPRS(3, r1); \
  951. REST_2GPRS(7, r1); \
  952. lwz r10,_XER(r1); \
  953. lwz r11,_CTR(r1); \
  954. mtspr SPRN_XER,r10; \
  955. mtctr r11; \
  956. PPC405_ERR77(0,r1); \
  957. stwcx. r0,0,r1; /* to clear the reservation */ \
  958. lwz r11,_LINK(r1); \
  959. mtlr r11; \
  960. lwz r10,_CCR(r1); \
  961. mtcrf 0xff,r10; \
  962. PPC_40x_TURN_OFF_MSR_DR; \
  963. lwz r9,_DEAR(r1); \
  964. lwz r10,_ESR(r1); \
  965. mtspr SPRN_DEAR,r9; \
  966. mtspr SPRN_ESR,r10; \
  967. lwz r11,_NIP(r1); \
  968. lwz r12,_MSR(r1); \
  969. mtspr exc_lvl_srr0,r11; \
  970. mtspr exc_lvl_srr1,r12; \
  971. lwz r9,GPR9(r1); \
  972. lwz r12,GPR12(r1); \
  973. lwz r10,GPR10(r1); \
  974. lwz r11,GPR11(r1); \
  975. lwz r1,GPR1(r1); \
  976. PPC405_ERR77_SYNC; \
  977. exc_lvl_rfi; \
  978. b .; /* prevent prefetch past exc_lvl_rfi */
  979. #define RESTORE_xSRR(exc_lvl_srr0, exc_lvl_srr1) \
  980. lwz r9,_##exc_lvl_srr0(r1); \
  981. lwz r10,_##exc_lvl_srr1(r1); \
  982. mtspr SPRN_##exc_lvl_srr0,r9; \
  983. mtspr SPRN_##exc_lvl_srr1,r10;
  984. #if defined(CONFIG_PPC_BOOK3E_MMU)
  985. #ifdef CONFIG_PHYS_64BIT
  986. #define RESTORE_MAS7 \
  987. lwz r11,MAS7(r1); \
  988. mtspr SPRN_MAS7,r11;
  989. #else
  990. #define RESTORE_MAS7
  991. #endif /* CONFIG_PHYS_64BIT */
  992. #define RESTORE_MMU_REGS \
  993. lwz r9,MAS0(r1); \
  994. lwz r10,MAS1(r1); \
  995. lwz r11,MAS2(r1); \
  996. mtspr SPRN_MAS0,r9; \
  997. lwz r9,MAS3(r1); \
  998. mtspr SPRN_MAS1,r10; \
  999. lwz r10,MAS6(r1); \
  1000. mtspr SPRN_MAS2,r11; \
  1001. mtspr SPRN_MAS3,r9; \
  1002. mtspr SPRN_MAS6,r10; \
  1003. RESTORE_MAS7;
  1004. #elif defined(CONFIG_44x)
  1005. #define RESTORE_MMU_REGS \
  1006. lwz r9,MMUCR(r1); \
  1007. mtspr SPRN_MMUCR,r9;
  1008. #else
  1009. #define RESTORE_MMU_REGS
  1010. #endif
  1011. #ifdef CONFIG_40x
  1012. .globl ret_from_crit_exc
  1013. ret_from_crit_exc:
  1014. mfspr r9,SPRN_SPRG_THREAD
  1015. lis r10,saved_ksp_limit@ha;
  1016. lwz r10,saved_ksp_limit@l(r10);
  1017. tovirt(r9,r9);
  1018. stw r10,KSP_LIMIT(r9)
  1019. lis r9,crit_srr0@ha;
  1020. lwz r9,crit_srr0@l(r9);
  1021. lis r10,crit_srr1@ha;
  1022. lwz r10,crit_srr1@l(r10);
  1023. mtspr SPRN_SRR0,r9;
  1024. mtspr SPRN_SRR1,r10;
  1025. RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
  1026. #endif /* CONFIG_40x */
  1027. #ifdef CONFIG_BOOKE
  1028. .globl ret_from_crit_exc
  1029. ret_from_crit_exc:
  1030. mfspr r9,SPRN_SPRG_THREAD
  1031. lwz r10,SAVED_KSP_LIMIT(r1)
  1032. stw r10,KSP_LIMIT(r9)
  1033. RESTORE_xSRR(SRR0,SRR1);
  1034. RESTORE_MMU_REGS;
  1035. RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, PPC_RFCI)
  1036. .globl ret_from_debug_exc
  1037. ret_from_debug_exc:
  1038. mfspr r9,SPRN_SPRG_THREAD
  1039. lwz r10,SAVED_KSP_LIMIT(r1)
  1040. stw r10,KSP_LIMIT(r9)
  1041. lwz r9,THREAD_INFO-THREAD(r9)
  1042. CURRENT_THREAD_INFO(r10, r1)
  1043. lwz r10,TI_PREEMPT(r10)
  1044. stw r10,TI_PREEMPT(r9)
  1045. RESTORE_xSRR(SRR0,SRR1);
  1046. RESTORE_xSRR(CSRR0,CSRR1);
  1047. RESTORE_MMU_REGS;
  1048. RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, PPC_RFDI)
  1049. .globl ret_from_mcheck_exc
  1050. ret_from_mcheck_exc:
  1051. mfspr r9,SPRN_SPRG_THREAD
  1052. lwz r10,SAVED_KSP_LIMIT(r1)
  1053. stw r10,KSP_LIMIT(r9)
  1054. RESTORE_xSRR(SRR0,SRR1);
  1055. RESTORE_xSRR(CSRR0,CSRR1);
  1056. RESTORE_xSRR(DSRR0,DSRR1);
  1057. RESTORE_MMU_REGS;
  1058. RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, PPC_RFMCI)
  1059. #endif /* CONFIG_BOOKE */
  1060. /*
  1061. * Load the DBCR0 value for a task that is being ptraced,
  1062. * having first saved away the global DBCR0. Note that r0
  1063. * has the dbcr0 value to set upon entry to this.
  1064. */
  1065. load_dbcr0:
  1066. mfmsr r10 /* first disable debug exceptions */
  1067. rlwinm r10,r10,0,~MSR_DE
  1068. mtmsr r10
  1069. isync
  1070. mfspr r10,SPRN_DBCR0
  1071. lis r11,global_dbcr0@ha
  1072. addi r11,r11,global_dbcr0@l
  1073. #ifdef CONFIG_SMP
  1074. CURRENT_THREAD_INFO(r9, r1)
  1075. lwz r9,TI_CPU(r9)
  1076. slwi r9,r9,3
  1077. add r11,r11,r9
  1078. #endif
  1079. stw r10,0(r11)
  1080. mtspr SPRN_DBCR0,r0
  1081. lwz r10,4(r11)
  1082. addi r10,r10,1
  1083. stw r10,4(r11)
  1084. li r11,-1
  1085. mtspr SPRN_DBSR,r11 /* clear all pending debug events */
  1086. blr
  1087. .section .bss
  1088. .align 4
  1089. global_dbcr0:
  1090. .space 8*NR_CPUS
  1091. .previous
  1092. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  1093. do_work: /* r10 contains MSR_KERNEL here */
  1094. andi. r0,r9,_TIF_NEED_RESCHED
  1095. beq do_user_signal
  1096. do_resched: /* r10 contains MSR_KERNEL here */
  1097. /* Note: We don't need to inform lockdep that we are enabling
  1098. * interrupts here. As far as it knows, they are already enabled
  1099. */
  1100. ori r10,r10,MSR_EE
  1101. SYNC
  1102. MTMSRD(r10) /* hard-enable interrupts */
  1103. bl schedule
  1104. recheck:
  1105. /* Note: And we don't tell it we are disabling them again
  1106. * neither. Those disable/enable cycles used to peek at
  1107. * TI_FLAGS aren't advertised.
  1108. */
  1109. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  1110. SYNC
  1111. MTMSRD(r10) /* disable interrupts */
  1112. CURRENT_THREAD_INFO(r9, r1)
  1113. lwz r9,TI_FLAGS(r9)
  1114. andi. r0,r9,_TIF_NEED_RESCHED
  1115. bne- do_resched
  1116. andi. r0,r9,_TIF_USER_WORK_MASK
  1117. beq restore_user
  1118. do_user_signal: /* r10 contains MSR_KERNEL here */
  1119. ori r10,r10,MSR_EE
  1120. SYNC
  1121. MTMSRD(r10) /* hard-enable interrupts */
  1122. /* save r13-r31 in the exception frame, if not already done */
  1123. lwz r3,_TRAP(r1)
  1124. andi. r0,r3,1
  1125. beq 2f
  1126. SAVE_NVGPRS(r1)
  1127. rlwinm r3,r3,0,0,30
  1128. stw r3,_TRAP(r1)
  1129. 2: addi r3,r1,STACK_FRAME_OVERHEAD
  1130. mr r4,r9
  1131. bl do_notify_resume
  1132. REST_NVGPRS(r1)
  1133. b recheck
  1134. /*
  1135. * We come here when we are at the end of handling an exception
  1136. * that occurred at a place where taking an exception will lose
  1137. * state information, such as the contents of SRR0 and SRR1.
  1138. */
  1139. nonrecoverable:
  1140. lis r10,exc_exit_restart_end@ha
  1141. addi r10,r10,exc_exit_restart_end@l
  1142. cmplw r12,r10
  1143. bge 3f
  1144. lis r11,exc_exit_restart@ha
  1145. addi r11,r11,exc_exit_restart@l
  1146. cmplw r12,r11
  1147. blt 3f
  1148. lis r10,ee_restarts@ha
  1149. lwz r12,ee_restarts@l(r10)
  1150. addi r12,r12,1
  1151. stw r12,ee_restarts@l(r10)
  1152. mr r12,r11 /* restart at exc_exit_restart */
  1153. blr
  1154. 3: /* OK, we can't recover, kill this process */
  1155. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  1156. BEGIN_FTR_SECTION
  1157. blr
  1158. END_FTR_SECTION_IFSET(CPU_FTR_601)
  1159. lwz r3,_TRAP(r1)
  1160. andi. r0,r3,1
  1161. beq 4f
  1162. SAVE_NVGPRS(r1)
  1163. rlwinm r3,r3,0,0,30
  1164. stw r3,_TRAP(r1)
  1165. 4: addi r3,r1,STACK_FRAME_OVERHEAD
  1166. bl nonrecoverable_exception
  1167. /* shouldn't return */
  1168. b 4b
  1169. .section .bss
  1170. .align 2
  1171. ee_restarts:
  1172. .space 4
  1173. .previous
  1174. /*
  1175. * PROM code for specific machines follows. Put it
  1176. * here so it's easy to add arch-specific sections later.
  1177. * -- Cort
  1178. */
  1179. #ifdef CONFIG_PPC_RTAS
  1180. /*
  1181. * On CHRP, the Run-Time Abstraction Services (RTAS) have to be
  1182. * called with the MMU off.
  1183. */
  1184. _GLOBAL(enter_rtas)
  1185. stwu r1,-INT_FRAME_SIZE(r1)
  1186. mflr r0
  1187. stw r0,INT_FRAME_SIZE+4(r1)
  1188. LOAD_REG_ADDR(r4, rtas)
  1189. lis r6,1f@ha /* physical return address for rtas */
  1190. addi r6,r6,1f@l
  1191. tophys(r6,r6)
  1192. tophys(r7,r1)
  1193. lwz r8,RTASENTRY(r4)
  1194. lwz r4,RTASBASE(r4)
  1195. mfmsr r9
  1196. stw r9,8(r1)
  1197. LOAD_MSR_KERNEL(r0,MSR_KERNEL)
  1198. SYNC /* disable interrupts so SRR0/1 */
  1199. MTMSRD(r0) /* don't get trashed */
  1200. li r9,MSR_KERNEL & ~(MSR_IR|MSR_DR)
  1201. mtlr r6
  1202. mtspr SPRN_SPRG_RTAS,r7
  1203. mtspr SPRN_SRR0,r8
  1204. mtspr SPRN_SRR1,r9
  1205. RFI
  1206. 1: tophys(r9,r1)
  1207. lwz r8,INT_FRAME_SIZE+4(r9) /* get return address */
  1208. lwz r9,8(r9) /* original msr value */
  1209. FIX_SRR1(r9,r0)
  1210. addi r1,r1,INT_FRAME_SIZE
  1211. li r0,0
  1212. mtspr SPRN_SPRG_RTAS,r0
  1213. mtspr SPRN_SRR0,r8
  1214. mtspr SPRN_SRR1,r9
  1215. RFI /* return to caller */
  1216. .globl machine_check_in_rtas
  1217. machine_check_in_rtas:
  1218. twi 31,0,0
  1219. /* XXX load up BATs and panic */
  1220. #endif /* CONFIG_PPC_RTAS */
  1221. #ifdef CONFIG_FUNCTION_TRACER
  1222. #ifdef CONFIG_DYNAMIC_FTRACE
  1223. _GLOBAL(mcount)
  1224. _GLOBAL(_mcount)
  1225. /*
  1226. * It is required that _mcount on PPC32 must preserve the
  1227. * link register. But we have r0 to play with. We use r0
  1228. * to push the return address back to the caller of mcount
  1229. * into the ctr register, restore the link register and
  1230. * then jump back using the ctr register.
  1231. */
  1232. mflr r0
  1233. mtctr r0
  1234. lwz r0, 4(r1)
  1235. mtlr r0
  1236. bctr
  1237. _GLOBAL(ftrace_caller)
  1238. MCOUNT_SAVE_FRAME
  1239. /* r3 ends up with link register */
  1240. subi r3, r3, MCOUNT_INSN_SIZE
  1241. .globl ftrace_call
  1242. ftrace_call:
  1243. bl ftrace_stub
  1244. nop
  1245. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1246. .globl ftrace_graph_call
  1247. ftrace_graph_call:
  1248. b ftrace_graph_stub
  1249. _GLOBAL(ftrace_graph_stub)
  1250. #endif
  1251. MCOUNT_RESTORE_FRAME
  1252. /* old link register ends up in ctr reg */
  1253. bctr
  1254. #else
  1255. _GLOBAL(mcount)
  1256. _GLOBAL(_mcount)
  1257. MCOUNT_SAVE_FRAME
  1258. subi r3, r3, MCOUNT_INSN_SIZE
  1259. LOAD_REG_ADDR(r5, ftrace_trace_function)
  1260. lwz r5,0(r5)
  1261. mtctr r5
  1262. bctrl
  1263. nop
  1264. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1265. b ftrace_graph_caller
  1266. #endif
  1267. MCOUNT_RESTORE_FRAME
  1268. bctr
  1269. #endif
  1270. EXPORT_SYMBOL(_mcount)
  1271. _GLOBAL(ftrace_stub)
  1272. blr
  1273. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  1274. _GLOBAL(ftrace_graph_caller)
  1275. /* load r4 with local address */
  1276. lwz r4, 44(r1)
  1277. subi r4, r4, MCOUNT_INSN_SIZE
  1278. /* Grab the LR out of the caller stack frame */
  1279. lwz r3,52(r1)
  1280. bl prepare_ftrace_return
  1281. nop
  1282. /*
  1283. * prepare_ftrace_return gives us the address we divert to.
  1284. * Change the LR in the callers stack frame to this.
  1285. */
  1286. stw r3,52(r1)
  1287. MCOUNT_RESTORE_FRAME
  1288. /* old link register ends up in ctr reg */
  1289. bctr
  1290. _GLOBAL(return_to_handler)
  1291. /* need to save return values */
  1292. stwu r1, -32(r1)
  1293. stw r3, 20(r1)
  1294. stw r4, 16(r1)
  1295. stw r31, 12(r1)
  1296. mr r31, r1
  1297. bl ftrace_return_to_handler
  1298. nop
  1299. /* return value has real return address */
  1300. mtlr r3
  1301. lwz r3, 20(r1)
  1302. lwz r4, 16(r1)
  1303. lwz r31,12(r1)
  1304. lwz r1, 0(r1)
  1305. /* Jump back to real return address */
  1306. blr
  1307. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  1308. #endif /* CONFIG_FUNCTION_TRACER */