1 
2 /*
3  * Ovlymgr.c -- Runtime Overlay Manager for the GDB testsuite.
4  */
5 
6 #include "ovlymgr.h"
7 #include <string.h>
8 #include <stdlib.h>
9 
10 /* Local functions and data: */
11 
12 extern unsigned long _ovly_table[][4];
13 extern unsigned long _novlys __attribute__ ((section (".data")));
14 enum ovly_index { VMA, SIZE, LMA, MAPPED};
15 
16 static void ovly_copy (unsigned long dst, unsigned long src, long size);
17 
18 /* Flush the data and instruction caches at address START for SIZE bytes.
19    Support for each new port must be added here.  */
20 /* FIXME: Might be better to have a standard libgloss function that
21    ports provide that we can then use.  Use libgloss instead of newlib
22    since libgloss is the one intended to handle low level system issues.
23    I would suggest something like _flush_cache to avoid the user's namespace
24    but not be completely obscure as other things may need this facility.  */
25 
26 static void
FlushCache(void)27 FlushCache (void)
28 {
29 #ifdef __M32R__
30   volatile char *mspr = (char *) 0xfffffff7;
31   *mspr = 1;
32 #endif
33 }
34 
35 /* _ovly_debug_event:
36  * Debuggers may set a breakpoint here, to be notified
37  * when the overlay table has been modified.
38  */
39 static void
_ovly_debug_event(void)40 _ovly_debug_event (void)
41 {
42 }
43 
44 /* OverlayLoad:
45  * Copy the overlay into its runtime region,
46  * and mark the overlay as "mapped".
47  */
48 
49 bool
OverlayLoad(unsigned long ovlyno)50 OverlayLoad (unsigned long ovlyno)
51 {
52   unsigned long i;
53 
54   if (ovlyno < 0 || ovlyno >= _novlys)
55     exit (-1);	/* fail, bad ovly number */
56 
57   if (_ovly_table[ovlyno][MAPPED])
58     return TRUE;	/* this overlay already mapped -- nothing to do! */
59 
60   for (i = 0; i < _novlys; i++)
61     if (i == ovlyno)
62       _ovly_table[i][MAPPED] = 1;	/* this one now mapped */
63     else if (_ovly_table[i][VMA] == _ovly_table[ovlyno][VMA])
64       _ovly_table[i][MAPPED] = 0;	/* this one now un-mapped */
65 
66   ovly_copy (_ovly_table[ovlyno][VMA],
67 	     _ovly_table[ovlyno][LMA],
68 	     _ovly_table[ovlyno][SIZE]);
69 
70   FlushCache ();
71   _ovly_debug_event ();
72   return TRUE;
73 }
74 
75 /* OverlayUnload:
76  * Copy the overlay back into its "load" region.
77  * Does NOT mark overlay as "unmapped", therefore may be called
78  * more than once for the same mapped overlay.
79  */
80 
81 bool
OverlayUnload(unsigned long ovlyno)82 OverlayUnload (unsigned long ovlyno)
83 {
84   if (ovlyno < 0 || ovlyno >= _novlys)
85     exit (-1);  /* fail, bad ovly number */
86 
87   if (!_ovly_table[ovlyno][MAPPED])
88     exit (-1);  /* error, can't copy out a segment that's not "in" */
89 
90   ovly_copy (_ovly_table[ovlyno][LMA],
91 	     _ovly_table[ovlyno][VMA],
92 	     _ovly_table[ovlyno][SIZE]);
93 
94   _ovly_debug_event ();
95   return TRUE;
96 }
97 
98 static void
ovly_copy(unsigned long dst,unsigned long src,long size)99 ovly_copy (unsigned long dst, unsigned long src, long size)
100 {
101   memcpy ((void *) dst, (void *) src, size);
102 }
103