Skip to content

Commit da834a2

Browse files
author
Blaise Tine
committed
adding support for TLS global variables
1 parent 77002dd commit da834a2

File tree

4 files changed

+91
-35
lines changed

4 files changed

+91
-35
lines changed

runtime/linker/vx_link32.ld

+12-2
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,22 @@ SECTIONS
8989
.gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }
9090
.exception_ranges : ONLY_IF_RW { *(.exception_ranges*) }
9191
/* Thread Local Storage sections */
92-
.tdata :
92+
.tdata :
9393
{
9494
PROVIDE_HIDDEN (__tdata_start = .);
9595
*(.tdata .tdata.* .gnu.linkonce.td.*)
96+
PROVIDE_HIDDEN (__tdata_end = .);
9697
}
97-
.tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
98+
PROVIDE (__tdata_size = SIZEOF (.tdata));
99+
.tbss :
100+
{
101+
PROVIDE_HIDDEN (__tbss_start = .);
102+
PROVIDE_HIDDEN (__tbss_offset = ABSOLUTE (__tbss_start - __tdata_start));
103+
*(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon)
104+
PROVIDE_HIDDEN (__tbss_end = .);
105+
}
106+
PROVIDE (__tbss_size = SIZEOF (.tbss));
107+
PROVIDE (__tcb_aligned_size = ALIGN(__tbss_end - __tdata_start, 64));
98108
.preinit_array :
99109
{
100110
PROVIDE_HIDDEN (__preinit_array_start = .);

runtime/linker/vx_link64.ld

+12-2
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,22 @@ SECTIONS
8989
.gcc_except_table : ONLY_IF_RW { *(.gcc_except_table .gcc_except_table.*) }
9090
.exception_ranges : ONLY_IF_RW { *(.exception_ranges*) }
9191
/* Thread Local Storage sections */
92-
.tdata :
92+
.tdata :
9393
{
9494
PROVIDE_HIDDEN (__tdata_start = .);
9595
*(.tdata .tdata.* .gnu.linkonce.td.*)
96+
PROVIDE_HIDDEN (__tdata_end = .);
9697
}
97-
.tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
98+
PROVIDE (__tdata_size = SIZEOF (.tdata));
99+
.tbss :
100+
{
101+
PROVIDE_HIDDEN (__tbss_start = .);
102+
PROVIDE_HIDDEN (__tbss_offset = ABSOLUTE (__tbss_start - __tdata_start));
103+
*(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon)
104+
PROVIDE_HIDDEN (__tbss_end = .);
105+
}
106+
PROVIDE (__tbss_size = SIZEOF (.tbss));
107+
PROVIDE (__tcb_aligned_size = ALIGN(__tbss_end - __tdata_start, 64));
98108
.preinit_array :
99109
{
100110
PROVIDE_HIDDEN (__preinit_array_start = .);

runtime/src/vx_start.S

+36-15
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,44 @@
44
.global _start
55
.type _start, @function
66
_start:
7-
8-
# execute stack initialization on all warps
9-
la a1, vx_set_sp
7+
8+
# initialize per-thread registers
109
csrr a0, CSR_NW # get num warps
10+
la a1, init_regs
1111
.insn s 0x6b, 1, a1, 0(a0) # wspawn a0, a1
12-
jal vx_set_sp
12+
jal init_regs
13+
# return back to single thread execution
14+
li a0, 1
15+
.insn s 0x6b, 0, x0, 0(a0) # tmc a0
1316

17+
# initialize TLS for all warps
18+
csrr a0, CSR_NW # get num warps
19+
la a1, __init_tls
20+
.insn s 0x6b, 1, a1, 0(a0) # wspawn a0, a1
21+
call __init_tls
1422
# return back to single thread execution
1523
li a0, 1
1624
.insn s 0x6b, 0, x0, 0(a0) # tmc a0
1725

18-
# Clear the bss segment
26+
# clear BSS segment
1927
la a0, _edata
2028
la a2, _end
2129
sub a2, a2, a0
2230
li a1, 0
2331
call memset
2432

33+
# Initialize trap vector
34+
# a t0, trap_entry
35+
# csrw mtvec, t0
36+
2537
# Register global termination functions
26-
la a0, __libc_fini_array
38+
la a0, __libc_fini_array
2739

2840
# to be called upon exit
29-
call atexit
41+
call atexit
3042

3143
# Run global initialization functions
32-
call __libc_init_array
44+
call __libc_init_array
3345

3446
# call main program routine
3547
call main
@@ -49,9 +61,9 @@ _exit:
4961
.insn s 0x6b, 0, x0, 0(a0) # tmc a0
5062

5163
.section .text
52-
.type vx_set_sp, @function
53-
.global vx_set_sp
54-
vx_set_sp:
64+
.type init_regs, @function
65+
.global init_regs
66+
init_regs:
5567
# activate all threads
5668
li a0, -1
5769
.insn s 0x6b, 0, x0, 0(a0) # tmc a0
@@ -66,12 +78,21 @@ vx_set_sp:
6678
# set stack pointer
6779
li sp, SMEM_BASE_ADDR # load stack base address
6880
#if SM_ENABLE
69-
csrr a2, CSR_LTID # get local thread id
81+
csrr a0, CSR_LTID # get local thread id
7082
#else
71-
csrr a2, CSR_GTID # get global thread id
83+
csrr a0, CSR_GTID # get global thread id
7284
#endif
73-
slli a1, a2, STACK_LOG2_SIZE
74-
sub sp, sp, a1 # sub thread block
85+
sll a1, a0, STACK_LOG2_SIZE
86+
sub sp, sp, a1
87+
88+
# set thread pointer register
89+
# use address space after BSS region
90+
# ensure cacheline alignment
91+
la a1, __tcb_aligned_size
92+
mul a0, a0, a1
93+
la tp, _end + 63
94+
add tp, tp, a0
95+
and tp, tp, -64
7596

7697
# disable active warps except warp0
7798
csrr a3, CSR_LWID # get local wid

runtime/src/vx_syscalls.c

+31-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <unistd.h>
44
#include <vx_intrinsics.h>
55
#include <vx_print.h>
6+
#include <string.h>
67

78
int _close(int file) { return -1; }
89

@@ -22,18 +23,36 @@ caddr_t _sbrk(int incr) {
2223
}
2324

2425
int _write(int file, char *ptr, int len) {
25-
int i;
26-
for (i = 0; i < len; ++i) {
27-
vx_putchar(*ptr++);
28-
}
29-
return len;
30-
}
26+
int i;
27+
for (i = 0; i < len; ++i) {
28+
vx_putchar(*ptr++);
29+
}
30+
return len;
31+
}
32+
33+
int _kill(int pid, int sig) { return -1; }
34+
35+
int _getpid() {
36+
return vx_warp_gid();
37+
}
3138

32-
int _kill(int pid, int sig) { return -1; }
39+
void __init_tls(void) {
40+
extern char __tdata_start[];
41+
extern char __tbss_offset[];
42+
extern char __tdata_size[];
43+
extern char __tbss_size[];
3344

34-
int _getpid() {
35-
return vx_warp_gid();
36-
}
45+
// activate all threads
46+
vx_tmc(-1);
47+
48+
// TLS memory initialization
49+
register char *__thread_self __asm__ ("tp");
50+
memcpy(__thread_self, __tdata_start, (size_t)__tdata_size);
51+
memset(__thread_self + (size_t)__tbss_offset, 0, (size_t)__tbss_size);
52+
53+
// back to single thread execution
54+
vx_tmc(0 == vx_warp_id());
55+
}
3756

3857
#ifdef HAVE_INITFINI_ARRAY
3958

@@ -48,9 +67,7 @@ extern void _init (void);
4867
#endif
4968

5069
/* Iterate over all the init routines. */
51-
void
52-
__libc_init_array (void)
53-
{
70+
void __libc_init_array (void) {
5471
size_t count;
5572
size_t i;
5673

@@ -77,9 +94,7 @@ extern void _fini (void);
7794
#endif
7895

7996
/* Run all the cleanup routines. */
80-
void
81-
__libc_fini_array (void)
82-
{
97+
void __libc_fini_array (void) {
8398
size_t count;
8499
size_t i;
85100

0 commit comments

Comments
 (0)