Statistics
| Branch: | Revision:

root / linux-user / main.c @ 46027c07

History | View | Annotate | Download (73.7 kB)

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

    
27
#include "qemu.h"
28

    
29
#define DEBUG_LOGFILE "/tmp/qemu.log"
30

    
31
#ifdef __APPLE__
32
#include <crt_externs.h>
33
# define environ  (*_NSGetEnviron())
34
#endif
35

    
36
static const char *interp_prefix = CONFIG_QEMU_PREFIX;
37
const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
38

    
39
#if defined(__i386__) && !defined(CONFIG_STATIC)
40
/* Force usage of an ELF interpreter even if it is an ELF shared
41
   object ! */
42
const char interp[] __attribute__((section(".interp"))) = "/lib/ld-linux.so.2";
43
#endif
44

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

    
64
/* XXX: on x86 MAP_GROWSDOWN only works if ESP <= address + 32, so
65
   we allocate a bigger stack. Need a better solution, for example
66
   by remapping the process stack directly at the right place */
67
unsigned long x86_stack_size = 512 * 1024;
68

    
69
void gemu_log(const char *fmt, ...)
70
{
71
    va_list ap;
72

    
73
    va_start(ap, fmt);
74
    vfprintf(stderr, fmt, ap);
75
    va_end(ap);
76
}
77

    
78
void cpu_outb(CPUState *env, int addr, int val)
79
{
80
    fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
81
}
82

    
83
void cpu_outw(CPUState *env, int addr, int val)
84
{
85
    fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
86
}
87

    
88
void cpu_outl(CPUState *env, int addr, int val)
89
{
90
    fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
91
}
92

    
93
int cpu_inb(CPUState *env, int addr)
94
{
95
    fprintf(stderr, "inb: port=0x%04x\n", addr);
96
    return 0;
97
}
98

    
99
int cpu_inw(CPUState *env, int addr)
100
{
101
    fprintf(stderr, "inw: port=0x%04x\n", addr);
102
    return 0;
103
}
104

    
105
int cpu_inl(CPUState *env, int addr)
106
{
107
    fprintf(stderr, "inl: port=0x%04x\n", addr);
108
    return 0;
109
}
110

    
111
int cpu_get_pic_interrupt(CPUState *env)
112
{
113
    return -1;
114
}
115

    
116
/* timers for rdtsc */
117

    
118
#if 0
119

120
static uint64_t emu_time;
121

122
int64_t cpu_get_real_ticks(void)
123
{
124
    return emu_time++;
125
}
126

127
#endif
128

    
129
#ifdef TARGET_I386
130
/***********************************************************/
131
/* CPUX86 core interface */
132

    
133
void cpu_smm_update(CPUState *env)
134
{
135
}
136

    
137
uint64_t cpu_get_tsc(CPUX86State *env)
138
{
139
    return cpu_get_real_ticks();
140
}
141

    
142
static void write_dt(void *ptr, unsigned long addr, unsigned long limit,
143
                     int flags)
144
{
145
    unsigned int e1, e2;
146
    uint32_t *p;
147
    e1 = (addr << 16) | (limit & 0xffff);
148
    e2 = ((addr >> 16) & 0xff) | (addr & 0xff000000) | (limit & 0x000f0000);
149
    e2 |= flags;
150
    p = ptr;
151
    p[0] = tswapl(e1);
152
    p[1] = tswapl(e2);
153
}
154

    
155
static void set_gate(void *ptr, unsigned int type, unsigned int dpl,
156
                     unsigned long addr, unsigned int sel)
157
{
158
    unsigned int e1, e2;
159
    uint32_t *p;
160
    e1 = (addr & 0xffff) | (sel << 16);
161
    e2 = (addr & 0xffff0000) | 0x8000 | (dpl << 13) | (type << 8);
162
    p = ptr;
163
    p[0] = tswapl(e1);
164
    p[1] = tswapl(e2);
165
}
166

    
167
uint64_t gdt_table[6];
168
uint64_t idt_table[256];
169

    
170
/* only dpl matters as we do only user space emulation */
171
static void set_idt(int n, unsigned int dpl)
172
{
173
    set_gate(idt_table + n, 0, dpl, 0, 0);
174
}
175

    
176
void cpu_loop(CPUX86State *env)
177
{
178
    int trapnr;
179
    abi_ulong pc;
180
    target_siginfo_t info;
181

    
182
    for(;;) {
183
        trapnr = cpu_x86_exec(env);
184
        switch(trapnr) {
185
        case 0x80:
186
            /* linux syscall */
187
            env->regs[R_EAX] = do_syscall(env,
188
                                          env->regs[R_EAX],
189
                                          env->regs[R_EBX],
190
                                          env->regs[R_ECX],
191
                                          env->regs[R_EDX],
192
                                          env->regs[R_ESI],
193
                                          env->regs[R_EDI],
194
                                          env->regs[R_EBP]);
195
            break;
196
        case EXCP0B_NOSEG:
197
        case EXCP0C_STACK:
198
            info.si_signo = SIGBUS;
199
            info.si_errno = 0;
200
            info.si_code = TARGET_SI_KERNEL;
201
            info._sifields._sigfault._addr = 0;
202
            queue_signal(info.si_signo, &info);
203
            break;
204
        case EXCP0D_GPF:
205
#ifndef TARGET_X86_64
206
            if (env->eflags & VM_MASK) {
207
                handle_vm86_fault(env);
208
            } else
209
#endif
210
            {
211
                info.si_signo = SIGSEGV;
212
                info.si_errno = 0;
213
                info.si_code = TARGET_SI_KERNEL;
214
                info._sifields._sigfault._addr = 0;
215
                queue_signal(info.si_signo, &info);
216
            }
217
            break;
218
        case EXCP0E_PAGE:
219
            info.si_signo = SIGSEGV;
220
            info.si_errno = 0;
221
            if (!(env->error_code & 1))
222
                info.si_code = TARGET_SEGV_MAPERR;
223
            else
224
                info.si_code = TARGET_SEGV_ACCERR;
225
            info._sifields._sigfault._addr = env->cr[2];
226
            queue_signal(info.si_signo, &info);
227
            break;
228
        case EXCP00_DIVZ:
229
#ifndef TARGET_X86_64
230
            if (env->eflags & VM_MASK) {
231
                handle_vm86_trap(env, trapnr);
232
            } else
233
#endif
234
            {
235
                /* division by zero */
236
                info.si_signo = SIGFPE;
237
                info.si_errno = 0;
238
                info.si_code = TARGET_FPE_INTDIV;
239
                info._sifields._sigfault._addr = env->eip;
240
                queue_signal(info.si_signo, &info);
241
            }
242
            break;
243
        case EXCP01_SSTP:
244
        case EXCP03_INT3:
245
#ifndef TARGET_X86_64
246
            if (env->eflags & VM_MASK) {
247
                handle_vm86_trap(env, trapnr);
248
            } else
249
#endif
250
            {
251
                info.si_signo = SIGTRAP;
252
                info.si_errno = 0;
253
                if (trapnr == EXCP01_SSTP) {
254
                    info.si_code = TARGET_TRAP_BRKPT;
255
                    info._sifields._sigfault._addr = env->eip;
256
                } else {
257
                    info.si_code = TARGET_SI_KERNEL;
258
                    info._sifields._sigfault._addr = 0;
259
                }
260
                queue_signal(info.si_signo, &info);
261
            }
262
            break;
263
        case EXCP04_INTO:
264
        case EXCP05_BOUND:
265
#ifndef TARGET_X86_64
266
            if (env->eflags & VM_MASK) {
267
                handle_vm86_trap(env, trapnr);
268
            } else
269
#endif
270
            {
271
                info.si_signo = SIGSEGV;
272
                info.si_errno = 0;
273
                info.si_code = TARGET_SI_KERNEL;
274
                info._sifields._sigfault._addr = 0;
275
                queue_signal(info.si_signo, &info);
276
            }
277
            break;
278
        case EXCP06_ILLOP:
279
            info.si_signo = SIGILL;
280
            info.si_errno = 0;
281
            info.si_code = TARGET_ILL_ILLOPN;
282
            info._sifields._sigfault._addr = env->eip;
283
            queue_signal(info.si_signo, &info);
284
            break;
285
        case EXCP_INTERRUPT:
286
            /* just indicate that signals should be handled asap */
287
            break;
288
        case EXCP_DEBUG:
289
            {
290
                int sig;
291

    
292
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
293
                if (sig)
294
                  {
295
                    info.si_signo = sig;
296
                    info.si_errno = 0;
297
                    info.si_code = TARGET_TRAP_BRKPT;
298
                    queue_signal(info.si_signo, &info);
299
                  }
300
            }
301
            break;
302
        default:
303
            pc = env->segs[R_CS].base + env->eip;
304
            fprintf(stderr, "qemu: 0x%08lx: unhandled CPU exception 0x%x - aborting\n",
305
                    (long)pc, trapnr);
306
            abort();
307
        }
308
        process_pending_signals(env);
309
    }
310
}
311
#endif
312

    
313
#ifdef TARGET_ARM
314

    
315
/* XXX: find a better solution */
316
extern void tb_invalidate_page_range(abi_ulong start, abi_ulong end);
317

    
318
static void arm_cache_flush(abi_ulong start, abi_ulong last)
319
{
320
    abi_ulong addr, last1;
321

    
322
    if (last < start)
323
        return;
324
    addr = start;
325
    for(;;) {
326
        last1 = ((addr + TARGET_PAGE_SIZE) & TARGET_PAGE_MASK) - 1;
327
        if (last1 > last)
328
            last1 = last;
329
        tb_invalidate_page_range(addr, last1 + 1);
330
        if (last1 == last)
331
            break;
332
        addr = last1 + 1;
333
    }
334
}
335

    
336
void cpu_loop(CPUARMState *env)
337
{
338
    int trapnr;
339
    unsigned int n, insn;
340
    target_siginfo_t info;
341
    uint32_t addr;
342

    
343
    for(;;) {
344
        trapnr = cpu_arm_exec(env);
345
        switch(trapnr) {
346
        case EXCP_UDEF:
347
            {
348
                TaskState *ts = env->opaque;
349
                uint32_t opcode;
350

    
351
                /* we handle the FPU emulation here, as Linux */
352
                /* we get the opcode */
353
                opcode = tget32(env->regs[15]);
354

    
355
                if (EmulateAll(opcode, &ts->fpa, env) == 0) {
356
                    info.si_signo = SIGILL;
357
                    info.si_errno = 0;
358
                    info.si_code = TARGET_ILL_ILLOPN;
359
                    info._sifields._sigfault._addr = env->regs[15];
360
                    queue_signal(info.si_signo, &info);
361
                } else {
362
                    /* increment PC */
363
                    env->regs[15] += 4;
364
                }
365
            }
366
            break;
367
        case EXCP_SWI:
368
        case EXCP_BKPT:
369
            {
370
                env->eabi = 1;
371
                /* system call */
372
                if (trapnr == EXCP_BKPT) {
373
                    if (env->thumb) {
374
                        insn = tget16(env->regs[15]);
375
                        n = insn & 0xff;
376
                        env->regs[15] += 2;
377
                    } else {
378
                        insn = tget32(env->regs[15]);
379
                        n = (insn & 0xf) | ((insn >> 4) & 0xff0);
380
                        env->regs[15] += 4;
381
                    }
382
                } else {
383
                    if (env->thumb) {
384
                        insn = tget16(env->regs[15] - 2);
385
                        n = insn & 0xff;
386
                    } else {
387
                        insn = tget32(env->regs[15] - 4);
388
                        n = insn & 0xffffff;
389
                    }
390
                }
391

    
392
                if (n == ARM_NR_cacheflush) {
393
                    arm_cache_flush(env->regs[0], env->regs[1]);
394
                } else if (n == ARM_NR_semihosting
395
                           || n == ARM_NR_thumb_semihosting) {
396
                    env->regs[0] = do_arm_semihosting (env);
397
                } else if (n == 0 || n >= ARM_SYSCALL_BASE
398
                           || (env->thumb && n == ARM_THUMB_SYSCALL)) {
399
                    /* linux syscall */
400
                    if (env->thumb || n == 0) {
401
                        n = env->regs[7];
402
                    } else {
403
                        n -= ARM_SYSCALL_BASE;
404
                        env->eabi = 0;
405
                    }
406
                    env->regs[0] = do_syscall(env,
407
                                              n,
408
                                              env->regs[0],
409
                                              env->regs[1],
410
                                              env->regs[2],
411
                                              env->regs[3],
412
                                              env->regs[4],
413
                                              env->regs[5]);
414
                } else {
415
                    goto error;
416
                }
417
            }
418
            break;
419
        case EXCP_INTERRUPT:
420
            /* just indicate that signals should be handled asap */
421
            break;
422
        case EXCP_PREFETCH_ABORT:
423
            addr = env->cp15.c6_data;
424
            goto do_segv;
425
        case EXCP_DATA_ABORT:
426
            addr = env->cp15.c6_insn;
427
            goto do_segv;
428
        do_segv:
429
            {
430
                info.si_signo = SIGSEGV;
431
                info.si_errno = 0;
432
                /* XXX: check env->error_code */
433
                info.si_code = TARGET_SEGV_MAPERR;
434
                info._sifields._sigfault._addr = addr;
435
                queue_signal(info.si_signo, &info);
436
            }
437
            break;
438
        case EXCP_DEBUG:
439
            {
440
                int sig;
441

    
442
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
443
                if (sig)
444
                  {
445
                    info.si_signo = sig;
446
                    info.si_errno = 0;
447
                    info.si_code = TARGET_TRAP_BRKPT;
448
                    queue_signal(info.si_signo, &info);
449
                  }
450
            }
451
            break;
452
        default:
453
        error:
454
            fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
455
                    trapnr);
456
            cpu_dump_state(env, stderr, fprintf, 0);
457
            abort();
458
        }
459
        process_pending_signals(env);
460
    }
461
}
462

    
463
#endif
464

    
465
#ifdef TARGET_SPARC
466

    
467
//#define DEBUG_WIN
468

    
469
/* WARNING: dealing with register windows _is_ complicated. More info
470
   can be found at http://www.sics.se/~psm/sparcstack.html */
471
static inline int get_reg_index(CPUSPARCState *env, int cwp, int index)
472
{
473
    index = (index + cwp * 16) & (16 * NWINDOWS - 1);
474
    /* wrap handling : if cwp is on the last window, then we use the
475
       registers 'after' the end */
476
    if (index < 8 && env->cwp == (NWINDOWS - 1))
477
        index += (16 * NWINDOWS);
478
    return index;
479
}
480

    
481
/* save the register window 'cwp1' */
482
static inline void save_window_offset(CPUSPARCState *env, int cwp1)
483
{
484
    unsigned int i;
485
    abi_ulong sp_ptr;
486

    
487
    sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
488
#if defined(DEBUG_WIN)
489
    printf("win_overflow: sp_ptr=0x%x save_cwp=%d\n",
490
           (int)sp_ptr, cwp1);
491
#endif
492
    for(i = 0; i < 16; i++) {
493
        tputl(sp_ptr, env->regbase[get_reg_index(env, cwp1, 8 + i)]);
494
        sp_ptr += sizeof(abi_ulong);
495
    }
496
}
497

    
498
static void save_window(CPUSPARCState *env)
499
{
500
#ifndef TARGET_SPARC64
501
    unsigned int new_wim;
502
    new_wim = ((env->wim >> 1) | (env->wim << (NWINDOWS - 1))) &
503
        ((1LL << NWINDOWS) - 1);
504
    save_window_offset(env, (env->cwp - 2) & (NWINDOWS - 1));
505
    env->wim = new_wim;
506
#else
507
    save_window_offset(env, (env->cwp - 2) & (NWINDOWS - 1));
508
    env->cansave++;
509
    env->canrestore--;
510
#endif
511
}
512

    
513
static void restore_window(CPUSPARCState *env)
514
{
515
    unsigned int new_wim, i, cwp1;
516
    abi_ulong sp_ptr;
517

    
518
    new_wim = ((env->wim << 1) | (env->wim >> (NWINDOWS - 1))) &
519
        ((1LL << NWINDOWS) - 1);
520

    
521
    /* restore the invalid window */
522
    cwp1 = (env->cwp + 1) & (NWINDOWS - 1);
523
    sp_ptr = env->regbase[get_reg_index(env, cwp1, 6)];
524
#if defined(DEBUG_WIN)
525
    printf("win_underflow: sp_ptr=0x%x load_cwp=%d\n",
526
           (int)sp_ptr, cwp1);
527
#endif
528
    for(i = 0; i < 16; i++) {
529
        env->regbase[get_reg_index(env, cwp1, 8 + i)] = tgetl(sp_ptr);
530
        sp_ptr += sizeof(abi_ulong);
531
    }
532
    env->wim = new_wim;
533
#ifdef TARGET_SPARC64
534
    env->canrestore++;
535
    if (env->cleanwin < NWINDOWS - 1)
536
        env->cleanwin++;
537
    env->cansave--;
538
#endif
539
}
540

    
541
static void flush_windows(CPUSPARCState *env)
542
{
543
    int offset, cwp1;
544

    
545
    offset = 1;
546
    for(;;) {
547
        /* if restore would invoke restore_window(), then we can stop */
548
        cwp1 = (env->cwp + offset) & (NWINDOWS - 1);
549
        if (env->wim & (1 << cwp1))
550
            break;
551
        save_window_offset(env, cwp1);
552
        offset++;
553
    }
554
    /* set wim so that restore will reload the registers */
555
    cwp1 = (env->cwp + 1) & (NWINDOWS - 1);
556
    env->wim = 1 << cwp1;
557
#if defined(DEBUG_WIN)
558
    printf("flush_windows: nb=%d\n", offset - 1);
559
#endif
560
}
561

    
562
void cpu_loop (CPUSPARCState *env)
563
{
564
    int trapnr, ret;
565
    target_siginfo_t info;
566

    
567
    while (1) {
568
        trapnr = cpu_sparc_exec (env);
569

    
570
        switch (trapnr) {
571
#ifndef TARGET_SPARC64
572
        case 0x88:
573
        case 0x90:
574
#else
575
        case 0x110:
576
        case 0x16d:
577
#endif
578
            ret = do_syscall (env, env->gregs[1],
579
                              env->regwptr[0], env->regwptr[1],
580
                              env->regwptr[2], env->regwptr[3],
581
                              env->regwptr[4], env->regwptr[5]);
582
            if ((unsigned int)ret >= (unsigned int)(-515)) {
583
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
584
                env->xcc |= PSR_CARRY;
585
#else
586
                env->psr |= PSR_CARRY;
587
#endif
588
                ret = -ret;
589
            } else {
590
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
591
                env->xcc &= ~PSR_CARRY;
592
#else
593
                env->psr &= ~PSR_CARRY;
594
#endif
595
            }
596
            env->regwptr[0] = ret;
597
            /* next instruction */
598
            env->pc = env->npc;
599
            env->npc = env->npc + 4;
600
            break;
601
        case 0x83: /* flush windows */
602
#ifdef TARGET_ABI32
603
        case 0x103:
604
#endif
605
            flush_windows(env);
606
            /* next instruction */
607
            env->pc = env->npc;
608
            env->npc = env->npc + 4;
609
            break;
610
#ifndef TARGET_SPARC64
611
        case TT_WIN_OVF: /* window overflow */
612
            save_window(env);
613
            break;
614
        case TT_WIN_UNF: /* window underflow */
615
            restore_window(env);
616
            break;
617
        case TT_TFAULT:
618
        case TT_DFAULT:
619
            {
620
                info.si_signo = SIGSEGV;
621
                info.si_errno = 0;
622
                /* XXX: check env->error_code */
623
                info.si_code = TARGET_SEGV_MAPERR;
624
                info._sifields._sigfault._addr = env->mmuregs[4];
625
                queue_signal(info.si_signo, &info);
626
            }
627
            break;
628
#else
629
        case TT_SPILL: /* window overflow */
630
            save_window(env);
631
            break;
632
        case TT_FILL: /* window underflow */
633
            restore_window(env);
634
            break;
635
        case TT_TFAULT:
636
        case TT_DFAULT:
637
            {
638
                info.si_signo = SIGSEGV;
639
                info.si_errno = 0;
640
                /* XXX: check env->error_code */
641
                info.si_code = TARGET_SEGV_MAPERR;
642
                if (trapnr == TT_DFAULT)
643
                    info._sifields._sigfault._addr = env->dmmuregs[4];
644
                else
645
                    info._sifields._sigfault._addr = env->tpc[env->tl];
646
                queue_signal(info.si_signo, &info);
647
            }
648
            break;
649
        case 0x16e:
650
            flush_windows(env);
651
            sparc64_get_context(env);
652
            break;
653
        case 0x16f:
654
            flush_windows(env);
655
            sparc64_set_context(env);
656
            break;
657
#endif
658
        case EXCP_INTERRUPT:
659
            /* just indicate that signals should be handled asap */
660
            break;
661
        case EXCP_DEBUG:
662
            {
663
                int sig;
664

    
665
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
666
                if (sig)
667
                  {
668
                    info.si_signo = sig;
669
                    info.si_errno = 0;
670
                    info.si_code = TARGET_TRAP_BRKPT;
671
                    queue_signal(info.si_signo, &info);
672
                  }
673
            }
674
            break;
675
        default:
676
            printf ("Unhandled trap: 0x%x\n", trapnr);
677
            cpu_dump_state(env, stderr, fprintf, 0);
678
            exit (1);
679
        }
680
        process_pending_signals (env);
681
    }
682
}
683

    
684
#endif
685

    
686
#ifdef TARGET_PPC
687
static inline uint64_t cpu_ppc_get_tb (CPUState *env)
688
{
689
    /* TO FIX */
690
    return 0;
691
}
692

    
693
uint32_t cpu_ppc_load_tbl (CPUState *env)
694
{
695
    return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
696
}
697

    
698
uint32_t cpu_ppc_load_tbu (CPUState *env)
699
{
700
    return cpu_ppc_get_tb(env) >> 32;
701
}
702

    
703
uint32_t cpu_ppc_load_atbl (CPUState *env)
704
{
705
    return cpu_ppc_get_tb(env) & 0xFFFFFFFF;
706
}
707

    
708
uint32_t cpu_ppc_load_atbu (CPUState *env)
709
{
710
    return cpu_ppc_get_tb(env) >> 32;
711
}
712

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

    
716
uint32_t cpu_ppc601_load_rtcl (CPUState *env)
717
{
718
    return cpu_ppc_load_tbl(env) & 0x3FFFFF80;
719
}
720

    
721
/* XXX: to be fixed */
722
int ppc_dcr_read (ppc_dcr_t *dcr_env, int dcrn, target_ulong *valp)
723
{
724
    return -1;
725
}
726

    
727
int ppc_dcr_write (ppc_dcr_t *dcr_env, int dcrn, target_ulong val)
728
{
729
    return -1;
730
}
731

    
732
#define EXCP_DUMP(env, fmt, args...)                                         \
733
do {                                                                          \
734
    fprintf(stderr, fmt , ##args);                                            \
735
    cpu_dump_state(env, stderr, fprintf, 0);                                  \
736
    if (loglevel != 0) {                                                      \
737
        fprintf(logfile, fmt , ##args);                                       \
738
        cpu_dump_state(env, logfile, fprintf, 0);                             \
739
    }                                                                         \
740
} while (0)
741

    
742
void cpu_loop(CPUPPCState *env)
743
{
744
    target_siginfo_t info;
745
    int trapnr;
746
    uint32_t ret;
747

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

    
973
                sig = gdb_handlesig(env, TARGET_SIGTRAP);
974
                if (sig) {
975
                    info.si_signo = sig;
976
                    info.si_errno = 0;
977
                    info.si_code = TARGET_TRAP_BRKPT;
978
                    queue_signal(info.si_signo, &info);
979
                  }
980
            }
981
            break;
982
#if defined(TARGET_PPCEMB)
983
        case POWERPC_EXCP_SPEU:     /* SPE/embedded floating-point unavail.  */
984
            EXCP_DUMP(env, "No SPE/floating-point instruction allowed\n");
985
            info.si_signo = TARGET_SIGILL;
986
            info.si_errno = 0;
987
            info.si_code = TARGET_ILL_COPROC;
988
            info._sifields._sigfault._addr = env->nip - 4;
989
            queue_signal(info.si_signo, &info);
990
            break;
991
        case POWERPC_EXCP_EFPDI:    /* Embedded floating-point data IRQ      */
992
            cpu_abort(env, "Embedded floating-point data IRQ not handled\n");
993
            break;
994
        case POWERPC_EXCP_EFPRI:    /* Embedded floating-point round IRQ     */
995
            cpu_abort(env, "Embedded floating-point round IRQ not handled\n");
996
            break;
997
        case POWERPC_EXCP_EPERFM:   /* Embedded performance monitor IRQ      */
998
            cpu_abort(env, "Performance monitor exception not handled\n");
999
            break;
1000
        case POWERPC_EXCP_DOORI:    /* Embedded doorbell interrupt           */
1001
            cpu_abort(env, "Doorbell interrupt while in user mode. "
1002
                       "Aborting\n");
1003
            break;
1004
        case POWERPC_EXCP_DOORCI:   /* Embedded doorbell critical interrupt  */
1005
            cpu_abort(env, "Doorbell critical interrupt while in user mode. "
1006
                      "Aborting\n");
1007
            break;
1008
        case POWERPC_EXCP_RESET:    /* System reset exception                */
1009
            cpu_abort(env, "Reset interrupt while in user mode. "
1010
                      "Aborting\n");
1011
            break;
1012
#endif /* defined(TARGET_PPCEMB) */
1013
#if defined(TARGET_PPC64) && !defined(TARGET_ABI32) /* PowerPC 64 */
1014
        case POWERPC_EXCP_DSEG:     /* Data segment exception                */
1015
            cpu_abort(env, "Data segment exception while in user mode. "
1016
                      "Aborting\n");
1017
            break;
1018
        case POWERPC_EXCP_ISEG:     /* Instruction segment exception         */
1019
            cpu_abort(env, "Instruction segment exception "
1020
                      "while in user mode. Aborting\n");
1021
            break;
1022
#endif /* defined(TARGET_PPC64) && !defined(TARGET_ABI32) */
1023
#if defined(TARGET_PPC64H) && !defined(TARGET_ABI32)
1024
        /* PowerPC 64 with hypervisor mode support */
1025
        case POWERPC_EXCP_HDECR:    /* Hypervisor decrementer exception      */
1026
            cpu_abort(env, "Hypervisor decrementer interrupt "
1027
                      "while in user mode. Aborting\n");
1028
            break;
1029
#endif /* defined(TARGET_PPC64H) && !defined(TARGET_ABI32) */
1030
        case POWERPC_EXCP_TRACE:    /* Trace exception                       */
1031
            /* Nothing to do:
1032
             * we use this exception to emulate step-by-step execution mode.
1033
             */
1034
            break;
1035
#if defined(TARGET_PPC64H) && !defined(TARGET_ABI32)
1036
        /* PowerPC 64 with hypervisor mode support */
1037
        case POWERPC_EXCP_HDSI:     /* Hypervisor data storage exception     */
1038
            cpu_abort(env, "Hypervisor data storage exception "
1039
                      "while in user mode. Aborting\n");
1040
            break;
1041
        case POWERPC_EXCP_HISI:     /* Hypervisor instruction storage excp   */
1042
            cpu_abort(env, "Hypervisor instruction storage exception "
1043
                      "while in user mode. Aborting\n");
1044
            break;
1045
        case POWERPC_EXCP_HDSEG:    /* Hypervisor data segment exception     */
1046
            cpu_abort(env, "Hypervisor data segment exception "
1047
                      "while in user mode. Aborting\n");
1048
            break;
1049
        case POWERPC_EXCP_HISEG:    /* Hypervisor instruction segment excp   */
1050
            cpu_abort(env, "Hypervisor instruction segment exception "
1051
                      "while in user mode. Aborting\n");
1052
            break;
1053
#endif /* defined(TARGET_PPC64H) && !defined(TARGET_ABI32) */
1054
        case POWERPC_EXCP_VPU:      /* Vector unavailable exception          */
1055
            EXCP_DUMP(env, "No Altivec instructions allowed\n");
1056
            info.si_signo = TARGET_SIGILL;
1057
            info.si_errno = 0;
1058
            info.si_code = TARGET_ILL_COPROC;
1059
            info._sifields._sigfault._addr = env->nip - 4;
1060
            queue_signal(info.si_signo, &info);
1061
            break;
1062
        case POWERPC_EXCP_PIT:      /* Programmable interval timer IRQ       */
1063
            cpu_abort(env, "Programable interval timer interrupt "
1064
                      "while in user mode. Aborting\n");
1065
            break;
1066
        case POWERPC_EXCP_IO:       /* IO error exception                    */
1067
            cpu_abort(env, "IO error exception while in user mode. "
1068
                      "Aborting\n");
1069
            break;
1070
        case POWERPC_EXCP_RUNM:     /* Run mode exception                    */
1071
            cpu_abort(env, "Run mode exception while in user mode. "
1072
                      "Aborting\n");
1073
            break;
1074
        case POWERPC_EXCP_EMUL:     /* Emulation trap exception              */
1075
            cpu_abort(env, "Emulation trap exception not handled\n");
1076
            break;
1077
        case POWERPC_EXCP_IFTLB:    /* Instruction fetch TLB error           */
1078
            cpu_abort(env, "Instruction fetch TLB exception "
1079
                      "while in user-mode. Aborting");
1080
            break;
1081
        case POWERPC_EXCP_DLTLB:    /* Data load TLB miss                    */
1082
            cpu_abort(env, "Data load TLB exception while in user-mode. "
1083
                      "Aborting");
1084
            break;
1085
        case POWERPC_EXCP_DSTLB:    /* Data store TLB miss                   */
1086
            cpu_abort(env, "Data store TLB exception while in user-mode. "
1087
                      "Aborting");
1088
            break;
1089
        case POWERPC_EXCP_FPA:      /* Floating-point assist exception       */
1090
            cpu_abort(env, "Floating-point assist exception not handled\n");
1091
            break;
1092
        case POWERPC_EXCP_IABR:     /* Instruction address breakpoint        */
1093
            cpu_abort(env, "Instruction address breakpoint exception "
1094
                      "not handled\n");
1095
            break;
1096
        case POWERPC_EXCP_SMI:      /* System management interrupt           */
1097
            cpu_abort(env, "System management interrupt while in user mode. "
1098
                      "Aborting\n");
1099
            break;
1100
        case POWERPC_EXCP_THERM:    /* Thermal interrupt                     */
1101
            cpu_abort(env, "Thermal interrupt interrupt while in user mode. "
1102
                      "Aborting\n");
1103
            break;
1104
        case POWERPC_EXCP_PERFM:   /* Embedded performance monitor IRQ      */
1105
            cpu_abort(env, "Performance monitor exception not handled\n");
1106
            break;
1107
        case POWERPC_EXCP_VPUA:     /* Vector assist exception               */
1108
            cpu_abort(env, "Vector assist exception not handled\n");
1109
            break;
1110
        case POWERPC_EXCP_SOFTP:    /* Soft patch exception                  */
1111
            cpu_abort(env, "Soft patch exception not handled\n");
1112
            break;
1113
        case POWERPC_EXCP_MAINT:    /* Maintenance exception                 */
1114
            cpu_abort(env, "Maintenance exception while in user mode. "
1115
                      "Aborting\n");
1116
            break;
1117
        case POWERPC_EXCP_STOP:     /* stop translation                      */
1118
            /* We did invalidate the instruction cache. Go on */
1119
            break;
1120
        case POWERPC_EXCP_BRANCH:   /* branch instruction:                   */
1121
            /* We just stopped because of a branch. Go on */
1122
            break;
1123
        case POWERPC_EXCP_SYSCALL_USER:
1124
            /* system call in user-mode emulation */
1125
            /* WARNING:
1126
             * PPC ABI uses overflow flag in cr0 to signal an error
1127
             * in syscalls.
1128
             */
1129
#if 0
1130
            printf("syscall %d 0x%08x 0x%08x 0x%08x 0x%08x\n", env->gpr[0],
1131
                   env->gpr[3], env->gpr[4], env->gpr[5], env->gpr[6]);
1132
#endif
1133
            env->crf[0] &= ~0x1;
1134
            ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
1135
                             env->gpr[5], env->gpr[6], env->gpr[7],
1136
                             env->gpr[8]);
1137
            if (ret > (uint32_t)(-515)) {
1138
                env->crf[0] |= 0x1;
1139
                ret = -ret;
1140
            }
1141
            env->gpr[3] = ret;
1142
#if 0
1143
            printf("syscall returned 0x%08x (%d)\n", ret, ret);
1144
#endif
1145
            break;
1146
        case EXCP_INTERRUPT:
1147
            /* just indicate that signals should be handled asap */
1148
            break;
1149
        default:
1150
            cpu_abort(env, "Unknown exception 0x%d. Aborting\n", trapnr);
1151
            break;
1152
        }
1153
        process_pending_signals(env);
1154
    }
1155
}
1156
#endif
1157

    
1158
#ifdef TARGET_MIPS
1159

    
1160
#define MIPS_SYS(name, args) args,
1161

    
1162
static const uint8_t mips_syscall_args[] = {
1163
        MIPS_SYS(sys_syscall        , 0)        /* 4000 */
1164
        MIPS_SYS(sys_exit        , 1)
1165
        MIPS_SYS(sys_fork        , 0)
1166
        MIPS_SYS(sys_read        , 3)
1167
        MIPS_SYS(sys_write        , 3)
1168
        MIPS_SYS(sys_open        , 3)        /* 4005 */
1169
        MIPS_SYS(sys_close        , 1)
1170
        MIPS_SYS(sys_waitpid        , 3)
1171
        MIPS_SYS(sys_creat        , 2)
1172
        MIPS_SYS(sys_link        , 2)
1173
        MIPS_SYS(sys_unlink        , 1)        /* 4010 */
1174
        MIPS_SYS(sys_execve        , 0)
1175
        MIPS_SYS(sys_chdir        , 1)
1176
        MIPS_SYS(sys_time        , 1)
1177
        MIPS_SYS(sys_mknod        , 3)
1178
        MIPS_SYS(sys_chmod        , 2)        /* 4015 */
1179
        MIPS_SYS(sys_lchown        , 3)
1180
        MIPS_SYS(sys_ni_syscall        , 0)
1181
        MIPS_SYS(sys_ni_syscall        , 0)        /* was sys_stat */
1182
        MIPS_SYS(sys_lseek        , 3)
1183
        MIPS_SYS(sys_getpid        , 0)        /* 4020 */
1184
        MIPS_SYS(sys_mount        , 5)
1185
        MIPS_SYS(sys_oldumount        , 1)
1186
        MIPS_SYS(sys_setuid        , 1)
1187
        MIPS_SYS(sys_getuid        , 0)
1188
        MIPS_SYS(sys_stime        , 1)        /* 4025 */
1189
        MIPS_SYS(sys_ptrace        , 4)
1190
        MIPS_SYS(sys_alarm        , 1)
1191
        MIPS_SYS(sys_ni_syscall        , 0)        /* was sys_fstat */
1192
        MIPS_SYS(sys_pause        , 0)
1193
        MIPS_SYS(sys_utime        , 2)        /* 4030 */
1194
        MIPS_SYS(sys_ni_syscall        , 0)
1195
        MIPS_SYS(sys_ni_syscall        , 0)
1196
        MIPS_SYS(sys_access        , 2)
1197
        MIPS_SYS(sys_nice        , 1)
1198
        MIPS_SYS(sys_ni_syscall        , 0)        /* 4035 */
1199
        MIPS_SYS(sys_sync        , 0)
1200
        MIPS_SYS(sys_kill        , 2)
1201
        MIPS_SYS(sys_rename        , 2)
1202
        MIPS_SYS(sys_mkdir        , 2)
1203
        MIPS_SYS(sys_rmdir        , 1)        /* 4040 */
1204
        MIPS_SYS(sys_dup                , 1)
1205
        MIPS_SYS(sys_pipe        , 0)
1206
        MIPS_SYS(sys_times        , 1)
1207
        MIPS_SYS(sys_ni_syscall        , 0)
1208
        MIPS_SYS(sys_brk                , 1)        /* 4045 */
1209
        MIPS_SYS(sys_setgid        , 1)
1210
        MIPS_SYS(sys_getgid        , 0)
1211
        MIPS_SYS(sys_ni_syscall        , 0)        /* was signal(2) */
1212
        MIPS_SYS(sys_geteuid        , 0)
1213
        MIPS_SYS(sys_getegid        , 0)        /* 4050 */
1214
        MIPS_SYS(sys_acct        , 0)
1215
        MIPS_SYS(sys_umount        , 2)
1216
        MIPS_SYS(sys_ni_syscall        , 0)
1217
        MIPS_SYS(sys_ioctl        , 3)
1218
        MIPS_SYS(sys_fcntl        , 3)        /* 4055 */
1219
        MIPS_SYS(sys_ni_syscall        , 2)
1220
        MIPS_SYS(sys_setpgid        , 2)
1221
        MIPS_SYS(sys_ni_syscall        , 0)
1222
        MIPS_SYS(sys_olduname        , 1)
1223
        MIPS_SYS(sys_umask        , 1)        /* 4060 */
1224
        MIPS_SYS(sys_chroot        , 1)
1225
        MIPS_SYS(sys_ustat        , 2)
1226
        MIPS_SYS(sys_dup2        , 2)
1227
        MIPS_SYS(sys_getppid        , 0)
1228
        MIPS_SYS(sys_getpgrp        , 0)        /* 4065 */
1229
        MIPS_SYS(sys_setsid        , 0)
1230
        MIPS_SYS(sys_sigaction        , 3)
1231
        MIPS_SYS(sys_sgetmask        , 0)
1232
        MIPS_SYS(sys_ssetmask        , 1)
1233
        MIPS_SYS(sys_setreuid        , 2)        /* 4070 */
1234
        MIPS_SYS(sys_setregid        , 2)
1235
        MIPS_SYS(sys_sigsuspend        , 0)
1236
        MIPS_SYS(sys_sigpending        , 1)
1237
        MIPS_SYS(sys_sethostname        , 2)
1238
        MIPS_SYS(sys_setrlimit        , 2)        /* 4075 */
1239
        MIPS_SYS(sys_getrlimit        , 2)
1240
        MIPS_SYS(sys_getrusage        , 2)
1241
        MIPS_SYS(sys_gettimeofday, 2)
1242
        MIPS_SYS(sys_settimeofday, 2)
1243
        MIPS_SYS(sys_getgroups        , 2)        /* 4080 */
1244
        MIPS_SYS(sys_setgroups        , 2)
1245
        MIPS_SYS(sys_ni_syscall        , 0)        /* old_select */
1246
        MIPS_SYS(sys_symlink        , 2)
1247
        MIPS_SYS(sys_ni_syscall        , 0)        /* was sys_lstat */
1248
        MIPS_SYS(sys_readlink        , 3)        /* 4085 */
1249
        MIPS_SYS(sys_uselib        , 1)
1250
        MIPS_SYS(sys_swapon        , 2)
1251
        MIPS_SYS(sys_reboot        , 3)
1252
        MIPS_SYS(old_readdir        , 3)
1253
        MIPS_SYS(old_mmap        , 6)        /* 4090 */
1254
        MIPS_SYS(sys_munmap        , 2)
1255
        MIPS_SYS(sys_truncate        , 2)
1256
        MIPS_SYS(sys_ftruncate        , 2)
1257
        MIPS_SYS(sys_fchmod        , 2)
1258
        MIPS_SYS(sys_fchown        , 3)        /* 4095 */
1259
        MIPS_SYS(sys_getpriority        , 2)
1260
        MIPS_SYS(sys_setpriority        , 3)
1261
        MIPS_SYS(sys_ni_syscall        , 0)
1262
        MIPS_SYS(sys_statfs        , 2)
1263
        MIPS_SYS(sys_fstatfs        , 2)        /* 4100 */
1264
        MIPS_SYS(sys_ni_syscall        , 0)        /* was ioperm(2) */
1265
        MIPS_SYS(sys_socketcall        , 2)
1266
        MIPS_SYS(sys_syslog        , 3)
1267
        MIPS_SYS(sys_setitimer        , 3)
1268
        MIPS_SYS(sys_getitimer        , 2)        /* 4105 */
1269
        MIPS_SYS(sys_newstat        , 2)
1270
        MIPS_SYS(sys_newlstat        , 2)
1271
        MIPS_SYS(sys_newfstat        , 2)
1272
        MIPS_SYS(sys_uname        , 1)
1273
        MIPS_SYS(sys_ni_syscall        , 0)        /* 4110 was iopl(2) */
1274
        MIPS_SYS(sys_vhangup        , 0)
1275
        MIPS_SYS(sys_ni_syscall        , 0)        /* was sys_idle() */
1276
        MIPS_SYS(sys_ni_syscall        , 0)        /* was sys_vm86 */
1277
        MIPS_SYS(sys_wait4        , 4)
1278
        MIPS_SYS(sys_swapoff        , 1)        /* 4115 */
1279
        MIPS_SYS(sys_sysinfo        , 1)
1280
        MIPS_SYS(sys_ipc                , 6)
1281
        MIPS_SYS(sys_fsync        , 1)
1282
        MIPS_SYS(sys_sigreturn        , 0)
1283
        MIPS_SYS(sys_clone        , 0)        /* 4120 */
1284
        MIPS_SYS(sys_setdomainname, 2)
1285
        MIPS_SYS(sys_newuname        , 1)
1286
        MIPS_SYS(sys_ni_syscall        , 0)        /* sys_modify_ldt */
1287
        MIPS_SYS(sys_adjtimex        , 1)
1288
        MIPS_SYS(sys_mprotect        , 3)        /* 4125 */
1289
        MIPS_SYS(sys_sigprocmask        , 3)
1290
        MIPS_SYS(sys_ni_syscall        , 0)        /* was create_module */
1291
        MIPS_SYS(sys_init_module        , 5)
1292
        MIPS_SYS(sys_delete_module, 1)
1293
        MIPS_SYS(sys_ni_syscall        , 0)        /* 4130        was get_kernel_syms */
1294
        MIPS_SYS(sys_quotactl        , 0)
1295
        MIPS_SYS(sys_getpgid        , 1)
1296
        MIPS_SYS(sys_fchdir        , 1)
1297
        MIPS_SYS(sys_bdflush        , 2)
1298
        MIPS_SYS(sys_sysfs        , 3)        /* 4135 */
1299
        MIPS_SYS(sys_personality        , 1)
1300
        MIPS_SYS(sys_ni_syscall        , 0)        /* for afs_syscall */
1301
        MIPS_SYS(sys_setfsuid        , 1)
1302
        MIPS_SYS(sys_setfsgid        , 1)
1303
        MIPS_SYS(sys_llseek        , 5)        /* 4140 */
1304
        MIPS_SYS(sys_getdents        , 3)
1305
        MIPS_SYS(sys_select        , 5)
1306
        MIPS_SYS(sys_flock        , 2)
1307
        MIPS_SYS(sys_msync        , 3)
1308
        MIPS_SYS(sys_readv        , 3)        /* 4145 */
1309
        MIPS_SYS(sys_writev        , 3)
1310
        MIPS_SYS(sys_cacheflush        , 3)
1311
        MIPS_SYS(sys_cachectl        , 3)
1312
        MIPS_SYS(sys_sysmips        , 4)
1313
        MIPS_SYS(sys_ni_syscall        , 0)        /* 4150 */
1314
        MIPS_SYS(sys_getsid        , 1)
1315
        MIPS_SYS(sys_fdatasync        , 0)
1316
        MIPS_SYS(sys_sysctl        , 1)
1317
        MIPS_SYS(sys_mlock        , 2)
1318
        MIPS_SYS(sys_munlock        , 2)        /* 4155 */
1319
        MIPS_SYS(sys_mlockall        , 1)
1320
        MIPS_SYS(sys_munlockall        , 0)
1321
        MIPS_SYS(sys_sched_setparam, 2)
1322
        MIPS_SYS(sys_sched_getparam, 2)
1323
        MIPS_SYS(sys_sched_setscheduler, 3)        /* 4160 */
1324
        MIPS_SYS(sys_sched_getscheduler, 1)
1325
        MIPS_SYS(sys_sched_yield        , 0)
1326
        MIPS_SYS(sys_sched_get_priority_max, 1)
1327
        MIPS_SYS(sys_sched_get_priority_min, 1)
1328
        MIPS_SYS(sys_sched_rr_get_interval, 2)        /* 4165 */
1329
        MIPS_SYS(sys_nanosleep,        2)
1330
        MIPS_SYS(sys_mremap        , 4)
1331
        MIPS_SYS(sys_accept        , 3)
1332
        MIPS_SYS(sys_bind        , 3)
1333
        MIPS_SYS(sys_connect        , 3)        /* 4170 */
1334
        MIPS_SYS(sys_getpeername        , 3)
1335
        MIPS_SYS(sys_getsockname        , 3)
1336
        MIPS_SYS(sys_getsockopt        , 5)
1337
        MIPS_SYS(sys_listen        , 2)
1338
        MIPS_SYS(sys_recv        , 4)        /* 4175 */
1339
        MIPS_SYS(sys_recvfrom        , 6)
1340
        MIPS_SYS(sys_recvmsg        , 3)
1341
        MIPS_SYS(sys_send        , 4)
1342
        MIPS_SYS(sys_sendmsg        , 3)
1343
        MIPS_SYS(sys_sendto        , 6)        /* 4180 */
1344
        MIPS_SYS(sys_setsockopt        , 5)
1345
        MIPS_SYS(sys_shutdown        , 2)
1346
        MIPS_SYS(sys_socket        , 3)
1347
        MIPS_SYS(sys_socketpair        , 4)
1348
        MIPS_SYS(sys_setresuid        , 3)        /* 4185 */
1349
        MIPS_SYS(sys_getresuid        , 3)
1350
        MIPS_SYS(sys_ni_syscall        , 0)        /* was sys_query_module */
1351
        MIPS_SYS(sys_poll        , 3)
1352
        MIPS_SYS(sys_nfsservctl        , 3)
1353
        MIPS_SYS(sys_setresgid        , 3)        /* 4190 */
1354
        MIPS_SYS(sys_getresgid        , 3)
1355
        MIPS_SYS(sys_prctl        , 5)
1356
        MIPS_SYS(sys_rt_sigreturn, 0)
1357
        MIPS_SYS(sys_rt_sigaction, 4)
1358
        MIPS_SYS(sys_rt_sigprocmask, 4)        /* 4195 */
1359
        MIPS_SYS(sys_rt_sigpending, 2)
1360
        MIPS_SYS(sys_rt_sigtimedwait, 4)
1361
        MIPS_SYS(sys_rt_sigqueueinfo, 3)
1362
        MIPS_SYS(sys_rt_sigsuspend, 0)
1363
        MIPS_SYS(sys_pread64        , 6)        /* 4200 */
1364
        MIPS_SYS(sys_pwrite64        , 6)
1365
        MIPS_SYS(sys_chown        , 3)
1366
        MIPS_SYS(sys_getcwd        , 2)
1367
        MIPS_SYS(sys_capget        , 2)
1368
        MIPS_SYS(sys_capset        , 2)        /* 4205 */
1369
        MIPS_SYS(sys_sigaltstack        , 0)
1370
        MIPS_SYS(sys_sendfile        , 4)
1371
        MIPS_SYS(sys_ni_syscall        , 0)
1372
        MIPS_SYS(sys_ni_syscall        , 0)
1373
        MIPS_SYS(sys_mmap2        , 6)        /* 4210 */
1374
        MIPS_SYS(sys_truncate64        , 4)
1375
        MIPS_SYS(sys_ftruncate64        , 4)
1376
        MIPS_SYS(sys_stat64        , 2)
1377
        MIPS_SYS(sys_lstat64        , 2)
1378
        MIPS_SYS(sys_fstat64        , 2)        /* 4215 */
1379
        MIPS_SYS(sys_pivot_root        , 2)
1380
        MIPS_SYS(sys_mincore        , 3)
1381
        MIPS_SYS(sys_madvise        , 3)
1382
        MIPS_SYS(sys_getdents64        , 3)
1383
        MIPS_SYS(sys_fcntl64        , 3)        /* 4220 */
1384
        MIPS_SYS(sys_ni_syscall        , 0)
1385
        MIPS_SYS(sys_gettid        , 0)
1386
        MIPS_SYS(sys_readahead        , 5)
1387
        MIPS_SYS(sys_setxattr        , 5)
1388
        MIPS_SYS(sys_lsetxattr        , 5)        /* 4225 */
1389
        MIPS_SYS(sys_fsetxattr        , 5)
1390
        MIPS_SYS(sys_getxattr        , 4)
1391
        MIPS_SYS(sys_lgetxattr        , 4)
1392
        MIPS_SYS(sys_fgetxattr        , 4)
1393
        MIPS_SYS(sys_listxattr        , 3)        /* 4230 */
1394
        MIPS_SYS(sys_llistxattr        , 3)
1395
        MIPS_SYS(sys_flistxattr        , 3)
1396
        MIPS_SYS(sys_removexattr        , 2)
1397
        MIPS_SYS(sys_lremovexattr, 2)
1398
        MIPS_SYS(sys_fremovexattr, 2)        /* 4235 */
1399
        MIPS_SYS(sys_tkill        , 2)
1400
        MIPS_SYS(sys_sendfile64        , 5)
1401
        MIPS_SYS(sys_futex        , 2)
1402
        MIPS_SYS(sys_sched_setaffinity, 3)
1403
        MIPS_SYS(sys_sched_getaffinity, 3)        /* 4240 */
1404
        MIPS_SYS(sys_io_setup        , 2)
1405
        MIPS_SYS(sys_io_destroy        , 1)
1406
        MIPS_SYS(sys_io_getevents, 5)
1407
        MIPS_SYS(sys_io_submit        , 3)
1408
        MIPS_SYS(sys_io_cancel        , 3)        /* 4245 */
1409
        MIPS_SYS(sys_exit_group        , 1)
1410
        MIPS_SYS(sys_lookup_dcookie, 3)
1411
        MIPS_SYS(sys_epoll_create, 1)
1412
        MIPS_SYS(sys_epoll_ctl        , 4)
1413
        MIPS_SYS(sys_epoll_wait        , 3)        /* 4250 */
1414
        MIPS_SYS(sys_remap_file_pages, 5)
1415
        MIPS_SYS(sys_set_tid_address, 1)
1416
        MIPS_SYS(sys_restart_syscall, 0)
1417
        MIPS_SYS(sys_fadvise64_64, 7)
1418
        MIPS_SYS(sys_statfs64        , 3)        /* 4255 */
1419
        MIPS_SYS(sys_fstatfs64        , 2)
1420
        MIPS_SYS(sys_timer_create, 3)
1421
        MIPS_SYS(sys_timer_settime, 4)
1422
        MIPS_SYS(sys_timer_gettime, 2)
1423
        MIPS_SYS(sys_timer_getoverrun, 1)        /* 4260 */
1424
        MIPS_SYS(sys_timer_delete, 1)
1425
        MIPS_SYS(sys_clock_settime, 2)
1426
        MIPS_SYS(sys_clock_gettime, 2)
1427
        MIPS_SYS(sys_clock_getres, 2)
1428
        MIPS_SYS(sys_clock_nanosleep, 4)        /* 4265 */
1429
        MIPS_SYS(sys_tgkill        , 3)
1430
        MIPS_SYS(sys_utimes        , 2)
1431
        MIPS_SYS(sys_mbind        , 4)
1432
        MIPS_SYS(sys_ni_syscall        , 0)        /* sys_get_mempolicy */
1433
        MIPS_SYS(sys_ni_syscall        , 0)        /* 4270 sys_set_mempolicy */
1434
        MIPS_SYS(sys_mq_open        , 4)
1435
        MIPS_SYS(sys_mq_unlink        , 1)
1436
        MIPS_SYS(sys_mq_timedsend, 5)
1437
        MIPS_SYS(sys_mq_timedreceive, 5)
1438
        MIPS_SYS(sys_mq_notify        , 2)        /* 4275 */
1439
        MIPS_SYS(sys_mq_getsetattr, 3)
1440
        MIPS_SYS(sys_ni_syscall        , 0)        /* sys_vserver */
1441
        MIPS_SYS(sys_waitid        , 4)
1442
        MIPS_SYS(sys_ni_syscall        , 0)        /* available, was setaltroot */
1443
        MIPS_SYS(sys_add_key        , 5)
1444
        MIPS_SYS(sys_request_key, 4)
1445
        MIPS_SYS(sys_keyctl        , 5)
1446
        MIPS_SYS(sys_set_thread_area, 1)
1447
        MIPS_SYS(sys_inotify_init, 0)
1448
        MIPS_SYS(sys_inotify_add_watch, 3) /* 4285 */
1449
        MIPS_SYS(sys_inotify_rm_watch, 2)
1450
        MIPS_SYS(sys_migrate_pages, 4)
1451
        MIPS_SYS(sys_openat, 4)
1452
        MIPS_SYS(sys_mkdirat, 3)
1453
        MIPS_SYS(sys_mknodat, 4)        /* 4290 */
1454
        MIPS_SYS(sys_fchownat, 5)
1455
        MIPS_SYS(sys_futimesat, 3)
1456
        MIPS_SYS(sys_fstatat64, 4)
1457
        MIPS_SYS(sys_unlinkat, 3)
1458
        MIPS_SYS(sys_renameat, 4)        /* 4295 */
1459
        MIPS_SYS(sys_linkat, 5)
1460
        MIPS_SYS(sys_symlinkat, 3)
1461
        MIPS_SYS(sys_readlinkat, 4)
1462
        MIPS_SYS(sys_fchmodat, 3)
1463
        MIPS_SYS(sys_faccessat, 3)        /* 4300 */
1464
        MIPS_SYS(sys_pselect6, 6)
1465
        MIPS_SYS(sys_ppoll, 5)
1466
        MIPS_SYS(sys_unshare, 1)
1467
        MIPS_SYS(sys_splice, 4)
1468
        MIPS_SYS(sys_sync_file_range, 7) /* 4305 */
1469
        MIPS_SYS(sys_tee, 4)
1470
        MIPS_SYS(sys_vmsplice, 4)
1471
        MIPS_SYS(sys_move_pages, 6)
1472
        MIPS_SYS(sys_set_robust_list, 2)
1473
        MIPS_SYS(sys_get_robust_list, 3) /* 4310 */
1474
        MIPS_SYS(sys_kexec_load, 4)
1475
        MIPS_SYS(sys_getcpu, 3)
1476
        MIPS_SYS(sys_epoll_pwait, 6)
1477
        MIPS_SYS(sys_ioprio_set, 3)
1478
        MIPS_SYS(sys_ioprio_get, 2)
1479
};
1480

    
1481
#undef MIPS_SYS
1482

    
1483
void cpu_loop(CPUMIPSState *env)
1484
{
1485
    target_siginfo_t info;
1486
    int trapnr, ret;
1487
    unsigned int syscall_num;
1488

    
1489
    for(;;) {
1490
        trapnr = cpu_mips_exec(env);
1491
        switch(trapnr) {
1492
        case EXCP_SYSCALL:
1493
            syscall_num = env->gpr[2][env->current_tc] - 4000;
1494
            env->PC[env->current_tc] += 4;
1495
            if (syscall_num >= sizeof(mips_syscall_args)) {
1496
                ret = -ENOSYS;
1497
            } else {
1498
                int nb_args;
1499
                abi_ulong sp_reg;
1500
                abi_ulong arg5 = 0, arg6 = 0, arg7 = 0, arg8 = 0;
1501

    
1502
                nb_args = mips_syscall_args[syscall_num];
1503
                sp_reg = env->gpr[29][env->current_tc];
1504
                switch (nb_args) {
1505
                /* these arguments are taken from the stack */
1506
                case 8: arg8 = tgetl(sp_reg + 28);
1507
                case 7: arg7 = tgetl(sp_reg + 24);
1508
                case 6: arg6 = tgetl(sp_reg + 20);
1509
                case 5: arg5 = tgetl(sp_reg + 16);
1510
                default:
1511
                    break;
1512
                }
1513
                ret = do_syscall(env, env->gpr[2][env->current_tc],
1514
                                 env->gpr[4][env->current_tc],
1515
                                 env->gpr[5][env->current_tc],
1516
                                 env->gpr[6][env->current_tc],
1517
                                 env->gpr[7][env->current_tc],
1518
                                 arg5, arg6/*, arg7, arg8*/);
1519
            }
1520
            if ((unsigned int)ret >= (unsigned int)(-1133)) {
1521
                env->gpr[7][env->current_tc] = 1; /* error flag */
1522
                ret = -ret;
1523
            } else {
1524
                env->gpr[7][env->current_tc] = 0; /* error flag */
1525
            }
1526
            env->gpr[2][env->current_tc] = ret;
1527
            break;
1528
        case EXCP_TLBL:
1529
        case EXCP_TLBS:
1530
        case EXCP_CpU:
1531
        case EXCP_RI:
1532
            info.si_signo = TARGET_SIGILL;
1533
            info.si_errno = 0;
1534
            info.si_code = 0;
1535
            queue_signal(info.si_signo, &info);
1536
            break;
1537
        case EXCP_INTERRUPT:
1538
            /* just indicate that signals should be handled asap */
1539
            break;
1540
        case EXCP_DEBUG:
1541
            {
1542
                int sig;
1543

    
1544
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
1545
                if (sig)
1546
                  {
1547
                    info.si_signo = sig;
1548
                    info.si_errno = 0;
1549
                    info.si_code = TARGET_TRAP_BRKPT;
1550
                    queue_signal(info.si_signo, &info);
1551
                  }
1552
            }
1553
            break;
1554
        default:
1555
            //        error:
1556
            fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
1557
                    trapnr);
1558
            cpu_dump_state(env, stderr, fprintf, 0);
1559
            abort();
1560
        }
1561
        process_pending_signals(env);
1562
    }
1563
}
1564
#endif
1565

    
1566
#ifdef TARGET_SH4
1567
void cpu_loop (CPUState *env)
1568
{
1569
    int trapnr, ret;
1570
    target_siginfo_t info;
1571

    
1572
    while (1) {
1573
        trapnr = cpu_sh4_exec (env);
1574

    
1575
        switch (trapnr) {
1576
        case 0x160:
1577
            ret = do_syscall(env,
1578
                             env->gregs[3],
1579
                             env->gregs[4],
1580
                             env->gregs[5],
1581
                             env->gregs[6],
1582
                             env->gregs[7],
1583
                             env->gregs[0],
1584
                             0);
1585
            env->gregs[0] = ret;
1586
            env->pc += 2;
1587
            break;
1588
        case EXCP_DEBUG:
1589
            {
1590
                int sig;
1591

    
1592
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
1593
                if (sig)
1594
                  {
1595
                    info.si_signo = sig;
1596
                    info.si_errno = 0;
1597
                    info.si_code = TARGET_TRAP_BRKPT;
1598
                    queue_signal(info.si_signo, &info);
1599
                  }
1600
            }
1601
            break;
1602
        default:
1603
            printf ("Unhandled trap: 0x%x\n", trapnr);
1604
            cpu_dump_state(env, stderr, fprintf, 0);
1605
            exit (1);
1606
        }
1607
        process_pending_signals (env);
1608
    }
1609
}
1610
#endif
1611

    
1612
#ifdef TARGET_CRIS
1613
void cpu_loop (CPUState *env)
1614
{
1615
    int trapnr, ret;
1616
    target_siginfo_t info;
1617
    
1618
    while (1) {
1619
        trapnr = cpu_cris_exec (env);
1620
        switch (trapnr) {
1621
        case 0xaa:
1622
            {
1623
                info.si_signo = SIGSEGV;
1624
                info.si_errno = 0;
1625
                /* XXX: check env->error_code */
1626
                info.si_code = TARGET_SEGV_MAPERR;
1627
                info._sifields._sigfault._addr = env->debug1;
1628
                queue_signal(info.si_signo, &info);
1629
            }
1630
            break;
1631
        case EXCP_BREAK:
1632
            ret = do_syscall(env, 
1633
                             env->regs[9], 
1634
                             env->regs[10], 
1635
                             env->regs[11], 
1636
                             env->regs[12], 
1637
                             env->regs[13], 
1638
                             env->pregs[7], 
1639
                             env->pregs[11]);
1640
            env->regs[10] = ret;
1641
            env->pc += 2;
1642
            break;
1643
        case EXCP_DEBUG:
1644
            {
1645
                int sig;
1646

    
1647
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
1648
                if (sig)
1649
                  {
1650
                    info.si_signo = sig;
1651
                    info.si_errno = 0;
1652
                    info.si_code = TARGET_TRAP_BRKPT;
1653
                    queue_signal(info.si_signo, &info);
1654
                  }
1655
            }
1656
            break;
1657
        default:
1658
            printf ("Unhandled trap: 0x%x\n", trapnr);
1659
            cpu_dump_state(env, stderr, fprintf, 0);
1660
            exit (1);
1661
        }
1662
        process_pending_signals (env);
1663
    }
1664
}
1665
#endif
1666

    
1667
#ifdef TARGET_M68K
1668

    
1669
void cpu_loop(CPUM68KState *env)
1670
{
1671
    int trapnr;
1672
    unsigned int n;
1673
    target_siginfo_t info;
1674
    TaskState *ts = env->opaque;
1675

    
1676
    for(;;) {
1677
        trapnr = cpu_m68k_exec(env);
1678
        switch(trapnr) {
1679
        case EXCP_ILLEGAL:
1680
            {
1681
                if (ts->sim_syscalls) {
1682
                    uint16_t nr;
1683
                    nr = lduw(env->pc + 2);
1684
                    env->pc += 4;
1685
                    do_m68k_simcall(env, nr);
1686
                } else {
1687
                    goto do_sigill;
1688
                }
1689
            }
1690
            break;
1691
        case EXCP_HALT_INSN:
1692
            /* Semihosing syscall.  */
1693
            env->pc += 4;
1694
            do_m68k_semihosting(env, env->dregs[0]);
1695
            break;
1696
        case EXCP_LINEA:
1697
        case EXCP_LINEF:
1698
        case EXCP_UNSUPPORTED:
1699
        do_sigill:
1700
            info.si_signo = SIGILL;
1701
            info.si_errno = 0;
1702
            info.si_code = TARGET_ILL_ILLOPN;
1703
            info._sifields._sigfault._addr = env->pc;
1704
            queue_signal(info.si_signo, &info);
1705
            break;
1706
        case EXCP_TRAP0:
1707
            {
1708
                ts->sim_syscalls = 0;
1709
                n = env->dregs[0];
1710
                env->pc += 2;
1711
                env->dregs[0] = do_syscall(env,
1712
                                          n,
1713
                                          env->dregs[1],
1714
                                          env->dregs[2],
1715
                                          env->dregs[3],
1716
                                          env->dregs[4],
1717
                                          env->dregs[5],
1718
                                          env->dregs[6]);
1719
            }
1720
            break;
1721
        case EXCP_INTERRUPT:
1722
            /* just indicate that signals should be handled asap */
1723
            break;
1724
        case EXCP_ACCESS:
1725
            {
1726
                info.si_signo = SIGSEGV;
1727
                info.si_errno = 0;
1728
                /* XXX: check env->error_code */
1729
                info.si_code = TARGET_SEGV_MAPERR;
1730
                info._sifields._sigfault._addr = env->mmu.ar;
1731
                queue_signal(info.si_signo, &info);
1732
            }
1733
            break;
1734
        case EXCP_DEBUG:
1735
            {
1736
                int sig;
1737

    
1738
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
1739
                if (sig)
1740
                  {
1741
                    info.si_signo = sig;
1742
                    info.si_errno = 0;
1743
                    info.si_code = TARGET_TRAP_BRKPT;
1744
                    queue_signal(info.si_signo, &info);
1745
                  }
1746
            }
1747
            break;
1748
        default:
1749
            fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
1750
                    trapnr);
1751
            cpu_dump_state(env, stderr, fprintf, 0);
1752
            abort();
1753
        }
1754
        process_pending_signals(env);
1755
    }
1756
}
1757
#endif /* TARGET_M68K */
1758

    
1759
#ifdef TARGET_ALPHA
1760
void cpu_loop (CPUState *env)
1761
{
1762
    int trapnr;
1763
    target_siginfo_t info;
1764

    
1765
    while (1) {
1766
        trapnr = cpu_alpha_exec (env);
1767

    
1768
        switch (trapnr) {
1769
        case EXCP_RESET:
1770
            fprintf(stderr, "Reset requested. Exit\n");
1771
            exit(1);
1772
            break;
1773
        case EXCP_MCHK:
1774
            fprintf(stderr, "Machine check exception. Exit\n");
1775
            exit(1);
1776
            break;
1777
        case EXCP_ARITH:
1778
            fprintf(stderr, "Arithmetic trap.\n");
1779
            exit(1);
1780
            break;
1781
        case EXCP_HW_INTERRUPT:
1782
            fprintf(stderr, "External interrupt. Exit\n");
1783
            exit(1);
1784
            break;
1785
        case EXCP_DFAULT:
1786
            fprintf(stderr, "MMU data fault\n");
1787
            exit(1);
1788
            break;
1789
        case EXCP_DTB_MISS_PAL:
1790
            fprintf(stderr, "MMU data TLB miss in PALcode\n");
1791
            exit(1);
1792
            break;
1793
        case EXCP_ITB_MISS:
1794
            fprintf(stderr, "MMU instruction TLB miss\n");
1795
            exit(1);
1796
            break;
1797
        case EXCP_ITB_ACV:
1798
            fprintf(stderr, "MMU instruction access violation\n");
1799
            exit(1);
1800
            break;
1801
        case EXCP_DTB_MISS_NATIVE:
1802
            fprintf(stderr, "MMU data TLB miss\n");
1803
            exit(1);
1804
            break;
1805
        case EXCP_UNALIGN:
1806
            fprintf(stderr, "Unaligned access\n");
1807
            exit(1);
1808
            break;
1809
        case EXCP_OPCDEC:
1810
            fprintf(stderr, "Invalid instruction\n");
1811
            exit(1);
1812
            break;
1813
        case EXCP_FEN:
1814
            fprintf(stderr, "Floating-point not allowed\n");
1815
            exit(1);
1816
            break;
1817
        case EXCP_CALL_PAL ... (EXCP_CALL_PALP - 1):
1818
            fprintf(stderr, "Call to PALcode\n");
1819
            call_pal(env, (trapnr >> 6) | 0x80);
1820
            break;
1821
        case EXCP_CALL_PALP ... (EXCP_CALL_PALE - 1):
1822
            fprintf(stderr, "Privileged call to PALcode\n");
1823
            exit(1);
1824
            break;
1825
        case EXCP_DEBUG:
1826
            {
1827
                int sig;
1828

    
1829
                sig = gdb_handlesig (env, TARGET_SIGTRAP);
1830
                if (sig)
1831
                  {
1832
                    info.si_signo = sig;
1833
                    info.si_errno = 0;
1834
                    info.si_code = TARGET_TRAP_BRKPT;
1835
                    queue_signal(info.si_signo, &info);
1836
                  }
1837
            }
1838
            break;
1839
        default:
1840
            printf ("Unhandled trap: 0x%x\n", trapnr);
1841
            cpu_dump_state(env, stderr, fprintf, 0);
1842
            exit (1);
1843
        }
1844
        process_pending_signals (env);
1845
    }
1846
}
1847
#endif /* TARGET_ALPHA */
1848

    
1849
void usage(void)
1850
{
1851
    printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2007 Fabrice Bellard\n"
1852
           "usage: qemu-" TARGET_ARCH " [-h] [-g] [-d opts] [-L path] [-s size] [-cpu model] program [arguments...]\n"
1853
           "Linux CPU emulator (compiled for %s emulation)\n"
1854
           "\n"
1855
           "-h                print this help\n"
1856
           "-g port           wait gdb connection to port\n"
1857
           "-L path           set the elf interpreter prefix (default=%s)\n"
1858
           "-s size           set the stack size in bytes (default=%ld)\n"
1859
           "-cpu model        select CPU (-cpu ? for list)\n"
1860
           "-drop-ld-preload  drop LD_PRELOAD for target process\n"
1861
           "\n"
1862
           "debug options:\n"
1863
#ifdef USE_CODE_COPY
1864
           "-no-code-copy   disable code copy acceleration\n"
1865
#endif
1866
           "-d options   activate log (logfile=%s)\n"
1867
           "-p pagesize  set the host page size to 'pagesize'\n",
1868
           TARGET_ARCH,
1869
           interp_prefix,
1870
           x86_stack_size,
1871
           DEBUG_LOGFILE);
1872
    _exit(1);
1873
}
1874

    
1875
/* XXX: currently only used for async signals (see signal.c) */
1876
CPUState *global_env;
1877

    
1878
/* used to free thread contexts */
1879
TaskState *first_task_state;
1880

    
1881
int main(int argc, char **argv)
1882
{
1883
    const char *filename;
1884
    const char *cpu_model;
1885
    struct target_pt_regs regs1, *regs = &regs1;
1886
    struct image_info info1, *info = &info1;
1887
    TaskState ts1, *ts = &ts1;
1888
    CPUState *env;
1889
    int optind;
1890
    const char *r;
1891
    int gdbstub_port = 0;
1892
    int drop_ld_preload = 0, environ_count = 0;
1893
    char **target_environ, **wrk, **dst;
1894

    
1895
    if (argc <= 1)
1896
        usage();
1897

    
1898
    /* init debug */
1899
    cpu_set_log_filename(DEBUG_LOGFILE);
1900

    
1901
    cpu_model = NULL;
1902
    optind = 1;
1903
    for(;;) {
1904
        if (optind >= argc)
1905
            break;
1906
        r = argv[optind];
1907
        if (r[0] != '-')
1908
            break;
1909
        optind++;
1910
        r++;
1911
        if (!strcmp(r, "-")) {
1912
            break;
1913
        } else if (!strcmp(r, "d")) {
1914
            int mask;
1915
            CPULogItem *item;
1916

    
1917
            if (optind >= argc)
1918
                break;
1919

    
1920
            r = argv[optind++];
1921
            mask = cpu_str_to_log_mask(r);
1922
            if (!mask) {
1923
                printf("Log items (comma separated):\n");
1924
                for(item = cpu_log_items; item->mask != 0; item++) {
1925
                    printf("%-10s %s\n", item->name, item->help);
1926
                }
1927
                exit(1);
1928
            }
1929
            cpu_set_log(mask);
1930
        } else if (!strcmp(r, "s")) {
1931
            r = argv[optind++];
1932
            x86_stack_size = strtol(r, (char **)&r, 0);
1933
            if (x86_stack_size <= 0)
1934
                usage();
1935
            if (*r == 'M')
1936
                x86_stack_size *= 1024 * 1024;
1937
            else if (*r == 'k' || *r == 'K')
1938
                x86_stack_size *= 1024;
1939
        } else if (!strcmp(r, "L")) {
1940
            interp_prefix = argv[optind++];
1941
        } else if (!strcmp(r, "p")) {
1942
            qemu_host_page_size = atoi(argv[optind++]);
1943
            if (qemu_host_page_size == 0 ||
1944
                (qemu_host_page_size & (qemu_host_page_size - 1)) != 0) {
1945
                fprintf(stderr, "page size must be a power of two\n");
1946
                exit(1);
1947
            }
1948
        } else if (!strcmp(r, "g")) {
1949
            gdbstub_port = atoi(argv[optind++]);
1950
        } else if (!strcmp(r, "r")) {
1951
            qemu_uname_release = argv[optind++];
1952
        } else if (!strcmp(r, "cpu")) {
1953
            cpu_model = argv[optind++];
1954
            if (strcmp(cpu_model, "?") == 0) {
1955
/* XXX: implement xxx_cpu_list for targets that still miss it */
1956
#if defined(cpu_list)
1957
                    cpu_list(stdout, &fprintf);
1958
#endif
1959
                _exit(1);
1960
            }
1961
        } else if (!strcmp(r, "drop-ld-preload")) {
1962
            drop_ld_preload = 1;
1963
        } else
1964
#ifdef USE_CODE_COPY
1965
        if (!strcmp(r, "no-code-copy")) {
1966
            code_copy_enabled = 0;
1967
        } else
1968
#endif
1969
        {
1970
            usage();
1971
        }
1972
    }
1973
    if (optind >= argc)
1974
        usage();
1975
    filename = argv[optind];
1976

    
1977
    /* Zero out regs */
1978
    memset(regs, 0, sizeof(struct target_pt_regs));
1979

    
1980
    /* Zero out image_info */
1981
    memset(info, 0, sizeof(struct image_info));
1982

    
1983
    /* Scan interp_prefix dir for replacement files. */
1984
    init_paths(interp_prefix);
1985

    
1986
#if defined(TARGET_I386)
1987
    /* must be done before cpu_init() for x86 XXX: suppress this hack
1988
       by adding a new parameter to cpu_init and by suppressing
1989
       cpu_xxx_register() */
1990
    if (cpu_model == NULL) {
1991
#ifdef TARGET_X86_64
1992
        cpu_model = "qemu64";
1993
#else
1994
        cpu_model = "qemu32";
1995
#endif
1996
    }
1997
    if (x86_find_cpu_by_name(cpu_model)) {
1998
        fprintf(stderr, "Unable to find x86 CPU definition\n");
1999
        exit(1);
2000
    }
2001
#endif
2002

    
2003
    /* NOTE: we need to init the CPU at this stage to get
2004
       qemu_host_page_size */
2005
    env = cpu_init();
2006
    global_env = env;
2007

    
2008
    if(getenv("QEMU_STRACE") ){
2009
      do_strace=1;
2010
    }
2011

    
2012
    wrk = environ;
2013
    while (*(wrk++))
2014
        environ_count++;
2015

    
2016
    target_environ = malloc((environ_count + 1) * sizeof(char *));
2017
    if (!target_environ)
2018
        abort();
2019
    for (wrk = environ, dst = target_environ; *wrk; wrk++) {
2020
        if (drop_ld_preload && !strncmp(*wrk, "LD_PRELOAD=", 11))
2021
            continue;
2022
        *(dst++) = strdup(*wrk);
2023
    }
2024
    *dst = NULL; /* NULL terminate target_environ */
2025

    
2026
    if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
2027
        printf("Error loading %s\n", filename);
2028
        _exit(1);
2029
    }
2030

    
2031
    for (wrk = target_environ; *wrk; wrk++) {
2032
        free(*wrk);
2033
    }
2034

    
2035
    free(target_environ);
2036

    
2037
    if (loglevel) {
2038
        page_dump(logfile);
2039

    
2040
        fprintf(logfile, "start_brk   0x" TARGET_FMT_lx "\n", info->start_brk);
2041
        fprintf(logfile, "end_code    0x" TARGET_FMT_lx "\n", info->end_code);
2042
        fprintf(logfile, "start_code  0x" TARGET_FMT_lx "\n",
2043
                info->start_code);
2044
        fprintf(logfile, "start_data  0x" TARGET_FMT_lx "\n",
2045
                info->start_data);
2046
        fprintf(logfile, "end_data    0x" TARGET_FMT_lx "\n", info->end_data);
2047
        fprintf(logfile, "start_stack 0x" TARGET_FMT_lx "\n",
2048
                info->start_stack);
2049
        fprintf(logfile, "brk         0x" TARGET_FMT_lx "\n", info->brk);
2050
        fprintf(logfile, "entry       0x" TARGET_FMT_lx "\n", info->entry);
2051
    }
2052

    
2053
    target_set_brk(info->brk);
2054
    syscall_init();
2055
    signal_init();
2056

    
2057
    /* build Task State */
2058
    memset(ts, 0, sizeof(TaskState));
2059
    env->opaque = ts;
2060
    ts->used = 1;
2061
    ts->info = info;
2062
    env->user_mode_only = 1;
2063

    
2064
#if defined(TARGET_I386)
2065
    cpu_x86_set_cpl(env, 3);
2066

    
2067
    env->cr[0] = CR0_PG_MASK | CR0_WP_MASK | CR0_PE_MASK;
2068
    env->hflags |= HF_PE_MASK;
2069
    if (env->cpuid_features & CPUID_SSE) {
2070
        env->cr[4] |= CR4_OSFXSR_MASK;
2071
        env->hflags |= HF_OSFXSR_MASK;
2072
    }
2073

    
2074
    /* flags setup : we activate the IRQs by default as in user mode */
2075
    env->eflags |= IF_MASK;
2076

    
2077
    /* linux register setup */
2078
#if defined(TARGET_X86_64)
2079
    env->regs[R_EAX] = regs->rax;
2080
    env->regs[R_EBX] = regs->rbx;
2081
    env->regs[R_ECX] = regs->rcx;
2082
    env->regs[R_EDX] = regs->rdx;
2083
    env->regs[R_ESI] = regs->rsi;
2084
    env->regs[R_EDI] = regs->rdi;
2085
    env->regs[R_EBP] = regs->rbp;
2086
    env->regs[R_ESP] = regs->rsp;
2087
    env->eip = regs->rip;
2088
#else
2089
    env->regs[R_EAX] = regs->eax;
2090
    env->regs[R_EBX] = regs->ebx;
2091
    env->regs[R_ECX] = regs->ecx;
2092
    env->regs[R_EDX] = regs->edx;
2093
    env->regs[R_ESI] = regs->esi;
2094
    env->regs[R_EDI] = regs->edi;
2095
    env->regs[R_EBP] = regs->ebp;
2096
    env->regs[R_ESP] = regs->esp;
2097
    env->eip = regs->eip;
2098
#endif
2099

    
2100
    /* linux interrupt setup */
2101
    env->idt.base = h2g(idt_table);
2102
    env->idt.limit = sizeof(idt_table) - 1;
2103
    set_idt(0, 0);
2104
    set_idt(1, 0);
2105
    set_idt(2, 0);
2106
    set_idt(3, 3);
2107
    set_idt(4, 3);
2108
    set_idt(5, 3);
2109
    set_idt(6, 0);
2110
    set_idt(7, 0);
2111
    set_idt(8, 0);
2112
    set_idt(9, 0);
2113
    set_idt(10, 0);
2114
    set_idt(11, 0);
2115
    set_idt(12, 0);
2116
    set_idt(13, 0);
2117
    set_idt(14, 0);
2118
    set_idt(15, 0);
2119
    set_idt(16, 0);
2120
    set_idt(17, 0);
2121
    set_idt(18, 0);
2122
    set_idt(19, 0);
2123
    set_idt(0x80, 3);
2124

    
2125
    /* linux segment setup */
2126
    env->gdt.base = h2g(gdt_table);
2127
    env->gdt.limit = sizeof(gdt_table) - 1;
2128
    write_dt(&gdt_table[__USER_CS >> 3], 0, 0xfffff,
2129
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2130
             (3 << DESC_DPL_SHIFT) | (0xa << DESC_TYPE_SHIFT));
2131
    write_dt(&gdt_table[__USER_DS >> 3], 0, 0xfffff,
2132
             DESC_G_MASK | DESC_B_MASK | DESC_P_MASK | DESC_S_MASK |
2133
             (3 << DESC_DPL_SHIFT) | (0x2 << DESC_TYPE_SHIFT));
2134
    cpu_x86_load_seg(env, R_CS, __USER_CS);
2135
    cpu_x86_load_seg(env, R_DS, __USER_DS);
2136
    cpu_x86_load_seg(env, R_ES, __USER_DS);
2137
    cpu_x86_load_seg(env, R_SS, __USER_DS);
2138
    cpu_x86_load_seg(env, R_FS, __USER_DS);
2139
    cpu_x86_load_seg(env, R_GS, __USER_DS);
2140

    
2141
    /* This hack makes Wine work... */
2142
    env->segs[R_FS].selector = 0;
2143
#elif defined(TARGET_ARM)
2144
    {
2145
        int i;
2146
        if (cpu_model == NULL)
2147
            cpu_model = "arm926";
2148
        cpu_arm_set_model(env, cpu_model);
2149
        cpsr_write(env, regs->uregs[16], 0xffffffff);
2150
        for(i = 0; i < 16; i++) {
2151
            env->regs[i] = regs->uregs[i];
2152
        }
2153
    }
2154
#elif defined(TARGET_SPARC)
2155
    {
2156
        int i;
2157
        const sparc_def_t *def;
2158
#ifdef TARGET_SPARC64
2159
        if (cpu_model == NULL)
2160
            cpu_model = "TI UltraSparc II";
2161
#else
2162
        if (cpu_model == NULL)
2163
            cpu_model = "Fujitsu MB86904";
2164
#endif
2165
        sparc_find_by_name(cpu_model, &def);
2166
        if (def == NULL) {
2167
            fprintf(stderr, "Unable to find Sparc CPU definition\n");
2168
            exit(1);
2169
        }
2170
        cpu_sparc_register(env, def, 0);
2171
        env->pc = regs->pc;
2172
        env->npc = regs->npc;
2173
        env->y = regs->y;
2174
        for(i = 0; i < 8; i++)
2175
            env->gregs[i] = regs->u_regs[i];
2176
        for(i = 0; i < 8; i++)
2177
            env->regwptr[i] = regs->u_regs[i + 8];
2178
    }
2179
#elif defined(TARGET_PPC)
2180
    {
2181
        ppc_def_t *def;
2182
        int i;
2183

    
2184
        /* Choose and initialise CPU */
2185
        if (cpu_model == NULL)
2186
            cpu_model = "750";
2187
        ppc_find_by_name(cpu_model, &def);
2188
        if (def == NULL) {
2189
            cpu_abort(env,
2190
                      "Unable to find PowerPC CPU definition\n");
2191
        }
2192
        cpu_ppc_register(env, def);
2193
        cpu_ppc_reset(env);
2194
#if defined(TARGET_PPC64)
2195
#if defined(TARGET_ABI32)
2196
        env->msr &= ~((target_ulong)1 << MSR_SF);
2197
#else
2198
        env->msr |= (target_ulong)1 << MSR_SF;
2199
#endif
2200
#endif
2201
        env->nip = regs->nip;
2202
        for(i = 0; i < 32; i++) {
2203
            env->gpr[i] = regs->gpr[i];
2204
        }
2205
    }
2206
#elif defined(TARGET_M68K)
2207
    {
2208
        if (cpu_model == NULL)
2209
            cpu_model = "any";
2210
        if (cpu_m68k_set_model(env, cpu_model)) {
2211
            cpu_abort(cpu_single_env,
2212
                      "Unable to find m68k CPU definition\n");
2213
        }
2214
        env->pc = regs->pc;
2215
        env->dregs[0] = regs->d0;
2216
        env->dregs[1] = regs->d1;
2217
        env->dregs[2] = regs->d2;
2218
        env->dregs[3] = regs->d3;
2219
        env->dregs[4] = regs->d4;
2220
        env->dregs[5] = regs->d5;
2221
        env->dregs[6] = regs->d6;
2222
        env->dregs[7] = regs->d7;
2223
        env->aregs[0] = regs->a0;
2224
        env->aregs[1] = regs->a1;
2225
        env->aregs[2] = regs->a2;
2226
        env->aregs[3] = regs->a3;
2227
        env->aregs[4] = regs->a4;
2228
        env->aregs[5] = regs->a5;
2229
        env->aregs[6] = regs->a6;
2230
        env->aregs[7] = regs->usp;
2231
        env->sr = regs->sr;
2232
        ts->sim_syscalls = 1;
2233
    }
2234
#elif defined(TARGET_MIPS)
2235
    {
2236
        mips_def_t *def;
2237
        int i;
2238

    
2239
        /* Choose and initialise CPU */
2240
        if (cpu_model == NULL)
2241
#if defined(TARGET_MIPSN32) || defined(TARGET_MIPS64)
2242
            cpu_model = "20Kc";
2243
#else
2244
            cpu_model = "24Kf";
2245
#endif
2246
        mips_find_by_name(cpu_model, &def);
2247
        if (def == NULL)
2248
            cpu_abort(env, "Unable to find MIPS CPU definition\n");
2249
        cpu_mips_register(env, def);
2250

    
2251
        for(i = 0; i < 32; i++) {
2252
            env->gpr[i][env->current_tc] = regs->regs[i];
2253
        }
2254
        env->PC[env->current_tc] = regs->cp0_epc;
2255
    }
2256
#elif defined(TARGET_SH4)
2257
    {
2258
        int i;
2259

    
2260
        for(i = 0; i < 16; i++) {
2261
            env->gregs[i] = regs->regs[i];
2262
        }
2263
        env->pc = regs->pc;
2264
    }
2265
#elif defined(TARGET_ALPHA)
2266
    {
2267
        int i;
2268

    
2269
        for(i = 0; i < 28; i++) {
2270
            env->ir[i] = ((abi_ulong *)regs)[i];
2271
        }
2272
        env->ipr[IPR_USP] = regs->usp;
2273
        env->ir[30] = regs->usp;
2274
        env->pc = regs->pc;
2275
        env->unique = regs->unique;
2276
    }
2277
#elif defined(TARGET_CRIS)
2278
    {
2279
            env->regs[0] = regs->r0;
2280
            env->regs[1] = regs->r1;
2281
            env->regs[2] = regs->r2;
2282
            env->regs[3] = regs->r3;
2283
            env->regs[4] = regs->r4;
2284
            env->regs[5] = regs->r5;
2285
            env->regs[6] = regs->r6;
2286
            env->regs[7] = regs->r7;
2287
            env->regs[8] = regs->r8;
2288
            env->regs[9] = regs->r9;
2289
            env->regs[10] = regs->r10;
2290
            env->regs[11] = regs->r11;
2291
            env->regs[12] = regs->r12;
2292
            env->regs[13] = regs->r13;
2293
            env->regs[14] = info->start_stack;
2294
            env->regs[15] = regs->acr;            
2295
            env->pc = regs->erp;
2296
    }
2297
#else
2298
#error unsupported target CPU
2299
#endif
2300

    
2301
#if defined(TARGET_ARM) || defined(TARGET_M68K)
2302
    ts->stack_base = info->start_stack;
2303
    ts->heap_base = info->brk;
2304
    /* This will be filled in on the first SYS_HEAPINFO call.  */
2305
    ts->heap_limit = 0;
2306
#endif
2307

    
2308
    if (gdbstub_port) {
2309
        gdbserver_start (gdbstub_port);
2310
        gdb_handlesig(env, 0);
2311
    }
2312
    cpu_loop(env);
2313
    /* never exits */
2314
    return 0;
2315
}