/*
 * Test various processor modes
 */

#include <types.h>
#include <sc/arm.h>
#include <sc/sc.h>
#include <ioslibc.h>
#include <sc/dv_boot.h>
#include "dv_cpu_local.h"

extern u32 modeDataAbortHandler;
extern u32 modeFetchAbortHandler;
extern u32 modeSWIHandler;
extern void modeUserLaunch(u32 jump, u32 tid);

/*
 * Stacks for the data abort handler and user mode
 */
u32 modeDataAbortStack[DABORT_STACK_SIZE>>2] __attribute__ ((aligned (16))) = {0xff};
u32 userStack[USER_STACK_SIZE>>2] __attribute__ ((aligned (16))) = {0xff};

/* The data abort exception handler will write this flag */
volatile u32 modeDataAbortFlag = -1;

/* The prefetch abort exception handler will write this flag */
volatile u32 modeFetchAbortFlag = -1;

volatile u32 junk[4] __attribute__ ((aligned (CACHE_LINE_SIZE)));

/*
 * Call a procedure in user mode.  Requires asm support, of course.
 */
void
runUserProc(u32 proc, u32 len, u32 tid)
{
    u32 pstart;
    u32 pend;
    int count;
    u32 *to;

    /*
     * Set RX permissions on the text of the procedure
     */
    pstart = ROUNDDOWN(proc, CACHE_LINE_SIZE);
    pend = ROUNDUP(proc+len, CACHE_LINE_SIZE);
    count = (pend - pstart)/CACHE_LINE_SIZE;
    to = (u32 *) (DAX_START + pstart);
    while (count-- > 0) {
        *to = (PT_ACCESS_EXEC << PT_SC_PROT_SHIFT) | tid;
        to += CACHE_LINE_SIZE/sizeof (*to);
    }
    /* Make sure new permissions get picked up */
    DL1Invalidate((void *)pstart, len);
    IL1Invalidate((void *)pstart, len);

    /*
     * Set RX permissions on the ASM routine that does the mode switch
     * This needs to be executable in USR mode (at least the last bit does)
     */
    to = (u32 *) ROUNDDOWN(DAX_START + (u32)modeUserLaunch, CACHE_LINE_SIZE);
    count = 6;	/* XXX Gag me with a spoon! */
    while (count-- > 0) {
        *to = (PT_ACCESS_EXEC << PT_SC_PROT_SHIFT) | SYS_SMP_TID_MASK;
        to += CACHE_LINE_SIZE/sizeof (*to);
    }

    /*
     * Set RW + tid on the user stack
     */
    count = USER_STACK_SIZE / CACHE_LINE_SIZE;
    to = (u32 *) (DAX_START + ((u32)userStack + USER_STACK_SIZE));
    to -= CACHE_LINE_SIZE/sizeof (*to);
    while (count-- > 0) {
        *to = (PT_ACCESS_RW << PT_SC_PROT_SHIFT) | tid;
        to -= CACHE_LINE_SIZE/sizeof (*to);
    }
    /* Make sure new permissions get picked up */
    DL1Invalidate((void *)userStack, USER_STACK_SIZE);

    (void) DVSetExcHandler(ARM_EXC_SWI, (u32) &modeSWIHandler);
    modeUserLaunch(proc, tid);
}

/*
 * Make sure user routines do not share cachelines with system mode code
 */
void
foo(void)
{
    __asm__("nop");
    __asm__("nop");
    __asm__("nop");
}

u32
readRegUser(void)
{
    u32 val;

    val = IO_READ(SYS_SMP_REG);
    val = IO_READ(SYS_CTRL_REG);
    val = IO_READ(PWR_CTRL_REG);
    return val;
}

u32
jumpBadUser(void)
{
    u32 val = -1;
    void (*fp)(void);

    /*
     * "junk" has RW permission with TID == 0, so this thread shouldn't
     * be able to read it or execute from it
     */
    val = junk[0];

    /*
     * Try to jump to data space with no exec permission,
     * which should generate a prefetch abort.  The exception
     * handler causes a return to the next instruction after
     * the fault.
     */
    fp = (void (*)(void))junk;
    fp();

    return val;
}

/*
 * Make sure user routines do not share cachelines with system mode code
 */
void
bar(void)
{
    __asm__("nop");
    __asm__("nop");
    __asm__("nop");
}

#define DELAY(n) do { \
    int delay = n; \
    while (delay-- > 0) ; \
} while (0)

volatile int dabort_count = 0;
volatile int iabort_count = 0;

int 
cpu0main(void)
{
    u32 val;
    int rv = 0;
    int i;

    //DELAY(1000000000);
    val = IO_READ(SYS_SMP_REG);

    modeDataAbortFlag = 0;
    (void) DVSetExcHandler(ARM_EXC_DATA_ABORT, (u32) &modeDataAbortHandler);

    /*
     * Now invoke the procedure to read registers in USR mode
     */
    runUserProc((u32)readRegUser, (u32)&bar - (u32)&readRegUser, 5);

    /*
     * A data abort should have been triggered
     */
    if (modeDataAbortFlag == 0)
        rv = -3;

    for (i = 0; i < sizeof(junk)/sizeof(junk[0]); i++) {
        junk[i] = i + 3;
    }

    u32 *to;
    /* Set the attributes on "junk" to be R/O, TID == 5 */
    val = (u32) &junk[0];
    to = (u32 *) (DAX_START + val);
    *to = (PT_ACCESS_READ << PT_SC_PROT_SHIFT) | 5;

    /* Invalidate the cache to make sure the new attributes are picked up */
    DL1Invalidate((void *)junk, CACHE_LINE_SIZE);
    IL1Invalidate((void *)junk, CACHE_LINE_SIZE);

    modeDataAbortFlag = 0;
    modeFetchAbortFlag = 0;
    (void) DVSetExcHandler(ARM_EXC_PREFETCH_ABORT, (u32) &modeFetchAbortHandler);

    /*
     * Now invoke the procedure to execute with bad permission in USR mode
     * and TID not matching ((x5 & 0x7a) == 0)
     */
    runUserProc((u32)jumpBadUser, (u32)&bar - (u32)&jumpBadUser, 0x7a);

    /*
     * A prefetch abort and data abort should have been triggered
     */
    if (modeFetchAbortFlag == 0){
        rv = -5;
    } else {
        iabort_count++;
    }
    if (modeDataAbortFlag == 0) {
        rv = -7;
    } else {
        dabort_count++;
    }
    
#if 1
    printf("usermode succeeded: total data aborts %d\n", dabort_count);
    printf("usermode succeeded: total prefetch aborts %d\n", iabort_count);
#endif
    return rv;
}

int 
cpu1main(void)
{
    return 0;
}
