1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* signal-dispatch-lowlevel.c - ia32 specific signal handling functions.
Copyright (C) 2008 Free Software Foundation, Inc.
Written by Neal H. Walfield <neal@gnu.org>.
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 3 of
the License, or (at your option) any later version.
The GNU Hurd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see
<http://www.gnu.org/licenses/>. */
#include <pt-internal.h>
#include <sig-internal.h>
#include <hurd/thread.h>
#include <pthread.h>
#include <stdint.h>
#include <atomic.h>
extern char _signal_dispatch_entry;
/* - 0(%esp) a pointer to the thread's struct signal_state.
- 4(%esp) a pointer to a siginfo_t.
- 8(%esp) is a pointer to the ss_flags field (or NULL).
- 12(%esp)+4 is the value of the sp when the thread was interrupted (intr_sp)
- 0(intr_sp) is the value of the ip when the thread was interrupted.
- 16(%esp) - 16 byte register save area
*/
__asm__ ("\n\
.globl _signal_dispatch_entry\n\
_signal_dispatch_entry:\n\
/* Save caller saved registers (16 bytes). */\n\
mov %eax, 16(%esp)\n\
mov %ecx, 16+4(%esp)\n\
mov %edx, 16+8(%esp)\n\
pushf\n\
popl %eax\n\
mov %eax, 16+12(%esp)\n\
\n\
/* Reset EFLAGS. */\n\
cld\n\
call signal_dispatch\n\
\n\
/* Get the original stack and begin restoration. */\n\
mov 12(%esp), %edx\n\
\n\
/* Move the saved registers to the user stack. */\n\
sub $16, %edx\n\
/* eax. */\n\
mov 16+0(%esp), %ecx\n\
mov %ecx, 0(%edx)\n\
/* ecx. */\n\
mov 16+4(%esp), %ecx\n\
mov %ecx, 4(%edx)\n\
/* edx. */\n\
mov 16+8(%esp), %ecx\n\
mov %ecx, 8(%edx)\n\
/* eflags. */\n\
mov 16+12(%esp), %ecx\n\
mov %ecx, 12(%edx)\n\
\n\
/* Get the pointer to the sigaltstack flags. */\n\
mov 8(%esp), %ecx\n\
\n\
/* Restore the user stack. */\n\
mov %edx, %esp\n\
\n\
/* Clear the SA_ONSTACK flag. */\n\
and %ecx, %ecx\n\
jz after_clear\n\
lock; and $~1, 0(%ecx)\n\
after_clear:\n\
\n\
/* Restore eflags, the scratch regs and the original sp and ip. */\n\
popl %eax\n\
popl %ecx\n\
popl %edx\n\
popf\n\
ret\n");
extern char _signal_dispatch_entry_self;
/* - 0(%esp) is the return address (we ignore it)
- 4(%esp) is the sp to load
Since we are returning to signal_dispatch_lowlevel's caller, we
also need to restore its frame pointer. */
__asm__ ("\n\
.globl _signal_dispatch_entry_self\n\
_signal_dispatch_entry_self:\n\
mov 0(%ebp), %ebp\n\
mov 4(%esp), %esp\n\
jmp _signal_dispatch_entry\n");
void
signal_dispatch_lowlevel (struct signal_state *ss, pthread_t tid,
siginfo_t si)
{
assert (pthread_mutex_trylock (&ss->lock) == EBUSY);
struct __pthread *thread = __pthread_getid (tid);
bool self = tid == pthread_self ();
uintptr_t intr_sp;
if (self)
{
/* The return address is just before the first argument. */
intr_sp = (uintptr_t) &ss - 4;
assert (* (void **) intr_sp == __builtin_return_address (0));
}
else
{
struct hurd_thread_exregs_in in;
memset (&in, 0, sizeof (in));
struct hurd_thread_exregs_out out;
error_t err;
err = rm_thread_exregs (ADDR_VOID, thread->object.addr,
HURD_EXREGS_STOP | HURD_EXREGS_ABORT_IPC
| HURD_EXREGS_GET_REGS,
in, &out);
if (err)
panic ("Failed to modify thread " ADDR_FMT,
ADDR_PRINTF (thread->object.addr));
intr_sp = out.sp;
/* Push the ip on the user stack. */
intr_sp -= 4;
* (uintptr_t *) intr_sp = out.ip;
}
bool altstack = false;
uintptr_t sp;
if (! (ss->actions[si.si_signo - 1].sa_flags & SA_ONSTACK)
|| (ss->stack.ss_flags & SS_DISABLE)
|| (ss->stack.ss_flags & SS_ONSTACK))
{
assert (! self);
sp = intr_sp;
}
else
{
/* The stack grows down. */
sp = (uintptr_t) ss->stack.ss_sp + ss->stack.ss_size;
/* We know intimately that SS_ONSTACK is the least significant
bit. */
assert (SS_ONSTACK == 1);
atomic_bit_set (&ss->stack.ss_flags, 0);
altstack = true;
}
/* Set up the call frame for a call to signal_dispatch_entry. */
/* Allocate a siginfo structure on the stack. */
sp = sp - sizeof (siginfo_t);
siginfo_t *sip = (void *) sp;
/* Copy the user supplied values. */
*sip = si;
/* Add space for the 4 caller saved registers. */
sp -= 4 * sizeof (uintptr_t);
/* Save the interrupted sp. */
sp -= 4;
* (uintptr_t *) sp = intr_sp;
/* Address of the ss_flags. */
sp -= 4;
if (altstack)
* (uintptr_t *) sp = (uintptr_t) &ss->stack.ss_flags;
else
* (uintptr_t *) sp = 0;
/* Push the parameters to signal_dispatch. */
/* signal info structure. */
sp -= 4;
* (uintptr_t *) sp = (uintptr_t) sip;
/* The ss. */
sp -= 4;
* (uintptr_t *) sp = (uintptr_t) ss;
pthread_mutex_transfer_np (&ss->lock, tid);
if (self)
((void (*) (uintptr_t)) &_signal_dispatch_entry_self) ((uintptr_t) sp);
else
{
struct hurd_thread_exregs_in in;
struct hurd_thread_exregs_out out;
in.sp = sp;
in.ip = (uintptr_t) &_signal_dispatch_entry;
rm_thread_exregs (ADDR_VOID, thread->object.addr,
HURD_EXREGS_SET_SP_IP
| HURD_EXREGS_START | HURD_EXREGS_ABORT_IPC,
in, &out);
}
}
|