| ... | ... | @@ -1,8 +1,9 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | const builtin = std.builtin; |
| 2 | 3 | const os = std.os; |
| 3 | 4 | const mem = std.mem; |
| 4 | 5 | const elf = std.elf; |
| 5 | | const builtin = @import("builtin"); |
| 6 | const math = std.math; |
| 6 | 7 | const assert = std.debug.assert; |
| 7 | 8 | |
| 8 | 9 | // This file implements the two TLS variants [1] used by ELF-based systems. |
| ... | ... | @@ -60,10 +61,11 @@ const tls_tcb_size = switch (builtin.arch) { |
| 60 | 61 | else => @sizeOf(usize), |
| 61 | 62 | }; |
| 62 | 63 | |
| 63 | | // Controls if the TCB should be aligned according to the TLS segment p_align |
| 64 | // Controls the minimum alignment of the TCB end address. The effective value |
| 65 | // used by the code is min(this_value, tls_segment.p_align) |
| 64 | 66 | const tls_tcb_align_size = switch (builtin.arch) { |
| 65 | | .arm, .armeb, .aarch64, .aarch64_be => true, |
| 66 | | else => false, |
| 67 | .arm, .armeb, .aarch64, .aarch64_be => 16, |
| 68 | else => 1, |
| 67 | 69 | }; |
| 68 | 70 | |
| 69 | 71 | // Controls if the TP points to the end of the TCB instead of its beginning |
| ... | ... | @@ -72,13 +74,6 @@ const tls_tp_points_past_tcb = switch (builtin.arch) { |
| 72 | 74 | else => false, |
| 73 | 75 | }; |
| 74 | 76 | |
| 75 | | // Check if the architecture-specific parameters look correct |
| 76 | | comptime { |
| 77 | | if (tls_tcb_align_size and tls_variant != TLSVariant.VariantI) { |
| 78 | | @compileError("tls_tcb_align_size is only meaningful for variant I TLS"); |
| 79 | | } |
| 80 | | } |
| 81 | | |
| 82 | 77 | // Some architectures add some offset to the tp and dtv addresses in order to |
| 83 | 78 | // make the generated code more efficient |
| 84 | 79 | |
| ... | ... | @@ -94,17 +89,19 @@ const tls_dtv_offset = switch (builtin.arch) { |
| 94 | 89 | }; |
| 95 | 90 | |
| 96 | 91 | // Per-thread storage for Zig's use |
| 97 | | const CustomData = packed struct {}; |
| 92 | const CustomData = struct { |
| 93 | padding: [16]usize, |
| 94 | }; |
| 98 | 95 | |
| 99 | 96 | // Dynamic Thread Vector |
| 100 | | const DTV = packed struct { |
| 97 | const DTV = extern struct { |
| 101 | 98 | entries: usize, |
| 102 | | tls_block: [1]usize, |
| 99 | tls_block: [1][*]u8, |
| 103 | 100 | }; |
| 104 | 101 | |
| 105 | 102 | // Holds all the information about the process TLS image |
| 106 | 103 | const TLSImage = struct { |
| 107 | | data_src: []u8, |
| 104 | data_src: []const u8, |
| 108 | 105 | alloc_size: usize, |
| 109 | 106 | tcb_offset: usize, |
| 110 | 107 | dtv_offset: usize, |
| ... | ... | @@ -113,13 +110,13 @@ const TLSImage = struct { |
| 113 | 110 | gdt_entry_number: usize, |
| 114 | 111 | }; |
| 115 | 112 | |
| 116 | | pub var tls_image: ?TLSImage = null; |
| 113 | pub var tls_image: TLSImage = undefined; |
| 117 | 114 | |
| 118 | 115 | pub fn setThreadPointer(addr: usize) void { |
| 119 | 116 | switch (builtin.arch) { |
| 120 | 117 | .i386 => { |
| 121 | 118 | var user_desc = std.os.linux.user_desc{ |
| 122 | | .entry_number = tls_image.?.gdt_entry_number, |
| 119 | .entry_number = tls_image.gdt_entry_number, |
| 123 | 120 | .base_addr = addr, |
| 124 | 121 | .limit = 0xfffff, |
| 125 | 122 | .seg_32bit = 1, |
| ... | ... | @@ -134,7 +131,7 @@ pub fn setThreadPointer(addr: usize) void { |
| 134 | 131 | |
| 135 | 132 | const gdt_entry_number = user_desc.entry_number; |
| 136 | 133 | // We have to keep track of our slot as it's also needed for clone() |
| 137 | | tls_image.?.gdt_entry_number = gdt_entry_number; |
| 134 | tls_image.gdt_entry_number = gdt_entry_number; |
| 138 | 135 | // Update the %gs selector |
| 139 | 136 | asm volatile ("movl %[gs_val], %%gs" |
| 140 | 137 | : |
| ... | ... | @@ -171,7 +168,7 @@ pub fn setThreadPointer(addr: usize) void { |
| 171 | 168 | } |
| 172 | 169 | } |
| 173 | 170 | |
| 174 | | pub fn initTLS() ?*elf.Phdr { |
| 171 | fn initTLS() void { |
| 175 | 172 | var tls_phdr: ?*elf.Phdr = null; |
| 176 | 173 | var img_base: usize = 0; |
| 177 | 174 | |
| ... | ... | @@ -195,124 +192,138 @@ pub fn initTLS() ?*elf.Phdr { |
| 195 | 192 | // Sanity check |
| 196 | 193 | assert(at_phent == @sizeOf(elf.Phdr)); |
| 197 | 194 | |
| 198 | | // Search the TLS section |
| 195 | // Find the TLS section |
| 199 | 196 | const phdrs = (@intToPtr([*]elf.Phdr, at_phdr))[0..at_phnum]; |
| 200 | 197 | |
| 201 | | var gnu_stack: ?*elf.Phdr = null; |
| 202 | | |
| 203 | 198 | for (phdrs) |*phdr| { |
| 204 | 199 | switch (phdr.p_type) { |
| 205 | 200 | elf.PT_PHDR => img_base = at_phdr - phdr.p_vaddr, |
| 206 | 201 | elf.PT_TLS => tls_phdr = phdr, |
| 207 | | elf.PT_GNU_STACK => gnu_stack = phdr, |
| 208 | | else => continue, |
| 202 | else => {}, |
| 209 | 203 | } |
| 210 | 204 | } |
| 211 | 205 | |
| 212 | | if (tls_phdr) |phdr| { |
| 213 | | // If the cpu is arm-based, check if it supports the TLS register |
| 214 | | if (builtin.arch == .arm and at_hwcap & std.os.linux.HWCAP_TLS == 0) { |
| 215 | | // If the CPU does not support TLS via a coprocessor register, |
| 216 | | // a kernel helper function can be used instead on certain linux kernels. |
| 217 | | // See linux/arch/arm/include/asm/tls.h and musl/src/thread/arm/__set_thread_area.c. |
| 218 | | @panic("TODO: Implement ARM fallback TLS functionality"); |
| 219 | | } |
| 206 | // If the cpu is ARM-based, check if it supports the TLS register |
| 207 | if (comptime builtin.arch.isARM() and at_hwcap & std.os.linux.HWCAP_TLS == 0) { |
| 208 | // If the CPU does not support TLS via a coprocessor register, |
| 209 | // a kernel helper function can be used instead on certain linux kernels. |
| 210 | // See linux/arch/arm/include/asm/tls.h and musl/src/thread/arm/__set_thread_area.c. |
| 211 | @panic("TODO: Implement ARM fallback TLS functionality"); |
| 212 | } |
| 220 | 213 | |
| 221 | | // Offsets into the allocated TLS area |
| 222 | | var tcb_offset: usize = undefined; |
| 223 | | var dtv_offset: usize = undefined; |
| 224 | | var data_offset: usize = undefined; |
| 225 | | var thread_data_offset: usize = undefined; |
| 226 | | // Compute the total size of the ABI-specific data plus our own control |
| 227 | | // structures |
| 228 | | const alloc_size = switch (tls_variant) { |
| 229 | | .VariantI => blk: { |
| 230 | | var l: usize = 0; |
| 231 | | dtv_offset = l; |
| 232 | | l += @sizeOf(DTV); |
| 233 | | thread_data_offset = l; |
| 234 | | l += @sizeOf(CustomData); |
| 235 | | l = mem.alignForward(l, phdr.p_align); |
| 236 | | tcb_offset = l; |
| 237 | | if (tls_tcb_align_size) { |
| 238 | | l += mem.alignForward(tls_tcb_size, phdr.p_align); |
| 239 | | } else { |
| 240 | | l += tls_tcb_size; |
| 241 | | } |
| 242 | | data_offset = l; |
| 243 | | l += phdr.p_memsz; |
| 244 | | break :blk l; |
| 245 | | }, |
| 246 | | .VariantII => blk: { |
| 247 | | var l: usize = 0; |
| 248 | | data_offset = l; |
| 249 | | l += phdr.p_memsz; |
| 250 | | l = mem.alignForward(l, phdr.p_align); |
| 251 | | tcb_offset = l; |
| 252 | | l += tls_tcb_size; |
| 253 | | thread_data_offset = l; |
| 254 | | l += @sizeOf(CustomData); |
| 255 | | dtv_offset = l; |
| 256 | | l += @sizeOf(DTV); |
| 257 | | break :blk l; |
| 258 | | }, |
| 259 | | }; |
| 260 | | |
| 261 | | tls_image = TLSImage{ |
| 262 | | .data_src = @intToPtr([*]u8, phdr.p_vaddr + img_base)[0..phdr.p_filesz], |
| 263 | | .alloc_size = alloc_size, |
| 264 | | .tcb_offset = tcb_offset, |
| 265 | | .dtv_offset = dtv_offset, |
| 266 | | .data_offset = data_offset, |
| 267 | | .gdt_entry_number = @bitCast(usize, @as(isize, -1)), |
| 268 | | }; |
| 214 | var tls_align_factor: usize = undefined; |
| 215 | var tls_data: []const u8 = undefined; |
| 216 | if (tls_phdr) |phdr| { |
| 217 | tls_align_factor = phdr.p_align; |
| 218 | tls_data = @intToPtr([*]u8, img_base + phdr.p_vaddr)[0..phdr.p_memsz]; |
| 219 | } else { |
| 220 | tls_align_factor = @alignOf(*usize); |
| 221 | tls_data = &[_]u8{}; |
| 269 | 222 | } |
| 270 | 223 | |
| 271 | | return gnu_stack; |
| 224 | // Offsets into the allocated TLS area |
| 225 | var tcb_offset: usize = undefined; |
| 226 | var dtv_offset: usize = undefined; |
| 227 | var data_offset: usize = undefined; |
| 228 | var thread_data_offset: usize = undefined; |
| 229 | // Compute the total size of the ABI-specific data plus our own control |
| 230 | // structures |
| 231 | const alloc_size = switch (tls_variant) { |
| 232 | .VariantI => blk: { |
| 233 | var l: usize = 0; |
| 234 | // Unneeded because l is zero |
| 235 | // l = mem.alignForward(l, @alignOf(DTV)); |
| 236 | dtv_offset = l; |
| 237 | l += @sizeOf(DTV); |
| 238 | l = mem.alignForward(l, @alignOf(CustomData)); |
| 239 | thread_data_offset = l; |
| 240 | l += @sizeOf(CustomData); |
| 241 | // Make sure the TP is aligned |
| 242 | l = mem.alignForward(l, tls_align_factor); |
| 243 | tcb_offset = l; |
| 244 | // Ensure there are at least tls_tcb_align_size bytes of padding |
| 245 | const min_align = math.max(tls_tcb_align_size, tls_align_factor); |
| 246 | l += mem.alignForward(tls_tcb_size, min_align); |
| 247 | data_offset = l; |
| 248 | l += mem.alignForward(tls_data.len, tls_align_factor); |
| 249 | break :blk l; |
| 250 | }, |
| 251 | .VariantII => blk: { |
| 252 | var l: usize = 0; |
| 253 | data_offset = l; |
| 254 | l = mem.alignForward(tls_data.len, tls_align_factor); |
| 255 | // The TP is aligned to p_align |
| 256 | tcb_offset = l; |
| 257 | l += tls_tcb_size; |
| 258 | l = mem.alignForward(l, @alignOf(CustomData)); |
| 259 | thread_data_offset = l; |
| 260 | l += @sizeOf(CustomData); |
| 261 | l = mem.alignForward(l, @alignOf(DTV)); |
| 262 | dtv_offset = l; |
| 263 | l += @sizeOf(DTV); |
| 264 | break :blk l; |
| 265 | }, |
| 266 | }; |
| 267 | |
| 268 | tls_image = TLSImage{ |
| 269 | .data_src = tls_data, |
| 270 | .alloc_size = alloc_size, |
| 271 | .tcb_offset = tcb_offset, |
| 272 | .dtv_offset = dtv_offset, |
| 273 | .data_offset = data_offset, |
| 274 | .gdt_entry_number = @bitCast(usize, @as(isize, -1)), |
| 275 | }; |
| 272 | 276 | } |
| 273 | 277 | |
| 274 | | pub fn copyTLS(addr: usize) usize { |
| 275 | | const tls_img = tls_image.?; |
| 278 | inline fn alignPtrCast(comptime T: type, ptr: [*]u8) *T { |
| 279 | return @ptrCast(*T, @alignCast(@alignOf(*T), ptr)); |
| 280 | } |
| 276 | 281 | |
| 277 | | // Be paranoid, clear the area we're going to use |
| 278 | | @memset(@intToPtr([*]u8, addr), 0, tls_img.alloc_size); |
| 282 | /// Initializes all the fields of the static TLS area and returns the computed |
| 283 | /// architecture-specific value of the thread-pointer register |
| 284 | pub fn prepareTLS(area: []u8) usize { |
| 285 | // Clear the area we're going to use, just to be safe |
| 286 | mem.set(u8, area, 0); |
| 279 | 287 | // Prepare the DTV |
| 280 | | const dtv = @intToPtr(*DTV, addr + tls_img.dtv_offset); |
| 288 | const dtv = alignPtrCast(DTV, area.ptr + tls_image.dtv_offset); |
| 281 | 289 | dtv.entries = 1; |
| 282 | | dtv.tls_block[0] = addr + tls_img.data_offset + tls_dtv_offset; |
| 283 | | // Set-up the TCB |
| 284 | | // Force the alignment to 1 byte as the TCB may start from a non-aligned |
| 285 | | // address under the variant II model |
| 286 | | const tcb_ptr = @intToPtr(*align(1) usize, addr + tls_img.tcb_offset); |
| 287 | | if (tls_variant == TLSVariant.VariantI) { |
| 288 | | tcb_ptr.* = addr + tls_img.dtv_offset; |
| 289 | | } else { |
| 290 | | tcb_ptr.* = addr + tls_img.tcb_offset; |
| 291 | | } |
| 290 | dtv.tls_block[0] = area.ptr + tls_dtv_offset + tls_image.data_offset; |
| 291 | // Prepare the TCB |
| 292 | const tcb_ptr = alignPtrCast([*]u8, area.ptr + tls_image.tcb_offset); |
| 293 | tcb_ptr.* = switch (tls_variant) { |
| 294 | .VariantI => area.ptr + tls_image.dtv_offset, |
| 295 | .VariantII => area.ptr + tls_image.tcb_offset, |
| 296 | }; |
| 292 | 297 | // Copy the data |
| 293 | | @memcpy(@intToPtr([*]u8, addr + tls_img.data_offset), tls_img.data_src.ptr, tls_img.data_src.len); |
| 298 | mem.copy(u8, area[tls_image.data_offset..], tls_image.data_src); |
| 294 | 299 | |
| 295 | 300 | // Return the corrected (if needed) value for the tp register |
| 296 | | return addr + tls_tp_offset + |
| 297 | | if (tls_tp_points_past_tcb) tls_img.data_offset else tls_img.tcb_offset; |
| 301 | return @ptrToInt(area.ptr) + tls_tp_offset + |
| 302 | if (tls_tp_points_past_tcb) tls_image.data_offset else tls_image.tcb_offset; |
| 298 | 303 | } |
| 299 | 304 | |
| 300 | 305 | var main_thread_tls_buffer: [256]u8 align(32) = undefined; |
| 301 | 306 | |
| 302 | | pub fn allocateTLS(size: usize) usize { |
| 303 | | // Small TLS allocation, use our local buffer |
| 304 | | if (size < main_thread_tls_buffer.len) { |
| 305 | | return @ptrToInt(&main_thread_tls_buffer); |
| 306 | | } |
| 307 | pub fn initStaticTLS() void { |
| 308 | initTLS(); |
| 307 | 309 | |
| 308 | | const slice = os.mmap( |
| 309 | | null, |
| 310 | | size, |
| 311 | | os.PROT_READ | os.PROT_WRITE, |
| 312 | | os.MAP_PRIVATE | os.MAP_ANONYMOUS, |
| 313 | | -1, |
| 314 | | 0, |
| 315 | | ) catch @panic("out of memory"); |
| 310 | var tls_area = blk: { |
| 311 | // Fast path for the common case where the TLS data is really small, |
| 312 | // avoid an allocation and use our local buffer |
| 313 | if (tls_image.alloc_size < main_thread_tls_buffer.len) { |
| 314 | break :blk main_thread_tls_buffer[0..tls_image.alloc_size]; |
| 315 | } |
| 316 | 316 | |
| 317 | | return @ptrToInt(slice.ptr); |
| 317 | break :blk os.mmap( |
| 318 | null, |
| 319 | tls_image.alloc_size, |
| 320 | os.PROT_READ | os.PROT_WRITE, |
| 321 | os.MAP_PRIVATE | os.MAP_ANONYMOUS, |
| 322 | -1, |
| 323 | 0, |
| 324 | ) catch @panic("out of memory"); |
| 325 | }; |
| 326 | |
| 327 | const tp_value = prepareTLS(tls_area); |
| 328 | setThreadPointer(tp_value); |
| 318 | 329 | } |