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