entry_32.S 25 KB

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