-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
147 lines (123 loc) · 4.38 KB
/
main.c
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
/*
* Quick&Dirty program for testing tpidrurw register with threading on ARM
*
* Copyright 2013 André Hentschel
*
* This 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.
*
* This 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 this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <sys/types.h>
#ifdef __arm__
#define __ASM_GLOBAL_FUNC(name,code) asm(".text\n\t.align 4\n\t.globl " #name "\n\t.type " #name ",%function\n" #name ":\n\t.cfi_startproc\n\t" code "\n\t.cfi_endproc\n\t.previous");
extern unsigned int get_tls2( void );
__ASM_GLOBAL_FUNC( get_tls2,
"mrc p15, 0, r0, c13, c0, 2\n\t"
"bx LR\n\t"
)
extern void set_tls2( unsigned int );
__ASM_GLOBAL_FUNC( set_tls2,
"mcr p15, 0, r0, c13, c0, 2\n\t"
"bx lr\n\t"
)
#elif __aarch64__
#warning This test should be compiled for ARM, ARM64 support is an experimental feature
#define __ASM_GLOBAL_FUNC(name,code) asm(".text\n\t.align 4\n\t.globl " #name "\n\t.type " #name ",@function\n" #name ":\n\t.cfi_startproc\n\t" code "\n\t.cfi_endproc\n\t.previous");
extern unsigned int get_tls2( void );
__ASM_GLOBAL_FUNC( get_tls2,
"mrs x0, tpidr_el0\n\t"
"ret\n\t"
)
extern void set_tls2( unsigned int );
__ASM_GLOBAL_FUNC( set_tls2,
"msr tpidr_el0, x0\n\t"
"ret\n\t"
)
#else
#warning This test should be compiled for ARM
static unsigned int x86TPIDRURW;
unsigned int get_tls2( void )
{
return x86TPIDRURW;
}
void set_tls2( unsigned int t )
{
x86TPIDRURW = t;
}
#endif
static void test_thread( unsigned int *info )
{
int i;
set_tls2(*info);
printf("INFO: test_thread with %08x\n", *info);
for (i=0; i<5; i++)
{
unsigned int r = get_tls2();
if (r != *info)
printf("ERROR: TPIDRURW is %08x, expected %08x\n", r, *info);
sleep(1);
}
}
static void test_fork_thread( unsigned int info )
{
int i;
printf("INFO: test_fork_thread with %08x\n", info);
for (i=0; i<5; i++)
{
unsigned int r = get_tls2();
if (r != info)
printf("ERROR: TPIDRURW is %08x, expected %08x\n", r, info);
sleep(1);
}
}
static void test_fork( unsigned int *info )
{
int i;
pid_t tpid;
set_tls2(*info);
printf("INFO: test_fork with %08x\n", *info);
tpid = fork();
if (tpid == -1)
printf("WARNING: failed to fork\n");
else if (tpid == 0) /* one for the child */
test_fork_thread(*info);
else /* and one for the parent */
test_fork_thread(*info);
}
int main(void)
{
pthread_t pthread_id1, pthread_id2, pthread_id3;
pthread_attr_t attr1, attr2, attr3;
unsigned int t1 = 0xcafebabe, t2 = 0xdeadbeef, t3 = 0x12345678, r;
set_tls2(0xdeadc0de);
r = get_tls2();
if (r != 0xdeadc0de)
printf("ERROR: TPIDRURW is %08x, expected %08x\n", r, 0xdeadc0de);
pthread_attr_init( &attr1 );
if (pthread_create( &pthread_id1, &attr1, (void * (*)(void *))test_thread, &t1 ))
printf("WARNING: failed to create first thread\n");
pthread_attr_init( &attr2 );
if (pthread_create( &pthread_id2, &attr2, (void * (*)(void *))test_thread, &t2 ))
printf("WARNING: failed to create first thread\n");
pthread_attr_init( &attr3 );
if (pthread_create( &pthread_id3, &attr3, (void * (*)(void *))test_fork, &t3 ))
printf("WARNING: failed to create first thread\n");
set_tls2(0x1badbabe);
r = get_tls2();
if (r != 0x1badbabe)
printf("ERROR: TPIDRURW is %08x, expected %08x\n", r, 0x1badbabe);
sleep(12);
printf("\nIf there are no lines above starting with \"ERROR\", then your kernel is suitable for running Windows RT Applications with Wine!\n\n");
return 0;
}