summaryrefslogtreecommitdiff
path: root/sysdeps/aarch64/tst-sme-clone3.c
blob: 402b040cfd69acd95b8174db46f0c0c889e42282 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* Test that ZA state of SME is cleared in both parent and child
   when clone3() syscall is used.
   Copyright (C) 2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include "tst-sme-skeleton.c"

#include <clone3.h>

#include <errno.h>
#include <sys/wait.h>
#include <support/xsched.h>

/* Since clone3 is not a public symbol, we link this test explicitly
   with clone3.o and have to provide this declaration.  */
int __clone3 (struct clone_args *cl_args, size_t size,
	    int (*func)(void *arg), void *arg);

static int
fun (void * const arg)
{
  printf ("in child: %s\n", (const char *)arg);
  /* Check that ZA state of SME was disabled in child.  */
  check_sme_za_state ("after clone3 in child", /* Clear.  */ true);
  return 0;
}

static char __attribute__((aligned(16)))
stack[1024 * 1024];

/* Required by __arm_za_disable.o and provided by the startup code
   as a hidden symbol.  */
uint64_t _dl_hwcap2;

static void
run (struct blk *ptr)
{
  _dl_hwcap2 = getauxval (AT_HWCAP2);

  char *syscall_name = (char *)"clone3";
  struct clone_args args = {
    .flags = CLONE_VM | CLONE_VFORK,
    .exit_signal = SIGCHLD,
    .stack = (uintptr_t) stack,
    .stack_size = sizeof (stack),
  };
  printf ("in parent: before %s\n", syscall_name);

  /* Enabled ZA state so that effect of disabling be observable.  */
  enable_sme_za_state (ptr);
  check_sme_za_state ("before clone3", /* Clear.  */ false);

  pid_t pid = __clone3 (&args, sizeof (args), fun, syscall_name);

  /* Check that ZA state of SME was disabled in parent.  */
  check_sme_za_state ("after clone3 in parent", /* Clear.  */ true);

  printf ("%s child pid: %d\n", syscall_name, pid);

  xwaitpid (pid, NULL, 0);
  printf ("in parent: after %s\n", syscall_name);
}

/* Workaround to simplify linking with clone3.o.  */
void __syscall_error(int code)
{
  int err = -code;
  fprintf (stderr, "syscall error %d (%s)\n", err, strerror (err));
  exit (err);
}