entry.S 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. /*
  2. * OpenRISC entry.S
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2005 Gyorgy Jeney <nog@bsemi.com>
  11. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * as published by the Free Software Foundation; either version
  16. * 2 of the License, or (at your option) any later version.
  17. */
  18. #include <linux/linkage.h>
  19. #include <asm/processor.h>
  20. #include <asm/unistd.h>
  21. #include <asm/thread_info.h>
  22. #include <asm/errno.h>
  23. #include <asm/spr_defs.h>
  24. #include <asm/page.h>
  25. #include <asm/mmu.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/asm-offsets.h>
  28. #define DISABLE_INTERRUPTS(t1,t2) \
  29. l.mfspr t2,r0,SPR_SR ;\
  30. l.movhi t1,hi(~(SPR_SR_IEE|SPR_SR_TEE)) ;\
  31. l.ori t1,t1,lo(~(SPR_SR_IEE|SPR_SR_TEE)) ;\
  32. l.and t2,t2,t1 ;\
  33. l.mtspr r0,t2,SPR_SR
  34. #define ENABLE_INTERRUPTS(t1) \
  35. l.mfspr t1,r0,SPR_SR ;\
  36. l.ori t1,t1,lo(SPR_SR_IEE|SPR_SR_TEE) ;\
  37. l.mtspr r0,t1,SPR_SR
  38. /* =========================================================[ macros ]=== */
  39. /*
  40. * We need to disable interrupts at beginning of RESTORE_ALL
  41. * since interrupt might come in after we've loaded EPC return address
  42. * and overwrite EPC with address somewhere in RESTORE_ALL
  43. * which is of course wrong!
  44. */
  45. #define RESTORE_ALL \
  46. DISABLE_INTERRUPTS(r3,r4) ;\
  47. l.lwz r3,PT_PC(r1) ;\
  48. l.mtspr r0,r3,SPR_EPCR_BASE ;\
  49. l.lwz r3,PT_SR(r1) ;\
  50. l.mtspr r0,r3,SPR_ESR_BASE ;\
  51. l.lwz r2,PT_GPR2(r1) ;\
  52. l.lwz r3,PT_GPR3(r1) ;\
  53. l.lwz r4,PT_GPR4(r1) ;\
  54. l.lwz r5,PT_GPR5(r1) ;\
  55. l.lwz r6,PT_GPR6(r1) ;\
  56. l.lwz r7,PT_GPR7(r1) ;\
  57. l.lwz r8,PT_GPR8(r1) ;\
  58. l.lwz r9,PT_GPR9(r1) ;\
  59. l.lwz r10,PT_GPR10(r1) ;\
  60. l.lwz r11,PT_GPR11(r1) ;\
  61. l.lwz r12,PT_GPR12(r1) ;\
  62. l.lwz r13,PT_GPR13(r1) ;\
  63. l.lwz r14,PT_GPR14(r1) ;\
  64. l.lwz r15,PT_GPR15(r1) ;\
  65. l.lwz r16,PT_GPR16(r1) ;\
  66. l.lwz r17,PT_GPR17(r1) ;\
  67. l.lwz r18,PT_GPR18(r1) ;\
  68. l.lwz r19,PT_GPR19(r1) ;\
  69. l.lwz r20,PT_GPR20(r1) ;\
  70. l.lwz r21,PT_GPR21(r1) ;\
  71. l.lwz r22,PT_GPR22(r1) ;\
  72. l.lwz r23,PT_GPR23(r1) ;\
  73. l.lwz r24,PT_GPR24(r1) ;\
  74. l.lwz r25,PT_GPR25(r1) ;\
  75. l.lwz r26,PT_GPR26(r1) ;\
  76. l.lwz r27,PT_GPR27(r1) ;\
  77. l.lwz r28,PT_GPR28(r1) ;\
  78. l.lwz r29,PT_GPR29(r1) ;\
  79. l.lwz r30,PT_GPR30(r1) ;\
  80. l.lwz r31,PT_GPR31(r1) ;\
  81. l.lwz r1,PT_SP(r1) ;\
  82. l.rfe
  83. #define EXCEPTION_ENTRY(handler) \
  84. .global handler ;\
  85. handler: ;\
  86. /* r1, EPCR, ESR a already saved */ ;\
  87. l.sw PT_GPR2(r1),r2 ;\
  88. l.sw PT_GPR3(r1),r3 ;\
  89. /* r4 already save */ ;\
  90. l.sw PT_GPR5(r1),r5 ;\
  91. l.sw PT_GPR6(r1),r6 ;\
  92. l.sw PT_GPR7(r1),r7 ;\
  93. l.sw PT_GPR8(r1),r8 ;\
  94. l.sw PT_GPR9(r1),r9 ;\
  95. /* r10 already saved */ ;\
  96. l.sw PT_GPR11(r1),r11 ;\
  97. /* r12 already saved */ ;\
  98. l.sw PT_GPR13(r1),r13 ;\
  99. l.sw PT_GPR14(r1),r14 ;\
  100. l.sw PT_GPR15(r1),r15 ;\
  101. l.sw PT_GPR16(r1),r16 ;\
  102. l.sw PT_GPR17(r1),r17 ;\
  103. l.sw PT_GPR18(r1),r18 ;\
  104. l.sw PT_GPR19(r1),r19 ;\
  105. l.sw PT_GPR20(r1),r20 ;\
  106. l.sw PT_GPR21(r1),r21 ;\
  107. l.sw PT_GPR22(r1),r22 ;\
  108. l.sw PT_GPR23(r1),r23 ;\
  109. l.sw PT_GPR24(r1),r24 ;\
  110. l.sw PT_GPR25(r1),r25 ;\
  111. l.sw PT_GPR26(r1),r26 ;\
  112. l.sw PT_GPR27(r1),r27 ;\
  113. l.sw PT_GPR28(r1),r28 ;\
  114. l.sw PT_GPR29(r1),r29 ;\
  115. /* r30 already save */ ;\
  116. /* l.sw PT_GPR30(r1),r30*/ ;\
  117. l.sw PT_GPR31(r1),r31 ;\
  118. /* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\
  119. l.addi r30,r0,-1 ;\
  120. l.sw PT_ORIG_GPR11(r1),r30
  121. #define UNHANDLED_EXCEPTION(handler,vector) \
  122. .global handler ;\
  123. handler: ;\
  124. /* r1, EPCR, ESR already saved */ ;\
  125. l.sw PT_GPR2(r1),r2 ;\
  126. l.sw PT_GPR3(r1),r3 ;\
  127. l.sw PT_GPR5(r1),r5 ;\
  128. l.sw PT_GPR6(r1),r6 ;\
  129. l.sw PT_GPR7(r1),r7 ;\
  130. l.sw PT_GPR8(r1),r8 ;\
  131. l.sw PT_GPR9(r1),r9 ;\
  132. /* r10 already saved */ ;\
  133. l.sw PT_GPR11(r1),r11 ;\
  134. /* r12 already saved */ ;\
  135. l.sw PT_GPR13(r1),r13 ;\
  136. l.sw PT_GPR14(r1),r14 ;\
  137. l.sw PT_GPR15(r1),r15 ;\
  138. l.sw PT_GPR16(r1),r16 ;\
  139. l.sw PT_GPR17(r1),r17 ;\
  140. l.sw PT_GPR18(r1),r18 ;\
  141. l.sw PT_GPR19(r1),r19 ;\
  142. l.sw PT_GPR20(r1),r20 ;\
  143. l.sw PT_GPR21(r1),r21 ;\
  144. l.sw PT_GPR22(r1),r22 ;\
  145. l.sw PT_GPR23(r1),r23 ;\
  146. l.sw PT_GPR24(r1),r24 ;\
  147. l.sw PT_GPR25(r1),r25 ;\
  148. l.sw PT_GPR26(r1),r26 ;\
  149. l.sw PT_GPR27(r1),r27 ;\
  150. l.sw PT_GPR28(r1),r28 ;\
  151. l.sw PT_GPR29(r1),r29 ;\
  152. /* r31 already saved */ ;\
  153. l.sw PT_GPR30(r1),r30 ;\
  154. /* l.sw PT_GPR31(r1),r31 */ ;\
  155. /* Store -1 in orig_gpr11 for non-syscall exceptions */ ;\
  156. l.addi r30,r0,-1 ;\
  157. l.sw PT_ORIG_GPR11(r1),r30 ;\
  158. l.addi r3,r1,0 ;\
  159. /* r4 is exception EA */ ;\
  160. l.addi r5,r0,vector ;\
  161. l.jal unhandled_exception ;\
  162. l.nop ;\
  163. l.j _ret_from_exception ;\
  164. l.nop
  165. /* clobbers 'reg' */
  166. #define CLEAR_LWA_FLAG(reg) \
  167. l.movhi reg,hi(lwa_flag) ;\
  168. l.ori reg,reg,lo(lwa_flag) ;\
  169. l.sw 0(reg),r0
  170. /*
  171. * NOTE: one should never assume that SPR_EPC, SPR_ESR, SPR_EEAR
  172. * contain the same values as when exception we're handling
  173. * occured. in fact they never do. if you need them use
  174. * values saved on stack (for SPR_EPC, SPR_ESR) or content
  175. * of r4 (for SPR_EEAR). for details look at EXCEPTION_HANDLE()
  176. * in 'arch/or32/kernel/head.S'
  177. */
  178. /* =====================================================[ exceptions] === */
  179. /* ---[ 0x100: RESET exception ]----------------------------------------- */
  180. EXCEPTION_ENTRY(_tng_kernel_start)
  181. l.jal _start
  182. l.andi r0,r0,0
  183. /* ---[ 0x200: BUS exception ]------------------------------------------- */
  184. EXCEPTION_ENTRY(_bus_fault_handler)
  185. CLEAR_LWA_FLAG(r3)
  186. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  187. l.jal do_bus_fault
  188. l.addi r3,r1,0 /* pt_regs */
  189. l.j _ret_from_exception
  190. l.nop
  191. /* ---[ 0x300: Data Page Fault exception ]------------------------------- */
  192. EXCEPTION_ENTRY(_dtlb_miss_page_fault_handler)
  193. CLEAR_LWA_FLAG(r3)
  194. l.and r5,r5,r0
  195. l.j 1f
  196. l.nop
  197. EXCEPTION_ENTRY(_data_page_fault_handler)
  198. CLEAR_LWA_FLAG(r3)
  199. /* set up parameters for do_page_fault */
  200. l.ori r5,r0,0x300 // exception vector
  201. 1:
  202. l.addi r3,r1,0 // pt_regs
  203. /* r4 set be EXCEPTION_HANDLE */ // effective address of fault
  204. /*
  205. * __PHX__: TODO
  206. *
  207. * all this can be written much simpler. look at
  208. * DTLB miss handler in the CONFIG_GUARD_PROTECTED_CORE part
  209. */
  210. #ifdef CONFIG_OPENRISC_NO_SPR_SR_DSX
  211. l.lwz r6,PT_PC(r3) // address of an offending insn
  212. l.lwz r6,0(r6) // instruction that caused pf
  213. l.srli r6,r6,26 // check opcode for jump insn
  214. l.sfeqi r6,0 // l.j
  215. l.bf 8f
  216. l.sfeqi r6,1 // l.jal
  217. l.bf 8f
  218. l.sfeqi r6,3 // l.bnf
  219. l.bf 8f
  220. l.sfeqi r6,4 // l.bf
  221. l.bf 8f
  222. l.sfeqi r6,0x11 // l.jr
  223. l.bf 8f
  224. l.sfeqi r6,0x12 // l.jalr
  225. l.bf 8f
  226. l.nop
  227. l.j 9f
  228. l.nop
  229. 8: // offending insn is in delay slot
  230. l.lwz r6,PT_PC(r3) // address of an offending insn
  231. l.addi r6,r6,4
  232. l.lwz r6,0(r6) // instruction that caused pf
  233. l.srli r6,r6,26 // get opcode
  234. 9: // offending instruction opcode loaded in r6
  235. #else
  236. l.lwz r6,PT_SR(r3) // SR
  237. l.andi r6,r6,SPR_SR_DSX // check for delay slot exception
  238. l.sfne r6,r0 // exception happened in delay slot
  239. l.bnf 7f
  240. l.lwz r6,PT_PC(r3) // address of an offending insn
  241. l.addi r6,r6,4 // offending insn is in delay slot
  242. 7:
  243. l.lwz r6,0(r6) // instruction that caused pf
  244. l.srli r6,r6,26 // check opcode for write access
  245. #endif
  246. l.sfgeui r6,0x33 // check opcode for write access
  247. l.bnf 1f
  248. l.sfleui r6,0x37
  249. l.bnf 1f
  250. l.ori r6,r0,0x1 // write access
  251. l.j 2f
  252. l.nop
  253. 1: l.ori r6,r0,0x0 // !write access
  254. 2:
  255. /* call fault.c handler in or32/mm/fault.c */
  256. l.jal do_page_fault
  257. l.nop
  258. l.j _ret_from_exception
  259. l.nop
  260. /* ---[ 0x400: Insn Page Fault exception ]------------------------------- */
  261. EXCEPTION_ENTRY(_itlb_miss_page_fault_handler)
  262. CLEAR_LWA_FLAG(r3)
  263. l.and r5,r5,r0
  264. l.j 1f
  265. l.nop
  266. EXCEPTION_ENTRY(_insn_page_fault_handler)
  267. CLEAR_LWA_FLAG(r3)
  268. /* set up parameters for do_page_fault */
  269. l.ori r5,r0,0x400 // exception vector
  270. 1:
  271. l.addi r3,r1,0 // pt_regs
  272. /* r4 set be EXCEPTION_HANDLE */ // effective address of fault
  273. l.ori r6,r0,0x0 // !write access
  274. /* call fault.c handler in or32/mm/fault.c */
  275. l.jal do_page_fault
  276. l.nop
  277. l.j _ret_from_exception
  278. l.nop
  279. /* ---[ 0x500: Timer exception ]----------------------------------------- */
  280. EXCEPTION_ENTRY(_timer_handler)
  281. CLEAR_LWA_FLAG(r3)
  282. l.jal timer_interrupt
  283. l.addi r3,r1,0 /* pt_regs */
  284. l.j _ret_from_intr
  285. l.nop
  286. /* ---[ 0x600: Alignment exception ]-------------------------------------- */
  287. EXCEPTION_ENTRY(_alignment_handler)
  288. CLEAR_LWA_FLAG(r3)
  289. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  290. l.jal do_unaligned_access
  291. l.addi r3,r1,0 /* pt_regs */
  292. l.j _ret_from_exception
  293. l.nop
  294. #if 0
  295. EXCEPTION_ENTRY(_alignment_handler)
  296. // l.mfspr r2,r0,SPR_EEAR_BASE /* Load the effective address */
  297. l.addi r2,r4,0
  298. // l.mfspr r5,r0,SPR_EPCR_BASE /* Load the insn address */
  299. l.lwz r5,PT_PC(r1)
  300. l.lwz r3,0(r5) /* Load insn */
  301. l.srli r4,r3,26 /* Shift left to get the insn opcode */
  302. l.sfeqi r4,0x00 /* Check if the load/store insn is in delay slot */
  303. l.bf jmp
  304. l.sfeqi r4,0x01
  305. l.bf jmp
  306. l.sfeqi r4,0x03
  307. l.bf jmp
  308. l.sfeqi r4,0x04
  309. l.bf jmp
  310. l.sfeqi r4,0x11
  311. l.bf jr
  312. l.sfeqi r4,0x12
  313. l.bf jr
  314. l.nop
  315. l.j 1f
  316. l.addi r5,r5,4 /* Increment PC to get return insn address */
  317. jmp:
  318. l.slli r4,r3,6 /* Get the signed extended jump length */
  319. l.srai r4,r4,4
  320. l.lwz r3,4(r5) /* Load the real load/store insn */
  321. l.add r5,r5,r4 /* Calculate jump target address */
  322. l.j 1f
  323. l.srli r4,r3,26 /* Shift left to get the insn opcode */
  324. jr:
  325. l.slli r4,r3,9 /* Shift to get the reg nb */
  326. l.andi r4,r4,0x7c
  327. l.lwz r3,4(r5) /* Load the real load/store insn */
  328. l.add r4,r4,r1 /* Load the jump register value from the stack */
  329. l.lwz r5,0(r4)
  330. l.srli r4,r3,26 /* Shift left to get the insn opcode */
  331. 1:
  332. // l.mtspr r0,r5,SPR_EPCR_BASE
  333. l.sw PT_PC(r1),r5
  334. l.sfeqi r4,0x26
  335. l.bf lhs
  336. l.sfeqi r4,0x25
  337. l.bf lhz
  338. l.sfeqi r4,0x22
  339. l.bf lws
  340. l.sfeqi r4,0x21
  341. l.bf lwz
  342. l.sfeqi r4,0x37
  343. l.bf sh
  344. l.sfeqi r4,0x35
  345. l.bf sw
  346. l.nop
  347. 1: l.j 1b /* I don't know what to do */
  348. l.nop
  349. lhs: l.lbs r5,0(r2)
  350. l.slli r5,r5,8
  351. l.lbz r6,1(r2)
  352. l.or r5,r5,r6
  353. l.srli r4,r3,19
  354. l.andi r4,r4,0x7c
  355. l.add r4,r4,r1
  356. l.j align_end
  357. l.sw 0(r4),r5
  358. lhz: l.lbz r5,0(r2)
  359. l.slli r5,r5,8
  360. l.lbz r6,1(r2)
  361. l.or r5,r5,r6
  362. l.srli r4,r3,19
  363. l.andi r4,r4,0x7c
  364. l.add r4,r4,r1
  365. l.j align_end
  366. l.sw 0(r4),r5
  367. lws: l.lbs r5,0(r2)
  368. l.slli r5,r5,24
  369. l.lbz r6,1(r2)
  370. l.slli r6,r6,16
  371. l.or r5,r5,r6
  372. l.lbz r6,2(r2)
  373. l.slli r6,r6,8
  374. l.or r5,r5,r6
  375. l.lbz r6,3(r2)
  376. l.or r5,r5,r6
  377. l.srli r4,r3,19
  378. l.andi r4,r4,0x7c
  379. l.add r4,r4,r1
  380. l.j align_end
  381. l.sw 0(r4),r5
  382. lwz: l.lbz r5,0(r2)
  383. l.slli r5,r5,24
  384. l.lbz r6,1(r2)
  385. l.slli r6,r6,16
  386. l.or r5,r5,r6
  387. l.lbz r6,2(r2)
  388. l.slli r6,r6,8
  389. l.or r5,r5,r6
  390. l.lbz r6,3(r2)
  391. l.or r5,r5,r6
  392. l.srli r4,r3,19
  393. l.andi r4,r4,0x7c
  394. l.add r4,r4,r1
  395. l.j align_end
  396. l.sw 0(r4),r5
  397. sh:
  398. l.srli r4,r3,9
  399. l.andi r4,r4,0x7c
  400. l.add r4,r4,r1
  401. l.lwz r5,0(r4)
  402. l.sb 1(r2),r5
  403. l.srli r5,r5,8
  404. l.j align_end
  405. l.sb 0(r2),r5
  406. sw:
  407. l.srli r4,r3,9
  408. l.andi r4,r4,0x7c
  409. l.add r4,r4,r1
  410. l.lwz r5,0(r4)
  411. l.sb 3(r2),r5
  412. l.srli r5,r5,8
  413. l.sb 2(r2),r5
  414. l.srli r5,r5,8
  415. l.sb 1(r2),r5
  416. l.srli r5,r5,8
  417. l.j align_end
  418. l.sb 0(r2),r5
  419. align_end:
  420. l.j _ret_from_intr
  421. l.nop
  422. #endif
  423. /* ---[ 0x700: Illegal insn exception ]---------------------------------- */
  424. EXCEPTION_ENTRY(_illegal_instruction_handler)
  425. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  426. l.jal do_illegal_instruction
  427. l.addi r3,r1,0 /* pt_regs */
  428. l.j _ret_from_exception
  429. l.nop
  430. /* ---[ 0x800: External interrupt exception ]---------------------------- */
  431. EXCEPTION_ENTRY(_external_irq_handler)
  432. #ifdef CONFIG_OPENRISC_ESR_EXCEPTION_BUG_CHECK
  433. l.lwz r4,PT_SR(r1) // were interrupts enabled ?
  434. l.andi r4,r4,SPR_SR_IEE
  435. l.sfeqi r4,0
  436. l.bnf 1f // ext irq enabled, all ok.
  437. l.nop
  438. l.addi r1,r1,-0x8
  439. l.movhi r3,hi(42f)
  440. l.ori r3,r3,lo(42f)
  441. l.sw 0x0(r1),r3
  442. l.jal printk
  443. l.sw 0x4(r1),r4
  444. l.addi r1,r1,0x8
  445. .section .rodata, "a"
  446. 42:
  447. .string "\n\rESR interrupt bug: in _external_irq_handler (ESR %x)\n\r"
  448. .align 4
  449. .previous
  450. l.ori r4,r4,SPR_SR_IEE // fix the bug
  451. // l.sw PT_SR(r1),r4
  452. 1:
  453. #endif
  454. CLEAR_LWA_FLAG(r3)
  455. l.addi r3,r1,0
  456. l.movhi r8,hi(do_IRQ)
  457. l.ori r8,r8,lo(do_IRQ)
  458. l.jalr r8
  459. l.nop
  460. l.j _ret_from_intr
  461. l.nop
  462. /* ---[ 0x900: DTLB miss exception ]------------------------------------- */
  463. /* ---[ 0xa00: ITLB miss exception ]------------------------------------- */
  464. /* ---[ 0xb00: Range exception ]----------------------------------------- */
  465. UNHANDLED_EXCEPTION(_vector_0xb00,0xb00)
  466. /* ---[ 0xc00: Syscall exception ]--------------------------------------- */
  467. /*
  468. * Syscalls are a special type of exception in that they are
  469. * _explicitly_ invoked by userspace and can therefore be
  470. * held to conform to the same ABI as normal functions with
  471. * respect to whether registers are preserved across the call
  472. * or not.
  473. */
  474. /* Upon syscall entry we just save the callee-saved registers
  475. * and not the call-clobbered ones.
  476. */
  477. _string_syscall_return:
  478. .string "syscall return %ld \n\r\0"
  479. .align 4
  480. ENTRY(_sys_call_handler)
  481. /* syscalls run with interrupts enabled */
  482. ENABLE_INTERRUPTS(r29) // enable interrupts, r29 is temp
  483. /* r1, EPCR, ESR a already saved */
  484. l.sw PT_GPR2(r1),r2
  485. /* r3-r8 must be saved because syscall restart relies
  486. * on us being able to restart the syscall args... technically
  487. * they should be clobbered, otherwise
  488. */
  489. l.sw PT_GPR3(r1),r3
  490. /*
  491. * r4 already saved
  492. * r4 holds the EEAR address of the fault, use it as screatch reg and
  493. * then load the original r4
  494. */
  495. CLEAR_LWA_FLAG(r4)
  496. l.lwz r4,PT_GPR4(r1)
  497. l.sw PT_GPR5(r1),r5
  498. l.sw PT_GPR6(r1),r6
  499. l.sw PT_GPR7(r1),r7
  500. l.sw PT_GPR8(r1),r8
  501. l.sw PT_GPR9(r1),r9
  502. /* r10 already saved */
  503. l.sw PT_GPR11(r1),r11
  504. /* orig_gpr11 must be set for syscalls */
  505. l.sw PT_ORIG_GPR11(r1),r11
  506. /* r12,r13 already saved */
  507. /* r14-r28 (even) aren't touched by the syscall fast path below
  508. * so we don't need to save them. However, the functions that return
  509. * to userspace via a call to switch() DO need to save these because
  510. * switch() effectively clobbers them... saving these registers for
  511. * such functions is handled in their syscall wrappers (see fork, vfork,
  512. * and clone, below).
  513. /* r30 is the only register we clobber in the fast path */
  514. /* r30 already saved */
  515. /* l.sw PT_GPR30(r1),r30 */
  516. _syscall_check_trace_enter:
  517. /* If TIF_SYSCALL_TRACE is set, then we want to do syscall tracing */
  518. l.lwz r30,TI_FLAGS(r10)
  519. l.andi r30,r30,_TIF_SYSCALL_TRACE
  520. l.sfne r30,r0
  521. l.bf _syscall_trace_enter
  522. l.nop
  523. _syscall_check:
  524. /* Ensure that the syscall number is reasonable */
  525. l.sfgeui r11,__NR_syscalls
  526. l.bf _syscall_badsys
  527. l.nop
  528. _syscall_call:
  529. l.movhi r29,hi(sys_call_table)
  530. l.ori r29,r29,lo(sys_call_table)
  531. l.slli r11,r11,2
  532. l.add r29,r29,r11
  533. l.lwz r29,0(r29)
  534. l.jalr r29
  535. l.nop
  536. _syscall_return:
  537. /* All syscalls return here... just pay attention to ret_from_fork
  538. * which does it in a round-about way.
  539. */
  540. l.sw PT_GPR11(r1),r11 // save return value
  541. #if 0
  542. _syscall_debug:
  543. l.movhi r3,hi(_string_syscall_return)
  544. l.ori r3,r3,lo(_string_syscall_return)
  545. l.ori r27,r0,1
  546. l.sw -4(r1),r27
  547. l.sw -8(r1),r11
  548. l.addi r1,r1,-8
  549. l.movhi r27,hi(printk)
  550. l.ori r27,r27,lo(printk)
  551. l.jalr r27
  552. l.nop
  553. l.addi r1,r1,8
  554. #endif
  555. _syscall_check_trace_leave:
  556. /* r30 is a callee-saved register so this should still hold the
  557. * _TIF_SYSCALL_TRACE flag from _syscall_check_trace_enter above...
  558. * _syscall_trace_leave expects syscall result to be in pt_regs->r11.
  559. */
  560. l.sfne r30,r0
  561. l.bf _syscall_trace_leave
  562. l.nop
  563. /* This is where the exception-return code begins... interrupts need to be
  564. * disabled the rest of the way here because we can't afford to miss any
  565. * interrupts that set NEED_RESCHED or SIGNALPENDING... really true? */
  566. _syscall_check_work:
  567. /* Here we need to disable interrupts */
  568. DISABLE_INTERRUPTS(r27,r29)
  569. l.lwz r30,TI_FLAGS(r10)
  570. l.andi r30,r30,_TIF_WORK_MASK
  571. l.sfne r30,r0
  572. l.bnf _syscall_resume_userspace
  573. l.nop
  574. /* Work pending follows a different return path, so we need to
  575. * make sure that all the call-saved registers get into pt_regs
  576. * before branching...
  577. */
  578. l.sw PT_GPR14(r1),r14
  579. l.sw PT_GPR16(r1),r16
  580. l.sw PT_GPR18(r1),r18
  581. l.sw PT_GPR20(r1),r20
  582. l.sw PT_GPR22(r1),r22
  583. l.sw PT_GPR24(r1),r24
  584. l.sw PT_GPR26(r1),r26
  585. l.sw PT_GPR28(r1),r28
  586. /* _work_pending needs to be called with interrupts disabled */
  587. l.j _work_pending
  588. l.nop
  589. _syscall_resume_userspace:
  590. // ENABLE_INTERRUPTS(r29)
  591. /* This is the hot path for returning to userspace from a syscall. If there's
  592. * work to be done and the branch to _work_pending was taken above, then the
  593. * return to userspace will be done via the normal exception return path...
  594. * that path restores _all_ registers and will overwrite the "clobbered"
  595. * registers with whatever garbage is in pt_regs -- that's OK because those
  596. * registers are clobbered anyway and because the extra work is insignificant
  597. * in the context of the extra work that _work_pending is doing.
  598. /* Once again, syscalls are special and only guarantee to preserve the
  599. * same registers as a normal function call */
  600. /* The assumption here is that the registers r14-r28 (even) are untouched and
  601. * don't need to be restored... be sure that that's really the case!
  602. */
  603. /* This is still too much... we should only be restoring what we actually
  604. * clobbered... we should even be using 'scratch' (odd) regs above so that
  605. * we don't need to restore anything, hardly...
  606. */
  607. l.lwz r2,PT_GPR2(r1)
  608. /* Restore args */
  609. /* r3-r8 are technically clobbered, but syscall restart needs these
  610. * to be restored...
  611. */
  612. l.lwz r3,PT_GPR3(r1)
  613. l.lwz r4,PT_GPR4(r1)
  614. l.lwz r5,PT_GPR5(r1)
  615. l.lwz r6,PT_GPR6(r1)
  616. l.lwz r7,PT_GPR7(r1)
  617. l.lwz r8,PT_GPR8(r1)
  618. l.lwz r9,PT_GPR9(r1)
  619. l.lwz r10,PT_GPR10(r1)
  620. l.lwz r11,PT_GPR11(r1)
  621. /* r30 is the only register we clobber in the fast path */
  622. l.lwz r30,PT_GPR30(r1)
  623. /* Here we use r13-r19 (odd) as scratch regs */
  624. l.lwz r13,PT_PC(r1)
  625. l.lwz r15,PT_SR(r1)
  626. l.lwz r1,PT_SP(r1)
  627. /* Interrupts need to be disabled for setting EPCR and ESR
  628. * so that another interrupt doesn't come in here and clobber
  629. * them before we can use them for our l.rfe */
  630. DISABLE_INTERRUPTS(r17,r19)
  631. l.mtspr r0,r13,SPR_EPCR_BASE
  632. l.mtspr r0,r15,SPR_ESR_BASE
  633. l.rfe
  634. /* End of hot path!
  635. * Keep the below tracing and error handling out of the hot path...
  636. */
  637. _syscall_trace_enter:
  638. /* Here we pass pt_regs to do_syscall_trace_enter. Make sure
  639. * that function is really getting all the info it needs as
  640. * pt_regs isn't a complete set of userspace regs, just the
  641. * ones relevant to the syscall...
  642. *
  643. * Note use of delay slot for setting argument.
  644. */
  645. l.jal do_syscall_trace_enter
  646. l.addi r3,r1,0
  647. /* Restore arguments (not preserved across do_syscall_trace_enter)
  648. * so that we can do the syscall for real and return to the syscall
  649. * hot path.
  650. */
  651. l.lwz r11,PT_GPR11(r1)
  652. l.lwz r3,PT_GPR3(r1)
  653. l.lwz r4,PT_GPR4(r1)
  654. l.lwz r5,PT_GPR5(r1)
  655. l.lwz r6,PT_GPR6(r1)
  656. l.lwz r7,PT_GPR7(r1)
  657. l.j _syscall_check
  658. l.lwz r8,PT_GPR8(r1)
  659. _syscall_trace_leave:
  660. l.jal do_syscall_trace_leave
  661. l.addi r3,r1,0
  662. l.j _syscall_check_work
  663. l.nop
  664. _syscall_badsys:
  665. /* Here we effectively pretend to have executed an imaginary
  666. * syscall that returns -ENOSYS and then return to the regular
  667. * syscall hot path.
  668. * Note that "return value" is set in the delay slot...
  669. */
  670. l.j _syscall_return
  671. l.addi r11,r0,-ENOSYS
  672. /******* END SYSCALL HANDLING *******/
  673. /* ---[ 0xd00: Trap exception ]------------------------------------------ */
  674. UNHANDLED_EXCEPTION(_vector_0xd00,0xd00)
  675. /* ---[ 0xe00: Trap exception ]------------------------------------------ */
  676. EXCEPTION_ENTRY(_trap_handler)
  677. CLEAR_LWA_FLAG(r3)
  678. /* r4: EA of fault (set by EXCEPTION_HANDLE) */
  679. l.jal do_trap
  680. l.addi r3,r1,0 /* pt_regs */
  681. l.j _ret_from_exception
  682. l.nop
  683. /* ---[ 0xf00: Reserved exception ]-------------------------------------- */
  684. UNHANDLED_EXCEPTION(_vector_0xf00,0xf00)
  685. /* ---[ 0x1000: Reserved exception ]------------------------------------- */
  686. UNHANDLED_EXCEPTION(_vector_0x1000,0x1000)
  687. /* ---[ 0x1100: Reserved exception ]------------------------------------- */
  688. UNHANDLED_EXCEPTION(_vector_0x1100,0x1100)
  689. /* ---[ 0x1200: Reserved exception ]------------------------------------- */
  690. UNHANDLED_EXCEPTION(_vector_0x1200,0x1200)
  691. /* ---[ 0x1300: Reserved exception ]------------------------------------- */
  692. UNHANDLED_EXCEPTION(_vector_0x1300,0x1300)
  693. /* ---[ 0x1400: Reserved exception ]------------------------------------- */
  694. UNHANDLED_EXCEPTION(_vector_0x1400,0x1400)
  695. /* ---[ 0x1500: Reserved exception ]------------------------------------- */
  696. UNHANDLED_EXCEPTION(_vector_0x1500,0x1500)
  697. /* ---[ 0x1600: Reserved exception ]------------------------------------- */
  698. UNHANDLED_EXCEPTION(_vector_0x1600,0x1600)
  699. /* ---[ 0x1700: Reserved exception ]------------------------------------- */
  700. UNHANDLED_EXCEPTION(_vector_0x1700,0x1700)
  701. /* ---[ 0x1800: Reserved exception ]------------------------------------- */
  702. UNHANDLED_EXCEPTION(_vector_0x1800,0x1800)
  703. /* ---[ 0x1900: Reserved exception ]------------------------------------- */
  704. UNHANDLED_EXCEPTION(_vector_0x1900,0x1900)
  705. /* ---[ 0x1a00: Reserved exception ]------------------------------------- */
  706. UNHANDLED_EXCEPTION(_vector_0x1a00,0x1a00)
  707. /* ---[ 0x1b00: Reserved exception ]------------------------------------- */
  708. UNHANDLED_EXCEPTION(_vector_0x1b00,0x1b00)
  709. /* ---[ 0x1c00: Reserved exception ]------------------------------------- */
  710. UNHANDLED_EXCEPTION(_vector_0x1c00,0x1c00)
  711. /* ---[ 0x1d00: Reserved exception ]------------------------------------- */
  712. UNHANDLED_EXCEPTION(_vector_0x1d00,0x1d00)
  713. /* ---[ 0x1e00: Reserved exception ]------------------------------------- */
  714. UNHANDLED_EXCEPTION(_vector_0x1e00,0x1e00)
  715. /* ---[ 0x1f00: Reserved exception ]------------------------------------- */
  716. UNHANDLED_EXCEPTION(_vector_0x1f00,0x1f00)
  717. /* ========================================================[ return ] === */
  718. _resume_userspace:
  719. DISABLE_INTERRUPTS(r3,r4)
  720. l.lwz r4,TI_FLAGS(r10)
  721. l.andi r13,r4,_TIF_WORK_MASK
  722. l.sfeqi r13,0
  723. l.bf _restore_all
  724. l.nop
  725. _work_pending:
  726. l.lwz r5,PT_ORIG_GPR11(r1)
  727. l.sfltsi r5,0
  728. l.bnf 1f
  729. l.nop
  730. l.andi r5,r5,0
  731. 1:
  732. l.jal do_work_pending
  733. l.ori r3,r1,0 /* pt_regs */
  734. l.sfeqi r11,0
  735. l.bf _restore_all
  736. l.nop
  737. l.sfltsi r11,0
  738. l.bnf 1f
  739. l.nop
  740. l.and r11,r11,r0
  741. l.ori r11,r11,__NR_restart_syscall
  742. l.j _syscall_check_trace_enter
  743. l.nop
  744. 1:
  745. l.lwz r11,PT_ORIG_GPR11(r1)
  746. /* Restore arg registers */
  747. l.lwz r3,PT_GPR3(r1)
  748. l.lwz r4,PT_GPR4(r1)
  749. l.lwz r5,PT_GPR5(r1)
  750. l.lwz r6,PT_GPR6(r1)
  751. l.lwz r7,PT_GPR7(r1)
  752. l.j _syscall_check_trace_enter
  753. l.lwz r8,PT_GPR8(r1)
  754. _restore_all:
  755. RESTORE_ALL
  756. /* This returns to userspace code */
  757. ENTRY(_ret_from_intr)
  758. ENTRY(_ret_from_exception)
  759. l.lwz r4,PT_SR(r1)
  760. l.andi r3,r4,SPR_SR_SM
  761. l.sfeqi r3,0
  762. l.bnf _restore_all
  763. l.nop
  764. l.j _resume_userspace
  765. l.nop
  766. ENTRY(ret_from_fork)
  767. l.jal schedule_tail
  768. l.nop
  769. /* Check if we are a kernel thread */
  770. l.sfeqi r20,0
  771. l.bf 1f
  772. l.nop
  773. /* ...we are a kernel thread so invoke the requested callback */
  774. l.jalr r20
  775. l.or r3,r22,r0
  776. 1:
  777. /* _syscall_returns expect r11 to contain return value */
  778. l.lwz r11,PT_GPR11(r1)
  779. /* The syscall fast path return expects call-saved registers
  780. * r12-r28 to be untouched, so we restore them here as they
  781. * will have been effectively clobbered when arriving here
  782. * via the call to switch()
  783. */
  784. l.lwz r12,PT_GPR12(r1)
  785. l.lwz r14,PT_GPR14(r1)
  786. l.lwz r16,PT_GPR16(r1)
  787. l.lwz r18,PT_GPR18(r1)
  788. l.lwz r20,PT_GPR20(r1)
  789. l.lwz r22,PT_GPR22(r1)
  790. l.lwz r24,PT_GPR24(r1)
  791. l.lwz r26,PT_GPR26(r1)
  792. l.lwz r28,PT_GPR28(r1)
  793. l.j _syscall_return
  794. l.nop
  795. /* ========================================================[ switch ] === */
  796. /*
  797. * This routine switches between two different tasks. The process
  798. * state of one is saved on its kernel stack. Then the state
  799. * of the other is restored from its kernel stack. The memory
  800. * management hardware is updated to the second process's state.
  801. * Finally, we can return to the second process, via the 'return'.
  802. *
  803. * Note: there are two ways to get to the "going out" portion
  804. * of this code; either by coming in via the entry (_switch)
  805. * or via "fork" which must set up an environment equivalent
  806. * to the "_switch" path. If you change this (or in particular, the
  807. * SAVE_REGS macro), you'll have to change the fork code also.
  808. */
  809. /* _switch MUST never lay on page boundry, cause it runs from
  810. * effective addresses and beeing interrupted by iTLB miss would kill it.
  811. * dTLB miss seams to never accour in the bad place since data accesses
  812. * are from task structures which are always page aligned.
  813. *
  814. * The problem happens in RESTORE_ALL_NO_R11 where we first set the EPCR
  815. * register, then load the previous register values and only at the end call
  816. * the l.rfe instruction. If get TLB miss in beetwen the EPCR register gets
  817. * garbled and we end up calling l.rfe with the wrong EPCR. (same probably
  818. * holds for ESR)
  819. *
  820. * To avoid this problems it is sufficient to align _switch to
  821. * some nice round number smaller than it's size...
  822. */
  823. /* ABI rules apply here... we either enter _switch via schedule() or via
  824. * an imaginary call to which we shall return at return_from_fork. Either
  825. * way, we are a function call and only need to preserve the callee-saved
  826. * registers when we return. As such, we don't need to save the registers
  827. * on the stack that we won't be returning as they were...
  828. */
  829. .align 0x400
  830. ENTRY(_switch)
  831. /* We don't store SR as _switch only gets called in a context where
  832. * the SR will be the same going in and coming out... */
  833. /* Set up new pt_regs struct for saving task state */
  834. l.addi r1,r1,-(INT_FRAME_SIZE)
  835. /* No need to store r1/PT_SP as it goes into KSP below */
  836. l.sw PT_GPR2(r1),r2
  837. l.sw PT_GPR9(r1),r9
  838. /* This is wrong, r12 shouldn't be here... but GCC is broken for the time being
  839. * and expects r12 to be callee-saved... */
  840. l.sw PT_GPR12(r1),r12
  841. l.sw PT_GPR14(r1),r14
  842. l.sw PT_GPR16(r1),r16
  843. l.sw PT_GPR18(r1),r18
  844. l.sw PT_GPR20(r1),r20
  845. l.sw PT_GPR22(r1),r22
  846. l.sw PT_GPR24(r1),r24
  847. l.sw PT_GPR26(r1),r26
  848. l.sw PT_GPR28(r1),r28
  849. l.sw PT_GPR30(r1),r30
  850. l.addi r11,r10,0 /* Save old 'current' to 'last' return value*/
  851. /* We use thread_info->ksp for storing the address of the above
  852. * structure so that we can get back to it later... we don't want
  853. * to lose the value of thread_info->ksp, though, so store it as
  854. * pt_regs->sp so that we can easily restore it when we are made
  855. * live again...
  856. */
  857. /* Save the old value of thread_info->ksp as pt_regs->sp */
  858. l.lwz r29,TI_KSP(r10)
  859. l.sw PT_SP(r1),r29
  860. /* Swap kernel stack pointers */
  861. l.sw TI_KSP(r10),r1 /* Save old stack pointer */
  862. l.or r10,r4,r0 /* Set up new current_thread_info */
  863. l.lwz r1,TI_KSP(r10) /* Load new stack pointer */
  864. /* Restore the old value of thread_info->ksp */
  865. l.lwz r29,PT_SP(r1)
  866. l.sw TI_KSP(r10),r29
  867. /* ...and restore the registers, except r11 because the return value
  868. * has already been set above.
  869. */
  870. l.lwz r2,PT_GPR2(r1)
  871. l.lwz r9,PT_GPR9(r1)
  872. /* No need to restore r10 */
  873. /* ...and do not restore r11 */
  874. /* This is wrong, r12 shouldn't be here... but GCC is broken for the time being
  875. * and expects r12 to be callee-saved... */
  876. l.lwz r12,PT_GPR12(r1)
  877. l.lwz r14,PT_GPR14(r1)
  878. l.lwz r16,PT_GPR16(r1)
  879. l.lwz r18,PT_GPR18(r1)
  880. l.lwz r20,PT_GPR20(r1)
  881. l.lwz r22,PT_GPR22(r1)
  882. l.lwz r24,PT_GPR24(r1)
  883. l.lwz r26,PT_GPR26(r1)
  884. l.lwz r28,PT_GPR28(r1)
  885. l.lwz r30,PT_GPR30(r1)
  886. /* Unwind stack to pre-switch state */
  887. l.addi r1,r1,(INT_FRAME_SIZE)
  888. /* Return via the link-register back to where we 'came from', where
  889. * that may be either schedule(), ret_from_fork(), or
  890. * ret_from_kernel_thread(). If we are returning to a new thread,
  891. * we are expected to have set up the arg to schedule_tail already,
  892. * hence we do so here unconditionally:
  893. */
  894. l.lwz r3,TI_TASK(r3) /* Load 'prev' as schedule_tail arg */
  895. l.jr r9
  896. l.nop
  897. /* ==================================================================== */
  898. /* These all use the delay slot for setting the argument register, so the
  899. * jump is always happening after the l.addi instruction.
  900. *
  901. * These are all just wrappers that don't touch the link-register r9, so the
  902. * return from the "real" syscall function will return back to the syscall
  903. * code that did the l.jal that brought us here.
  904. */
  905. /* fork requires that we save all the callee-saved registers because they
  906. * are all effectively clobbered by the call to _switch. Here we store
  907. * all the registers that aren't touched by the syscall fast path and thus
  908. * weren't saved there.
  909. */
  910. _fork_save_extra_regs_and_call:
  911. l.sw PT_GPR14(r1),r14
  912. l.sw PT_GPR16(r1),r16
  913. l.sw PT_GPR18(r1),r18
  914. l.sw PT_GPR20(r1),r20
  915. l.sw PT_GPR22(r1),r22
  916. l.sw PT_GPR24(r1),r24
  917. l.sw PT_GPR26(r1),r26
  918. l.jr r29
  919. l.sw PT_GPR28(r1),r28
  920. ENTRY(__sys_clone)
  921. l.movhi r29,hi(sys_clone)
  922. l.ori r29,r29,lo(sys_clone)
  923. l.j _fork_save_extra_regs_and_call
  924. l.addi r7,r1,0
  925. ENTRY(__sys_fork)
  926. l.movhi r29,hi(sys_fork)
  927. l.ori r29,r29,lo(sys_fork)
  928. l.j _fork_save_extra_regs_and_call
  929. l.addi r3,r1,0
  930. ENTRY(sys_rt_sigreturn)
  931. l.jal _sys_rt_sigreturn
  932. l.addi r3,r1,0
  933. l.sfne r30,r0
  934. l.bnf _no_syscall_trace
  935. l.nop
  936. l.jal do_syscall_trace_leave
  937. l.addi r3,r1,0
  938. _no_syscall_trace:
  939. l.j _resume_userspace
  940. l.nop
  941. /* This is a catch-all syscall for atomic instructions for the OpenRISC 1000.
  942. * The functions takes a variable number of parameters depending on which
  943. * particular flavour of atomic you want... parameter 1 is a flag identifying
  944. * the atomic in question. Currently, this function implements the
  945. * following variants:
  946. *
  947. * XCHG:
  948. * @flag: 1
  949. * @ptr1:
  950. * @ptr2:
  951. * Atomically exchange the values in pointers 1 and 2.
  952. *
  953. */
  954. ENTRY(sys_or1k_atomic)
  955. /* FIXME: This ignores r3 and always does an XCHG */
  956. DISABLE_INTERRUPTS(r17,r19)
  957. l.lwz r29,0(r4)
  958. l.lwz r27,0(r5)
  959. l.sw 0(r4),r27
  960. l.sw 0(r5),r29
  961. ENABLE_INTERRUPTS(r17)
  962. l.jr r9
  963. l.or r11,r0,r0
  964. /* ============================================================[ EOF ]=== */