2 * Copyright (C) 2015-2020 Paul Cercueil <paul@crapouillou.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
15 #include "blockcache.h"
17 #include "lightrec-private.h"
18 #include "memmanager.h"
23 /* Must be power of two */
24 #define LUT_SIZE 0x4000
27 struct lightrec_state *state;
28 struct block * lut[LUT_SIZE];
31 struct block * lightrec_find_block(struct blockcache *cache, u32 pc)
37 for (block = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
38 block; block = block->next)
39 if (kunseg(block->pc) == pc)
45 void remove_from_code_lut(struct blockcache *cache, struct block *block)
47 struct lightrec_state *state = block->state;
48 const struct opcode *op;
49 u32 offset = lut_offset(block->pc);
51 /* Use state->get_next_block in the code LUT, which basically
52 * calls back get_next_block_func(), until the compiler
53 * overrides this. This is required, as a NULL value in the code
54 * LUT means an outdated block. */
55 state->code_lut[offset] = state->get_next_block;
57 for (op = block->opcode_list; op; op = op->next)
58 if (op->c.i.op == OP_META_SYNC)
59 state->code_lut[offset + op->offset] = NULL;
63 void lightrec_register_block(struct blockcache *cache, struct block *block)
65 u32 pc = kunseg(block->pc);
68 old = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
72 cache->lut[(pc >> 2) & (LUT_SIZE - 1)] = block;
74 remove_from_code_lut(cache, block);
77 void lightrec_unregister_block(struct blockcache *cache, struct block *block)
79 u32 pc = kunseg(block->pc);
80 struct block *old = cache->lut[(pc >> 2) & (LUT_SIZE - 1)];
83 cache->lut[(pc >> 2) & (LUT_SIZE - 1)] = old->next;
87 for (; old; old = old->next) {
88 if (old->next == block) {
89 old->next = block->next;
94 pr_err("Block at PC 0x%x is not in cache\n", block->pc);
97 void lightrec_free_block_cache(struct blockcache *cache)
99 struct block *block, *next;
102 for (i = 0; i < LUT_SIZE; i++) {
103 for (block = cache->lut[i]; block; block = next) {
105 lightrec_free_block(block);
109 lightrec_free(cache->state, MEM_FOR_LIGHTREC, sizeof(*cache), cache);
112 struct blockcache * lightrec_blockcache_init(struct lightrec_state *state)
114 struct blockcache *cache;
116 cache = lightrec_calloc(state, MEM_FOR_LIGHTREC, sizeof(*cache));
120 cache->state = state;
125 u32 lightrec_calculate_block_hash(const struct block *block)
127 const struct lightrec_mem_map *map = block->map;
128 u32 pc, hash = 0xffffffff;
132 pc = kunseg(block->pc) - map->pc;
134 while (map->mirror_of)
135 map = map->mirror_of;
137 code = map->address + pc;
139 /* Jenkins one-at-a-time hash algorithm */
140 for (i = 0; i < block->nb_ops; i++) {
142 hash += (hash << 10);
147 hash ^= (hash >> 11);
148 hash += (hash << 15);
153 bool lightrec_block_is_outdated(struct block *block)
155 void **lut_entry = &block->state->code_lut[lut_offset(block->pc)];
161 outdated = block->hash != lightrec_calculate_block_hash(block);
162 if (likely(!outdated)) {
163 /* The block was marked as outdated, but the content is still
166 *lut_entry = block->function;
168 *lut_entry = block->state->get_next_block;