1 : /*
2 : * Copyright 2010 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can
4 : * be found in the LICENSE file.
5 : */
6 :
7 : #include <signal.h>
8 : #if !NACL_ANDROID
9 : #include <sys/ucontext.h>
10 : #endif
11 :
12 : #include "native_client/src/trusted/service_runtime/linux/android_compat.h"
13 : #include "native_client/src/trusted/service_runtime/nacl_signal.h"
14 :
15 : /*
16 : * Definition of the POSIX ucontext_t for Linux can be found in:
17 : * /usr/include/sys/ucontext.h
18 : */
19 :
20 : /*
21 : * Fill a signal context structure from the raw platform dependent
22 : * signal information.
23 : */
24 140375 : void NaClSignalContextFromHandler(struct NaClSignalContext *sig_ctx,
25 : const void *raw_ctx) {
26 140375 : const ucontext_t *uctx = (const ucontext_t *) raw_ctx;
27 140375 : const mcontext_t *mctx = &uctx->uc_mcontext;
28 :
29 140375 : sig_ctx->prog_ctr = mctx->gregs[REG_EIP];
30 140375 : sig_ctx->stack_ptr = mctx->gregs[REG_ESP];
31 :
32 140375 : sig_ctx->eax = mctx->gregs[REG_EAX];
33 140375 : sig_ctx->ebx = mctx->gregs[REG_EBX];
34 140375 : sig_ctx->ecx = mctx->gregs[REG_ECX];
35 140375 : sig_ctx->edx = mctx->gregs[REG_EDX];
36 140375 : sig_ctx->esi = mctx->gregs[REG_ESI];
37 140375 : sig_ctx->edi = mctx->gregs[REG_EDI];
38 140375 : sig_ctx->ebp = mctx->gregs[REG_EBP];
39 140375 : sig_ctx->flags = mctx->gregs[REG_EFL];
40 : /*
41 : * We need to drop the top 16 bits with the casts below. In some
42 : * situations, Linux does not assign to the top 2 bytes of the
43 : * REG_CS array entry when writing %cs to the stack (and similarly
44 : * for the other segment registers). Therefore we need to drop the
45 : * undefined top 2 bytes.
46 : *
47 : * This happens in 32-bit processes running on the 64-bit kernel
48 : * from Ubuntu Hardy, but not on 32-bit kernels. The kernel
49 : * version in Ubuntu Lucid also does not have this problem.
50 : *
51 : * See http://code.google.com/p/nativeclient/issues/detail?id=1486
52 : */
53 140375 : sig_ctx->cs = (uint16_t) mctx->gregs[REG_CS];
54 140375 : sig_ctx->ss = (uint16_t) mctx->gregs[REG_SS];
55 140375 : sig_ctx->ds = (uint16_t) mctx->gregs[REG_DS];
56 140375 : sig_ctx->es = (uint16_t) mctx->gregs[REG_ES];
57 140375 : sig_ctx->fs = (uint16_t) mctx->gregs[REG_FS];
58 140375 : sig_ctx->gs = (uint16_t) mctx->gregs[REG_GS];
59 140375 : }
60 :
61 :
62 : /*
63 : * Update the raw platform dependent signal information from the
64 : * signal context structure.
65 : */
66 125943 : void NaClSignalContextToHandler(void *raw_ctx,
67 : const struct NaClSignalContext *sig_ctx) {
68 125943 : ucontext_t *uctx = (ucontext_t *) raw_ctx;
69 125943 : mcontext_t *mctx = &uctx->uc_mcontext;
70 :
71 125943 : mctx->gregs[REG_EIP] = sig_ctx->prog_ctr;
72 125943 : mctx->gregs[REG_ESP] = sig_ctx->stack_ptr;
73 :
74 125943 : mctx->gregs[REG_EAX] = sig_ctx->eax;
75 125943 : mctx->gregs[REG_EBX] = sig_ctx->ebx;
76 125943 : mctx->gregs[REG_ECX] = sig_ctx->ecx;
77 125943 : mctx->gregs[REG_EDX] = sig_ctx->edx;
78 125943 : mctx->gregs[REG_ESI] = sig_ctx->esi;
79 125943 : mctx->gregs[REG_EDI] = sig_ctx->edi;
80 125943 : mctx->gregs[REG_EBP] = sig_ctx->ebp;
81 125943 : mctx->gregs[REG_EFL] = sig_ctx->flags;
82 125943 : mctx->gregs[REG_CS] = sig_ctx->cs;
83 125943 : mctx->gregs[REG_SS] = sig_ctx->ss;
84 125943 : mctx->gregs[REG_DS] = sig_ctx->ds;
85 125943 : mctx->gregs[REG_ES] = sig_ctx->es;
86 125943 : mctx->gregs[REG_FS] = sig_ctx->fs;
87 125943 : mctx->gregs[REG_GS] = sig_ctx->gs;
88 125943 : }
89 :
90 :
91 :
|