xref: /openbmc/qemu/bsd-user/riscv/signal.c (revision feb58e3b)
1 /*
2  *  RISC-V signal definitions
3  *
4  *  Copyright (c) 2019 Mark Corbin
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, see <http://www.gnu.org/licenses/>.
18  */
19 #include "qemu/osdep.h"
20 
21 #include "qemu.h"
22 
23 /*
24  * Compare with sendsig() in riscv/riscv/exec_machdep.c
25  * Assumes that target stack frame memory is locked.
26  */
27 abi_long
28 set_sigtramp_args(CPURISCVState *regs, int sig, struct target_sigframe *frame,
29     abi_ulong frame_addr, struct target_sigaction *ka)
30 {
31     /*
32      * Arguments to signal handler:
33      *  a0 (10) = signal number
34      *  a1 (11) = siginfo pointer
35      *  a2 (12) = ucontext pointer
36      *  pc      = signal pointer handler
37      *  sp (2)  = sigframe pointer
38      *  ra (1)  = sigtramp at base of user stack
39      */
40 
41      regs->gpr[xA0] = sig;
42      regs->gpr[xA1] = frame_addr +
43          offsetof(struct target_sigframe, sf_si);
44      regs->gpr[xA2] = frame_addr +
45          offsetof(struct target_sigframe, sf_uc);
46      regs->pc = ka->_sa_handler;
47      regs->gpr[xSP] = frame_addr;
48      regs->gpr[xRA] = TARGET_PS_STRINGS - TARGET_SZSIGCODE;
49      return 0;
50 }
51 
52 /*
53  * Compare to riscv/riscv/exec_machdep.c sendsig()
54  * Assumes that the memory is locked if frame points to user memory.
55  */
56 abi_long setup_sigframe_arch(CPURISCVState *env, abi_ulong frame_addr,
57                              struct target_sigframe *frame, int flags)
58 {
59     target_mcontext_t *mcp = &frame->sf_uc.uc_mcontext;
60 
61     get_mcontext(env, mcp, flags);
62     return 0;
63 }
64 
65 /*
66  * Compare with get_mcontext() in riscv/riscv/machdep.c
67  * Assumes that the memory is locked if mcp points to user memory.
68  */
69 abi_long get_mcontext(CPURISCVState *regs, target_mcontext_t *mcp,
70         int flags)
71 {
72 
73     mcp->mc_gpregs.gp_t[0] = tswap64(regs->gpr[5]);
74     mcp->mc_gpregs.gp_t[1] = tswap64(regs->gpr[6]);
75     mcp->mc_gpregs.gp_t[2] = tswap64(regs->gpr[7]);
76     mcp->mc_gpregs.gp_t[3] = tswap64(regs->gpr[28]);
77     mcp->mc_gpregs.gp_t[4] = tswap64(regs->gpr[29]);
78     mcp->mc_gpregs.gp_t[5] = tswap64(regs->gpr[30]);
79     mcp->mc_gpregs.gp_t[6] = tswap64(regs->gpr[31]);
80 
81     mcp->mc_gpregs.gp_s[0] = tswap64(regs->gpr[8]);
82     mcp->mc_gpregs.gp_s[1] = tswap64(regs->gpr[9]);
83     mcp->mc_gpregs.gp_s[2] = tswap64(regs->gpr[18]);
84     mcp->mc_gpregs.gp_s[3] = tswap64(regs->gpr[19]);
85     mcp->mc_gpregs.gp_s[4] = tswap64(regs->gpr[20]);
86     mcp->mc_gpregs.gp_s[5] = tswap64(regs->gpr[21]);
87     mcp->mc_gpregs.gp_s[6] = tswap64(regs->gpr[22]);
88     mcp->mc_gpregs.gp_s[7] = tswap64(regs->gpr[23]);
89     mcp->mc_gpregs.gp_s[8] = tswap64(regs->gpr[24]);
90     mcp->mc_gpregs.gp_s[9] = tswap64(regs->gpr[25]);
91     mcp->mc_gpregs.gp_s[10] = tswap64(regs->gpr[26]);
92     mcp->mc_gpregs.gp_s[11] = tswap64(regs->gpr[27]);
93 
94     mcp->mc_gpregs.gp_a[0] = tswap64(regs->gpr[10]);
95     mcp->mc_gpregs.gp_a[1] = tswap64(regs->gpr[11]);
96     mcp->mc_gpregs.gp_a[2] = tswap64(regs->gpr[12]);
97     mcp->mc_gpregs.gp_a[3] = tswap64(regs->gpr[13]);
98     mcp->mc_gpregs.gp_a[4] = tswap64(regs->gpr[14]);
99     mcp->mc_gpregs.gp_a[5] = tswap64(regs->gpr[15]);
100     mcp->mc_gpregs.gp_a[6] = tswap64(regs->gpr[16]);
101     mcp->mc_gpregs.gp_a[7] = tswap64(regs->gpr[17]);
102 
103     if (flags & TARGET_MC_GET_CLEAR_RET) {
104         mcp->mc_gpregs.gp_a[0] = 0; /* a0 */
105         mcp->mc_gpregs.gp_a[1] = 0; /* a1 */
106         mcp->mc_gpregs.gp_t[0] = 0; /* clear syscall error */
107     }
108 
109     mcp->mc_gpregs.gp_ra = tswap64(regs->gpr[1]);
110     mcp->mc_gpregs.gp_sp = tswap64(regs->gpr[2]);
111     mcp->mc_gpregs.gp_gp = tswap64(regs->gpr[3]);
112     mcp->mc_gpregs.gp_tp = tswap64(regs->gpr[4]);
113     mcp->mc_gpregs.gp_sepc = tswap64(regs->pc);
114 
115     return 0;
116 }
117 
118 /* Compare with set_mcontext() in riscv/riscv/exec_machdep.c */
119 abi_long set_mcontext(CPURISCVState *regs, target_mcontext_t *mcp,
120         int srflag)
121 {
122 
123     regs->gpr[5] = tswap64(mcp->mc_gpregs.gp_t[0]);
124     regs->gpr[6] = tswap64(mcp->mc_gpregs.gp_t[1]);
125     regs->gpr[7] = tswap64(mcp->mc_gpregs.gp_t[2]);
126     regs->gpr[28] = tswap64(mcp->mc_gpregs.gp_t[3]);
127     regs->gpr[29] = tswap64(mcp->mc_gpregs.gp_t[4]);
128     regs->gpr[30] = tswap64(mcp->mc_gpregs.gp_t[5]);
129     regs->gpr[31] = tswap64(mcp->mc_gpregs.gp_t[6]);
130 
131     regs->gpr[8] = tswap64(mcp->mc_gpregs.gp_s[0]);
132     regs->gpr[9] = tswap64(mcp->mc_gpregs.gp_s[1]);
133     regs->gpr[18] = tswap64(mcp->mc_gpregs.gp_s[2]);
134     regs->gpr[19] = tswap64(mcp->mc_gpregs.gp_s[3]);
135     regs->gpr[20] = tswap64(mcp->mc_gpregs.gp_s[4]);
136     regs->gpr[21] = tswap64(mcp->mc_gpregs.gp_s[5]);
137     regs->gpr[22] = tswap64(mcp->mc_gpregs.gp_s[6]);
138     regs->gpr[23] = tswap64(mcp->mc_gpregs.gp_s[7]);
139     regs->gpr[24] = tswap64(mcp->mc_gpregs.gp_s[8]);
140     regs->gpr[25] = tswap64(mcp->mc_gpregs.gp_s[9]);
141     regs->gpr[26] = tswap64(mcp->mc_gpregs.gp_s[10]);
142     regs->gpr[27] = tswap64(mcp->mc_gpregs.gp_s[11]);
143 
144     regs->gpr[10] = tswap64(mcp->mc_gpregs.gp_a[0]);
145     regs->gpr[11] = tswap64(mcp->mc_gpregs.gp_a[1]);
146     regs->gpr[12] = tswap64(mcp->mc_gpregs.gp_a[2]);
147     regs->gpr[13] = tswap64(mcp->mc_gpregs.gp_a[3]);
148     regs->gpr[14] = tswap64(mcp->mc_gpregs.gp_a[4]);
149     regs->gpr[15] = tswap64(mcp->mc_gpregs.gp_a[5]);
150     regs->gpr[16] = tswap64(mcp->mc_gpregs.gp_a[6]);
151     regs->gpr[17] = tswap64(mcp->mc_gpregs.gp_a[7]);
152 
153 
154     regs->gpr[1] = tswap64(mcp->mc_gpregs.gp_ra);
155     regs->gpr[2] = tswap64(mcp->mc_gpregs.gp_sp);
156     regs->gpr[3] = tswap64(mcp->mc_gpregs.gp_gp);
157     regs->gpr[4] = tswap64(mcp->mc_gpregs.gp_tp);
158     regs->pc = tswap64(mcp->mc_gpregs.gp_sepc);
159 
160     return 0;
161 }
162 
163 /* Compare with sys_sigreturn() in riscv/riscv/machdep.c */
164 abi_long get_ucontext_sigreturn(CPURISCVState *regs,
165                         abi_ulong target_sf, abi_ulong *target_uc)
166 {
167 
168     *target_uc = target_sf;
169     return 0;
170 }
171