-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.cpp
440 lines (391 loc) · 13.8 KB
/
transaction.cpp
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#include "transaction.h"
#include "x86_flags.h"
using namespace simple;
Transaction::Transaction(unsigned int cpu_num, Thrift_Client* tc)
{
mCpuNum = cpu_num;
mTc = tc;
cpu = SIM_get_processor(mCpuNum);
clearStats();
memory_trace = new MemoryTrace(cpu);
nesting = 0;
mPauseDepth = 0;
mHandlingException = false;
mLastLoad = 0;
/* Checkpointing state for the x86-p4 floating point unit */
fpu_status_att = (attr_value_t*)malloc(sizeof(attr_value_t));
fpu_status_att->kind = Sim_Val_Integer;
fpu_status_att->u.integer = 0;
fpu_control_att = (attr_value_t*)malloc(sizeof(attr_value_t));
fpu_control_att->kind = Sim_Val_Integer;
fpu_control_att->u.integer = 0;
fpu_tag_att = (attr_value_t*)malloc(sizeof(attr_value_t));
fpu_tag_att->kind = Sim_Val_Integer;
fpu_tag_att->u.integer = 0;
fpu_registers_att = (attr_value_t*)malloc(sizeof(attr_value_t));
fpu_registers_att->kind = Sim_Val_List;
fpu_registers_att->u.list.size = 8; //8 80-bit fp registers
fpu_registers_att->u.list.vector = (attr_value_t*)malloc(8 * sizeof(attr_value_t));
for(int i = 0; i < 8; i++) {
fpu_registers_att->u.list.vector[i].kind = Sim_Val_Data;
fpu_registers_att->u.list.vector[i].u.data.size = 11; //10 * 8 bytes, plus empty/valid byte
fpu_registers_att->u.list.vector[i].u.data.data = new uint8[11];
}
/* Checkpointing state for the x86-p4 sse unit */
/* Checkpointing state for the x86-p4 mmx registers */
}
Transaction::~Transaction()
{
//clean up any storage for this transaction
delete memory_trace;
registers.clear();
fpu_registers.clear();
}
bool Transaction::runningTransaction() {
return (mStatus == TRANS_ON);
}
int Transaction::nestingLevel() {
return nesting;
}
void Transaction::BeginTransaction(int process_num)
{
nesting++;
assert(nesting != 0); //test for overflow
if(nesting == 1)
{
// grab the program counter
pc = SIM_get_program_counter(cpu);
tm_process_num = process_num;
mStatus = TRANS_ON;
//clear sets
//disable interrupts
cout << "[" << mCpuNum << "] Begin transaction (" << process_num << ")" << endl;
mTc->operate(mCpuNum, BEGIN, (int)SIM_cycle_count(cpu));
//cout << SIM_get_program_counter(SIM_current_processor()) << endl;
//cout << "Begin started at " << SIM_get_program_counter(cpu) << endl;
checkpointRegisters();
disableInterrupts();
clearStats();
}
else {
cout << "nesting > 1? [cpu:" << mCpuNum << "] ==> " << nesting << " resetting to 1 " << endl;
nesting = 1;
}
//SIM_clear_exception();
}
void Transaction::CommitTransaction()
{
/* if(mStatus == TRANS_PAUSE) { //commit triggered at start of exception nesting
assert(0); //does this happen anymore?
mStatus = TRANS_OFF; //set transactions off we don't want to do anything
clearStats();
memory_trace->commit_writes();
memory_trace->clear();
registers.clear();
fpu_registers.clear();
pc = 0;
tm_process_num = -1;
return;
}
else if(mStatus == TRANS_OFF) {
assert(0); //does this happen anymore?
nesting--;
if(nesting != 0)
{
cout << "[assert] nesting depth [cpu:" << mCpuNum << "] ==> " << nesting << endl;
nesting = 0;
}
assert(((int)nesting) == 0);
if(nesting == 0) {
assert(mHandlingException); //if we've just handled a exception wake all
mHandlingException = false;
memory_trace->clear();
registers.clear();
fpu_registers.clear();
pc = 0;
tm_process_num = -1;
}
}
else { //default case inside well-behaved transactions! */
nesting--;
if(nesting != 0) {
cout << "[normal] nesting depth [cpu:" << mCpuNum << "] ==> " << nesting << endl;
nesting = 0;
}
assert(((int)nesting) == 0);
assert(!mHandlingException);
if(nesting == 0)
{
mStatus = TRANS_OFF;
enableInterrupts();
cout << "[" << mCpuNum << "] Commit transaction (" << tm_process_num <<
")" << endl;
clearStats();
memory_trace->commit_writes();
memory_trace->clear();
registers.clear();
fpu_registers.clear();
pc = 0;
tm_process_num = -1;
}
// }
mTc->operate(mCpuNum, COMMIT, (int)SIM_cycle_count(cpu));
}
void Transaction::AbortTransaction()
{
cout << "[" << mCpuNum << "] Abort transaction (" << tm_process_num <<
") depth " << nesting << endl;
mTc->operate(mCpuNum, ABORT, (int)SIM_cycle_count(cpu));
restoreRegisters();
clearTransaction();
/*nesting = 0;
memory_trace->clear();
restoreRegisters();
registers.clear();
fpu_registers.clear();
clearStats();
mStatus = TRANS_OFF;
enableInterrupts();
mHandlingException = false;
mPauseDepth = 0;*/
}
void Transaction::clearTransaction()
{
nesting = 0;
mPauseDepth = 0;
mHandlingException = false;
enableInterrupts();
mStatus = TRANS_OFF;
memory_trace->clear();
registers.clear();
fpu_registers.clear();
clearStats();
}
void Transaction::clearStats()
{
memory_ops = 0;
memory_obs = 0;
memory_loads = 0;
memory_stores = 0;
memory_loads_obs = 0;
memory_stores_obs = 0;
}
void Transaction::AbortReset()
{
assert(0);
/*if(mStatus == TRANS_ABORT)
{
mStatus = TRANS_ON;
}*/
}
bool Transaction::CheckForReadConflict(physical_address_t addr, int size) {
return memory_trace->has_read(addr, size);
}
bool Transaction::CheckForReadConflict(physical_address_t addr) {
return CheckForReadConflict(addr, 4);
}
int Transaction::getWriteBufferSize()
{
return memory_trace->getWriteBufferSize();
}
physical_address_t Transaction::getBufferedWrite(int index)
{
return memory_trace->getBufferedWrite(index);
}
int Transaction::MemoryOperation(generic_transaction_t *mop)
{
if(mStatus == TRANS_ON) {
switch(mop->type)
{
case Sim_Trans_Load:
memory_trace->OperateMemoryRead(mop);
memory_ops++;
memory_loads++;
return Sim_Trans_Load;
case Sim_Trans_Store:
memory_trace->OperateMemoryWrite(mop);
memory_ops++;
memory_stores++;
return Sim_Trans_Store;
default: return -1;
}
}
return -1;
}
int Transaction::MemoryObserve(generic_transaction_t *mop)
{
if(mStatus == TRANS_ON) {
switch(mop->type)
{
case Sim_Trans_Load:
memory_trace->ObserveMemoryRead(mop);
memory_obs++;
memory_loads_obs++;
return Sim_Trans_Load;
case Sim_Trans_Store:
memory_trace->ObserveMemoryWrite(mop);
memory_obs++;
memory_stores_obs++;
return Sim_Trans_Store;
default: return -1;
}
}
return -1;
}
void Transaction::clear_write_set()
{
//clear the transactions write set
}
void Transaction::clear_read_set()
{
//clear the transactions read set
}
/* disable interrupts in an x86 proc */
void Transaction::disableInterrupts()
{
/* int eflag_reg_number = SIM_get_register_number(cpu, "eflags");
uinteger_t eflag = SIM_read_register(cpu, eflag_reg_number);
uinteger_t new_eflag = (eflag & ~X86_EFLAGS_IF) | X86_EFLAGS_AC;
SIM_write_register(cpu, eflag_reg_number, new_eflag);
*/
}
/* enable interrupts in an x86 cpu */
void Transaction::enableInterrupts()
{
/* int eflag_reg_number = SIM_get_register_number(cpu, "eflags");
uinteger_t eflag = SIM_read_register(cpu, eflag_reg_number);
// set bit 9
//uinteger_t new_eflag = (eflag | 0x200);
uinteger_t new_eflag = (eflag | X86_EFLAGS_IF) & (~X86_EFLAGS_AC);
SIM_write_register(cpu, eflag_reg_number, new_eflag);
*/
}
void Transaction::earlyRelease(int var)
{
memory_trace->earlyRelease(var);
}
void Transaction::checkpointRegisters()
{
//get the data type containing all registers
attr_value_t all_registers = SIM_get_all_registers(cpu);
//convert to a list, strictly check that it is of type list first
attr_list_t all_reg_list = all_registers.u.list;
for(int i = 0; i < all_reg_list.size; i++) {
registers.push_back(SIM_read_register(cpu,
all_reg_list.vector[i].u.integer));
}
//read floating point registers
attr_list_t fpu_regs = SIM_get_attribute(cpu, "fpu_regs").u.list;
for(int i = 0; i < fpu_regs.size; i++) {
// cout << "[" << mCpuNum << "] fpureg[" << i << "] ";
//uint8 data[fpu_regs.vector[i].u.data.size];
for(int j = 0; j < fpu_regs.vector[i].u.data.size; j++) {
fpu_registers_att->u.list.vector[i].u.data.data[j] = fpu_regs.vector[i].u.data.data[j];
// cout << (int)data[j] << " ";
}
// cout << endl;
}
fpu_status_att->u.integer = SIM_get_attribute(cpu, "fpu_status").u.integer;
fpu_control_att->u.integer = SIM_get_attribute(cpu, "fpu_control").u.integer;
fpu_tag_att->u.integer = SIM_get_attribute(cpu, "fpu_tag").u.integer;
//cout << "fpu_status " << fpu_status << endl;
//cout << "fpu_control " << fpu_control << endl;
//cout << "fpu_tag " << fpu_tag << endl;
/*
fpu_commit_last_instr = SIM_get_attribute(cpu, "fpu_commit_last_instr").u.integer;
fpu_last_instr_pointer0 = SIM_get_attribute(cpu, "fpu_last_instr_pointer0").u.integer;
fpu_last_instr_pointer1 = SIM_get_attribute(cpu, "fpu_last_instr_pointer1").u.integer;
fpu_last_instr_selector0 = SIM_get_attribute(cpu, "fpu_last_instr_selector0").u.integer;
fpu_last_instr_selector1 = SIM_get_attribute(cpu, "fpu_last_instr_selector1").u.integer;
fpu_last_opcode0 = SIM_get_attribute(cpu, "fpu_last_opcode0").u.integer;
fpu_last_opcode1 = SIM_get_attribute(cpu, "fpu_last_opcode1").u.integer;
fpu_last_operand_pointer0 = SIM_get_attribute(cpu, "fpu_last_operand_pointer0").u.integer;
fpu_last_operand_pointer1 = SIM_get_attribute(cpu, "fpu_last_operand_pointer1").u.integer;
fpu_last_operand_selector0 = SIM_get_attribute(cpu, "fpu_last_operand_selector0").u.integer;
fpu_last_operand_selector1 = SIM_get_attribute(cpu, "fpu_last_operand_selector1").u.integer;*/
}
void Transaction::restoreRegisters()
{
//printf("Register list size == %d\n", (int)registers.size());
for(int i = 0; i < (int)registers.size(); i++)
{
if(i != 51) {
SIM_write_register(cpu, i, registers[i]);
}
}
attr_list_t fpu_regs = SIM_get_attribute(cpu, "fpu_regs").u.list;
SIM_set_attribute(cpu, "fpu_regs", fpu_registers_att);
/* for(int i = 0; i < (int)fpu_registers.size(); i++)
{
cout << "[" << mCpuNum << "] fpureg[" << i << "] ";
for(int j = 0; j < fpu_regs.vector[i].u.data.size; j++) {
cout << (int)fpu_regs.vector[i].u.data.data[j] << " ";
fpu_regs.vector[i].u.data.data[j] = fpu_registers[i][j];
}
cout << endl;
}
*/
//cout << "fpu_status " << SIM_get_attribute(cpu, "fpu_status").u.integer << endl;
SIM_set_attribute(cpu, "fpu_status", fpu_status_att);
//cout << "fpu_statuss " << SIM_get_attribute(cpu, "fpu_status").u.integer << endl;
//cout << "fpu_control " << SIM_get_attribute(cpu, "fpu_control").u.integer << endl;
SIM_set_attribute(cpu, "fpu_control", fpu_control_att);
//cout << "fpu_controls " << SIM_get_attribute(cpu, "fpu_control").u.integer << endl;
//cout << "fpu_tag " << SIM_get_attribute(cpu, "fpu_tag").u.integer << endl;
SIM_set_attribute(cpu, "fpu_tag", fpu_tag_att);
//cout << "fpu_tags " << SIM_get_attribute(cpu, "fpu_tag").u.integer << endl;
/*SIM_get_attribute(cpu, "fpu_commit_last_instr").u.integer = 0; //fpu_commit_last_instr;
SIM_get_attribute(cpu, "fpu_last_instr_pointer0").u.integer = 0; //fpu_last_instr_pointer0;
SIM_get_attribute(cpu, "fpu_last_instr_pointer1").u.integer = 0; //fpu_last_instr_pointer1;
SIM_get_attribute(cpu, "fpu_last_instr_selector0").u.integer = 0; //fpu_last_instr_selector0;
SIM_get_attribute(cpu, "fpu_last_instr_selector1").u.integer = 0; //fpu_last_instr_selector1;
SIM_get_attribute(cpu, "fpu_last_opcode0").u.integer = 0; //fpu_last_opcode0;
SIM_get_attribute(cpu, "fpu_last_opcode1").u.integer = 0; //fpu_last_opcode1;
SIM_get_attribute(cpu, "fpu_last_operand_pointer0").u.integer = 0; //fpu_last_operand_pointer0;
SIM_get_attribute(cpu, "fpu_last_operand_pointer1").u.integer = 0; //fpu_last_operand_pointer1;
SIM_get_attribute(cpu, "fpu_last_operand_selector0").u.integer = 0; //fpu_last_operand_selector0;
SIM_get_attribute(cpu, "fpu_last_operand_selector1").u.integer = 0; //fpu_last_operand_selector1;*/
SIM_set_program_counter(cpu, pc);
}
int Transaction::handlingException(int exception)
{
mHandlingException = true;
mPauseDepth++;
mStatus = TRANS_PAUSE;
enableInterrupts();
return mPauseDepth;
}
void Transaction::ResumeTransaction()
{
assert(mStatus == TRANS_PAUSE);
mStatus = TRANS_ON;
mHandlingException = false;
}
int Transaction::clearingException(int exception)
{
mPauseDepth--;
if(mPauseDepth == 0)
{
//mStatus = TRANS_ON;
//mHandlingException = false;
//disableInterrupts();
//AbortTransaction();
}
return mPauseDepth;
}
bool Transaction::inException()
{
if(mStatus == TRANS_PAUSE)
assert(mHandlingException);
return (mStatus == TRANS_PAUSE);
}
bool Transaction::lastLoad(physical_address_t addr) {
if(addr == mLastLoad) {
mLastLoad = -1;
return true;
}
else {
mLastLoad = addr;
return false;
}
}