~vcs-imports/qemu/git

« back to all changes in this revision

Viewing changes to exec.c

  • Committer: blueswir1
  • Date: 2007-11-25 08:48:16 UTC
  • Revision ID: git-v1:b76482e76560345c00e7d6c89199ced204a926d2
 Fix buffer mux handling for unconnected serial ports


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@3737 c046a42c-6fe2-441c-8c8c-71466251a162

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 */
20
20
#include "config.h"
21
21
#ifdef _WIN32
22
 
#define WIN32_LEAN_AND_MEAN
23
22
#include <windows.h>
24
23
#else
25
24
#include <sys/types.h>
35
34
 
36
35
#include "cpu.h"
37
36
#include "exec-all.h"
38
 
#include "qemu-common.h"
39
 
#include "tcg.h"
40
 
#include "hw/hw.h"
41
 
#include "osdep.h"
42
 
#include "kvm.h"
43
37
#if defined(CONFIG_USER_ONLY)
44
38
#include <qemu.h>
45
39
#endif
61
55
#undef DEBUG_TB_CHECK
62
56
#endif
63
57
 
 
58
/* threshold to flush the translated code buffer */
 
59
#define CODE_GEN_BUFFER_MAX_SIZE (CODE_GEN_BUFFER_SIZE - CODE_GEN_MAX_SIZE)
 
60
 
64
61
#define SMC_BITMAP_USE_THRESHOLD 10
65
62
 
66
63
#define MMAP_AREA_START        0x00000000
75
72
#define TARGET_VIRT_ADDR_SPACE_BITS 42
76
73
#elif defined(TARGET_PPC64)
77
74
#define TARGET_PHYS_ADDR_SPACE_BITS 42
78
 
#elif defined(TARGET_X86_64) && !defined(USE_KQEMU)
79
 
#define TARGET_PHYS_ADDR_SPACE_BITS 42
80
 
#elif defined(TARGET_I386) && !defined(USE_KQEMU)
81
 
#define TARGET_PHYS_ADDR_SPACE_BITS 36
82
75
#else
83
76
/* Note: for compatibility with kqemu, we use 32 bits for x86_64 */
84
77
#define TARGET_PHYS_ADDR_SPACE_BITS 32
85
78
#endif
86
79
 
87
 
static TranslationBlock *tbs;
88
 
int code_gen_max_blocks;
 
80
TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
89
81
TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
90
 
static int nb_tbs;
 
82
int nb_tbs;
91
83
/* any access to the tbs or the page table must use this lock */
92
84
spinlock_t tb_lock = SPIN_LOCK_UNLOCKED;
93
85
 
94
 
#if defined(__arm__) || defined(__sparc_v9__)
95
 
/* The prologue must be reachable with a direct jump. ARM and Sparc64
96
 
 have limited branch ranges (possibly also PPC) so place it in a
97
 
 section close to code segment. */
98
 
#define code_gen_section                                \
99
 
    __attribute__((__section__(".gen_code")))           \
100
 
    __attribute__((aligned (32)))
101
 
#else
102
 
#define code_gen_section                                \
103
 
    __attribute__((aligned (32)))
104
 
#endif
105
 
 
106
 
uint8_t code_gen_prologue[1024] code_gen_section;
107
 
static uint8_t *code_gen_buffer;
108
 
static unsigned long code_gen_buffer_size;
109
 
/* threshold to flush the translated code buffer */
110
 
static unsigned long code_gen_buffer_max_size;
 
86
uint8_t code_gen_buffer[CODE_GEN_BUFFER_SIZE] __attribute__((aligned (32)));
111
87
uint8_t *code_gen_ptr;
112
88
 
113
 
#if !defined(CONFIG_USER_ONLY)
114
 
ram_addr_t phys_ram_size;
 
89
int phys_ram_size;
115
90
int phys_ram_fd;
116
91
uint8_t *phys_ram_base;
117
92
uint8_t *phys_ram_dirty;
118
 
static int in_migration;
119
93
static ram_addr_t phys_ram_alloc_offset = 0;
120
 
#endif
121
94
 
122
95
CPUState *first_cpu;
123
96
/* current CPU in the current thread. It is only valid inside
124
97
   cpu_exec() */
125
98
CPUState *cpu_single_env;
126
 
/* 0 = Do not count executed instructions.
127
 
   1 = Precise instruction counting.
128
 
   2 = Adaptive rate instruction counting.  */
129
 
int use_icount = 0;
130
 
/* Current instruction counter.  While executing translated code this may
131
 
   include some instructions that have not yet been executed.  */
132
 
int64_t qemu_icount;
133
99
 
134
100
typedef struct PageDesc {
135
101
    /* list of TBs intersecting this ram page */
144
110
} PageDesc;
145
111
 
146
112
typedef struct PhysPageDesc {
147
 
    /* offset in host memory of the page + io_index in the low bits */
148
 
    ram_addr_t phys_offset;
 
113
    /* offset in host memory of the page + io_index in the low 12 bits */
 
114
    uint32_t phys_offset;
149
115
} PhysPageDesc;
150
116
 
151
117
#define L2_BITS 10
162
128
#define L1_SIZE (1 << L1_BITS)
163
129
#define L2_SIZE (1 << L2_BITS)
164
130
 
 
131
static void io_mem_init(void);
 
132
 
165
133
unsigned long qemu_real_host_page_size;
166
134
unsigned long qemu_host_page_bits;
167
135
unsigned long qemu_host_page_size;
169
137
 
170
138
/* XXX: for system emulation, it could just be an array */
171
139
static PageDesc *l1_map[L1_SIZE];
172
 
static PhysPageDesc **l1_phys_map;
173
 
 
174
 
#if !defined(CONFIG_USER_ONLY)
175
 
static void io_mem_init(void);
 
140
PhysPageDesc **l1_phys_map;
176
141
 
177
142
/* io memory support */
178
143
CPUWriteMemoryFunc *io_mem_write[IO_MEM_NB_ENTRIES][4];
179
144
CPUReadMemoryFunc *io_mem_read[IO_MEM_NB_ENTRIES][4];
180
145
void *io_mem_opaque[IO_MEM_NB_ENTRIES];
181
146
static int io_mem_nb;
 
147
#if defined(CONFIG_SOFTMMU)
182
148
static int io_mem_watch;
183
149
#endif
184
150
 
185
151
/* log support */
186
 
static const char *logfilename = "/tmp/qemu.log";
 
152
char *logfilename = "/tmp/qemu.log";
187
153
FILE *logfile;
188
154
int loglevel;
189
155
static int log_append = 0;
196
162
#define SUBPAGE_IDX(addr) ((addr) & ~TARGET_PAGE_MASK)
197
163
typedef struct subpage_t {
198
164
    target_phys_addr_t base;
199
 
    CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE][4];
200
 
    CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE][4];
201
 
    void *opaque[TARGET_PAGE_SIZE][2][4];
 
165
    CPUReadMemoryFunc **mem_read[TARGET_PAGE_SIZE];
 
166
    CPUWriteMemoryFunc **mem_write[TARGET_PAGE_SIZE];
 
167
    void *opaque[TARGET_PAGE_SIZE];
202
168
} subpage_t;
203
169
 
204
 
#ifdef _WIN32
205
 
static void map_exec(void *addr, long size)
206
 
{
207
 
    DWORD old_protect;
208
 
    VirtualProtect(addr, size,
209
 
                   PAGE_EXECUTE_READWRITE, &old_protect);
210
 
    
211
 
}
212
 
#else
213
 
static void map_exec(void *addr, long size)
214
 
{
215
 
    unsigned long start, end, page_size;
216
 
    
217
 
    page_size = getpagesize();
218
 
    start = (unsigned long)addr;
219
 
    start &= ~(page_size - 1);
220
 
    
221
 
    end = (unsigned long)addr + size;
222
 
    end += page_size - 1;
223
 
    end &= ~(page_size - 1);
224
 
    
225
 
    mprotect((void *)start, end - start,
226
 
             PROT_READ | PROT_WRITE | PROT_EXEC);
227
 
}
228
 
#endif
229
 
 
230
170
static void page_init(void)
231
171
{
232
172
    /* NOTE: we can always suppose that qemu_host_page_size >=
234
174
#ifdef _WIN32
235
175
    {
236
176
        SYSTEM_INFO system_info;
 
177
        DWORD old_protect;
237
178
 
238
179
        GetSystemInfo(&system_info);
239
180
        qemu_real_host_page_size = system_info.dwPageSize;
 
181
 
 
182
        VirtualProtect(code_gen_buffer, sizeof(code_gen_buffer),
 
183
                       PAGE_EXECUTE_READWRITE, &old_protect);
240
184
    }
241
185
#else
242
186
    qemu_real_host_page_size = getpagesize();
 
187
    {
 
188
        unsigned long start, end;
 
189
 
 
190
        start = (unsigned long)code_gen_buffer;
 
191
        start &= ~(qemu_real_host_page_size - 1);
 
192
 
 
193
        end = (unsigned long)code_gen_buffer + sizeof(code_gen_buffer);
 
194
        end += qemu_real_host_page_size - 1;
 
195
        end &= ~(qemu_real_host_page_size - 1);
 
196
 
 
197
        mprotect((void *)start, end - start,
 
198
                 PROT_READ | PROT_WRITE | PROT_EXEC);
 
199
    }
243
200
#endif
 
201
 
244
202
    if (qemu_host_page_size == 0)
245
203
        qemu_host_page_size = qemu_real_host_page_size;
246
204
    if (qemu_host_page_size < TARGET_PAGE_SIZE)
251
209
    qemu_host_page_mask = ~(qemu_host_page_size - 1);
252
210
    l1_phys_map = qemu_vmalloc(L1_SIZE * sizeof(void *));
253
211
    memset(l1_phys_map, 0, L1_SIZE * sizeof(void *));
254
 
 
255
 
#if !defined(_WIN32) && defined(CONFIG_USER_ONLY)
256
 
    {
257
 
        long long startaddr, endaddr;
258
 
        FILE *f;
259
 
        int n;
260
 
 
261
 
        mmap_lock();
262
 
        last_brk = (unsigned long)sbrk(0);
263
 
        f = fopen("/proc/self/maps", "r");
264
 
        if (f) {
265
 
            do {
266
 
                n = fscanf (f, "%llx-%llx %*[^\n]\n", &startaddr, &endaddr);
267
 
                if (n == 2) {
268
 
                    startaddr = MIN(startaddr,
269
 
                                    (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
270
 
                    endaddr = MIN(endaddr,
271
 
                                    (1ULL << TARGET_PHYS_ADDR_SPACE_BITS) - 1);
272
 
                    page_set_flags(startaddr & TARGET_PAGE_MASK,
273
 
                                   TARGET_PAGE_ALIGN(endaddr),
274
 
                                   PAGE_RESERVED); 
275
 
                }
276
 
            } while (!feof(f));
277
 
            fclose(f);
278
 
        }
279
 
        mmap_unlock();
280
 
    }
281
 
#endif
282
 
}
283
 
 
284
 
static inline PageDesc **page_l1_map(target_ulong index)
285
 
{
286
 
#if TARGET_LONG_BITS > 32
287
 
    /* Host memory outside guest VM.  For 32-bit targets we have already
288
 
       excluded high addresses.  */
289
 
    if (index > ((target_ulong)L2_SIZE * L1_SIZE))
290
 
        return NULL;
291
 
#endif
292
 
    return &l1_map[index >> L2_BITS];
293
 
}
294
 
 
295
 
static inline PageDesc *page_find_alloc(target_ulong index)
 
212
}
 
213
 
 
214
static inline PageDesc *page_find_alloc(unsigned int index)
296
215
{
297
216
    PageDesc **lp, *p;
298
 
    lp = page_l1_map(index);
299
 
    if (!lp)
300
 
        return NULL;
301
217
 
 
218
    lp = &l1_map[index >> L2_BITS];
302
219
    p = *lp;
303
220
    if (!p) {
304
221
        /* allocate if not found */
305
 
#if defined(CONFIG_USER_ONLY)
306
 
        unsigned long addr;
307
 
        size_t len = sizeof(PageDesc) * L2_SIZE;
308
 
        /* Don't use qemu_malloc because it may recurse.  */
309
 
        p = mmap(0, len, PROT_READ | PROT_WRITE,
310
 
                 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
311
 
        *lp = p;
312
 
        addr = h2g(p);
313
 
        if (addr == (target_ulong)addr) {
314
 
            page_set_flags(addr & TARGET_PAGE_MASK,
315
 
                           TARGET_PAGE_ALIGN(addr + len),
316
 
                           PAGE_RESERVED); 
317
 
        }
318
 
#else
319
 
        p = qemu_mallocz(sizeof(PageDesc) * L2_SIZE);
320
 
        *lp = p;
321
 
#endif
 
222
        p = qemu_malloc(sizeof(PageDesc) * L2_SIZE);
 
223
        memset(p, 0, sizeof(PageDesc) * L2_SIZE);
 
224
        *lp = p;
322
225
    }
323
226
    return p + (index & (L2_SIZE - 1));
324
227
}
325
228
 
326
 
static inline PageDesc *page_find(target_ulong index)
 
229
static inline PageDesc *page_find(unsigned int index)
327
230
{
328
 
    PageDesc **lp, *p;
329
 
    lp = page_l1_map(index);
330
 
    if (!lp)
331
 
        return NULL;
 
231
    PageDesc *p;
332
232
 
333
 
    p = *lp;
 
233
    p = l1_map[index >> L2_BITS];
334
234
    if (!p)
335
235
        return 0;
336
236
    return p + (index & (L2_SIZE - 1));
382
282
static void tlb_protect_code(ram_addr_t ram_addr);
383
283
static void tlb_unprotect_code_phys(CPUState *env, ram_addr_t ram_addr,
384
284
                                    target_ulong vaddr);
385
 
#define mmap_lock() do { } while(0)
386
 
#define mmap_unlock() do { } while(0)
387
 
#endif
388
 
 
389
 
#define DEFAULT_CODE_GEN_BUFFER_SIZE (32 * 1024 * 1024)
390
 
 
391
 
#if defined(CONFIG_USER_ONLY)
392
 
/* Currently it is not recommanded to allocate big chunks of data in
393
 
   user mode. It will change when a dedicated libc will be used */
394
 
#define USE_STATIC_CODE_GEN_BUFFER
395
 
#endif
396
 
 
397
 
#ifdef USE_STATIC_CODE_GEN_BUFFER
398
 
static uint8_t static_code_gen_buffer[DEFAULT_CODE_GEN_BUFFER_SIZE];
399
 
#endif
400
 
 
401
 
static void code_gen_alloc(unsigned long tb_size)
402
 
{
403
 
#ifdef USE_STATIC_CODE_GEN_BUFFER
404
 
    code_gen_buffer = static_code_gen_buffer;
405
 
    code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
406
 
    map_exec(code_gen_buffer, code_gen_buffer_size);
407
 
#else
408
 
    code_gen_buffer_size = tb_size;
409
 
    if (code_gen_buffer_size == 0) {
410
 
#if defined(CONFIG_USER_ONLY)
411
 
        /* in user mode, phys_ram_size is not meaningful */
412
 
        code_gen_buffer_size = DEFAULT_CODE_GEN_BUFFER_SIZE;
413
 
#else
414
 
        /* XXX: needs ajustments */
415
 
        code_gen_buffer_size = (unsigned long)(phys_ram_size / 4);
416
 
#endif
417
 
    }
418
 
    if (code_gen_buffer_size < MIN_CODE_GEN_BUFFER_SIZE)
419
 
        code_gen_buffer_size = MIN_CODE_GEN_BUFFER_SIZE;
420
 
    /* The code gen buffer location may have constraints depending on
421
 
       the host cpu and OS */
422
 
#if defined(__linux__) 
423
 
    {
424
 
        int flags;
425
 
        void *start = NULL;
426
 
 
427
 
        flags = MAP_PRIVATE | MAP_ANONYMOUS;
428
 
#if defined(__x86_64__)
429
 
        flags |= MAP_32BIT;
430
 
        /* Cannot map more than that */
431
 
        if (code_gen_buffer_size > (800 * 1024 * 1024))
432
 
            code_gen_buffer_size = (800 * 1024 * 1024);
433
 
#elif defined(__sparc_v9__)
434
 
        // Map the buffer below 2G, so we can use direct calls and branches
435
 
        flags |= MAP_FIXED;
436
 
        start = (void *) 0x60000000UL;
437
 
        if (code_gen_buffer_size > (512 * 1024 * 1024))
438
 
            code_gen_buffer_size = (512 * 1024 * 1024);
439
 
#endif
440
 
        code_gen_buffer = mmap(start, code_gen_buffer_size,
441
 
                               PROT_WRITE | PROT_READ | PROT_EXEC,
442
 
                               flags, -1, 0);
443
 
        if (code_gen_buffer == MAP_FAILED) {
444
 
            fprintf(stderr, "Could not allocate dynamic translator buffer\n");
445
 
            exit(1);
446
 
        }
447
 
    }
448
 
#elif defined(__FreeBSD__)
449
 
    {
450
 
        int flags;
451
 
        void *addr = NULL;
452
 
        flags = MAP_PRIVATE | MAP_ANONYMOUS;
453
 
#if defined(__x86_64__)
454
 
        /* FreeBSD doesn't have MAP_32BIT, use MAP_FIXED and assume
455
 
         * 0x40000000 is free */
456
 
        flags |= MAP_FIXED;
457
 
        addr = (void *)0x40000000;
458
 
        /* Cannot map more than that */
459
 
        if (code_gen_buffer_size > (800 * 1024 * 1024))
460
 
            code_gen_buffer_size = (800 * 1024 * 1024);
461
 
#endif
462
 
        code_gen_buffer = mmap(addr, code_gen_buffer_size,
463
 
                               PROT_WRITE | PROT_READ | PROT_EXEC, 
464
 
                               flags, -1, 0);
465
 
        if (code_gen_buffer == MAP_FAILED) {
466
 
            fprintf(stderr, "Could not allocate dynamic translator buffer\n");
467
 
            exit(1);
468
 
        }
469
 
    }
470
 
#else
471
 
    code_gen_buffer = qemu_malloc(code_gen_buffer_size);
472
 
    if (!code_gen_buffer) {
473
 
        fprintf(stderr, "Could not allocate dynamic translator buffer\n");
474
 
        exit(1);
475
 
    }
476
 
    map_exec(code_gen_buffer, code_gen_buffer_size);
477
 
#endif
478
 
#endif /* !USE_STATIC_CODE_GEN_BUFFER */
479
 
    map_exec(code_gen_prologue, sizeof(code_gen_prologue));
480
 
    code_gen_buffer_max_size = code_gen_buffer_size - 
481
 
        code_gen_max_block_size();
482
 
    code_gen_max_blocks = code_gen_buffer_size / CODE_GEN_AVG_BLOCK_SIZE;
483
 
    tbs = qemu_malloc(code_gen_max_blocks * sizeof(TranslationBlock));
484
 
}
485
 
 
486
 
/* Must be called before using the QEMU cpus. 'tb_size' is the size
487
 
   (in bytes) allocated to the translation buffer. Zero means default
488
 
   size. */
489
 
void cpu_exec_init_all(unsigned long tb_size)
490
 
{
491
 
    cpu_gen_init();
492
 
    code_gen_alloc(tb_size);
493
 
    code_gen_ptr = code_gen_buffer;
494
 
    page_init();
495
 
#if !defined(CONFIG_USER_ONLY)
496
 
    io_mem_init();
497
 
#endif
498
 
}
499
 
 
500
 
#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
501
 
 
502
 
#define CPU_COMMON_SAVE_VERSION 1
503
 
 
504
 
static void cpu_common_save(QEMUFile *f, void *opaque)
505
 
{
506
 
    CPUState *env = opaque;
507
 
 
508
 
    qemu_put_be32s(f, &env->halted);
509
 
    qemu_put_be32s(f, &env->interrupt_request);
510
 
}
511
 
 
512
 
static int cpu_common_load(QEMUFile *f, void *opaque, int version_id)
513
 
{
514
 
    CPUState *env = opaque;
515
 
 
516
 
    if (version_id != CPU_COMMON_SAVE_VERSION)
517
 
        return -EINVAL;
518
 
 
519
 
    qemu_get_be32s(f, &env->halted);
520
 
    qemu_get_be32s(f, &env->interrupt_request);
521
 
    tlb_flush(env, 1);
522
 
 
523
 
    return 0;
524
 
}
525
285
#endif
526
286
 
527
287
void cpu_exec_init(CPUState *env)
529
289
    CPUState **penv;
530
290
    int cpu_index;
531
291
 
 
292
    if (!code_gen_ptr) {
 
293
        code_gen_ptr = code_gen_buffer;
 
294
        page_init();
 
295
        io_mem_init();
 
296
    }
532
297
    env->next_cpu = NULL;
533
298
    penv = &first_cpu;
534
299
    cpu_index = 0;
539
304
    env->cpu_index = cpu_index;
540
305
    env->nb_watchpoints = 0;
541
306
    *penv = env;
542
 
#if defined(CPU_SAVE_VERSION) && !defined(CONFIG_USER_ONLY)
543
 
    register_savevm("cpu_common", cpu_index, CPU_COMMON_SAVE_VERSION,
544
 
                    cpu_common_save, cpu_common_load, env);
545
 
    register_savevm("cpu", cpu_index, CPU_SAVE_VERSION,
546
 
                    cpu_save, cpu_load, env);
547
 
#endif
548
307
}
549
308
 
550
309
static inline void invalidate_page_bitmap(PageDesc *p)
585
344
           nb_tbs, nb_tbs > 0 ?
586
345
           ((unsigned long)(code_gen_ptr - code_gen_buffer)) / nb_tbs : 0);
587
346
#endif
588
 
    if ((unsigned long)(code_gen_ptr - code_gen_buffer) > code_gen_buffer_size)
589
 
        cpu_abort(env1, "Internal error: code buffer overflow\n");
590
 
 
591
347
    nb_tbs = 0;
592
348
 
593
349
    for(env = first_cpu; env != NULL; env = env->next_cpu) {
639
395
    }
640
396
}
641
397
 
642
 
static void tb_jmp_check(TranslationBlock *tb)
 
398
void tb_jmp_check(TranslationBlock *tb)
643
399
{
644
400
    TranslationBlock *tb1;
645
401
    unsigned int n1;
728
484
    tb_set_jmp_target(tb, n, (unsigned long)(tb->tc_ptr + tb->tb_next_offset[n]));
729
485
}
730
486
 
731
 
void tb_phys_invalidate(TranslationBlock *tb, target_ulong page_addr)
 
487
static inline void tb_phys_invalidate(TranslationBlock *tb, unsigned int page_addr)
732
488
{
733
489
    CPUState *env;
734
490
    PageDesc *p;
735
491
    unsigned int h, n1;
736
 
    target_phys_addr_t phys_pc;
 
492
    target_ulong phys_pc;
737
493
    TranslationBlock *tb1, *tb2;
738
494
 
739
495
    /* remove the TB from the hash list */
816
572
    int n, tb_start, tb_end;
817
573
    TranslationBlock *tb;
818
574
 
819
 
    p->code_bitmap = qemu_mallocz(TARGET_PAGE_SIZE / 8);
 
575
    p->code_bitmap = qemu_malloc(TARGET_PAGE_SIZE / 8);
820
576
    if (!p->code_bitmap)
821
577
        return;
 
578
    memset(p->code_bitmap, 0, TARGET_PAGE_SIZE / 8);
822
579
 
823
580
    tb = p->first_tb;
824
581
    while (tb != NULL) {
841
598
    }
842
599
}
843
600
 
844
 
TranslationBlock *tb_gen_code(CPUState *env,
845
 
                              target_ulong pc, target_ulong cs_base,
846
 
                              int flags, int cflags)
 
601
#ifdef TARGET_HAS_PRECISE_SMC
 
602
 
 
603
static void tb_gen_code(CPUState *env,
 
604
                        target_ulong pc, target_ulong cs_base, int flags,
 
605
                        int cflags)
847
606
{
848
607
    TranslationBlock *tb;
849
608
    uint8_t *tc_ptr;
857
616
        tb_flush(env);
858
617
        /* cannot fail at this point */
859
618
        tb = tb_alloc(pc);
860
 
        /* Don't forget to invalidate previous TB info.  */
861
 
        tb_invalidated_flag = 1;
862
619
    }
863
620
    tc_ptr = code_gen_ptr;
864
621
    tb->tc_ptr = tc_ptr;
865
622
    tb->cs_base = cs_base;
866
623
    tb->flags = flags;
867
624
    tb->cflags = cflags;
868
 
    cpu_gen_code(env, tb, &code_gen_size);
 
625
    cpu_gen_code(env, tb, CODE_GEN_MAX_SIZE, &code_gen_size);
869
626
    code_gen_ptr = (void *)(((unsigned long)code_gen_ptr + code_gen_size + CODE_GEN_ALIGN - 1) & ~(CODE_GEN_ALIGN - 1));
870
627
 
871
628
    /* check next page if needed */
875
632
        phys_page2 = get_phys_addr_code(env, virt_page2);
876
633
    }
877
634
    tb_link_phys(tb, phys_pc, phys_page2);
878
 
    return tb;
879
635
}
 
636
#endif
880
637
 
881
638
/* invalidate all TBs which intersect with the target physical page
882
639
   starting in range [start;end[. NOTE: start and end must refer to
883
640
   the same physical page. 'is_cpu_write_access' should be true if called
884
641
   from a real cpu write access: the virtual CPU will exit the current
885
642
   TB if code is modified inside this TB. */
886
 
void tb_invalidate_phys_page_range(target_phys_addr_t start, target_phys_addr_t end,
 
643
void tb_invalidate_phys_page_range(target_ulong start, target_ulong end,
887
644
                                   int is_cpu_write_access)
888
645
{
889
646
    int n, current_tb_modified, current_tb_not_found, current_flags;
931
688
            if (current_tb_not_found) {
932
689
                current_tb_not_found = 0;
933
690
                current_tb = NULL;
934
 
                if (env->mem_io_pc) {
 
691
                if (env->mem_write_pc) {
935
692
                    /* now we have a real cpu fault */
936
 
                    current_tb = tb_find_pc(env->mem_io_pc);
 
693
                    current_tb = tb_find_pc(env->mem_write_pc);
937
694
                }
938
695
            }
939
696
            if (current_tb == tb &&
940
 
                (current_tb->cflags & CF_COUNT_MASK) != 1) {
 
697
                !(current_tb->cflags & CF_SINGLE_INSN)) {
941
698
                /* If we are modifying the current TB, we must stop
942
699
                its execution. We could be more precise by checking
943
700
                that the modification is after the current PC, but it
946
703
 
947
704
                current_tb_modified = 1;
948
705
                cpu_restore_state(current_tb, env,
949
 
                                  env->mem_io_pc, NULL);
 
706
                                  env->mem_write_pc, NULL);
950
707
#if defined(TARGET_I386)
951
708
                current_flags = env->hflags;
952
709
                current_flags |= (env->eflags & (IOPL_MASK | TF_MASK | VM_MASK));
978
735
    if (!p->first_tb) {
979
736
        invalidate_page_bitmap(p);
980
737
        if (is_cpu_write_access) {
981
 
            tlb_unprotect_code_phys(env, start, env->mem_io_vaddr);
 
738
            tlb_unprotect_code_phys(env, start, env->mem_write_vaddr);
982
739
        }
983
740
    }
984
741
#endif
988
745
           modifying the memory. It will ensure that it cannot modify
989
746
           itself */
990
747
        env->current_tb = NULL;
991
 
        tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
 
748
        tb_gen_code(env, current_pc, current_cs_base, current_flags,
 
749
                    CF_SINGLE_INSN);
992
750
        cpu_resume_from_signal(env, NULL);
993
751
    }
994
752
#endif
995
753
}
996
754
 
997
755
/* len must be <= 8 and start must be a multiple of len */
998
 
static inline void tb_invalidate_phys_page_fast(target_phys_addr_t start, int len)
 
756
static inline void tb_invalidate_phys_page_fast(target_ulong start, int len)
999
757
{
1000
758
    PageDesc *p;
1001
759
    int offset, b;
1003
761
    if (1) {
1004
762
        if (loglevel) {
1005
763
            fprintf(logfile, "modifying code at 0x%x size=%d EIP=%x PC=%08x\n",
1006
 
                   cpu_single_env->mem_io_vaddr, len,
 
764
                   cpu_single_env->mem_write_vaddr, len,
1007
765
                   cpu_single_env->eip,
1008
766
                   cpu_single_env->eip + (long)cpu_single_env->segs[R_CS].base);
1009
767
        }
1024
782
}
1025
783
 
1026
784
#if !defined(CONFIG_SOFTMMU)
1027
 
static void tb_invalidate_phys_page(target_phys_addr_t addr,
 
785
static void tb_invalidate_phys_page(target_ulong addr,
1028
786
                                    unsigned long pc, void *puc)
1029
787
{
1030
788
    int n, current_flags, current_tb_modified;
1055
813
        tb = (TranslationBlock *)((long)tb & ~3);
1056
814
#ifdef TARGET_HAS_PRECISE_SMC
1057
815
        if (current_tb == tb &&
1058
 
            (current_tb->cflags & CF_COUNT_MASK) != 1) {
 
816
            !(current_tb->cflags & CF_SINGLE_INSN)) {
1059
817
                /* If we are modifying the current TB, we must stop
1060
818
                   its execution. We could be more precise by checking
1061
819
                   that the modification is after the current PC, but it
1084
842
           modifying the memory. It will ensure that it cannot modify
1085
843
           itself */
1086
844
        env->current_tb = NULL;
1087
 
        tb_gen_code(env, current_pc, current_cs_base, current_flags, 1);
 
845
        tb_gen_code(env, current_pc, current_cs_base, current_flags,
 
846
                    CF_SINGLE_INSN);
1088
847
        cpu_resume_from_signal(env, puc);
1089
848
    }
1090
849
#endif
1152
911
{
1153
912
    TranslationBlock *tb;
1154
913
 
1155
 
    if (nb_tbs >= code_gen_max_blocks ||
1156
 
        (code_gen_ptr - code_gen_buffer) >= code_gen_buffer_max_size)
 
914
    if (nb_tbs >= CODE_GEN_MAX_BLOCKS ||
 
915
        (code_gen_ptr - code_gen_buffer) >= CODE_GEN_BUFFER_MAX_SIZE)
1157
916
        return NULL;
1158
917
    tb = &tbs[nb_tbs++];
1159
918
    tb->pc = pc;
1161
920
    return tb;
1162
921
}
1163
922
 
1164
 
void tb_free(TranslationBlock *tb)
1165
 
{
1166
 
    /* In practice this is mostly used for single use temporary TB
1167
 
       Ignore the hard cases and just back up if this TB happens to
1168
 
       be the last one generated.  */
1169
 
    if (nb_tbs > 0 && tb == &tbs[nb_tbs - 1]) {
1170
 
        code_gen_ptr = tb->tc_ptr;
1171
 
        nb_tbs--;
1172
 
    }
1173
 
}
1174
 
 
1175
923
/* add a new TB and link it to the physical page tables. phys_page2 is
1176
924
   (-1) to indicate that only one page contains the TB. */
1177
925
void tb_link_phys(TranslationBlock *tb,
1180
928
    unsigned int h;
1181
929
    TranslationBlock **ptb;
1182
930
 
1183
 
    /* Grab the mmap lock to stop another thread invalidating this TB
1184
 
       before we are done.  */
1185
 
    mmap_lock();
1186
931
    /* add in the physical hash table */
1187
932
    h = tb_phys_hash_func(phys_pc);
1188
933
    ptb = &tb_phys_hash[h];
1209
954
#ifdef DEBUG_TB_CHECK
1210
955
    tb_page_check();
1211
956
#endif
1212
 
    mmap_unlock();
1213
957
}
1214
958
 
1215
959
/* find the TB 'tb' such that tb[0].tc_ptr <= tc_ptr <
1311
1055
#endif
1312
1056
 
1313
1057
/* Add a watchpoint.  */
1314
 
int cpu_watchpoint_insert(CPUState *env, target_ulong addr, int type)
 
1058
int  cpu_watchpoint_insert(CPUState *env, target_ulong addr)
1315
1059
{
1316
1060
    int i;
1317
1061
 
1324
1068
 
1325
1069
    i = env->nb_watchpoints++;
1326
1070
    env->watchpoint[i].vaddr = addr;
1327
 
    env->watchpoint[i].type = type;
1328
1071
    tlb_flush_page(env, addr);
1329
1072
    /* FIXME: This flush is needed because of the hack to make memory ops
1330
1073
       terminate the TB.  It can be removed once the proper IO trap and
1349
1092
    return -1;
1350
1093
}
1351
1094
 
1352
 
/* Remove all watchpoints. */
1353
 
void cpu_watchpoint_remove_all(CPUState *env) {
1354
 
    int i;
1355
 
 
1356
 
    for (i = 0; i < env->nb_watchpoints; i++) {
1357
 
        tlb_flush_page(env, env->watchpoint[i].vaddr);
1358
 
    }
1359
 
    env->nb_watchpoints = 0;
1360
 
}
1361
 
 
1362
1095
/* add a breakpoint. EXCP_DEBUG is returned by the CPU loop if a
1363
1096
   breakpoint is reached */
1364
1097
int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
1382
1115
#endif
1383
1116
}
1384
1117
 
1385
 
/* remove all breakpoints */
1386
 
void cpu_breakpoint_remove_all(CPUState *env) {
1387
 
#if defined(TARGET_HAS_ICE)
1388
 
    int i;
1389
 
    for(i = 0; i < env->nb_breakpoints; i++) {
1390
 
        breakpoint_invalidate(env, env->breakpoints[i]);
1391
 
    }
1392
 
    env->nb_breakpoints = 0;
1393
 
#endif
1394
 
}
1395
 
 
1396
1118
/* remove a breakpoint */
1397
1119
int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
1398
1120
{
1442
1164
#if !defined(CONFIG_SOFTMMU)
1443
1165
        /* must avoid mmap() usage of glibc by setting a buffer "by hand" */
1444
1166
        {
1445
 
            static char logfile_buf[4096];
 
1167
            static uint8_t logfile_buf[4096];
1446
1168
            setvbuf(logfile, logfile_buf, _IOLBF, sizeof(logfile_buf));
1447
1169
        }
1448
1170
#else
1469
1191
/* mask must never be zero, except for A20 change call */
1470
1192
void cpu_interrupt(CPUState *env, int mask)
1471
1193
{
1472
 
#if !defined(USE_NPTL)
1473
1194
    TranslationBlock *tb;
1474
 
    static spinlock_t interrupt_lock = SPIN_LOCK_UNLOCKED;
1475
 
#endif
1476
 
    int old_mask;
 
1195
    static int interrupt_lock;
1477
1196
 
1478
 
    old_mask = env->interrupt_request;
1479
 
    /* FIXME: This is probably not threadsafe.  A different thread could
1480
 
       be in the middle of a read-modify-write operation.  */
1481
1197
    env->interrupt_request |= mask;
1482
 
#if defined(USE_NPTL)
1483
 
    /* FIXME: TB unchaining isn't SMP safe.  For now just ignore the
1484
 
       problem and hope the cpu will stop of its own accord.  For userspace
1485
 
       emulation this often isn't actually as bad as it sounds.  Often
1486
 
       signals are used primarily to interrupt blocking syscalls.  */
1487
 
#else
1488
 
    if (use_icount) {
1489
 
        env->icount_decr.u16.high = 0xffff;
1490
 
#ifndef CONFIG_USER_ONLY
1491
 
        /* CPU_INTERRUPT_EXIT isn't a real interrupt.  It just means
1492
 
           an async event happened and we need to process it.  */
1493
 
        if (!can_do_io(env)
1494
 
            && (mask & ~(old_mask | CPU_INTERRUPT_EXIT)) != 0) {
1495
 
            cpu_abort(env, "Raised interrupt while not in I/O function");
1496
 
        }
1497
 
#endif
1498
 
    } else {
1499
 
        tb = env->current_tb;
1500
 
        /* if the cpu is currently executing code, we must unlink it and
1501
 
           all the potentially executing TB */
1502
 
        if (tb && !testandset(&interrupt_lock)) {
1503
 
            env->current_tb = NULL;
1504
 
            tb_reset_jump_recursive(tb);
1505
 
            resetlock(&interrupt_lock);
1506
 
        }
 
1198
    /* if the cpu is currently executing code, we must unlink it and
 
1199
       all the potentially executing TB */
 
1200
    tb = env->current_tb;
 
1201
    if (tb && !testandset(&interrupt_lock)) {
 
1202
        env->current_tb = NULL;
 
1203
        tb_reset_jump_recursive(tb);
 
1204
        interrupt_lock = 0;
1507
1205
    }
1508
 
#endif
1509
1206
}
1510
1207
 
1511
1208
void cpu_reset_interrupt(CPUState *env, int mask)
1513
1210
    env->interrupt_request &= ~mask;
1514
1211
}
1515
1212
 
1516
 
const CPULogItem cpu_log_items[] = {
 
1213
CPULogItem cpu_log_items[] = {
1517
1214
    { CPU_LOG_TB_OUT_ASM, "out_asm",
1518
1215
      "show generated host assembly code for each compiled TB" },
1519
1216
    { CPU_LOG_TB_IN_ASM, "in_asm",
1520
1217
      "show target assembly code for each compiled TB" },
1521
1218
    { CPU_LOG_TB_OP, "op",
1522
 
      "show micro ops for each compiled TB" },
 
1219
      "show micro ops for each compiled TB (only usable if 'in_asm' used)" },
 
1220
#ifdef TARGET_I386
1523
1221
    { CPU_LOG_TB_OP_OPT, "op_opt",
1524
 
      "show micro ops "
1525
 
#ifdef TARGET_I386
1526
 
      "before eflags optimization and "
 
1222
      "show micro ops after optimization for each compiled TB" },
1527
1223
#endif
1528
 
      "after liveness analysis" },
1529
1224
    { CPU_LOG_INT, "int",
1530
1225
      "show interrupts/exceptions in short format" },
1531
1226
    { CPU_LOG_EXEC, "exec",
1553
1248
/* takes a comma separated list of log masks. Return 0 if error. */
1554
1249
int cpu_str_to_log_mask(const char *str)
1555
1250
{
1556
 
    const CPULogItem *item;
 
1251
    CPULogItem *item;
1557
1252
    int mask;
1558
1253
    const char *p, *p1;
1559
1254
 
1594
1289
    vfprintf(stderr, fmt, ap);
1595
1290
    fprintf(stderr, "\n");
1596
1291
#ifdef TARGET_I386
 
1292
    if(env->intercept & INTERCEPT_SVM_MASK) {
 
1293
        /* most probably the virtual machine should not
 
1294
           be shut down but rather caught by the VMM */
 
1295
        vmexit(SVM_EXIT_SHUTDOWN, 0);
 
1296
    }
1597
1297
    cpu_dump_state(env, stderr, fprintf, X86_DUMP_FPU | X86_DUMP_CCOP);
1598
1298
#else
1599
1299
    cpu_dump_state(env, stderr, fprintf, 0);
1617
1317
 
1618
1318
CPUState *cpu_copy(CPUState *env)
1619
1319
{
1620
 
    CPUState *new_env = cpu_init(env->cpu_model_str);
 
1320
#if 0
 
1321
    /* XXX: broken, must be handled by each CPU */
 
1322
    CPUState *new_env = cpu_init();
1621
1323
    /* preserve chaining and index */
1622
1324
    CPUState *next_cpu = new_env->next_cpu;
1623
1325
    int cpu_index = new_env->cpu_index;
1625
1327
    new_env->next_cpu = next_cpu;
1626
1328
    new_env->cpu_index = cpu_index;
1627
1329
    return new_env;
 
1330
#else
 
1331
    return NULL;
 
1332
#endif
1628
1333
}
1629
1334
 
1630
1335
#if !defined(CONFIG_USER_ONLY)
1631
1336
 
1632
 
static inline void tlb_flush_jmp_cache(CPUState *env, target_ulong addr)
1633
 
{
1634
 
    unsigned int i;
1635
 
 
1636
 
    /* Discard jump cache entries for any tb which might potentially
1637
 
       overlap the flushed page.  */
1638
 
    i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
1639
 
    memset (&env->tb_jmp_cache[i], 0, 
1640
 
            TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1641
 
 
1642
 
    i = tb_jmp_cache_hash_page(addr);
1643
 
    memset (&env->tb_jmp_cache[i], 0, 
1644
 
            TB_JMP_PAGE_SIZE * sizeof(TranslationBlock *));
1645
 
}
1646
 
 
1647
1337
/* NOTE: if flush_global is true, also flush global entries (not
1648
1338
   implemented yet) */
1649
1339
void tlb_flush(CPUState *env, int flush_global)
1678
1368
 
1679
1369
    memset (env->tb_jmp_cache, 0, TB_JMP_CACHE_SIZE * sizeof (void *));
1680
1370
 
 
1371
#if !defined(CONFIG_SOFTMMU)
 
1372
    munmap((void *)MMAP_AREA_START, MMAP_AREA_END - MMAP_AREA_START);
 
1373
#endif
1681
1374
#ifdef USE_KQEMU
1682
1375
    if (env->kqemu_enabled) {
1683
1376
        kqemu_flush(env, flush_global);
1703
1396
void tlb_flush_page(CPUState *env, target_ulong addr)
1704
1397
{
1705
1398
    int i;
 
1399
    TranslationBlock *tb;
1706
1400
 
1707
1401
#if defined(DEBUG_TLB)
1708
1402
    printf("tlb_flush_page: " TARGET_FMT_lx "\n", addr);
1722
1416
#endif
1723
1417
#endif
1724
1418
 
1725
 
    tlb_flush_jmp_cache(env, addr);
1726
 
 
 
1419
    /* Discard jump cache entries for any tb which might potentially
 
1420
       overlap the flushed page.  */
 
1421
    i = tb_jmp_cache_hash_page(addr - TARGET_PAGE_SIZE);
 
1422
    memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
 
1423
 
 
1424
    i = tb_jmp_cache_hash_page(addr);
 
1425
    memset (&env->tb_jmp_cache[i], 0, TB_JMP_PAGE_SIZE * sizeof(tb));
 
1426
 
 
1427
#if !defined(CONFIG_SOFTMMU)
 
1428
    if (addr < MMAP_AREA_END)
 
1429
        munmap((void *)addr, TARGET_PAGE_SIZE);
 
1430
#endif
1727
1431
#ifdef USE_KQEMU
1728
1432
    if (env->kqemu_enabled) {
1729
1433
        kqemu_flush_page(env, addr);
1755
1459
    if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_RAM) {
1756
1460
        addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
1757
1461
        if ((addr - start) < length) {
1758
 
            tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | TLB_NOTDIRTY;
 
1462
            tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_NOTDIRTY;
1759
1463
        }
1760
1464
    }
1761
1465
}
1809
1513
#endif
1810
1514
#endif
1811
1515
    }
1812
 
}
1813
 
 
1814
 
int cpu_physical_memory_set_dirty_tracking(int enable)
1815
 
{
1816
 
    in_migration = enable;
1817
 
    return 0;
1818
 
}
1819
 
 
1820
 
int cpu_physical_memory_get_dirty_tracking(void)
1821
 
{
1822
 
    return in_migration;
 
1516
 
 
1517
#if !defined(CONFIG_SOFTMMU)
 
1518
    /* XXX: this is expensive */
 
1519
    {
 
1520
        VirtPageDesc *p;
 
1521
        int j;
 
1522
        target_ulong addr;
 
1523
 
 
1524
        for(i = 0; i < L1_SIZE; i++) {
 
1525
            p = l1_virt_map[i];
 
1526
            if (p) {
 
1527
                addr = i << (TARGET_PAGE_BITS + L2_BITS);
 
1528
                for(j = 0; j < L2_SIZE; j++) {
 
1529
                    if (p->valid_tag == virt_valid_tag &&
 
1530
                        p->phys_addr >= start && p->phys_addr < end &&
 
1531
                        (p->prot & PROT_WRITE)) {
 
1532
                        if (addr < MMAP_AREA_END) {
 
1533
                            mprotect((void *)addr, TARGET_PAGE_SIZE,
 
1534
                                     p->prot & ~PROT_WRITE);
 
1535
                        }
 
1536
                    }
 
1537
                    addr += TARGET_PAGE_SIZE;
 
1538
                    p++;
 
1539
                }
 
1540
            }
 
1541
        }
 
1542
    }
 
1543
#endif
1823
1544
}
1824
1545
 
1825
1546
static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry)
1830
1551
        ram_addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) +
1831
1552
            tlb_entry->addend - (unsigned long)phys_ram_base;
1832
1553
        if (!cpu_physical_memory_is_dirty(ram_addr)) {
1833
 
            tlb_entry->addr_write |= TLB_NOTDIRTY;
 
1554
            tlb_entry->addr_write |= IO_MEM_NOTDIRTY;
1834
1555
        }
1835
1556
    }
1836
1557
}
1853
1574
#endif
1854
1575
}
1855
1576
 
1856
 
static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry, target_ulong vaddr)
 
1577
static inline void tlb_set_dirty1(CPUTLBEntry *tlb_entry,
 
1578
                                  unsigned long start)
1857
1579
{
1858
 
    if (tlb_entry->addr_write == (vaddr | TLB_NOTDIRTY))
1859
 
        tlb_entry->addr_write = vaddr;
 
1580
    unsigned long addr;
 
1581
    if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) == IO_MEM_NOTDIRTY) {
 
1582
        addr = (tlb_entry->addr_write & TARGET_PAGE_MASK) + tlb_entry->addend;
 
1583
        if (addr == start) {
 
1584
            tlb_entry->addr_write = (tlb_entry->addr_write & TARGET_PAGE_MASK) | IO_MEM_RAM;
 
1585
        }
 
1586
    }
1860
1587
}
1861
1588
 
1862
 
/* update the TLB corresponding to virtual page vaddr
1863
 
   so that it is no longer dirty */
1864
 
static inline void tlb_set_dirty(CPUState *env, target_ulong vaddr)
 
1589
/* update the TLB corresponding to virtual page vaddr and phys addr
 
1590
   addr so that it is no longer dirty */
 
1591
static inline void tlb_set_dirty(CPUState *env,
 
1592
                                 unsigned long addr, target_ulong vaddr)
1865
1593
{
1866
1594
    int i;
1867
1595
 
1868
 
    vaddr &= TARGET_PAGE_MASK;
 
1596
    addr &= TARGET_PAGE_MASK;
1869
1597
    i = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1870
 
    tlb_set_dirty1(&env->tlb_table[0][i], vaddr);
1871
 
    tlb_set_dirty1(&env->tlb_table[1][i], vaddr);
 
1598
    tlb_set_dirty1(&env->tlb_table[0][i], addr);
 
1599
    tlb_set_dirty1(&env->tlb_table[1][i], addr);
1872
1600
#if (NB_MMU_MODES >= 3)
1873
 
    tlb_set_dirty1(&env->tlb_table[2][i], vaddr);
 
1601
    tlb_set_dirty1(&env->tlb_table[2][i], addr);
1874
1602
#if (NB_MMU_MODES == 4)
1875
 
    tlb_set_dirty1(&env->tlb_table[3][i], vaddr);
 
1603
    tlb_set_dirty1(&env->tlb_table[3][i], addr);
1876
1604
#endif
1877
1605
#endif
1878
1606
}
1889
1617
    unsigned long pd;
1890
1618
    unsigned int index;
1891
1619
    target_ulong address;
1892
 
    target_ulong code_address;
1893
1620
    target_phys_addr_t addend;
1894
1621
    int ret;
1895
1622
    CPUTLBEntry *te;
1896
1623
    int i;
1897
 
    target_phys_addr_t iotlb;
1898
1624
 
1899
1625
    p = phys_page_find(paddr >> TARGET_PAGE_BITS);
1900
1626
    if (!p) {
1908
1634
#endif
1909
1635
 
1910
1636
    ret = 0;
1911
 
    address = vaddr;
1912
 
    if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
1913
 
        /* IO memory case (romd handled later) */
1914
 
        address |= TLB_MMIO;
1915
 
    }
1916
 
    addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
1917
 
    if ((pd & ~TARGET_PAGE_MASK) <= IO_MEM_ROM) {
1918
 
        /* Normal RAM.  */
1919
 
        iotlb = pd & TARGET_PAGE_MASK;
1920
 
        if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM)
1921
 
            iotlb |= IO_MEM_NOTDIRTY;
1922
 
        else
1923
 
            iotlb |= IO_MEM_ROM;
1924
 
    } else {
1925
 
        /* IO handlers are currently passed a phsical address.
1926
 
           It would be nice to pass an offset from the base address
1927
 
           of that region.  This would avoid having to special case RAM,
1928
 
           and avoid full address decoding in every device.
1929
 
           We can't use the high bits of pd for this because
1930
 
           IO_MEM_ROMD uses these as a ram address.  */
1931
 
        iotlb = (pd & ~TARGET_PAGE_MASK) + paddr;
1932
 
    }
1933
 
 
1934
 
    code_address = address;
1935
 
    /* Make accesses to pages with watchpoints go via the
1936
 
       watchpoint trap routines.  */
1937
 
    for (i = 0; i < env->nb_watchpoints; i++) {
1938
 
        if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
1939
 
            iotlb = io_mem_watch + paddr;
1940
 
            /* TODO: The memory case can be optimized by not trapping
1941
 
               reads of pages with a write breakpoint.  */
1942
 
            address |= TLB_MMIO;
1943
 
        }
1944
 
    }
1945
 
 
1946
 
    index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
1947
 
    env->iotlb[mmu_idx][index] = iotlb - vaddr;
1948
 
    te = &env->tlb_table[mmu_idx][index];
1949
 
    te->addend = addend - vaddr;
1950
 
    if (prot & PAGE_READ) {
1951
 
        te->addr_read = address;
1952
 
    } else {
1953
 
        te->addr_read = -1;
1954
 
    }
1955
 
 
1956
 
    if (prot & PAGE_EXEC) {
1957
 
        te->addr_code = code_address;
1958
 
    } else {
1959
 
        te->addr_code = -1;
1960
 
    }
1961
 
    if (prot & PAGE_WRITE) {
1962
 
        if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
1963
 
            (pd & IO_MEM_ROMD)) {
1964
 
            /* Write access calls the I/O callback.  */
1965
 
            te->addr_write = address | TLB_MMIO;
1966
 
        } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
1967
 
                   !cpu_physical_memory_is_dirty(pd)) {
1968
 
            te->addr_write = address | TLB_NOTDIRTY;
1969
 
        } else {
1970
 
            te->addr_write = address;
1971
 
        }
1972
 
    } else {
1973
 
        te->addr_write = -1;
1974
 
    }
 
1637
#if !defined(CONFIG_SOFTMMU)
 
1638
    if (is_softmmu)
 
1639
#endif
 
1640
    {
 
1641
        if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM && !(pd & IO_MEM_ROMD)) {
 
1642
            /* IO memory case */
 
1643
            address = vaddr | pd;
 
1644
            addend = paddr;
 
1645
        } else {
 
1646
            /* standard memory */
 
1647
            address = vaddr;
 
1648
            addend = (unsigned long)phys_ram_base + (pd & TARGET_PAGE_MASK);
 
1649
        }
 
1650
 
 
1651
        /* Make accesses to pages with watchpoints go via the
 
1652
           watchpoint trap routines.  */
 
1653
        for (i = 0; i < env->nb_watchpoints; i++) {
 
1654
            if (vaddr == (env->watchpoint[i].vaddr & TARGET_PAGE_MASK)) {
 
1655
                if (address & ~TARGET_PAGE_MASK) {
 
1656
                    env->watchpoint[i].addend = 0;
 
1657
                    address = vaddr | io_mem_watch;
 
1658
                } else {
 
1659
                    env->watchpoint[i].addend = pd - paddr +
 
1660
                        (unsigned long) phys_ram_base;
 
1661
                    /* TODO: Figure out how to make read watchpoints coexist
 
1662
                       with code.  */
 
1663
                    pd = (pd & TARGET_PAGE_MASK) | io_mem_watch | IO_MEM_ROMD;
 
1664
                }
 
1665
            }
 
1666
        }
 
1667
 
 
1668
        index = (vaddr >> TARGET_PAGE_BITS) & (CPU_TLB_SIZE - 1);
 
1669
        addend -= vaddr;
 
1670
        te = &env->tlb_table[mmu_idx][index];
 
1671
        te->addend = addend;
 
1672
        if (prot & PAGE_READ) {
 
1673
            te->addr_read = address;
 
1674
        } else {
 
1675
            te->addr_read = -1;
 
1676
        }
 
1677
        if (prot & PAGE_EXEC) {
 
1678
            te->addr_code = address;
 
1679
        } else {
 
1680
            te->addr_code = -1;
 
1681
        }
 
1682
        if (prot & PAGE_WRITE) {
 
1683
            if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
 
1684
                (pd & IO_MEM_ROMD)) {
 
1685
                /* write access calls the I/O callback */
 
1686
                te->addr_write = vaddr |
 
1687
                    (pd & ~(TARGET_PAGE_MASK | IO_MEM_ROMD));
 
1688
            } else if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
 
1689
                       !cpu_physical_memory_is_dirty(pd)) {
 
1690
                te->addr_write = vaddr | IO_MEM_NOTDIRTY;
 
1691
            } else {
 
1692
                te->addr_write = address;
 
1693
            }
 
1694
        } else {
 
1695
            te->addr_write = -1;
 
1696
        }
 
1697
    }
 
1698
#if !defined(CONFIG_SOFTMMU)
 
1699
    else {
 
1700
        if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM) {
 
1701
            /* IO access: no mapping is done as it will be handled by the
 
1702
               soft MMU */
 
1703
            if (!(env->hflags & HF_SOFTMMU_MASK))
 
1704
                ret = 2;
 
1705
        } else {
 
1706
            void *map_addr;
 
1707
 
 
1708
            if (vaddr >= MMAP_AREA_END) {
 
1709
                ret = 2;
 
1710
            } else {
 
1711
                if (prot & PROT_WRITE) {
 
1712
                    if ((pd & ~TARGET_PAGE_MASK) == IO_MEM_ROM ||
 
1713
#if defined(TARGET_HAS_SMC) || 1
 
1714
                        first_tb ||
 
1715
#endif
 
1716
                        ((pd & ~TARGET_PAGE_MASK) == IO_MEM_RAM &&
 
1717
                         !cpu_physical_memory_is_dirty(pd))) {
 
1718
                        /* ROM: we do as if code was inside */
 
1719
                        /* if code is present, we only map as read only and save the
 
1720
                           original mapping */
 
1721
                        VirtPageDesc *vp;
 
1722
 
 
1723
                        vp = virt_page_find_alloc(vaddr >> TARGET_PAGE_BITS, 1);
 
1724
                        vp->phys_addr = pd;
 
1725
                        vp->prot = prot;
 
1726
                        vp->valid_tag = virt_valid_tag;
 
1727
                        prot &= ~PAGE_WRITE;
 
1728
                    }
 
1729
                }
 
1730
                map_addr = mmap((void *)vaddr, TARGET_PAGE_SIZE, prot,
 
1731
                                MAP_SHARED | MAP_FIXED, phys_ram_fd, (pd & TARGET_PAGE_MASK));
 
1732
                if (map_addr == MAP_FAILED) {
 
1733
                    cpu_abort(env, "mmap failed when mapped physical address 0x%08x to virtual address 0x%08x\n",
 
1734
                              paddr, vaddr);
 
1735
                }
 
1736
            }
 
1737
        }
 
1738
    }
 
1739
#endif
1975
1740
    return ret;
1976
1741
}
1977
1742
 
 
1743
/* called from signal handler: invalidate the code and unprotect the
 
1744
   page. Return TRUE if the fault was succesfully handled. */
 
1745
int page_unprotect(target_ulong addr, unsigned long pc, void *puc)
 
1746
{
 
1747
#if !defined(CONFIG_SOFTMMU)
 
1748
    VirtPageDesc *vp;
 
1749
 
 
1750
#if defined(DEBUG_TLB)
 
1751
    printf("page_unprotect: addr=0x%08x\n", addr);
 
1752
#endif
 
1753
    addr &= TARGET_PAGE_MASK;
 
1754
 
 
1755
    /* if it is not mapped, no need to worry here */
 
1756
    if (addr >= MMAP_AREA_END)
 
1757
        return 0;
 
1758
    vp = virt_page_find(addr >> TARGET_PAGE_BITS);
 
1759
    if (!vp)
 
1760
        return 0;
 
1761
    /* NOTE: in this case, validate_tag is _not_ tested as it
 
1762
       validates only the code TLB */
 
1763
    if (vp->valid_tag != virt_valid_tag)
 
1764
        return 0;
 
1765
    if (!(vp->prot & PAGE_WRITE))
 
1766
        return 0;
 
1767
#if defined(DEBUG_TLB)
 
1768
    printf("page_unprotect: addr=0x%08x phys_addr=0x%08x prot=%x\n",
 
1769
           addr, vp->phys_addr, vp->prot);
 
1770
#endif
 
1771
    if (mprotect((void *)addr, TARGET_PAGE_SIZE, vp->prot) < 0)
 
1772
        cpu_abort(cpu_single_env, "error mprotect addr=0x%lx prot=%d\n",
 
1773
                  (unsigned long)addr, vp->prot);
 
1774
    /* set the dirty bit */
 
1775
    phys_ram_dirty[vp->phys_addr >> TARGET_PAGE_BITS] = 0xff;
 
1776
    /* flush the code inside */
 
1777
    tb_invalidate_phys_page(vp->phys_addr, pc, puc);
 
1778
    return 1;
 
1779
#else
 
1780
    return 0;
 
1781
#endif
 
1782
}
 
1783
 
1978
1784
#else
1979
1785
 
1980
1786
void tlb_flush(CPUState *env, int flush_global)
2053
1859
    PageDesc *p;
2054
1860
    target_ulong addr;
2055
1861
 
2056
 
    /* mmap_lock should already be held.  */
2057
1862
    start = start & TARGET_PAGE_MASK;
2058
1863
    end = TARGET_PAGE_ALIGN(end);
2059
1864
    if (flags & PAGE_WRITE)
2060
1865
        flags |= PAGE_WRITE_ORG;
 
1866
    spin_lock(&tb_lock);
2061
1867
    for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2062
1868
        p = page_find_alloc(addr >> TARGET_PAGE_BITS);
2063
 
        /* We may be called for host regions that are outside guest
2064
 
           address space.  */
2065
 
        if (!p)
2066
 
            return;
2067
1869
        /* if the write protection is set, then we invalidate the code
2068
1870
           inside */
2069
1871
        if (!(p->flags & PAGE_WRITE) &&
2073
1875
        }
2074
1876
        p->flags = flags;
2075
1877
    }
 
1878
    spin_unlock(&tb_lock);
2076
1879
}
2077
1880
 
2078
1881
int page_check_range(target_ulong start, target_ulong len, int flags)
2081
1884
    target_ulong end;
2082
1885
    target_ulong addr;
2083
1886
 
2084
 
    if (start + len < start)
 
1887
    end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
 
1888
    start = start & TARGET_PAGE_MASK;
 
1889
 
 
1890
    if( end < start )
2085
1891
        /* we've wrapped around */
2086
1892
        return -1;
2087
 
 
2088
 
    end = TARGET_PAGE_ALIGN(start+len); /* must do before we loose bits in the next step */
2089
 
    start = start & TARGET_PAGE_MASK;
2090
 
 
2091
1893
    for(addr = start; addr < end; addr += TARGET_PAGE_SIZE) {
2092
1894
        p = page_find(addr >> TARGET_PAGE_BITS);
2093
1895
        if( !p )
2120
1922
    PageDesc *p, *p1;
2121
1923
    target_ulong host_start, host_end, addr;
2122
1924
 
2123
 
    /* Technically this isn't safe inside a signal handler.  However we
2124
 
       know this only ever happens in a synchronous SEGV handler, so in
2125
 
       practice it seems to be ok.  */
2126
 
    mmap_lock();
2127
 
 
2128
1925
    host_start = address & qemu_host_page_mask;
2129
1926
    page_index = host_start >> TARGET_PAGE_BITS;
2130
1927
    p1 = page_find(page_index);
2131
 
    if (!p1) {
2132
 
        mmap_unlock();
 
1928
    if (!p1)
2133
1929
        return 0;
2134
 
    }
2135
1930
    host_end = host_start + qemu_host_page_size;
2136
1931
    p = p1;
2137
1932
    prot = 0;
2153
1948
#ifdef DEBUG_TB_CHECK
2154
1949
            tb_invalidate_check(address);
2155
1950
#endif
2156
 
            mmap_unlock();
2157
1951
            return 1;
2158
1952
        }
2159
1953
    }
2160
 
    mmap_unlock();
2161
1954
    return 0;
2162
1955
}
2163
1956
 
2167
1960
}
2168
1961
#endif /* defined(CONFIG_USER_ONLY) */
2169
1962
 
2170
 
#if !defined(CONFIG_USER_ONLY)
2171
1963
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2172
 
                             ram_addr_t memory);
2173
 
static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2174
 
                           ram_addr_t orig_memory);
 
1964
                             int memory);
 
1965
static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
 
1966
                           int orig_memory);
2175
1967
#define CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2, \
2176
1968
                      need_subpage)                                     \
2177
1969
    do {                                                                \
2196
1988
   page size. If (phys_offset & ~TARGET_PAGE_MASK) != 0, then it is an
2197
1989
   io memory page */
2198
1990
void cpu_register_physical_memory(target_phys_addr_t start_addr,
2199
 
                                  ram_addr_t size,
2200
 
                                  ram_addr_t phys_offset)
 
1991
                                  unsigned long size,
 
1992
                                  unsigned long phys_offset)
2201
1993
{
2202
1994
    target_phys_addr_t addr, end_addr;
2203
1995
    PhysPageDesc *p;
2204
1996
    CPUState *env;
2205
 
    ram_addr_t orig_size = size;
 
1997
    unsigned long orig_size = size;
2206
1998
    void *subpage;
2207
1999
 
2208
 
#ifdef USE_KQEMU
2209
 
    /* XXX: should not depend on cpu context */
2210
 
    env = first_cpu;
2211
 
    if (env->kqemu_enabled) {
2212
 
        kqemu_set_phys_mem(start_addr, size, phys_offset);
2213
 
    }
2214
 
#endif
2215
 
    if (kvm_enabled())
2216
 
        kvm_set_phys_mem(start_addr, size, phys_offset);
2217
 
 
2218
2000
    size = (size + TARGET_PAGE_SIZE - 1) & TARGET_PAGE_MASK;
2219
2001
    end_addr = start_addr + (target_phys_addr_t)size;
2220
2002
    for(addr = start_addr; addr != end_addr; addr += TARGET_PAGE_SIZE) {
2221
2003
        p = phys_page_find(addr >> TARGET_PAGE_BITS);
2222
2004
        if (p && p->phys_offset != IO_MEM_UNASSIGNED) {
2223
 
            ram_addr_t orig_memory = p->phys_offset;
 
2005
            unsigned long orig_memory = p->phys_offset;
2224
2006
            target_phys_addr_t start_addr2, end_addr2;
2225
2007
            int need_subpage = 0;
2226
2008
 
2227
2009
            CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr, end_addr2,
2228
2010
                          need_subpage);
2229
 
            if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
 
2011
            if (need_subpage) {
2230
2012
                if (!(orig_memory & IO_MEM_SUBPAGE)) {
2231
2013
                    subpage = subpage_init((addr & TARGET_PAGE_MASK),
2232
2014
                                           &p->phys_offset, orig_memory);
2254
2036
                CHECK_SUBPAGE(addr, start_addr, start_addr2, end_addr,
2255
2037
                              end_addr2, need_subpage);
2256
2038
 
2257
 
                if (need_subpage || phys_offset & IO_MEM_SUBWIDTH) {
 
2039
                if (need_subpage) {
2258
2040
                    subpage = subpage_init((addr & TARGET_PAGE_MASK),
2259
2041
                                           &p->phys_offset, IO_MEM_UNASSIGNED);
2260
2042
                    subpage_register(subpage, start_addr2, end_addr2,
2273
2055
}
2274
2056
 
2275
2057
/* XXX: temporary until new memory mapping API */
2276
 
ram_addr_t cpu_get_physical_page_desc(target_phys_addr_t addr)
 
2058
uint32_t cpu_get_physical_page_desc(target_phys_addr_t addr)
2277
2059
{
2278
2060
    PhysPageDesc *p;
2279
2061
 
2284
2066
}
2285
2067
 
2286
2068
/* XXX: better than nothing */
2287
 
ram_addr_t qemu_ram_alloc(ram_addr_t size)
 
2069
ram_addr_t qemu_ram_alloc(unsigned int size)
2288
2070
{
2289
2071
    ram_addr_t addr;
2290
 
    if ((phys_ram_alloc_offset + size) > phys_ram_size) {
2291
 
        fprintf(stderr, "Not enough memory (requested_size = %" PRIu64 ", max memory = %" PRIu64 ")\n",
2292
 
                (uint64_t)size, (uint64_t)phys_ram_size);
 
2072
    if ((phys_ram_alloc_offset + size) >= phys_ram_size) {
 
2073
        fprintf(stderr, "Not enough memory (requested_size = %u, max memory = %d)\n",
 
2074
                size, phys_ram_size);
2293
2075
        abort();
2294
2076
    }
2295
2077
    addr = phys_ram_alloc_offset;
2306
2088
#ifdef DEBUG_UNASSIGNED
2307
2089
    printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2308
2090
#endif
2309
 
#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2310
 
    do_unassigned_access(addr, 0, 0, 0, 1);
2311
 
#endif
2312
 
    return 0;
2313
 
}
2314
 
 
2315
 
static uint32_t unassigned_mem_readw(void *opaque, target_phys_addr_t addr)
2316
 
{
2317
 
#ifdef DEBUG_UNASSIGNED
2318
 
    printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2319
 
#endif
2320
 
#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2321
 
    do_unassigned_access(addr, 0, 0, 0, 2);
2322
 
#endif
2323
 
    return 0;
2324
 
}
2325
 
 
2326
 
static uint32_t unassigned_mem_readl(void *opaque, target_phys_addr_t addr)
2327
 
{
2328
 
#ifdef DEBUG_UNASSIGNED
2329
 
    printf("Unassigned mem read " TARGET_FMT_plx "\n", addr);
2330
 
#endif
2331
 
#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2332
 
    do_unassigned_access(addr, 0, 0, 0, 4);
 
2091
#ifdef TARGET_SPARC
 
2092
    do_unassigned_access(addr, 0, 0, 0);
 
2093
#elif TARGET_CRIS
 
2094
    do_unassigned_access(addr, 0, 0, 0);
2333
2095
#endif
2334
2096
    return 0;
2335
2097
}
2339
2101
#ifdef DEBUG_UNASSIGNED
2340
2102
    printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2341
2103
#endif
2342
 
#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2343
 
    do_unassigned_access(addr, 1, 0, 0, 1);
2344
 
#endif
2345
 
}
2346
 
 
2347
 
static void unassigned_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2348
 
{
2349
 
#ifdef DEBUG_UNASSIGNED
2350
 
    printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2351
 
#endif
2352
 
#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2353
 
    do_unassigned_access(addr, 1, 0, 0, 2);
2354
 
#endif
2355
 
}
2356
 
 
2357
 
static void unassigned_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2358
 
{
2359
 
#ifdef DEBUG_UNASSIGNED
2360
 
    printf("Unassigned mem write " TARGET_FMT_plx " = 0x%x\n", addr, val);
2361
 
#endif
2362
 
#if defined(TARGET_SPARC) || defined(TARGET_CRIS)
2363
 
    do_unassigned_access(addr, 1, 0, 0, 4);
 
2104
#ifdef TARGET_SPARC
 
2105
    do_unassigned_access(addr, 1, 0, 0);
 
2106
#elif TARGET_CRIS
 
2107
    do_unassigned_access(addr, 1, 0, 0);
2364
2108
#endif
2365
2109
}
2366
2110
 
2367
2111
static CPUReadMemoryFunc *unassigned_mem_read[3] = {
2368
2112
    unassigned_mem_readb,
2369
 
    unassigned_mem_readw,
2370
 
    unassigned_mem_readl,
 
2113
    unassigned_mem_readb,
 
2114
    unassigned_mem_readb,
2371
2115
};
2372
2116
 
2373
2117
static CPUWriteMemoryFunc *unassigned_mem_write[3] = {
2374
2118
    unassigned_mem_writeb,
2375
 
    unassigned_mem_writew,
2376
 
    unassigned_mem_writel,
 
2119
    unassigned_mem_writeb,
 
2120
    unassigned_mem_writeb,
2377
2121
};
2378
2122
 
2379
 
static void notdirty_mem_writeb(void *opaque, target_phys_addr_t ram_addr,
2380
 
                                uint32_t val)
 
2123
static void notdirty_mem_writeb(void *opaque, target_phys_addr_t addr, uint32_t val)
2381
2124
{
 
2125
    unsigned long ram_addr;
2382
2126
    int dirty_flags;
 
2127
    ram_addr = addr - (unsigned long)phys_ram_base;
2383
2128
    dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2384
2129
    if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2385
2130
#if !defined(CONFIG_USER_ONLY)
2387
2132
        dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2388
2133
#endif
2389
2134
    }
2390
 
    stb_p(phys_ram_base + ram_addr, val);
 
2135
    stb_p((uint8_t *)(long)addr, val);
2391
2136
#ifdef USE_KQEMU
2392
2137
    if (cpu_single_env->kqemu_enabled &&
2393
2138
        (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2398
2143
    /* we remove the notdirty callback only if the code has been
2399
2144
       flushed */
2400
2145
    if (dirty_flags == 0xff)
2401
 
        tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
 
2146
        tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
2402
2147
}
2403
2148
 
2404
 
static void notdirty_mem_writew(void *opaque, target_phys_addr_t ram_addr,
2405
 
                                uint32_t val)
 
2149
static void notdirty_mem_writew(void *opaque, target_phys_addr_t addr, uint32_t val)
2406
2150
{
 
2151
    unsigned long ram_addr;
2407
2152
    int dirty_flags;
 
2153
    ram_addr = addr - (unsigned long)phys_ram_base;
2408
2154
    dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2409
2155
    if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2410
2156
#if !defined(CONFIG_USER_ONLY)
2412
2158
        dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2413
2159
#endif
2414
2160
    }
2415
 
    stw_p(phys_ram_base + ram_addr, val);
 
2161
    stw_p((uint8_t *)(long)addr, val);
2416
2162
#ifdef USE_KQEMU
2417
2163
    if (cpu_single_env->kqemu_enabled &&
2418
2164
        (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2423
2169
    /* we remove the notdirty callback only if the code has been
2424
2170
       flushed */
2425
2171
    if (dirty_flags == 0xff)
2426
 
        tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
 
2172
        tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
2427
2173
}
2428
2174
 
2429
 
static void notdirty_mem_writel(void *opaque, target_phys_addr_t ram_addr,
2430
 
                                uint32_t val)
 
2175
static void notdirty_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
2431
2176
{
 
2177
    unsigned long ram_addr;
2432
2178
    int dirty_flags;
 
2179
    ram_addr = addr - (unsigned long)phys_ram_base;
2433
2180
    dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2434
2181
    if (!(dirty_flags & CODE_DIRTY_FLAG)) {
2435
2182
#if !defined(CONFIG_USER_ONLY)
2437
2184
        dirty_flags = phys_ram_dirty[ram_addr >> TARGET_PAGE_BITS];
2438
2185
#endif
2439
2186
    }
2440
 
    stl_p(phys_ram_base + ram_addr, val);
 
2187
    stl_p((uint8_t *)(long)addr, val);
2441
2188
#ifdef USE_KQEMU
2442
2189
    if (cpu_single_env->kqemu_enabled &&
2443
2190
        (dirty_flags & KQEMU_MODIFY_PAGE_MASK) != KQEMU_MODIFY_PAGE_MASK)
2448
2195
    /* we remove the notdirty callback only if the code has been
2449
2196
       flushed */
2450
2197
    if (dirty_flags == 0xff)
2451
 
        tlb_set_dirty(cpu_single_env, cpu_single_env->mem_io_vaddr);
 
2198
        tlb_set_dirty(cpu_single_env, addr, cpu_single_env->mem_write_vaddr);
2452
2199
}
2453
2200
 
2454
2201
static CPUReadMemoryFunc *error_mem_read[3] = {
2463
2210
    notdirty_mem_writel,
2464
2211
};
2465
2212
 
2466
 
/* Generate a debug exception if a watchpoint has been hit.  */
2467
 
static void check_watchpoint(int offset, int flags)
2468
 
{
2469
 
    CPUState *env = cpu_single_env;
2470
 
    target_ulong vaddr;
2471
 
    int i;
2472
 
 
2473
 
    vaddr = (env->mem_io_vaddr & TARGET_PAGE_MASK) + offset;
2474
 
    for (i = 0; i < env->nb_watchpoints; i++) {
2475
 
        if (vaddr == env->watchpoint[i].vaddr
2476
 
                && (env->watchpoint[i].type & flags)) {
2477
 
            env->watchpoint_hit = i + 1;
2478
 
            cpu_interrupt(env, CPU_INTERRUPT_DEBUG);
2479
 
            break;
2480
 
        }
2481
 
    }
2482
 
}
2483
 
 
 
2213
#if defined(CONFIG_SOFTMMU)
2484
2214
/* Watchpoint access routines.  Watchpoints are inserted using TLB tricks,
2485
2215
   so these check for a hit then pass through to the normal out-of-line
2486
2216
   phys routines.  */
2487
2217
static uint32_t watch_mem_readb(void *opaque, target_phys_addr_t addr)
2488
2218
{
2489
 
    check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
2490
2219
    return ldub_phys(addr);
2491
2220
}
2492
2221
 
2493
2222
static uint32_t watch_mem_readw(void *opaque, target_phys_addr_t addr)
2494
2223
{
2495
 
    check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
2496
2224
    return lduw_phys(addr);
2497
2225
}
2498
2226
 
2499
2227
static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)
2500
2228
{
2501
 
    check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_READ);
2502
2229
    return ldl_phys(addr);
2503
2230
}
2504
2231
 
 
2232
/* Generate a debug exception if a watchpoint has been hit.
 
2233
   Returns the real physical address of the access.  addr will be a host
 
2234
   address in case of a RAM location.  */
 
2235
static target_ulong check_watchpoint(target_phys_addr_t addr)
 
2236
{
 
2237
    CPUState *env = cpu_single_env;
 
2238
    target_ulong watch;
 
2239
    target_ulong retaddr;
 
2240
    int i;
 
2241
 
 
2242
    retaddr = addr;
 
2243
    for (i = 0; i < env->nb_watchpoints; i++) {
 
2244
        watch = env->watchpoint[i].vaddr;
 
2245
        if (((env->mem_write_vaddr ^ watch) & TARGET_PAGE_MASK) == 0) {
 
2246
            retaddr = addr - env->watchpoint[i].addend;
 
2247
            if (((addr ^ watch) & ~TARGET_PAGE_MASK) == 0) {
 
2248
                cpu_single_env->watchpoint_hit = i + 1;
 
2249
                cpu_interrupt(cpu_single_env, CPU_INTERRUPT_DEBUG);
 
2250
                break;
 
2251
            }
 
2252
        }
 
2253
    }
 
2254
    return retaddr;
 
2255
}
 
2256
 
2505
2257
static void watch_mem_writeb(void *opaque, target_phys_addr_t addr,
2506
2258
                             uint32_t val)
2507
2259
{
2508
 
    check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
 
2260
    addr = check_watchpoint(addr);
2509
2261
    stb_phys(addr, val);
2510
2262
}
2511
2263
 
2512
2264
static void watch_mem_writew(void *opaque, target_phys_addr_t addr,
2513
2265
                             uint32_t val)
2514
2266
{
2515
 
    check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
 
2267
    addr = check_watchpoint(addr);
2516
2268
    stw_phys(addr, val);
2517
2269
}
2518
2270
 
2519
2271
static void watch_mem_writel(void *opaque, target_phys_addr_t addr,
2520
2272
                             uint32_t val)
2521
2273
{
2522
 
    check_watchpoint(addr & ~TARGET_PAGE_MASK, PAGE_WRITE);
 
2274
    addr = check_watchpoint(addr);
2523
2275
    stl_phys(addr, val);
2524
2276
}
2525
2277
 
2534
2286
    watch_mem_writew,
2535
2287
    watch_mem_writel,
2536
2288
};
 
2289
#endif
2537
2290
 
2538
2291
static inline uint32_t subpage_readlen (subpage_t *mmio, target_phys_addr_t addr,
2539
2292
                                 unsigned int len)
2540
2293
{
 
2294
    CPUReadMemoryFunc **mem_read;
2541
2295
    uint32_t ret;
2542
2296
    unsigned int idx;
2543
2297
 
2546
2300
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d\n", __func__,
2547
2301
           mmio, len, addr, idx);
2548
2302
#endif
2549
 
    ret = (**mmio->mem_read[idx][len])(mmio->opaque[idx][0][len], addr);
 
2303
    mem_read = mmio->mem_read[idx];
 
2304
    ret = (*mem_read[len])(mmio->opaque[idx], addr);
2550
2305
 
2551
2306
    return ret;
2552
2307
}
2554
2309
static inline void subpage_writelen (subpage_t *mmio, target_phys_addr_t addr,
2555
2310
                              uint32_t value, unsigned int len)
2556
2311
{
 
2312
    CPUWriteMemoryFunc **mem_write;
2557
2313
    unsigned int idx;
2558
2314
 
2559
2315
    idx = SUBPAGE_IDX(addr - mmio->base);
2561
2317
    printf("%s: subpage %p len %d addr " TARGET_FMT_plx " idx %d value %08x\n", __func__,
2562
2318
           mmio, len, addr, idx, value);
2563
2319
#endif
2564
 
    (**mmio->mem_write[idx][len])(mmio->opaque[idx][1][len], addr, value);
 
2320
    mem_write = mmio->mem_write[idx];
 
2321
    (*mem_write[len])(mmio->opaque[idx], addr, value);
2565
2322
}
2566
2323
 
2567
2324
static uint32_t subpage_readb (void *opaque, target_phys_addr_t addr)
2631
2388
};
2632
2389
 
2633
2390
static int subpage_register (subpage_t *mmio, uint32_t start, uint32_t end,
2634
 
                             ram_addr_t memory)
 
2391
                             int memory)
2635
2392
{
2636
2393
    int idx, eidx;
2637
 
    unsigned int i;
2638
2394
 
2639
2395
    if (start >= TARGET_PAGE_SIZE || end >= TARGET_PAGE_SIZE)
2640
2396
        return -1;
2646
2402
#endif
2647
2403
    memory >>= IO_MEM_SHIFT;
2648
2404
    for (; idx <= eidx; idx++) {
2649
 
        for (i = 0; i < 4; i++) {
2650
 
            if (io_mem_read[memory][i]) {
2651
 
                mmio->mem_read[idx][i] = &io_mem_read[memory][i];
2652
 
                mmio->opaque[idx][0][i] = io_mem_opaque[memory];
2653
 
            }
2654
 
            if (io_mem_write[memory][i]) {
2655
 
                mmio->mem_write[idx][i] = &io_mem_write[memory][i];
2656
 
                mmio->opaque[idx][1][i] = io_mem_opaque[memory];
2657
 
            }
2658
 
        }
 
2405
        mmio->mem_read[idx] = io_mem_read[memory];
 
2406
        mmio->mem_write[idx] = io_mem_write[memory];
 
2407
        mmio->opaque[idx] = io_mem_opaque[memory];
2659
2408
    }
2660
2409
 
2661
2410
    return 0;
2662
2411
}
2663
2412
 
2664
 
static void *subpage_init (target_phys_addr_t base, ram_addr_t *phys,
2665
 
                           ram_addr_t orig_memory)
 
2413
static void *subpage_init (target_phys_addr_t base, uint32_t *phys,
 
2414
                           int orig_memory)
2666
2415
{
2667
2416
    subpage_t *mmio;
2668
2417
    int subpage_memory;
2689
2438
    cpu_register_io_memory(IO_MEM_NOTDIRTY >> IO_MEM_SHIFT, error_mem_read, notdirty_mem_write, NULL);
2690
2439
    io_mem_nb = 5;
2691
2440
 
2692
 
    io_mem_watch = cpu_register_io_memory(0, watch_mem_read,
 
2441
#if defined(CONFIG_SOFTMMU)
 
2442
    io_mem_watch = cpu_register_io_memory(-1, watch_mem_read,
2693
2443
                                          watch_mem_write, NULL);
 
2444
#endif
2694
2445
    /* alloc dirty bits array */
2695
2446
    phys_ram_dirty = qemu_vmalloc(phys_ram_size >> TARGET_PAGE_BITS);
2696
2447
    memset(phys_ram_dirty, 0xff, phys_ram_size >> TARGET_PAGE_BITS);
2698
2449
 
2699
2450
/* mem_read and mem_write are arrays of functions containing the
2700
2451
   function to access byte (index 0), word (index 1) and dword (index
2701
 
   2). Functions can be omitted with a NULL function pointer. The
2702
 
   registered functions may be modified dynamically later.
2703
 
   If io_index is non zero, the corresponding io zone is
2704
 
   modified. If it is zero, a new io zone is allocated. The return
2705
 
   value can be used with cpu_register_physical_memory(). (-1) is
2706
 
   returned if error. */
 
2452
   2). All functions must be supplied. If io_index is non zero, the
 
2453
   corresponding io zone is modified. If it is zero, a new io zone is
 
2454
   allocated. The return value can be used with
 
2455
   cpu_register_physical_memory(). (-1) is returned if error. */
2707
2456
int cpu_register_io_memory(int io_index,
2708
2457
                           CPUReadMemoryFunc **mem_read,
2709
2458
                           CPUWriteMemoryFunc **mem_write,
2710
2459
                           void *opaque)
2711
2460
{
2712
 
    int i, subwidth = 0;
 
2461
    int i;
2713
2462
 
2714
2463
    if (io_index <= 0) {
2715
2464
        if (io_mem_nb >= IO_MEM_NB_ENTRIES)
2721
2470
    }
2722
2471
 
2723
2472
    for(i = 0;i < 3; i++) {
2724
 
        if (!mem_read[i] || !mem_write[i])
2725
 
            subwidth = IO_MEM_SUBWIDTH;
2726
2473
        io_mem_read[io_index][i] = mem_read[i];
2727
2474
        io_mem_write[io_index][i] = mem_write[i];
2728
2475
    }
2729
2476
    io_mem_opaque[io_index] = opaque;
2730
 
    return (io_index << IO_MEM_SHIFT) | subwidth;
 
2477
    return io_index << IO_MEM_SHIFT;
2731
2478
}
2732
2479
 
2733
2480
CPUWriteMemoryFunc **cpu_get_io_memory_write(int io_index)
2740
2487
    return io_mem_read[io_index >> IO_MEM_SHIFT];
2741
2488
}
2742
2489
 
2743
 
#endif /* !defined(CONFIG_USER_ONLY) */
2744
 
 
2745
2490
/* physical memory access (slow version, mainly for debug) */
2746
2491
#if defined(CONFIG_USER_ONLY)
2747
2492
void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
2763
2508
            if (!(flags & PAGE_WRITE))
2764
2509
                return;
2765
2510
            /* XXX: this code should not depend on lock_user */
2766
 
            if (!(p = lock_user(VERIFY_WRITE, addr, l, 0)))
 
2511
            if (!(p = lock_user(VERIFY_WRITE, addr, len, 0)))
2767
2512
                /* FIXME - should this return an error rather than just fail? */
2768
2513
                return;
2769
 
            memcpy(p, buf, l);
2770
 
            unlock_user(p, addr, l);
 
2514
            memcpy(p, buf, len);
 
2515
            unlock_user(p, addr, len);
2771
2516
        } else {
2772
2517
            if (!(flags & PAGE_READ))
2773
2518
                return;
2774
2519
            /* XXX: this code should not depend on lock_user */
2775
 
            if (!(p = lock_user(VERIFY_READ, addr, l, 1)))
 
2520
            if (!(p = lock_user(VERIFY_READ, addr, len, 1)))
2776
2521
                /* FIXME - should this return an error rather than just fail? */
2777
2522
                return;
2778
 
            memcpy(buf, p, l);
 
2523
            memcpy(buf, p, len);
2779
2524
            unlock_user(p, addr, 0);
2780
2525
        }
2781
2526
        len -= l;
3019
2764
        io_index = (pd >> IO_MEM_SHIFT) & (IO_MEM_NB_ENTRIES - 1);
3020
2765
        io_mem_write[io_index][2](io_mem_opaque[io_index], addr, val);
3021
2766
    } else {
3022
 
        unsigned long addr1 = (pd & TARGET_PAGE_MASK) + (addr & ~TARGET_PAGE_MASK);
3023
 
        ptr = phys_ram_base + addr1;
 
2767
        ptr = phys_ram_base + (pd & TARGET_PAGE_MASK) +
 
2768
            (addr & ~TARGET_PAGE_MASK);
3024
2769
        stl_p(ptr, val);
3025
 
 
3026
 
        if (unlikely(in_migration)) {
3027
 
            if (!cpu_physical_memory_is_dirty(addr1)) {
3028
 
                /* invalidate code */
3029
 
                tb_invalidate_phys_page_range(addr1, addr1 + 4, 0);
3030
 
                /* set dirty bit */
3031
 
                phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
3032
 
                    (0xff & ~CODE_DIRTY_FLAG);
3033
 
            }
3034
 
        }
3035
2770
    }
3036
2771
}
3037
2772
 
3148
2883
    return 0;
3149
2884
}
3150
2885
 
3151
 
/* in deterministic execution mode, instructions doing device I/Os
3152
 
   must be at the end of the TB */
3153
 
void cpu_io_recompile(CPUState *env, void *retaddr)
3154
 
{
3155
 
    TranslationBlock *tb;
3156
 
    uint32_t n, cflags;
3157
 
    target_ulong pc, cs_base;
3158
 
    uint64_t flags;
3159
 
 
3160
 
    tb = tb_find_pc((unsigned long)retaddr);
3161
 
    if (!tb) {
3162
 
        cpu_abort(env, "cpu_io_recompile: could not find TB for pc=%p", 
3163
 
                  retaddr);
3164
 
    }
3165
 
    n = env->icount_decr.u16.low + tb->icount;
3166
 
    cpu_restore_state(tb, env, (unsigned long)retaddr, NULL);
3167
 
    /* Calculate how many instructions had been executed before the fault
3168
 
       occurred.  */
3169
 
    n = n - env->icount_decr.u16.low;
3170
 
    /* Generate a new TB ending on the I/O insn.  */
3171
 
    n++;
3172
 
    /* On MIPS and SH, delay slot instructions can only be restarted if
3173
 
       they were already the first instruction in the TB.  If this is not
3174
 
       the first instruction in a TB then re-execute the preceding
3175
 
       branch.  */
3176
 
#if defined(TARGET_MIPS)
3177
 
    if ((env->hflags & MIPS_HFLAG_BMASK) != 0 && n > 1) {
3178
 
        env->active_tc.PC -= 4;
3179
 
        env->icount_decr.u16.low++;
3180
 
        env->hflags &= ~MIPS_HFLAG_BMASK;
3181
 
    }
3182
 
#elif defined(TARGET_SH4)
3183
 
    if ((env->flags & ((DELAY_SLOT | DELAY_SLOT_CONDITIONAL))) != 0
3184
 
            && n > 1) {
3185
 
        env->pc -= 2;
3186
 
        env->icount_decr.u16.low++;
3187
 
        env->flags &= ~(DELAY_SLOT | DELAY_SLOT_CONDITIONAL);
3188
 
    }
3189
 
#endif
3190
 
    /* This should never happen.  */
3191
 
    if (n > CF_COUNT_MASK)
3192
 
        cpu_abort(env, "TB too big during recompile");
3193
 
 
3194
 
    cflags = n | CF_LAST_IO;
3195
 
    pc = tb->pc;
3196
 
    cs_base = tb->cs_base;
3197
 
    flags = tb->flags;
3198
 
    tb_phys_invalidate(tb, -1);
3199
 
    /* FIXME: In theory this could raise an exception.  In practice
3200
 
       we have already translated the block once so it's probably ok.  */
3201
 
    tb_gen_code(env, pc, cs_base, flags, cflags);
3202
 
    /* TODO: If env->pc != tb->pc (i.e. the faulting instruction was not
3203
 
       the first in the TB) then we end up generating a whole new TB and
3204
 
       repeating the fault, which is horribly inefficient.
3205
 
       Better would be to execute just this insn uncached, or generate a
3206
 
       second new TB.  */
3207
 
    cpu_resume_from_signal(env, NULL);
3208
 
}
3209
 
 
3210
2886
void dump_exec_info(FILE *f,
3211
2887
                    int (*cpu_fprintf)(FILE *f, const char *fmt, ...))
3212
2888
{
3234
2910
        }
3235
2911
    }
3236
2912
    /* XXX: avoid using doubles ? */
3237
 
    cpu_fprintf(f, "Translation buffer state:\n");
3238
 
    cpu_fprintf(f, "gen code size       %ld/%ld\n",
3239
 
                code_gen_ptr - code_gen_buffer, code_gen_buffer_max_size);
3240
 
    cpu_fprintf(f, "TB count            %d/%d\n", 
3241
 
                nb_tbs, code_gen_max_blocks);
 
2913
    cpu_fprintf(f, "TB count            %d\n", nb_tbs);
3242
2914
    cpu_fprintf(f, "TB avg target size  %d max=%d bytes\n",
3243
2915
                nb_tbs ? target_code_size / nb_tbs : 0,
3244
2916
                max_target_code_size);
3253
2925
                nb_tbs ? (direct_jmp_count * 100) / nb_tbs : 0,
3254
2926
                direct_jmp2_count,
3255
2927
                nb_tbs ? (direct_jmp2_count * 100) / nb_tbs : 0);
3256
 
    cpu_fprintf(f, "\nStatistics:\n");
3257
2928
    cpu_fprintf(f, "TB flush count      %d\n", tb_flush_count);
3258
2929
    cpu_fprintf(f, "TB invalidate count %d\n", tb_phys_invalidate_count);
3259
2930
    cpu_fprintf(f, "TLB flush count     %d\n", tlb_flush_count);
3260
 
    tcg_dump_info(f, cpu_fprintf);
3261
2931
}
3262
2932
 
3263
2933
#if !defined(CONFIG_USER_ONLY)