]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/rx/mem.c
sim/rx: avoid pointer arithmetic on void * pointers
[thirdparty/binutils-gdb.git] / sim / rx / mem.c
CommitLineData
4f8d4a38
DD
1/* mem.c --- memory for RX simulator.
2
3666a048 3Copyright (C) 2005-2021 Free Software Foundation, Inc.
4f8d4a38
DD
4Contributed by Red Hat, Inc.
5
6This file is part of the GNU simulators.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21/* This slows down the simulator and we get some false negatives from
22 gcc, like when it uses a long-sized hole to hold a byte-sized
23 variable, knowing that it doesn't care about the other bits. But,
24 if you need to track down a read-from-unitialized bug, set this to
25 1. */
26#define RDCHECK 0
27
93378652 28#include "config.h"
4f8d4a38
DD
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
f9c7014e 33#include "opcode/rx.h"
4f8d4a38
DD
34#include "mem.h"
35#include "cpu.h"
36#include "syscalls.h"
37#include "misc.h"
38#include "err.h"
39
40#define L1_BITS (10)
41#define L2_BITS (10)
93378652 42#define OFF_BITS PAGE_BITS
4f8d4a38
DD
43
44#define L1_LEN (1 << L1_BITS)
45#define L2_LEN (1 << L2_BITS)
46#define OFF_LEN (1 << OFF_BITS)
47
48static unsigned char **pt[L1_LEN];
49static unsigned char **ptr[L1_LEN];
f9c7014e 50static RX_Opcode_Decoded ***ptdc[L1_LEN];
4f8d4a38
DD
51
52/* [ get=0/put=1 ][ byte size ] */
53static unsigned int mem_counters[2][5];
54
55#define COUNT(isput,bytes) \
56 if (verbose && enable_counting) mem_counters[isput][bytes]++
57
58void
59init_mem (void)
60{
61 int i, j;
62
63 for (i = 0; i < L1_LEN; i++)
64 if (pt[i])
65 {
66 for (j = 0; j < L2_LEN; j++)
67 if (pt[i][j])
68 free (pt[i][j]);
69 free (pt[i]);
70 }
71 memset (pt, 0, sizeof (pt));
72 memset (ptr, 0, sizeof (ptr));
73 memset (mem_counters, 0, sizeof (mem_counters));
74}
75
93378652
DD
76unsigned char *
77rx_mem_ptr (unsigned long address, enum mem_ptr_action action)
4f8d4a38
DD
78{
79 int pt1 = (address >> (L2_BITS + OFF_BITS)) & ((1 << L1_BITS) - 1);
80 int pt2 = (address >> OFF_BITS) & ((1 << L2_BITS) - 1);
81 int pto = address & ((1 << OFF_BITS) - 1);
82
83 if (address == 0)
84 execution_error (SIM_ERR_NULL_POINTER_DEREFERENCE, 0);
85
86 if (pt[pt1] == 0)
87 {
88 pt[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
89 ptr[pt1] = (unsigned char **) calloc (L2_LEN, sizeof (char **));
f9c7014e 90 ptdc[pt1] = (RX_Opcode_Decoded ***) calloc (L2_LEN, sizeof (RX_Opcode_Decoded ***));
4f8d4a38
DD
91 }
92 if (pt[pt1][pt2] == 0)
93 {
94 if (action == MPA_READING)
95 execution_error (SIM_ERR_READ_UNWRITTEN_PAGES, address);
96
f9c7014e
DD
97 pt[pt1][pt2] = (unsigned char *) calloc (OFF_LEN, 1);
98 ptr[pt1][pt2] = (unsigned char *) calloc (OFF_LEN, 1);
99 ptdc[pt1][pt2] = (RX_Opcode_Decoded **) calloc (OFF_LEN, sizeof(RX_Opcode_Decoded *));
4f8d4a38
DD
100 }
101 else if (action == MPA_READING
102 && ptr[pt1][pt2][pto] == MC_UNINIT)
103 execution_error (SIM_ERR_READ_UNWRITTEN_BYTES, address);
104
105 if (action == MPA_WRITING)
106 {
e4dcb664 107 int pto_dc;
4f8d4a38
DD
108 if (ptr[pt1][pt2][pto] == MC_PUSHED_PC)
109 execution_error (SIM_ERR_CORRUPT_STACK, address);
110 ptr[pt1][pt2][pto] = MC_DATA;
e4dcb664
KB
111
112 /* The instruction decoder doesn't store it's decoded instructions
113 at word swapped addresses. Therefore, when clearing the decode
114 cache, we have to account for that here. */
115 pto_dc = pto ^ (rx_big_endian ? 3 : 0);
116 if (ptdc[pt1][pt2][pto_dc])
f9c7014e 117 {
e4dcb664
KB
118 free (ptdc[pt1][pt2][pto_dc]);
119 ptdc[pt1][pt2][pto_dc] = NULL;
f9c7014e 120 }
4f8d4a38
DD
121 }
122
123 if (action == MPA_CONTENT_TYPE)
f9c7014e
DD
124 return (unsigned char *) (ptr[pt1][pt2] + pto);
125
126 if (action == MPA_DECODE_CACHE)
127 return (unsigned char *) (ptdc[pt1][pt2] + pto);
4f8d4a38
DD
128
129 return pt[pt1][pt2] + pto;
130}
131
f9c7014e
DD
132RX_Opcode_Decoded **
133rx_mem_decode_cache (unsigned long address)
134{
135 return (RX_Opcode_Decoded **) rx_mem_ptr (address, MPA_DECODE_CACHE);
136}
137
4f8d4a38
DD
138static inline int
139is_reserved_address (unsigned int address)
140{
141 return (address >= 0x00020000 && address < 0x00080000)
142 || (address >= 0x00100000 && address < 0x01000000)
143 || (address >= 0x08000000 && address < 0xff000000);
144}
145
146static void
147used (int rstart, int i, int j)
148{
149 int rend = i << (L2_BITS + OFF_BITS);
150 rend += j << OFF_BITS;
151 if (rstart == 0xe0000 && rend == 0xe1000)
152 return;
153 printf ("mem: %08x - %08x (%dk bytes)\n", rstart, rend - 1,
154 (rend - rstart) / 1024);
155}
156
157static char *
158mcs (int isput, int bytes)
159{
160 return comma (mem_counters[isput][bytes]);
161}
162
163void
1c3e93a4 164mem_usage_stats (void)
4f8d4a38
DD
165{
166 int i, j;
167 int rstart = 0;
168 int pending = 0;
169
170 for (i = 0; i < L1_LEN; i++)
171 if (pt[i])
172 {
173 for (j = 0; j < L2_LEN; j++)
174 if (pt[i][j])
175 {
176 if (!pending)
177 {
178 pending = 1;
179 rstart = (i << (L2_BITS + OFF_BITS)) + (j << OFF_BITS);
180 }
181 }
182 else if (pending)
183 {
184 pending = 0;
185 used (rstart, i, j);
186 }
187 }
188 else
189 {
190 if (pending)
191 {
192 pending = 0;
193 used (rstart, i, 0);
194 }
195 }
196 /* mem foo: 123456789012 123456789012 123456789012 123456789012
197 123456789012 */
198 printf (" byte short 3byte long"
199 " opcode\n");
200 if (verbose > 1)
201 {
202 /* Only use comma separated numbers when being very verbose.
203 Comma separated numbers are hard to parse in awk scripts. */
204 printf ("mem get: %12s %12s %12s %12s %12s\n", mcs (0, 1), mcs (0, 2),
205 mcs (0, 3), mcs (0, 4), mcs (0, 0));
206 printf ("mem put: %12s %12s %12s %12s\n", mcs (1, 1), mcs (1, 2),
207 mcs (1, 3), mcs (1, 4));
208 }
209 else
210 {
211 printf ("mem get: %12u %12u %12u %12u %12u\n",
212 mem_counters[0][1], mem_counters[0][2],
213 mem_counters[0][3], mem_counters[0][4],
214 mem_counters[0][0]);
215 printf ("mem put: %12u %12u %12u %12u\n",
216 mem_counters [1][1], mem_counters [1][2],
217 mem_counters [1][3], mem_counters [1][4]);
218 }
219}
220
221unsigned long
222mem_usage_cycles (void)
223{
224 unsigned long rv = mem_counters[0][0];
225 rv += mem_counters[0][1] * 1;
226 rv += mem_counters[0][2] * 2;
227 rv += mem_counters[0][3] * 3;
228 rv += mem_counters[0][4] * 4;
229 rv += mem_counters[1][1] * 1;
230 rv += mem_counters[1][2] * 2;
231 rv += mem_counters[1][3] * 3;
232 rv += mem_counters[1][4] * 4;
233 return rv;
234}
235
236static int tpr = 0;
237static void
238s (int address, char *dir)
239{
240 if (tpr == 0)
241 printf ("MEM[%08x] %s", address, dir);
242 tpr++;
243}
244
245#define S(d) if (trace) s(address, d)
246static void
1c3e93a4 247e (void)
4f8d4a38
DD
248{
249 if (!trace)
250 return;
251 tpr--;
252 if (tpr == 0)
253 printf ("\n");
254}
255
256static char
257mtypec (int address)
258{
93378652 259 unsigned char *cp = rx_mem_ptr (address, MPA_CONTENT_TYPE);
4f8d4a38
DD
260 return "udp"[*cp];
261}
262
263#define E() if (trace) e()
264
73d4725f 265static void
4f8d4a38
DD
266mem_put_byte (unsigned int address, unsigned char value)
267{
268 unsigned char *m;
269 char tc = ' ';
270
271 if (trace)
272 tc = mtypec (address);
93378652 273 m = rx_mem_ptr (address, MPA_WRITING);
4f8d4a38
DD
274 if (trace)
275 printf (" %02x%c", value, tc);
276 *m = value;
277 switch (address)
278 {
93378652
DD
279 case 0x0008c02a: /* PA.DR */
280 {
4f8d4a38 281 static int old_led = -1;
93378652 282 int red_on = 0;
4f8d4a38 283 int i;
93378652 284
4f8d4a38
DD
285 if (old_led != value)
286 {
93378652
DD
287 fputs (" ", stdout);
288 for (i = 0; i < 8; i++)
4f8d4a38 289 if (value & (1 << i))
93378652
DD
290 {
291 if (! red_on)
292 {
293 fputs ("\033[31m", stdout);
294 red_on = 1;
295 }
296 fputs (" @", stdout);
297 }
4f8d4a38 298 else
93378652
DD
299 {
300 if (red_on)
301 {
302 fputs ("\033[0m", stdout);
303 red_on = 0;
304 }
305 fputs (" *", stdout);
306 }
307
308 if (red_on)
309 fputs ("\033[0m", stdout);
310
311 fputs ("\r", stdout);
4f8d4a38
DD
312 fflush (stdout);
313 old_led = value;
314 }
315 }
316 break;
317
93378652
DD
318#ifdef CYCLE_STATS
319 case 0x0008c02b: /* PB.DR */
4f8d4a38 320 {
4f8d4a38 321 if (value == 0)
93378652
DD
322 halt_pipeline_stats ();
323 else
324 reset_pipeline_stats ();
325 }
326#endif
327
328 case 0x00088263: /* SCI4.TDR */
329 {
330 static int pending_exit = 0;
331 if (pending_exit == 2)
4f8d4a38 332 {
93378652
DD
333 step_result = RX_MAKE_EXITED(value);
334 longjmp (decode_jmp_buf, 1);
4f8d4a38 335 }
93378652
DD
336 else if (value == 3)
337 pending_exit ++;
4f8d4a38 338 else
93378652
DD
339 pending_exit = 0;
340
341 putchar(value);
4f8d4a38
DD
342 }
343 break;
344
345 default:
346 if (is_reserved_address (address))
347 generate_access_exception ();
348 }
349}
350
351void
352mem_put_qi (int address, unsigned char value)
353{
354 S ("<=");
355 mem_put_byte (address, value & 0xff);
356 E ();
357 COUNT (1, 1);
358}
359
f9c7014e 360#ifdef CYCLE_ACCURATE
93378652 361static int tpu_base;
f9c7014e 362#endif
93378652 363
4f8d4a38
DD
364void
365mem_put_hi (int address, unsigned short value)
366{
367 S ("<=");
93378652 368 switch (address)
4f8d4a38 369 {
93378652
DD
370#ifdef CYCLE_ACCURATE
371 case 0x00088126: /* TPU1.TCNT */
372 tpu_base = regs.cycle_count;
373 break;
374 case 0x00088136: /* TPU2.TCNT */
375 tpu_base = regs.cycle_count;
376 break;
377#endif
378 default:
379 if (rx_big_endian)
380 {
381 mem_put_byte (address, value >> 8);
382 mem_put_byte (address + 1, value & 0xff);
383 }
384 else
385 {
386 mem_put_byte (address, value & 0xff);
387 mem_put_byte (address + 1, value >> 8);
388 }
4f8d4a38
DD
389 }
390 E ();
391 COUNT (1, 2);
392}
393
394void
395mem_put_psi (int address, unsigned long value)
396{
397 S ("<=");
398 if (rx_big_endian)
399 {
400 mem_put_byte (address, value >> 16);
401 mem_put_byte (address + 1, (value >> 8) & 0xff);
402 mem_put_byte (address + 2, value & 0xff);
403 }
404 else
405 {
406 mem_put_byte (address, value & 0xff);
407 mem_put_byte (address + 1, (value >> 8) & 0xff);
408 mem_put_byte (address + 2, value >> 16);
409 }
410 E ();
411 COUNT (1, 3);
412}
413
414void
415mem_put_si (int address, unsigned long value)
416{
417 S ("<=");
418 if (rx_big_endian)
419 {
420 mem_put_byte (address + 0, (value >> 24) & 0xff);
421 mem_put_byte (address + 1, (value >> 16) & 0xff);
422 mem_put_byte (address + 2, (value >> 8) & 0xff);
423 mem_put_byte (address + 3, value & 0xff);
424 }
425 else
426 {
427 mem_put_byte (address + 0, value & 0xff);
428 mem_put_byte (address + 1, (value >> 8) & 0xff);
429 mem_put_byte (address + 2, (value >> 16) & 0xff);
430 mem_put_byte (address + 3, (value >> 24) & 0xff);
431 }
432 E ();
433 COUNT (1, 4);
434}
435
436void
da9ecd60 437mem_put_blk (int address, void *bufptr_void, int nbytes)
4f8d4a38 438{
da9ecd60
AB
439 unsigned char *bufptr = (unsigned char *) bufptr_void;
440
4f8d4a38
DD
441 S ("<=");
442 if (enable_counting)
443 mem_counters[1][1] += nbytes;
444 while (nbytes--)
da9ecd60 445 mem_put_byte (address++, *bufptr++);
4f8d4a38
DD
446 E ();
447}
448
449unsigned char
450mem_get_pc (int address)
451{
93378652 452 unsigned char *m = rx_mem_ptr (address, MPA_READING);
4f8d4a38
DD
453 COUNT (0, 0);
454 return *m;
455}
456
457static unsigned char
458mem_get_byte (unsigned int address)
459{
460 unsigned char *m;
461
462 S ("=>");
93378652 463 m = rx_mem_ptr (address, MPA_READING);
4f8d4a38
DD
464 switch (address)
465 {
93378652 466 case 0x00088264: /* SCI4.SSR */
4f8d4a38 467 E();
93378652 468 return 0x04; /* transmitter empty */
4f8d4a38
DD
469 break;
470 default:
471 if (trace)
472 printf (" %02x%c", *m, mtypec (address));
473 if (is_reserved_address (address))
474 generate_access_exception ();
475 break;
476 }
477 E ();
478 return *m;
479}
480
481unsigned char
482mem_get_qi (int address)
483{
484 unsigned char rv;
485 S ("=>");
486 rv = mem_get_byte (address);
487 COUNT (0, 1);
488 E ();
489 return rv;
490}
491
492unsigned short
493mem_get_hi (int address)
494{
495 unsigned short rv;
496 S ("=>");
93378652 497 switch (address)
4f8d4a38 498 {
93378652
DD
499#ifdef CYCLE_ACCURATE
500 case 0x00088126: /* TPU1.TCNT */
501 rv = (regs.cycle_count - tpu_base) >> 16;
502 break;
503 case 0x00088136: /* TPU2.TCNT */
504 rv = (regs.cycle_count - tpu_base) >> 0;
505 break;
506#endif
507
508 default:
509 if (rx_big_endian)
510 {
511 rv = mem_get_byte (address) << 8;
512 rv |= mem_get_byte (address + 1);
513 }
514 else
515 {
516 rv = mem_get_byte (address);
517 rv |= mem_get_byte (address + 1) << 8;
518 }
4f8d4a38
DD
519 }
520 COUNT (0, 2);
521 E ();
522 return rv;
523}
524
525unsigned long
526mem_get_psi (int address)
527{
528 unsigned long rv;
529 S ("=>");
530 if (rx_big_endian)
531 {
532 rv = mem_get_byte (address + 2);
533 rv |= mem_get_byte (address + 1) << 8;
534 rv |= mem_get_byte (address) << 16;
535 }
536 else
537 {
538 rv = mem_get_byte (address);
539 rv |= mem_get_byte (address + 1) << 8;
540 rv |= mem_get_byte (address + 2) << 16;
541 }
542 COUNT (0, 3);
543 E ();
544 return rv;
545}
546
547unsigned long
548mem_get_si (int address)
549{
550 unsigned long rv;
551 S ("=>");
552 if (rx_big_endian)
553 {
554 rv = mem_get_byte (address + 3);
555 rv |= mem_get_byte (address + 2) << 8;
556 rv |= mem_get_byte (address + 1) << 16;
557 rv |= mem_get_byte (address) << 24;
558 }
559 else
560 {
561 rv = mem_get_byte (address);
562 rv |= mem_get_byte (address + 1) << 8;
563 rv |= mem_get_byte (address + 2) << 16;
564 rv |= mem_get_byte (address + 3) << 24;
565 }
566 COUNT (0, 4);
567 E ();
568 return rv;
569}
570
571void
da9ecd60 572mem_get_blk (int address, void *bufptr_void, int nbytes)
4f8d4a38 573{
da9ecd60
AB
574 char *bufptr = (char *) bufptr_void;
575
4f8d4a38
DD
576 S ("=>");
577 if (enable_counting)
578 mem_counters[0][1] += nbytes;
579 while (nbytes--)
da9ecd60 580 *bufptr++ = mem_get_byte (address++);
4f8d4a38
DD
581 E ();
582}
583
584int
585sign_ext (int v, int bits)
586{
587 if (bits < 32)
588 {
589 v &= (1 << bits) - 1;
590 if (v & (1 << (bits - 1)))
591 v -= (1 << bits);
592 }
593 return v;
594}
595
596void
597mem_set_content_type (int address, enum mem_content_type type)
598{
93378652 599 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);
4f8d4a38
DD
600 *mt = type;
601}
602
603void
604mem_set_content_range (int start_address, int end_address, enum mem_content_type type)
605{
606 while (start_address < end_address)
607 {
608 int sz, ofs;
609 unsigned char *mt;
610
611 sz = end_address - start_address;
612 ofs = start_address % L1_LEN;
613 if (sz + ofs > L1_LEN)
614 sz = L1_LEN - ofs;
615
93378652 616 mt = rx_mem_ptr (start_address, MPA_CONTENT_TYPE);
4f8d4a38
DD
617 memset (mt, type, sz);
618
619 start_address += sz;
620 }
621}
622
623enum mem_content_type
624mem_get_content_type (int address)
625{
93378652 626 unsigned char *mt = rx_mem_ptr (address, MPA_CONTENT_TYPE);
4f8d4a38
DD
627 return *mt;
628}