2 * Copyright (c) 2011 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.
7 #include "native_client/src/trusted/service_runtime/arch/arm/sel_ldr_arm.h"
8 #include "native_client/src/trusted/service_runtime/nacl_config.h"
11 * This is code, but it is not code that is actually part of the
12 * program/library being linked. Marking it as read-only data
13 * instead ensures that nothing like linker code-rewriting will
14 * be applied to this code.
19 * Assembly code template.
20 * This is linked into the service runtime but is unused as code -- it is used
21 * as data to be patched into a NaCl app's address space.
23 * Trampoline to transfer control from native client module to
24 * sel_ldr's NaClSyscallSeg residing in the service runtime portion of address
25 * space. Trampolines are patched into nacl module's address space in the
26 * trampoline region. They are patched by NaClLoadTrampoline() code (sel_ldr.c).
27 * Each trampoline code segment corresponds to a system call, so the trampoline
28 * region is full of identical trampoline code segments. Service runtime
29 * distinguish which system call is requested using the address of an executed
30 * trampoline (it is saved on stack in NaClSyscallSeg()).
32 * ARM passes parameters to a callee in registers r0-r3. If there are more
33 * than 4 parameters, the first four are passed in registers and the rest are
34 * placed on the stack. This code saves all parameters from registers into the
35 * stack; thus, we keep all parameters on the stack as follows:
36 * top - arg0, arg1, arg2, arg3 .... argN
38 * On top of that we save the return address, so we will know where to return
39 * after the system call.
41 * At the point this code calls NaClSyscallSeg, the stack layout is as follows:
43 * sp+0x04: argument 5 (if present)
44 * sp: argument 4 (if present)
49 * sp-0x14: return address to untrusted code
50 * (sp-0x18: return address to trampoline - saved later by NaClSyscallSeg)
52 * Usually, signal-safe code will not save data below the stack
53 * pointer, but it is safe to do so here because the trusted signal
54 * handler will never run on the untrusted stack.
56 * When service runtime serves a system call, it first creates a structure which
57 * utilizes these arguments. The structure is created by Decoder functions in
58 * nacl_syscall_handlers.c. (nacl_syscall_handlers.c is an automatically
59 * generated file and placed in
60 * scons-out//gen/native_client/src/trusted/service_runtime).
63 DEFINE_GLOBAL_HIDDEN_LOCATION(NaCl_trampoline_seg_code):
65 * Save first 4 syscall arguments below the stack pointer. Any
66 * further arguments are already on the stack.
68 stmfd sp, {r0, r1, r2, r3}
69 /* Save the return address. */
72 /* this accesses the data item immediately after the halt */
73 ldr r0, NaCl_trampoline_syscall_seg_addr
74 /* NOTE: we use the blx instead of bx because the return addres */
75 /* is used to determine which trampoline was used */
81 DEFINE_GLOBAL_HIDDEN_LOCATION(NaCl_trampoline_syscall_seg_addr):
82 /* a DATA item will be patched in here */
85 DEFINE_GLOBAL_HIDDEN_LOCATION(NaCl_trampoline_seg_end):