1

What would be an example of how the 32-bit debug or control registers could be used in x86? Can these be written to directly, and if so how? For example, I tried:

mov $1, %DR0
mov %eax, %cr0

But does not assemble. How can one write to or examine these registers? Or are these 'kernel-related' things and not available for normal assembly instructions? (and if so, what would be an example of why it would be used?)

carl.hiass
  • 1,526
  • 1
  • 6
  • 26
  • 2
    https://www.felixcloutier.com/x86/mov-2 - there only form of mov that can access debug regs is mov to/from `reg/mem`, not an immediate. Read the manual; that's why it doesn't assemble. As for actually using them, that's more complicated and you normally don't need to bother with them, let your debugger + kernel do that. Or read Intel's manuals to learn how to write an OS that could debug programs. – Peter Cordes Oct 11 '20 at 03:02
  • The basic usage is for hardware breakpoints and watchpoints: break when a load or store accesses a certain address (programmmed into debug regs), or when code execution reaches a certain point. (The latter can also be done by replacing a byte of code with `int3`, a software breakpoint, but code that reads itself will notice. Hardware support for watchpoints is the only way to do that efficiently. Otherwise you'd have to single-step and decode, or emulate.) – Peter Cordes Oct 11 '20 at 03:51
  • Control registers have more obvious necessary uses, e.g. to set modes, enable paging, etc. e.g. CR3 is the physical address of the top-level page-directory. mov to/from control regs are privileged instructions. – Peter Cordes Oct 11 '20 at 03:51

0 Answers0