/*
 *  linux/m68k/ptrace.c
 *
 *  Copyright (C) 1994 by Hamish Macdonald
 *  Taken from linux/kernel/ptrace.c and modified for M680x0.
 *  linux/kernel/ptrace.c is by Ross Biro 1/23/92, edited by Linus Torvalds
 *
 * This file is subject to the terms and conditions of the GNU General
 * Public License.  See the file README.legal in the main directory of
 * this archive for more details.
 */

#include <asm/segment.h>

#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/bootinfo.h>

/*
 * This routine gets a long from any process space by following the page
 * tables. NOTE! You should check that the long isn't on a page boundary,
 * and that it is in the task area before calling this: this routine does
 * no checking.
 *
 */
static unsigned long get_long(struct task_struct * tsk,
	unsigned long addr)
{
	unsigned long page, *ptrp;

repeat:
	page = tsk->tss.pagedir_v[L1_INDEX(addr)];
	if (!(page & PAGE_TABLE)) {
		do_no_page(0,addr,tsk,0);
		goto repeat;
	}
	ptrp = (unsigned long *)PTOV(page & TABLE_MASK);
	page = ptrp[L2_INDEX(addr)];
	if (!(page & PAGE_TABLE)) {
		do_no_page(0,addr,tsk,0);
		goto repeat;
	}
	page = PTOV(page & PAGE_MASK);
	page += PAGE_PTR(addr);
	page = *(unsigned long *)page;
	if (!(page & PAGE_PRESENT)) {
		do_no_page(0,addr,tsk,0);
		goto repeat;
	}
	page = PTOV(page & PAGE_MASK);
	page += addr & ~PAGE_MASK;
	return *(unsigned long *) page;
}

/*
 * This routine puts a long into any process space by following the page
 * tables. NOTE! You should check that the long isn't on a page boundary,
 * and that it is in the task area before calling this: this routine does
 * no checking.
 *
 * Now keeps R/W state of page so that a text page stays readonly
 * even if a debugger scribbles breakpoints into it.  -M.U-
 */
static void put_long(struct task_struct * tsk, unsigned long addr,
	unsigned long data)
{
	unsigned long page, pte = 0, *ptrp;
	int readonly = 0;

repeat:
	page = tsk->tss.pagedir_v[L1_INDEX(addr)];
	if (!(page & PAGE_TABLE)) {
		do_no_page(0,addr,tsk,0);
		goto repeat;
	}
	ptrp = (unsigned long *)PTOV(page & TABLE_MASK);
	page = ptrp[L2_INDEX(addr)];
	if (!(page & PAGE_TABLE)) {
		do_no_page(0,addr,tsk,0);
		goto repeat;
	}
	page = PTOV(page & PAGE_MASK);
	page += PAGE_PTR(addr);
	pte = page;
	page = *(unsigned long *)page;
	if (!(page & PAGE_PRESENT)) {
		do_no_page(0 /* PAGE_RW */ ,addr,tsk,0);
		goto repeat;
	}
	if (!PAGE_IS_RW(page)) {
		if(!(page & PAGE_COW))
			readonly = 1;
		do_wp_page(PAGE_RW | PAGE_PRESENT,addr,tsk,0);
		goto repeat;
	}
/* we're bypassing pagetables, so we have to set the dirty bit ourselves */
	*(unsigned long *) pte |= (PAGE_DIRTY|PAGE_COW);
	page = PTOV(page & PAGE_MASK);
	page += addr & ~PAGE_MASK;
	*(unsigned long *) page = data;
	if(readonly) {
		*(unsigned long *) pte &=~ (PAGE_RW|PAGE_COW);
		invalidate();
	}
}

/*
 * This routine checks the page boundaries, and that the offset is
 * within the task area. It then calls get_long() to read a long.
 */
int ptrace_read_long(struct task_struct * tsk, long addr,
		     unsigned long * result)
{
	unsigned long low,high;

	if (addr > TASK_SIZE-sizeof(long))
		return -EIO;
	if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
		low = get_long(tsk,addr & ~(sizeof(long)-1));
		high = get_long(tsk,(addr+sizeof(long)) & ~(sizeof(long)-1));
		switch (addr & (sizeof(long)-1)) {
			case 1:
				low >>= 8;
				low |= high << 24;
				break;
			case 2:
				low >>= 16;
				low |= high << 16;
				break;
			case 3:
				low >>= 24;
				low |= high << 8;
				break;
		}
		*result = low;
	} else
		*result = get_long(tsk,addr);
	return 0;
}

/*
 * This routine checks the page boundaries, and that the offset is
 * within the task area. It then calls put_long() to write a long.
 */
int ptrace_write_long(struct task_struct * tsk, long addr, long data)
{
	unsigned long low,high;

	if (addr > TASK_SIZE-sizeof(long))
		return -EIO;
	if ((addr & ~PAGE_MASK) > PAGE_SIZE-sizeof(long)) {
		low = get_long(tsk,addr & ~(sizeof(long)-1));
		high = get_long(tsk,(addr+sizeof(long)) & ~(sizeof(long)-1));
		switch (addr & (sizeof(long)-1)) {
			case 0: /* shouldn't happen, but safety first */
				low = data;
				break;
			case 1:
				low &= 0x000000ff;
				low |= data << 8;
				high &= ~0xff;
				high |= data >> 24;
				break;
			case 2:
				low &= 0x0000ffff;
				low |= data << 16;
				high &= ~0xffff;
				high |= data >> 16;
				break;
			case 3:
				low &= 0x00ffffff;
				low |= data << 24;
				high &= ~0xffffff;
				high |= data >> 8;
				break;
		}
		put_long(tsk,addr & ~(sizeof(long)-1),low);
		put_long(tsk,(addr+sizeof(long)) & ~(sizeof(long)-1),high);
	} else
		put_long(tsk,addr,data);
	return 0;
}

int ptrace_peekusr (struct task_struct *tsk, long addr, long data)
{
	unsigned long tmp;
	int res;

	if ((addr & 3) || addr < 0 || addr > 18*sizeof(long))
		return -EIO;

	res = verify_area(VERIFY_WRITE, (void *) data, sizeof(long));
	if (res)
		return res;
	tmp = 0;  /* Default return condition */
	if(addr < 19*sizeof(long)) {
		addr = addr >> 2; /* temporary hack. */
		tmp = ptrace_get_stack_long(tsk, sizeof(long)*addr);
		if (addr == PT_SR)
			tmp &= 0xffff;
	}
	put_fs_long(tmp,(unsigned long *) data);
	return 0;
}

int ptrace_pokeusr(struct task_struct *tsk, long addr, long data)
{
	if ((addr & 3) || addr < 0 || addr > 18*sizeof(long))
		return -EIO;

	addr = addr >> 2; /* temporary hack. */

	if (addr == PT_ORIG_D0)
		return -EIO;
	if (addr == PT_SR) {
		data &= SR_MASK;
		data |= ptrace_get_stack_long(tsk, PT_SR*sizeof(long)) & ~SR_MASK;
	}
	/* Do not allow the user to set the debug register for kernel
	   address space */
	if(addr < 19){
		if (ptrace_put_stack_long(tsk, sizeof(long)*addr, data))
			return -EIO;
		return 0;
	};
	return -EIO;
}
