aboutsummaryrefslogtreecommitdiff
path: root/src/target/mips64.c
blob: 347cdfc4be3582123aaaec2d96dad485b23171d1 (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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
 * Support for processors implementing MIPS64 instruction set
 *
 * Copyright (C) 2014 by Andrey Sidorov <anysidorov@gmail.com>
 * Copyright (C) 2014 by Aleksey Kuleshov <rndfax@yandex.ru>
 * Copyright (C) 2014 by Antony Pavlov <antonynpavlov@gmail.com>
 * Copyright (C) 2014 by Peter Mamonov <pmamonov@gmail.com>
 *
 * Based on the work of:
 *   Copyright (C) 2008 by Spencer Oliver
 *   Copyright (C) 2008 by David T.L. Wong
 *   Copyright (C) 2010 by Konstantin Kostyukhin, Nikolay Shmyrev
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "mips64.h"

static const struct {
	unsigned id;
	const char *name;
	enum reg_type type;
	const char *group;
	const char *feature;
	int flag;
} mips64_regs[] = {
	{  0,  "r0", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  1,  "r1", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  2,  "r2", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  3,  "r3", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  4,  "r4", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  5,  "r5", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  6,  "r6", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  7,  "r7", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  8,  "r8", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{  9,  "r9", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 10, "r10", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 11, "r11", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 12, "r12", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 13, "r13", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 14, "r14", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 15, "r15", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 16, "r16", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 17, "r17", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 18, "r18", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 19, "r19", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 20, "r20", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 21, "r21", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 22, "r22", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 23, "r23", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 24, "r24", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 25, "r25", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 26, "r26", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 27, "r27", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 28, "r28", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 29, "r29", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 30, "r30", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 31, "r31", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 32, "lo", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ 33, "hi", REG_TYPE_UINT64, NULL, "org.gnu.gdb.mips.cpu", 0 },
	{ MIPS64_NUM_CORE_REGS + 0, "pc", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cpu", 0 },
	{ MIPS64_NUM_CORE_REGS + 1, "Random", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 2, "Entrylo_0", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 3, "Entrylo_1", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 4, "Context", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 5, "Pagemask", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 6, "Wired", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 7, "badvaddr", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 8, "Count", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 9, "EntryHi", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 10, "Compare", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 11, "status", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 12, "cause", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 13, "EPC", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 14, "PrID", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 15, "Config", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 16, "LLA", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 17, "WatchLo0", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 18, "WatchLo1", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 19, "WatchHi0", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 20, "WatchHi1", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 21, "Xcontext", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 22, "ChipMemCtrl", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 23, "Debug", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 24, "Perfcount, sel=0", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 25, "Perfcount, sel=1", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 26, "Perfcount, sel=2", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 27, "Perfcount, sel=3", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 28, "ECC", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 29, "CacheErr", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 30, "TagLo", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 31, "TagHi", REG_TYPE_UINT32, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 32, "DataHi", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_REGS + 33, "EEPC", REG_TYPE_UINT64, NULL,
		"org.gnu.gdb.mips.cp0", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 0,  "f0", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 1,  "f1", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 2,  "f2", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 3,  "f3", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 4, "f4", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 5,  "f5", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 6,  "f6", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 7,  "f7", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 8,  "f8", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 9,  "f9", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 10, "f10", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 11, "f11", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 12, "f12", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 13, "f13", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 14, "f14", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 15, "f15", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 16, "f16", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 17, "f17", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 18, "f18", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 19, "f19", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 20, "f20", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 21, "f21", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 22, "f22", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 23, "f23", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 24, "f24", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 25, "f25", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 26, "f26", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 27, "f27", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 28, "f28", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 29, "f29", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 30, "f30", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 31, "f31", REG_TYPE_IEEE_DOUBLE, NULL,
		 "org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 32, "fcsr", REG_TYPE_INT, "float",
		"org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 33, "fir", REG_TYPE_INT, "float",
		"org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 34, "fconfig", REG_TYPE_INT, "float",
		"org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 35, "fccr", REG_TYPE_INT, "float",
		"org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 36, "fexr", REG_TYPE_INT, "float",
		"org.gnu.gdb.mips.fpu", 0 },
	{ MIPS64_NUM_CORE_C0_REGS + 37, "fenr", REG_TYPE_INT, "float",
		"org.gnu.gdb.mips.fpu", 0 },
};

static int reg_type2size(enum reg_type type)
{
	switch (type) {
	case REG_TYPE_UINT32:
	case REG_TYPE_INT:
		return 32;
	case REG_TYPE_UINT64:
	case REG_TYPE_IEEE_DOUBLE:
		return 64;
	default:
		return 64;
	}
}

static int mips64_get_core_reg(struct reg *reg)
{
	int retval;
	struct mips64_core_reg *mips64_reg = reg->arch_info;
	struct target *target = mips64_reg->target;
	struct mips64_common *mips64_target = target->arch_info;

	if (target->state != TARGET_HALTED)
		return ERROR_TARGET_NOT_HALTED;

	retval = mips64_target->read_core_reg(target, mips64_reg->num);

	return retval;
}

static int mips64_set_core_reg(struct reg *reg, uint8_t *buf)
{
	struct mips64_core_reg *mips64_reg = reg->arch_info;
	struct target *target = mips64_reg->target;
	uint64_t value = buf_get_u64(buf, 0, 64);

	if (target->state != TARGET_HALTED)
		return ERROR_TARGET_NOT_HALTED;

	buf_set_u64(reg->value, 0, 64, value);
	reg->dirty = 1;
	reg->valid = 1;

	return ERROR_OK;
}

static int mips64_read_core_reg(struct target *target, int num)
{
	uint64_t reg_value;

	/* get pointers to arch-specific information */
	struct mips64_common *mips64 = target->arch_info;

	if ((num < 0) || (num >= MIPS64_NUM_REGS))
		return ERROR_COMMAND_ARGUMENT_INVALID;

	reg_value = mips64->core_regs[num];
	buf_set_u64(mips64->core_cache->reg_list[num].value, 0, 64, reg_value);
	mips64->core_cache->reg_list[num].valid = 1;
	mips64->core_cache->reg_list[num].dirty = 0;

	return ERROR_OK;
}

static int mips64_write_core_reg(struct target *target, int num)
{
	uint64_t reg_value;

	/* get pointers to arch-specific information */
	struct mips64_common *mips64 = target->arch_info;

	if ((num < 0) || (num >= MIPS64_NUM_REGS))
		return ERROR_COMMAND_ARGUMENT_INVALID;

	reg_value = buf_get_u64(mips64->core_cache->reg_list[num].value, 0, 64);
	mips64->core_regs[num] = reg_value;
	LOG_DEBUG("write core reg %i value 0x%" PRIx64 "", num, reg_value);
	mips64->core_cache->reg_list[num].valid = 1;
	mips64->core_cache->reg_list[num].dirty = 0;

	return ERROR_OK;
}

int mips64_invalidate_core_regs(struct target *target)
{
	/* get pointers to arch-specific information */
	struct mips64_common *mips64 = target->arch_info;
	unsigned int i;

	for (i = 0; i < mips64->core_cache->num_regs; i++) {
		mips64->core_cache->reg_list[i].valid = 0;
		mips64->core_cache->reg_list[i].dirty = 0;
	}

	return ERROR_OK;
}


int mips64_get_gdb_reg_list(struct target *target, struct reg **reg_list[],
	int *reg_list_size, enum target_register_class reg_class)
{
	/* get pointers to arch-specific information */
	struct mips64_common *mips64 = target->arch_info;
	register int i;

	/* include floating point registers */
	*reg_list_size = MIPS64_NUM_REGS;
	*reg_list = malloc(sizeof(struct reg *) * (*reg_list_size));

	for (i = 0; i < MIPS64_NUM_REGS; i++)
		(*reg_list)[i] = &mips64->core_cache->reg_list[i];

	return ERROR_OK;
}

int mips64_save_context(struct target *target)
{
	int retval;
	struct mips64_common *mips64 = target->arch_info;
	struct mips_ejtag *ejtag_info = &mips64->ejtag_info;

	retval = mips64_pracc_read_regs(ejtag_info, mips64->core_regs);
	if (retval != ERROR_OK)
		return retval;

	for (unsigned i = 0; i < MIPS64_NUM_REGS; i++)
			retval = mips64->read_core_reg(target, i);

	return retval;
}

int mips64_restore_context(struct target *target)
{
	struct mips64_common *mips64 = target->arch_info;
	struct mips_ejtag *ejtag_info = &mips64->ejtag_info;

	for (unsigned i = 0; i < MIPS64_NUM_REGS; i++) {
		if (mips64->core_cache->reg_list[i].dirty)
			mips64->write_core_reg(target, i);
	}

	return mips64_pracc_write_regs(ejtag_info, mips64->core_regs);
}

int mips64_arch_state(struct target *target)
{
	struct mips64_common *mips64 = target->arch_info;
	struct reg *pc = &mips64->core_cache->reg_list[MIPS64_PC];

	if (mips64->common_magic != MIPS64_COMMON_MAGIC) {
		LOG_ERROR("BUG: called for a non-MIPS64 target");
		exit(-1);
	}

	LOG_USER("target halted due to %s, pc: 0x%" PRIx64 "",
		 debug_reason_name(target), buf_get_u64(pc->value, 0, 64));

	return ERROR_OK;
}

static const struct reg_arch_type mips64_reg_type = {
	.get = mips64_get_core_reg,
	.set = mips64_set_core_reg,
};

int mips64_build_reg_cache(struct target *target)
{
	/* get pointers to arch-specific information */
	struct mips64_common *mips64 = target->arch_info;
	struct reg_cache **cache_p, *cache;
	struct mips64_core_reg *arch_info = NULL;
	struct reg *reg_list = NULL;
	unsigned i;

	cache = calloc(1, sizeof(*cache));
	if (!cache) {
		LOG_ERROR("unable to allocate cache");
		return ERROR_FAIL;
	}

	reg_list = calloc(MIPS64_NUM_REGS, sizeof(*reg_list));
	if (!reg_list) {
		LOG_ERROR("unable to allocate reg_list");
		goto alloc_fail;
	}

	arch_info = calloc(MIPS64_NUM_REGS, sizeof(*arch_info));
	if (!arch_info) {
		LOG_ERROR("unable to allocate arch_info");
		goto alloc_fail;
	}

	for (i = 0; i < MIPS64_NUM_REGS; i++) {
		struct mips64_core_reg *a = &arch_info[i];
		struct reg *r = &reg_list[i];

		r->arch_info = &arch_info[i];
		r->caller_save = true;	/* gdb defaults to true */
		r->exist = true;
		r->feature = &a->feature;
		r->feature->name = mips64_regs[i].feature;
		r->group = mips64_regs[i].group;
		r->name = mips64_regs[i].name;
		r->number = i;
		r->reg_data_type = &a->reg_data_type;
		r->reg_data_type->type = mips64_regs[i].type;
		r->size = reg_type2size(mips64_regs[i].type);
		r->type = &mips64_reg_type;
		r->value = &a->value[0];

		a->mips64_common = mips64;
		a->num = mips64_regs[i].id;
		a->target = target;
	}

	cache->name = "mips64 registers";
	cache->reg_list = reg_list;
	cache->num_regs = MIPS64_NUM_REGS;

	cache_p = register_get_last_cache_p(&target->reg_cache);
	(*cache_p) = cache;

	mips64->core_cache = cache;

	return ERROR_OK;

alloc_fail:
	free(cache);
	free(reg_list);
	free(arch_info);

	return ERROR_FAIL;
}

int mips64_init_arch_info(struct target *target, struct mips64_common *mips64,
			  struct jtag_tap *tap)
{
	mips64->bp_scanned = false;
	mips64->common_magic = MIPS64_COMMON_MAGIC;
	mips64->data_break_list = NULL;
	mips64->ejtag_info.tap = tap;
	mips64->fast_data_area = NULL;
	mips64->mips64mode32 = false;
	mips64->read_core_reg = mips64_read_core_reg;
	mips64->write_core_reg = mips64_write_core_reg;

	return ERROR_OK;
}

int mips64_run_algorithm(struct target *target, int num_mem_params,
			 struct mem_param *mem_params, int num_reg_params,
			 struct reg_param *reg_params, target_addr_t entry_point,
			 target_addr_t exit_point, int timeout_ms, void *arch_info)
{
	/* TODO */
	return ERROR_OK;
}

int mips64_examine(struct target *target)
{
	struct mips64_common *mips64 = target->arch_info;

	if (target_was_examined(target))
		return ERROR_OK;

	/* TODO: why we do not do mips64_configure_break_unit() here? */
	mips64->bp_scanned = false;
	mips64->num_data_bpoints = 0;
	mips64->num_data_bpoints_avail = 0;
	mips64->num_inst_bpoints = 0;
	mips64->num_inst_bpoints_avail = 0;

	target_set_examined(target);

	return ERROR_OK;
}

static int mips64_configure_i_break_unit(struct target *target)
{
	/* get pointers to arch-specific information */
	struct mips64_common *mips64 = target->arch_info;
	struct mips64_comparator *ibl;
	uint64_t bpinfo;
	int retval;
	int i;

	/* get number of inst breakpoints */
	retval = target_read_u64(target, EJTAG64_V25_IBS, &bpinfo);
	if (retval != ERROR_OK)
		return retval;

	mips64->num_inst_bpoints = (bpinfo >> 24) & 0x0F;
	mips64->num_inst_bpoints_avail = mips64->num_inst_bpoints;
	ibl = calloc(mips64->num_inst_bpoints, sizeof(*ibl));
	if (!ibl) {
		LOG_ERROR("unable to allocate inst_break_list");
		return ERROR_FAIL;
	}

	for (i = 0; i < mips64->num_inst_bpoints; i++)
		ibl[i].reg_address = EJTAG64_V25_IBA0 + (0x100 * i);

	mips64->inst_break_list = ibl;
	/* clear IBIS reg */
	retval = target_write_u64(target, EJTAG64_V25_IBS, 0);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

static int mips64_configure_d_break_unit(struct target *target)
{
	struct mips64_common *mips64 = target->arch_info;
	struct mips64_comparator *dbl;
	uint64_t bpinfo;
	int retval;
	int i;

	/* get number of data breakpoints */
	retval = target_read_u64(target, EJTAG64_V25_DBS, &bpinfo);
	if (retval != ERROR_OK)
		return retval;

	mips64->num_data_bpoints = (bpinfo >> 24) & 0x0F;
	mips64->num_data_bpoints_avail = mips64->num_data_bpoints;

	dbl = calloc(mips64->num_data_bpoints, sizeof(*dbl));

	if (!dbl) {
		LOG_ERROR("unable to allocate data_break_list");
		return ERROR_FAIL;
	}

	for (i = 0; i < mips64->num_data_bpoints; i++)
		dbl[i].reg_address = EJTAG64_V25_DBA0 + (0x100 * i);

	mips64->data_break_list = dbl;

	/* clear DBIS reg */
	retval = target_write_u64(target, EJTAG64_V25_DBS, 0);
	if (retval != ERROR_OK)
		return retval;

	return ERROR_OK;
}

int mips64_configure_break_unit(struct target *target)
{
	struct mips64_common *mips64 = target->arch_info;
	uint64_t dcr;
	int retval;

	if (mips64->bp_scanned)
		return ERROR_OK;

	/* get info about breakpoint support */
	retval = target_read_u64(target, EJTAG64_DCR, &dcr);
	if (retval != ERROR_OK)
		return retval;

	if (dcr & EJTAG64_DCR_IB) {
		retval = mips64_configure_i_break_unit(target);
		if (retval != ERROR_OK)
			return retval;
	}

	if (dcr & EJTAG64_DCR_DB) {
		retval = mips64_configure_d_break_unit(target);
		if (retval != ERROR_OK)
			return retval;
	}

	LOG_DEBUG("DCR 0x%" PRIx64 " numinst %i numdata %i", dcr,
		  mips64->num_inst_bpoints, mips64->num_data_bpoints);

	mips64->bp_scanned = true;

	return ERROR_OK;
}

int mips64_enable_interrupts(struct target *target, bool enable)
{
	int retval;
	bool update = false;
	uint64_t dcr;

	/* read debug control register */
	retval = target_read_u64(target, EJTAG64_DCR, &dcr);
	if (retval != ERROR_OK)
		return retval;

	if (enable) {
		if (!(dcr & EJTAG64_DCR_INTE)) {
			/* enable interrupts */
			dcr |= EJTAG64_DCR_INTE;
			update = true;
		}
	} else {
		if (dcr & EJTAG64_DCR_INTE) {
			/* disable interrupts */
			dcr &= ~(uint64_t)EJTAG64_DCR_INTE;
			update = true;
		}
	}

	if (update) {
		retval = target_write_u64(target, EJTAG64_DCR, dcr);
		if (retval != ERROR_OK)
			return retval;
	}

	return ERROR_OK;
}