1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2020 Linaro Limited. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Viresh Kumar <viresh.kumar@linaro.org>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci#ifndef LAPI_CLONE_H__
8f08c3bdfSopenharmony_ci#define LAPI_CLONE_H__
9f08c3bdfSopenharmony_ci
10f08c3bdfSopenharmony_ci#include <sys/syscall.h>
11f08c3bdfSopenharmony_ci#include <linux/types.h>
12f08c3bdfSopenharmony_ci#include <sched.h>
13f08c3bdfSopenharmony_ci#include <stdint.h>
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include "config.h"
16f08c3bdfSopenharmony_ci#include "lapi/syscalls.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#ifndef HAVE_CLONE3
19f08c3bdfSopenharmony_cistruct clone_args {
20f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) flags;
21f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) pidfd;
22f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) child_tid;
23f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) parent_tid;
24f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) exit_signal;
25f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) stack;
26f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) stack_size;
27f08c3bdfSopenharmony_ci	uint64_t __attribute__((aligned(8))) tls;
28f08c3bdfSopenharmony_ci};
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic inline int clone3(struct clone_args *args, size_t size)
31f08c3bdfSopenharmony_ci{
32f08c3bdfSopenharmony_ci	return tst_syscall(__NR_clone3, args, size);
33f08c3bdfSopenharmony_ci}
34f08c3bdfSopenharmony_ci#endif
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_ci#ifndef CLONE_PIDFD
37f08c3bdfSopenharmony_ci#define CLONE_PIDFD	0x00001000	/* set if a pidfd should be placed in parent */
38f08c3bdfSopenharmony_ci#endif
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ci#ifndef CLONE_NEWUSER
41f08c3bdfSopenharmony_ci# define CLONE_NEWUSER	0x10000000
42f08c3bdfSopenharmony_ci#endif
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_cistatic inline void clone3_supported_by_kernel(void)
45f08c3bdfSopenharmony_ci{
46f08c3bdfSopenharmony_ci	long ret;
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_ci	if ((tst_kvercmp(5, 3, 0)) < 0) {
49f08c3bdfSopenharmony_ci		/* Check if the syscall is backported on an older kernel */
50f08c3bdfSopenharmony_ci		ret = syscall(__NR_clone3, NULL, 0);
51f08c3bdfSopenharmony_ci		if (ret == -1 && errno == ENOSYS)
52f08c3bdfSopenharmony_ci			tst_brk(TCONF, "Test not supported on kernel version < v5.3");
53f08c3bdfSopenharmony_ci	}
54f08c3bdfSopenharmony_ci}
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci#endif /* LAPI_CLONE_H__ */
57