14
14
* GNU General Public License for more details.
16
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., 51 Franklin Street - Fifth Floor, Boston,
17
* along with this program; if not, see <http://www.gnu.org/licenses/>.
21
19
#include <stdlib.h>
42
#if defined(CONFIG_USE_GUEST_BASE)
43
unsigned long mmap_min_addr;
44
unsigned long guest_base;
42
48
static const char *interp_prefix = CONFIG_QEMU_PREFIX;
43
49
const char *qemu_uname_release = CONFIG_UNAME_RELEASE;
85
void cpu_outb(CPUState *env, int addr, int val)
87
fprintf(stderr, "outb: port=0x%04x, data=%02x\n", addr, val);
90
void cpu_outw(CPUState *env, int addr, int val)
92
fprintf(stderr, "outw: port=0x%04x, data=%04x\n", addr, val);
95
void cpu_outl(CPUState *env, int addr, int val)
97
fprintf(stderr, "outl: port=0x%04x, data=%08x\n", addr, val);
100
int cpu_inb(CPUState *env, int addr)
102
fprintf(stderr, "inb: port=0x%04x\n", addr);
106
int cpu_inw(CPUState *env, int addr)
108
fprintf(stderr, "inw: port=0x%04x\n", addr);
112
int cpu_inl(CPUState *env, int addr)
114
fprintf(stderr, "inl: port=0x%04x\n", addr);
118
91
#if defined(TARGET_I386)
119
92
int cpu_get_pic_interrupt(CPUState *env)
143
116
We don't require a full sync, only that no cpus are executing guest code.
144
117
The alternative is to map target atomic ops onto host equivalents,
145
118
which requires quite a lot of per host/target work. */
119
static pthread_mutex_t cpu_list_mutex = PTHREAD_MUTEX_INITIALIZER;
146
120
static pthread_mutex_t exclusive_lock = PTHREAD_MUTEX_INITIALIZER;
147
121
static pthread_cond_t exclusive_cond = PTHREAD_COND_INITIALIZER;
148
122
static pthread_cond_t exclusive_resume = PTHREAD_COND_INITIALIZER;
165
139
thread_env->next_cpu = NULL;
166
140
pending_cpus = 0;
167
141
pthread_mutex_init(&exclusive_lock, NULL);
142
pthread_mutex_init(&cpu_list_mutex, NULL);
168
143
pthread_cond_init(&exclusive_cond, NULL);
169
144
pthread_cond_init(&exclusive_resume, NULL);
170
145
pthread_mutex_init(&tb_lock, NULL);
237
212
exclusive_idle();
238
213
pthread_mutex_unlock(&exclusive_lock);
240
#else /* if !USE_NPTL */
216
void cpu_list_lock(void)
218
pthread_mutex_lock(&cpu_list_mutex);
221
void cpu_list_unlock(void)
223
pthread_mutex_unlock(&cpu_list_mutex);
225
#else /* if !CONFIG_USE_NPTL */
241
226
/* These are no-ops because we are not threadsafe. */
242
227
static inline void cpu_exec_start(CPUState *env)
1061
#define EXCP_DUMP(env, fmt, args...) \
1063
fprintf(stderr, fmt , ##args); \
1064
cpu_dump_state(env, stderr, fprintf, 0); \
1065
qemu_log(fmt, ##args); \
1066
log_cpu_state(env, 0); \
1054
#define EXCP_DUMP(env, fmt, ...) \
1056
fprintf(stderr, fmt , ## __VA_ARGS__); \
1057
cpu_dump_state(env, stderr, fprintf, 0); \
1058
qemu_log(fmt, ## __VA_ARGS__); \
1060
log_cpu_state(env, 0); \
1069
1063
void cpu_loop(CPUPPCState *env)
1439
1433
ret = do_syscall(env, env->gpr[0], env->gpr[3], env->gpr[4],
1440
1434
env->gpr[5], env->gpr[6], env->gpr[7],
1436
if (ret == (uint32_t)(-TARGET_QEMU_ESIGRETURN)) {
1437
/* Returning from a successful sigreturn syscall.
1438
Avoid corrupting register state. */
1442
1441
if (ret > (uint32_t)(-515)) {
1443
1442
env->crf[0] |= 0x1;
1598
1597
MIPS_SYS(sys_ipc , 6)
1599
1598
MIPS_SYS(sys_fsync , 1)
1600
1599
MIPS_SYS(sys_sigreturn , 0)
1601
MIPS_SYS(sys_clone , 0) /* 4120 */
1600
MIPS_SYS(sys_clone , 6) /* 4120 */
1602
1601
MIPS_SYS(sys_setdomainname, 2)
1603
1602
MIPS_SYS(sys_newuname , 1)
1604
1603
MIPS_SYS(sys_ni_syscall , 0) /* sys_modify_ldt */
1799
1798
#undef MIPS_SYS
1800
static int do_store_exclusive(CPUMIPSState *env)
1803
target_ulong page_addr;
1810
addr = env->CP0_LLAddr;
1811
page_addr = addr & TARGET_PAGE_MASK;
1814
flags = page_get_flags(page_addr);
1815
if ((flags & PAGE_READ) == 0) {
1818
reg = env->llreg & 0x1f;
1819
d = (env->llreg & 0x20) != 0;
1821
segv = get_user_s64(val, addr);
1823
segv = get_user_s32(val, addr);
1826
if (val != env->llval) {
1827
env->active_tc.gpr[reg] = 0;
1830
segv = put_user_u64(env->llnewval, addr);
1832
segv = put_user_u32(env->llnewval, addr);
1835
env->active_tc.gpr[reg] = 1;
1840
env->CP0_LLAddr = -1;
1842
env->active_tc.PC += 4;
1801
1849
void cpu_loop(CPUMIPSState *env)
1803
1851
target_siginfo_t info;
1836
1886
env->active_tc.gpr[7],
1837
1887
arg5, arg6/*, arg7, arg8*/);
1889
if (ret == -TARGET_QEMU_ESIGRETURN) {
1890
/* Returning from a successful sigreturn syscall.
1891
Avoid clobbering register state. */
1839
1894
if ((unsigned int)ret >= (unsigned int)(-1133)) {
1840
1895
env->active_tc.gpr[7] = 1; /* error flag */
1847
1902
case EXCP_TLBL:
1848
1903
case EXCP_TLBS:
1904
info.si_signo = TARGET_SIGSEGV;
1906
/* XXX: check env->error_code */
1907
info.si_code = TARGET_SEGV_MAPERR;
1908
info._sifields._sigfault._addr = env->CP0_BadVAddr;
1909
queue_signal(env, info.si_signo, &info);
1851
1913
info.si_signo = TARGET_SIGILL;
1936
if (do_store_exclusive(env)) {
1937
info.si_signo = TARGET_SIGSEGV;
1939
info.si_code = TARGET_SEGV_MAPERR;
1940
info._sifields._sigfault._addr = env->active_tc.PC;
1941
queue_signal(env, info.si_signo, &info);
1875
1946
fprintf(stderr, "qemu: unhandled CPU exception 0x%x - aborting\n",
2071
#ifdef TARGET_MICROBLAZE
2072
void cpu_loop (CPUState *env)
2075
target_siginfo_t info;
2078
trapnr = cpu_mb_exec (env);
2082
info.si_signo = SIGSEGV;
2084
/* XXX: check env->error_code */
2085
info.si_code = TARGET_SEGV_MAPERR;
2086
info._sifields._sigfault._addr = 0;
2087
queue_signal(env, info.si_signo, &info);
2090
case EXCP_INTERRUPT:
2091
/* just indicate that signals should be handled asap */
2094
/* Return address is 4 bytes after the call. */
2096
ret = do_syscall(env,
2105
env->sregs[SR_PC] = env->regs[14];
2111
sig = gdb_handlesig (env, TARGET_SIGTRAP);
2114
info.si_signo = sig;
2116
info.si_code = TARGET_TRAP_BRKPT;
2117
queue_signal(env, info.si_signo, &info);
2122
printf ("Unhandled trap: 0x%x\n", trapnr);
2123
cpu_dump_state(env, stderr, fprintf, 0);
2126
process_pending_signals (env);
2000
2131
#ifdef TARGET_M68K
2002
2133
void cpu_loop(CPUM68KState *env)
2181
2312
static void usage(void)
2183
printf("qemu-" TARGET_ARCH " version " QEMU_VERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
2314
printf("qemu-" TARGET_ARCH " version " QEMU_VERSION QEMU_PKGVERSION ", Copyright (c) 2003-2008 Fabrice Bellard\n"
2184
2315
"usage: qemu-" TARGET_ARCH " [options] program [arguments...]\n"
2185
2316
"Linux CPU emulator (compiled for %s emulation)\n"
2193
2324
"-drop-ld-preload drop LD_PRELOAD for target process\n"
2194
2325
"-E var=value sets/modifies targets environment variable(s)\n"
2195
2326
"-U var unsets targets environment variable(s)\n"
2327
"-0 argv0 forces target process argv[0] to be argv0\n"
2328
#if defined(CONFIG_USE_GUEST_BASE)
2329
"-B address set guest_base address to address\n"
2197
2332
"Debug options:\n"
2198
2333
"-d options activate log (logfile=%s)\n"
2199
2334
"-p pagesize set the host page size to 'pagesize'\n"
2335
"-singlestep always run in singlestep mode\n"
2200
2336
"-strace log system calls\n"
2202
2338
"Environment variables:\n"
2219
2355
THREAD CPUState *thread_env;
2357
void task_settid(TaskState *ts)
2359
if (ts->ts_tid == 0) {
2360
#ifdef CONFIG_USE_NPTL
2361
ts->ts_tid = (pid_t)syscall(SYS_gettid);
2363
/* when no threads are used, tid becomes pid */
2364
ts->ts_tid = getpid();
2369
void stop_all_tasks(void)
2372
* We trust that when using NPTL, start_exclusive()
2373
* handles thread stopping correctly.
2221
2378
/* Assumes contents are already zeroed. */
2222
2379
void init_task_state(TaskState *ts)
2237
2394
const char *cpu_model;
2238
2395
struct target_pt_regs regs1, *regs = ®s1;
2239
2396
struct image_info info1, *info = &info1;
2397
struct linux_binprm bprm;
2240
2398
TaskState ts1, *ts = &ts1;
2244
2402
int gdbstub_port = 0;
2245
2403
char **target_environ, **wrk;
2246
2406
envlist_t *envlist = NULL;
2407
const char *argv0 = NULL;
2506
#if defined(CONFIG_USE_GUEST_BASE)
2507
} else if (!strcmp(r, "B")) {
2508
guest_base = strtol(argv[optind++], NULL, 0);
2509
have_guest_base = 1;
2340
2511
} else if (!strcmp(r, "drop-ld-preload")) {
2341
2512
(void) envlist_unsetenv(envlist, "LD_PRELOAD");
2513
} else if (!strcmp(r, "singlestep")) {
2342
2515
} else if (!strcmp(r, "strace")) {
2357
2530
/* Zero out image_info */
2358
2531
memset(info, 0, sizeof(struct image_info));
2533
memset(&bprm, 0, sizeof (bprm));
2360
2535
/* Scan interp_prefix dir for replacement files. */
2361
2536
init_paths(interp_prefix);
2410
2585
target_environ = envlist_to_environ(envlist, NULL);
2411
2586
envlist_free(envlist);
2413
if (loader_exec(filename, argv+optind, target_environ, regs, info) != 0) {
2414
printf("Error loading %s\n", filename);
2588
#if defined(CONFIG_USE_GUEST_BASE)
2590
* Now that page sizes are configured in cpu_init() we can do
2591
* proper page alignment for guest_base.
2593
guest_base = HOST_PAGE_ALIGN(guest_base);
2596
* Read in mmap_min_addr kernel parameter. This value is used
2597
* When loading the ELF image to determine whether guest_base
2600
* When user has explicitly set the quest base, we skip this
2603
if (!have_guest_base) {
2606
if ((fp = fopen("/proc/sys/vm/mmap_min_addr", "r")) != NULL) {
2608
if (fscanf(fp, "%lu", &tmp) == 1) {
2609
mmap_min_addr = tmp;
2610
qemu_log("host mmap_min_addr=0x%lx\n", mmap_min_addr);
2615
#endif /* CONFIG_USE_GUEST_BASE */
2618
* Prepare copy of argv vector for target.
2620
target_argc = argc - optind;
2621
target_argv = calloc(target_argc + 1, sizeof (char *));
2622
if (target_argv == NULL) {
2623
(void) fprintf(stderr, "Unable to allocate memory for target_argv\n");
2628
* If argv0 is specified (using '-0' switch) we replace
2629
* argv[0] pointer with the given one.
2632
if (argv0 != NULL) {
2633
target_argv[i++] = strdup(argv0);
2635
for (; i < target_argc; i++) {
2636
target_argv[i] = strdup(argv[optind + i]);
2638
target_argv[target_argc] = NULL;
2640
memset(ts, 0, sizeof(TaskState));
2641
init_task_state(ts);
2642
/* build Task State */
2648
ret = loader_exec(filename, target_argv, target_environ, regs,
2651
printf("Error %d while loading %s\n", ret, filename);
2655
for (i = 0; i < target_argc; i++) {
2656
free(target_argv[i]);
2418
2660
for (wrk = target_environ; *wrk; wrk++) {
2422
2664
free(target_environ);
2424
2666
if (qemu_log_enabled()) {
2667
#if defined(CONFIG_USE_GUEST_BASE)
2668
qemu_log("guest_base 0x%lx\n", guest_base);
2425
2670
log_page_dump();
2427
2672
qemu_log("start_brk 0x" TARGET_ABI_FMT_lx "\n", info->start_brk);
2620
2859
env->sr = regs->sr;
2621
2860
ts->sim_syscalls = 1;
2862
#elif defined(TARGET_MICROBLAZE)
2864
env->regs[0] = regs->r0;
2865
env->regs[1] = regs->r1;
2866
env->regs[2] = regs->r2;
2867
env->regs[3] = regs->r3;
2868
env->regs[4] = regs->r4;
2869
env->regs[5] = regs->r5;
2870
env->regs[6] = regs->r6;
2871
env->regs[7] = regs->r7;
2872
env->regs[8] = regs->r8;
2873
env->regs[9] = regs->r9;
2874
env->regs[10] = regs->r10;
2875
env->regs[11] = regs->r11;
2876
env->regs[12] = regs->r12;
2877
env->regs[13] = regs->r13;
2878
env->regs[14] = regs->r14;
2879
env->regs[15] = regs->r15;
2880
env->regs[16] = regs->r16;
2881
env->regs[17] = regs->r17;
2882
env->regs[18] = regs->r18;
2883
env->regs[19] = regs->r19;
2884
env->regs[20] = regs->r20;
2885
env->regs[21] = regs->r21;
2886
env->regs[22] = regs->r22;
2887
env->regs[23] = regs->r23;
2888
env->regs[24] = regs->r24;
2889
env->regs[25] = regs->r25;
2890
env->regs[26] = regs->r26;
2891
env->regs[27] = regs->r27;
2892
env->regs[28] = regs->r28;
2893
env->regs[29] = regs->r29;
2894
env->regs[30] = regs->r30;
2895
env->regs[31] = regs->r31;
2896
env->sregs[SR_PC] = regs->pc;
2623
2898
#elif defined(TARGET_MIPS)