xref: /openbmc/qemu/linux-user/hexagon/cpu_loop.c (revision 2113aed6)
1 /*
2  *  qemu user cpu loop
3  *
4  *  Copyright (c) 2003-2008 Fabrice Bellard
5  *  Copyright(c) 2019-2021 Qualcomm Innovation Center, Inc. All Rights Reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "qemu/osdep.h"
22 #include "qemu.h"
23 #include "cpu_loop-common.h"
24 #include "signal-common.h"
25 #include "internal.h"
26 
27 void cpu_loop(CPUHexagonState *env)
28 {
29     CPUState *cs = env_cpu(env);
30     int trapnr, signum, sigcode;
31     target_ulong sigaddr;
32     target_ulong syscallnum;
33     target_ulong ret;
34 
35     for (;;) {
36         cpu_exec_start(cs);
37         trapnr = cpu_exec(cs);
38         cpu_exec_end(cs);
39         process_queued_cpu_work(cs);
40 
41         signum = 0;
42         sigcode = 0;
43         sigaddr = 0;
44 
45         switch (trapnr) {
46         case EXCP_INTERRUPT:
47             /* just indicate that signals should be handled asap */
48             break;
49         case HEX_EXCP_TRAP0:
50             syscallnum = env->gpr[6];
51             env->gpr[HEX_REG_PC] += 4;
52             ret = do_syscall(env,
53                              syscallnum,
54                              env->gpr[0],
55                              env->gpr[1],
56                              env->gpr[2],
57                              env->gpr[3],
58                              env->gpr[4],
59                              env->gpr[5],
60                              0, 0);
61             if (ret == -TARGET_ERESTARTSYS) {
62                 env->gpr[HEX_REG_PC] -= 4;
63             } else if (ret != -TARGET_QEMU_ESIGRETURN) {
64                 env->gpr[0] = ret;
65             }
66             break;
67         case HEX_EXCP_FETCH_NO_UPAGE:
68         case HEX_EXCP_PRIV_NO_UREAD:
69         case HEX_EXCP_PRIV_NO_UWRITE:
70             signum = TARGET_SIGSEGV;
71             sigcode = TARGET_SEGV_MAPERR;
72             break;
73         case EXCP_ATOMIC:
74             cpu_exec_step_atomic(cs);
75             break;
76         default:
77             EXCP_DUMP(env, "\nqemu: unhandled CPU exception %#x - aborting\n",
78                      trapnr);
79             exit(EXIT_FAILURE);
80         }
81 
82         if (signum) {
83             target_siginfo_t info = {
84                 .si_signo = signum,
85                 .si_errno = 0,
86                 .si_code = sigcode,
87                 ._sifields._sigfault._addr = sigaddr
88             };
89             queue_signal(env, info.si_signo, QEMU_SI_KILL, &info);
90         }
91 
92         process_pending_signals(env);
93     }
94 }
95 
96 void target_cpu_copy_regs(CPUArchState *env, struct target_pt_regs *regs)
97 {
98     env->gpr[HEX_REG_PC] = regs->sepc;
99     env->gpr[HEX_REG_SP] = regs->sp;
100     env->gpr[HEX_REG_USR] = 0x56000;
101 }
102