1/*
2 * Copyright (C) 2013 The Android Open Source Project
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
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <platform/bionic/tls_defines.h>
30#include <private/bionic_asm.h>
31#include <asm/signal.h>
32
33// Must match the defines in linux/sched.h
34#define CLONE_VM 0x00000100
35#define CLONE_VFORK 0x00004000
36
37ENTRY(vfork)
38__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(vfork)
39    // x9 = __get_tls()[TLS_SLOT_THREAD_ID]
40    mrs     x9, tpidr_el0
41    ldr     x9, [x9, #(TLS_SLOT_THREAD_ID * 8)]
42
43    // Set cached_pid_ to 0, vforked_ to 1, and stash the previous value.
44    mov     w0, #0x80000000
45    ldr     w10, [x9, #20]
46    str     w0, [x9, #20]
47
48    mov     x0, #(CLONE_VM | CLONE_VFORK | SIGCHLD)
49    mov     x1, xzr
50    mov     x2, xzr
51    mov     x3, xzr
52    mov     x4, xzr
53
54    mov     x8, __NR_clone
55    svc     #0
56
57    cbz     x0, .L_exit
58
59    // rc != 0: reset cached_pid_ and vforked_.
60    str     w10, [x9, #20]
61    cmn     x0, #(MAX_ERRNO + 1)
62    cneg    x0, x0, hi
63    b.hi    __set_errno_internal
64
65#if __has_feature(hwaddress_sanitizer)
66    cbz x0, .L_exit
67
68    // Clean up stack shadow in the parent process.
69    // https://github.com/google/sanitizers/issues/925
70    stp x0, x30, [sp, #-16]!
71    .cfi_adjust_cfa_offset 16
72    .cfi_rel_offset x0, 0
73    .cfi_rel_offset x30, 8
74
75    add x0, sp, #16
76    bl __hwasan_handle_vfork
77
78    ldp x0, x30, [sp], #16
79    .cfi_adjust_cfa_offset -16
80    .cfi_restore x0
81    .cfi_restore x30
82
83#endif
84
85.L_exit:
86    ret
87END(vfork)
88