1 /* Copyright (C) 2009-2024 Free Software Foundation, Inc.
3 This file is part of GDB.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 #include "nat/gdb_ptrace.h"
19 #include "mips-linux-watch.h"
21 /* Assuming usable watch registers REGS, return the irw_mask of
25 mips_linux_watch_get_irw_mask (struct pt_watch_regs
*regs
, int n
)
29 case pt_watch_style_mips32
:
30 return regs
->mips32
.watch_masks
[n
] & IRW_MASK
;
31 case pt_watch_style_mips64
:
32 return regs
->mips64
.watch_masks
[n
] & IRW_MASK
;
34 internal_error (_("Unrecognized watch register style"));
38 /* Assuming usable watch registers REGS, return the reg_mask of
42 get_reg_mask (struct pt_watch_regs
*regs
, int n
)
46 case pt_watch_style_mips32
:
47 return regs
->mips32
.watch_masks
[n
] & ~IRW_MASK
;
48 case pt_watch_style_mips64
:
49 return regs
->mips64
.watch_masks
[n
] & ~IRW_MASK
;
51 internal_error (_("Unrecognized watch register style"));
55 /* Assuming usable watch registers REGS, return the num_valid. */
58 mips_linux_watch_get_num_valid (struct pt_watch_regs
*regs
)
62 case pt_watch_style_mips32
:
63 return regs
->mips32
.num_valid
;
64 case pt_watch_style_mips64
:
65 return regs
->mips64
.num_valid
;
67 internal_error (_("Unrecognized watch register style"));
71 /* Assuming usable watch registers REGS, return the watchlo of
75 mips_linux_watch_get_watchlo (struct pt_watch_regs
*regs
, int n
)
79 case pt_watch_style_mips32
:
80 return regs
->mips32
.watchlo
[n
];
81 case pt_watch_style_mips64
:
82 return regs
->mips64
.watchlo
[n
];
84 internal_error (_("Unrecognized watch register style"));
88 /* Assuming usable watch registers REGS, set watchlo of register N to
92 mips_linux_watch_set_watchlo (struct pt_watch_regs
*regs
, int n
,
97 case pt_watch_style_mips32
:
98 /* The cast will never throw away bits as 64 bit addresses can
99 never be used on a 32 bit kernel. */
100 regs
->mips32
.watchlo
[n
] = (uint32_t) value
;
102 case pt_watch_style_mips64
:
103 regs
->mips64
.watchlo
[n
] = value
;
106 internal_error (_("Unrecognized watch register style"));
110 /* Assuming usable watch registers REGS, return the watchhi of
114 mips_linux_watch_get_watchhi (struct pt_watch_regs
*regs
, int n
)
118 case pt_watch_style_mips32
:
119 return regs
->mips32
.watchhi
[n
];
120 case pt_watch_style_mips64
:
121 return regs
->mips64
.watchhi
[n
];
123 internal_error (_("Unrecognized watch register style"));
127 /* Assuming usable watch registers REGS, set watchhi of register N to
131 mips_linux_watch_set_watchhi (struct pt_watch_regs
*regs
, int n
,
136 case pt_watch_style_mips32
:
137 regs
->mips32
.watchhi
[n
] = value
;
139 case pt_watch_style_mips64
:
140 regs
->mips64
.watchhi
[n
] = value
;
143 internal_error (_("Unrecognized watch register style"));
147 /* Read the watch registers of process LWPID and store it in
148 WATCH_READBACK. Save true to *WATCH_READBACK_VALID if watch
149 registers are valid. Return 1 if watch registers are usable.
150 Cached information is used unless FORCE is true. */
153 mips_linux_read_watch_registers (long lwpid
,
154 struct pt_watch_regs
*watch_readback
,
155 int *watch_readback_valid
, int force
)
157 if (force
|| *watch_readback_valid
== 0)
159 if (ptrace (PTRACE_GET_WATCH_REGS
, lwpid
, watch_readback
, NULL
) == -1)
161 *watch_readback_valid
= -1;
164 switch (watch_readback
->style
)
166 case pt_watch_style_mips32
:
167 if (watch_readback
->mips32
.num_valid
== 0)
169 *watch_readback_valid
= -1;
173 case pt_watch_style_mips64
:
174 if (watch_readback
->mips64
.num_valid
== 0)
176 *watch_readback_valid
= -1;
181 *watch_readback_valid
= -1;
184 /* Watch registers appear to be usable. */
185 *watch_readback_valid
= 1;
187 return (*watch_readback_valid
== 1) ? 1 : 0;
190 /* Convert GDB's TYPE to an IRW mask. */
193 mips_linux_watch_type_to_irw (enum target_hw_bp_type type
)
202 return (W_MASK
| R_MASK
);
208 /* Set any low order bits in MASK that are not set. */
211 fill_mask (CORE_ADDR mask
)
215 while (f
&& f
< mask
)
223 /* Try to add a single watch to the specified registers REGS. The
224 address of added watch is ADDR, the length is LEN, and the mask
225 is IRW. Return 1 on success, 0 on failure. */
228 mips_linux_watch_try_one_watch (struct pt_watch_regs
*regs
,
229 CORE_ADDR addr
, int len
, uint32_t irw
)
231 CORE_ADDR base_addr
, last_byte
, break_addr
, segment_len
;
232 CORE_ADDR mask_bits
, t_low
;
235 struct pt_watch_regs regs_copy
;
240 last_byte
= addr
+ len
- 1;
241 mask_bits
= fill_mask (addr
^ last_byte
) | IRW_MASK
;
242 base_addr
= addr
& ~mask_bits
;
244 /* Check to see if it is covered by current registers. */
245 for (i
= 0; i
< mips_linux_watch_get_num_valid (regs
); i
++)
247 t_low
= mips_linux_watch_get_watchlo (regs
, i
);
248 if (t_low
!= 0 && irw
== ((uint32_t) t_low
& irw
))
250 t_hi
= mips_linux_watch_get_watchhi (regs
, i
) | IRW_MASK
;
251 t_low
&= ~(CORE_ADDR
) t_hi
;
252 if (addr
>= t_low
&& last_byte
<= (t_low
+ t_hi
))
256 /* Try to find an empty register. */
258 for (i
= 0; i
< mips_linux_watch_get_num_valid (regs
); i
++)
260 t_low
= mips_linux_watch_get_watchlo (regs
, i
);
262 && irw
== (mips_linux_watch_get_irw_mask (regs
, i
) & irw
))
264 if (mask_bits
<= (get_reg_mask (regs
, i
) | IRW_MASK
))
266 /* It fits, we'll take it. */
267 mips_linux_watch_set_watchlo (regs
, i
, base_addr
| irw
);
268 mips_linux_watch_set_watchhi (regs
, i
, mask_bits
& ~IRW_MASK
);
273 /* It doesn't fit, but has the proper IRW capabilities. */
278 if (free_watches
> 1)
280 /* Try to split it across several registers. */
282 for (i
= 0; i
< mips_linux_watch_get_num_valid (®s_copy
); i
++)
284 t_low
= mips_linux_watch_get_watchlo (®s_copy
, i
);
285 t_hi
= get_reg_mask (®s_copy
, i
) | IRW_MASK
;
286 if (t_low
== 0 && irw
== (t_hi
& irw
))
288 t_low
= addr
& ~(CORE_ADDR
) t_hi
;
289 break_addr
= t_low
+ t_hi
+ 1;
290 if (break_addr
>= addr
+ len
)
293 segment_len
= break_addr
- addr
;
294 mask_bits
= fill_mask (addr
^ (addr
+ segment_len
- 1));
295 mips_linux_watch_set_watchlo (®s_copy
, i
,
296 (addr
& ~mask_bits
) | irw
);
297 mips_linux_watch_set_watchhi (®s_copy
, i
,
298 mask_bits
& ~IRW_MASK
);
299 if (break_addr
>= addr
+ len
)
304 len
= addr
+ len
- break_addr
;
309 /* It didn't fit anywhere, we failed. */
313 /* Fill in the watch registers REGS with the currently cached
314 watches CURRENT_WATCHES. */
317 mips_linux_watch_populate_regs (struct mips_watchpoint
*current_watches
,
318 struct pt_watch_regs
*regs
)
320 struct mips_watchpoint
*w
;
323 /* Clear them out. */
324 for (i
= 0; i
< mips_linux_watch_get_num_valid (regs
); i
++)
326 mips_linux_watch_set_watchlo (regs
, i
, 0);
327 mips_linux_watch_set_watchhi (regs
, i
, 0);
333 uint32_t irw
= mips_linux_watch_type_to_irw (w
->type
);
335 i
= mips_linux_watch_try_one_watch (regs
, w
->addr
, w
->len
, irw
);
336 /* They must all fit, because we previously calculated that they