entry.S 31 KB

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