1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include "qemu.h"
21#include "signal-common.h"
22#include "linux-user/trace.h"
23
24struct target_sigcontext {
25 union {
26
27 abi_ulong gregs[56];
28 struct {
29 abi_ulong __gregs[53];
30 abi_ulong tp;
31 abi_ulong sp;
32 abi_ulong lr;
33 };
34 };
35 abi_ulong pc;
36 abi_ulong ics;
37 abi_ulong faultnum;
38 abi_ulong pad[5];
39};
40
41struct target_ucontext {
42 abi_ulong tuc_flags;
43 abi_ulong tuc_link;
44 target_stack_t tuc_stack;
45 struct target_sigcontext tuc_mcontext;
46 target_sigset_t tuc_sigmask;
47};
48
49struct target_rt_sigframe {
50 unsigned char save_area[16];
51 struct target_siginfo info;
52 struct target_ucontext uc;
53 abi_ulong retcode[2];
54};
55
56#define INSN_MOVELI_R10_139 0x00045fe551483000ULL
57#define INSN_SWINT1 0x286b180051485000ULL
58
59
60static void setup_sigcontext(struct target_sigcontext *sc,
61 CPUArchState *env, int signo)
62{
63 int i;
64
65 for (i = 0; i < TILEGX_R_COUNT; ++i) {
66 __put_user(env->regs[i], &sc->gregs[i]);
67 }
68
69 __put_user(env->pc, &sc->pc);
70 __put_user(0, &sc->ics);
71 __put_user(signo, &sc->faultnum);
72}
73
74static void restore_sigcontext(CPUTLGState *env, struct target_sigcontext *sc)
75{
76 int i;
77
78 for (i = 0; i < TILEGX_R_COUNT; ++i) {
79 __get_user(env->regs[i], &sc->gregs[i]);
80 }
81
82 __get_user(env->pc, &sc->pc);
83}
84
85static abi_ulong get_sigframe(struct target_sigaction *ka, CPUArchState *env,
86 size_t frame_size)
87{
88 unsigned long sp = get_sp_from_cpustate(env);
89
90 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - frame_size))) {
91 return -1UL;
92 }
93
94 sp = target_sigsp(sp, ka) - frame_size;
95 sp &= -16UL;
96 return sp;
97}
98
99void setup_rt_frame(int sig, struct target_sigaction *ka,
100 target_siginfo_t *info,
101 target_sigset_t *set, CPUArchState *env)
102{
103 abi_ulong frame_addr;
104 struct target_rt_sigframe *frame;
105 unsigned long restorer;
106
107 frame_addr = get_sigframe(ka, env, sizeof(*frame));
108 trace_user_setup_rt_frame(env, frame_addr);
109 if (!lock_user_struct(VERIFY_WRITE, frame, frame_addr, 0)) {
110 goto give_sigsegv;
111 }
112
113
114 if (ka->sa_flags & TARGET_SA_SIGINFO) {
115
116 tswap_siginfo(&frame->info, info);
117
118 } else {
119 __put_user(info->si_signo, &frame->info.si_signo);
120 }
121
122
123 __put_user(0, &frame->uc.tuc_flags);
124 __put_user(0, &frame->uc.tuc_link);
125 target_save_altstack(&frame->uc.tuc_stack, env);
126 setup_sigcontext(&frame->uc.tuc_mcontext, env, info->si_signo);
127
128 if (ka->sa_flags & TARGET_SA_RESTORER) {
129 restorer = (unsigned long) ka->sa_restorer;
130 } else {
131 __put_user(INSN_MOVELI_R10_139, &frame->retcode[0]);
132 __put_user(INSN_SWINT1, &frame->retcode[1]);
133 restorer = frame_addr + offsetof(struct target_rt_sigframe, retcode);
134 }
135 env->pc = (unsigned long) ka->_sa_handler;
136 env->regs[TILEGX_R_SP] = (unsigned long) frame;
137 env->regs[TILEGX_R_LR] = restorer;
138 env->regs[0] = (unsigned long) sig;
139 env->regs[1] = (unsigned long) &frame->info;
140 env->regs[2] = (unsigned long) &frame->uc;
141
142
143 unlock_user_struct(frame, frame_addr, 1);
144 return;
145
146give_sigsegv:
147 force_sigsegv(sig);
148}
149
150long do_rt_sigreturn(CPUTLGState *env)
151{
152 abi_ulong frame_addr = env->regs[TILEGX_R_SP];
153 struct target_rt_sigframe *frame;
154 sigset_t set;
155
156 trace_user_do_rt_sigreturn(env, frame_addr);
157 if (!lock_user_struct(VERIFY_READ, frame, frame_addr, 1)) {
158 goto badframe;
159 }
160 target_to_host_sigset(&set, &frame->uc.tuc_sigmask);
161 set_sigmask(&set);
162
163 restore_sigcontext(env, &frame->uc.tuc_mcontext);
164 if (do_sigaltstack(frame_addr + offsetof(struct target_rt_sigframe,
165 uc.tuc_stack),
166 0, env->regs[TILEGX_R_SP]) == -EFAULT) {
167 goto badframe;
168 }
169
170 unlock_user_struct(frame, frame_addr, 0);
171 return -TARGET_QEMU_ESIGRETURN;
172
173
174 badframe:
175 unlock_user_struct(frame, frame_addr, 0);
176 force_sig(TARGET_SIGSEGV);
177 return -TARGET_QEMU_ESIGRETURN;
178}
179