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