public inbox for frysk@sourceware.org
 help / color / mirror / Atom feed
* [RFC] initial support for watchpoints in frysk
@ 2008-05-14  7:50 Thiago Jung Bauermann
  2008-05-14 17:31 ` Phil Muldoon
  0 siblings, 1 reply; 4+ messages in thread
From: Thiago Jung Bauermann @ 2008-05-14  7:50 UTC (permalink / raw)
  To: frysk ml

Hi,

Since fhpd is complaining rather loudly about not supporting watchpoints
in Power, I decided to add such support. :-)

This patch has an initial implementation, but it doesn't work yet (more
about it later). I'd like to ask your opinion, especially regarding two
methods I had to add to frysk.sys.Ptrace and
frysk.proc.live.LinuxPtraceTask: setDebugReg and getDebugReg. They are
necessary because on the PowerPC architecture, debug registers are a
priviledged resource, and userland need to access them via
PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG. What do you think, are
these changes ok?

Now regarding why the patch doesn't work: in order to verify if the
process stopped because of the hardware watchpoint, I need to examine
the siginfo structure which comes with the SIGTRAP. This information is
not available to Frysk yet, so I'd like your opinion on the following
approach:

A new method would be added to frysk.sys.Signal to get the siginfo
(cleverly called getSigInfo). It would return a SigInfo class with
methods for each member of the siginfo struct that would be of interst
to frysk (for my purposes, the si_addr member). The method
frysk.sys.Poll.poll would be responsible for creating and the SigInfo
object and setting the member values.

If my understanding is correct, this would make the siginfo available to
LinuxPtraceTaskState.Running.handleStoppedEvent, and from there I just
have to make it reach all the way down to
WatchpointFunctions.hasWatchpointTriggered. How does this sound?

Follows the patch. I didn't write a ChangeLog entry yet, will do when I
commit it.
-- 
[]'s
Thiago Jung Bauermann
Software Engineer
IBM Linux Technology Center

diff --git a/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java b/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java
new file mode 100644
index 0000000..2a57010
--- /dev/null
+++ b/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java
@@ -0,0 +1,184 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2008, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.isa.watchpoints;
+
+import frysk.proc.Task;
+import frysk.proc.live.LinuxPtraceTask;
+
+class PPCWatchpointFunctions extends WatchpointFunctions {
+
+    // Architecture Watchpoint Count. Number of usable
+    // Address-Breakpoint Registers: only one, DABR.
+    public PPCWatchpointFunctions () {
+	noOfWatchpoints = 1;
+    }
+   
+    /**
+     * Builds and sets a hardware watchpoint on a task.
+     *
+     * @param task - task to set a watchpoint on.
+     * @param index - watchpoint number to write. 4 on
+     * x8664
+     * @param addr - linear virtual address to watch.
+     * @param range - length of range to watch. Normally
+     * 1,24, or 8 bytes.
+     * @param writeOnly - When true, only trigger when address is
+     * written. False, trigger when address is read or written to.
+     */
+    public void setWatchpoint(Task task, int index, 
+	       long addr, int range,
+	       boolean writeOnly) {
+
+	// turn bit off (b = bit no): l &= ~(1L << b)
+	// turn bit on ( b= bit no):  l |= (1L << b);
+
+	if (index != 0) {
+	    throw new RuntimeException("Invalid hardware watchpoint index." +
+	       "Has to be 0.");
+	}
+
+	if (range != 8) {
+	    throw new RuntimeException("Invalid size for watchpoint " +
+	       "range. Has to be 8.");
+	}
+	    
+	if ((addr &~ 7) != addr)
+	    throw new RuntimeException("Address 0x"+Long.toHexString(addr) + 
+		    " is not aligned on an 8 byte boundary. Cannot set watchpoint.");
+
+	if (writeOnly)
+	    // set translation and write bits
+	    addr |= 6;
+	else
+	    // set translation and read bits
+	    addr |= 5;
+
+	// Set the debug register on all threads.
+	((LinuxPtraceTask) task).setDebugReg(0, addr);
+    }
+
+    /**
+     * Reads a watchpoint. Takes a task, and an index.
+     *
+     * @param task - task to read a watchpoint from.
+     * @param index - watchpoint number to read.
+
+     * @return Watchpoint - value of Watchpoint at
+     * register. 
+     */
+    public Watchpoint readWatchpoint(Task task, int index) {
+
+	if (index != 0) {
+	    throw new RuntimeException("Invalid hardware watchpoint index." +
+	       "Has to be 0.");
+	}
+
+	// Get Address from watchpoint register
+	long dabr_value = ((LinuxPtraceTask) task).getDebugReg(0);
+	long address = dabr_value & ~7;
+	boolean writeOnly = testBit(address, 0) && !testBit(address, 1);
+
+	return Watchpoint.create(address, 8, index, writeOnly);
+    }	
+
+    /**
+     * Deletes a watchpoint. Takes a task, and an index.
+     *
+     * @param task - task on which to delete a watchpoint.
+     * @param index - watchpoint number to delete.
+     *
+     * @return long - value of register for wp
+     */
+    public final void deleteWatchpoint(Task task, int index) {
+	((LinuxPtraceTask) task).setDebugReg(0, 0);
+    }
+    
+    /**
+     * Reads the Debug control register.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     */
+    protected long readControlRegister(Task task) {
+	throw new RuntimeException("There is no debug control register in PowerPC.");
+    }
+
+    /**
+     * Reads the Debug cstatus register.
+     *
+     * @param task - task to read the debug status
+     * register from.
+     */
+    protected long readStatusRegister(Task task) {
+	throw new RuntimeException("There is no debug status register in PowerPC.");
+    }
+
+    /**
+     * Reads the Debug Status Register and checks if 
+     * the breakpoint specified has fired.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     */
+    public boolean hasWatchpointTriggered(Task task, int index) {
+	// FIXME: in PowerPC, the address which triggered the watchpoint is
+	// sent in the siginfo for the SIGTRAP.  There's no way to grab it
+	// in Frysk yet.
+	throw new RuntimeException("Watchpoints not fully supported in PowerPC yet.");
+    }
+
+    /**
+     * Resets the appropriate bit in the debug status register
+     * after a watchpoint has triggered, thereby reseting it.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     * @param index - Debug register to reset.
+     */
+    public void resetWatchpoint(Task task, int index) {
+	// FIXME: AFAIK, this can be only a NOP for PowerPC. There's no
+	// need to reset a watchpoint after it triggers.
+    }
+
+    private boolean testBit(long register, int bitToTest) {
+	return (register & (1L << bitToTest)) != 0;
+    }
+
+}
diff --git a/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java b/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
index da434ce..f16615a 100644
--- a/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
+++ b/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
@@ -50,6 +50,8 @@ public class WatchpointFunctionFactory {
     private static final ISAMap watchpointTables = new ISAMap("watchpoint table")
 	.put(ISA.IA32, new IA32WatchpointFunctions())
 	.put(ISA.X8664, new X8664WatchpointFunctions())
+	.put(ISA.PPC32BE, new PPCWatchpointFunctions())
+	.put(ISA.PPC64BE, new PPCWatchpointFunctions())
 	;
 
     public static WatchpointFunctions getWatchpointFunctions(ISA isa) {
diff --git a/frysk-core/frysk/proc/live/LinuxPtraceTask.java b/frysk-core/frysk/proc/live/LinuxPtraceTask.java
index 0ecadb3..f1f04c0 100644
--- a/frysk-core/frysk/proc/live/LinuxPtraceTask.java
+++ b/frysk-core/frysk/proc/live/LinuxPtraceTask.java
@@ -380,6 +380,14 @@ public class LinuxPtraceTask extends LiveTask {
 	}
     }
 
+    public void setDebugReg(int reg, long value) {
+	Ptrace.setDebugReg(tid, reg, value);
+    }
+
+    public long getDebugReg(int reg) {
+	return Ptrace.getDebugReg(tid, reg);
+    }
+
     // XXX: Should be selecting the trace flags based on the contents
     // of .observers?  Ptrace.optionTraceSysgood not set by default
     private long ptraceOptions = (Ptrace.OPTION_CLONE
diff --git a/frysk-sys/frysk/sys/ptrace/Ptrace.java b/frysk-sys/frysk/sys/ptrace/Ptrace.java
index aa5b3b8..ea80906 100644
--- a/frysk-sys/frysk/sys/ptrace/Ptrace.java
+++ b/frysk-sys/frysk/sys/ptrace/Ptrace.java
@@ -111,6 +111,29 @@ public class Ptrace {
     private static native long getEventMsg(int pid);
 
     /**
+     * Set PID's debug register number REG.
+     */
+    public static void setDebugReg(ProcessIdentifier pid, int reg, long value) {
+	fine.log("setDebugReg", pid, "...");
+	setDebugReg(pid.intValue(), reg, value);
+	fine.log("... setDebugReg", pid, "returns.");
+    }
+    private static native void setDebugReg(int pid, int reg, long value);
+
+    /**
+     * Get the value of PID's debug register number REG.
+     *
+     * @return value of debug register
+     */
+    public static long getDebugReg(ProcessIdentifier pid, int reg) {
+	fine.log("getDebugReg", pid, "...");
+	long ret = getDebugReg(pid.intValue(), reg);
+	fine.log("... getDebugReg", pid, "returns", ret);
+	return ret;
+    }
+    private static native long getDebugReg(int pid, int reg);
+
+    /**
      * Set PID's trace options.  OPTIONS is formed by or'ing the
      * values returned by the option* methods below.
      */
diff --git a/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx b/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx
index 4aa12eb..9ea6c13 100644
--- a/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx
+++ b/frysk-sys/frysk/sys/ptrace/cni/Ptrace.cxx
@@ -125,6 +125,20 @@ frysk::sys::ptrace::Ptrace::getEventMsg(jint pid) {
   ptraceOp(PTRACE_GETEVENTMSG, pid, NULL, (long) &msg);
   return msg;
 }
+
+void
+frysk::sys::ptrace::Ptrace::setDebugReg(jint pid, jint reg, jlong value) {
+  ptraceOp(PTRACE_SET_DEBUGREG, pid, (void *) reg, value);
+}
+
+jlong
+frysk::sys::ptrace::Ptrace::getDebugReg(jint pid, jint reg) {
+  jlong value;
+
+  ptraceOp(PTRACE_SET_DEBUGREG, pid, (void *) reg, (long) &value);
+
+  return value;
+}
 \f
 void
 frysk::sys::ptrace::Ptrace::setOptions(jint pid, jlong options) {


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] initial support for watchpoints in frysk
  2008-05-14  7:50 [RFC] initial support for watchpoints in frysk Thiago Jung Bauermann
@ 2008-05-14 17:31 ` Phil Muldoon
  2008-05-15  4:07   ` Thiago Jung Bauermann
  0 siblings, 1 reply; 4+ messages in thread
From: Phil Muldoon @ 2008-05-14 17:31 UTC (permalink / raw)
  To: Thiago Jung Bauermann; +Cc: frysk ml

Thiago Jung Bauermann wrote:
> Hi,
>   

Hi Thiago,


> Since fhpd is complaining rather loudly about not supporting watchpoints
> in Power, I decided to add such support. :-)
>   
> I'd like to ask your opinion, especially regarding two
> methods I had to add to frysk.sys.Ptrace and
> frysk.proc.live.LinuxPtraceTask: setDebugReg and getDebugReg. They are
> necessary because on the PowerPC architecture, debug registers are a
> priviledged resource, and userland need to access them via
> PTRACE_SET_DEBUGREG and PTRACE_GET_DEBUGREG. What do you think, are
> these changes ok?
>   


The watchpoint specific code looks ok. I just added an alignment check 
yesterday to x86 and x8664, so if this is important to PPC then should 
add the check too.

I'm a bit worried about the live.LinuxPtraceTask setDebugReg and 
getDebugReg. This is a PPC specific function but it is in a non-ISA 
class. So I think it would lead to confusion. If you were to call this 
on x86 and x8664 what would happen?

Access to the x86 and x8664 debug status registers are performed as follows:

    return task.getRegister(X8664Registers.DEBUG_STATUS);

I think this call eventually resolves to 
RegisterBanks.getBankArrayRegister() which again resolves into ISA 
specific methods to get the register. Andrew who did the register code 
would know better here.

> Now regarding why the patch doesn't work: in order to verify if the
> process stopped because of the hardware watchpoint, I need to examine
> the siginfo structure which comes with the SIGTRAP. This information is
> not available to Frysk yet, so I'd like your opinion on the following
> approach:
>
>   

On x86/x8664 we can just look at the bits in the debug status register 
when a stop event/sigtrap has arrived to see if  it is a watchpoint 
event. If you look at the handleTrappedEvent in the Running and the 
Stepping states you can see there is a very specific sequence of checks 
here. I'm guessing this won't work on the PPC then as you need to 
examine the siginfo structure. What data are you looking for in there?


> If my understanding is correct, this would make the siginfo available to
> LinuxPtraceTaskState.Running.handleStoppedEvent, 

Should that be LinuxPtraceTaskState.Running.handleTrappedEvent and 
LinuxPtraceTaskState.Stepping.handleTrappedEvent? These two states right 
now are the only states that handle watchpoints. (ie sigtrap occurs, 
check if it is a watchpoint, if not, pass the sigtrap along).


Regards

Phil

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] initial support for watchpoints in frysk
  2008-05-14 17:31 ` Phil Muldoon
@ 2008-05-15  4:07   ` Thiago Jung Bauermann
  2008-05-15 15:41     ` Thiago Jung Bauermann
  0 siblings, 1 reply; 4+ messages in thread
From: Thiago Jung Bauermann @ 2008-05-15  4:07 UTC (permalink / raw)
  To: Phil Muldoon; +Cc: frysk ml

Hi Phil,

Thanks for your comments.

On Wed, 2008-05-14 at 08:49 +0100, Phil Muldoon wrote:
> The watchpoint specific code looks ok. I just added an alignment check 
> yesterday to x86 and x8664, so if this is important to PPC then should 
> add the check too.

Yes, I saw that and put the same in powerpc. Thanks for the heads up.

> I'm a bit worried about the live.LinuxPtraceTask setDebugReg and 
> getDebugReg. This is a PPC specific function but it is in a non-ISA 
> class. So I think it would lead to confusion. If you were to call this 
> on x86 and x8664 what would happen?

Right, I just found out that powerpc is the only Linux arch with
PTRACE_SET_DEBUGREG...

> Access to the x86 and x8664 debug status registers are performed as follows:
> 
>     return task.getRegister(X8664Registers.DEBUG_STATUS);
> 
> I think this call eventually resolves to 
> RegisterBanks.getBankArrayRegister() which again resolves into ISA 
> specific methods to get the register. Andrew who did the register code 
> would know better here.

I can create a debugregs() method in the frysk.sys.ptrace.RegisterSet
class (or in a subclass, perhaps?) and then use the
RegisterSetByteBuffer to use the infrastructure you mention. It's a
somehwat involved solution for just one register, but it does fit better
the Frysk registers framework.

> On x86/x8664 we can just look at the bits in the debug status register 
> when a stop event/sigtrap has arrived to see if  it is a watchpoint 
> event. If you look at the handleTrappedEvent in the Running and the 
> Stepping states you can see there is a very specific sequence of checks 
> here. I'm guessing this won't work on the PPC then as you need to 
> examine the siginfo structure.

I just looked at Running.checkWatchpoint (I believe that is what you are
referring to) and it will work fine for powerpc. The siginfo check would
take place inside WatchpointFunctions.hasWatchpointTriggered method, so
it's encapsulated. I just need checkWatchpoint to pass the Signal object
to it (and also, I need the object to have the siginfo information).

>  What data are you looking for in there?

I need to examine siginfo.si_code to verify that it's a trap originated
from a hardware watchpoint. I can also look at siginfo.si_addr to get
the exact address which triggered it (since the watchpoint looks at an 8
byte range).

> > If my understanding is correct, this would make the siginfo available to
> > LinuxPtraceTaskState.Running.handleStoppedEvent, 
> 
> Should that be LinuxPtraceTaskState.Running.handleTrappedEvent and 
> LinuxPtraceTaskState.Stepping.handleTrappedEvent? These two states right 
> now are the only states that handle watchpoints. (ie sigtrap occurs, 
> check if it is a watchpoint, if not, pass the sigtrap along).

Right, my mistake.
-- 
[]'s
Thiago Jung Bauermann
Software Engineer
IBM Linux Technology Center

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [RFC] initial support for watchpoints in frysk
  2008-05-15  4:07   ` Thiago Jung Bauermann
@ 2008-05-15 15:41     ` Thiago Jung Bauermann
  0 siblings, 0 replies; 4+ messages in thread
From: Thiago Jung Bauermann @ 2008-05-15 15:41 UTC (permalink / raw)
  To: Phil Muldoon; +Cc: frysk ml

On Wed, 2008-05-14 at 14:31 -0300, Thiago Jung Bauermann wrote:
> > Access to the x86 and x8664 debug status registers are performed as follows:
> > 
> >     return task.getRegister(X8664Registers.DEBUG_STATUS);
> > 
> > I think this call eventually resolves to 
> > RegisterBanks.getBankArrayRegister() which again resolves into ISA 
> > specific methods to get the register. Andrew who did the register code 
> > would know better here.
> 
> I can create a debugregs() method in the frysk.sys.ptrace.RegisterSet
> class (or in a subclass, perhaps?) and then use the
> RegisterSetByteBuffer to use the infrastructure you mention. It's a
> somehwat involved solution for just one register, but it does fit better
> the Frysk registers framework.

The patch below implements this approach. What do you think?
The only disadvantage is that if powerpc's ptrace ever offers a second
debug register using PTRACE_{GET,SET}_DEBUGREG, this implementation will
not work. But that could be fixed by creating a new kind of ByteBuffer
to deal with that special situation, I believe.

The PPCWatchpointFunctions class is nearly the same as before, except
that it now uses:

task.setRegister(PPC64Registers.DABR, addr);

This patch still doesn't work because of the siginfo issue.

> > On x86/x8664 we can just look at the bits in the debug status register 
> > when a stop event/sigtrap has arrived to see if  it is a watchpoint 
> > event. If you look at the handleTrappedEvent in the Running and the 
> > Stepping states you can see there is a very specific sequence of checks 
> > here. I'm guessing this won't work on the PPC then as you need to 
> > examine the siginfo structure.
> 
> I just looked at Running.checkWatchpoint (I believe that is what you are
> referring to) and it will work fine for powerpc. The siginfo check would
> take place inside WatchpointFunctions.hasWatchpointTriggered method, so
> it's encapsulated. I just need checkWatchpoint to pass the Signal object
> to it (and also, I need the object to have the siginfo information).

I had a quick first look and it seems this approach isn't good. Frysk
maintains only one Signal object per kind of signal, so having siginfo
piggyback on it won't work (since the latter will be different for each
Signal instance). Unless it is guaranteed that there's only one signal
of each kind being handled by Frysk at any moment. Then we could set the
siginfo attribute each time the signal arrives, and not worry about
changing the signal object behind someone's back.

I still need to investigate more to see if I can make that assumption.
-- 
[]'s
Thiago Jung Bauermann
Software Engineer
IBM Linux Technology Center

diff --git a/frysk-core/frysk/isa/banks/LinuxPPCRegisterBanks.java b/frysk-core/frysk/isa/banks/LinuxPPCRegisterBanks.java
index b2fd02b..e08a5ce 100644
--- a/frysk-core/frysk/isa/banks/LinuxPPCRegisterBanks.java
+++ b/frysk-core/frysk/isa/banks/LinuxPPCRegisterBanks.java
@@ -260,4 +260,6 @@ public class LinuxPPCRegisterBanks {
 	.add(new BankRegister(148*8, 8, PPC64Registers.VRSAVE)) // PT_VRSAVE (PT_VR0 + 33*2), index 148
 	;
 
+    public static final BankRegisterMap DEBUGREGS
+	= new BankRegisterMap().add(new BankRegister(0, 8, PPC64Registers.DABR));
 }
diff --git a/frysk-core/frysk/isa/banks/PPCBankRegisters.java b/frysk-core/frysk/isa/banks/PPCBankRegisters.java
index 40d72d6..b8588b9 100644
--- a/frysk-core/frysk/isa/banks/PPCBankRegisters.java
+++ b/frysk-core/frysk/isa/banks/PPCBankRegisters.java
@@ -55,6 +55,7 @@ public class PPCBankRegisters {
 	= new BankArrayRegisterMap()
 	.add(0, LinuxPPCRegisterBanks.GREGS32)
 	.add(0, LinuxPPCRegisterBanks.FPREGS32)
+	.add(1, LinuxPPCRegisterBanks.DEBUGREGS)
 	;
 
     /**
@@ -66,6 +67,7 @@ public class PPCBankRegisters {
 	.add(0, LinuxPPCRegisterBanks.FPREGS64)
 	// AltiVec registers go to a separate note section called NT_PPC_VMX
 	.add(0, LinuxPPCRegisterBanks.VRREGS64)
+	.add(1, LinuxPPCRegisterBanks.DEBUGREGS)
 	;
 
     public static final BankArrayRegisterMap PPC32BE_ON_PPC64BE
diff --git a/frysk-core/frysk/isa/registers/PPC32Registers.java b/frysk-core/frysk/isa/registers/PPC32Registers.java
index 1aabb9c..adb990c 100644
--- a/frysk-core/frysk/isa/registers/PPC32Registers.java
+++ b/frysk-core/frysk/isa/registers/PPC32Registers.java
@@ -246,6 +246,12 @@ public class PPC32Registers extends Registers {
             = new Register("fpscr", StandardTypes.INT32B_T);
 
     /*
+     * Debug register.
+     */
+    public static final Register DABR
+	= new Register("dabr", StandardTypes.INT64B_T);
+
+    /*
      * Defining Register Groups
      */
     public static final RegisterGroup GENERAL
diff --git a/frysk-core/frysk/isa/registers/PPC64Registers.java b/frysk-core/frysk/isa/registers/PPC64Registers.java
index a5505c4..248c769 100644
--- a/frysk-core/frysk/isa/registers/PPC64Registers.java
+++ b/frysk-core/frysk/isa/registers/PPC64Registers.java
@@ -345,6 +345,12 @@ public class PPC64Registers extends Registers {
     public static final Register SPEFSCR
 	= new Register("spefscr", StandardTypes.INT64B_T);
 
+    /*
+     * Debug register.
+     */
+    public static final Register DABR
+	= new Register("dabr", StandardTypes.INT64B_T);
+
     /* 
      * Defining Register Groups
      */
diff --git a/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java b/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java
new file mode 100644
index 0000000..3309bd7
--- /dev/null
+++ b/frysk-core/frysk/isa/watchpoints/PPCWatchpointFunctions.java
@@ -0,0 +1,179 @@
+// This file is part of the program FRYSK.
+//
+// Copyright 2008, Red Hat Inc.
+//
+// FRYSK is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation; version 2 of the License.
+//
+// FRYSK is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+// 
+// You should have received a copy of the GNU General Public License
+// along with FRYSK; if not, write to the Free Software Foundation,
+// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+// 
+// In addition, as a special exception, Red Hat, Inc. gives You the
+// additional right to link the code of FRYSK with code not covered
+// under the GNU General Public License ("Non-GPL Code") and to
+// distribute linked combinations including the two, subject to the
+// limitations in this paragraph. Non-GPL Code permitted under this
+// exception must only link to the code of FRYSK through those well
+// defined interfaces identified in the file named EXCEPTION found in
+// the source code files (the "Approved Interfaces"). The files of
+// Non-GPL Code may instantiate templates or use macros or inline
+// functions from the Approved Interfaces without causing the
+// resulting work to be covered by the GNU General Public
+// License. Only Red Hat, Inc. may make changes or additions to the
+// list of Approved Interfaces. You must obey the GNU General Public
+// License in all respects for all of the FRYSK code and other code
+// used in conjunction with FRYSK except the Non-GPL Code covered by
+// this exception. If you modify this file, you may extend this
+// exception to your version of the file, but you are not obligated to
+// do so. If you do not wish to provide this exception without
+// modification, you must delete this exception statement from your
+// version and license this file solely under the GPL without
+// exception.
+
+package frysk.isa.watchpoints;
+
+import frysk.isa.registers.PPC64Registers;
+import frysk.proc.Task;
+
+class PPCWatchpointFunctions extends WatchpointFunctions {
+
+    // Architecture Watchpoint Count. Number of usable
+    // Address-Breakpoint Registers: only one, DABR.
+    public PPCWatchpointFunctions () {
+	noOfWatchpoints = 1;
+    }
+
+    /**
+     * Builds and sets a hardware watchpoint on a task.
+     *
+     * @param task - task to set a watchpoint on.
+     * @param index - watchpoint number to write. 4 on
+     * x8664
+     * @param addr - linear virtual address to watch.
+     * @param range - length of range to watch. Normally
+     * 1,24, or 8 bytes.
+     * @param writeOnly - When true, only trigger when address is
+     * written. False, trigger when address is read or written to.
+     */
+    public void setWatchpoint(Task task, int index, 
+	       long addr, int range,
+	       boolean writeOnly) {
+
+	if (index != 0) {
+	    throw new RuntimeException("Invalid hardware watchpoint index." +
+	       "Has to be 0.");
+	}
+
+	if (range != 8) {
+	    throw new RuntimeException("Invalid size for watchpoint " +
+	       "range. Has to be 8.");
+	}
+	    
+	if ((addr &~ 7) != addr)
+	    throw new RuntimeException("Address 0x"+Long.toHexString(addr) + 
+		    " is not aligned on an 8 byte boundary. Cannot set watchpoint.");
+
+	if (writeOnly)
+	    // set translation and write bits
+	    addr |= 6;
+	else
+	    // set translation and read bits
+	    addr |= 5;
+
+	task.setRegister(PPC64Registers.DABR, addr);
+    }
+
+    /**
+     * Reads a watchpoint. Takes a task, and an index.
+     *
+     * @param task - task to read a watchpoint from.
+     * @param index - watchpoint number to read.
+
+     * @return Watchpoint - value of Watchpoint at
+     * register. 
+     */
+    public Watchpoint readWatchpoint(Task task, int index) {
+
+	if (index != 0) {
+	    throw new RuntimeException("Invalid hardware watchpoint index." +
+	       "Has to be 0.");
+	}
+
+	// Get Address from watchpoint register
+	long dabr_value = task.getRegister(PPC64Registers.DABR);
+	long address = dabr_value & ~7;
+	boolean writeOnly = testBit(address, 0) && !testBit(address, 1);
+
+	return Watchpoint.create(address, 8, index, writeOnly);
+    }	
+
+    /**
+     * Deletes a watchpoint. Takes a task, and an index.
+     *
+     * @param task - task on which to delete a watchpoint.
+     * @param index - watchpoint number to delete.
+     *
+     * @return long - value of register for wp
+     */
+    public final void deleteWatchpoint(Task task, int index) {
+	task.setRegister(PPC64Registers.DABR, 0);
+    }
+    
+    /**
+     * Reads the Debug control register.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     */
+    protected long readControlRegister(Task task) {
+	throw new RuntimeException("There is no debug control register in PowerPC.");
+    }
+
+    /**
+     * Reads the Debug cstatus register.
+     *
+     * @param task - task to read the debug status
+     * register from.
+     */
+    protected long readStatusRegister(Task task) {
+	throw new RuntimeException("There is no debug status register in PowerPC.");
+    }
+
+    /**
+     * Reads the Debug Status Register and checks if 
+     * the breakpoint specified has fired.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     */
+    public boolean hasWatchpointTriggered(Task task, int index) {
+	// FIXME: in PowerPC, the address which triggered the watchpoint is
+	// sent in the siginfo for the SIGTRAP.  There's no way to grab it
+	// in Frysk yet.
+	throw new RuntimeException("Watchpoints not fully supported in PowerPC yet.");
+    }
+
+    /**
+     * Resets the appropriate bit in the debug status register
+     * after a watchpoint has triggered, thereby reseting it.
+     *
+     * @param task - task to read the debug control
+     * register from.
+     * @param index - Debug register to reset.
+     */
+    public void resetWatchpoint(Task task, int index) {
+	// There is no need to reset a watchpoint after it triggers.
+    }
+
+    private boolean testBit(long register, int bitToTest) {
+	return (register & (1L << bitToTest)) != 0;
+    }
+
+}
diff --git a/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java b/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
index fe8b81b..5ff68da 100644
--- a/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
+++ b/frysk-core/frysk/isa/watchpoints/WatchpointFunctionFactory.java
@@ -50,6 +50,8 @@ public class WatchpointFunctionFactory {
     private static final ISAMap watchpointTables = new ISAMap("watchpoint table")
 	.put(ISA.IA32, new IA32WatchpointFunctions())
 	.put(ISA.X8664, new X8664WatchpointFunctions())
+	.put(ISA.PPC32BE, new PPCWatchpointFunctions())
+	.put(ISA.PPC64BE, new PPCWatchpointFunctions())
 	;
 
     public static WatchpointFunctions getWatchpointFunctions(ISA isa) {
diff --git a/frysk-core/frysk/proc/live/PtraceRegisterBanksFactory.java b/frysk-core/frysk/proc/live/PtraceRegisterBanksFactory.java
index daa86d9..656f687 100644
--- a/frysk-core/frysk/proc/live/PtraceRegisterBanksFactory.java
+++ b/frysk-core/frysk/proc/live/PtraceRegisterBanksFactory.java
@@ -85,7 +85,8 @@ class PtraceRegisterBanksFactory {
 
     private static ByteBuffer[] ppcBanksBE(ProcessIdentifier pid) {
 	ByteBuffer[] bankBuffers = new ByteBuffer[] {
-            new AddressSpaceByteBuffer(pid, AddressSpace.USR)
+            new AddressSpaceByteBuffer(pid, AddressSpace.USR),
+            new RegisterSetByteBuffer(pid, RegisterSet.DEBUGREGS)
         };
 
 	for (int i = 0; i < bankBuffers.length; i++) {
diff --git a/frysk-sys/frysk/sys/ptrace/RegisterSet.java b/frysk-sys/frysk/sys/ptrace/RegisterSet.java
index 7fa028a..3a4340d 100644
--- a/frysk-sys/frysk/sys/ptrace/RegisterSet.java
+++ b/frysk-sys/frysk/sys/ptrace/RegisterSet.java
@@ -80,8 +80,10 @@ public class RegisterSet {
     private static native RegisterSet regs();
     private static native RegisterSet fpregs();
     private static native RegisterSet fpxregs();
+    private static native RegisterSet debugregs();
 
     public static final RegisterSet REGS = regs();
     public static final RegisterSet FPREGS = fpregs();
     public static final RegisterSet FPXREGS = fpxregs();
+    public static final RegisterSet DEBUGREGS = debugregs();
 }
diff --git a/frysk-sys/frysk/sys/ptrace/cni/RegisterSet.cxx b/frysk-sys/frysk/sys/ptrace/cni/RegisterSet.cxx
index 2c3a861..51cf13c 100644
--- a/frysk-sys/frysk/sys/ptrace/cni/RegisterSet.cxx
+++ b/frysk-sys/frysk/sys/ptrace/cni/RegisterSet.cxx
@@ -87,3 +87,15 @@ frysk::sys::ptrace::RegisterSet::fpxregs() {
   return NULL;
 #endif
 }
+
+frysk::sys::ptrace::RegisterSet*
+frysk::sys::ptrace::RegisterSet::debugregs() {
+#if defined(__powerpc__)
+  /* The DABR is 64-bit wide, even in powerpc32.  */
+  return new frysk::sys::ptrace::RegisterSet(sizeof(long long),
+					     PTRACE_GET_DEBUGREG,
+					     PTRACE_SET_DEBUGREG);
+#else
+  return NULL;
+#endif
+}


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2008-05-15  4:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-14  7:50 [RFC] initial support for watchpoints in frysk Thiago Jung Bauermann
2008-05-14 17:31 ` Phil Muldoon
2008-05-15  4:07   ` Thiago Jung Bauermann
2008-05-15 15:41     ` Thiago Jung Bauermann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).