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