1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/wait.h>
4 
5 #define THIS_PROGRAM "./vstate_exec_nolibc"
6 
main(int argc,char ** argv)7 int main(int argc, char **argv)
8 {
9 	int rc, pid, status, test_inherit = 0, xtheadvector = 0;
10 	long ctrl, ctrl_c;
11 	char *exec_argv[2], *exec_envp[2];
12 
13 	if (argc > 1 && strcmp(argv[1], "x"))
14 		test_inherit = 1;
15 
16 	if (argc > 2 && strcmp(argv[2], "x"))
17 		xtheadvector = 1;
18 
19 	ctrl = my_syscall1(__NR_prctl, PR_RISCV_V_GET_CONTROL);
20 	if (ctrl < 0) {
21 		puts("PR_RISCV_V_GET_CONTROL is not supported\n");
22 		return ctrl;
23 	}
24 
25 	if (test_inherit) {
26 		pid = fork();
27 		if (pid == -1) {
28 			puts("fork failed\n");
29 			exit(-1);
30 		}
31 
32 		/* child  */
33 		if (!pid) {
34 			exec_argv[0] = THIS_PROGRAM;
35 			exec_argv[1] = NULL;
36 			exec_envp[0] = NULL;
37 			exec_envp[1] = NULL;
38 			/* launch the program again to check inherit */
39 			rc = execve(THIS_PROGRAM, exec_argv, exec_envp);
40 			if (rc) {
41 				puts("child execve failed\n");
42 				exit(-1);
43 			}
44 		}
45 
46 	} else {
47 		pid = fork();
48 		if (pid == -1) {
49 			puts("fork failed\n");
50 			exit(-1);
51 		}
52 
53 		if (!pid) {
54 			rc = my_syscall1(__NR_prctl, PR_RISCV_V_GET_CONTROL);
55 			if (rc != ctrl) {
56 				puts("child's vstate_ctrl not equal to parent's\n");
57 				exit(-1);
58 			}
59 			if (xtheadvector)
60 				asm volatile (".4byte	0x00007ed7");
61 			else
62 				asm volatile (".option push\n\t"
63 					".option arch, +v\n\t"
64 					"vsetvli x0, x0, e32, m8, ta, ma\n\t"
65 					".option pop\n\t"
66 					);
67 			exit(ctrl);
68 		}
69 	}
70 
71 	rc = waitpid(-1, &status, 0);
72 
73 	if (WIFEXITED(status) && WEXITSTATUS(status) == -1) {
74 		puts("child exited abnormally\n");
75 		exit(-1);
76 	}
77 
78 	if (WIFSIGNALED(status)) {
79 		if (WTERMSIG(status) != SIGILL) {
80 			puts("child was terminated by unexpected signal\n");
81 			exit(-1);
82 		}
83 
84 		if ((ctrl & PR_RISCV_V_VSTATE_CTRL_CUR_MASK) != PR_RISCV_V_VSTATE_CTRL_OFF) {
85 			puts("child signaled by illegal V access but vstate_ctrl is not off\n");
86 			exit(-1);
87 		}
88 
89 		/* child terminated, and its vstate_ctrl is off */
90 		exit(ctrl);
91 	}
92 
93 	ctrl_c = WEXITSTATUS(status);
94 	if (test_inherit) {
95 		if (ctrl & PR_RISCV_V_VSTATE_CTRL_INHERIT) {
96 			if (!(ctrl_c & PR_RISCV_V_VSTATE_CTRL_INHERIT)) {
97 				puts("parent has inherit bit, but child has not\n");
98 				exit(-1);
99 			}
100 		}
101 		rc = (ctrl & PR_RISCV_V_VSTATE_CTRL_NEXT_MASK) >> 2;
102 		if (rc != PR_RISCV_V_VSTATE_CTRL_DEFAULT) {
103 			if (rc != (ctrl_c & PR_RISCV_V_VSTATE_CTRL_CUR_MASK)) {
104 				puts("parent's next setting does not equal to child's\n");
105 				exit(-1);
106 			}
107 
108 			if (!(ctrl & PR_RISCV_V_VSTATE_CTRL_INHERIT)) {
109 				if ((ctrl_c & PR_RISCV_V_VSTATE_CTRL_NEXT_MASK) !=
110 				    PR_RISCV_V_VSTATE_CTRL_DEFAULT) {
111 					puts("must clear child's next vstate_ctrl if !inherit\n");
112 					exit(-1);
113 				}
114 			}
115 		}
116 	}
117 	return ctrl;
118 }
119