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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
//! This file implements the two TLS variants [1] used by ELF-based systems. Note that, in reality,
//! Variant I has two sub-variants.
//!
//! It is important to understand that the term TCB (Thread Control Block) is overloaded here.
//! Official ABI documentation uses it simply to mean the ABI TCB, i.e. a small area of ABI-defined
//! data, usually one or two words (see the `AbiTcb` type below). People will also often use TCB to
//! refer to the libc TCB, which can be any size and contain anything. (One could even omit it!) We
//! refer to the latter as the Zig TCB; see the `ZigTcb` type below.
//!
//! [1] https://www.akkadia.org/drepper/tls.pdf
const std = @import("std");
const mem = std.mem;
const elf = std.elf;
const math = std.math;
const assert = std.debug.assert;
const native_arch = @import("builtin").cpu.arch;
const linux = std.os.linux;
const posix = std.posix;
/// Represents an ELF TLS variant.
///
/// In all variants, the TP and the TLS blocks must be aligned to the `p_align` value in the
/// `PT_TLS` ELF program header. Everything else has natural alignment.
///
/// The location of the DTV does not actually matter. For simplicity, we put it in the TLS area, but
/// there is no actual ABI requirement that it reside there.
const Variant = enum {
/// The original Variant I:
///
/// ----------------------------------------
/// | DTV | Zig TCB | ABI TCB | TLS Blocks |
/// ----------------^-----------------------
/// `-- The TP register points here.
///
/// The layout in this variant necessitates separate alignment of both the TP and the TLS
/// blocks.
///
/// The first word in the ABI TCB points to the DTV. For some architectures, there may be a
/// second word with an unspecified meaning.
I_original,
/// The modified Variant I:
///
/// ---------------------------------------------------
/// | DTV | Zig TCB | ABI TCB | [Offset] | TLS Blocks |
/// -------------------------------------^-------------
/// `-- The TP register points here.
///
/// The offset (which can be zero) is applied to the TP only; there is never physical gap
/// between the ABI TCB and the TLS blocks. This implies that we only need to align the TP.
///
/// The first (and only) word in the ABI TCB points to the DTV.
I_modified,
/// Variant II:
///
/// ----------------------------------------
/// | TLS Blocks | ABI TCB | Zig TCB | DTV |
/// -------------^--------------------------
/// `-- The TP register points here.
///
/// The first (and only) word in the ABI TCB points to the ABI TCB itself.
II,
};
const current_variant: Variant = switch (native_arch) {
.arc,
.arm,
.armeb,
.aarch64,
.aarch64_be,
.csky,
.thumb,
.thumbeb,
=> .I_original,
.loongarch32,
.loongarch64,
.m68k,
.mips,
.mipsel,
.mips64,
.mips64el,
.powerpc,
.powerpcle,
.powerpc64,
.powerpc64le,
.riscv32,
.riscv64,
=> .I_modified,
.hexagon,
.s390x,
.sparc,
.sparc64,
.x86,
.x86_64,
=> .II,
else => @compileError("undefined TLS variant for this architecture"),
};
/// The Offset value for the modified Variant I.
const current_tp_offset = switch (native_arch) {
.m68k,
.mips,
.mipsel,
.mips64,
.mips64el,
.powerpc,
.powerpcle,
.powerpc64,
.powerpc64le,
=> 0x7000,
else => 0,
};
/// Usually only used by the modified Variant I.
const current_dtv_offset = switch (native_arch) {
.m68k,
.mips,
.mipsel,
.mips64,
.mips64el,
.powerpc,
.powerpcle,
.powerpc64,
.powerpc64le,
=> 0x8000,
.riscv32,
.riscv64,
=> 0x800,
else => 0,
};
/// Per-thread storage for the ELF TLS ABI.
const AbiTcb = switch (current_variant) {
.I_original, .I_modified => switch (native_arch) {
// ARM EABI mandates enough space for two pointers: the first one points to the DTV as
// usual, while the second one is unspecified.
.aarch64,
.aarch64_be,
.arm,
.armeb,
.thumb,
.thumbeb,
=> extern struct {
/// This is offset by `current_dtv_offset`.
dtv: usize,
reserved: ?*anyopaque,
},
else => extern struct {
/// This is offset by `current_dtv_offset`.
dtv: usize,
},
},
.II => extern struct {
/// This is self-referential.
self: *AbiTcb,
},
};
/// Per-thread storage for Zig's use. Currently unused.
const ZigTcb = struct {
dummy: usize,
};
/// Dynamic Thread Vector as specified in the ELF TLS ABI. Ordinarily, there is a block pointer per
/// dynamically-loaded module, but since we only support static TLS, we only need one block pointer.
const Dtv = extern struct {
len: usize = 1,
tls_block: [*]u8,
};
/// Describes a process's TLS area. The area encompasses the DTV, both TCBs, and the TLS block, with
/// the exact layout of these being dependent primarily on `current_variant`.
const AreaDesc = struct {
size: usize,
alignment: usize,
dtv: struct {
/// Offset into the TLS area.
offset: usize,
},
abi_tcb: struct {
/// Offset into the TLS area.
offset: usize,
},
block: struct {
/// The initial data to be copied into the TLS block. Note that this may be smaller than
/// `size`, in which case any remaining data in the TLS block is simply left uninitialized.
init: []const u8,
/// Offset into the TLS area.
offset: usize,
/// This is the effective size of the TLS block, which may be greater than `init.len`.
size: usize,
},
/// Only used on the 32-bit x86 architecture (not x86_64, nor x32).
gdt_entry_number: usize,
};
pub var area_desc: AreaDesc = undefined;
pub fn setThreadPointer(addr: usize) void {
@setRuntimeSafety(false);
@disableInstrumentation();
switch (native_arch) {
.x86 => {
var user_desc: linux.user_desc = .{
.entry_number = area_desc.gdt_entry_number,
.base_addr = addr,
.limit = 0xfffff,
.flags = .{
.seg_32bit = 1,
.contents = 0, // Data
.read_exec_only = 0,
.limit_in_pages = 1,
.seg_not_present = 0,
.useable = 1,
},
};
const rc = @call(.always_inline, linux.syscall1, .{ .set_thread_area, @intFromPtr(&user_desc) });
assert(rc == 0);
const gdt_entry_number = user_desc.entry_number;
// We have to keep track of our slot as it's also needed for clone()
area_desc.gdt_entry_number = gdt_entry_number;
// Update the %gs selector
asm volatile ("movl %[gs_val], %%gs"
:
: [gs_val] "r" (gdt_entry_number << 3 | 3),
);
},
.x86_64 => {
const rc = @call(.always_inline, linux.syscall2, .{ .arch_prctl, linux.ARCH.SET_FS, addr });
assert(rc == 0);
},
.aarch64, .aarch64_be => {
asm volatile (
\\ msr tpidr_el0, %[addr]
:
: [addr] "r" (addr),
);
},
.arc => {
// We apparently need to both set r25 (TP) *and* inform the kernel...
asm volatile (
\\ mov r25, %[addr]
:
: [addr] "r" (addr),
);
const rc = @call(.always_inline, linux.syscall1, .{ .arc_settls, addr });
assert(rc == 0);
},
.arm, .armeb, .thumb, .thumbeb => {
const rc = @call(.always_inline, linux.syscall1, .{ .set_tls, addr });
assert(rc == 0);
},
.m68k => {
const rc = linux.syscall1(.set_thread_area, addr);
assert(rc == 0);
},
.hexagon => {
asm volatile (
\\ ugp = %[addr]
:
: [addr] "r" (addr),
);
},
.loongarch32, .loongarch64 => {
asm volatile (
\\ move $tp, %[addr]
:
: [addr] "r" (addr),
);
},
.riscv32, .riscv64 => {
asm volatile (
\\ mv tp, %[addr]
:
: [addr] "r" (addr),
);
},
.csky, .mips, .mipsel, .mips64, .mips64el => {
const rc = @call(.always_inline, linux.syscall1, .{ .set_thread_area, addr });
assert(rc == 0);
},
.powerpc, .powerpcle => {
asm volatile (
\\ mr 2, %[addr]
:
: [addr] "r" (addr),
);
},
.powerpc64, .powerpc64le => {
asm volatile (
\\ mr 13, %[addr]
:
: [addr] "r" (addr),
);
},
.s390x => {
asm volatile (
\\ lgr %%r0, %[addr]
\\ sar %%a1, %%r0
\\ srlg %%r0, %%r0, 32
\\ sar %%a0, %%r0
:
: [addr] "r" (addr),
: "r0"
);
},
.sparc, .sparc64 => {
asm volatile (
\\ mov %[addr], %%g7
:
: [addr] "r" (addr),
);
},
else => @compileError("Unsupported architecture"),
}
}
fn computeAreaDesc(phdrs: []elf.Phdr) void {
@setRuntimeSafety(false);
@disableInstrumentation();
var tls_phdr: ?*elf.Phdr = null;
var img_base: usize = 0;
for (phdrs) |*phdr| {
switch (phdr.p_type) {
elf.PT_PHDR => img_base = @intFromPtr(phdrs.ptr) - phdr.p_vaddr,
elf.PT_TLS => tls_phdr = phdr,
else => {},
}
}
var align_factor: usize = undefined;
var block_init: []const u8 = undefined;
var block_size: usize = undefined;
if (tls_phdr) |phdr| {
align_factor = phdr.p_align;
// The effective size in memory is represented by `p_memsz`; the length of the data stored
// in the `PT_TLS` segment is `p_filesz` and may be less than the former.
block_init = @as([*]u8, @ptrFromInt(img_base + phdr.p_vaddr))[0..phdr.p_filesz];
block_size = phdr.p_memsz;
} else {
align_factor = @alignOf(usize);
block_init = &[_]u8{};
block_size = 0;
}
// Offsets into the allocated TLS area.
var dtv_offset: usize = undefined;
var abi_tcb_offset: usize = undefined;
var block_offset: usize = undefined;
// Compute the total size of the ABI-specific data plus our own `ZigTcb` structure. All the
// offsets calculated here assume a well-aligned base address.
const area_size = switch (current_variant) {
.I_original => blk: {
var l: usize = 0;
dtv_offset = l;
l += @sizeOf(Dtv);
// Add some padding here so that the TP (`abi_tcb_offset`) is aligned to `align_factor`
// and the `ZigTcb` structure can be found by simply subtracting `@sizeOf(ZigTcb)` from
// the TP.
const delta = (l + @sizeOf(ZigTcb)) & (align_factor - 1);
if (delta > 0)
l += align_factor - delta;
l += @sizeOf(ZigTcb);
abi_tcb_offset = l;
l += alignForward(@sizeOf(AbiTcb), align_factor);
block_offset = l;
l += block_size;
break :blk l;
},
.I_modified => blk: {
var l: usize = 0;
dtv_offset = l;
l += @sizeOf(Dtv);
// In this variant, the TLS blocks must begin immediately after the end of the ABI TCB,
// with the TP pointing to the beginning of the TLS blocks. Add padding so that the TP
// (`abi_tcb_offset`) is aligned to `align_factor` and the `ZigTcb` structure can be
// found by subtracting `@sizeOf(AbiTcb) + @sizeOf(ZigTcb)` from the TP.
const delta = (l + @sizeOf(ZigTcb) + @sizeOf(AbiTcb)) & (align_factor - 1);
if (delta > 0)
l += align_factor - delta;
l += @sizeOf(ZigTcb);
abi_tcb_offset = l;
l += @sizeOf(AbiTcb);
block_offset = l;
l += block_size;
break :blk l;
},
.II => blk: {
var l: usize = 0;
block_offset = l;
l += alignForward(block_size, align_factor);
// The TP is aligned to `align_factor`.
abi_tcb_offset = l;
l += @sizeOf(AbiTcb);
// The `ZigTcb` structure is right after the `AbiTcb` with no padding in between so it
// can be easily found.
l += @sizeOf(ZigTcb);
// It doesn't really matter where we put the DTV, so give it natural alignment.
l = alignForward(l, @alignOf(Dtv));
dtv_offset = l;
l += @sizeOf(Dtv);
break :blk l;
},
};
area_desc = .{
.size = area_size,
.alignment = align_factor,
.dtv = .{
.offset = dtv_offset,
},
.abi_tcb = .{
.offset = abi_tcb_offset,
},
.block = .{
.init = block_init,
.offset = block_offset,
.size = block_size,
},
.gdt_entry_number = @as(usize, @bitCast(@as(isize, -1))),
};
}
/// Inline because TLS is not set up yet.
inline fn alignForward(addr: usize, alignment: usize) usize {
return alignBackward(addr + (alignment - 1), alignment);
}
/// Inline because TLS is not set up yet.
inline fn alignBackward(addr: usize, alignment: usize) usize {
return addr & ~(alignment - 1);
}
/// Inline because TLS is not set up yet.
inline fn alignPtrCast(comptime T: type, ptr: [*]u8) *T {
return @ptrCast(@alignCast(ptr));
}
/// Initializes all the fields of the static TLS area and returns the computed architecture-specific
/// value of the TP register.
pub fn prepareArea(area: []u8) usize {
@setRuntimeSafety(false);
@disableInstrumentation();
// Clear the area we're going to use, just to be safe.
@memset(area, 0);
// Prepare the ABI TCB.
const abi_tcb = alignPtrCast(AbiTcb, area.ptr + area_desc.abi_tcb.offset);
switch (current_variant) {
.I_original, .I_modified => abi_tcb.dtv = @intFromPtr(area.ptr + area_desc.dtv.offset),
.II => abi_tcb.self = abi_tcb,
}
// Prepare the DTV.
const dtv = alignPtrCast(Dtv, area.ptr + area_desc.dtv.offset);
dtv.len = 1;
dtv.tls_block = area.ptr + current_dtv_offset + area_desc.block.offset;
// Copy the initial data.
@memcpy(area[area_desc.block.offset..][0..area_desc.block.init.len], area_desc.block.init);
// Return the corrected value (if needed) for the TP register. Overflow here is not a problem;
// the pointer arithmetic involving the TP is done with wrapping semantics.
return @intFromPtr(area.ptr) +% switch (current_variant) {
.I_original, .II => area_desc.abi_tcb.offset,
.I_modified => area_desc.block.offset +% current_tp_offset,
};
}
// The main motivation for the size chosen here is that this is how much ends up being requested for
// the thread-local variables of the `std.crypto.random` implementation. I'm not sure why it ends up
// being so much; the struct itself is only 64 bytes. I think it has to do with being page-aligned
// and LLVM or LLD is not smart enough to lay out the TLS data in a space-conserving way. Anyway, I
// think it's fine because it's less than 3 pages of memory, and putting it in the ELF like this is
// equivalent to moving the `mmap` call below into the kernel, avoiding syscall overhead.
var main_thread_area_buffer: [0x2100]u8 align(mem.page_size) = undefined;
/// Computes the layout of the static TLS area, allocates the area, initializes all of its fields,
/// and assigns the architecture-specific value to the TP register.
pub fn initStatic(phdrs: []elf.Phdr) void {
@setRuntimeSafety(false);
@disableInstrumentation();
computeAreaDesc(phdrs);
const area = blk: {
// Fast path for the common case where the TLS data is really small, avoid an allocation and
// use our local buffer.
if (area_desc.alignment <= mem.page_size and area_desc.size <= main_thread_area_buffer.len) {
break :blk main_thread_area_buffer[0..area_desc.size];
}
const begin_addr = mmap(
null,
area_desc.size + area_desc.alignment - 1,
posix.PROT.READ | posix.PROT.WRITE,
.{ .TYPE = .PRIVATE, .ANONYMOUS = true },
-1,
0,
);
if (@as(isize, @bitCast(begin_addr)) < 0) @trap();
const area_ptr: [*]align(mem.page_size) u8 = @ptrFromInt(begin_addr);
// Make sure the slice is correctly aligned.
const begin_aligned_addr = alignForward(begin_addr, area_desc.alignment);
const start = begin_aligned_addr - begin_addr;
break :blk area_ptr[start..][0..area_desc.size];
};
const tp_value = prepareArea(area);
setThreadPointer(tp_value);
}
inline fn mmap(address: ?[*]u8, length: usize, prot: usize, flags: linux.MAP, fd: i32, offset: i64) usize {
if (@hasField(linux.SYS, "mmap2")) {
return @call(.always_inline, linux.syscall6, .{
.mmap2,
@intFromPtr(address),
length,
prot,
@as(u32, @bitCast(flags)),
@as(usize, @bitCast(@as(isize, fd))),
@as(usize, @truncate(@as(u64, @bitCast(offset)) / linux.MMAP2_UNIT)),
});
} else {
// The s390x mmap() syscall existed before Linux supported syscalls with 5+ parameters, so
// it takes a single pointer to an array of arguments instead.
return if (native_arch == .s390x) @call(.always_inline, linux.syscall1, .{
.mmap,
@intFromPtr(&[_]usize{
@intFromPtr(address),
length,
prot,
@as(u32, @bitCast(flags)),
@as(usize, @bitCast(@as(isize, fd))),
@as(u64, @bitCast(offset)),
}),
}) else @call(.always_inline, linux.syscall6, .{
.mmap,
@intFromPtr(address),
length,
prot,
@as(u32, @bitCast(flags)),
@as(usize, @bitCast(@as(isize, fd))),
@as(u64, @bitCast(offset)),
});
}
}
|