/*
 * Copyright (c) 2009 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 debug
 * @{
 */
/** @file
 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <bool.h>
#include <udebug.h>

#include <kernel/arch/context_offset.h>

#include "../../../cons.h"
#include "../../../main.h"
#include "../../../include/arch.h"

#define OPCODE_INT3		0xCC

void arch_dthread_initialize(dthread_t *dt)
{
	dt->arch.singlestep = false;
}

static int _set_trap_flag(dthread_t *dt, bool enable)
{
	static istate_t istate;
	int rc;

	rc = udebug_regs_read(app_phone, dt->hash, &istate);
	if (rc < 0) { printf("regs read failed\n"); return -1; }

	if (enable) istate.rflags |= 0x0100; /* trap flag */
	else if (!active_bkpt) istate.rflags &= ~0x0100; /* trap flag */

	rc = udebug_regs_write(app_phone, dt->hash, &istate);	
	if (rc < 0) { printf("regs write failed\n"); return -1; }

	return 0;
}

int arch_breakpoint_set(breakpoint_t *b)
{
	char brkp[1];
	int rc;

	rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, 1);
	cons_printf("udebug_mem_read() -> %d\n", rc);
	if (rc < 0) return rc;

	brkp[0] = OPCODE_INT3;
	rc = udebug_mem_write(app_phone, brkp, b->addr, 1);
	if (rc < 0) return rc;

	cons_printf("udebug_mem_write() -> %d\n", rc);
	return 0;
}

int arch_breakpoint_remove(breakpoint_t *b)
{
	int rc;

	if (b->active) {
		active_bkpt = NULL;
	} else {
	        rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 1);
		if (rc < 0) {
			cons_printf("error writing mem\n");
			return rc;
		}
	}

	return 0;
}

void arch_event_breakpoint(thash_t thread_hash)
{
	static istate_t istate;
	breakpoint_t *b;
	int rc;

	rc = udebug_regs_read(app_phone, thread_hash, &istate);
//	cons_printf("udebug_regs_read -> %d\n", rc);
//	cons_printf("RIP was 0x%08x\n", istate.rip);
	int brk_addr = istate.rip - 1;

	b = breakpoint_find_by_addr(brk_addr);
	if (!b) {
		cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr);
		return;
	}

	istate.rip = brk_addr;
	istate.rflags |= 0x0100; /* trap flag */

	rc = udebug_regs_write(app_phone, thread_hash, &istate);
	if (rc < 0) { cons_printf("error writing regs\n"); return; }
        rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 1);
	if (rc < 0) { cons_printf("error writing mem\n"); return; }
//		cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc);

	b->active = true;
	active_bkpt = b;

	breakpoint_hit(b);
}

void arch_event_trap(dthread_t *dt)
{
	breakpoint_t *b;
	static istate_t istate;
	unsigned char brkinstr[1];
	int rc;

//	cons_printf("trap event\n");
	b = active_bkpt;
	
	if (b) {
		brkinstr[0] = OPCODE_INT3;
		rc = udebug_mem_write(app_phone, brkinstr, b->addr, 1);
//		cons_printf("restore breakpoint -> %d\n", rc);
		active_bkpt = NULL;
	}

	rc = _set_trap_flag(dt, false);
	dt->arch.singlestep = false;

	singlestep_hit();
}

void arch_dump_regs(thash_t thash)
{
	static istate_t istate;
	int rc;

	rc = udebug_regs_read(app_phone, thash, &istate);
	if (rc < 0) { cons_printf("Error reading regs\n"); return; }

	cons_printf(
	    "rip:%016x rflags:%016x rax:%016x rcx:%016x ecx:%016x "
	    "rdx:%016x\nrsi:%016x rdi:%016x r8:%016x r9:%016x "
	    "r10:%016x r11:%016x cs:%04x\n",
	    istate.rip, istate.rflags, istate.rax, istate.rcx,
	    istate.rdx, istate.rsi, istate.rdi, istate.r8, istate.r9,
	    istate.r10, istate.r11, istate.cs);
}

void arch_singlestep(dthread_t *dt)
{
	int rc;

	rc = _set_trap_flag(dt, true);
	if (rc != 0) return;

	dthread_resume(dt);
}

/** @}
 */
