4 * Copyright (c) 2003-2008 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
22 #include "cpu_loop-common.h"
24 void cpu_loop(CPUNios2State
*env
)
26 CPUState
*cs
= ENV_GET_CPU(env
);
27 Nios2CPU
*cpu
= NIOS2_CPU(cs
);
28 target_siginfo_t info
;
33 trapnr
= cpu_exec(cs
);
38 /* just indicate that signals should be handled asap */
41 if (env
->regs
[R_AT
] == 0) {
43 qemu_log_mask(CPU_LOG_INT
, "\nSyscall\n");
45 ret
= do_syscall(env
, env
->regs
[2],
46 env
->regs
[4], env
->regs
[5], env
->regs
[6],
47 env
->regs
[7], env
->regs
[8], env
->regs
[9],
50 if (env
->regs
[2] == 0) { /* FIXME: syscall 0 workaround */
54 env
->regs
[2] = abs(ret
);
55 /* Return value is 0..4096 */
56 env
->regs
[7] = (ret
> 0xfffffffffffff000ULL
);
57 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
58 env
->regs
[CR_STATUS
] &= ~0x3;
59 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
63 qemu_log_mask(CPU_LOG_INT
, "\nTrap\n");
65 env
->regs
[CR_ESTATUS
] = env
->regs
[CR_STATUS
];
66 env
->regs
[CR_STATUS
] &= ~0x3;
67 env
->regs
[R_EA
] = env
->regs
[R_PC
] + 4;
68 env
->regs
[R_PC
] = cpu
->exception_addr
;
70 info
.si_signo
= TARGET_SIGTRAP
;
72 info
.si_code
= TARGET_TRAP_BRKPT
;
73 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
77 switch (env
->regs
[R_PC
]) {
78 /*case 0x1000:*/ /* TODO:__kuser_helper_version */
79 case 0x1004: /* __kuser_cmpxchg */
81 if (env
->regs
[4] & 0x3) {
84 ret
= get_user_u32(env
->regs
[2], env
->regs
[4]);
89 env
->regs
[2] -= env
->regs
[5];
90 if (env
->regs
[2] == 0) {
91 put_user_u32(env
->regs
[6], env
->regs
[4]);
94 env
->regs
[R_PC
] = env
->regs
[R_RA
];
96 /*case 0x1040:*/ /* TODO:__kuser_sigtramp */
100 info
.si_signo
= TARGET_SIGSEGV
;
102 /* TODO: check env->error_code */
103 info
.si_code
= TARGET_SEGV_MAPERR
;
104 info
._sifields
._sigfault
._addr
= env
->regs
[R_PC
];
105 queue_signal(env
, info
.si_signo
, QEMU_SI_FAULT
, &info
);
109 EXCP_DUMP(env
, "\nqemu: unhandled CPU exception %#x - aborting\n",
114 process_pending_signals(env
);
118 void target_cpu_copy_regs(CPUArchState
*env
, struct target_pt_regs
*regs
)
121 env
->regs
[1] = regs
->r1
;
122 env
->regs
[2] = regs
->r2
;
123 env
->regs
[3] = regs
->r3
;
124 env
->regs
[4] = regs
->r4
;
125 env
->regs
[5] = regs
->r5
;
126 env
->regs
[6] = regs
->r6
;
127 env
->regs
[7] = regs
->r7
;
128 env
->regs
[8] = regs
->r8
;
129 env
->regs
[9] = regs
->r9
;
130 env
->regs
[10] = regs
->r10
;
131 env
->regs
[11] = regs
->r11
;
132 env
->regs
[12] = regs
->r12
;
133 env
->regs
[13] = regs
->r13
;
134 env
->regs
[14] = regs
->r14
;
135 env
->regs
[15] = regs
->r15
;
136 /* TODO: unsigned long orig_r2; */
137 env
->regs
[R_RA
] = regs
->ra
;
138 env
->regs
[R_FP
] = regs
->fp
;
139 env
->regs
[R_SP
] = regs
->sp
;
140 env
->regs
[R_GP
] = regs
->gp
;
141 env
->regs
[CR_ESTATUS
] = regs
->estatus
;
142 env
->regs
[R_EA
] = regs
->ea
;
143 /* TODO: unsigned long orig_r7; */
145 /* Emulate eret when starting thread. */
146 env
->regs
[R_PC
] = regs
->ea
;