~ubuntu-branches/ubuntu/utopic/eglibc/utopic

« back to all changes in this revision

Viewing changes to .pc/hurd-i386/tg-tls.diff/sysdeps/mach/hurd/i386/trampoline.c

  • Committer: Package Import Robot
  • Author(s): Adam Conrad
  • Date: 2012-10-26 05:14:58 UTC
  • mfrom: (1.5.1) (4.4.22 experimental)
  • Revision ID: package-import@ubuntu.com-20121026051458-oryotr4i03ob5pab
Tags: 2.16-0ubuntu1
* Merge with unreleased 2.16 in Debian experimental, remaining changes:
  - Drop the Breaks line from libc6, which refers to a Debian transition
  - Remove the libc6 recommends on libc6-i686, which we don't build
  - Enable libc6{,-dev}-armel on armhf and libc6{-dev}-armhf on armel
  - Ship update-locale and validlocale in /usr/sbin in libc-bin
  - Don't build locales or locales-all in Ubuntu, we rely on langpacks
  - Heavily mangle the way we do service restarting on major upgrades
  - Use different MIN_KERNEL_SUPPORTED versions than Debian, due to
    buildd needs.  This should be universally bumped to 3.2.0 once all
    our buildds (including the PPA guests) are running precise kernels
  - Build i386 variants as -march=i686, build amd64 with -O3, and build
    ppc64 variants (both 64-bit and 32-bit) with -O3 -fno-tree-vectorize
  - Re-enable unsubmitted-ldconfig-cache-abi.diff and rebuild the cache
    on upgrades from previous versions that used a different constant
  - debian/patches/any/local-CVE-2012-3406.diff: switch to malloc when
    array grows too large to handle via alloca extension (CVE-2012-3406)
  - Build generic i386/i686 flavour with -mno-tls-direct-seg-refs
* Changes added/dropped with this merge while reducing our delta:
  - Stop building glibc docs from the eglibc source, and instead make
    the glibc-docs stub have a hard dependency on glibc-doc-reference
  - Remove outdated conflicts against ancient versions of ia32-libs
  - Drop the tzdata dependency from libc6, it's in required and minimal
  - Use gcc-4.7/g++-4.7 by default on all our supported architectures
  - Save our historical changelog as changelog.ubuntu in the source
  - Drop nscd's libaudit build-dep for now, as libaudit is in universe
  - Drop the unnecessary Breaks from libc6 to locales and locales-all
  - Ship xen's ld.so.conf.d snippet as /etc/ld.so.conf.d/libc6-xen.conf
* Disable hard failures on the test suite for the first upload to raring

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Set thread_state for sighandler, and sigcontext to recover.  i386 version.
 
2
   Copyright (C) 1994,1995,1996,1997,1998,1999,2005,2008
 
3
        Free Software Foundation, Inc.
 
4
   This file is part of the GNU C Library.
 
5
 
 
6
   The GNU C Library is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU Lesser General Public
 
8
   License as published by the Free Software Foundation; either
 
9
   version 2.1 of the License, or (at your option) any later version.
 
10
 
 
11
   The GNU C Library 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 GNU
 
14
   Lesser General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU Lesser General Public
 
17
   License along with the GNU C Library; if not, see
 
18
   <http://www.gnu.org/licenses/>.  */
 
19
 
 
20
#include <hurd/signal.h>
 
21
#include <hurd/userlink.h>
 
22
#include <thread_state.h>
 
23
#include <mach/machine/eflags.h>
 
24
#include <assert.h>
 
25
#include <errno.h>
 
26
#include "hurdfault.h"
 
27
#include <intr-msg.h>
 
28
 
 
29
 
 
30
struct sigcontext *
 
31
_hurd_setup_sighandler (struct hurd_sigstate *ss, __sighandler_t handler,
 
32
                        int signo, struct hurd_signal_detail *detail,
 
33
                        volatile int rpc_wait,
 
34
                        struct machine_thread_all_state *state)
 
35
{
 
36
  void trampoline (void);
 
37
  void rpc_wait_trampoline (void);
 
38
  void firewall (void);
 
39
  extern const void _hurd_intr_rpc_msg_in_trap;
 
40
  extern const void _hurd_intr_rpc_msg_cx_sp;
 
41
  extern const void _hurd_intr_rpc_msg_sp_restored;
 
42
  void *volatile sigsp;
 
43
  struct sigcontext *scp;
 
44
  struct
 
45
    {
 
46
      int signo;
 
47
      long int sigcode;
 
48
      struct sigcontext *scp;   /* Points to ctx, below.  */
 
49
      void *sigreturn_addr;
 
50
      void *sigreturn_returns_here;
 
51
      struct sigcontext *return_scp; /* Same; arg to sigreturn.  */
 
52
      struct sigcontext ctx;
 
53
      struct hurd_userlink link;
 
54
    } *stackframe;
 
55
 
 
56
  if (ss->context)
 
57
    {
 
58
      /* We have a previous sigcontext that sigreturn was about
 
59
         to restore when another signal arrived.  We will just base
 
60
         our setup on that.  */
 
61
      if (! _hurdsig_catch_memory_fault (ss->context))
 
62
        {
 
63
          memcpy (&state->basic, &ss->context->sc_i386_thread_state,
 
64
                  sizeof (state->basic));
 
65
          memcpy (&state->fpu, &ss->context->sc_i386_float_state,
 
66
                  sizeof (state->fpu));
 
67
          state->set |= (1 << i386_THREAD_STATE) | (1 << i386_FLOAT_STATE);
 
68
        }
 
69
    }
 
70
 
 
71
  if (! machine_get_basic_state (ss->thread, state))
 
72
    return NULL;
 
73
 
 
74
  /* Save the original SP in the gratuitous `esp' slot.
 
75
     We may need to reset the SP (the `uesp' slot) to avoid clobbering an
 
76
     interrupted RPC frame.  */
 
77
  state->basic.esp = state->basic.uesp;
 
78
 
 
79
  if ((ss->actions[signo].sa_flags & SA_ONSTACK) &&
 
80
      !(ss->sigaltstack.ss_flags & (SS_DISABLE|SS_ONSTACK)))
 
81
    {
 
82
      sigsp = ss->sigaltstack.ss_sp + ss->sigaltstack.ss_size;
 
83
      ss->sigaltstack.ss_flags |= SS_ONSTACK;
 
84
      /* XXX need to set up base of new stack for
 
85
         per-thread variables, cthreads.  */
 
86
    }
 
87
  /* This code has intimate knowledge of the special mach_msg system call
 
88
     done in intr-msg.c; that code does (see intr-msg.h):
 
89
                                        movl %esp, %ecx
 
90
                                        leal ARGS, %esp
 
91
        _hurd_intr_rpc_msg_cx_sp:       movl $-25, %eax
 
92
        _hurd_intr_rpc_msg_do_trap:     lcall $7, $0
 
93
        _hurd_intr_rpc_msg_in_trap:     movl %ecx, %esp
 
94
        _hurd_intr_rpc_msg_sp_restored:
 
95
     We must check for the window during which %esp points at the
 
96
     mach_msg arguments.  The space below until %ecx is used by
 
97
     the _hurd_intr_rpc_mach_msg frame, and must not be clobbered.  */
 
98
  else if (state->basic.eip >= (int) &_hurd_intr_rpc_msg_cx_sp &&
 
99
           state->basic.eip < (int) &_hurd_intr_rpc_msg_sp_restored)
 
100
    /* The SP now points at the mach_msg args, but there is more stack
 
101
       space used below it.  The real SP is saved in %ecx; we must push the
 
102
       new frame below there, and restore that value as the SP on
 
103
       sigreturn.  */
 
104
    sigsp = (char *) (state->basic.uesp = state->basic.ecx);
 
105
  else
 
106
    sigsp = (char *) state->basic.uesp;
 
107
 
 
108
  /* Push the arguments to call `trampoline' on the stack.  */
 
109
  sigsp -= sizeof (*stackframe);
 
110
  stackframe = sigsp;
 
111
 
 
112
  if (_hurdsig_catch_memory_fault (stackframe))
 
113
    {
 
114
      /* We got a fault trying to write the stack frame.
 
115
         We cannot set up the signal handler.
 
116
         Returning NULL tells our caller, who will nuke us with a SIGILL.  */
 
117
      return NULL;
 
118
    }
 
119
  else
 
120
    {
 
121
      int ok;
 
122
 
 
123
      extern void _hurdsig_longjmp_from_handler (void *, jmp_buf, int);
 
124
 
 
125
      /* Add a link to the thread's active-resources list.  We mark this as
 
126
         the only user of the "resource", so the cleanup function will be
 
127
         called by any longjmp which is unwinding past the signal frame.
 
128
         The cleanup function (in sigunwind.c) will make sure that all the
 
129
         appropriate cleanups done by sigreturn are taken care of.  */
 
130
      stackframe->link.cleanup = &_hurdsig_longjmp_from_handler;
 
131
      stackframe->link.cleanup_data = &stackframe->ctx;
 
132
      stackframe->link.resource.next = NULL;
 
133
      stackframe->link.resource.prevp = NULL;
 
134
      stackframe->link.thread.next = ss->active_resources;
 
135
      stackframe->link.thread.prevp = &ss->active_resources;
 
136
      if (stackframe->link.thread.next)
 
137
        stackframe->link.thread.next->thread.prevp
 
138
          = &stackframe->link.thread.next;
 
139
      ss->active_resources = &stackframe->link;
 
140
 
 
141
      /* Set up the arguments for the signal handler.  */
 
142
      stackframe->signo = signo;
 
143
      stackframe->sigcode = detail->code;
 
144
      stackframe->scp = stackframe->return_scp = scp = &stackframe->ctx;
 
145
      stackframe->sigreturn_addr = &__sigreturn;
 
146
      stackframe->sigreturn_returns_here = firewall; /* Crash on return.  */
 
147
 
 
148
      /* Set up the sigcontext from the current state of the thread.  */
 
149
 
 
150
      scp->sc_onstack = ss->sigaltstack.ss_flags & SS_ONSTACK ? 1 : 0;
 
151
 
 
152
      /* struct sigcontext is laid out so that starting at sc_gs mimics a
 
153
         struct i386_thread_state.  */
 
154
      memcpy (&scp->sc_i386_thread_state,
 
155
              &state->basic, sizeof (state->basic));
 
156
 
 
157
      /* struct sigcontext is laid out so that starting at sc_fpkind mimics
 
158
         a struct i386_float_state.  */
 
159
      ok = machine_get_state (ss->thread, state, i386_FLOAT_STATE,
 
160
                              &state->fpu, &scp->sc_i386_float_state,
 
161
                              sizeof (state->fpu));
 
162
 
 
163
      _hurdsig_end_catch_fault ();
 
164
 
 
165
      if (! ok)
 
166
        return NULL;
 
167
    }
 
168
 
 
169
  /* Modify the thread state to call the trampoline code on the new stack.  */
 
170
  if (rpc_wait)
 
171
    {
 
172
      /* The signalee thread was blocked in a mach_msg_trap system call,
 
173
         still waiting for a reply.  We will have it run the special
 
174
         trampoline code which retries the message receive before running
 
175
         the signal handler.
 
176
 
 
177
         To do this we change the OPTION argument on its stack to enable only
 
178
         message reception, since the request message has already been
 
179
         sent.  */
 
180
 
 
181
      struct mach_msg_trap_args *args = (void *) state->basic.esp;
 
182
 
 
183
      if (_hurdsig_catch_memory_fault (args))
 
184
        {
 
185
          /* Faulted accessing ARGS.  Bomb.  */
 
186
          return NULL;
 
187
        }
 
188
 
 
189
      assert (args->option & MACH_RCV_MSG);
 
190
      /* Disable the message-send, since it has already completed.  The
 
191
         calls we retry need only wait to receive the reply message.  */
 
192
      args->option &= ~MACH_SEND_MSG;
 
193
 
 
194
      /* Limit the time to receive the reply message, in case the server
 
195
         claimed that `interrupt_operation' succeeded but in fact the RPC
 
196
         is hung.  */
 
197
      args->option |= MACH_RCV_TIMEOUT;
 
198
      args->timeout = _hurd_interrupted_rpc_timeout;
 
199
 
 
200
      _hurdsig_end_catch_fault ();
 
201
 
 
202
      state->basic.eip = (int) rpc_wait_trampoline;
 
203
      /* The reply-receiving trampoline code runs initially on the original
 
204
         user stack.  We pass it the signal stack pointer in %ebx.  */
 
205
      state->basic.uesp = state->basic.esp; /* Restore mach_msg syscall SP.  */
 
206
      state->basic.ebx = (int) sigsp;
 
207
      /* After doing the message receive, the trampoline code will need to
 
208
         update the %eax value to be restored by sigreturn.  To simplify
 
209
         the assembly code, we pass the address of its slot in SCP to the
 
210
         trampoline code in %ecx.  */
 
211
      state->basic.ecx = (int) &scp->sc_eax;
 
212
    }
 
213
  else
 
214
    {
 
215
      state->basic.eip = (int) trampoline;
 
216
      state->basic.uesp = (int) sigsp;
 
217
    }
 
218
  /* We pass the handler function to the trampoline code in %edx.  */
 
219
  state->basic.edx = (int) handler;
 
220
 
 
221
  /* The x86 ABI says the DF bit is clear on entry to any function.  */
 
222
  state->basic.efl &= ~EFL_DF;
 
223
 
 
224
  return scp;
 
225
}
 
226
 
 
227
/* The trampoline code follows.  This used to be located inside
 
228
   _hurd_setup_sighandler, but was optimized away by gcc 2.95.  */
 
229
 
 
230
asm ("rpc_wait_trampoline:\n");
 
231
  /* This is the entry point when we have an RPC reply message to receive
 
232
     before running the handler.  The MACH_MSG_SEND bit has already been
 
233
     cleared in the OPTION argument on our stack.  The interrupted user
 
234
     stack pointer has not been changed, so the system call can find its
 
235
     arguments; the signal stack pointer is in %ebx.  For our convenience,
 
236
     %ecx points to the sc_eax member of the sigcontext.  */
 
237
asm (/* Retry the interrupted mach_msg system call.  */
 
238
     "movl $-25, %eax\n"        /* mach_msg_trap */
 
239
     "lcall $7, $0\n"
 
240
     /* When the sigcontext was saved, %eax was MACH_RCV_INTERRUPTED.  But
 
241
        now the message receive has completed and the original caller of
 
242
        the RPC (i.e. the code running when the signal arrived) needs to
 
243
        see the final return value of the message receive in %eax.  So
 
244
        store the new %eax value into the sc_eax member of the sigcontext
 
245
        (whose address is in %ecx to make this code simpler).  */
 
246
     "movl %eax, (%ecx)\n"
 
247
     /* Switch to the signal stack.  */
 
248
     "movl %ebx, %esp\n");
 
249
 
 
250
 asm ("trampoline:\n");
 
251
  /* Entry point for running the handler normally.  The arguments to the
 
252
     handler function are already on the top of the stack:
 
253
 
 
254
       0(%esp)  SIGNO
 
255
       4(%esp)  SIGCODE
 
256
       8(%esp)  SCP
 
257
     */
 
258
asm ("call *%edx\n"             /* Call the handler function.  */
 
259
     "addl $12, %esp\n"         /* Pop its args.  */
 
260
     /* The word at the top of stack is &__sigreturn; following are a dummy
 
261
        word to fill the slot for the address for __sigreturn to return to,
 
262
        and a copy of SCP for __sigreturn's argument.  "Return" to calling
 
263
        __sigreturn (SCP); this call never returns.  */
 
264
     "ret");
 
265
 
 
266
asm ("firewall:\n"
 
267
     "hlt");