imcplugin demo: Extend to support stat() call
[nativeclient.git] / service_runtime / nacl_switch_to_app.c
bloba02297b513de1af3529f377ce7aa4b71aec76d0f
1 /*
2 * Copyright 2008, Google Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * NaCl Service Runtime, C-level context switch code.
36 #include "native_client/service_runtime/sel_ldr.h"
37 #include "native_client/service_runtime/sel_rt.h"
38 #include "native_client/service_runtime/nacl_globals.h"
39 #include "native_client/service_runtime/nacl_switch_to_app.h"
41 NORETURN void NaClStartThreadInApp(struct NaClAppThread *natp,
42 uint32_t new_eip)
44 struct NaClApp *nap;
46 * Save service runtime segment registers; fs/gs is used for TLS
47 * on Windows and Linux respectively, so will change. The others
48 * should be global, but we save them from the thread anyway.
50 #if 0 /* restored by trampoline code */
51 natp->sys.cs = NaClGetCs();
52 natp->sys.ds = NaClGetDs();
53 #endif
54 natp->sys.es = NaClGetEs();
55 natp->sys.fs = NaClGetFs();
56 natp->sys.gs = NaClGetGs();
57 natp->sys.ss = NaClGetSs();
59 * Preserves stack alignment. The trampoline code loads this value
60 * to %esp, then pushes the thread ID (LDT index) onto the stack as
61 * argument to NaClSyscallCSegHook. See nacl_syscall.S.
63 natp->sys.esp = (NaClGetEsp() & ~0xf) + 4;
65 nap = natp->nap;
67 NaClSwitch(
68 new_eip,
69 natp->user.ebp,
70 natp->user.edi,
71 natp->user.esi,
72 natp->user.ebx,
73 (uint32_t) natp->user.gs,
74 (uint32_t) natp->user.fs,
75 (uint32_t) natp->user.es,
76 nap->springboard_addr,
77 (uint32_t) natp->user.cs,
78 /* rest popped by NaCl_springboard */
79 (uint32_t) natp->user.ds,
80 0, /* %eax not used to return */
81 natp->user.esp,
82 (uint32_t) natp->user.ss);
86 * syscall return
88 NORETURN void NaClSwitchToApp(struct NaClAppThread *natp,
89 uint32_t new_eip)
91 struct NaClApp *nap;
93 nap = natp->nap;
94 NaClSwitch(
95 new_eip,
96 natp->user.ebp,
97 natp->user.edi,
98 natp->user.esi,
99 natp->user.ebx,
100 (uint32_t) natp->user.gs,
101 (uint32_t) natp->user.fs,
102 (uint32_t) natp->user.es,
103 nap->springboard_addr,
104 (uint32_t) natp->user.cs,
105 /* rest popped by NaCl_springboard */
106 (uint32_t) natp->user.ds,
107 natp->sysret,
108 natp->user.esp,
109 (uint32_t) natp->user.ss);