@@ -11,6 +11,8 @@
#define CSR_STVAL 0x143
#define CSR_SATP 0x180
+#define SSTATUS_SIE (_AC(1, UL) << 1)
+
/* Exception cause high bit - is an interrupt if set */
#define CAUSE_IRQ_FLAG (_AC(1, UL) << (__riscv_xlen - 1))
@@ -5,6 +5,7 @@
#include <asm/ptrace.h>
#define EXCEPTION_CAUSE_MAX 16
+#define INTERRUPT_CAUSE_MAX 16
typedef void (*exception_fn)(struct pt_regs *);
@@ -13,6 +14,7 @@ struct thread_info {
unsigned long hartid;
unsigned long isa[1];
exception_fn exception_handlers[EXCEPTION_CAUSE_MAX];
+ exception_fn interrupt_handlers[INTERRUPT_CAUSE_MAX];
};
static inline struct thread_info *current_thread_info(void)
@@ -20,7 +22,18 @@ static inline struct thread_info *current_thread_info(void)
return (struct thread_info *)csr_read(CSR_SSCRATCH);
}
+static inline void local_irq_enable(void)
+{
+ csr_set(CSR_SSTATUS, SSTATUS_SIE);
+}
+
+static inline void local_irq_disable(void)
+{
+ csr_clear(CSR_SSTATUS, SSTATUS_SIE);
+}
+
void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *));
+void install_irq_handler(unsigned long cause, void (*handler)(struct pt_regs *));
void do_handle_exception(struct pt_regs *regs);
void thread_info_init(void);
@@ -36,10 +36,21 @@ void do_handle_exception(struct pt_regs *regs)
{
struct thread_info *info = current_thread_info();
- assert(regs->cause < EXCEPTION_CAUSE_MAX);
- if (info->exception_handlers[regs->cause]) {
- info->exception_handlers[regs->cause](regs);
- return;
+ if (regs->cause & CAUSE_IRQ_FLAG) {
+ unsigned long irq_cause = regs->cause & ~CAUSE_IRQ_FLAG;
+
+ assert(irq_cause < INTERRUPT_CAUSE_MAX);
+ if (info->interrupt_handlers[irq_cause]) {
+ info->interrupt_handlers[irq_cause](regs);
+ return;
+ }
+ } else {
+ assert(regs->cause < EXCEPTION_CAUSE_MAX);
+
+ if (info->exception_handlers[regs->cause]) {
+ info->exception_handlers[regs->cause](regs);
+ return;
+ }
}
show_regs(regs);
@@ -47,6 +58,14 @@ void do_handle_exception(struct pt_regs *regs)
abort();
}
+void install_irq_handler(unsigned long cause, void (*handler)(struct pt_regs *))
+{
+ struct thread_info *info = current_thread_info();
+
+ assert(cause < INTERRUPT_CAUSE_MAX);
+ info->interrupt_handlers[cause] = handler;
+}
+
void install_exception_handler(unsigned long cause, void (*handler)(struct pt_regs *))
{
struct thread_info *info = current_thread_info();