about summary refs log tree commit diff
path: root/sysdeps/powerpc/test-get_hwcap.c
blob: a64b63080756ca594cb1aeb8db6c354d67b6071e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* Check __ppc_get_hwcap() and __ppc_get_at_plaftorm() functionality.
   Copyright (C) 2015-2021 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

/* Tests if the hwcap, hwcap2 and platform data are stored in the TCB.  */

#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>

#include <support/check.h>
#include <support/xthread.h>

#include <sys/auxv.h>

#include <dl-procinfo.h>

#ifndef STATIC_TST_HWCAP
#undef PROCINFO_DECL
#include <dl-procinfo.c>
#endif

/* Offsets copied from tcb-offsets.h.  */

#ifdef __powerpc64__
# define __TPREG     "r13"
# define __HWCAPOFF -28776
# define __ATPLATOFF -28764
#else
# define __TPREG     "r2"
# define __HWCAPOFF -28736
# define __HWCAP2OFF -28732
# define __ATPLATOFF -28724
#endif

uint64_t check_tcbhwcap (long tid)
{

  uint32_t tcb_at_platform, at_platform;
  uint64_t hwcap, hwcap2, tcb_hwcap;
  const char *at_platform_string;

  /* Testing if the hwcap/hwcap2 data is correctly initialized by
     TLS_TP_INIT.  */

  register unsigned long __tp __asm__ (__TPREG);

#ifdef __powerpc64__
  __asm__  ("ld %0,%1(%2)\n"
	    : "=r" (tcb_hwcap)
	    : "n" (__HWCAPOFF), "b" (__tp));
#else
  uint64_t h1, h2;

  __asm__ ("lwz %0,%1(%2)\n"
      : "=r" (h1)
      : "n" (__HWCAPOFF), "b" (__tp));
  __asm__ ("lwz %0,%1(%2)\n"
      : "=r" (h2)
      : "n" (__HWCAP2OFF), "b" (__tp));
  tcb_hwcap = (h1 >> 32) << 32 | (h2 >> 32);
#endif

  hwcap = getauxval (AT_HWCAP);
  hwcap2 = getauxval (AT_HWCAP2);

  /* hwcap contains only the latest supported ISA, the code checks which is
     and fills the previous supported ones.  This is necessary because the
     same is done in hwcapinfo.c when setting the values that are copied to
     the TCB.  */

  if (hwcap2 & PPC_FEATURE2_ARCH_2_07)
    hwcap |= PPC_FEATURE_ARCH_2_06
	  | PPC_FEATURE_ARCH_2_05
	  | PPC_FEATURE_POWER5_PLUS
	  | PPC_FEATURE_POWER5
	  | PPC_FEATURE_POWER4;
  else if (hwcap & PPC_FEATURE_ARCH_2_06)
    hwcap |= PPC_FEATURE_ARCH_2_05
	  | PPC_FEATURE_POWER5_PLUS
	  | PPC_FEATURE_POWER5
	  | PPC_FEATURE_POWER4;
  else if (hwcap & PPC_FEATURE_ARCH_2_05)
    hwcap |= PPC_FEATURE_POWER5_PLUS
	  | PPC_FEATURE_POWER5
	  | PPC_FEATURE_POWER4;
  else if (hwcap & PPC_FEATURE_POWER5_PLUS)
    hwcap |= PPC_FEATURE_POWER5
	  | PPC_FEATURE_POWER4;
  else if (hwcap & PPC_FEATURE_POWER5)
    hwcap |= PPC_FEATURE_POWER4;

  hwcap = (hwcap << 32) + hwcap2;

  if ( tcb_hwcap != hwcap )
    {
      printf ("FAIL: __ppc_get_hwcap() - HWCAP is %" PRIx64 ". Should be %"
	      PRIx64 " for thread %ld.\n", tcb_hwcap, hwcap, tid);
      return 1;
    }

  /* Same test for the platform number.  */
  __asm__  ("lwz %0,%1(%2)\n"
	    : "=r" (tcb_at_platform)
	    : "n" (__ATPLATOFF), "b" (__tp));

  at_platform_string = (const char *) getauxval (AT_PLATFORM);
  at_platform = _dl_string_platform (at_platform_string);

  if ( tcb_at_platform != at_platform )
    {
      printf ("FAIL: __ppc_get_at_platform() - AT_PLATFORM is %x. Should be %x"
	     " for thread %ld\n", tcb_at_platform, at_platform, tid);
      return 1;
    }

  return 0;
}

void *t1 (void *tid)
{
  if (check_tcbhwcap ((long) tid))
    {
      pthread_exit (tid);
    }

  pthread_exit (NULL);

}

static int
do_test (void)
{

  pthread_t threads[2];
  pthread_attr_t attr;
  pthread_attr_init (&attr);
  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_JOINABLE);

  long i = 0;

  /* Check for main.  */
  if (check_tcbhwcap (i))
    {
      return 1;
    }

  /* Check for other thread.  */
  i++;
  threads[i] = xpthread_create (&attr, t1, (void *)i);

  pthread_attr_destroy (&attr);
  TEST_VERIFY_EXIT (xpthread_join (threads[i]) == NULL);

  printf("PASS: HWCAP, HWCAP2 and AT_PLATFORM are correctly set in the TCB for"
	 " all threads.\n");

  pthread_exit (NULL);

}

#include <support/test-driver.c>