#include <test_helper.h>
#include <test_paging.h>
#include <aos/paging.h>

// ASSESSMENT M1: these lines show mapping to fixed locations and read/write

// used to avoid conflicts between tests
static lvaddr_t vaddr = 0x0000100000000000L;

static void test_paging_run(errval_t func(void), char *name) {
    errval_t err;

    // debug_printf("TEST_PAGING %19s: start\n", name);
    err = func();

    if (err_is_ok(err)) {
        debug_printf("TEST_PAGING %23s: OK\n", name);
    } else {
        debug_printf("TEST_PAGING %23s: ERR\n", name);
        USER_PANIC_ERR(err, "Test Failed with Error");
    }
}

static errval_t test_paging_full_l3(void) {
    errval_t err;

    size_t mapping_size = BASE_PAGE_SIZE;

    // map all entries of a single l3 table
    for(size_t i = 0; i < VMSAv8_64_PTABLE_NUM_ENTRIES; ++i) {
        struct capref frame;
        err = frame_alloc(&frame, mapping_size, NULL);
        if (err_is_fail(err)) {
            return err_push(err, LIB_ERR_FRAME_ALLOC);
        }
        err = paging_map_fixed_attr(get_current_paging_state(), vaddr, frame, mapping_size, VREGION_FLAGS_READ_WRITE);
        if (err_is_fail(err)) {
            return err_push(err, LIB_ERR_PMAP_NOT_MAPPED);
        }

        // verify mapping using a random value at a random location
        uint8_t val = rand() % 256;
        memset((void *) vaddr, val, mapping_size);
        if(*((uint8_t *) vaddr + (rand() % mapping_size)) != val) {
            return ERR_INVALID_ARGS;
        }

        vaddr += mapping_size;
    }

    return SYS_ERR_OK;
}

static errval_t test_paging_full_l2(void) {
    errval_t err;

    size_t mapping_size = BASE_PAGE_SIZE;

    // map a page in all slots of a single l2 node (the 0-th slot was already used in a previous test)
    for(size_t i = 1; i < VMSAv8_64_PTABLE_NUM_ENTRIES; ++i) {
        struct capref frame;
        err = frame_alloc(&frame, mapping_size, NULL);
        if (err_is_fail(err)) {
            return err_push(err, LIB_ERR_FRAME_ALLOC);
        }
        err = paging_map_fixed_attr(get_current_paging_state(), vaddr, frame, mapping_size, VREGION_FLAGS_READ_WRITE);
        if (err_is_fail(err)) {
            return err_push(err, LIB_ERR_PMAP_NOT_MAPPED);
        }

        // verify mapping using a random value at a random location
        uint8_t val = rand() % 256;
        memset((void *) vaddr, val, mapping_size);
        if(*((uint8_t *) vaddr + (rand() % mapping_size)) != val) {
            return ERR_INVALID_ARGS;
        }

        vaddr += LARGE_PAGE_SIZE;
    }

    return SYS_ERR_OK;
}

static errval_t test_paging_big_regions(void) {
    errval_t err;

    size_t mapping_size;

    // map larger areas
    for(size_t i = 1; i <= VMSAv8_64_PTABLE_NUM_ENTRIES - 1; ++i) {
        mapping_size = i * BASE_PAGE_SIZE;

        struct capref frame;
        err = frame_alloc(&frame, mapping_size, NULL);
        if (err_is_fail(err)) {
            return err_push(err, LIB_ERR_FRAME_ALLOC);
        }
        err = paging_map_fixed_attr(get_current_paging_state(), vaddr, frame, mapping_size, VREGION_FLAGS_READ_WRITE);
        if (err_is_fail(err)) {
            return err_push(err, LIB_ERR_PMAP_NOT_MAPPED);
        }

        // verify mapping using a random value at a random location
        uint8_t val = rand() % 256;
        memset((void *) vaddr, val, mapping_size);
        if(*((uint8_t *) vaddr + (rand() % mapping_size)) != val) {
            return ERR_INVALID_ARGS;
        }

        vaddr += LARGE_PAGE_SIZE;
    }

    return SYS_ERR_OK;
}

void do_test_paging(void) {
  test_paging_run(test_paging_full_l3, "test_paging_full_l3");
  test_paging_run(test_paging_full_l2, "test_paging_full_l2");
  test_paging_run(test_paging_big_regions, "test_paging_big_regions");

  // Test paging
  for (int i = 0; i < 40; i++) {
    debug_printf("TEST: page %lu\n", i);
    char *data = alloc_frame(5176*4096, 16, NULL, NULL);
    memset(data, 33, 5176*4096);
  }
}
