1 /* Copyright (C) 1992-2024 Free Software Foundation, Inc.
2 
3    This file is part of GDB.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17 
18 #include "target-dcache.h"
19 #include "progspace.h"
20 #include "cli/cli-cmds.h"
21 
22 /* The target dcache is kept per-address-space.  This key lets us
23    associate the cache with the address space.  */
24 
25 static const registry<address_space>::key<DCACHE, dcache_deleter>
26   target_dcache_aspace_key;
27 
28 /* Target dcache is initialized or not.  */
29 
30 int
target_dcache_init_p(address_space_ref_ptr aspace)31 target_dcache_init_p (address_space_ref_ptr aspace)
32 {
33   DCACHE *dcache
34     = target_dcache_aspace_key.get (aspace.get ());
35 
36   return (dcache != NULL);
37 }
38 
39 /* Invalidate the target dcache.  */
40 
41 void
target_dcache_invalidate(address_space_ref_ptr aspace)42 target_dcache_invalidate (address_space_ref_ptr aspace)
43 {
44   DCACHE *dcache
45     = target_dcache_aspace_key.get (aspace.get ());
46 
47   if (dcache != NULL)
48     dcache_invalidate (dcache);
49 }
50 
51 /* Return the target dcache.  Return NULL if target dcache is not
52    initialized yet.  */
53 
54 DCACHE *
target_dcache_get(address_space_ref_ptr aspace)55 target_dcache_get (address_space_ref_ptr aspace)
56 {
57   return target_dcache_aspace_key.get (aspace.get ());
58 }
59 
60 /* Return the target dcache.  If it is not initialized yet, initialize
61    it.  */
62 
63 DCACHE *
target_dcache_get_or_init(address_space_ref_ptr aspace)64 target_dcache_get_or_init (address_space_ref_ptr aspace)
65 {
66   DCACHE *dcache
67     = target_dcache_aspace_key.get (aspace.get ());
68 
69   if (dcache == NULL)
70     {
71       dcache = dcache_init ();
72       target_dcache_aspace_key.set (aspace.get (), dcache);
73     }
74 
75   return dcache;
76 }
77 
78 /* The option sets this.  */
79 static bool stack_cache_enabled_1 = true;
80 /* And set_stack_cache updates this.
81    The reason for the separation is so that we don't flush the cache for
82    on->on transitions.  */
83 static int stack_cache_enabled = 1;
84 
85 /* This is called *after* the stack-cache has been set.
86    Flush the cache for off->on and on->off transitions.
87    There's no real need to flush the cache for on->off transitions,
88    except cleanliness.  */
89 
90 static void
set_stack_cache(const char * args,int from_tty,struct cmd_list_element * c)91 set_stack_cache (const char *args, int from_tty, struct cmd_list_element *c)
92 {
93   if (stack_cache_enabled != stack_cache_enabled_1)
94     target_dcache_invalidate (current_program_space->aspace);
95 
96   stack_cache_enabled = stack_cache_enabled_1;
97 }
98 
99 static void
show_stack_cache(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)100 show_stack_cache (struct ui_file *file, int from_tty,
101                       struct cmd_list_element *c, const char *value)
102 {
103   gdb_printf (file, _("Cache use for stack accesses is %s.\n"), value);
104 }
105 
106 /* Return true if "stack cache" is enabled, otherwise, return false.  */
107 
108 int
stack_cache_enabled_p(void)109 stack_cache_enabled_p (void)
110 {
111   return stack_cache_enabled;
112 }
113 
114 /* The option sets this.  */
115 
116 static bool code_cache_enabled_1 = true;
117 
118 /* And set_code_cache updates this.
119    The reason for the separation is so that we don't flush the cache for
120    on->on transitions.  */
121 static int code_cache_enabled = 1;
122 
123 /* This is called *after* the code-cache has been set.
124    Flush the cache for off->on and on->off transitions.
125    There's no real need to flush the cache for on->off transitions,
126    except cleanliness.  */
127 
128 static void
set_code_cache(const char * args,int from_tty,struct cmd_list_element * c)129 set_code_cache (const char *args, int from_tty, struct cmd_list_element *c)
130 {
131   if (code_cache_enabled != code_cache_enabled_1)
132     target_dcache_invalidate (current_program_space->aspace);
133 
134   code_cache_enabled = code_cache_enabled_1;
135 }
136 
137 /* Show option "code-cache".  */
138 
139 static void
show_code_cache(struct ui_file * file,int from_tty,struct cmd_list_element * c,const char * value)140 show_code_cache (struct ui_file *file, int from_tty,
141                      struct cmd_list_element *c, const char *value)
142 {
143   gdb_printf (file, _("Cache use for code accesses is %s.\n"), value);
144 }
145 
146 /* Return true if "code cache" is enabled, otherwise, return false.  */
147 
148 int
code_cache_enabled_p(void)149 code_cache_enabled_p (void)
150 {
151   return code_cache_enabled;
152 }
153 
154 /* Implement the 'maint flush dcache' command.  */
155 
156 static void
maint_flush_dcache_command(const char * command,int from_tty)157 maint_flush_dcache_command (const char *command, int from_tty)
158 {
159   target_dcache_invalidate (current_program_space->aspace);
160   if (from_tty)
161     gdb_printf (_("The dcache was flushed.\n"));
162 }
163 
164 void _initialize_target_dcache ();
165 void
_initialize_target_dcache()166 _initialize_target_dcache ()
167 {
168   add_setshow_boolean_cmd ("stack-cache", class_support,
169                                  &stack_cache_enabled_1, _("\
170 Set cache use for stack access."), _("\
171 Show cache use for stack access."), _("\
172 When on, use the target memory cache for all stack access, regardless of any\n\
173 configured memory regions.  This improves remote performance significantly.\n\
174 By default, caching for stack access is on."),
175                                  set_stack_cache,
176                                  show_stack_cache,
177                                  &setlist, &showlist);
178 
179   add_setshow_boolean_cmd ("code-cache", class_support,
180                                  &code_cache_enabled_1, _("\
181 Set cache use for code segment access."), _("\
182 Show cache use for code segment access."), _("\
183 When on, use the target memory cache for all code segment accesses,\n\
184 regardless of any configured memory regions.  This improves remote\n\
185 performance significantly.  By default, caching for code segment\n\
186 access is on."),
187                                  set_code_cache,
188                                  show_code_cache,
189                                  &setlist, &showlist);
190 
191   add_cmd ("dcache", class_maintenance, maint_flush_dcache_command,
192              _("\
193 Force gdb to flush its target memory data cache.\n\
194 \n\
195 The dcache caches all target memory accesses where possible, this\n\
196 includes the stack-cache and the code-cache."),
197              &maintenanceflushlist);
198 }
199