xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/prctl/prctl10.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2022 FUJITSU LIMITED. All rights reserved.
4  * Author: Yang Xu <[email protected]>
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Basic test to test behaviour of PR_GET_TSC and PR_SET_TSC.
11  *
12  * Set the state of the flag determining whether the timestamp counter can
13  * be read by the process.
14  *
15  * - Pass PR_TSC_ENABLE to arg2 to allow it to be read.
16  * - Pass PR_TSC_SIGSEGV to arg2 to generate a SIGSEGV when read.
17  */
18 
19 #include <sys/prctl.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include "tst_test.h"
24 #include "lapi/prctl.h"
25 
26 #define TCASE_ENTRY(tsc_read_stat) { .name = #tsc_read_stat, .read_stat = tsc_read_stat}
27 
28 static const char * const tsc_read_stat_names[] = {
29 	[0] = "[not set]",
30 	[PR_TSC_ENABLE] = "PR_TSC_ENABLE",
31 	[PR_TSC_SIGSEGV] = "PR_TSC_SIGSEGV",
32 };
33 
34 static struct tcase {
35 	char *name;
36 	int read_stat;
37 } tcases[] = {
38 	TCASE_ENTRY(PR_TSC_ENABLE),
39 	TCASE_ENTRY(PR_TSC_SIGSEGV)
40 };
41 
rdtsc(void)42 static uint64_t rdtsc(void)
43 {
44 	uint32_t lo = 0, hi = 0;
45 
46 #if (defined(__x86_64__) || defined(__i386__))
47 	/* We cannot use "=A", since this would use %rax on x86_64 */
48 	__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
49 #endif
50 
51 	return (uint64_t)hi << 32 | lo;
52 }
53 
54 
expected_status(int status,int exp_status)55 static int expected_status(int status, int exp_status)
56 {
57 	if (!exp_status && WIFEXITED(status))
58 		return 0;
59 
60 	if (exp_status && WIFSIGNALED(status) && WTERMSIG(status) == exp_status)
61 		return 0;
62 
63 	return 1;
64 }
65 
verify_prctl(unsigned int n)66 static void verify_prctl(unsigned int n)
67 {
68 	struct tcase *tc = &tcases[n];
69 	unsigned long long time1, time2;
70 	int tsc_val = 0, pid, status;
71 
72 	pid = SAFE_FORK();
73 	if (!pid) {
74 		TST_EXP_PASS_SILENT(prctl(PR_SET_TSC, tc->read_stat));
75 		TST_EXP_PASS_SILENT(prctl(PR_GET_TSC, &tsc_val));
76 		if (tsc_val == tc->read_stat)
77 			tst_res(TPASS, "current state is %s(%d)",
78 					tc->name, tc->read_stat);
79 		else
80 			tst_res(TFAIL, "current state is %s(%d), expect %s(%d)",
81 					tsc_read_stat_names[tsc_val],
82 					tsc_val, tc->name, tc->read_stat);
83 
84 		time1 = rdtsc();
85 		time2 = rdtsc();
86 		if (time2 > time1)
87 			tst_res(TPASS, "rdtsc works correctly, %lld ->%lld",
88 				time1, time2);
89 		else
90 			tst_res(TFAIL, "rdtsc works incorrectly, %lld ->%lld",
91 				time1, time2);
92 		exit(0);
93 	}
94 	SAFE_WAITPID(pid, &status, 0);
95 
96 	if (expected_status(status, tc->read_stat == PR_TSC_SIGSEGV ? SIGSEGV : 0))
97 		tst_res(TFAIL, "Test %s failed", tc->name);
98 	else
99 		tst_res(TPASS, "Test %s succeeded", tc->name);
100 }
101 
102 static struct tst_test test = {
103 	.forks_child = 1,
104 	.test = verify_prctl,
105 	.tcnt = ARRAY_SIZE(tcases),
106 	.supported_archs = (const char *const []) {
107 		"x86",
108 		"x86_64",
109 		NULL
110 	},
111 };
112