[258] | 1 | /************************************************************************************ |
---|
[742] | 2 | * This file contains the two entry points in the GIET_VM code: |
---|
| 3 | * - the _init entry point (from the boot code) is 0x80000000 |
---|
| 4 | * - the _giet entry point (from user applications) is 0x80000180 |
---|
| 5 | * => The base address of the segment containing this code MUST be 0x80000000. |
---|
[258] | 6 | * |
---|
[742] | 7 | * The _giet uses two arrays of functions: |
---|
[258] | 8 | * - the _cause_vector[16] array defines the 16 causes to enter the GIET |
---|
| 9 | * it is initialized in th exc_handler.c file |
---|
[267] | 10 | * - the _syscall_vector[64] array defines the 64 system calls entry points |
---|
[258] | 11 | * it is initialised in the sys_handler.c file |
---|
| 12 | ***********************************************************************************/ |
---|
| 13 | |
---|
| 14 | .section .giet, "ax", @progbits |
---|
| 15 | |
---|
| 16 | /* |
---|
[742] | 17 | * INIT entry point (at address 0x80000000) |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | .func _init |
---|
| 21 | .type _init, %function |
---|
| 22 | |
---|
| 23 | _init: |
---|
| 24 | la $26, _kernel_init |
---|
| 25 | jr $26 /* jump to _kernel_init */ |
---|
| 26 | |
---|
| 27 | .endfunc |
---|
| 28 | .size _init, .-_init |
---|
| 29 | |
---|
| 30 | .space 0x170 /* the _entry function occupies 16 bytes */ |
---|
| 31 | |
---|
| 32 | /* |
---|
[258] | 33 | * GIET Entry point (at address 0x80000180) |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | .func _giet |
---|
| 37 | .type _giet, %function |
---|
| 38 | |
---|
| 39 | _giet: |
---|
| 40 | mfc0 $27, $13 /* $27 <= Cause register */ |
---|
[798] | 41 | andi $27, $27, 0x3c /* $27 <= XCODE*4 */ |
---|
| 42 | beq $27, $0, do_jump /* jump if interrupt */ |
---|
| 43 | addiu $26, $0, 0x20 |
---|
| 44 | beq $27, $26, do_jump /* jump if syscall */ |
---|
| 45 | /* Other exception: saving registers for future display */ |
---|
| 46 | addiu $27, $29, -38*4 |
---|
| 47 | sw $29, (29*4)($27) |
---|
| 48 | or $29, $27, $0 |
---|
| 49 | |
---|
| 50 | .set noat |
---|
| 51 | sw $1, (1*4)($29) |
---|
| 52 | .set at |
---|
| 53 | sw $2, (2*4)($29) |
---|
| 54 | sw $3, (3*4)($29) |
---|
| 55 | sw $4, (4*4)($29) |
---|
| 56 | sw $5, (5*4)($29) |
---|
| 57 | sw $6, (6*4)($29) |
---|
| 58 | sw $7, (7*4)($29) |
---|
| 59 | sw $8, (8*4)($29) |
---|
| 60 | sw $9, (9*4)($29) |
---|
| 61 | sw $10, (10*4)($29) |
---|
| 62 | sw $11, (11*4)($29) |
---|
| 63 | sw $12, (12*4)($29) |
---|
| 64 | sw $13, (13*4)($29) |
---|
| 65 | sw $14, (14*4)($29) |
---|
| 66 | sw $15, (15*4)($29) |
---|
| 67 | sw $24, (24*4)($29) |
---|
| 68 | sw $25, (25*4)($29) |
---|
| 69 | sw $16, (16*4)($29) |
---|
| 70 | mfc0 $16, $14 /* Read EPC */ |
---|
| 71 | sw $17, (17*4)($29) |
---|
| 72 | mfc0 $17, $13 /* read CR (used later) */ |
---|
| 73 | sw $18, (18*4)($29) |
---|
| 74 | mfc0 $18, $12 /* Read current SR (used later) */ |
---|
| 75 | sw $19, (19*4)($29) |
---|
| 76 | sw $20, (20*4)($29) |
---|
| 77 | sw $21, (21*4)($29) |
---|
| 78 | sw $22, (22*4)($29) |
---|
| 79 | sw $23, (23*4)($29) |
---|
| 80 | sw $30, (30*4)($29) |
---|
| 81 | sw $28, (28*4)($29) |
---|
| 82 | mflo $14 /* read LO */ |
---|
| 83 | mfhi $15 /* read HI */ |
---|
| 84 | sw $31, (31*4)($29) /* save RA */ |
---|
| 85 | sw $16, (32*4)($29) /* Save EPC */ |
---|
| 86 | sw $17, (33*4)($29) /* Save CR */ |
---|
| 87 | sw $18, (34*4)($29) /* Save SR */ |
---|
| 88 | sw $14, (35*4)($29) /* save LO */ |
---|
| 89 | sw $15, (36*4)($29) /* save HI */ |
---|
| 90 | or $4, $0, $27 |
---|
| 91 | |
---|
| 92 | |
---|
| 93 | do_jump: |
---|
| 94 | mfc0 $27, $13 /* $27 <= Cause register */ |
---|
| 95 | andi $27, $27, 0x3c /* $27 <= XCODE*4 */ |
---|
| 96 | la $26, _cause_vector /* $26 <= _cause_vector */ |
---|
| 97 | addu $26, $26, $27 /* $26 <= &_cause_vector[XCODE] */ |
---|
| 98 | lw $26, 0($26) /* $26 <= _cause_vector[XCODE] */ |
---|
| 99 | jr $26 /* Jump to handler indexed by XCODE */ |
---|
[258] | 100 | |
---|
| 101 | .endfunc |
---|
| 102 | .size _giet, .-_giet |
---|
| 103 | |
---|
| 104 | /* |
---|
[742] | 105 | * *** Syscall Handler *** |
---|
[258] | 106 | * |
---|
| 107 | * A system call is handled as a special function call. |
---|
| 108 | * - $2 contains the system call index (< 64). |
---|
| 109 | * - $3 is used to store the syscall address |
---|
| 110 | * - $4, $5, $6, $7 contain the arguments values. |
---|
[346] | 111 | * - The return address (EPC) and the (SR) are saved in the stack. |
---|
[258] | 112 | * - Interrupts are enabled before branching to the syscall. |
---|
| 113 | * - All syscalls must return to the syscall handler. |
---|
| 114 | * - $2, $3, $4, $5, $6, $7 as well as $26 & $27 can be modified. |
---|
| 115 | * |
---|
[346] | 116 | * In case of undefined system call, an error message displays |
---|
| 117 | * the value of EPC on the kernel TTY, and the user program is killed. |
---|
[258] | 118 | */ |
---|
| 119 | |
---|
| 120 | .globl _sys_handler |
---|
| 121 | .func _sys_handler |
---|
| 122 | .type _sys_handler, %function |
---|
| 123 | |
---|
| 124 | _sys_handler: |
---|
[346] | 125 | addiu $29, $29, -24 /* 2 slots for SR & EPC, 4 slots for args */ |
---|
| 126 | mfc0 $27, $14 /* $27 <= EPC */ |
---|
| 127 | addiu $27, $27, 4 /* increment EPC for return address */ |
---|
| 128 | sw $27, 20($29) /* save EPC in the stack */ |
---|
| 129 | mfc0 $27, $12 /* $27 <= SR */ |
---|
| 130 | sw $27, 16($29) /* save SR in the stack */ |
---|
[258] | 131 | |
---|
[346] | 132 | andi $26, $2, 0x3F /* $26 <= syscall index (i < 64) */ |
---|
| 133 | sll $26, $26, 2 /* $26 <= index * 4 */ |
---|
| 134 | la $27, _syscall_vector /* $27 <= &_syscall_vector[0] */ |
---|
| 135 | addu $27, $27, $26 /* $27 <= &_syscall_vector[i] */ |
---|
| 136 | lw $3, 0($27) /* $3 <= syscall address */ |
---|
| 137 | li $27, 0xFFFFFFED /* Mask for UM & EXL bits */ |
---|
| 138 | mfc0 $26, $12 /* $26 <= SR */ |
---|
| 139 | and $26, $26, $27 /* UM = 0 / EXL = 0 */ |
---|
| 140 | mtc0 $26, $12 /* interrupt enabled */ |
---|
| 141 | jalr $3 /* jump to the proper syscall */ |
---|
[258] | 142 | |
---|
[346] | 143 | mtc0 $0, $12 /* SR <= 0 : interrupt disabled */ |
---|
| 144 | lw $26, 16($29) /* $26 <= SR from stack */ |
---|
| 145 | mtc0 $26, $12 /* restore SR */ |
---|
| 146 | lw $26, 20($29) /* $26 <= EPC from stack */ |
---|
| 147 | mtc0 $26, $14 /* restore EPC */ |
---|
| 148 | addiu $29, $29, 24 /* restore stack pointer */ |
---|
| 149 | eret /* exit GIET */ |
---|
[258] | 150 | |
---|
| 151 | .endfunc |
---|
| 152 | .size _sys_handler, .-_sys_handler |
---|
| 153 | |
---|
| 154 | /* |
---|
| 155 | * *** Interrupt Handler *** |
---|
| 156 | * |
---|
| 157 | * This simple interrupt handler cannot be interrupted. |
---|
| 158 | * |
---|
| 159 | * All non persistant registers, such as $1 to $15, and $24 to $25, as well as |
---|
| 160 | * register $31 and EPC, are saved in the interrupted program stack, before |
---|
| 161 | * calling the Interrupt Service Routine. These registers can be used by the |
---|
| 162 | * ISR code. |
---|
| 163 | */ |
---|
| 164 | |
---|
| 165 | .globl _int_handler |
---|
| 166 | .func _int_handler |
---|
| 167 | .type _int_handler, %function |
---|
| 168 | |
---|
| 169 | _int_handler: |
---|
| 170 | addiu $29, $29, -25*4 /* stack space reservation (21 registers to |
---|
| 171 | save and 4 free words to call function) */ |
---|
| 172 | .set noat |
---|
| 173 | sw $1, 4*4($29) /* save $1 */ |
---|
| 174 | .set at |
---|
| 175 | sw $2, 5*4($29) /* save $2 */ |
---|
| 176 | sw $3, 6*4($29) /* save $3 */ |
---|
| 177 | sw $4, 7*4($29) /* save $4 */ |
---|
| 178 | sw $5, 8*4($29) /* save $5 */ |
---|
| 179 | sw $6, 9*4($29) /* save $6 */ |
---|
| 180 | sw $7, 10*4($29) /* save $7 */ |
---|
| 181 | sw $8, 11*4($29) /* save $8 */ |
---|
| 182 | sw $9, 12*4($29) /* save $9 */ |
---|
| 183 | sw $10, 13*4($29) /* save $10 */ |
---|
| 184 | sw $11, 14*4($29) /* save $11 */ |
---|
| 185 | sw $12, 15*4($29) /* save $12 */ |
---|
| 186 | sw $13, 16*4($29) /* save $13 */ |
---|
| 187 | sw $14, 17*4($29) /* save $14 */ |
---|
| 188 | sw $15, 18*4($29) /* save $15 */ |
---|
| 189 | sw $24, 19*4($29) /* save $24 */ |
---|
| 190 | sw $25, 20*4($29) /* save $25 */ |
---|
| 191 | sw $31, 21*4($29) /* save $31 */ |
---|
| 192 | mflo $26 |
---|
| 193 | sw $26, 22*4($29) /* save LO */ |
---|
| 194 | mfhi $26 |
---|
| 195 | sw $26, 23*4($29) /* save HI */ |
---|
| 196 | mfc0 $27, $14 |
---|
| 197 | sw $27, 24*4($29) /* save EPC */ |
---|
| 198 | |
---|
| 199 | la $26, _irq_demux |
---|
| 200 | jalr $26 /* jump to a C function to find the proper ISR */ |
---|
| 201 | |
---|
| 202 | restore: |
---|
| 203 | .set noat |
---|
| 204 | lw $1, 4*4($29) /* restore $1 */ |
---|
| 205 | .set at |
---|
| 206 | lw $2, 4*5($29) /* restore $2 */ |
---|
| 207 | lw $3, 4*6($29) /* restore $3 */ |
---|
| 208 | lw $4, 4*7($29) /* restore $4 */ |
---|
| 209 | lw $5, 4*8($29) /* restore $5 */ |
---|
| 210 | lw $6, 4*9($29) /* restore $6 */ |
---|
| 211 | lw $7, 4*10($29) /* restore $7 */ |
---|
| 212 | lw $8, 4*11($29) /* restore $8 */ |
---|
| 213 | lw $9, 4*12($29) /* restore $9 */ |
---|
| 214 | lw $10, 4*13($29) /* restore $10 */ |
---|
| 215 | lw $11, 4*14($29) /* restore $11 */ |
---|
| 216 | lw $12, 4*15($29) /* restore $12 */ |
---|
| 217 | lw $13, 4*16($29) /* restore $13 */ |
---|
| 218 | lw $14, 4*17($29) /* restore $14 */ |
---|
| 219 | lw $15, 4*18($29) /* restore $15 */ |
---|
| 220 | lw $24, 4*19($29) /* restore $24 */ |
---|
| 221 | lw $25, 4*20($29) /* restore $25 */ |
---|
| 222 | lw $31, 4*21($29) /* restore $31 */ |
---|
| 223 | lw $26, 4*22($29) |
---|
| 224 | mtlo $26 /* restore LO */ |
---|
| 225 | lw $26, 4*23($29) |
---|
| 226 | mthi $26 /* restore HI */ |
---|
| 227 | lw $27, 4*24($29) /* return address (EPC) */ |
---|
| 228 | addiu $29, $29, 25*4 /* restore stack pointer */ |
---|
| 229 | mtc0 $27, $14 /* restore EPC */ |
---|
| 230 | eret /* exit GIET */ |
---|
| 231 | |
---|
| 232 | .endfunc |
---|
| 233 | .size _int_handler, .-_int_handler |
---|
| 234 | |
---|