From f67f6df4699833cc5ea3342ace95e884afc72f2e Mon Sep 17 00:00:00 2001
From: Thomas Chou <thomas@wytron.com.tw>
Date: Fri, 18 Jan 2008 18:54:40 +0800
Subject: [PATCH] nios2: ptrace registers number reverted to Altera gdb

The user registers number are reverted to those of Altera gdb, so that
they are compatible and eased future updates.

The single step handler in ptrace is removed, because nios2 does not
have single step instruction and we use software single step simulation
from gdb host.

Signed-off-by: Thomas Chou <thomas@wytron.com.tw>
---
 linux-2.6.x/arch/nios2nommu/kernel/ptrace.c |   32 +++++---------------------
 linux-2.6.x/include/asm-nios2nommu/ptrace.h |   24 ++++++++++++-------
 user/gdbserver/gdbserver/linux-nios2-low.c  |   23 +++++++++++-------
 user/gdbserver/regformats/reg-nios2.dat     |    8 ++++++-
 4 files changed, 42 insertions(+), 45 deletions(-)

diff --git a/linux-2.6.x/arch/nios2nommu/kernel/ptrace.c b/linux-2.6.x/arch/nios2nommu/kernel/ptrace.c
index 6982f9a..8682ab6 100644
--- a/linux-2.6.x/arch/nios2nommu/kernel/ptrace.c
+++ b/linux-2.6.x/arch/nios2nommu/kernel/ptrace.c
@@ -26,7 +26,7 @@
 #include <asm/processor.h>
 #include <asm/cacheflush.h>
 
-#define DEBUG
+/* #define DEBUG */
 
 #ifdef DEBUG
 # define PRINTK_DEBUG(str...)   printk(KERN_DEBUG str)
@@ -55,13 +55,14 @@ static int regoff[] = {
 	         -1, PT_REG( r1), PT_REG( r2), PT_REG( r3),
 	PT_REG( r4), PT_REG( r5), PT_REG( r6), PT_REG( r7),
 	PT_REG( r8), PT_REG( r9), PT_REG(r10), PT_REG(r11),
-	PT_REG(r12), PT_REG(r13), PT_REG(r14), PT_REG(r15),
+	PT_REG(r12), PT_REG(r13), PT_REG(r14), PT_REG(r15),  /* reg 15 */
 	SW_REG(r16), SW_REG(r17), SW_REG(r18), SW_REG(r19),
 	SW_REG(r20), SW_REG(r21), SW_REG(r22), SW_REG(r23),
 	         -1,          -1, PT_REG( gp), PT_REG( sp),
-	         -1, PT_REG( ea),          -1, PT_REG( ra),
-	         -1, PT_REG(estatus),      -1,          -1,
-	         -1,          -1
+	PT_REG( fp), PT_REG( ea),          -1, PT_REG( ra),  /* reg 31 */
+	PT_REG( ea),          -1,          -1,          -1,  /* use ea for pc */
+	         -1,          -1,          -1,          -1,
+	         -1,          -1,          -1,          -1   /* reg 43 */ 
 };
 
 /*
@@ -228,27 +229,6 @@ long arch_ptrace(struct task_struct *child, long request, long addr, long data)
 			break;
 		}
 
-		/*
-		 * Single stepping requires placing break instructions in
-		 * the code to break back. If you are stepping through a 
-		 * conditional branch you need to decode the test and put
-		 * the break in the correct location.
-		 */
-		case PTRACE_SINGLESTEP: {  /* set the trap flag. */
-
-			PRINTK_DEBUG("%s SINGLESTEP: addr=0x%08x, data=0x%08x\n", __FUNCTION__, (u32)addr, (u32)data);
-			ret = -EIO;
-			if ((unsigned long) data > _NSIG)
-				break;
-			clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
-
-			child->exit_code = data;
-			/* give it a chance to run. */
-			wake_up_process(child);
-			ret = 0;
-			break;
-		}
-
 		case PTRACE_DETACH:	/* detach a process that was attached. */
 			PRINTK_DEBUG("%s DETACH\n", __FUNCTION__);
 			ret = ptrace_detach(child, data);
diff --git a/linux-2.6.x/include/asm-nios2nommu/ptrace.h b/linux-2.6.x/include/asm-nios2nommu/ptrace.h
index ceeed6b..8686d55 100644
--- a/linux-2.6.x/include/asm-nios2nommu/ptrace.h
+++ b/linux-2.6.x/include/asm-nios2nommu/ptrace.h
@@ -64,17 +64,23 @@
 #define PTR_BA		30
 #define PTR_RA		31
 /* Control registers */
-#define PTR_STATUS	32
-#define PTR_ESTATUS	33
-#define PTR_BSTATUS	34
-#define PTR_IENABLE	35
-#define PTR_IPENDING	36
-#define PTR_CPUID	37
+#define PTR_PC    	32
+#define PTR_STATUS	33
+#define PTR_ESTATUS	34
+#define PTR_BSTATUS	35
+#define PTR_IENABLE	36
+#define PTR_IPENDING	37
+#define PTR_CPUID	38
+#define PTR_CTL6	39
+#define PTR_CTL7	40
+#define PTR_PTEADDR	41
+#define PTR_TLBACC	42
+#define PTR_TLBMISC	43
 
 /* Text/data offsets, needed by gdbserver */
-#define PT_TEXT_ADDR	38*4
-#define PT_DATA_ADDR	39*4
-#define PT_TEXT_END_ADDR 40*4
+#define PT_TEXT_ADDR	44*4
+#define PT_TEXT_END_ADDR 45*4
+#define PT_DATA_ADDR	46*4
 
 /* this struct defines the way the registers are stored on the
    stack during a system call. 
diff --git a/user/gdbserver/gdbserver/linux-nios2-low.c b/user/gdbserver/gdbserver/linux-nios2-low.c
index adafd73..1fab749 100644
--- a/user/gdbserver/gdbserver/linux-nios2-low.c
+++ b/user/gdbserver/gdbserver/linux-nios2-low.c
@@ -14,13 +14,14 @@ static int nios2_regmap[] =
            -1, PTR_R1  * 4, PTR_R2  * 4, PTR_R3  * 4,
   PTR_R4  * 4, PTR_R5  * 4, PTR_R6  * 4, PTR_R7  * 4,
   PTR_R8  * 4, PTR_R9  * 4, PTR_R10 * 4, PTR_R11 * 4,
-  PTR_R12 * 4, PTR_R13 * 4, PTR_R14 * 4, PTR_R15 * 4,
+  PTR_R12 * 4, PTR_R13 * 4, PTR_R14 * 4, PTR_R15 * 4,  /* reg 15 */
   PTR_R16 * 4, PTR_R17 * 4, PTR_R18 * 4, PTR_R19 * 4,
   PTR_R20 * 4, PTR_R21 * 4, PTR_R22 * 4, PTR_R23 * 4,
-           -1,          -1, PTR_GP  * 4, PTR_SP  * 4,
-           -1, PTR_EA  * 4,          -1, PTR_RA  * 4,
-           -1, PTR_ESTATUS*4,        -1,          -1,
-           -1,          -1
+           -1,          -1, PTR_GP  * 4, PTR_SP  * 4, 
+  PTR_FP  * 4,          -1,          -1, PTR_RA  * 4,  /* reg 31 */
+  PTR_PC  * 4,          -1,          -1,          -1,
+	   -1,          -1,          -1,          -1,
+	   -1,          -1,          -1,          -1
 };
 
 #define nios2_num_regs (sizeof(nios2_regmap) / sizeof(nios2_regmap[0]))
@@ -28,20 +29,20 @@ static int nios2_regmap[] =
 static int
 nios2_cannot_store_register (int regno)
 {
-  return (regno >= nios2_num_regs);
+  return (regno >= 33);
 }
 
 static int
 nios2_cannot_fetch_register (int regno)
 {
-  return (regno >= nios2_num_regs);
+  return (regno >= 33);
 }
 
 static CORE_ADDR
 nios2_get_pc ()
 {
   unsigned long pc;
-  collect_register_by_name ("ea", &pc);
+  collect_register_by_name ("pc", &pc);
   return pc;
 }
 
@@ -49,10 +50,14 @@ static void
 nios2_set_pc (CORE_ADDR pc)
 {
   unsigned long newpc = pc;
-  supply_register_by_name ("ea", &newpc);
+  supply_register_by_name ("pc", &newpc);
 }
 
+#if 0
+static const unsigned long nios2_breakpoint = 0x003da03a;
+#else
 static const unsigned long nios2_breakpoint = 0x003b687a;	/* Trap instr. w/imm=0x01 */
+#endif
 #define nios2_breakpoint_len 4
 
 static int
diff --git a/user/gdbserver/regformats/reg-nios2.dat b/user/gdbserver/regformats/reg-nios2.dat
index 9e93a70..77b39a9 100644
--- a/user/gdbserver/regformats/reg-nios2.dat
+++ b/user/gdbserver/regformats/reg-nios2.dat
@@ -1,5 +1,5 @@
 name:nios2
-expedite:sp,fp,ea
+expedite:sp,fp,pc
 32:zero
 32:at
 32:r2
@@ -32,9 +32,15 @@ expedite:sp,fp,ea
 32:ea
 32:ba
 32:ra
+32:pc
 32:status
 32:estatus
 32:bstatus
 32:ienable
 32:ipending
 32:cpuid
+32:ctl6
+32:ctl7
+32:pteaddr
+32:tlbacc
+32:tlbmisc
-- 
1.5.3.3

