Implementing hardware breakpoints using x86 debug register

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
RajivKumarSrivastav
Member
Member
Posts: 26
Joined: Wed Sep 28, 2011 6:46 am
Location: Bangalore - India

Implementing hardware breakpoints using x86 debug register

Post by RajivKumarSrivastav »

I am writing program to implement hardware breakpoints using x86 Debug Registers, it’s not working somehow :(.
X86 Interrupt descriptor table (IDT) for exceptions and GDT for segments are set properly. I am not using paging and it’s in Protected mode when I set DR registers.

Per Intel manual (System Programming Guide 1– Chapter- 16) , if I set DR0 to DR3 with 4 addresses and enable them using DR6 , then HW breakpoint should work. I referred LINUX source (version 2.6.37.3) for files Hw_breakpoint.c which also does same thing.
I tried ACCESS and EXECUTE breakpoint one by one but none is working. Below is the given source which writes into Debug Registers.

Code: Select all

// These address reference to valid function address in my source code.
void Setx86DebugReg(void)
{
         unsigned int dr0=0xfffe0b9e, dr1=0xfffe02dc,  dr2=0xfffe0300,  dr3 =0xfffe053c;
         unsigned int dr7=0x000007ff; // or 0x4aa or 0x7aa or 0x6aa -> also not working

        asm volatile ("movl %%eax, %%dr0":: "a"(dr0));
        asm volatile ("movl %%eax, %%dr1":: "a"(dr1));
        asm volatile ("movl %%eax, %%dr2":: "a"(dr2));
        asm volatile ("movl %%eax, %%dr3":: "a"(dr3)); 
        asm volatile ("movl %%eax, %%dr7":: "a"(dr7));
 return;
}
Please help and let me know if i need to set any other Register too. Or if i am missing something ?
After call to Setx86DebugReg() function, i should be able to hit breakpoint, which is not happening.
User avatar
Owen
Member
Member
Posts: 1700
Joined: Fri Jun 13, 2008 3:21 pm
Location: Cambridge, United Kingdom
Contact:

Re: Implementing hardware breakpoints using x86 debug regist

Post by Owen »

Real hardware or emulator? In the later case... emulators are notorious for not implementing the debugging hardware.

For instruction breakpoints, you're often best off substituting the first byte of the instruction for an INT3 opcode (and recording somewhere the the original byte so it can be restored and the instruction single stepped)
RajivKumarSrivastav
Member
Member
Posts: 26
Joined: Wed Sep 28, 2011 6:46 am
Location: Bangalore - India

Re: Implementing hardware breakpoints using x86 debug regist

Post by RajivKumarSrivastav »

Thanks for the reply. I am working with real x486 hardware.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Implementing hardware breakpoints using x86 debug regist

Post by Combuster »

[edit] I misinterpreted the documentation [/edit]
Last edited by Combuster on Fri Jul 06, 2012 3:18 am, edited 1 time in total.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
RajivKumarSrivastav
Member
Member
Posts: 26
Joined: Wed Sep 28, 2011 6:46 am
Location: Bangalore - India

Re: Implementing hardware breakpoints using x86 debug regist

Post by RajivKumarSrivastav »

Intel manual "system programming guide 1" .
CHAPTER 16 - DEBUGGING, PROFILING BRANCHES AND TIMESTAMP COUNTER.
First Para:
Intel 64 and IA-32 architectures provide debug facilities for use in debugging code
and monitoring performance. These facilities are valuable for debugging application
software, system software, and multitasking operating systems. Debug support is
accessed using debug registers (DB0 through DB7) and model-specific registers (MSRs):
Does it mean 486 lacks DRx ( 0r DBx) registers ?
User avatar
iansjack
Member
Member
Posts: 4908
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Implementing hardware breakpoints using x86 debug regist

Post by iansjack »

Combuster wrote:The 486 lacked DRx registers...
I believe that information is incorrect. http://bitsavers.org/pdf/intel/_dataShe ... _Apr89.pdf
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Implementing hardware breakpoints using x86 debug regist

Post by Combuster »

I should read properly. The debugging extensions were added after the 486, and its summarisation across several documents led me to the conclusion that without that capability the developer was left with Trapflag and 0xCC debugging.

At any rate, the OP should at least check whether he is trying to use the definition of 386 watchpoints or P1 watchpoints when the CPU is left in the other state.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
RajivKumarSrivastav
Member
Member
Posts: 26
Joined: Wed Sep 28, 2011 6:46 am
Location: Bangalore - India

Re: Implementing hardware breakpoints using x86 debug regist

Post by RajivKumarSrivastav »

Thanks. But my question still remains and given code does not work.
Please let me know, if i am missing something. I have already tried with all possible option through DR7 - control register.
stlw
Member
Member
Posts: 359
Joined: Fri Apr 04, 2008 6:43 am
Contact:

Re: Implementing hardware breakpoints using x86 debug regist

Post by stlw »

I would suggest you an opposite of others. Did you try to compile Bochs with --enable-x86-debugger and try the same ?

Bochs x86 debugger handling is pretty good, it has issues with reporting non-enabled breakpoints to DR6 (x86 gray area) but in the mainstream stuff it should work like HW.

Stanislav
RajivKumarSrivastav
Member
Member
Posts: 26
Joined: Wed Sep 28, 2011 6:46 am
Location: Bangalore - India

Re: Implementing hardware breakpoints using x86 debug regist

Post by RajivKumarSrivastav »

I am not using Bochs. I am using x86 hardware.
stlw
Member
Member
Posts: 359
Joined: Fri Apr 04, 2008 6:43 am
Contact:

Re: Implementing hardware breakpoints using x86 debug regist

Post by stlw »

rajiv123 wrote:I am not using Bochs. I am using x86 hardware.
The code breakpoint could be missed if breakpoint trap cannot be executed for some reason.

For example debug trap is blocked by MOV_SS instruction.

Or for example code breakpoint won't be checked when EFLAGS.RF flag is set.

Stanislav
RajivKumarSrivastav
Member
Member
Posts: 26
Joined: Wed Sep 28, 2011 6:46 am
Location: Bangalore - India

Re: Implementing hardware breakpoints using x86 debug regist

Post by RajivKumarSrivastav »

Thanks all for reply.
My code and concept was correct, but somehow tool to work with x86 hw had problem. Problem was solved When i switched to another tool.
Post Reply