blob: 38300079a1e7fd4529885894babbb6a420513bd2 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/*
2 * Host code generation
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
David 'Digit' Turner2910f182010-05-10 18:48:35 -070017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080018 */
19#include <stdarg.h>
20#include <stdlib.h>
21#include <stdio.h>
22#include <string.h>
23#include <inttypes.h>
24
25#include "config.h"
26
27#define NO_CPU_IO_DEFS
28#include "cpu.h"
29#include "exec-all.h"
30#include "disas.h"
31#include "tcg.h"
David Turner6a9ef172010-09-09 22:54:36 +020032#include "qemu-timer.h"
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080033
34/* code generation context */
35TCGContext tcg_ctx;
36
37uint16_t gen_opc_buf[OPC_BUF_SIZE];
38TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
39
40target_ulong gen_opc_pc[OPC_BUF_SIZE];
41uint16_t gen_opc_icount[OPC_BUF_SIZE];
42uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
43#if defined(TARGET_I386)
44uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
45#elif defined(TARGET_SPARC)
46target_ulong gen_opc_npc[OPC_BUF_SIZE];
47target_ulong gen_opc_jump_pc[2];
48#elif defined(TARGET_MIPS) || defined(TARGET_SH4)
49uint32_t gen_opc_hflags[OPC_BUF_SIZE];
50#endif
51
Vladimir Chtchetkine5389aa12010-02-16 10:38:35 -080052#ifdef CONFIG_MEMCHECK
53/*
54 * Memchecker code in this module copies TB PC <-> Guest PC map to the TB
55 * descriptor after guest code has been translated in cpu_gen_init routine.
56 */
57#include "memcheck/memcheck_api.h"
58
59/* Array of (tb_pc, guest_pc) pairs, big enough for all translations. This
60 * array is used to obtain guest PC address from a translated PC address.
61 * tcg_gen_code_common will fill it up when memchecker is enabled. */
David 'Digit' Turnerd9b6cb92010-10-20 19:07:28 +020062static void* gen_opc_tpc2gpc[OPC_BUF_SIZE * 2];
63void** gen_opc_tpc2gpc_ptr = &gen_opc_tpc2gpc[0];
Vladimir Chtchetkine5389aa12010-02-16 10:38:35 -080064/* Number of (tb_pc, guest_pc) pairs stored in gen_opc_tpc2gpc array. */
65unsigned int gen_opc_tpc2gpc_pairs;
66#endif // CONFIG_MEMCHECK
67
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080068/* XXX: suppress that */
69unsigned long code_gen_max_block_size(void)
70{
71 static unsigned long max;
72
73 if (max == 0) {
74 max = TCG_MAX_OP_SIZE;
75#define DEF(s, n, copy_size) max = copy_size > max? copy_size : max;
76#include "tcg-opc.h"
77#undef DEF
78 max *= OPC_MAX_SIZE;
79 }
80
81 return max;
82}
83
84void cpu_gen_init(void)
85{
Vladimir Chtchetkine5389aa12010-02-16 10:38:35 -080086 tcg_context_init(&tcg_ctx);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080087 tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
88 CPU_TEMP_BUF_NLONGS * sizeof(long));
89}
90
91/* return non zero if the very first instruction is invalid so that
92 the virtual CPU can trigger an exception.
93
94 '*gen_code_size_ptr' contains the size of the generated code (host
95 code).
96*/
97int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
98{
99 TCGContext *s = &tcg_ctx;
100 uint8_t *gen_code_buf;
101 int gen_code_size;
102#ifdef CONFIG_PROFILER
103 int64_t ti;
104#endif
105
106#ifdef CONFIG_PROFILER
107 s->tb_count1++; /* includes aborted translations because of
108 exceptions */
109 ti = profile_getclock();
110#endif
111 tcg_func_start(s);
112
113 gen_intermediate_code(env, tb);
114
115 /* generate machine code */
116 gen_code_buf = tb->tc_ptr;
117 tb->tb_next_offset[0] = 0xffff;
118 tb->tb_next_offset[1] = 0xffff;
119 s->tb_next_offset = tb->tb_next_offset;
120#ifdef USE_DIRECT_JUMP
121 s->tb_jmp_offset = tb->tb_jmp_offset;
122 s->tb_next = NULL;
123 /* the following two entries are optional (only used for string ops) */
124 /* XXX: not used ? */
125 tb->tb_jmp_offset[2] = 0xffff;
126 tb->tb_jmp_offset[3] = 0xffff;
127#else
128 s->tb_jmp_offset = NULL;
129 s->tb_next = tb->tb_next;
130#endif
131
132#ifdef CONFIG_PROFILER
133 s->tb_count++;
134 s->interm_time += profile_getclock() - ti;
135 s->code_time -= profile_getclock();
136#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700137 gen_code_size = tcg_gen_code(s, gen_code_buf);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800138 *gen_code_size_ptr = gen_code_size;
139#ifdef CONFIG_PROFILER
140 s->code_time += profile_getclock();
141 s->code_in_len += tb->size;
142 s->code_out_len += gen_code_size;
143#endif
144
Vladimir Chtchetkine5389aa12010-02-16 10:38:35 -0800145#ifdef CONFIG_MEMCHECK
146 /* Save translated PC -> guest PC map into TB. */
147 if (memcheck_enabled && gen_opc_tpc2gpc_pairs && is_cpu_user(env)) {
148 tb->tpc2gpc =
149 qemu_malloc(gen_opc_tpc2gpc_pairs * 2 * sizeof(target_ulong));
150 if (tb->tpc2gpc != NULL) {
151 memcpy(tb->tpc2gpc, gen_opc_tpc2gpc_ptr,
152 gen_opc_tpc2gpc_pairs * 2 * sizeof(target_ulong));
153 tb->tpc2gpc_pairs = gen_opc_tpc2gpc_pairs;
154 }
155 }
156#endif // CONFIG_MEMCHECK
157
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800158#ifdef DEBUG_DISAS
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700159 if (qemu_loglevel_mask(CPU_LOG_TB_OUT_ASM)) {
160 qemu_log("OUT: [size=%d]\n", *gen_code_size_ptr);
161 log_disas(tb->tc_ptr, *gen_code_size_ptr);
162 qemu_log("\n");
163 qemu_log_flush();
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800164 }
165#endif
166 return 0;
167}
168
169/* The cpu state corresponding to 'searched_pc' is restored.
170 */
171int cpu_restore_state(TranslationBlock *tb,
172 CPUState *env, unsigned long searched_pc,
173 void *puc)
174{
175 TCGContext *s = &tcg_ctx;
176 int j;
177 unsigned long tc_ptr;
178#ifdef CONFIG_PROFILER
179 int64_t ti;
180#endif
181
182#ifdef CONFIG_PROFILER
183 ti = profile_getclock();
184#endif
185 tcg_func_start(s);
186
187 gen_intermediate_code_pc(env, tb);
188
189 if (use_icount) {
190 /* Reset the cycle counter to the start of the block. */
191 env->icount_decr.u16.low += tb->icount;
192 /* Clear the IO flag. */
193 env->can_do_io = 0;
194 }
195
196 /* find opc index corresponding to search_pc */
197 tc_ptr = (unsigned long)tb->tc_ptr;
198 if (searched_pc < tc_ptr)
199 return -1;
200
201 s->tb_next_offset = tb->tb_next_offset;
202#ifdef USE_DIRECT_JUMP
203 s->tb_jmp_offset = tb->tb_jmp_offset;
204 s->tb_next = NULL;
205#else
206 s->tb_jmp_offset = NULL;
207 s->tb_next = tb->tb_next;
208#endif
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700209 j = tcg_gen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -0800210 if (j < 0)
211 return -1;
212 /* now find start of instruction before */
213 while (gen_opc_instr_start[j] == 0)
214 j--;
215 env->icount_decr.u16.low -= gen_opc_icount[j];
216
217 gen_pc_load(env, tb, searched_pc, j, puc);
218
219#ifdef CONFIG_PROFILER
220 s->restore_time += profile_getclock() - ti;
221 s->restore_count++;
222#endif
223 return 0;
224}