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 be
4 * found in the LICENSE file.
9 #include <sys/ucontext.h>
11 #include "native_client/src/trusted/service_runtime/nacl_signal.h"
14 * Definition of the POSIX ucontext_t for Linux can be found in:
15 * /usr/include/sys/ucontext.h
19 * Fill a signal context structure from the raw platform dependent
22 void NaClSignalContextFromHandler(struct NaClSignalContext *sig_ctx,
23 const void *raw_ctx) {
24 const ucontext_t *uctx = (const ucontext_t *) raw_ctx;
25 const mcontext_t *mctx = &uctx->uc_mcontext;
27 memset(sig_ctx, 0, sizeof(*sig_ctx));
29 sig_ctx->prog_ctr = mctx->gregs[REG_RIP];
30 sig_ctx->stack_ptr = mctx->gregs[REG_RSP];
32 sig_ctx->rax = mctx->gregs[REG_RAX];
33 sig_ctx->rbx = mctx->gregs[REG_RBX];
34 sig_ctx->rcx = mctx->gregs[REG_RCX];
35 sig_ctx->rdx = mctx->gregs[REG_RDX];
36 sig_ctx->rsi = mctx->gregs[REG_RSI];
37 sig_ctx->rdi = mctx->gregs[REG_RDI];
38 sig_ctx->rbp = mctx->gregs[REG_RBP];
39 sig_ctx->r8 = mctx->gregs[REG_R8];
40 sig_ctx->r9 = mctx->gregs[REG_R9];
41 sig_ctx->r10 = mctx->gregs[REG_R10];
42 sig_ctx->r11 = mctx->gregs[REG_R11];
43 sig_ctx->r12 = mctx->gregs[REG_R12];
44 sig_ctx->r13 = mctx->gregs[REG_R13];
45 sig_ctx->r14 = mctx->gregs[REG_R14];
46 sig_ctx->r15 = mctx->gregs[REG_R15];
47 sig_ctx->flags = mctx->gregs[REG_EFL];
49 /* Linux stores CS, GS, FS, PAD into one 64b word. */
50 sig_ctx->cs = (uint32_t) (mctx->gregs[REG_CSGSFS] & 0xFFFF);
51 sig_ctx->gs = (uint32_t) ((mctx->gregs[REG_CSGSFS] >> 16) & 0xFFFF);
52 sig_ctx->fs = (uint32_t) ((mctx->gregs[REG_CSGSFS] >> 32) & 0xFFFF);
55 * TODO(noelallen) Pull from current context, since they must be
64 * Update the raw platform dependent signal information from the
65 * signal context structure.
67 void NaClSignalContextToHandler(void *raw_ctx,
68 const struct NaClSignalContext *sig_ctx) {
69 ucontext_t *uctx = (ucontext_t *) raw_ctx;
70 mcontext_t *mctx = &uctx->uc_mcontext;
72 mctx->gregs[REG_RIP] = sig_ctx->prog_ctr;
73 mctx->gregs[REG_RSP] = sig_ctx->stack_ptr;
75 mctx->gregs[REG_RAX] = sig_ctx->rax;
76 mctx->gregs[REG_RBX] = sig_ctx->rbx;
77 mctx->gregs[REG_RCX] = sig_ctx->rcx;
78 mctx->gregs[REG_RDX] = sig_ctx->rdx;
79 mctx->gregs[REG_RSI] = sig_ctx->rsi;
80 mctx->gregs[REG_RDI] = sig_ctx->rdi;
81 mctx->gregs[REG_RBP] = sig_ctx->rbp;
82 mctx->gregs[REG_R8] = sig_ctx->r8;
83 mctx->gregs[REG_R9] = sig_ctx->r9;
84 mctx->gregs[REG_R10] = sig_ctx->r10;
85 mctx->gregs[REG_R11] = sig_ctx->r11;
86 mctx->gregs[REG_R12] = sig_ctx->r12;
87 mctx->gregs[REG_R13] = sig_ctx->r13;
88 mctx->gregs[REG_R14] = sig_ctx->r14;
89 mctx->gregs[REG_R15] = sig_ctx->r15;
90 mctx->gregs[REG_EFL] = sig_ctx->flags;
92 /* Linux stores CS, GS, FS, PAD into one 64b word. */
93 mctx->gregs[REG_CSGSFS] = ((uint64_t) (sig_ctx->cs & 0xFFFF))
94 | (((uint64_t) (sig_ctx->gs & 0xFFFF)) << 16)
95 | (((uint64_t) (sig_ctx->fs & 0xFFFF)) << 32);
98 * We do not support modification of DS & SS in 64b, so
99 * we do not push them back into the context.