entry_32.S 25 KB

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