aboutsummaryrefslogtreecommitdiff
path: root/riscv/triggers.cc
blob: b6fa4dabad2a9c120e2e2dd16c7761f581e651ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include "arith.h"
#include "debug_defines.h"
#include "processor.h"
#include "triggers.h"

namespace triggers {

reg_t tdata2_csr_t::tdata2_read(const processor_t UNUSED * const proc) const noexcept {
  return tdata2;
}

bool tdata2_csr_t::tdata2_write(processor_t UNUSED * const UNUSED proc, const reg_t UNUSED val) noexcept {
  tdata2 = val;
  return true;
}

reg_t disabled_trigger_t::tdata1_read(const processor_t * const proc) const noexcept
{
  auto xlen = proc->get_xlen();
  reg_t tdata1 = 0;
  tdata1 = set_field(tdata1, CSR_TDATA1_TYPE(xlen), 15);
  tdata1 = set_field(tdata1, CSR_TDATA1_DMODE(xlen), dmode);
  return tdata1;
}

bool disabled_trigger_t::tdata1_write(processor_t * const proc, const reg_t val) noexcept
{
  auto xlen = proc->get_xlen();
  dmode = proc->get_state()->debug_mode ? get_field(val, CSR_TDATA1_DMODE(xlen)) : 0;
  return true;
}

reg_t mcontrol_t::tdata1_read(const processor_t * const proc) const noexcept {
  reg_t v = 0;
  auto xlen = proc->get_xlen();
  v = set_field(v, MCONTROL_TYPE(xlen), MCONTROL_TYPE_MATCH);
  v = set_field(v, MCONTROL_DMODE(xlen), dmode);
  v = set_field(v, MCONTROL_MASKMAX(xlen), 0);
  v = set_field(v, CSR_MCONTROL_HIT, hit);
  v = set_field(v, MCONTROL_SELECT, select);
  v = set_field(v, MCONTROL_TIMING, timing);
  v = set_field(v, MCONTROL_ACTION, action);
  v = set_field(v, MCONTROL_CHAIN, chain);
  v = set_field(v, MCONTROL_MATCH, match);
  v = set_field(v, MCONTROL_M, m);
  v = set_field(v, MCONTROL_S, s);
  v = set_field(v, MCONTROL_U, u);
  v = set_field(v, MCONTROL_EXECUTE, execute);
  v = set_field(v, MCONTROL_STORE, store);
  v = set_field(v, MCONTROL_LOAD, load);
  return v;
}

bool mcontrol_t::tdata1_write(processor_t * const proc, const reg_t val) noexcept {
  auto xlen = proc->get_xlen();
  dmode = get_field(val, MCONTROL_DMODE(xlen));
  hit = get_field(val, CSR_MCONTROL_HIT);
  select = get_field(val, MCONTROL_SELECT);
  timing = get_field(val, MCONTROL_TIMING);
  action = (triggers::action_t) get_field(val, MCONTROL_ACTION);
  chain = get_field(val, MCONTROL_CHAIN);
  unsigned match_value = get_field(val, MCONTROL_MATCH);
  switch (match_value) {
    case MATCH_EQUAL:
    case MATCH_NAPOT:
    case MATCH_GE:
    case MATCH_LT:
    case MATCH_MASK_LOW:
    case MATCH_MASK_HIGH:
      match = (triggers::mcontrol_t::match_t) match_value;
      break;
    default:
      match = MATCH_EQUAL;
      break;
  }
  m = get_field(val, MCONTROL_M);
  s = get_field(val, MCONTROL_S);
  u = get_field(val, MCONTROL_U);
  execute = get_field(val, MCONTROL_EXECUTE);
  store = get_field(val, MCONTROL_STORE);
  load = get_field(val, MCONTROL_LOAD);
  // Assume we're here because of csrw.
  if (execute)
    timing = 0;
  return true;
}

bool mcontrol_t::simple_match(unsigned xlen, reg_t value) const {
  switch (match) {
    case triggers::mcontrol_t::MATCH_EQUAL:
      return value == tdata2;
    case triggers::mcontrol_t::MATCH_NAPOT:
      {
        reg_t mask = ~((1 << (cto(tdata2)+1)) - 1);
        return (value & mask) == (tdata2 & mask);
      }
    case triggers::mcontrol_t::MATCH_GE:
      return value >= tdata2;
    case triggers::mcontrol_t::MATCH_LT:
      return value < tdata2;
    case triggers::mcontrol_t::MATCH_MASK_LOW:
      {
        reg_t mask = tdata2 >> (xlen/2);
        return (value & mask) == (tdata2 & mask);
      }
    case triggers::mcontrol_t::MATCH_MASK_HIGH:
      {
        reg_t mask = tdata2 >> (xlen/2);
        return ((value >> (xlen/2)) & mask) == (tdata2 & mask);
      }
  }
  assert(0);
}

match_result_t mcontrol_t::memory_access_match(processor_t * const proc, operation_t operation, reg_t address, std::optional<reg_t> data) {
  state_t * const state = proc->get_state();
  if ((operation == triggers::OPERATION_EXECUTE && !execute) ||
      (operation == triggers::OPERATION_STORE && !store) ||
      (operation == triggers::OPERATION_LOAD && !load) ||
      (state->prv == PRV_M && !m) ||
      (state->prv == PRV_S && !s) ||
      (state->prv == PRV_U && !u)) {
    return MATCH_NONE;
  }

  reg_t value;
  if (select) {
    if (!data.has_value())
      return MATCH_NONE;
    value = *data;
  } else {
    value = address;
  }

  // We need this because in 32-bit mode sometimes the PC bits get sign
  // extended.
  auto xlen = proc->get_xlen();
  if (xlen == 32) {
    value &= 0xffffffff;
  }

  if (simple_match(xlen, value)) {
    /* This is OK because this function is only called if the trigger was not
     * inhibited by the previous trigger in the chain. */
    hit = true;
    if (timing)
      return MATCH_FIRE_AFTER;
    else
      return MATCH_FIRE_BEFORE;
  }
  return MATCH_NONE;
}

reg_t itrigger_t::tdata1_read(const processor_t * const proc) const noexcept
{
  auto xlen = proc->get_xlen();
  reg_t tdata1 = 0;
  tdata1 = set_field(tdata1, CSR_ITRIGGER_TYPE(xlen), 4);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_DMODE(xlen), dmode);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_HIT(xlen), hit);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_VS, vs);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_VU, vu);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_NMI, nmi);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_M, m);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_S, s);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_U, u);
  tdata1 = set_field(tdata1, CSR_ITRIGGER_ACTION, action);
  return tdata1;
}

bool itrigger_t::tdata1_write(processor_t * const proc, const reg_t val) noexcept
{
  auto xlen = proc->get_xlen();
  assert(get_field(val, CSR_ITRIGGER_TYPE(xlen)) == 4);
  dmode = proc->get_state()->debug_mode ? get_field(val, CSR_ITRIGGER_DMODE(xlen)) : 0;
  hit = get_field(val, CSR_ITRIGGER_HIT(xlen));
  vs = proc->extension_enabled('H') ? get_field(val, CSR_ITRIGGER_VS) : 0;
  vu = proc->extension_enabled('H') ? get_field(val, CSR_ITRIGGER_VU) : 0;
  nmi = get_field(val, CSR_ITRIGGER_NMI);
  m = get_field(val, CSR_ITRIGGER_M);
  s = proc->extension_enabled('S') ? get_field(val, CSR_ITRIGGER_S) : 0;
  u = proc->extension_enabled('U') ? get_field(val, CSR_ITRIGGER_U) : 0;
  action = (action_t)get_field(val, CSR_ITRIGGER_ACTION);
  if (action > 4 || (action==1 && dmode==0))
    action = ACTION_DEBUG_EXCEPTION;
  return true;
}

match_result_t itrigger_t::trap_taking_match(processor_t * const proc, trap_t& t)
{
  state_t * const state = proc->get_state();
  if ((state->prv == PRV_M && !m) ||
      (state->prv == PRV_S && !s) ||
      (state->prv == PRV_U && !u)) {
    return MATCH_NONE;
  }

  bool interrupt = (t.cause() & ((reg_t)1 << (proc->get_const_xlen() - 1))) != 0;
  reg_t bit = t.cause() & 0xff;
  assert(bit < proc->get_const_xlen());
  if (interrupt && ((bit == 0 && nmi) || ((tdata2 >> bit) & 1))) { // Assume NMI's exception code is 0
    hit = true;
    return MATCH_FIRE_AFTER;
  }
  return MATCH_NONE;
}

reg_t etrigger_t::tdata1_read(const processor_t * const proc) const noexcept
{
  auto xlen = proc->get_xlen();
  reg_t tdata1 = 0;
  tdata1 = set_field(tdata1, CSR_ETRIGGER_TYPE(xlen), 5);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_DMODE(xlen), dmode);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_HIT(xlen), hit);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_VS, vs);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_VU, vu);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_M, m);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_S, s);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_U, u);
  tdata1 = set_field(tdata1, CSR_ETRIGGER_ACTION, action);
  return tdata1;
}

bool etrigger_t::tdata1_write(processor_t * const proc, const reg_t val) noexcept
{
  auto xlen = proc->get_xlen();
  assert(get_field(val, CSR_ETRIGGER_TYPE(xlen)) == 5);
  dmode = proc->get_state()->debug_mode ? get_field(val, CSR_ETRIGGER_DMODE(xlen)) : 0;
  hit = get_field(val, CSR_ETRIGGER_HIT(xlen));
  vs = proc->extension_enabled('H') ? get_field(val, CSR_ETRIGGER_VS) : 0;
  vu = proc->extension_enabled('H') ? get_field(val, CSR_ETRIGGER_VU) : 0;
  m = get_field(val, CSR_ETRIGGER_M);
  s = proc->extension_enabled('S') ? get_field(val, CSR_ETRIGGER_S) : 0;
  u = proc->extension_enabled('U') ? get_field(val, CSR_ETRIGGER_U) : 0;
  action = (action_t)get_field(val, CSR_ETRIGGER_ACTION);
  if (action > 4 || (action==1 && dmode==0))
    action = ACTION_DEBUG_EXCEPTION;
  return true;
}

match_result_t etrigger_t::trap_taking_match(processor_t * const proc, trap_t& t)
{
  state_t * const state = proc->get_state();
  if ((state->prv == PRV_M && !m) ||
      (state->prv == PRV_S && !s) ||
      (state->prv == PRV_U && !u)) {
    return MATCH_NONE;
  }

  reg_t bit = t.cause();
  bool interrupt = (bit & ((reg_t)1 << (proc->get_const_xlen() - 1))) != 0;
  assert((bit&0xff) < proc->get_const_xlen());
  if (!interrupt && ((tdata2 >> bit) & 1)) {
    hit = true;
    return MATCH_FIRE_AFTER;
  }
  return MATCH_NONE;
}

module_t::module_t(unsigned count) : triggers(count) {
  for (unsigned i = 0; i < count; i++) {
    triggers[i] = new disabled_trigger_t();
  }
}

module_t::~module_t() {
  for (auto trigger : triggers) {
    delete trigger;
  }
}

reg_t module_t::tdata1_read(const processor_t * const proc, unsigned index) const noexcept
{
  return triggers[index]->tdata1_read(proc);
}

bool module_t::tdata1_write(processor_t * const proc, unsigned index, const reg_t val) noexcept
{
  if (triggers[index]->get_dmode() && !proc->get_state()->debug_mode) {
    return false;
  }

  auto xlen = proc->get_xlen();

  // hardware should ignore writes that set dmode to 1 if the previous trigger has both dmode of 0 and chain of 1
  if (index > 0)
    if (!triggers[index-1]->get_dmode() && triggers[index-1]->get_chain())
      if (get_field(val, CSR_TDATA1_DMODE(xlen)))
        return true; // Andes: behave as writting the same value

  unsigned type = get_field(val, CSR_TDATA1_TYPE(xlen));
  reg_t tdata1 = val;
  reg_t tdata2 = triggers[index]->tdata2_read(proc);

  // hardware must zero chain in writes that set dmode to 0 if the next trigger has dmode of 1
  if (index+1 < triggers.size())
    if (triggers[index+1]->get_dmode())
      if (!get_field(val, CSR_TDATA1_DMODE(xlen)))
        if (type == 2 || type == 6) {
          assert(CSR_MCONTROL_CHAIN == CSR_MCONTROL6_CHAIN);
          tdata1 = set_field(tdata1, CSR_MCONTROL_CHAIN, 0);
        }

  // dmode only writable from debug mode
  if (!proc->get_state()->debug_mode)
    tdata1 = set_field(tdata1, CSR_TDATA1_DMODE(xlen), 0);

  delete triggers[index];
  switch (type) {
    case 2: triggers[index] = new mcontrol_t(); break;
    case 4: triggers[index] = new itrigger_t(); break;
    case 5: triggers[index] = new etrigger_t(); break;
    default: triggers[index] = new disabled_trigger_t(); break;
  }

  bool result = triggers[index]->tdata1_write(proc, tdata1);
  result &= triggers[index]->tdata2_write(proc, tdata2);
  proc->trigger_updated(triggers);
  return result;
}

reg_t module_t::tdata2_read(const processor_t * const proc, unsigned index) const noexcept
{
  return triggers[index]->tdata2_read(proc);
}

bool module_t::tdata2_write(processor_t * const proc, unsigned index, const reg_t val) noexcept
{
  if (triggers[index]->get_dmode() && !proc->get_state()->debug_mode) {
    return false;
  }
  bool result = triggers[index]->tdata2_write(proc, val);
  proc->trigger_updated(triggers);
  return result;
}

match_result_t module_t::memory_access_match(action_t * const action, operation_t operation, reg_t address, std::optional<reg_t> data)
{
  state_t * const state = proc->get_state();
  if (state->debug_mode)
    return MATCH_NONE;

  bool chain_ok = true;

  for (unsigned int i = 0; i < triggers.size(); i++) {
    if (!chain_ok) {
      chain_ok |= !triggers[i]->get_chain();
      continue;
    }

    /* Note: We call memory_access_match for each trigger in a chain as long as
     * the triggers are matching. This results in "temperature coding" so that
     * `hit` is set on each of the consecutive triggers that matched, even if the
     * entire chain did not match. This is allowed by the spec, because the final
     * trigger in the chain will never get `hit` set unless the entire chain
     * matches. */
    match_result_t result = triggers[i]->memory_access_match(proc, operation, address, data);
    if (result != MATCH_NONE && !triggers[i]->get_chain()) {
      *action = triggers[i]->get_action();
      return result;
    }

    chain_ok = result != MATCH_NONE || !triggers[i]->get_chain();
  }
  return MATCH_NONE;
}

match_result_t module_t::trap_taking_match(action_t * const action, trap_t& t)
{
  state_t * const state = proc->get_state();
  if (state->debug_mode)
    return MATCH_NONE;

  for (unsigned int i = 0; i < triggers.size(); i++) {
    match_result_t result = triggers[i]->trap_taking_match(proc, t);
    if (result != MATCH_NONE) {
      *action = triggers[i]->get_action();
      return result;
    }
  }
  return MATCH_NONE;
}

};