1 /* flushcache.c
2  * Copyright 1984-2017 Cisco Systems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "system.h"
18 
19 #ifdef FLUSHCACHE
20 typedef struct {
21   uptr start;
22   uptr end;
23 } mod_range;
24 
25 #define mod_range_start(x) (((mod_range *)&BVIT(x,0))->start)
26 #define mod_range_end(x) (((mod_range *)&BVIT(x,0))->end)
27 
28 static uptr max_gap;
29 
30 static ptr make_mod_range PROTO((uptr start, uptr end));
31 
make_mod_range(uptr start,uptr end)32 static ptr make_mod_range(uptr start, uptr end) {
33   ptr bv = S_bytevector(sizeof(mod_range));
34   mod_range_start(bv) = start;
35   mod_range_end(bv) = end;
36   return bv;
37 }
38 
39 /* we record info per thread so flush in one prematurely for another doesn't prevent
40    the other from doing its own flush...and also since it's not clear that flushing in one
41    actually syncs caches across cores & processors */
42 
S_record_code_mod(ptr tc,uptr addr,uptr bytes)43 void S_record_code_mod(ptr tc, uptr addr, uptr bytes) {
44   uptr end = addr + bytes;
45   ptr ls = CODERANGESTOFLUSH(tc);
46 
47   if (ls != Snil) {
48     ptr last_mod = Scar(ls);
49     uptr last_end = mod_range_end(last_mod);
50     if (addr > last_end && addr - last_end < max_gap) {
51 #ifdef DEBUG
52       printf("  record_code_mod merging %x %x and %x %x\n", mod_range_start(last_mod), last_end, addr, end); fflush(stdout);
53 #endif
54       mod_range_end(last_mod) = end;
55       return;
56     }
57   }
58 
59 #ifdef DEBUG
60       printf("  record_code_mod new range %x to %x\n", addr, end); fflush(stdout);
61 #endif
62   CODERANGESTOFLUSH(tc) = S_cons_in(space_new, 0, make_mod_range(addr, end), ls);
63   return;
64 }
65 
S_flush_instruction_cache(ptr tc)66 extern void S_flush_instruction_cache(ptr tc) {
67   ptr ls;
68 
69   for (ls = CODERANGESTOFLUSH(tc); ls != Snil; ls = Scdr(ls)) {
70     S_doflush(mod_range_start(Scar(ls)), mod_range_end(Scar(ls)));
71   }
72   CODERANGESTOFLUSH(tc) = Snil;
73 }
74 
S_flushcache_init()75 extern void S_flushcache_init() {
76   if (S_boot_time) {
77     max_gap = S_flushcache_max_gap();
78     if (max_gap < (uptr)(code_data_disp + byte_alignment)) {
79       max_gap = (uptr)(code_data_disp + byte_alignment);
80     }
81   }
82 }
83 #else /* FLUSHCACHE */
84 
S_record_code_mod(UNUSED ptr tc,UNUSED uptr addr,UNUSED uptr bytes)85 extern void S_record_code_mod(UNUSED ptr tc, UNUSED uptr addr, UNUSED uptr bytes) {}
S_flush_instruction_cache(UNUSED ptr tc)86 extern void S_flush_instruction_cache(UNUSED ptr tc) {}
S_flushcache_init()87 extern void S_flushcache_init() { return; }
88 
89 #endif /* FLUSHCACHE */
90