entry.S 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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/sys.h>
  23. #include <linux/threads.h>
  24. #include <asm/processor.h>
  25. #include <asm/page.h>
  26. #include <asm/mmu.h>
  27. #include <asm/cputable.h>
  28. #include <asm/thread_info.h>
  29. #include <asm/ppc_asm.h>
  30. #include <asm/asm-offsets.h>
  31. #include <asm/unistd.h>
  32. #undef SHOW_SYSCALLS
  33. #undef SHOW_SYSCALLS_TASK
  34. /*
  35. * MSR_KERNEL is > 0x10000 on 4xx/Book-E since it include MSR_CE.
  36. */
  37. #if MSR_KERNEL >= 0x10000
  38. #define LOAD_MSR_KERNEL(r, x) lis r,(x)@h; ori r,r,(x)@l
  39. #else
  40. #define LOAD_MSR_KERNEL(r, x) li r,(x)
  41. #endif
  42. #ifdef CONFIG_BOOKE
  43. #include "head_booke.h"
  44. #define TRANSFER_TO_HANDLER_EXC_LEVEL(exc_level) \
  45. mtspr exc_level##_SPRG,r8; \
  46. BOOKE_LOAD_EXC_LEVEL_STACK(exc_level); \
  47. lwz r0,GPR10-INT_FRAME_SIZE(r8); \
  48. stw r0,GPR10(r11); \
  49. lwz r0,GPR11-INT_FRAME_SIZE(r8); \
  50. stw r0,GPR11(r11); \
  51. mfspr r8,exc_level##_SPRG
  52. .globl mcheck_transfer_to_handler
  53. mcheck_transfer_to_handler:
  54. TRANSFER_TO_HANDLER_EXC_LEVEL(MCHECK)
  55. b transfer_to_handler_full
  56. .globl debug_transfer_to_handler
  57. debug_transfer_to_handler:
  58. TRANSFER_TO_HANDLER_EXC_LEVEL(DEBUG)
  59. b transfer_to_handler_full
  60. .globl crit_transfer_to_handler
  61. crit_transfer_to_handler:
  62. TRANSFER_TO_HANDLER_EXC_LEVEL(CRIT)
  63. /* fall through */
  64. #endif
  65. #ifdef CONFIG_40x
  66. .globl crit_transfer_to_handler
  67. crit_transfer_to_handler:
  68. lwz r0,crit_r10@l(0)
  69. stw r0,GPR10(r11)
  70. lwz r0,crit_r11@l(0)
  71. stw r0,GPR11(r11)
  72. /* fall through */
  73. #endif
  74. /*
  75. * This code finishes saving the registers to the exception frame
  76. * and jumps to the appropriate handler for the exception, turning
  77. * on address translation.
  78. * Note that we rely on the caller having set cr0.eq iff the exception
  79. * occurred in kernel mode (i.e. MSR:PR = 0).
  80. */
  81. .globl transfer_to_handler_full
  82. transfer_to_handler_full:
  83. SAVE_NVGPRS(r11)
  84. /* fall through */
  85. .globl transfer_to_handler
  86. transfer_to_handler:
  87. stw r2,GPR2(r11)
  88. stw r12,_NIP(r11)
  89. stw r9,_MSR(r11)
  90. andi. r2,r9,MSR_PR
  91. mfctr r12
  92. mfspr r2,SPRN_XER
  93. stw r12,_CTR(r11)
  94. stw r2,_XER(r11)
  95. mfspr r12,SPRN_SPRG3
  96. addi r2,r12,-THREAD
  97. tovirt(r2,r2) /* set r2 to current */
  98. beq 2f /* if from user, fix up THREAD.regs */
  99. addi r11,r1,STACK_FRAME_OVERHEAD
  100. stw r11,PT_REGS(r12)
  101. #if defined(CONFIG_40x) || defined(CONFIG_BOOKE)
  102. /* Check to see if the dbcr0 register is set up to debug. Use the
  103. single-step bit to do this. */
  104. lwz r12,THREAD_DBCR0(r12)
  105. andis. r12,r12,DBCR0_IC@h
  106. beq+ 3f
  107. /* From user and task is ptraced - load up global dbcr0 */
  108. li r12,-1 /* clear all pending debug events */
  109. mtspr SPRN_DBSR,r12
  110. lis r11,global_dbcr0@ha
  111. tophys(r11,r11)
  112. addi r11,r11,global_dbcr0@l
  113. lwz r12,0(r11)
  114. mtspr SPRN_DBCR0,r12
  115. lwz r12,4(r11)
  116. addi r12,r12,-1
  117. stw r12,4(r11)
  118. #endif
  119. b 3f
  120. 2: /* if from kernel, check interrupted DOZE/NAP mode and
  121. * check for stack overflow
  122. */
  123. lwz r9,THREAD_INFO-THREAD(r12)
  124. cmplw r1,r9 /* if r1 <= current->thread_info */
  125. ble- stack_ovf /* then the kernel stack overflowed */
  126. 5:
  127. #ifdef CONFIG_6xx
  128. tophys(r9,r9) /* check local flags */
  129. lwz r12,TI_LOCAL_FLAGS(r9)
  130. mtcrf 0x01,r12
  131. bt- 31-TLF_NAPPING,4f
  132. #endif /* CONFIG_6xx */
  133. .globl transfer_to_handler_cont
  134. transfer_to_handler_cont:
  135. 3:
  136. mflr r9
  137. lwz r11,0(r9) /* virtual address of handler */
  138. lwz r9,4(r9) /* where to go when done */
  139. mtspr SPRN_SRR0,r11
  140. mtspr SPRN_SRR1,r10
  141. mtlr r9
  142. SYNC
  143. RFI /* jump to handler, enable MMU */
  144. #ifdef CONFIG_6xx
  145. 4: rlwinm r12,r12,0,~_TLF_NAPPING
  146. stw r12,TI_LOCAL_FLAGS(r9)
  147. b power_save_6xx_restore
  148. #endif
  149. /*
  150. * On kernel stack overflow, load up an initial stack pointer
  151. * and call StackOverflow(regs), which should not return.
  152. */
  153. stack_ovf:
  154. /* sometimes we use a statically-allocated stack, which is OK. */
  155. lis r12,_end@h
  156. ori r12,r12,_end@l
  157. cmplw r1,r12
  158. ble 5b /* r1 <= &_end is OK */
  159. SAVE_NVGPRS(r11)
  160. addi r3,r1,STACK_FRAME_OVERHEAD
  161. lis r1,init_thread_union@ha
  162. addi r1,r1,init_thread_union@l
  163. addi r1,r1,THREAD_SIZE-STACK_FRAME_OVERHEAD
  164. lis r9,StackOverflow@ha
  165. addi r9,r9,StackOverflow@l
  166. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  167. FIX_SRR1(r10,r12)
  168. mtspr SPRN_SRR0,r9
  169. mtspr SPRN_SRR1,r10
  170. SYNC
  171. RFI
  172. /*
  173. * Handle a system call.
  174. */
  175. .stabs "arch/ppc/kernel/",N_SO,0,0,0f
  176. .stabs "entry.S",N_SO,0,0,0f
  177. 0:
  178. _GLOBAL(DoSyscall)
  179. stw r3,ORIG_GPR3(r1)
  180. li r12,0
  181. stw r12,RESULT(r1)
  182. lwz r11,_CCR(r1) /* Clear SO bit in CR */
  183. rlwinm r11,r11,0,4,2
  184. stw r11,_CCR(r1)
  185. #ifdef SHOW_SYSCALLS
  186. bl do_show_syscall
  187. #endif /* SHOW_SYSCALLS */
  188. rlwinm r10,r1,0,0,18 /* current_thread_info() */
  189. lwz r11,TI_FLAGS(r10)
  190. andi. r11,r11,_TIF_SYSCALL_T_OR_A
  191. bne- syscall_dotrace
  192. syscall_dotrace_cont:
  193. cmplwi 0,r0,NR_syscalls
  194. lis r10,sys_call_table@h
  195. ori r10,r10,sys_call_table@l
  196. slwi r0,r0,2
  197. bge- 66f
  198. lwzx r10,r10,r0 /* Fetch system call handler [ptr] */
  199. mtlr r10
  200. addi r9,r1,STACK_FRAME_OVERHEAD
  201. PPC440EP_ERR42
  202. blrl /* Call handler */
  203. .globl ret_from_syscall
  204. ret_from_syscall:
  205. #ifdef SHOW_SYSCALLS
  206. bl do_show_syscall_exit
  207. #endif
  208. mr r6,r3
  209. rlwinm r12,r1,0,0,18 /* current_thread_info() */
  210. /* disable interrupts so current_thread_info()->flags can't change */
  211. LOAD_MSR_KERNEL(r10,MSR_KERNEL) /* doesn't include MSR_EE */
  212. SYNC
  213. MTMSRD(r10)
  214. lwz r9,TI_FLAGS(r12)
  215. li r8,-_LAST_ERRNO
  216. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP|_TIF_USER_WORK_MASK|_TIF_PERSYSCALL_MASK)
  217. bne- syscall_exit_work
  218. cmplw 0,r3,r8
  219. blt+ syscall_exit_cont
  220. lwz r11,_CCR(r1) /* Load CR */
  221. neg r3,r3
  222. oris r11,r11,0x1000 /* Set SO bit in CR */
  223. stw r11,_CCR(r1)
  224. syscall_exit_cont:
  225. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  226. /* If the process has its own DBCR0 value, load it up. The single
  227. step bit tells us that dbcr0 should be loaded. */
  228. lwz r0,THREAD+THREAD_DBCR0(r2)
  229. andis. r10,r0,DBCR0_IC@h
  230. bnel- load_dbcr0
  231. #endif
  232. #ifdef CONFIG_44x
  233. lis r4,icache_44x_need_flush@ha
  234. lwz r5,icache_44x_need_flush@l(r4)
  235. cmplwi cr0,r5,0
  236. bne- 2f
  237. 1:
  238. #endif /* CONFIG_44x */
  239. stwcx. r0,0,r1 /* to clear the reservation */
  240. lwz r4,_LINK(r1)
  241. lwz r5,_CCR(r1)
  242. mtlr r4
  243. mtcr r5
  244. lwz r7,_NIP(r1)
  245. lwz r8,_MSR(r1)
  246. FIX_SRR1(r8, r0)
  247. lwz r2,GPR2(r1)
  248. lwz r1,GPR1(r1)
  249. mtspr SPRN_SRR0,r7
  250. mtspr SPRN_SRR1,r8
  251. SYNC
  252. RFI
  253. #ifdef CONFIG_44x
  254. 2: li r7,0
  255. iccci r0,r0
  256. stw r7,icache_44x_need_flush@l(r4)
  257. b 1b
  258. #endif /* CONFIG_44x */
  259. 66: li r3,-ENOSYS
  260. b ret_from_syscall
  261. .globl ret_from_fork
  262. ret_from_fork:
  263. REST_NVGPRS(r1)
  264. bl schedule_tail
  265. li r3,0
  266. b ret_from_syscall
  267. /* Traced system call support */
  268. syscall_dotrace:
  269. SAVE_NVGPRS(r1)
  270. li r0,0xc00
  271. stw r0,TRAP(r1)
  272. addi r3,r1,STACK_FRAME_OVERHEAD
  273. bl do_syscall_trace_enter
  274. lwz r0,GPR0(r1) /* Restore original registers */
  275. lwz r3,GPR3(r1)
  276. lwz r4,GPR4(r1)
  277. lwz r5,GPR5(r1)
  278. lwz r6,GPR6(r1)
  279. lwz r7,GPR7(r1)
  280. lwz r8,GPR8(r1)
  281. REST_NVGPRS(r1)
  282. b syscall_dotrace_cont
  283. syscall_exit_work:
  284. andi. r0,r9,_TIF_RESTOREALL
  285. beq+ 0f
  286. REST_NVGPRS(r1)
  287. b 2f
  288. 0: cmplw 0,r3,r8
  289. blt+ 1f
  290. andi. r0,r9,_TIF_NOERROR
  291. bne- 1f
  292. lwz r11,_CCR(r1) /* Load CR */
  293. neg r3,r3
  294. oris r11,r11,0x1000 /* Set SO bit in CR */
  295. stw r11,_CCR(r1)
  296. 1: stw r6,RESULT(r1) /* Save result */
  297. stw r3,GPR3(r1) /* Update return value */
  298. 2: andi. r0,r9,(_TIF_PERSYSCALL_MASK)
  299. beq 4f
  300. /* Clear per-syscall TIF flags if any are set. */
  301. li r11,_TIF_PERSYSCALL_MASK
  302. addi r12,r12,TI_FLAGS
  303. 3: lwarx r8,0,r12
  304. andc r8,r8,r11
  305. #ifdef CONFIG_IBM405_ERR77
  306. dcbt 0,r12
  307. #endif
  308. stwcx. r8,0,r12
  309. bne- 3b
  310. subi r12,r12,TI_FLAGS
  311. 4: /* Anything which requires enabling interrupts? */
  312. andi. r0,r9,(_TIF_SYSCALL_T_OR_A|_TIF_SINGLESTEP)
  313. beq ret_from_except
  314. /* Re-enable interrupts */
  315. ori r10,r10,MSR_EE
  316. SYNC
  317. MTMSRD(r10)
  318. /* Save NVGPRS if they're not saved already */
  319. lwz r4,TRAP(r1)
  320. andi. r4,r4,1
  321. beq 5f
  322. SAVE_NVGPRS(r1)
  323. li r4,0xc00
  324. stw r4,TRAP(r1)
  325. 5:
  326. addi r3,r1,STACK_FRAME_OVERHEAD
  327. bl do_syscall_trace_leave
  328. b ret_from_except_full
  329. #ifdef SHOW_SYSCALLS
  330. do_show_syscall:
  331. #ifdef SHOW_SYSCALLS_TASK
  332. lis r11,show_syscalls_task@ha
  333. lwz r11,show_syscalls_task@l(r11)
  334. cmp 0,r2,r11
  335. bnelr
  336. #endif
  337. stw r31,GPR31(r1)
  338. mflr r31
  339. lis r3,7f@ha
  340. addi r3,r3,7f@l
  341. lwz r4,GPR0(r1)
  342. lwz r5,GPR3(r1)
  343. lwz r6,GPR4(r1)
  344. lwz r7,GPR5(r1)
  345. lwz r8,GPR6(r1)
  346. lwz r9,GPR7(r1)
  347. bl printk
  348. lis r3,77f@ha
  349. addi r3,r3,77f@l
  350. lwz r4,GPR8(r1)
  351. mr r5,r2
  352. bl printk
  353. lwz r0,GPR0(r1)
  354. lwz r3,GPR3(r1)
  355. lwz r4,GPR4(r1)
  356. lwz r5,GPR5(r1)
  357. lwz r6,GPR6(r1)
  358. lwz r7,GPR7(r1)
  359. lwz r8,GPR8(r1)
  360. mtlr r31
  361. lwz r31,GPR31(r1)
  362. blr
  363. do_show_syscall_exit:
  364. #ifdef SHOW_SYSCALLS_TASK
  365. lis r11,show_syscalls_task@ha
  366. lwz r11,show_syscalls_task@l(r11)
  367. cmp 0,r2,r11
  368. bnelr
  369. #endif
  370. stw r31,GPR31(r1)
  371. mflr r31
  372. stw r3,RESULT(r1) /* Save result */
  373. mr r4,r3
  374. lis r3,79f@ha
  375. addi r3,r3,79f@l
  376. bl printk
  377. lwz r3,RESULT(r1)
  378. mtlr r31
  379. lwz r31,GPR31(r1)
  380. blr
  381. 7: .string "syscall %d(%x, %x, %x, %x, %x, "
  382. 77: .string "%x), current=%p\n"
  383. 79: .string " -> %x\n"
  384. .align 2,0
  385. #ifdef SHOW_SYSCALLS_TASK
  386. .data
  387. .globl show_syscalls_task
  388. show_syscalls_task:
  389. .long -1
  390. .text
  391. #endif
  392. #endif /* SHOW_SYSCALLS */
  393. /*
  394. * The fork/clone functions need to copy the full register set into
  395. * the child process. Therefore we need to save all the nonvolatile
  396. * registers (r13 - r31) before calling the C code.
  397. */
  398. .globl ppc_fork
  399. ppc_fork:
  400. SAVE_NVGPRS(r1)
  401. lwz r0,TRAP(r1)
  402. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  403. stw r0,TRAP(r1) /* register set saved */
  404. b sys_fork
  405. .globl ppc_vfork
  406. ppc_vfork:
  407. SAVE_NVGPRS(r1)
  408. lwz r0,TRAP(r1)
  409. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  410. stw r0,TRAP(r1) /* register set saved */
  411. b sys_vfork
  412. .globl ppc_clone
  413. ppc_clone:
  414. SAVE_NVGPRS(r1)
  415. lwz r0,TRAP(r1)
  416. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  417. stw r0,TRAP(r1) /* register set saved */
  418. b sys_clone
  419. .globl ppc_swapcontext
  420. ppc_swapcontext:
  421. SAVE_NVGPRS(r1)
  422. lwz r0,TRAP(r1)
  423. rlwinm r0,r0,0,0,30 /* clear LSB to indicate full */
  424. stw r0,TRAP(r1) /* register set saved */
  425. b sys_swapcontext
  426. /*
  427. * Top-level page fault handling.
  428. * This is in assembler because if do_page_fault tells us that
  429. * it is a bad kernel page fault, we want to save the non-volatile
  430. * registers before calling bad_page_fault.
  431. */
  432. .globl handle_page_fault
  433. handle_page_fault:
  434. stw r4,_DAR(r1)
  435. addi r3,r1,STACK_FRAME_OVERHEAD
  436. bl do_page_fault
  437. cmpwi r3,0
  438. beq+ ret_from_except
  439. SAVE_NVGPRS(r1)
  440. lwz r0,TRAP(r1)
  441. clrrwi r0,r0,1
  442. stw r0,TRAP(r1)
  443. mr r5,r3
  444. addi r3,r1,STACK_FRAME_OVERHEAD
  445. lwz r4,_DAR(r1)
  446. bl bad_page_fault
  447. b ret_from_except_full
  448. /*
  449. * This routine switches between two different tasks. The process
  450. * state of one is saved on its kernel stack. Then the state
  451. * of the other is restored from its kernel stack. The memory
  452. * management hardware is updated to the second process's state.
  453. * Finally, we can return to the second process.
  454. * On entry, r3 points to the THREAD for the current task, r4
  455. * points to the THREAD for the new task.
  456. *
  457. * This routine is always called with interrupts disabled.
  458. *
  459. * Note: there are two ways to get to the "going out" portion
  460. * of this code; either by coming in via the entry (_switch)
  461. * or via "fork" which must set up an environment equivalent
  462. * to the "_switch" path. If you change this , you'll have to
  463. * change the fork code also.
  464. *
  465. * The code which creates the new task context is in 'copy_thread'
  466. * in arch/ppc/kernel/process.c
  467. */
  468. _GLOBAL(_switch)
  469. stwu r1,-INT_FRAME_SIZE(r1)
  470. mflr r0
  471. stw r0,INT_FRAME_SIZE+4(r1)
  472. /* r3-r12 are caller saved -- Cort */
  473. SAVE_NVGPRS(r1)
  474. stw r0,_NIP(r1) /* Return to switch caller */
  475. mfmsr r11
  476. li r0,MSR_FP /* Disable floating-point */
  477. #ifdef CONFIG_ALTIVEC
  478. BEGIN_FTR_SECTION
  479. oris r0,r0,MSR_VEC@h /* Disable altivec */
  480. mfspr r12,SPRN_VRSAVE /* save vrsave register value */
  481. stw r12,THREAD+THREAD_VRSAVE(r2)
  482. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  483. #endif /* CONFIG_ALTIVEC */
  484. #ifdef CONFIG_SPE
  485. oris r0,r0,MSR_SPE@h /* Disable SPE */
  486. mfspr r12,SPRN_SPEFSCR /* save spefscr register value */
  487. stw r12,THREAD+THREAD_SPEFSCR(r2)
  488. #endif /* CONFIG_SPE */
  489. and. r0,r0,r11 /* FP or altivec or SPE enabled? */
  490. beq+ 1f
  491. andc r11,r11,r0
  492. MTMSRD(r11)
  493. isync
  494. 1: stw r11,_MSR(r1)
  495. mfcr r10
  496. stw r10,_CCR(r1)
  497. stw r1,KSP(r3) /* Set old stack pointer */
  498. #ifdef CONFIG_SMP
  499. /* We need a sync somewhere here to make sure that if the
  500. * previous task gets rescheduled on another CPU, it sees all
  501. * stores it has performed on this one.
  502. */
  503. sync
  504. #endif /* CONFIG_SMP */
  505. tophys(r0,r4)
  506. CLR_TOP32(r0)
  507. mtspr SPRN_SPRG3,r0 /* Update current THREAD phys addr */
  508. lwz r1,KSP(r4) /* Load new stack pointer */
  509. /* save the old current 'last' for return value */
  510. mr r3,r2
  511. addi r2,r4,-THREAD /* Update current */
  512. #ifdef CONFIG_ALTIVEC
  513. BEGIN_FTR_SECTION
  514. lwz r0,THREAD+THREAD_VRSAVE(r2)
  515. mtspr SPRN_VRSAVE,r0 /* if G4, restore VRSAVE reg */
  516. END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC)
  517. #endif /* CONFIG_ALTIVEC */
  518. #ifdef CONFIG_SPE
  519. lwz r0,THREAD+THREAD_SPEFSCR(r2)
  520. mtspr SPRN_SPEFSCR,r0 /* restore SPEFSCR reg */
  521. #endif /* CONFIG_SPE */
  522. lwz r0,_CCR(r1)
  523. mtcrf 0xFF,r0
  524. /* r3-r12 are destroyed -- Cort */
  525. REST_NVGPRS(r1)
  526. lwz r4,_NIP(r1) /* Return to _switch caller in new task */
  527. mtlr r4
  528. addi r1,r1,INT_FRAME_SIZE
  529. blr
  530. .globl fast_exception_return
  531. fast_exception_return:
  532. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  533. andi. r10,r9,MSR_RI /* check for recoverable interrupt */
  534. beq 1f /* if not, we've got problems */
  535. #endif
  536. 2: REST_4GPRS(3, r11)
  537. lwz r10,_CCR(r11)
  538. REST_GPR(1, r11)
  539. mtcr r10
  540. lwz r10,_LINK(r11)
  541. mtlr r10
  542. REST_GPR(10, r11)
  543. mtspr SPRN_SRR1,r9
  544. mtspr SPRN_SRR0,r12
  545. REST_GPR(9, r11)
  546. REST_GPR(12, r11)
  547. lwz r11,GPR11(r11)
  548. SYNC
  549. RFI
  550. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  551. /* check if the exception happened in a restartable section */
  552. 1: lis r3,exc_exit_restart_end@ha
  553. addi r3,r3,exc_exit_restart_end@l
  554. cmplw r12,r3
  555. bge 3f
  556. lis r4,exc_exit_restart@ha
  557. addi r4,r4,exc_exit_restart@l
  558. cmplw r12,r4
  559. blt 3f
  560. lis r3,fee_restarts@ha
  561. tophys(r3,r3)
  562. lwz r5,fee_restarts@l(r3)
  563. addi r5,r5,1
  564. stw r5,fee_restarts@l(r3)
  565. mr r12,r4 /* restart at exc_exit_restart */
  566. b 2b
  567. .section .bss
  568. .align 2
  569. fee_restarts:
  570. .space 4
  571. .previous
  572. /* aargh, a nonrecoverable interrupt, panic */
  573. /* aargh, we don't know which trap this is */
  574. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  575. 3:
  576. BEGIN_FTR_SECTION
  577. b 2b
  578. END_FTR_SECTION_IFSET(CPU_FTR_601)
  579. li r10,-1
  580. stw r10,TRAP(r11)
  581. addi r3,r1,STACK_FRAME_OVERHEAD
  582. lis r10,MSR_KERNEL@h
  583. ori r10,r10,MSR_KERNEL@l
  584. bl transfer_to_handler_full
  585. .long nonrecoverable_exception
  586. .long ret_from_except
  587. #endif
  588. .globl ret_from_except_full
  589. ret_from_except_full:
  590. REST_NVGPRS(r1)
  591. /* fall through */
  592. .globl ret_from_except
  593. ret_from_except:
  594. /* Hard-disable interrupts so that current_thread_info()->flags
  595. * can't change between when we test it and when we return
  596. * from the interrupt. */
  597. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  598. SYNC /* Some chip revs have problems here... */
  599. MTMSRD(r10) /* disable interrupts */
  600. lwz r3,_MSR(r1) /* Returning to user mode? */
  601. andi. r0,r3,MSR_PR
  602. beq resume_kernel
  603. user_exc_return: /* r10 contains MSR_KERNEL here */
  604. /* Check current_thread_info()->flags */
  605. rlwinm r9,r1,0,0,18
  606. lwz r9,TI_FLAGS(r9)
  607. andi. r0,r9,(_TIF_SIGPENDING|_TIF_RESTORE_SIGMASK|_TIF_NEED_RESCHED)
  608. bne do_work
  609. restore_user:
  610. #if defined(CONFIG_4xx) || defined(CONFIG_BOOKE)
  611. /* Check whether this process has its own DBCR0 value. The single
  612. step bit tells us that dbcr0 should be loaded. */
  613. lwz r0,THREAD+THREAD_DBCR0(r2)
  614. andis. r10,r0,DBCR0_IC@h
  615. bnel- load_dbcr0
  616. #endif
  617. #ifdef CONFIG_PREEMPT
  618. b restore
  619. /* N.B. the only way to get here is from the beq following ret_from_except. */
  620. resume_kernel:
  621. /* check current_thread_info->preempt_count */
  622. rlwinm r9,r1,0,0,18
  623. lwz r0,TI_PREEMPT(r9)
  624. cmpwi 0,r0,0 /* if non-zero, just restore regs and return */
  625. bne restore
  626. lwz r0,TI_FLAGS(r9)
  627. andi. r0,r0,_TIF_NEED_RESCHED
  628. beq+ restore
  629. andi. r0,r3,MSR_EE /* interrupts off? */
  630. beq restore /* don't schedule if so */
  631. 1: bl preempt_schedule_irq
  632. rlwinm r9,r1,0,0,18
  633. lwz r3,TI_FLAGS(r9)
  634. andi. r0,r3,_TIF_NEED_RESCHED
  635. bne- 1b
  636. #else
  637. resume_kernel:
  638. #endif /* CONFIG_PREEMPT */
  639. /* interrupts are hard-disabled at this point */
  640. restore:
  641. #ifdef CONFIG_44x
  642. lis r4,icache_44x_need_flush@ha
  643. lwz r5,icache_44x_need_flush@l(r4)
  644. cmplwi cr0,r5,0
  645. beq+ 1f
  646. li r6,0
  647. iccci r0,r0
  648. stw r6,icache_44x_need_flush@l(r4)
  649. 1:
  650. #endif /* CONFIG_44x */
  651. lwz r0,GPR0(r1)
  652. lwz r2,GPR2(r1)
  653. REST_4GPRS(3, r1)
  654. REST_2GPRS(7, r1)
  655. lwz r10,_XER(r1)
  656. lwz r11,_CTR(r1)
  657. mtspr SPRN_XER,r10
  658. mtctr r11
  659. PPC405_ERR77(0,r1)
  660. stwcx. r0,0,r1 /* to clear the reservation */
  661. #if !(defined(CONFIG_4xx) || defined(CONFIG_BOOKE))
  662. lwz r9,_MSR(r1)
  663. andi. r10,r9,MSR_RI /* check if this exception occurred */
  664. beql nonrecoverable /* at a bad place (MSR:RI = 0) */
  665. lwz r10,_CCR(r1)
  666. lwz r11,_LINK(r1)
  667. mtcrf 0xFF,r10
  668. mtlr r11
  669. /*
  670. * Once we put values in SRR0 and SRR1, we are in a state
  671. * where exceptions are not recoverable, since taking an
  672. * exception will trash SRR0 and SRR1. Therefore we clear the
  673. * MSR:RI bit to indicate this. If we do take an exception,
  674. * we can't return to the point of the exception but we
  675. * can restart the exception exit path at the label
  676. * exc_exit_restart below. -- paulus
  677. */
  678. LOAD_MSR_KERNEL(r10,MSR_KERNEL & ~MSR_RI)
  679. SYNC
  680. MTMSRD(r10) /* clear the RI bit */
  681. .globl exc_exit_restart
  682. exc_exit_restart:
  683. lwz r9,_MSR(r1)
  684. lwz r12,_NIP(r1)
  685. FIX_SRR1(r9,r10)
  686. mtspr SPRN_SRR0,r12
  687. mtspr SPRN_SRR1,r9
  688. REST_4GPRS(9, r1)
  689. lwz r1,GPR1(r1)
  690. .globl exc_exit_restart_end
  691. exc_exit_restart_end:
  692. SYNC
  693. RFI
  694. #else /* !(CONFIG_4xx || CONFIG_BOOKE) */
  695. /*
  696. * This is a bit different on 4xx/Book-E because it doesn't have
  697. * the RI bit in the MSR.
  698. * The TLB miss handler checks if we have interrupted
  699. * the exception exit path and restarts it if so
  700. * (well maybe one day it will... :).
  701. */
  702. lwz r11,_LINK(r1)
  703. mtlr r11
  704. lwz r10,_CCR(r1)
  705. mtcrf 0xff,r10
  706. REST_2GPRS(9, r1)
  707. .globl exc_exit_restart
  708. exc_exit_restart:
  709. lwz r11,_NIP(r1)
  710. lwz r12,_MSR(r1)
  711. exc_exit_start:
  712. mtspr SPRN_SRR0,r11
  713. mtspr SPRN_SRR1,r12
  714. REST_2GPRS(11, r1)
  715. lwz r1,GPR1(r1)
  716. .globl exc_exit_restart_end
  717. exc_exit_restart_end:
  718. PPC405_ERR77_SYNC
  719. rfi
  720. b . /* prevent prefetch past rfi */
  721. /*
  722. * Returning from a critical interrupt in user mode doesn't need
  723. * to be any different from a normal exception. For a critical
  724. * interrupt in the kernel, we just return (without checking for
  725. * preemption) since the interrupt may have happened at some crucial
  726. * place (e.g. inside the TLB miss handler), and because we will be
  727. * running with r1 pointing into critical_stack, not the current
  728. * process's kernel stack (and therefore current_thread_info() will
  729. * give the wrong answer).
  730. * We have to restore various SPRs that may have been in use at the
  731. * time of the critical interrupt.
  732. *
  733. */
  734. #ifdef CONFIG_40x
  735. #define PPC_40x_TURN_OFF_MSR_DR \
  736. /* avoid any possible TLB misses here by turning off MSR.DR, we \
  737. * assume the instructions here are mapped by a pinned TLB entry */ \
  738. li r10,MSR_IR; \
  739. mtmsr r10; \
  740. isync; \
  741. tophys(r1, r1);
  742. #else
  743. #define PPC_40x_TURN_OFF_MSR_DR
  744. #endif
  745. #define RET_FROM_EXC_LEVEL(exc_lvl_srr0, exc_lvl_srr1, exc_lvl_rfi) \
  746. REST_NVGPRS(r1); \
  747. lwz r3,_MSR(r1); \
  748. andi. r3,r3,MSR_PR; \
  749. LOAD_MSR_KERNEL(r10,MSR_KERNEL); \
  750. bne user_exc_return; \
  751. lwz r0,GPR0(r1); \
  752. lwz r2,GPR2(r1); \
  753. REST_4GPRS(3, r1); \
  754. REST_2GPRS(7, r1); \
  755. lwz r10,_XER(r1); \
  756. lwz r11,_CTR(r1); \
  757. mtspr SPRN_XER,r10; \
  758. mtctr r11; \
  759. PPC405_ERR77(0,r1); \
  760. stwcx. r0,0,r1; /* to clear the reservation */ \
  761. lwz r11,_LINK(r1); \
  762. mtlr r11; \
  763. lwz r10,_CCR(r1); \
  764. mtcrf 0xff,r10; \
  765. PPC_40x_TURN_OFF_MSR_DR; \
  766. lwz r9,_DEAR(r1); \
  767. lwz r10,_ESR(r1); \
  768. mtspr SPRN_DEAR,r9; \
  769. mtspr SPRN_ESR,r10; \
  770. lwz r11,_NIP(r1); \
  771. lwz r12,_MSR(r1); \
  772. mtspr exc_lvl_srr0,r11; \
  773. mtspr exc_lvl_srr1,r12; \
  774. lwz r9,GPR9(r1); \
  775. lwz r12,GPR12(r1); \
  776. lwz r10,GPR10(r1); \
  777. lwz r11,GPR11(r1); \
  778. lwz r1,GPR1(r1); \
  779. PPC405_ERR77_SYNC; \
  780. exc_lvl_rfi; \
  781. b .; /* prevent prefetch past exc_lvl_rfi */
  782. .globl ret_from_crit_exc
  783. ret_from_crit_exc:
  784. RET_FROM_EXC_LEVEL(SPRN_CSRR0, SPRN_CSRR1, RFCI)
  785. #ifdef CONFIG_BOOKE
  786. .globl ret_from_debug_exc
  787. ret_from_debug_exc:
  788. RET_FROM_EXC_LEVEL(SPRN_DSRR0, SPRN_DSRR1, RFDI)
  789. .globl ret_from_mcheck_exc
  790. ret_from_mcheck_exc:
  791. RET_FROM_EXC_LEVEL(SPRN_MCSRR0, SPRN_MCSRR1, RFMCI)
  792. #endif /* CONFIG_BOOKE */
  793. /*
  794. * Load the DBCR0 value for a task that is being ptraced,
  795. * having first saved away the global DBCR0. Note that r0
  796. * has the dbcr0 value to set upon entry to this.
  797. */
  798. load_dbcr0:
  799. mfmsr r10 /* first disable debug exceptions */
  800. rlwinm r10,r10,0,~MSR_DE
  801. mtmsr r10
  802. isync
  803. mfspr r10,SPRN_DBCR0
  804. lis r11,global_dbcr0@ha
  805. addi r11,r11,global_dbcr0@l
  806. stw r10,0(r11)
  807. mtspr SPRN_DBCR0,r0
  808. lwz r10,4(r11)
  809. addi r10,r10,1
  810. stw r10,4(r11)
  811. li r11,-1
  812. mtspr SPRN_DBSR,r11 /* clear all pending debug events */
  813. blr
  814. .section .bss
  815. .align 4
  816. global_dbcr0:
  817. .space 8
  818. .previous
  819. #endif /* !(CONFIG_4xx || CONFIG_BOOKE) */
  820. do_work: /* r10 contains MSR_KERNEL here */
  821. andi. r0,r9,_TIF_NEED_RESCHED
  822. beq do_user_signal
  823. do_resched: /* r10 contains MSR_KERNEL here */
  824. ori r10,r10,MSR_EE
  825. SYNC
  826. MTMSRD(r10) /* hard-enable interrupts */
  827. bl schedule
  828. recheck:
  829. LOAD_MSR_KERNEL(r10,MSR_KERNEL)
  830. SYNC
  831. MTMSRD(r10) /* disable interrupts */
  832. rlwinm r9,r1,0,0,18
  833. lwz r9,TI_FLAGS(r9)
  834. andi. r0,r9,_TIF_NEED_RESCHED
  835. bne- do_resched
  836. andi. r0,r9,_TIF_SIGPENDING
  837. beq restore_user
  838. do_user_signal: /* r10 contains MSR_KERNEL here */
  839. ori r10,r10,MSR_EE
  840. SYNC
  841. MTMSRD(r10) /* hard-enable interrupts */
  842. /* save r13-r31 in the exception frame, if not already done */
  843. lwz r3,TRAP(r1)
  844. andi. r0,r3,1
  845. beq 2f
  846. SAVE_NVGPRS(r1)
  847. rlwinm r3,r3,0,0,30
  848. stw r3,TRAP(r1)
  849. 2: li r3,0
  850. addi r4,r1,STACK_FRAME_OVERHEAD
  851. bl do_signal
  852. REST_NVGPRS(r1)
  853. b recheck
  854. /*
  855. * We come here when we are at the end of handling an exception
  856. * that occurred at a place where taking an exception will lose
  857. * state information, such as the contents of SRR0 and SRR1.
  858. */
  859. nonrecoverable:
  860. lis r10,exc_exit_restart_end@ha
  861. addi r10,r10,exc_exit_restart_end@l
  862. cmplw r12,r10
  863. bge 3f
  864. lis r11,exc_exit_restart@ha
  865. addi r11,r11,exc_exit_restart@l
  866. cmplw r12,r11
  867. blt 3f
  868. lis r10,ee_restarts@ha
  869. lwz r12,ee_restarts@l(r10)
  870. addi r12,r12,1
  871. stw r12,ee_restarts@l(r10)
  872. mr r12,r11 /* restart at exc_exit_restart */
  873. blr
  874. 3: /* OK, we can't recover, kill this process */
  875. /* but the 601 doesn't implement the RI bit, so assume it's OK */
  876. BEGIN_FTR_SECTION
  877. blr
  878. END_FTR_SECTION_IFSET(CPU_FTR_601)
  879. lwz r3,TRAP(r1)
  880. andi. r0,r3,1
  881. beq 4f
  882. SAVE_NVGPRS(r1)
  883. rlwinm r3,r3,0,0,30
  884. stw r3,TRAP(r1)
  885. 4: addi r3,r1,STACK_FRAME_OVERHEAD
  886. bl nonrecoverable_exception
  887. /* shouldn't return */
  888. b 4b
  889. .section .bss
  890. .align 2
  891. ee_restarts:
  892. .space 4
  893. .previous