0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <stdio.h>
0012 #include <unistd.h>
0013 #include <sys/syscall.h>
0014 #include <sys/time.h>
0015 #include <sys/types.h>
0016 #include <sys/wait.h>
0017 #include <stdlib.h>
0018 #include <string.h>
0019 #include <pthread.h>
0020 #include <altivec.h>
0021
0022 #include "utils.h"
0023
0024
0025 #define ITERATIONS 10
0026
0027
0028
0029
0030 #define THREAD_FACTOR 8
0031
0032 __thread vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
0033 {13,14,15,16},{17,18,19,20},{21,22,23,24},
0034 {25,26,27,28},{29,30,31,32},{33,34,35,36},
0035 {37,38,39,40},{41,42,43,44},{45,46,47,48}};
0036
0037 bool bad_context;
0038 int running;
0039 int threads_starting;
0040
0041 extern int preempt_vmx(vector int *varray, int *threads_starting, int *sentinal);
0042
0043 void signal_vmx_sig(int sig, siginfo_t *info, void *context)
0044 {
0045 int i;
0046 ucontext_t *uc = context;
0047 mcontext_t *mc = &uc->uc_mcontext;
0048
0049
0050 for (i = 20; i < 32; i++) {
0051 if (memcmp(mc->v_regs->vrregs[i], &varray[i - 20], 16)) {
0052 int j;
0053
0054
0055
0056
0057
0058
0059 printf("VMX mismatch at reg %d!\n", i);
0060 printf("Reg | Actual | Expected\n");
0061 for (j = 20; j < 32; j++) {
0062 printf("%d | 0x%04x%04x%04x%04x | 0x%04x%04x%04x%04x\n", j, mc->v_regs->vrregs[j][0],
0063 mc->v_regs->vrregs[j][1], mc->v_regs->vrregs[j][2], mc->v_regs->vrregs[j][3],
0064 varray[j - 20][0], varray[j - 20][1], varray[j - 20][2], varray[j - 20][3]);
0065 }
0066 bad_context = true;
0067 break;
0068 }
0069 }
0070 }
0071
0072 void *signal_vmx_c(void *p)
0073 {
0074 int i, j;
0075 long rc;
0076 struct sigaction act;
0077 act.sa_sigaction = signal_vmx_sig;
0078 act.sa_flags = SA_SIGINFO;
0079 rc = sigaction(SIGUSR1, &act, NULL);
0080 if (rc)
0081 return p;
0082
0083 srand(pthread_self());
0084 for (i = 0; i < 12; i++)
0085 for (j = 0; j < 4; j++)
0086 varray[i][j] = rand();
0087
0088 rc = preempt_vmx(varray, &threads_starting, &running);
0089
0090 return (void *) rc;
0091 }
0092
0093 int test_signal_vmx(void)
0094 {
0095 int i, j, rc, threads;
0096 void *rc_p;
0097 pthread_t *tids;
0098
0099
0100 SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
0101
0102 threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
0103 tids = malloc(threads * sizeof(pthread_t));
0104 FAIL_IF(!tids);
0105
0106 running = true;
0107 threads_starting = threads;
0108 for (i = 0; i < threads; i++) {
0109 rc = pthread_create(&tids[i], NULL, signal_vmx_c, NULL);
0110 FAIL_IF(rc);
0111 }
0112
0113 setbuf(stdout, NULL);
0114 printf("\tWaiting for %d workers to start... %d", threads, threads_starting);
0115 while (threads_starting) {
0116 asm volatile("": : :"memory");
0117 usleep(1000);
0118 printf(", %d", threads_starting);
0119 }
0120 printf(" ...done\n");
0121
0122 printf("\tSending signals to all threads %d times...", ITERATIONS);
0123 for (i = 0; i < ITERATIONS; i++) {
0124 for (j = 0; j < threads; j++) {
0125 pthread_kill(tids[j], SIGUSR1);
0126 }
0127 sleep(1);
0128 }
0129 printf("done\n");
0130
0131 printf("\tKilling workers...");
0132 running = 0;
0133 for (i = 0; i < threads; i++) {
0134 pthread_join(tids[i], &rc_p);
0135
0136
0137
0138
0139
0140 if ((long) rc_p || bad_context)
0141 printf("oops\n");
0142 if (bad_context)
0143 fprintf(stderr, "\t!! bad_context is true\n");
0144 FAIL_IF((long) rc_p || bad_context);
0145 }
0146 printf("done\n");
0147
0148 free(tids);
0149 return 0;
0150 }
0151
0152 int main(int argc, char *argv[])
0153 {
0154 return test_harness(test_signal_vmx, "vmx_signal");
0155 }