Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2022 ARM Limited.
0004  * Original author: Mark Brown <broonie@kernel.org>
0005  */
0006 
0007 // SPDX-License-Identifier: GPL-2.0-only
0008 
0009 #include <linux/sched.h>
0010 #include <linux/wait.h>
0011 
0012 #define EXPECTED_TESTS 1
0013 
0014 static void putstr(const char *str)
0015 {
0016     write(1, str, strlen(str));
0017 }
0018 
0019 static void putnum(unsigned int num)
0020 {
0021     char c;
0022 
0023     if (num / 10)
0024         putnum(num / 10);
0025 
0026     c = '0' + (num % 10);
0027     write(1, &c, 1);
0028 }
0029 
0030 static int tests_run;
0031 static int tests_passed;
0032 static int tests_failed;
0033 static int tests_skipped;
0034 
0035 static void print_summary(void)
0036 {
0037     if (tests_passed + tests_failed + tests_skipped != EXPECTED_TESTS)
0038         putstr("# UNEXPECTED TEST COUNT: ");
0039 
0040     putstr("# Totals: pass:");
0041     putnum(tests_passed);
0042     putstr(" fail:");
0043     putnum(tests_failed);
0044     putstr(" xfail:0 xpass:0 skip:");
0045     putnum(tests_skipped);
0046     putstr(" error:0\n");
0047 }
0048 
0049 int fork_test(void);
0050 int verify_fork(void);
0051 
0052 /*
0053  * If we fork the value in the parent should be unchanged and the
0054  * child should start with the same value.  This is called from the
0055  * fork_test() asm function.
0056  */
0057 int fork_test_c(void)
0058 {
0059     pid_t newpid, waiting;
0060     int child_status, parent_result;
0061 
0062     newpid = fork();
0063     if (newpid == 0) {
0064         /* In child */
0065         if (!verify_fork()) {
0066             putstr("# ZA state invalid in child\n");
0067             exit(0);
0068         } else {
0069             exit(1);
0070         }
0071     }
0072     if (newpid < 0) {
0073         putstr("# fork() failed: -");
0074         putnum(-newpid);
0075         putstr("\n");
0076         return 0;
0077     }
0078 
0079     parent_result = verify_fork();
0080     if (!parent_result)
0081         putstr("# ZA state invalid in parent\n");
0082 
0083     for (;;) {
0084         waiting = waitpid(newpid, &child_status, 0);
0085 
0086         if (waiting < 0) {
0087             if (errno == EINTR)
0088                 continue;
0089             putstr("# waitpid() failed: ");
0090             putnum(errno);
0091             putstr("\n");
0092             return 0;
0093         }
0094         if (waiting != newpid) {
0095             putstr("# waitpid() returned wrong PID\n");
0096             return 0;
0097         }
0098 
0099         if (!WIFEXITED(child_status)) {
0100             putstr("# child did not exit\n");
0101             return 0;
0102         }
0103 
0104         return WEXITSTATUS(child_status) && parent_result;
0105     }
0106 }
0107 
0108 #define run_test(name)               \
0109     if (name()) {                \
0110         tests_passed++;          \
0111     } else {                 \
0112         tests_failed++;          \
0113         putstr("not ");          \
0114     }                    \
0115     putstr("ok ");               \
0116     putnum(++tests_run);             \
0117     putstr(" " #name "\n");
0118 
0119 int main(int argc, char **argv)
0120 {
0121     int ret, i;
0122 
0123     putstr("TAP version 13\n");
0124     putstr("1..");
0125     putnum(EXPECTED_TESTS);
0126     putstr("\n");
0127 
0128     putstr("# PID: ");
0129     putnum(getpid());
0130     putstr("\n");
0131 
0132     /*
0133      * This test is run with nolibc which doesn't support hwcap and
0134      * it's probably disproportionate to implement so instead check
0135      * for the default vector length configuration in /proc.
0136      */
0137     ret = open("/proc/sys/abi/sme_default_vector_length", O_RDONLY, 0);
0138     if (ret >= 0) {
0139         run_test(fork_test);
0140 
0141     } else {
0142         putstr("# SME support not present\n");
0143 
0144         for (i = 0; i < EXPECTED_TESTS; i++) {
0145             putstr("ok ");
0146             putnum(i);
0147             putstr(" skipped\n");
0148         }
0149 
0150         tests_skipped += EXPECTED_TESTS;
0151     }
0152 
0153     print_summary();
0154 
0155     return 0;
0156 }