minix/kernel/arch/i386/debugreg.h
Erik van der Kouwe b42c66ed10 this patch adds access to the debug breakpoints to
the kernel. They are not used atm, but having them in trunk allows them
to be easily used when needed. To set a breakpoint that triggers when
the variable foo is written to (the most common use case), one calls:

breakpoint_set(vir2phys((vir_bytes) &foo), 0,
  BREAKPOINT_FLAG_MODE_GLOBAL |
  BREAKPOINT_FLAG_RW_WRITE |
  BREAKPOINT_FLAG_LEN_4);

It can later be disabled using:

breakpoint_set(vir2phys((vir_bytes) &foo), 0,
  BREAKPOINT_FLAG_MODE_OFF);

There are some limitations:

- There are at most four breakpoints (hardware limit); the index of the
  breakpoint (0-3) is specified as the second parameter of
  breakpoint_set.

- The breakpoint exception in the kernel is not handled and causes a
  panic; it would be reasonably easy to change this by inspecing DR6,
  printing a message, disabling the breakpoint and continuing. However,
  in my experience even just a panic can be very useful.

- Breakpoints can be set only in the part of the address space that is
  in every page table. It is useful for the kernel, but to use this for
  user processes would require saving and restoring the debug registers
  as part of the context switch. Although the CPU provides support for
  local breakpoints (I implemened this as BREAKPOINT_FLAG_LOCAL) they
  only work if task switching is used.
2010-03-19 19:15:20 +00:00

44 lines
1.4 KiB
C

#ifndef __DEBUGREG_H__
#define __DEBUGREG_H__
/* DR6: status flags */
#define DR6_B(bp) (1 << (bp)) /* breakpoint was triggered */
#define DR6_BD (1 << 13) /* debug register access detected */
#define DR6_BS (1 << 14) /* single step */
#define DR6_BT (1 << 15) /* task switch */
/* DR7: control flags */
#define DR7_L(bp) (1 << (2*(bp))) /* breakpoint armed locally */
#define DR7_G(bp) (1 << (1+2*(bp))) /* breakpoint armed globally */
#define DR7_LE (1 << 8) /* exact local breakpoints */
#define DR7_GE (1 << 9) /* exact global breakpoints */
#define DR7_GD (1 << 13) /* detect debug reg movs */
#define DR7_RW_MASK(bp) (3 << (16+4*(bp)))
#define DR7_RW_EXEC(bp) (0 << (16+4*(bp))) /* execute */
#define DR7_RW_WRITE(bp) (1 << (16+4*(bp))) /* write */
#define DR7_RW_IO(bp) (2 << (16+4*(bp))) /* IO */
#define DR7_RW_RW(bp) (3 << (16+4*(bp))) /* read or write */
#define DR7_LN_MASK(bp) (3 << (18+4*(bp)))
#define DR7_LN_1(bp) (0 << (18+4*(bp))) /* 1 byte */
#define DR7_LN_2(bp) (1 << (18+4*(bp))) /* 2 bytes */
#define DR7_LN_8(bp) (2 << (18+4*(bp))) /* 8 bytes */
#define DR7_LN_4(bp) (3 << (18+4*(bp))) /* 4 bytes */
/* debugreg.S */
void ld_dr0(u32_t value);
void ld_dr1(u32_t value);
void ld_dr2(u32_t value);
void ld_dr3(u32_t value);
void ld_dr6(u32_t value);
void ld_dr7(u32_t value);
u32_t st_dr0(void);
u32_t st_dr1(void);
u32_t st_dr2(void);
u32_t st_dr3(void);
u32_t st_dr6(void);
u32_t st_dr7(void);
#endif /* __DEBUGREG_H__ */