blob: 9b1923202394e69a75ebac07a86a9f3642b19c75 [file] [log] [blame]
Serban Constantinescue2104882013-09-26 11:37:10 +01001/*
Dan Albert6a918872014-08-05 20:53:31 +00002 * Copyright (C) 2013 The Android Open Source Project
Elliott Hughes54a74942014-01-03 16:40:37 -08003 * All rights reserved.
Serban Constantinescue2104882013-09-26 11:37:10 +01004 *
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
Christopher Ferrisc5d3a432019-09-25 17:50:36 -070029#include <platform/bionic/tls_defines.h>
Dan Albert6a918872014-08-05 20:53:31 +000030#include <private/bionic_asm.h>
Peter Collingbourneb6a592b2023-03-24 18:38:05 -070031#include <private/bionic_asm_offsets.h>
Dan Albert6a918872014-08-05 20:53:31 +000032#include <asm/signal.h>
Elliott Hughesfaac8e62022-10-14 21:36:58 +000033#include <linux/sched.h>
Serban Constantinescue2104882013-09-26 11:37:10 +010034
Dan Albert6a918872014-08-05 20:53:31 +000035ENTRY(vfork)
dimitryfa432522017-10-25 13:07:45 +020036__BIONIC_WEAK_ASM_FOR_NATIVE_BRIDGE(vfork)
Josh Gao23032832020-05-07 17:02:19 -070037 // x9 = __get_tls()[TLS_SLOT_THREAD_ID]
38 mrs x9, tpidr_el0
39 ldr x9, [x9, #(TLS_SLOT_THREAD_ID * 8)]
40
41 // Set cached_pid_ to 0, vforked_ to 1, and stash the previous value.
42 mov w0, #0x80000000
43 ldr w10, [x9, #20]
44 str w0, [x9, #20]
Elliott Hughes5891abd2015-08-07 18:27:47 -070045
Peter Collingbourneb6a592b2023-03-24 18:38:05 -070046 mov x0, #SIGCHLD
Evgenii Stepanov3031a7e2022-05-12 15:50:47 -070047
Peter Collingbourneb6a592b2023-03-24 18:38:05 -070048 // If either HWASan or stack MTE is enabled, set up the clone() flags to
49 // make vfork() act like fork(). We don't call the atfork handlers, so we
50 // may deadlock if the child allocates, but we have seen badly written
51 // atfork handlers themselves cause deadlocks [1]. ndk_translation already
52 // implements vfork() as fork() without calling handlers, so we have some
53 // evidence that it isn't necessary to call them.
54 //
55 // POSIX.1 defines vfork() to have the same effect as fork() except that
56 // most behavior, including heap allocation, becomes undefined in the child,
57 // so we aren't violating POSIX by doing this.
58 //
59 // [1] https://cs.android.com/android/platform/superproject/+/master:system/extras/simpleperf/app_api/cpp/simpleperf.cpp;drc=788fa4183441f4977ddbd5a055e42a7fe7691d21;l=308
60#if !__has_feature(hwaddress_sanitizer)
61 // if (!__libc_globals->memtag_stack) x0 |= CLONE_VM | CLONE_VFORK;
62 adrp x1, __libc_globals + OFFSETOF_libc_globals_memtag_stack
63 ldrb w1, [x1, :lo12:__libc_globals + OFFSETOF_libc_globals_memtag_stack]
64 cbnz w1, 1f
65 orr x0, x0, #CLONE_VM
66 orr x0, x0, #CLONE_VFORK
671:
68#endif
Dan Albert6a918872014-08-05 20:53:31 +000069 mov x1, xzr
70 mov x2, xzr
71 mov x3, xzr
72 mov x4, xzr
73
74 mov x8, __NR_clone
75 svc #0
76
Josh Gao23032832020-05-07 17:02:19 -070077 cbz x0, .L_exit
78
79 // rc != 0: reset cached_pid_ and vforked_.
80 str w10, [x9, #20]
Dan Albert6a918872014-08-05 20:53:31 +000081 cmn x0, #(MAX_ERRNO + 1)
82 cneg x0, x0, hi
Elliott Hughes011e1112014-09-08 15:25:01 -070083 b.hi __set_errno_internal
Dan Albert6a918872014-08-05 20:53:31 +000084
Evgenii Stepanov505168e2019-02-28 18:44:56 -080085.L_exit:
Dan Albert6a918872014-08-05 20:53:31 +000086 ret
87END(vfork)
Tamas Petzf5bdee72020-08-31 15:09:40 +020088
89NOTE_GNU_PROPERTY()