/*
 * Copyright (c) 2008 Jiri Svoboda
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/** @addtogroup genericmm
 * @{
 */

/**
 * @file
 * @brief	Debugger access to adress spaces.
 *
 * This file contains functions allowing debugger access to the user
 * address space. It allows bypassing the access mode restrictions.
 *
 */

#include <mm/as_debug.h>
#include <mm/slab.h>
#include <mm/page.h>
#include <errno.h>
#include <synch/mutex.h>
#include <memstr.h>
#include <align.h>
#include <arch.h>
#include <mm/as.h>

/** Write directly into a page, bypassing area flags.
 *
 * This allows a debugger to write into a page that is mapped read-only
 * (such as the text segment). Naturally, this can only be done if the
 * correspoinding area is private (not shared) and anonymous.
 *
 * If this is not the case, this function calls as_area_make_writeable()
 * first.
 */
static int debug_write_inside_page(uintptr_t va, void *data, size_t n)
{
	uintptr_t page;
	pte_t *pte;
	as_area_t *area;
	uintptr_t frame;
	ipl_t ipl;
	int rc;

	page = ALIGN_DOWN(va, PAGE_SIZE);
	ASSERT(ALIGN_DOWN(va + n - 1, PAGE_SIZE) == page);

restart:
	mutex_lock(&AS->lock);
	ipl = interrupts_disable();
	area = find_area_and_lock(AS, page);
	if (area->backend != &anon_backend || area->sh_info != NULL) {
		mutex_unlock(&area->lock);
		mutex_unlock(&AS->lock);
		interrupts_restore(ipl);

		rc = as_area_make_writeable(area->base);
		if (rc != 0) return rc;

		goto restart;
	}

	pte = page_mapping_find(AS, page);
	if (! (pte && PTE_VALID(pte) && PTE_PRESENT(pte)) ) {
		mutex_unlock(&area->lock);
		mutex_unlock(&AS->lock);
		interrupts_restore(ipl);

		rc = as_page_fault(page, PF_ACCESS_WRITE, NULL);
		if (rc == AS_PF_FAULT) return EINVAL;

		goto restart;
	}

	frame = PTE_GET_FRAME(pte);
	memcpy((void *)(PA2KA(frame) + (va - page)), data, n);

	mutex_unlock(&area->lock);
	mutex_unlock(&AS->lock);
	interrupts_restore(ipl);

	return EOK;
}

/** Write data bypassing area flags.
 *
 * See debug_write_inside_page().
 */
int as_debug_write(uintptr_t va, void *data, size_t n)
{
	size_t now;
	int rc;

	while (n > 0) {
		/* Number of bytes until the end of page */
		now = ALIGN_DOWN(va, PAGE_SIZE) + PAGE_SIZE - va;
		if (now > n) now = n;

		rc = debug_write_inside_page(va, data, now);
		if (rc != EOK) return rc;

		va += now;
		data += now;
		n -= now;
	}

	return EOK;
}

/** Make sure area is private and anonymous.
 *
 * Not atomic atm.
 * @param address	Virtual address in AS.
 */
int as_area_make_writeable(uintptr_t address)
{
	ipl_t ipl;
	as_area_t *area;
	uintptr_t base, page;
	uintptr_t old_frame, frame;
	size_t size;
	int flags;
	int page_flags;
	pte_t *pte;
	int rc;
	uintptr_t *pagemap;

	ipl = interrupts_disable();
	mutex_lock(&AS->lock);
	area = find_area_and_lock(AS, address);
	if (!area) {
		/*
		 * Could not find the address space area.
		 */
		mutex_unlock(&AS->lock);
		interrupts_restore(ipl);
		return ENOENT;
	}

	if (area->backend == &anon_backend && !area->sh_info) {
		/* Nothing to do */
		mutex_unlock(&area->lock);
		mutex_unlock(&AS->lock);
		interrupts_restore(ipl);
		return EOK;
	}

	base = area->base;
	size = area->pages * PAGE_SIZE;
	flags = area->flags;
	page_flags = as_area_get_flags(area);

	pagemap = malloc(area->pages * sizeof(uintptr_t), 0);
	page_table_lock(AS, false);

	for (page = base; page < base + size; page += PAGE_SIZE) {
		pte = page_mapping_find(AS, page);
		if (!pte || !PTE_PRESENT(pte) || !PTE_READABLE(pte)) {
			/* Fetch the missing page */
			if (!area->backend || !area->backend->page_fault) {
				page_table_unlock(AS, false);
				mutex_unlock(&area->lock);
				mutex_unlock(&AS->lock);
				interrupts_restore(ipl);
				return EINVAL;
			}
			if (area->backend->page_fault(area, page, PF_ACCESS_READ) != AS_PF_OK) {
				page_table_unlock(AS, false);
				mutex_unlock(&area->lock);
				mutex_unlock(&AS->lock);
				interrupts_restore(ipl);
				return EINVAL;
			}
		}
		ASSERT(PTE_VALID(pte));

		old_frame = PTE_GET_FRAME(pte);

		frame = (uintptr_t)frame_alloc(ONE_FRAME, 0);
		memcpy((void *) PA2KA(frame), (void *)PA2KA(old_frame),
		    FRAME_SIZE);

		pagemap[(page - base) / PAGE_SIZE] = frame;
	}

	page_table_unlock(AS, false);
	mutex_unlock(&area->lock);
	mutex_unlock(&AS->lock);
	interrupts_restore(ipl);

	rc = as_area_destroy(AS, address);
	if (rc < 0) {
		free(pagemap);
		return rc;
	}

	area = as_area_create(AS, flags, size, base, AS_AREA_ATTR_PARTIAL,
	    &anon_backend, NULL);
	if (area == NULL) {
		free(pagemap);
		return rc;
	}

	mutex_lock(&AS->lock);
	mutex_lock(&area->lock);
	page_table_lock(AS, false);
	for (page = base; page < base + size; page += PAGE_SIZE) {
		frame = pagemap[(page - base) / PAGE_SIZE];

		page_mapping_insert(AS, page, frame, page_flags);
		if (!used_space_insert(area, page, 1))
			panic("Could not insert used space.\n");
	}

	page_table_unlock(AS, false);

	area->attributes &= ~AS_AREA_ATTR_PARTIAL;

	mutex_unlock(&area->lock);
	mutex_unlock(&AS->lock);

	free(pagemap);

	return EOK;
}

/** @}
 */
