1c41372c8SMichał Górny //===-- NativeRegisterContextDBReg_x86.cpp --------------------------------===//
2c41372c8SMichał Górny //
3c41372c8SMichał Górny // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4c41372c8SMichał Górny // See https://llvm.org/LICENSE.txt for license information.
5c41372c8SMichał Górny // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c41372c8SMichał Górny //
7c41372c8SMichał Górny //===----------------------------------------------------------------------===//
8c41372c8SMichał Górny
9c41372c8SMichał Górny #include "NativeRegisterContextDBReg_x86.h"
10*c34698a8SPavel Labath #include "lldb/Utility/LLDBLog.h"
11c41372c8SMichał Górny #include "lldb/Utility/RegisterValue.h"
12c41372c8SMichał Górny
13c41372c8SMichał Górny #include "Plugins/Process/Utility/lldb-x86-register-enums.h"
14c41372c8SMichał Górny
15c41372c8SMichał Górny using namespace lldb_private;
16c41372c8SMichał Górny
17c41372c8SMichał Górny // Returns mask/value for status bit of wp_index in DR6
GetStatusBit(uint32_t wp_index)18c41372c8SMichał Górny static inline uint64_t GetStatusBit(uint32_t wp_index) {
19c41372c8SMichał Górny // DR6: ...BBBB
20c41372c8SMichał Górny // 3210 <- status bits for bp./wp. i; 1 if hit
21c41372c8SMichał Górny return 1 << wp_index;
22c41372c8SMichał Górny }
23c41372c8SMichał Górny
24c41372c8SMichał Górny // Returns mask/value for global enable bit of wp_index in DR7
GetEnableBit(uint32_t wp_index)25c41372c8SMichał Górny static inline uint64_t GetEnableBit(uint32_t wp_index) {
26c41372c8SMichał Górny // DR7: ...GLGLGLGL
27c41372c8SMichał Górny // 33221100 <- global/local enable for bp./wp.; 1 if enabled
28c41372c8SMichał Górny // we use global bits because NetBSD kernel does not preserve local
29c41372c8SMichał Górny // bits reliably; Linux seems fine with either
30c41372c8SMichał Górny return 1 << (2 * wp_index + 1);
31c41372c8SMichał Górny }
32c41372c8SMichał Górny
33c41372c8SMichał Górny // Returns mask for both enable bits of wp_index in DR7
GetBothEnableBitMask(uint32_t wp_index)34c41372c8SMichał Górny static inline uint64_t GetBothEnableBitMask(uint32_t wp_index) {
35c41372c8SMichał Górny // DR7: ...GLGLGLGL
36c41372c8SMichał Górny // 33221100 <- global/local enable for bp./wp.; 1 if enabled
37c41372c8SMichał Górny return 3 << (2 * wp_index + 1);
38c41372c8SMichał Górny }
39c41372c8SMichał Górny
40c41372c8SMichał Górny // Returns value for type bits of wp_index in DR7
GetWatchTypeBits(uint32_t watch_flags,uint32_t wp_index)41c41372c8SMichał Górny static inline uint64_t GetWatchTypeBits(uint32_t watch_flags,
42c41372c8SMichał Górny uint32_t wp_index) {
43c41372c8SMichał Górny // DR7:
44c41372c8SMichał Górny // bit: 3322222222221111...
45c41372c8SMichał Górny // 1098765432109876...
46c41372c8SMichał Górny // val: SSTTSSTTSSTTSSTT...
47c41372c8SMichał Górny // wp.: 3333222211110000...
48c41372c8SMichał Górny //
49c41372c8SMichał Górny // where T - type is 01 for write, 11 for r/w
50c41372c8SMichał Górny return watch_flags << (16 + 4 * wp_index);
51c41372c8SMichał Górny }
52c41372c8SMichał Górny
53c41372c8SMichał Górny // Returns value for size bits of wp_index in DR7
GetWatchSizeBits(uint32_t size,uint32_t wp_index)54c41372c8SMichał Górny static inline uint64_t GetWatchSizeBits(uint32_t size, uint32_t wp_index) {
55c41372c8SMichał Górny // DR7:
56c41372c8SMichał Górny // bit: 3322222222221111...
57c41372c8SMichał Górny // 1098765432109876...
58c41372c8SMichał Górny // val: SSTTSSTTSSTTSSTT...
59c41372c8SMichał Górny // wp.: 3333222211110000...
60c41372c8SMichał Górny //
61c41372c8SMichał Górny // where S - size is:
62c41372c8SMichał Górny // 00 for 1 byte
63c41372c8SMichał Górny // 01 for 2 bytes
64c41372c8SMichał Górny // 10 for 8 bytes
65c41372c8SMichał Górny // 11 for 4 bytes
66c41372c8SMichał Górny return (size == 8 ? 0x2 : size - 1) << (18 + 4 * wp_index);
67c41372c8SMichał Górny }
68c41372c8SMichał Górny
69c41372c8SMichał Górny // Returns bitmask for all bits controlling wp_index in DR7
GetWatchControlBitmask(uint32_t wp_index)70c41372c8SMichał Górny static inline uint64_t GetWatchControlBitmask(uint32_t wp_index) {
71c41372c8SMichał Górny // DR7:
72c41372c8SMichał Górny // bit: 33222222222211111111110000000000
73c41372c8SMichał Górny // 10987654321098765432109876543210
74c41372c8SMichał Górny // val: SSTTSSTTSSTTSSTTxxxxxxGLGLGLGLGL
75c41372c8SMichał Górny // wp.: 3333222211110000xxxxxxEE33221100
76c41372c8SMichał Górny return GetBothEnableBitMask(wp_index) | (0xF << (16 + 4 * wp_index));
77c41372c8SMichał Górny }
78c41372c8SMichał Górny
79c41372c8SMichał Górny // Bit mask for control bits regarding all watchpoints.
80c41372c8SMichał Górny static constexpr uint64_t watchpoint_all_control_bit_mask = 0xFFFF00FF;
81c41372c8SMichał Górny
GetDR(int num) const82c41372c8SMichał Górny const RegisterInfo *NativeRegisterContextDBReg_x86::GetDR(int num) const {
83c41372c8SMichał Górny assert(num >= 0 && num <= 7);
84c41372c8SMichał Górny switch (GetRegisterInfoInterface().GetTargetArchitecture().GetMachine()) {
85c41372c8SMichał Górny case llvm::Triple::x86:
86c41372c8SMichał Górny return GetRegisterInfoAtIndex(lldb_dr0_i386 + num);
87c41372c8SMichał Górny case llvm::Triple::x86_64:
88c41372c8SMichał Górny return GetRegisterInfoAtIndex(lldb_dr0_x86_64 + num);
89c41372c8SMichał Górny default:
90c41372c8SMichał Górny llvm_unreachable("Unhandled target architecture.");
91c41372c8SMichał Górny }
92c41372c8SMichał Górny }
93c41372c8SMichał Górny
IsWatchpointHit(uint32_t wp_index,bool & is_hit)94c41372c8SMichał Górny Status NativeRegisterContextDBReg_x86::IsWatchpointHit(uint32_t wp_index,
95c41372c8SMichał Górny bool &is_hit) {
96c41372c8SMichał Górny if (wp_index >= NumSupportedHardwareWatchpoints())
97c41372c8SMichał Górny return Status("Watchpoint index out of range");
98c41372c8SMichał Górny
99c41372c8SMichał Górny RegisterValue dr6;
100c41372c8SMichał Górny Status error = ReadRegister(GetDR(6), dr6);
101c41372c8SMichał Górny if (error.Fail())
102c41372c8SMichał Górny is_hit = false;
103c41372c8SMichał Górny else
104c41372c8SMichał Górny is_hit = dr6.GetAsUInt64() & GetStatusBit(wp_index);
105c41372c8SMichał Górny
106c41372c8SMichał Górny return error;
107c41372c8SMichał Górny }
108c41372c8SMichał Górny
109c41372c8SMichał Górny Status
GetWatchpointHitIndex(uint32_t & wp_index,lldb::addr_t trap_addr)110c41372c8SMichał Górny NativeRegisterContextDBReg_x86::GetWatchpointHitIndex(uint32_t &wp_index,
111c41372c8SMichał Górny lldb::addr_t trap_addr) {
112c41372c8SMichał Górny uint32_t num_hw_wps = NumSupportedHardwareWatchpoints();
113c41372c8SMichał Górny for (wp_index = 0; wp_index < num_hw_wps; ++wp_index) {
114c41372c8SMichał Górny bool is_hit;
115c41372c8SMichał Górny Status error = IsWatchpointHit(wp_index, is_hit);
116c41372c8SMichał Górny if (error.Fail()) {
117c41372c8SMichał Górny wp_index = LLDB_INVALID_INDEX32;
118c41372c8SMichał Górny return error;
119c41372c8SMichał Górny } else if (is_hit) {
120c41372c8SMichał Górny return error;
121c41372c8SMichał Górny }
122c41372c8SMichał Górny }
123c41372c8SMichał Górny wp_index = LLDB_INVALID_INDEX32;
124c41372c8SMichał Górny return Status();
125c41372c8SMichał Górny }
126c41372c8SMichał Górny
IsWatchpointVacant(uint32_t wp_index,bool & is_vacant)127c41372c8SMichał Górny Status NativeRegisterContextDBReg_x86::IsWatchpointVacant(uint32_t wp_index,
128c41372c8SMichał Górny bool &is_vacant) {
129c41372c8SMichał Górny if (wp_index >= NumSupportedHardwareWatchpoints())
130c41372c8SMichał Górny return Status("Watchpoint index out of range");
131c41372c8SMichał Górny
132c41372c8SMichał Górny RegisterValue dr7;
133c41372c8SMichał Górny Status error = ReadRegister(GetDR(7), dr7);
134c41372c8SMichał Górny if (error.Fail())
135c41372c8SMichał Górny is_vacant = false;
136c41372c8SMichał Górny else
137c41372c8SMichał Górny is_vacant = !(dr7.GetAsUInt64() & GetEnableBit(wp_index));
138c41372c8SMichał Górny
139c41372c8SMichał Górny return error;
140c41372c8SMichał Górny }
141c41372c8SMichał Górny
SetHardwareWatchpointWithIndex(lldb::addr_t addr,size_t size,uint32_t watch_flags,uint32_t wp_index)142c41372c8SMichał Górny Status NativeRegisterContextDBReg_x86::SetHardwareWatchpointWithIndex(
143c41372c8SMichał Górny lldb::addr_t addr, size_t size, uint32_t watch_flags, uint32_t wp_index) {
144c41372c8SMichał Górny
145c41372c8SMichał Górny if (wp_index >= NumSupportedHardwareWatchpoints())
146c41372c8SMichał Górny return Status("Watchpoint index out of range");
147c41372c8SMichał Górny
148c41372c8SMichał Górny // Read only watchpoints aren't supported on x86_64. Fall back to read/write
149c41372c8SMichał Górny // waitchpoints instead.
150c41372c8SMichał Górny // TODO: Add logic to detect when a write happens and ignore that watchpoint
151c41372c8SMichał Górny // hit.
152c41372c8SMichał Górny if (watch_flags == 2)
153c41372c8SMichał Górny watch_flags = 3;
154c41372c8SMichał Górny
155c41372c8SMichał Górny if (watch_flags != 1 && watch_flags != 3)
156c41372c8SMichał Górny return Status("Invalid read/write bits for watchpoint");
157c41372c8SMichał Górny if (size != 1 && size != 2 && size != 4 && size != 8)
158c41372c8SMichał Górny return Status("Invalid size for watchpoint");
159c41372c8SMichał Górny
160c41372c8SMichał Górny bool is_vacant;
161c41372c8SMichał Górny Status error = IsWatchpointVacant(wp_index, is_vacant);
162c41372c8SMichał Górny if (error.Fail())
163c41372c8SMichał Górny return error;
164c41372c8SMichał Górny if (!is_vacant)
165c41372c8SMichał Górny return Status("Watchpoint index not vacant");
166c41372c8SMichał Górny
167c41372c8SMichał Górny RegisterValue dr7, drN;
168c41372c8SMichał Górny error = ReadRegister(GetDR(7), dr7);
169c41372c8SMichał Górny if (error.Fail())
170c41372c8SMichał Górny return error;
171c41372c8SMichał Górny error = ReadRegister(GetDR(wp_index), drN);
172c41372c8SMichał Górny if (error.Fail())
173c41372c8SMichał Górny return error;
174c41372c8SMichał Górny
175c41372c8SMichał Górny uint64_t control_bits = dr7.GetAsUInt64() & ~GetWatchControlBitmask(wp_index);
176c41372c8SMichał Górny control_bits |= GetEnableBit(wp_index) |
177c41372c8SMichał Górny GetWatchTypeBits(watch_flags, wp_index) |
178c41372c8SMichał Górny GetWatchSizeBits(size, wp_index);
179c41372c8SMichał Górny
180c41372c8SMichał Górny // Clear dr6 if address or bits changed (i.e. we're not reenabling the same
181c41372c8SMichał Górny // watchpoint). This can not be done when clearing watchpoints since
182c41372c8SMichał Górny // the gdb-remote protocol repeatedly clears and readds watchpoints on all
183c41372c8SMichał Górny // program threads, effectively clearing pending events on NetBSD.
184c41372c8SMichał Górny // NB: enable bits in dr7 are always 0 here since we're (re)adding it
185c41372c8SMichał Górny if (drN.GetAsUInt64() != addr ||
186c41372c8SMichał Górny (dr7.GetAsUInt64() & GetWatchControlBitmask(wp_index)) !=
187c41372c8SMichał Górny (GetWatchTypeBits(watch_flags, wp_index) |
188c41372c8SMichał Górny GetWatchSizeBits(size, wp_index))) {
189c41372c8SMichał Górny ClearWatchpointHit(wp_index);
190c41372c8SMichał Górny
191c41372c8SMichał Górny // We skip update to drN if neither address nor mode changed.
192c41372c8SMichał Górny error = WriteRegister(GetDR(wp_index), RegisterValue(addr));
193c41372c8SMichał Górny if (error.Fail())
194c41372c8SMichał Górny return error;
195c41372c8SMichał Górny }
196c41372c8SMichał Górny
197c41372c8SMichał Górny error = WriteRegister(GetDR(7), RegisterValue(control_bits));
198c41372c8SMichał Górny if (error.Fail())
199c41372c8SMichał Górny return error;
200c41372c8SMichał Górny
201c41372c8SMichał Górny return error;
202c41372c8SMichał Górny }
203c41372c8SMichał Górny
ClearHardwareWatchpoint(uint32_t wp_index)204c41372c8SMichał Górny bool NativeRegisterContextDBReg_x86::ClearHardwareWatchpoint(
205c41372c8SMichał Górny uint32_t wp_index) {
206c41372c8SMichał Górny if (wp_index >= NumSupportedHardwareWatchpoints())
207c41372c8SMichał Górny return false;
208c41372c8SMichał Górny
209c41372c8SMichał Górny RegisterValue dr7;
210c41372c8SMichał Górny Status error = ReadRegister(GetDR(7), dr7);
211c41372c8SMichał Górny if (error.Fail())
212c41372c8SMichał Górny return false;
213c41372c8SMichał Górny
214c41372c8SMichał Górny return WriteRegister(GetDR(7), RegisterValue(dr7.GetAsUInt64() &
215c41372c8SMichał Górny ~GetBothEnableBitMask(wp_index)))
216c41372c8SMichał Górny .Success();
217c41372c8SMichał Górny }
218c41372c8SMichał Górny
ClearWatchpointHit(uint32_t wp_index)219c41372c8SMichał Górny Status NativeRegisterContextDBReg_x86::ClearWatchpointHit(uint32_t wp_index) {
220c41372c8SMichał Górny if (wp_index >= NumSupportedHardwareWatchpoints())
221c41372c8SMichał Górny return Status("Watchpoint index out of range");
222c41372c8SMichał Górny
223c41372c8SMichał Górny RegisterValue dr6;
224c41372c8SMichał Górny Status error = ReadRegister(GetDR(6), dr6);
225c41372c8SMichał Górny if (error.Fail())
226c41372c8SMichał Górny return error;
227c41372c8SMichał Górny
228c41372c8SMichał Górny return WriteRegister(
229c41372c8SMichał Górny GetDR(6), RegisterValue(dr6.GetAsUInt64() & ~GetStatusBit(wp_index)));
230c41372c8SMichał Górny }
231c41372c8SMichał Górny
ClearAllHardwareWatchpoints()232c41372c8SMichał Górny Status NativeRegisterContextDBReg_x86::ClearAllHardwareWatchpoints() {
233c41372c8SMichał Górny RegisterValue dr7;
234c41372c8SMichał Górny Status error = ReadRegister(GetDR(7), dr7);
235c41372c8SMichał Górny if (error.Fail())
236c41372c8SMichał Górny return error;
237c41372c8SMichał Górny return WriteRegister(
238c41372c8SMichał Górny GetDR(7),
239c41372c8SMichał Górny RegisterValue(dr7.GetAsUInt64() & ~watchpoint_all_control_bit_mask));
240c41372c8SMichał Górny }
241c41372c8SMichał Górny
SetHardwareWatchpoint(lldb::addr_t addr,size_t size,uint32_t watch_flags)242c41372c8SMichał Górny uint32_t NativeRegisterContextDBReg_x86::SetHardwareWatchpoint(
243c41372c8SMichał Górny lldb::addr_t addr, size_t size, uint32_t watch_flags) {
244a007a6d8SPavel Labath Log *log = GetLog(LLDBLog::Watchpoints);
245c41372c8SMichał Górny const uint32_t num_hw_watchpoints = NumSupportedHardwareWatchpoints();
246c41372c8SMichał Górny for (uint32_t wp_index = 0; wp_index < num_hw_watchpoints; ++wp_index) {
247c41372c8SMichał Górny bool is_vacant;
248c41372c8SMichał Górny Status error = IsWatchpointVacant(wp_index, is_vacant);
249c41372c8SMichał Górny if (is_vacant) {
250c41372c8SMichał Górny error = SetHardwareWatchpointWithIndex(addr, size, watch_flags, wp_index);
251c41372c8SMichał Górny if (error.Success())
252c41372c8SMichał Górny return wp_index;
253c41372c8SMichał Górny }
254c41372c8SMichał Górny if (error.Fail() && log) {
255c41372c8SMichał Górny LLDB_LOGF(log, "NativeRegisterContextDBReg_x86::%s Error: %s",
256c41372c8SMichał Górny __FUNCTION__, error.AsCString());
257c41372c8SMichał Górny }
258c41372c8SMichał Górny }
259c41372c8SMichał Górny return LLDB_INVALID_INDEX32;
260c41372c8SMichał Górny }
261c41372c8SMichał Górny
262c41372c8SMichał Górny lldb::addr_t
GetWatchpointAddress(uint32_t wp_index)263c41372c8SMichał Górny NativeRegisterContextDBReg_x86::GetWatchpointAddress(uint32_t wp_index) {
264c41372c8SMichał Górny if (wp_index >= NumSupportedHardwareWatchpoints())
265c41372c8SMichał Górny return LLDB_INVALID_ADDRESS;
266c41372c8SMichał Górny RegisterValue drN;
267c41372c8SMichał Górny if (ReadRegister(GetDR(wp_index), drN).Fail())
268c41372c8SMichał Górny return LLDB_INVALID_ADDRESS;
269c41372c8SMichał Górny return drN.GetAsUInt64();
270c41372c8SMichał Górny }
271c41372c8SMichał Górny
NumSupportedHardwareWatchpoints()272c41372c8SMichał Górny uint32_t NativeRegisterContextDBReg_x86::NumSupportedHardwareWatchpoints() {
273c41372c8SMichał Górny // Available debug address registers: dr0, dr1, dr2, dr3
274c41372c8SMichał Górny return 4;
275c41372c8SMichał Górny }
276