1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2024-11-16 19:55:20 +02:00
iris/mips/interrupts.ccp

327 lines
9.3 KiB
Plaintext
Raw Normal View History

2009-05-11 01:28:12 +03:00
#pypp 0
2009-06-01 15:26:42 +03:00
// Iris: micro-kernel for a capability-based operating system.
2009-06-24 01:47:13 +03:00
// mips/interrupts.ccp: Functions called by mips/entry.S.
2009-06-01 15:26:42 +03:00
// Copyright 2009 Bas Wijnen <wijnen@debian.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-05-22 23:48:49 +03:00
#define ARCH
2009-06-01 15:26:42 +03:00
#include "../kernel.hh"
2009-05-11 01:28:12 +03:00
2009-07-21 13:17:52 +03:00
void arch_flush_cache ():
2010-08-24 00:55:51 +03:00
__asm__ volatile ("\t.set noreorder\n"
"\tlui $k0, 0x8000\n"
"\tori $k1, $k0, 0x4000 - 0x20\n"
"1:\tcache 0, 0($k0)\n"
"\tcache 1, 0($k0)\n"
"\tbne $k0, $k1, 1b\n"
"\taddiu $k0, $k0, 0x20\n"
"\t.set reorder\n")
2009-07-21 13:17:52 +03:00
2010-01-24 22:34:24 +02:00
static kThread *handle_exit ():
dbg_check ()
2009-12-26 15:17:06 +02:00
--dbg_code.h
2009-08-24 22:02:35 +03:00
// Set must_wait to false, so random threads are not set to waiting when the kernel invokes something (such as a dbg_cap).
must_wait = false
2009-07-20 01:23:45 +03:00
if !current || (current == &idle):
2009-07-05 11:52:44 +03:00
schedule ()
if !current:
current = &idle
if (current->flags & (Iris::Thread::RUNNING | Iris::Thread::WAITING)) != Iris::Thread::RUNNING:
2010-01-24 22:34:24 +02:00
panic (current->flags, "non-scheduled thread running")
2009-07-05 11:52:44 +03:00
if old_current == current:
2010-01-24 22:34:24 +02:00
return current
2009-07-21 13:17:52 +03:00
arch_flush_cache ()
2009-07-05 11:52:44 +03:00
if current != &idle:
if (kMemory *)asids[current->address_space->arch.asid] != current->address_space:
2009-07-05 11:52:44 +03:00
if asids[0]:
current->address_space->arch.asid = asids[0]
asids[0] = asids[asids[0]]
else:
static unsigned random = 1
current->address_space->arch.asid = random
// Overwrite used asid, so flush those values from tlb.
flush_tlb (random)
++random
if random >= 64:
random = 1
asids[current->address_space->arch.asid] = (unsigned)current->address_space
cp0_set (CP0_ENTRY_HI, current->address_space->arch.asid)
directory = current->address_space->arch.directory
if current->flags & Iris::Thread::PRIV:
2009-07-05 11:52:44 +03:00
cp0_set (CP0_STATUS, 0x1000ff13)
else:
cp0_set (CP0_STATUS, 0x0000ff13)
dbg_push ((unsigned)current)
dbg_push (current->pc)
2010-01-24 22:34:24 +02:00
return current
2009-07-05 11:52:44 +03:00
/// A TLB miss has occurred. This is the slow version. It is only used
/// when k0 or k1 is not 0, or when an error occurs.
/// Otherwise, the ultra-fast code in entry.S is used.
kThread *tlb_refill ():
2009-12-26 15:17:06 +02:00
++dbg_code.h
2009-07-24 15:25:53 +03:00
old_current = current
if !directory:
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_NO_PAGE_DIRECTORY, addr)
2010-01-24 22:34:24 +02:00
return handle_exit ()
2009-05-23 21:55:31 +03:00
unsigned EntryHi
cp0_get (CP0_ENTRY_HI, EntryHi)
2010-01-17 11:01:42 +02:00
Table *t = directory[EntryHi >> 21]
if !t:
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_NO_PAGE_TABLE, addr)
2009-07-24 15:25:53 +03:00
else:
// - 2 instead of - 1 means reset bit 0
unsigned idx = (EntryHi >> 12) & ((1 << 9) - 2)
2010-01-17 11:01:42 +02:00
cp0_set (CP0_ENTRY_LO0, t->entrylo[idx])
cp0_set (CP0_ENTRY_LO1, t->entrylo[idx + 1])
2009-07-24 15:25:53 +03:00
__asm__ volatile ("tlbwr")
2010-01-24 22:34:24 +02:00
if dbg_code.l:
kdebug ("tlb refill ")
__asm__ volatile ("tlbp")
unsigned index
cp0_get (CP0_INDEX, index)
kdebug_num (index, 2)
kdebug ("|")
kdebug_num (t->entrylo[idx])
kdebug (":")
kdebug_num (t->entrylo[idx + 1])
kdebug (" for ")
kdebug_num (EntryHi)
kdebug ("\n")
return handle_exit ()
2009-05-11 01:28:12 +03:00
/// An interrupt which is not an exception has occurred.
kThread *interrupt ():
2009-12-26 15:17:06 +02:00
++dbg_code.h
2009-07-24 15:25:53 +03:00
old_current = current
2009-06-26 00:09:42 +03:00
unsigned ipr = INTC_IPR
2009-07-04 17:21:28 +03:00
for unsigned i = 0; i < 32; ++i:
2009-06-26 00:09:42 +03:00
if ipr & (1 << i):
2009-07-04 17:21:28 +03:00
// Handle timer interrupts specially: don't disable them.
if i == TIMER_INTERRUPT:
2009-07-04 17:21:28 +03:00
continue
2010-06-07 00:03:25 +03:00
//if i != 0x18:
//kdebug ("interrupt: ")
//kdebug_num (i, 2)
//kdebug ("\n")
2009-05-27 19:33:05 +03:00
// Disable the interrupt while handling it.
2009-06-26 00:09:42 +03:00
intc_mask_irq (i)
2009-07-04 17:21:28 +03:00
intc_ack_irq (i)
2009-05-27 19:33:05 +03:00
// Send message to interrupt handler.
if arch_interrupt_receiver[i]:
kCapability::Context c
2012-09-26 20:03:36 +03:00
c.data[0] = i
c.data[1] = 0
arch_interrupt_receiver[i]->send_message (0, &c)
2009-07-23 13:06:32 +03:00
arch_interrupt_receiver[i] = NULL
if ipr & (1 << TIMER_INTERRUPT):
#if defined (TRENDTAC)
2009-06-26 00:09:42 +03:00
ost_clear_uf (0)
#elif defined (NANONOTE)
tcu_clear_full_match_flag (0)
#else
#error unknown board
#endif
intc_ack_irq (TIMER_INTERRUPT)
2009-06-26 00:09:42 +03:00
timer_interrupt ()
2010-01-24 22:34:24 +02:00
return handle_exit ()
2009-05-11 01:28:12 +03:00
2009-06-26 00:09:42 +03:00
void flush_tlb (unsigned asid):
for unsigned tlb = 1; tlb < 32; ++tlb:
cp0_set (CP0_INDEX, tlb)
__asm__ volatile ("tlbr")
unsigned hi
cp0_get (CP0_ENTRY_HI, hi)
if (hi & 0x1f) == asid:
// Set asid to 0, which is only used by the idle task.
cp0_set (CP0_ENTRY_HI, 0x2000 * tlb)
__asm__ volatile ("tlbwi")
static void arch_invoke ():
2009-10-10 02:31:10 +03:00
kCapRef target = old_current->find_capability (old_current->arch.v[0], &must_wait)
2009-07-20 01:23:45 +03:00
do_schedule = false
kCapability::Context msg
2009-08-24 22:02:35 +03:00
if must_wait:
2009-09-06 12:04:09 +03:00
old_current->recv_reply = old_current->arch.t[2]
old_current->recv_arg = old_current->arch.t[3]
2010-01-24 22:34:24 +02:00
//kdebug_num (old_current->recv_reply)
//kdebug ("/")
//kdebug_num (old_current->recv_arg)
//kdebug ("\n")
2009-08-24 22:02:35 +03:00
if !target.valid ():
if must_wait:
old_current->wait ()
2010-01-24 22:34:24 +02:00
else:
dpanic (target.index, "invalid target called")
return
2009-10-10 02:31:10 +03:00
msg.reply = old_current->find_capability (old_current->arch.t[0], &msg.copy[0])
msg.arg = old_current->find_capability (old_current->arch.t[1], &msg.copy[1])
msg.data[0] = Iris::Num (old_current->arch.a[0], old_current->arch.a[1])
msg.data[1] = Iris::Num (old_current->arch.a[2], old_current->arch.a[3])
target->invoke (&msg)
2009-08-24 22:02:35 +03:00
if do_schedule && !must_wait:
// If the call was to schedule without wait, it isn't done yet.
schedule ()
else if old_current != current && old_current && (old_current->flags & (Iris::Thread::RUNNING | Iris::Thread::WAITING)) == Iris::Thread::RUNNING:
// If the caller received an immediate reply from the kernel, it is no longer set as current. Don't let it lose its timeslice.
current = old_current
2009-06-26 00:09:42 +03:00
2009-05-11 01:28:12 +03:00
/// A general exception has occurred.
kThread *exception ():
2009-12-26 15:17:06 +02:00
++dbg_code.h
2009-07-24 15:25:53 +03:00
old_current = current
2009-05-20 23:07:56 +03:00
unsigned cause
2009-05-23 21:55:31 +03:00
cp0_get (CP0_CAUSE, cause)
2009-05-20 23:07:56 +03:00
switch (cause >> 2) & 0x1f:
case 0:
2009-05-25 01:31:35 +03:00
// Interrupt. This shouldn't happen, since CAUSE[IV] == 1.
2009-07-25 01:54:12 +03:00
panic (0, "Interrupt on exception vector.")
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 1:
// TLB modification.
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_WRITE_DENIED, addr)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 2:
// TLB load or instruction fetch.
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_UNMAPPED_READ, addr)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 3:
// TLB store.
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_UNMAPPED_WRITE, addr)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 4:
// Address error load or instruction fetch.
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_INVALID_ADDRESS_READ, addr)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 5:
// Address error store.
2009-07-25 01:54:12 +03:00
unsigned addr
cp0_get (CP0_BAD_V_ADDR, addr)
current->raise (Iris::ERR_INVALID_ADDRESS_WRITE, addr)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 6:
// Bus error instruction fetch.
2009-07-25 01:54:12 +03:00
panic (0, "Bus error instruction fetch.")
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 7:
// Bus error load or store.
2009-07-25 01:54:12 +03:00
panic (0, "Bus error load or store.")
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 8:
// Syscall.
2009-06-08 14:46:13 +03:00
current->pc += 4
2009-05-23 21:55:31 +03:00
arch_invoke ()
2009-08-24 22:02:35 +03:00
//check (0x88392883, "check error")
2009-05-27 15:38:52 +03:00
break
2009-05-20 23:07:56 +03:00
case 9:
// Breakpoint.
2009-08-24 22:02:35 +03:00
#if 0 || defined (NDEBUG)
//current->raise (Iris::ERR_BREAKPOINT, 0)
2009-08-24 22:02:35 +03:00
#ifndef NDEBUG
current->pc += 4
#endif
2009-07-25 01:54:12 +03:00
#else
2010-06-07 00:03:25 +03:00
unsigned opcode = *(unsigned *)current->pc
if opcode == 0x0007000d:
panic (0, "Division by zero (detected by compiler)")
2009-07-20 01:23:45 +03:00
current->pc += 4
2009-08-24 22:02:35 +03:00
if current->arch.a[0]:
if dbg_cap.valid ():
dpanic (0, "Break instruction while log capability was already set")
2009-07-24 15:25:53 +03:00
break
2009-07-23 13:06:32 +03:00
bool dummy
2009-08-24 22:02:35 +03:00
dbg_cap = current->find_capability (current->arch.a[1], &dummy)
if !dbg_cap.valid ():
dpanic (0, "no log capability provided")
2009-07-24 15:25:53 +03:00
break
2010-01-24 22:34:24 +02:00
kdebug ("log capability registered.\n")
2009-07-23 13:06:32 +03:00
break
2010-01-24 22:34:24 +02:00
kdebug (current->arch.a[1])
2009-07-25 01:54:12 +03:00
#endif
2009-08-24 22:02:35 +03:00
break
2009-05-20 23:07:56 +03:00
case 10:
// Reserved instruction.
current->raise (Iris::ERR_RESERVED_INSTRUCTION, 0)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 11:
// Coprocessor unusable.
current->raise (Iris::ERR_COPROCESSOR_UNUSABLE, 0)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 12:
// Arithmetic overflow.
current->raise (Iris::ERR_OVERFLOW, 0)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 13:
// Trap.
current->raise (Iris::ERR_TRAP, 0)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 15:
// Floating point exception.
2009-07-21 13:17:52 +03:00
panic (0xe1223344, "Floating point exception.")
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 23:
// Reference to WatchHi/WatchLo address.
2009-12-18 10:00:38 +02:00
cp0_set0 (CP0_WATCH_LO)
current->raise (Iris::ERR_WATCHPOINT, 0)
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 24:
// Machine check.
2009-05-22 23:48:49 +03:00
panic (0xf3223344, "Machine check.")
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 30:
// Cache error (EJTAG only).
2009-05-22 23:48:49 +03:00
panic (0xf4223344, "Cache error (EJTAG only).")
2009-07-24 15:25:53 +03:00
break
2009-05-20 23:07:56 +03:00
case 14:
case 16:
case 17:
case 18:
case 19:
case 20:
case 21:
case 22:
case 25:
case 26:
case 27:
case 28:
case 29:
case 31:
// Reserved.
2009-05-23 21:55:31 +03:00
panic (0xf5223344, "Reserved exception code")
2009-07-24 15:25:53 +03:00
break
2009-05-23 21:55:31 +03:00
default:
panic (0xf6223344, "Impossible exception code")
2009-07-24 15:25:53 +03:00
break
2010-01-24 22:34:24 +02:00
return handle_exit ()
2009-05-11 01:28:12 +03:00
/// There's a cache error. Big trouble. Probably not worth trying to recover.
kThread *cache_error ():
2009-12-26 15:17:06 +02:00
++dbg_code.h
2009-05-11 01:28:12 +03:00
panic (0x33333333, "cache error")
2009-07-24 15:25:53 +03:00
old_current = current
2010-01-24 22:34:24 +02:00
return handle_exit ()