shithub: p9-stm32-example-os

ref: f801657f77f3923ec2388c25bdcb036c8019ba89
dir: /trap.c/

View raw version
#include	<u.h>
#include	"mem.h"
#include	"dat.h"
#include	"fns.h"
#include	"thumb2.h"
#include	"include/ureg.h"
#include	"libkern/kern.h"
#include	"port/error.h"

#define print_regs(sp)																\
	print("\npc=0x%08ux\nsp=0x%08ux\n", *(int*)(sp + 60), sp);							\
	print("r0=0x%08ux\nr1=0x%08ux\nr2=0x%08ux\nr3=0x%08ux\n",							\
		*(int *)(sp + 36),*(int *)(sp + 40),*(int *)(sp + 44),*(int *)(sp + 48));	\
	print("r4=0x%08ux\nr5=0x%08ux\nr6=0x%08ux\nr7=0x%08ux\n",							\
		*(int *)(sp),*(int *)(sp + 4),*(int *)(sp + 8),*(int *)(sp + 12));			\
	print("r8=0x%08ux\nr9=0x%08ux\nr10=0x%08ux\nr11=0x%08ux\n",							\
		*(int *)(sp + 16),*(int *)(sp + 20),*(int *)(sp + 24),*(int *)(sp + 28));	\
	print("r12=0x%08ux\nlr=0x%08ux\n",												\
		*(int *)(sp + 32),*(int *)(sp + 56));

void
trapinit()
{
   /* Enable the usage fault exception. */
    *(int *)SHCSR_ADDR |= SHCSR_USGFAULTENA;

    /* Enable division by zero trapping. */
    *(int *)CCR_ADDR |= CCR_DIV_0_TRP | CCR_UNALIGN_TRP;
}

void
switcher(Ureg *ureg)
{
    int t;

	if (up) up->pc = ureg->pc;
    t = m->ticks;       /* CPU time per proc */
    up = nil;           /* no process at interrupt level */
    hzclock(ureg);

    m->inidle = 0;
    up = m->proc;
    /* The number of clock ticks is partly used to determine preemption. */
    preemption(m->ticks - t);
    m->intr++;
    m->inidle = 0;
    splhi();
}

void
linkproc(void)
{
	spllo();
	(*up->kpfun)(up->arg);
	pexit("end proc", 1);
}

void
kprocchild(Proc *p, void (*func)(void*), void *arg)
{
    p->sched.pc = (ulong)linkproc;
    p->sched.sp = (ulong)p->kstack+KSTACK-8;

    p->kpfun = func;
    p->arg = arg;
}

/*	fault handlers	*/
void
dummy_panic()
{
	panic("dummy panic\n");
}

void
hard_fault_handler(int sp)
{
	print_regs(sp)
	allocdump();
	panic("hard fault\n");
}

void
bus_fault_handler(int sp)
{
	print_regs(sp)
	allocdump();
	panic("bus fault\n");
}

void
usage_fault_handler(int sp)
{
	print_regs(sp)
	allocdump();
	panic("usage fault\n");
}

void
mem_manage_handler(int sp)
{
	print_regs(sp)
	allocdump();
	panic("memory management\n");
}