1 /* 2 * Interrupt and TLB miss handling support. 3 * 4 * Copyright (C) 2015, 2016 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 .text 21 .extern interrupt_handler 22 .extern current_registers 23 .extern current_stack_pointer 24 .globl _tlb_entry 25 .globl _exc_entry 26 .globl _irq_entry 27 .globl _end_entries 28 .globl _enter_task 29 .set noreorder 30 31 #include "paging.h" 32 33 _tlb_entry: 34 /* Get the bad address. */ 35 36 mfc0 $k0, $10 /* CP0_ENTRYHI */ 37 nop 38 andi $k1, $k0, 0xff /* ASID */ 39 40 /* For ASID == 0... */ 41 42 beqz $k1, _tlb_entry_direct 43 nop 44 45 /* For addresses over 0x00080000... */ 46 47 li $k1, 0xfff80000 48 and $k1, $k0, $k1 49 bnez $k1, _tlb_entry_direct 50 nop 51 52 /* Otherwise, load the page table entries. */ 53 54 andi $k1, $k0, 0xff /* ASID */ 55 li $k0, page_table_task_size 56 mul $k0, $k0, $k1 /* [ASID] (ASID * page_table_task_size) */ 57 li $k1, page_table_start /* page_table */ 58 addu $k1, $k0, $k1 /* page_table[ASID] */ 59 60 mfc0 $k0, $4 /* CP0_CONTEXT */ 61 nop 62 srl $k0, $k0, 1 /* use 8 byte - not 16 byte - entries */ 63 addu $k0, $k0, $k1 /* page_table[ASID][entry] */ 64 65 lw $k1, 0($k0) /* page_table[ASID][entry][0] */ 66 mtc0 $k1, $2 /* CP0_ENTRYLO0 */ 67 68 lw $k1, 4($k0) /* page_table[ASID][entry][1] */ 69 mtc0 $k1, $3 /* CP0_ENTRYLO1 */ 70 /* page size is 4KB */ 71 mtc0 $zero, $5 /* CP0_PAGEMASK */ 72 nop 73 74 tlbwr 75 nop 76 77 j _tlb_exit 78 nop 79 80 _tlb_entry_direct: 81 /* Otherwise, just translate the address directly. */ 82 83 li $k1, 0xffffe000 84 and $k0, $k0, $k1 /* VPN2 (8KB resolution) */ 85 srl $k0, $k0, 6 /* PFN (maintain 8KB resolution, bit 6 remaining zero) */ 86 ori $k0, $k0, 0x1e /* flags */ 87 88 mtc0 $k0, $2 /* CP0_ENTRYLO0 */ 89 ori $k0, $k0, 0x40 /* page size is 4KB (bit 6 set) */ 90 mtc0 $k0, $3 /* CP0_ENTRYLO1 */ 91 nop /* page size is 4KB */ 92 mtc0 $zero, $5 /* CP0_PAGEMASK */ 93 nop 94 95 tlbwr 96 nop 97 98 /* For ASID == 0... */ 99 100 andi $k1, $k0, 0xff /* ASID */ 101 bnez $k1, _tlb_exit 102 nop 103 eret 104 nop 105 106 _tlb_exit: 107 /* For ASID != 0... */ 108 109 lui $k0, %hi(_enter_task) 110 ori $k0, $k0, %lo(_enter_task) 111 lw $k1, 0($k0) 112 jr $k1 113 nop 114 115 _exc_entry: 116 /* Handle TLB refill exceptions. */ 117 118 mfc0 $k0, $13 /* CP0_CAUSE */ 119 li $k1, 0x0000007c 120 and $k0, $k0, $k1 /* ExcCode << 2 */ 121 srl $k0, $k0, 2 /* ExcCode */ 122 addi $k1, $k0, -2 /* ExcCode == 2 */ 123 beqz $k1, _tlb_entry 124 addi $k1, $k0, -3 /* ExcCode == 3 */ 125 beqz $k1, _tlb_entry 126 nop 127 _fail: 128 b _fail 129 nop 130 131 _irq_entry: 132 /* Set the ASID. */ 133 134 mtc0 $zero, $10 /* CP0_ENTRYHI */ 135 nop 136 137 /* Obtain the kernel global offset table. */ 138 139 move $k0, $gp 140 lui $gp, %hi(_GLOBAL_OFFSET_TABLE_) 141 ori $gp, $gp, %lo(_GLOBAL_OFFSET_TABLE_) 142 143 /* Obtain a store of registers for the current task. */ 144 145 la $k1, current_registers 146 lw $k1, 0($k1) 147 148 /* Save registers that the assembler wants to trash. */ 149 150 sw $t9, 100($k1) 151 sw $k0, 104($k1) /* old gp */ 152 sw $ra, 112($k1) 153 154 /* Record the stack pointer. */ 155 156 la $k1, current_stack_pointer 157 lw $k1, 0($k1) /* &stack_pointers[current_task] */ 158 sw $sp, 0($k1) 159 160 /* Switch to the kernel stack. */ 161 162 li $sp, 0x80070000 163 164 /* Invoke the rest of the interrupt handling process. */ 165 166 la $k0, interrupt_handler 167 jr $k0 168 nop 169 170 _end_entries: 171 172 .set reorder