Subversion Repositories HelenOS

Rev

Rev 2341 | Rev 2355 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2007 Petr Stepan
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup arm32
  30.  * @{
  31.  */
  32. /** @file
  33.     @brief  Exception handlers and exception initialization routines.
  34.  */
  35.  
  36.  
  37. #include <arch/exception.h>
  38. #include <arch/debug/print.h>
  39. #include <arch/memstr.h>
  40. #include <arch/regutils.h>
  41. #include <interrupt.h>
  42. #include <arch/machine.h>
  43. #include <arch/mm/page_fault.h>
  44. #include <print.h>
  45. #include <syscall/syscall.h>
  46.  
  47.  
  48. #define PREFETCH_OFFSET      0x8
  49. #define BRANCH_OPCODE        0xea000000
  50. #define LDR_OPCODE           0xe59ff000
  51. #define VALID_BRANCH_MASK    0xff000000
  52. #define EXC_VECTORS_SIZE     0x20
  53. #define EXC_VECTORS          0x8
  54.  
  55.  
  56. extern uintptr_t supervisor_sp;
  57. extern uintptr_t exc_stack;
  58.  
  59.  
  60. inline static void setup_stack_and_save_regs()
  61. {
  62. /*
  63.  
  64. str r0, =exc_stack
  65. str r1, =exc_stack + 4
  66. mrs r0, cpsr
  67. mrs r1, spsr
  68. and r0, r0, #0x1f
  69. and r1, r1, #0x1f
  70. cmp r0, r1
  71. be 3f
  72. @different exception mode => preserve sp
  73. ldr r1, [=exc_stack + 4]
  74. ldr r0, [=exc_stack]
  75.  
  76. 3:
  77. mrs r0, spsr
  78. stmfd sp!, {r0, sp, lr}
  79. */
  80.    
  81. asm volatile("ldr r13, =exc_stack       \n\
  82.     stmfd r13!, {r0}            \n\
  83.     mrs r0, spsr                \n\
  84.     and r0, r0, #0x1f           \n\
  85.     cmp r0, #0x10               \n\
  86.     bne 1f                  \n\
  87.                         \n\
  88.     @prev mode was usermode         \n\
  89.     ldmfd r13!, {r0}            \n\
  90.     ldr r13, =supervisor_sp         \n\
  91.     ldr r13, [r13]              \n\
  92.     stmfd r13!, {lr}            \n\
  93.     stmfd r13!, {r0-r12}            \n\
  94.     stmfd r13!, {r13, lr}^          \n\
  95.     mrs r0, spsr                \n\
  96.     stmfd r13!, {r0}            \n\
  97.     b 2f                    \n\
  98.                         \n\
  99.     @prev mode was not usermode     \n\
  100. 1:                      \n\
  101.     stmfd r13!, {r1, r2, r3}        \n\
  102.     mrs r1, cpsr                \n\
  103.     mov r2, lr              \n\
  104.     bic r1, r1, #0x1f           \n\
  105.     orr r1, r1, r0              \n\
  106.     mrs r0, cpsr                \n\
  107.     msr cpsr_c, r1              \n\
  108.                         \n\
  109.     mov r3, r13             \n\
  110.     stmfd r13!, {r2}            \n\
  111.     mov r2, lr              \n\
  112.     stmfd r13!, {r4-r12}            \n\
  113.     mov r1, r13             \n\
  114.     @following two lines are for debugging  \n\
  115.     mov sp, #0              \n\
  116.     mov lr, #0              \n\
  117.     msr cpsr_c, r0              \n\
  118.                         \n\
  119.     ldmfd r13!, {r4, r5, r6, r7}        \n\
  120.     stmfd r1!, {r4, r5, r6}         \n\
  121.     stmfd r1!, {r7}             \n\
  122.     stmfd r1!, {r2}             \n\
  123.     stmfd r1!, {r3}             \n\
  124.     mrs r0, spsr                \n\
  125.     stmfd r1!, {r0}             \n\
  126.     mov r13, r1             \n\
  127. 2:"
  128. );
  129. }
  130.  
  131.  
  132. inline static void load_regs()
  133. {
  134. asm volatile(   "ldmfd r13!, {r0}       \n\
  135.     msr spsr, r0                \n\
  136.     and r0, r0, #0x1f           \n\
  137.     cmp r0, #0x10               \n\
  138.     bne 3f                  \n\
  139.                         \n\
  140.     @return to user mode            \n\
  141.     ldmfd r13!, {r13, lr}^          \n\
  142.     b 4f                    \n\
  143.                         \n\
  144.     @return to non-user mode        \n\
  145. 3:                      \n\
  146.     ldmfd r13!, {r1, r2}            \n\
  147.     mrs r3, cpsr                \n\
  148.     bic r3, r3, #0x1f           \n\
  149.     orr r3, r3, r0              \n\
  150.     mrs r0, cpsr                \n\
  151.     msr cpsr_c, r3              \n\
  152.                         \n\
  153.     mov r13, r1             \n\
  154.     mov lr, r2              \n\
  155.     msr cpsr_c, r0              \n\
  156.                         \n\
  157.     @actual return              \n\
  158. 4:  ldmfd r13, {r0-r12, pc}^"
  159. );
  160. }
  161.  
  162.  
  163.  
  164. /*#define SAVE_REGS_TO_STACK            \
  165.     asm("stmfd r13!, {r0-r12, r13, lr}");   \
  166.     asm("mrs r14, spsr");           \
  167.     asm("stmfd r13!, {r14}");
  168. */
  169.  
  170.  
  171. #define CALL_EXC_DISPATCH(exception)        \
  172.     asm("mov r0, %0" : : "i" (exception));  \
  173.     asm("mov r1, r13");         \
  174.     asm("bl exc_dispatch");    
  175.  
  176.  
  177. /**Loads registers from the stack and resets SPSR before exitting exception
  178.  * handler.
  179.  
  180. #define LOAD_REGS_FROM_STACK            \
  181.     asm("ldmfd r13!, {r14}");       \
  182.     asm("msr spsr, r14");           \
  183.     asm("ldmfd r13!, {r0-r12, r13, pc}^");
  184.  */
  185.  
  186.    
  187. /** General exception handler.
  188.  *  Stores registers, dispatches the exception,
  189.  *  and finally restores registers and returns from exception processing.
  190.  *
  191.  *  @param exception Exception number.
  192.  */
  193. #define PROCESS_EXCEPTION(exception)        \
  194.     setup_stack_and_save_regs();        \
  195.     CALL_EXC_DISPATCH(exception)        \
  196.     load_regs();
  197.  
  198. /* #define PROCESS_EXCEPTION(exception)     \
  199.     SAVE_REGS_TO_STACK      \
  200.     CALL_EXC_DISPATCH(exception)        \
  201.     LOAD_REGS_FROM_STACK*/
  202.  
  203.  
  204. /** Updates specified exception vector to jump to given handler.
  205.  *  Addresses of handlers are stored in memory following exception vectors.
  206.  */
  207. static void install_handler (unsigned handler_addr, unsigned* vector)
  208. {
  209.     /* relative address (related to exc. vector) of the word
  210.      * where handler's address is stored
  211.     */
  212.     volatile uint32_t handler_address_ptr = EXC_VECTORS_SIZE - PREFETCH_OFFSET;
  213.    
  214.     /* make it LDR instruction and store at exception vector */
  215.     *vector = handler_address_ptr | LDR_OPCODE;
  216.    
  217.     /* store handler's address */
  218.     *(vector + EXC_VECTORS) = handler_addr;
  219.  
  220. }
  221.  
  222.  
  223. /** Low-level Reset Exception handler. */
  224. static void reset_exception_entry()
  225. {
  226.     PROCESS_EXCEPTION(EXC_RESET);
  227. }
  228.  
  229.  
  230. /** Low-level Software Interrupt Exception handler. */
  231. static void swi_exception_entry()
  232. {
  233.     PROCESS_EXCEPTION(EXC_SWI);
  234. }
  235.  
  236.  
  237. /** Low-level Undefined Instruction Exception handler. */
  238. static void undef_instr_exception_entry()
  239. {
  240.     PROCESS_EXCEPTION(EXC_UNDEF_INSTR);
  241. }
  242.  
  243.  
  244. /** Low-level Fast Interrupt Exception handler. */
  245. static void fiq_exception_entry()
  246. {
  247.     PROCESS_EXCEPTION(EXC_FIQ);
  248. }
  249.  
  250.  
  251. /** Low-level Prefetch Abort Exception handler. */
  252. static void prefetch_abort_exception_entry()
  253. {
  254.     asm("sub lr, lr, #4");
  255.     PROCESS_EXCEPTION(EXC_PREFETCH_ABORT);
  256. }
  257.  
  258.  
  259. /** Low-level Data Abort Exception handler. */
  260. static void data_abort_exception_entry()
  261. {
  262.     asm("sub lr, lr, #8");
  263.     PROCESS_EXCEPTION(EXC_DATA_ABORT);
  264. }
  265.  
  266.  
  267. /** Low-level Interrupt Exception handler. */
  268. static void irq_exception_entry()
  269. {
  270.     asm("sub lr, lr, #4");
  271.     setup_stack_and_save_regs();
  272.  
  273.     /* switch to Undefined mode */
  274.     asm("stmfd sp!, {r0-r3}");
  275.     asm("mov r1, sp");
  276.     asm("mov r2, lr");
  277. //  asm("mrs r3, spsr");
  278.     asm("mrs r0, cpsr");
  279.     asm("bic r0, r0, #0x1f");
  280.     asm("orr r0, r0, #0x1b");
  281.     asm("msr cpsr_c, r0");
  282.     asm("mov sp, r1");
  283.     asm("mov lr, r2");
  284. //  asm("msr spsr, r3");
  285.     asm("ldmfd sp!, {r0-r3}");
  286.  
  287.     CALL_EXC_DISPATCH(EXC_IRQ)
  288.  
  289.     load_regs();
  290. }
  291.  
  292.  
  293. /** Software Interrupt handler.
  294.  *
  295.  * Dispatches the syscall.
  296.  */
  297. static void swi_exception(int exc_no, istate_t *istate)
  298. {
  299.     /*
  300.     dprintf("SYSCALL: r0-r4: %x, %x, %x, %x, %x; pc: %x\n", istate->r0,
  301.         istate->r1, istate->r2, istate->r3, istate->r4, istate->pc);
  302.     */
  303.  
  304.     istate->r0 = syscall_handler(
  305.         istate->r0,
  306.         istate->r1,
  307.         istate->r2,
  308.         istate->r3,
  309.         istate->r4);
  310. }
  311.  
  312.  
  313. /** Interrupt Exception handler.
  314.  *
  315.  * Determines the sources of interrupt, and calls their handlers.
  316.  */
  317. static void irq_exception(int exc_no, istate_t *istate)
  318. {
  319.     machine_irq_exception(exc_no, istate);
  320. }
  321.  
  322.  
  323. /** Fills exception vectors with appropriate exception handlers. */
  324. void install_exception_handlers(void)
  325. {
  326.     install_handler((unsigned)reset_exception_entry,
  327.              (unsigned*)EXC_RESET_VEC);
  328.    
  329.     install_handler((unsigned)undef_instr_exception_entry,
  330.              (unsigned*)EXC_UNDEF_INSTR_VEC);
  331.    
  332.     install_handler((unsigned)swi_exception_entry,
  333.              (unsigned*)EXC_SWI_VEC);
  334.    
  335.     install_handler((unsigned)prefetch_abort_exception_entry,
  336.              (unsigned*)EXC_PREFETCH_ABORT_VEC);
  337.    
  338.     install_handler((unsigned)data_abort_exception_entry,
  339.              (unsigned*)EXC_DATA_ABORT_VEC);
  340.    
  341.     install_handler((unsigned)irq_exception_entry,
  342.              (unsigned*)EXC_IRQ_VEC);
  343.    
  344.     install_handler((unsigned)fiq_exception_entry,
  345.              (unsigned*)EXC_FIQ_VEC);
  346. }
  347.  
  348.  
  349. #ifdef HIGH_EXCEPTION_VECTORS
  350. /** Activates use of high exception vectors addresses. */
  351. static void high_vectors()
  352. {
  353.     uint32_t control_reg;
  354.    
  355.     asm volatile( "mrc p15, 0, %0, c1, c1": "=r" (control_reg));
  356.    
  357.     //switch on the high vectors bit
  358.     control_reg |= CP15_R1_HIGH_VECTORS_BIT;
  359.    
  360.     asm volatile( "mcr p15, 0, %0, c1, c1" : : "r" (control_reg));
  361. }
  362. #endif
  363.  
  364.  
  365. /** Initializes exception handling.
  366.  *
  367.  * Installs low-level exception handlers and then registers
  368.  * exceptions and their handlers to kernel exception dispatcher.
  369.  */
  370. void exception_init(void)
  371. {
  372. #ifdef HIGH_EXCEPTION_VECTORS
  373.     high_vectors();
  374. #endif
  375.     install_exception_handlers();
  376.    
  377.     exc_register(EXC_IRQ, "interrupt", (iroutine) irq_exception);
  378.     exc_register(EXC_PREFETCH_ABORT, "prefetch abort", (iroutine) prefetch_abort);
  379.     exc_register(EXC_DATA_ABORT, "data abort", (iroutine) data_abort);
  380.     exc_register(EXC_SWI, "software interrupt", (iroutine) swi_exception);
  381.     /* TODO add next */
  382. }
  383.  
  384.  
  385. /** Sets stack pointers in all supported exception modes. */
  386. void setup_exception_stacks()
  387. {
  388.         /* switch to particular mode and set "r13" there */
  389.  
  390.         uint32_t cspr = current_status_reg_read();
  391.  
  392.         /* IRQ stack */
  393.         current_status_reg_control_write(
  394.                         (cspr & ~STATUS_REG_MODE_MASK) | IRQ_MODE
  395.         );
  396.         asm("ldr r13, =exc_stack");
  397.  
  398.         /* abort stack */
  399.         current_status_reg_control_write(
  400.                         (cspr & ~STATUS_REG_MODE_MASK) | ABORT_MODE
  401.         );
  402.         asm("ldr r13, =exc_stack");
  403.  
  404.         /* TODO if you want to test other exceptions than IRQ,
  405.         make stack analogous to irq_stack (in start.S),
  406.         and then set stack pointer here */
  407.  
  408.         current_status_reg_control_write(cspr);
  409. }
  410.  
  411.  
  412. /** Prints #istate_t structure content.
  413.  *
  414.  * @param istate Structure to be printed.
  415.  */
  416. void print_istate(istate_t *istate)
  417. {
  418.     dprintf("istate dump:\n");
  419.  
  420.     dprintf(" r0: %x    r1: %x    r2: %x    r3: %x\n",
  421.         istate->r0, istate->r1, istate->r2, istate->r3);
  422.     dprintf(" r4: %x    r5: %x    r6: %x    r7: %x\n",
  423.         istate->r4, istate->r5, istate->r6, istate->r7);
  424.     dprintf(" r8: %x    r8: %x   r10: %x   r11: %x\n",
  425.         istate->r8, istate->r9, istate->r10, istate->r11);
  426.     dprintf(" r12: %x    sp: %x    lr: %x  spsr: %x\n",
  427.         istate->r12, istate->sp, istate->lr, istate->spsr);
  428.  
  429.     dprintf(" pc: %x\n", istate->pc);
  430. }
  431.  
  432.  
  433. /** @}
  434.  */
  435.