main.c 75.3 KB
Newer Older
1
/*
2
 *  qemu user main
3
 *
bellard's avatar
bellard committed
4
 *  Copyright (c) 2003-2008 Fabrice Bellard
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
bellard's avatar
bellard committed
23
#include <string.h>
24
#include <errno.h>
bellard's avatar
bellard committed
25
#include <unistd.h>
26

bellard's avatar
bellard committed
27
#include "qemu.h"
28

bellard's avatar
bellard committed
29
#define DEBUG_LOGFILE "/tmp/qemu.log"
bellard's avatar
bellard committed
30

bellard's avatar
bellard committed
31
static const char *interp_prefix = CONFIG_QEMU_PREFIX;
32
const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
bellard's avatar
bellard committed
33

bellard's avatar
bellard committed
34
#if defined(__i386__) && !defined(CONFIG_STATIC)
35
36
37
/* Force usage of an ELF interpreter even if it is an ELF shared
   object ! */
const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
bellard's avatar
bellard committed
38
#endif
bellard's avatar
bellard committed
39

40
/* for recent libc, we add these dummy symbols which are not declared
bellard's avatar
bellard committed
41
   when generating a linked object (bug in ld ?) */
bellard's avatar
bellard committed
42
#if (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined(CONFIG_STATIC)
43
44
45
46
47
48
49
50
51
52
53
54
55
56
asm(".globl __preinit_array_start\n"
    ".globl __preinit_array_end\n"
    ".globl __init_array_start\n"
    ".globl __init_array_end\n"
    ".globl __fini_array_start\n"
    ".globl __fini_array_end\n"
    ".section \".rodata\"\n"
    "__preinit_array_start:\n"
    "__preinit_array_end:\n"
    "__init_array_start:\n"
    "__init_array_end:\n"
    "__fini_array_start:\n"
    "__fini_array_end:\n"
    ".long 0\n");
bellard's avatar
bellard committed
57
58
#endif

bellard's avatar
bellard committed
59
60
61
62
/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
   we allocate a bigger stack. Need a better solution, for example
   by remapping the process stack directly at the right place */
unsigned long x86_stack_size = 512 * 1024;
63
64
65
66
67
68
69
70
71
72

void gemu_log(const char *fmt, ...)
{
    va_list ap;

    va_start(ap, fmt);
    vfprintf(stderr, fmt, ap);
    va_end(ap);
}

bellard's avatar
bellard committed
73
void cpu_outb(CPUState *env, int addr, int val)
bellard's avatar
bellard committed
74
75
76
77
{
    fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
}

bellard's avatar
bellard committed
78
void cpu_outw(CPUState *env, int addr, int val)
bellard's avatar
bellard committed
79
80
81
82
{
    fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
}

bellard's avatar
bellard committed
83
void cpu_outl(CPUState *env, int addr, int val)
bellard's avatar
bellard committed
84
85
86
87
{
    fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
}

bellard's avatar
bellard committed
88
int cpu_inb(CPUState *env, int addr)
bellard's avatar
bellard committed
89
90
91
92
93
{
    fprintf(stderr, "inb: port=0x%04x\n", addr);
    return 0;
}

bellard's avatar
bellard committed
94
int cpu_inw(CPUState *env, int addr)
bellard's avatar
bellard committed
95
96
97
98
99
{
    fprintf(stderr, "inw: port=0x%04x\n", addr);
    return 0;
}

bellard's avatar
bellard committed
100
int cpu_inl(CPUState *env, int addr)
bellard's avatar
bellard committed
101
102
103
104
105
{
    fprintf(stderr, "inl: port=0x%04x\n", addr);
    return 0;
}

106
int cpu_get_pic_interrupt(CPUState *env)
107
108
109
110
{
    return -1;
}

bellard's avatar
bellard committed
111
112
/* timers for rdtsc */

113
#if 0
bellard's avatar
bellard committed
114
115
116
117
118
119
120
121
122
123

static uint64_t emu_time;

int64_t cpu_get_real_ticks(void)
{
    return emu_time++;
}

#endif

124
125
126
127
#ifdef TARGET_I386
/***********************************************************/
/* CPUX86 core interface */

bellard's avatar
bellard committed
128
129
130
131
void cpu_smm_update(CPUState *env)
{
}

bellard's avatar
bellard committed
132
133
134
135
136
uint64_t cpu_get_tsc(CPUX86State *env)
{
    return cpu_get_real_ticks();
}

137
static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
138
                     int flags)
bellard's avatar
bellard committed
139
{
140
    unsigned int e1, e2;
141
    uint32_t *p;
bellard's avatar
bellard committed
142
143
    e1 = (addr << 16) | (limit & 0xffff);
    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
144
    e2 |= flags;
145
146
147
    p = ptr;
    p[0] = tswapl(e1);
    p[1] = tswapl(e2);
148
149
}

bellard's avatar
bellard committed
150
151
152
153
154
#if TARGET_X86_64
uint64_t idt_table[512];

static void set_gate64(void *ptr, unsigned int type, unsigned int dpl,
                       uint64_t addr, unsigned int sel)
155
{
bellard's avatar
bellard committed
156
    uint32_t *p, e1, e2;
157
158
    e1 = (addr & 0xffff) | (sel << 16);
    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
159
    p = ptr;
bellard's avatar
bellard committed
160
161
162
163
    p[0] = tswap32(e1);
    p[1] = tswap32(e2);
    p[2] = tswap32(addr >> 32);
    p[3] = 0;
bellard's avatar
bellard committed
164
}
bellard's avatar
bellard committed
165
166
167
168
169
170
/* only dpl matters as we do only user space emulation */
static void set_idt(int n, unsigned int dpl)
{
    set_gate64(idt_table + n * 2, 0, dpl, 0, 0);
}
#else
171
172
uint64_t idt_table[256];

bellard's avatar
bellard committed
173
174
175
static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
                     uint32_t addr, unsigned int sel)
{
bellard's avatar
bellard committed
176
    uint32_t *p, e1, e2;
bellard's avatar
bellard committed
177
178
179
    e1 = (addr & 0xffff) | (sel << 16);
    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
    p = ptr;
bellard's avatar
bellard committed
180
181
    p[0] = tswap32(e1);
    p[1] = tswap32(e2);
bellard's avatar
bellard committed
182
183
}

184
185
186
187
188
/* only dpl matters as we do only user space emulation */
static void set_idt(int n, unsigned int dpl)
{
    set_gate(idt_table + n, 0, dpl, 0, 0);
}
bellard's avatar
bellard committed
189
#endif
190

bellard's avatar
bellard committed
191
void cpu_loop(CPUX86State *env)
bellard's avatar
bellard committed
192
{
bellard's avatar
bellard committed
193
    int trapnr;
194
    abi_ulong pc;
bellard's avatar
bellard committed
195
    target_siginfo_t info;
bellard's avatar
bellard committed
196

bellard's avatar
bellard committed
197
    for(;;) {
bellard's avatar
bellard committed
198
199
        trapnr = cpu_x86_exec(env);
        switch(trapnr) {
200
        case 0x80:
bellard's avatar
bellard committed
201
            /* linux syscall from int $0x80 */
202
203
            env->regs[R_EAX] = do_syscall(env,
                                          env->regs[R_EAX],
204
205
206
207
208
209
210
                                          env->regs[R_EBX],
                                          env->regs[R_ECX],
                                          env->regs[R_EDX],
                                          env->regs[R_ESI],
                                          env->regs[R_EDI],
                                          env->regs[R_EBP]);
            break;
bellard's avatar
bellard committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef TARGET_ABI32
        case EXCP_SYSCALL:
            /* linux syscall from syscall intruction */
            env->regs[R_EAX] = do_syscall(env,
                                          env->regs[R_EAX],
                                          env->regs[R_EDI],
                                          env->regs[R_ESI],
                                          env->regs[R_EDX],
                                          env->regs[10],
                                          env->regs[8],
                                          env->regs[9]);
            env->eip = env->exception_next_eip;
            break;
#endif
225
226
227
228
229
230
231
232
        case EXCP0B_NOSEG:
        case EXCP0C_STACK:
            info.si_signo = SIGBUS;
            info.si_errno = 0;
            info.si_code = TARGET_SI_KERNEL;
            info._sifields._sigfault._addr = 0;
            queue_signal(info.si_signo, &info);
            break;
bellard's avatar
bellard committed
233
        case EXCP0D_GPF:
bellard's avatar
bellard committed
234
            /* XXX: potential problem if ABI32 */
235
#ifndef TARGET_X86_64
bellard's avatar
bellard committed
236
            if (env->eflags & VM_MASK) {
bellard's avatar
bellard committed
237
                handle_vm86_fault(env);
238
239
240
            } else
#endif
            {
241
242
243
244
245
                info.si_signo = SIGSEGV;
                info.si_errno = 0;
                info.si_code = TARGET_SI_KERNEL;
                info._sifields._sigfault._addr = 0;
                queue_signal(info.si_signo, &info);
bellard's avatar
bellard committed
246
247
            }
            break;
bellard's avatar
bellard committed
248
249
250
251
252
253
254
        case EXCP0E_PAGE:
            info.si_signo = SIGSEGV;
            info.si_errno = 0;
            if (!(env->error_code & 1))
                info.si_code = TARGET_SEGV_MAPERR;
            else
                info.si_code = TARGET_SEGV_ACCERR;
bellard's avatar
bellard committed
255
            info._sifields._sigfault._addr = env->cr[2];
bellard's avatar
bellard committed
256
257
            queue_signal(info.si_signo, &info);
            break;
bellard's avatar
bellard committed
258
        case EXCP00_DIVZ:
259
#ifndef TARGET_X86_64
bellard's avatar
bellard committed
260
            if (env->eflags & VM_MASK) {
bellard's avatar
bellard committed
261
                handle_vm86_trap(env, trapnr);
262
263
264
            } else
#endif
            {
bellard's avatar
bellard committed
265
266
267
268
269
270
271
                /* division by zero */
                info.si_signo = SIGFPE;
                info.si_errno = 0;
                info.si_code = TARGET_FPE_INTDIV;
                info._sifields._sigfault._addr = env->eip;
                queue_signal(info.si_signo, &info);
            }
bellard's avatar
bellard committed
272
            break;
bellard's avatar
bellard committed
273
274
        case EXCP01_SSTP:
        case EXCP03_INT3:
275
#ifndef TARGET_X86_64
bellard's avatar
bellard committed
276
277
            if (env->eflags & VM_MASK) {
                handle_vm86_trap(env, trapnr);
278
279
280
            } else
#endif
            {
bellard's avatar
bellard committed
281
282
283
284
285
286
287
288
289
290
291
292
                info.si_signo = SIGTRAP;
                info.si_errno = 0;
                if (trapnr == EXCP01_SSTP) {
                    info.si_code = TARGET_TRAP_BRKPT;
                    info._sifields._sigfault._addr = env->eip;
                } else {
                    info.si_code = TARGET_SI_KERNEL;
                    info._sifields._sigfault._addr = 0;
                }
                queue_signal(info.si_signo, &info);
            }
            break;
bellard's avatar
bellard committed
293
294
        case EXCP04_INTO:
        case EXCP05_BOUND:
295
#ifndef TARGET_X86_64
bellard's avatar
bellard committed
296
            if (env->eflags & VM_MASK) {
bellard's avatar
bellard committed
297
                handle_vm86_trap(env, trapnr);
298
299
300
            } else
#endif
            {
bellard's avatar
bellard committed
301
302
                info.si_signo = SIGSEGV;
                info.si_errno = 0;
bellard's avatar
bellard committed
303
                info.si_code = TARGET_SI_KERNEL;
bellard's avatar
bellard committed
304
305
306
                info._sifields._sigfault._addr = 0;
                queue_signal(info.si_signo, &info);
            }
bellard's avatar
bellard committed
307
308
309
310
311
312
313
314
315
316
317
            break;
        case EXCP06_ILLOP:
            info.si_signo = SIGILL;
            info.si_errno = 0;
            info.si_code = TARGET_ILL_ILLOPN;
            info._sifields._sigfault._addr = env->eip;
            queue_signal(info.si_signo, &info);
            break;
        case EXCP_INTERRUPT:
            /* just indicate that signals should be handled asap */
            break;
318
319
320
321
322
323
324
325
326
327
328
329
330
331
        case EXCP_DEBUG:
            {
                int sig;

                sig = gdb_handlesig (env, TARGET_SIGTRAP);
                if (sig)
                  {
                    info.si_signo = sig;
                    info.si_errno = 0;
                    info.si_code = TARGET_TRAP_BRKPT;
                    queue_signal(info.si_signo, &info);
                  }
            }
            break;
bellard's avatar
bellard committed
332
        default:
bellard's avatar
bellard committed
333
            pc = env->segs[R_CS].base + env->eip;
334
            fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
bellard's avatar
bellard committed
335
                    (long)pc, trapnr);
bellard's avatar
bellard committed
336
337
            abort();
        }
bellard's avatar
bellard committed
338
        process_pending_signals(env);
bellard's avatar
bellard committed
339
340
    }
}
bellard's avatar
bellard committed
341
342
343
344
#endif

#ifdef TARGET_ARM

345
/* XXX: find a better solution */
346
extern void tb_invalidate_page_range(abi_ulong start, abi_ulong end);
347

348
static void arm_cache_flush(abi_ulong start, abi_ulong last)
349
{
350
    abi_ulong addr, last1;
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365

    if (last < start)
        return;
    addr = start;
    for(;;) {
        last1 = ((addr + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK) - 1;
        if (last1 > last)
            last1 = last;
        tb_invalidate_page_range(addr, last1 + 1);
        if (last1 == last)
            break;
        addr = last1 + 1;
    }
}

bellard's avatar
bellard committed
366
367
368
369
370
void cpu_loop(CPUARMState *env)
{
    int trapnr;
    unsigned int n, insn;
    target_siginfo_t info;
bellard's avatar
bellard committed
371
    uint32_t addr;
372

bellard's avatar
bellard committed
373
374
375
376
    for(;;) {
        trapnr = cpu_arm_exec(env);
        switch(trapnr) {
        case EXCP_UDEF:
377
378
379
380
381
382
            {
                TaskState *ts = env->opaque;
                uint32_t opcode;

                /* we handle the FPU emulation here, as Linux */
                /* we get the opcode */
383
384
                /* FIXME - what to do if get_user() fails? */
                get_user_u32(opcode, env->regs[15]);
385

386
                if (EmulateAll(opcode, &ts->fpa, env) == 0) {
387
388
389
390
391
392
393
394
395
396
                    info.si_signo = SIGILL;
                    info.si_errno = 0;
                    info.si_code = TARGET_ILL_ILLOPN;
                    info._sifields._sigfault._addr = env->regs[15];
                    queue_signal(info.si_signo, &info);
                } else {
                    /* increment PC */
                    env->regs[15] += 4;
                }
            }
bellard's avatar
bellard committed
397
398
            break;
        case EXCP_SWI:
pbrook's avatar
pbrook committed
399
        case EXCP_BKPT:
bellard's avatar
bellard committed
400
            {
pbrook's avatar
pbrook committed
401
                env->eabi = 1;
bellard's avatar
bellard committed
402
                /* system call */
pbrook's avatar
pbrook committed
403
404
                if (trapnr == EXCP_BKPT) {
                    if (env->thumb) {
405
406
                        /* FIXME - what to do if get_user() fails? */
                        get_user_u16(insn, env->regs[15]);
pbrook's avatar
pbrook committed
407
408
409
                        n = insn & 0xff;
                        env->regs[15] += 2;
                    } else {
410
411
                        /* FIXME - what to do if get_user() fails? */
                        get_user_u32(insn, env->regs[15]);
pbrook's avatar
pbrook committed
412
413
414
                        n = (insn & 0xf) | ((insn >> 4) & 0xff0);
                        env->regs[15] += 4;
                    }
bellard's avatar
bellard committed
415
                } else {
pbrook's avatar
pbrook committed
416
                    if (env->thumb) {
417
418
                        /* FIXME - what to do if get_user() fails? */
                        get_user_u16(insn, env->regs[15] - 2);
pbrook's avatar
pbrook committed
419
420
                        n = insn & 0xff;
                    } else {
421
422
                        /* FIXME - what to do if get_user() fails? */
                        get_user_u32(insn, env->regs[15] - 4);
pbrook's avatar
pbrook committed
423
424
                        n = insn & 0xffffff;
                    }
bellard's avatar
bellard committed
425
426
                }

427
428
                if (n == ARM_NR_cacheflush) {
                    arm_cache_flush(env->regs[0], env->regs[1]);
429
430
431
                } else if (n == ARM_NR_semihosting
                           || n == ARM_NR_thumb_semihosting) {
                    env->regs[0] = do_arm_semihosting (env);
pbrook's avatar
pbrook committed
432
                } else if (n == 0 || n >= ARM_SYSCALL_BASE
bellard's avatar
bellard committed
433
                           || (env->thumb && n == ARM_THUMB_SYSCALL)) {
bellard's avatar
bellard committed
434
                    /* linux syscall */
pbrook's avatar
pbrook committed
435
                    if (env->thumb || n == 0) {
bellard's avatar
bellard committed
436
437
438
                        n = env->regs[7];
                    } else {
                        n -= ARM_SYSCALL_BASE;
pbrook's avatar
pbrook committed
439
                        env->eabi = 0;
bellard's avatar
bellard committed
440
                    }
441
442
                    env->regs[0] = do_syscall(env,
                                              n,
bellard's avatar
bellard committed
443
444
445
446
447
                                              env->regs[0],
                                              env->regs[1],
                                              env->regs[2],
                                              env->regs[3],
                                              env->regs[4],
bellard's avatar
bellard committed
448
                                              env->regs[5]);
bellard's avatar
bellard committed
449
450
451
452
453
                } else {
                    goto error;
                }
            }
            break;
bellard's avatar
bellard committed
454
455
456
        case EXCP_INTERRUPT:
            /* just indicate that signals should be handled asap */
            break;
bellard's avatar
bellard committed
457
        case EXCP_PREFETCH_ABORT:
bellard's avatar
bellard committed
458
459
            addr = env->cp15.c6_data;
            goto do_segv;
bellard's avatar
bellard committed
460
        case EXCP_DATA_ABORT:
bellard's avatar
bellard committed
461
462
463
            addr = env->cp15.c6_insn;
            goto do_segv;
        do_segv:
bellard's avatar
bellard committed
464
465
466
467
468
            {
                info.si_signo = SIGSEGV;
                info.si_errno = 0;
                /* XXX: check env->error_code */
                info.si_code = TARGET_SEGV_MAPERR;
bellard's avatar
bellard committed
469
                info._sifields._sigfault._addr = addr;
bellard's avatar
bellard committed
470
471
472
                queue_signal(info.si_signo, &info);
            }
            break;
473
474
475
476
477
478
479
480
481
482
483
484
485
486
        case EXCP_DEBUG:
            {
                int sig;

                sig = gdb_handlesig (env, TARGET_SIGTRAP);
                if (sig)
                  {
                    info.si_signo = sig;
                    info.si_errno = 0;
                    info.si_code = TARGET_TRAP_BRKPT;
                    queue_signal(info.si_signo, &info);
                  }
            }
            break;
bellard's avatar
bellard committed
487
488
        default:
        error:
489
            fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
bellard's avatar
bellard committed
490
                    trapnr);
bellard's avatar
bellard committed
491
            cpu_dump_state(env, stderr, fprintf, 0);
bellard's avatar
bellard committed
492
493
494
495
496
497
498
            abort();
        }
        process_pending_signals(env);
    }
}

#endif
bellard's avatar
bellard committed
499

500
501
#ifdef TARGET_SPARC

502
503
//#define DEBUG_WIN

504
505
/* WARNING: dealing with register windows _is_ complicated. More info
   can be found at http://www.sics.se/~psm/sparcstack.html */
506
507
508
509
510
511
512
513
514
515
static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
{
    index = (index + cwp * 16) & (16 * NWINDOWS - 1);
    /* wrap handling : if cwp is on the last window, then we use the
       registers 'after' the end */
    if (index < 8 && env->cwp == (NWINDOWS - 1))
        index += (16 * NWINDOWS);
    return index;
}

516
517
/* save the register window 'cwp1' */
static inline void save_window_offset(CPUSPARCState *env, int cwp1)
518
{
519
    unsigned int i;
520
    abi_ulong sp_ptr;
521

522
    sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
523
#if defined(DEBUG_WIN)
524
    printf("win_overflow: sp_ptr=0x%x save_cwp=%d\n",
525
526
           (int)sp_ptr, cwp1);
#endif
527
    for(i = 0; i < 16; i++) {
528
529
        /* FIXME - what to do if put_user() fails? */
        put_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
530
        sp_ptr += sizeof(abi_ulong);
531
    }
532
533
534
535
}

static void save_window(CPUSPARCState *env)
{
536
#ifndef TARGET_SPARC64
537
538
539
540
541
    unsigned int new_wim;
    new_wim = ((env->wim >> 1) | (env->wim << (NWINDOWS - 1))) &
        ((1LL << NWINDOWS) - 1);
    save_window_offset(env, (env->cwp - 2) & (NWINDOWS - 1));
    env->wim = new_wim;
542
543
544
545
546
#else
    save_window_offset(env, (env->cwp - 2) & (NWINDOWS - 1));
    env->cansave++;
    env->canrestore--;
#endif
547
548
549
550
551
}

static void restore_window(CPUSPARCState *env)
{
    unsigned int new_wim, i, cwp1;
552
    abi_ulong sp_ptr;
553

554
555
    new_wim = ((env->wim << 1) | (env->wim >> (NWINDOWS - 1))) &
        ((1LL << NWINDOWS) - 1);
556

557
558
    /* restore the invalid window */
    cwp1 = (env->cwp + 1) & (NWINDOWS - 1);
559
    sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
560
#if defined(DEBUG_WIN)
561
    printf("win_underflow: sp_ptr=0x%x load_cwp=%d\n",
562
563
           (int)sp_ptr, cwp1);
#endif
564
    for(i = 0; i < 16; i++) {
565
566
        /* FIXME - what to do if get_user() fails? */
        get_user_ual(env->regbase[get_reg_index(env, cwp1, 8 + i)], sp_ptr);
567
        sp_ptr += sizeof(abi_ulong);
568
    }
569
    env->wim = new_wim;
570
571
572
573
574
575
#ifdef TARGET_SPARC64
    env->canrestore++;
    if (env->cleanwin < NWINDOWS - 1)
	env->cleanwin++;
    env->cansave--;
#endif
576
577
578
579
580
}

static void flush_windows(CPUSPARCState *env)
{
    int offset, cwp1;
581
582

    offset = 1;
583
584
    for(;;) {
        /* if restore would invoke restore_window(), then we can stop */
585
        cwp1 = (env->cwp + offset) & (NWINDOWS - 1);
586
587
        if (env->wim & (1 << cwp1))
            break;
588
        save_window_offset(env, cwp1);
589
590
        offset++;
    }
591
592
593
594
595
    /* set wim so that restore will reload the registers */
    cwp1 = (env->cwp + 1) & (NWINDOWS - 1);
    env->wim = 1 << cwp1;
#if defined(DEBUG_WIN)
    printf("flush_windows: nb=%d\n", offset - 1);
596
#endif
597
}
598

599
600
void cpu_loop (CPUSPARCState *env)
{
601
    int trapnr, ret;
bellard's avatar
bellard committed
602
    target_siginfo_t info;
603

604
605
    while (1) {
        trapnr = cpu_sparc_exec (env);
606

607
        switch (trapnr) {
608
#ifndef TARGET_SPARC64
609
        case 0x88:
610
        case 0x90:
611
#else
612
        case 0x110:
613
614
        case 0x16d:
#endif
615
            ret = do_syscall (env, env->gregs[1],
616
617
                              env->regwptr[0], env->regwptr[1],
                              env->regwptr[2], env->regwptr[3],
618
619
                              env->regwptr[4], env->regwptr[5]);
            if ((unsigned int)ret >= (unsigned int)(-515)) {
620
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
bellard's avatar
bellard committed
621
622
                env->xcc |= PSR_CARRY;
#else
623
                env->psr |= PSR_CARRY;
bellard's avatar
bellard committed
624
#endif
625
626
                ret = -ret;
            } else {
627
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
bellard's avatar
bellard committed
628
629
                env->xcc &= ~PSR_CARRY;
#else
630
                env->psr &= ~PSR_CARRY;
bellard's avatar
bellard committed
631
#endif
632
633
634
635
636
637
638
            }
            env->regwptr[0] = ret;
            /* next instruction */
            env->pc = env->npc;
            env->npc = env->npc + 4;
            break;
        case 0x83: /* flush windows */
639
640
641
#ifdef TARGET_ABI32
        case 0x103:
#endif
642
            flush_windows(env);
643
644
645
646
            /* next instruction */
            env->pc = env->npc;
            env->npc = env->npc + 4;
            break;
bellard's avatar
bellard committed
647
#ifndef TARGET_SPARC64
648
649
650
651
652
653
        case TT_WIN_OVF: /* window overflow */
            save_window(env);
            break;
        case TT_WIN_UNF: /* window underflow */
            restore_window(env);
            break;
bellard's avatar
bellard committed
654
655
656
657
658
659
660
661
662
663
664
        case TT_TFAULT:
        case TT_DFAULT:
            {
                info.si_signo = SIGSEGV;
                info.si_errno = 0;
                /* XXX: check env->error_code */
                info.si_code = TARGET_SEGV_MAPERR;
                info._sifields._sigfault._addr = env->mmuregs[4];
                queue_signal(info.si_signo, &info);
            }
            break;
bellard's avatar
bellard committed
665
#else
666
667
668
669
670
671
        case TT_SPILL: /* window overflow */
            save_window(env);
            break;
        case TT_FILL: /* window underflow */
            restore_window(env);
            break;
blueswir1's avatar
blueswir1 committed
672
673
674
675
676
677
678
679
680
681
682
683
684
685
        case TT_TFAULT:
        case TT_DFAULT:
            {
                info.si_signo = SIGSEGV;
                info.si_errno = 0;
                /* XXX: check env->error_code */
                info.si_code = TARGET_SEGV_MAPERR;
                if (trapnr == TT_DFAULT)
                    info._sifields._sigfault._addr = env->dmmuregs[4];
                else
                    info._sifields._sigfault._addr = env->tpc[env->tl];
                queue_signal(info.si_signo, &info);
            }
            break;
686
#ifndef TARGET_ABI32
687
688
689
690
691
692
693
694
        case 0x16e:
            flush_windows(env);
            sparc64_get_context(env);
            break;
        case 0x16f:
            flush_windows(env);
            sparc64_set_context(env);
            break;
695
#endif
bellard's avatar
bellard committed
696
#endif
bellard's avatar
bellard committed
697
698
699
        case EXCP_INTERRUPT:
            /* just indicate that signals should be handled asap */
            break;
700
701
702
703
704
705
706
707
708
709
710
711
712
713
        case EXCP_DEBUG:
            {
                int sig;

                sig = gdb_handlesig (env, TARGET_SIGTRAP);
                if (sig)
                  {
                    info.si_signo = sig;
                    info.si_errno = 0;
                    info.si_code = TARGET_TRAP_BRKPT;
                    queue_signal(info.si_signo, &info);
                  }
            }
            break;
714
715
        default:
            printf ("Unhandled trap: 0x%x\n", trapnr);
bellard's avatar
bellard committed
716
            cpu_dump_state(env, stderr, fprintf, 0);
717
718
719
720
            exit (1);
        }
        process_pending_signals (env);
    }
721
722
723
724
}

#endif

725
#ifdef TARGET_PPC
726
727
728
729
730
static inline uint64_t cpu_ppc_get_tb (CPUState *env)
{
    /* TO FIX */
    return 0;
}
731

732
733
734
735
uint32_t cpu_ppc_load_tbl (CPUState *env)
{
    return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
}
736

737
738
739
740
uint32_t cpu_ppc_load_tbu (CPUState *env)
{
    return cpu_ppc_get_tb(env) >> 32;
}
741

742
uint32_t cpu_ppc_load_atbl (CPUState *env)
743
{
744
    return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
745
}
746

747
uint32_t cpu_ppc_load_atbu (CPUState *env)
748
{
749
    return cpu_ppc_get_tb(env) >> 32;
750
}
751
752
753
754
755

uint32_t cpu_ppc601_load_rtcu (CPUState *env)
__attribute__ (( alias ("cpu_ppc_load_tbu") ));

uint32_t cpu_ppc601_load_rtcl (CPUState *env)
756
{
757
    return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
758
}
759

760
761
762
763
764
765
766
767
768
769
770
/* XXX: to be fixed */
int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
{
    return -1;
}

int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
{
    return -1;
}

771
772
773
774
775
776
777
778
779
780
#define EXCP_DUMP(env, fmt, args...)                                         \
do {                                                                          \
    fprintf(stderr, fmt , ##args);                                            \
    cpu_dump_state(env, stderr, fprintf, 0);                                  \
    if (loglevel != 0) {                                                      \
        fprintf(logfile, fmt , ##args);                                       \
        cpu_dump_state(env, logfile, fprintf, 0);                             \
    }                                                                         \
} while (0)

781
782
783
void cpu_loop(CPUPPCState *env)
{
    target_siginfo_t info;
bellard's avatar
bellard committed
784
785
    int trapnr;
    uint32_t ret;
786

787
788
789
    for(;;) {
        trapnr = cpu_ppc_exec(env);
        switch(trapnr) {
790
791
        case POWERPC_EXCP_NONE:
            /* Just go on */
792
            break;
793
794
795
        case POWERPC_EXCP_CRITICAL: /* Critical input                        */
            cpu_abort(env, "Critical interrupt while in user mode. "
                      "Aborting\n");
bellard's avatar
bellard committed
796
            break;
797
798
799
800
801
802
803
804
        case POWERPC_EXCP_MCHECK:   /* Machine check exception               */
            cpu_abort(env, "Machine check exception while in user mode. "
                      "Aborting\n");
            break;
        case POWERPC_EXCP_DSI:      /* Data storage exception                */
            EXCP_DUMP(env, "Invalid data memory access: 0x" ADDRX "\n",
                      env->spr[SPR_DAR]);
            /* XXX: check this. Seems bugged */
805
806
            switch (env->error_code & 0xFF000000) {
            case 0x40000000:
bellard's avatar
bellard committed
807
808
809
810
                info.si_signo = TARGET_SIGSEGV;
                info.si_errno = 0;
                info.si_code = TARGET_SEGV_MAPERR;
                break;
811
            case 0x04000000:
bellard's avatar
bellard committed
812
813
814
815
                info.si_signo = TARGET_SIGILL;
                info.si_errno = 0;
                info.si_code = TARGET_ILL_ILLADR;
                break;
816
            case 0x08000000:
bellard's avatar
bellard committed
817
818
819
820
821
822
                info.si_signo = TARGET_SIGSEGV;
                info.si_errno = 0;
                info.si_code = TARGET_SEGV_ACCERR;
                break;
            default:
                /* Let's send a regular segfault... */
823
824
                EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
                          env->error_code);
bellard's avatar
bellard committed
825
826
827
828
829
                info.si_signo = TARGET_SIGSEGV;
                info.si_errno = 0;
                info.si_code = TARGET_SEGV_MAPERR;
                break;
            }
830
831
832
            info._sifields._sigfault._addr = env->nip;
            queue_signal(info.si_signo, &info);
            break;
833
834
        case POWERPC_EXCP_ISI:      /* Instruction storage exception         */
            EXCP_DUMP(env, "Invalid instruction fetch: 0x\n" ADDRX "\n",
835
                      env->spr[SPR_SRR0]);
836
            /* XXX: check this */
837
838
            switch (env->error_code & 0xFF000000) {
            case 0x40000000:
bellard's avatar
bellard committed
839
                info.si_signo = TARGET_SIGSEGV;
840
            info.si_errno = 0;
bellard's avatar
bellard committed
841
842
                info.si_code = TARGET_SEGV_MAPERR;
                break;
843
844
            case 0x10000000:
            case 0x08000000:
bellard's avatar
bellard committed
845
846
847
848
849
850
                info.si_signo = TARGET_SIGSEGV;
                info.si_errno = 0;
                info.si_code = TARGET_SEGV_ACCERR;
                break;
            default:
                /* Let's send a regular segfault... */
851
852
                EXCP_DUMP(env, "Invalid segfault errno (%02x)\n",
                          env->error_code);
bellard's avatar
bellard committed
853
854
855
856
857
858
                info.si_signo = TARGET_SIGSEGV;
                info.si_errno = 0;
                info.si_code = TARGET_SEGV_MAPERR;
                break;
            }
            info._sifields._sigfault._addr = env->nip - 4;
859
860
            queue_signal(info.si_signo, &info);
            break;
861
862
863
864
865
866
867
        case POWERPC_EXCP_EXTERNAL: /* External input                        */
            cpu_abort(env, "External interrupt while in user mode. "
                      "Aborting\n");
            break;
        case POWERPC_EXCP_ALIGN:    /* Alignment exception                   */
            EXCP_DUMP(env, "Unaligned memory access\n");
            /* XXX: check this */
bellard's avatar
bellard committed
868
            info.si_signo = TARGET_SIGBUS;
869
            info.si_errno = 0;
bellard's avatar
bellard committed
870
871
            info.si_code = TARGET_BUS_ADRALN;
            info._sifields._sigfault._addr = env->nip - 4;
872
873
            queue_signal(info.si_signo, &info);
            break;
874
875
        case POWERPC_EXCP_PROGRAM:  /* Program exception                     */
            /* XXX: check this */
bellard's avatar
bellard committed
876
            switch (env->error_code & ~0xF) {
877
878
            case POWERPC_EXCP_FP:
                EXCP_DUMP(env, "Floating point program exception\n");
bellard's avatar
bellard committed
879
880
881
                info.si_signo = TARGET_SIGFPE;
                info.si_errno = 0;
                switch (env->error_code & 0xF) {
882
                case POWERPC_EXCP_FP_OX:
bellard's avatar
bellard committed
883
884
                    info.si_code = TARGET_FPE_FLTOVF;
                    break;
885
                case POWERPC_EXCP_FP_UX:
bellard's avatar
bellard committed
886
887
                    info.si_code = TARGET_FPE_FLTUND;
                    break;
888
889
                case POWERPC_EXCP_FP_ZX:
                case POWERPC_EXCP_FP_VXZDZ:
bellard's avatar
bellard committed
890
891
                    info.si_code = TARGET_FPE_FLTDIV;
                    break;
892
                case POWERPC_EXCP_FP_XX:
bellard's avatar
bellard committed
893
894
                    info.si_code = TARGET_FPE_FLTRES;
                    break;
895
                case POWERPC_EXCP_FP_VXSOFT:
bellard's avatar
bellard committed
896
897
                    info.si_code = TARGET_FPE_FLTINV;
                    break;
898
                case POWERPC_EXCP_FP_VXSNAN:
899
900
901
902
903
904
                case POWERPC_EXCP_FP_VXISI:
                case POWERPC_EXCP_FP_VXIDI:
                case POWERPC_EXCP_FP_VXIMZ:
                case POWERPC_EXCP_FP_VXVC:
                case POWERPC_EXCP_FP_VXSQRT:
                case POWERPC_EXCP_FP_VXCVI:
bellard's avatar
bellard committed
905
906
907
                    info.si_code = TARGET_FPE_FLTSUB;
                    break;
                default:
908
909
910
                    EXCP_DUMP(env, "Unknown floating point exception (%02x)\n",
                              env->error_code);
                    break;
bellard's avatar
bellard committed
911
                }
912
913
914
                break;
            case POWERPC_EXCP_INVAL:
                EXCP_DUMP(env, "Invalid instruction\n");
bellard's avatar
bellard committed
915
916
917
                info.si_signo = TARGET_SIGILL;
                info.si_errno = 0;
                switch (env->error_code & 0xF) {
918
                case POWERPC_EXCP_INVAL_INVAL:
bellard's avatar
bellard committed
919
920
                    info.si_code = TARGET_ILL_ILLOPC;
                    break;
921
                case POWERPC_EXCP_INVAL_LSWX:
922
                    info.si_code = TARGET_ILL_ILLOPN;
bellard's avatar
bellard committed
923
                    break;
924
                case POWERPC_EXCP_INVAL_SPR:
bellard's avatar
bellard committed
925
926
                    info.si_code = TARGET_ILL_PRVREG;
                    break;
927
                case POWERPC_EXCP_INVAL_FP:
bellard's avatar
bellard committed
928
929
930
                    info.si_code = TARGET_ILL_COPROC;
                    break;
                default:
931
932
                    EXCP_DUMP(env, "Unknown invalid operation (%02x)\n",
                              env->error_code & 0xF);
bellard's avatar
bellard committed
933
934
935
936
                    info.si_code = TARGET_ILL_ILLADR;
                    break;
                }
                break;
937
938
            case POWERPC_EXCP_PRIV:
                EXCP_DUMP(env, "Privilege violation\n");
bellard's avatar
bellard committed
939
940
941
                info.si_signo = TARGET_SIGILL;
                info.si_errno = 0;
                switch (env->error_code & 0xF) {
942
                case POWERPC_EXCP_PRIV_OPC:
bellard's avatar
bellard committed
943
944
                    info.si_code = TARGET_ILL_PRVOPC;
                    break;
945
                case POWERPC_EXCP_PRIV_REG:
bellard's avatar
bellard committed
946
                    info.si_code = TARGET_ILL_PRVREG;
947
                    break;
bellard's avatar
bellard committed
948
                default:
949
950
                    EXCP_DUMP(env, "Unknown privilege violation (%02x)\n",
                              env->error_code & 0xF);
bellard's avatar
bellard committed
951
952
953
954
                    info.si_code = TARGET_ILL_PRVOPC;
                    break;
                }
                break;
955
956
957
            case POWERPC_EXCP_TRAP:
                cpu_abort(env, "Tried to call a TRAP\n");
                break;
bellard's avatar
bellard committed
958
959
            default:
                /* Should not happen ! */
960
961
962
                cpu_abort(env, "Unknown program exception (%02x)\n",
                          env->error_code);
                break;
bellard's avatar
bellard committed
963
964
            }
            info._sifields._sigfault._addr = env->nip - 4;
965
966
            queue_signal(info.si_signo, &info);
            break;
967
968
        case POWERPC_EXCP_FPU:      /* Floating-point unavailable exception  */
            EXCP_DUMP(env, "No floating point allowed\n");
bellard's avatar
bellard committed
969
            info.si_signo = TARGET_SIGILL;
970
            info.si_errno = 0;
bellard's avatar
bellard committed
971
972
            info.si_code = TARGET_ILL_COPROC;
            info._sifields._sigfault._addr = env->nip - 4;
973
974
            queue_signal(info.si_signo, &info);
            break;
975
976
977
        case POWERPC_EXCP_SYSCALL:  /* System call exception                 */
            cpu_abort(env, "Syscall exception while in user mode. "
                      "Aborting\n");
bellard's avatar
bellard committed
978
            break;
979
980
981
982
983
984
985
        case POWERPC_EXCP_APU:      /* Auxiliary processor unavailable       */
            EXCP_DUMP(env, "No APU instruction allowed\n");
            info.si_signo = TARGET_SIGILL;
            info.si_errno = 0;
            info.si_code = TARGET_ILL_COPROC;
            info._sifields._sigfault._addr = env->nip - 4;
            queue_signal(info.si_signo, &info);
bellard's avatar
bellard committed
986
            break;
987
988
989
        case POWERPC_EXCP_DECR:     /* Decrementer exception                 */
            cpu_abort(env, "Decrementer interrupt while in user mode. "
                      "Aborting\n");
bellard's avatar
bellard committed
990
            break;
991
992
993
994
995
996
997
998
999
1000
        case POWERPC_EXCP_FIT:      /* Fixed-interval timer interrupt        */
            cpu_abort(env, "Fix interval timer interrupt while in user mode. "
                      "Aborting\n");
            break;
        case POWERPC_EXCP_WDT:      /* Watchdog timer interrupt              */
            cpu_abort(env, "Watchdog timer interrupt while in user mode. "
                      "Aborting\n");
            break;
        case POWERPC_EXCP_DTLB:     /* Data TLB error                        */
            cpu_abort(env, "Data TLB exception while in user mode. "
For faster browsing, not all history is shown. View entire blame