Validate virtio-net TX header
[qemu-kvm/fedora.git] / translate-all.c
blobcf8cc9bff08c6fa5077e63c17735c4678787ff3c
1 /*
2 * Host code generation
4 * Copyright (c) 2003 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 #include <stdarg.h>
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <inttypes.h>
26 #include "config.h"
28 #define NO_CPU_IO_DEFS
29 #include "cpu.h"
30 #include "exec-all.h"
31 #include "disas.h"
32 #include "tcg.h"
34 /* code generation context */
35 TCGContext tcg_ctx;
37 uint16_t gen_opc_buf[OPC_BUF_SIZE];
38 TCGArg gen_opparam_buf[OPPARAM_BUF_SIZE];
40 target_ulong gen_opc_pc[OPC_BUF_SIZE];
41 uint8_t gen_opc_instr_start[OPC_BUF_SIZE];
42 #if defined(TARGET_I386)
43 uint8_t gen_opc_cc_op[OPC_BUF_SIZE];
44 #elif defined(TARGET_SPARC)
45 target_ulong gen_opc_npc[OPC_BUF_SIZE];
46 target_ulong gen_opc_jump_pc[2];
47 #elif defined(TARGET_MIPS) || defined(TARGET_SH4)
48 uint32_t gen_opc_hflags[OPC_BUF_SIZE];
49 #endif
51 int code_copy_enabled = 1;
53 #ifdef CONFIG_PROFILER
54 int64_t dyngen_tb_count1;
55 int64_t dyngen_tb_count;
56 int64_t dyngen_op_count;
57 int64_t dyngen_old_op_count;
58 int64_t dyngen_tcg_del_op_count;
59 int dyngen_op_count_max;
60 int64_t dyngen_code_in_len;
61 int64_t dyngen_code_out_len;
62 int64_t dyngen_interm_time;
63 int64_t dyngen_code_time;
64 int64_t dyngen_restore_count;
65 int64_t dyngen_restore_time;
66 #endif
68 /* XXX: suppress that */
69 unsigned long code_gen_max_block_size(void)
71 static unsigned long max;
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;
81 return max;
84 void cpu_gen_init(void)
86 tcg_context_init(&tcg_ctx);
87 tcg_set_frame(&tcg_ctx, TCG_AREG0, offsetof(CPUState, temp_buf),
88 CPU_TEMP_BUF_NLONGS * sizeof(long));
91 /* return non zero if the very first instruction is invalid so that
92 the virtual CPU can trigger an exception.
94 '*gen_code_size_ptr' contains the size of the generated code (host
95 code).
97 int cpu_gen_code(CPUState *env, TranslationBlock *tb, int *gen_code_size_ptr)
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
106 #ifdef CONFIG_PROFILER
107 dyngen_tb_count1++; /* includes aborted translations because of
108 exceptions */
109 ti = profile_getclock();
110 #endif
111 tcg_func_start(s);
113 if (gen_intermediate_code(env, tb) < 0)
114 return -1;
116 /* generate machine code */
117 gen_code_buf = tb->tc_ptr;
118 tb->tb_next_offset[0] = 0xffff;
119 tb->tb_next_offset[1] = 0xffff;
120 s->tb_next_offset = tb->tb_next_offset;
121 #ifdef USE_DIRECT_JUMP
122 s->tb_jmp_offset = tb->tb_jmp_offset;
123 s->tb_next = NULL;
124 /* the following two entries are optional (only used for string ops) */
125 /* XXX: not used ? */
126 tb->tb_jmp_offset[2] = 0xffff;
127 tb->tb_jmp_offset[3] = 0xffff;
128 #else
129 s->tb_jmp_offset = NULL;
130 s->tb_next = tb->tb_next;
131 #endif
133 #ifdef CONFIG_PROFILER
134 dyngen_tb_count++;
135 dyngen_interm_time += profile_getclock() - ti;
136 dyngen_code_time -= profile_getclock();
137 #endif
138 gen_code_size = dyngen_code(s, gen_code_buf);
139 *gen_code_size_ptr = gen_code_size;
140 #ifdef CONFIG_PROFILER
141 dyngen_code_time += profile_getclock();
142 dyngen_code_in_len += tb->size;
143 dyngen_code_out_len += gen_code_size;
144 #endif
146 #ifdef DEBUG_DISAS
147 if (loglevel & CPU_LOG_TB_OUT_ASM) {
148 fprintf(logfile, "OUT: [size=%d]\n", *gen_code_size_ptr);
149 disas(logfile, tb->tc_ptr, *gen_code_size_ptr);
150 fprintf(logfile, "\n");
151 fflush(logfile);
153 #endif
154 return 0;
157 /* The cpu state corresponding to 'searched_pc' is restored.
159 int cpu_restore_state(TranslationBlock *tb,
160 CPUState *env, unsigned long searched_pc,
161 void *puc)
163 TCGContext *s = &tcg_ctx;
164 int j;
165 unsigned long tc_ptr;
166 #ifdef CONFIG_PROFILER
167 int64_t ti;
168 #endif
170 #ifdef CONFIG_PROFILER
171 ti = profile_getclock();
172 #endif
173 tcg_func_start(s);
175 if (gen_intermediate_code_pc(env, tb) < 0)
176 return -1;
178 /* find opc index corresponding to search_pc */
179 tc_ptr = (unsigned long)tb->tc_ptr;
180 if (searched_pc < tc_ptr)
181 return -1;
183 s->tb_next_offset = tb->tb_next_offset;
184 #ifdef USE_DIRECT_JUMP
185 s->tb_jmp_offset = tb->tb_jmp_offset;
186 s->tb_next = NULL;
187 #else
188 s->tb_jmp_offset = NULL;
189 s->tb_next = tb->tb_next;
190 #endif
191 j = dyngen_code_search_pc(s, (uint8_t *)tc_ptr, searched_pc - tc_ptr);
192 if (j < 0)
193 return -1;
194 /* now find start of instruction before */
195 while (gen_opc_instr_start[j] == 0)
196 j--;
198 gen_pc_load(env, tb, searched_pc, j, puc);
200 #ifdef CONFIG_PROFILER
201 dyngen_restore_time += profile_getclock() - ti;
202 dyngen_restore_count++;
203 #endif
204 return 0;