Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig standard library. Private Struct

ThreadCompletion

Private Struct
std.Thread.LinuxThreadImpl.ThreadCompletion
const ThreadCompletion = struct

Fields

completion: Completion = Completion.init(.running)
child_tid: std.atomic.Value(i32) = std.atomic.Value(i32).init(1)
parent_tid: i32 = undefined
mapped: []align(std.heap.page_size_min) u8

Values

  • var sparc_exit_stack

    On SPARC, the kernel needs to be able to restore the current register window from the stack when returning from a syscall. That presents a bit of a problem in freeAndExit since we're deallocating the stack! The good news is that, since we do not care about the contents of the incoming and local registers at that point, we can just tell the kernel that our stack is this undefined global buffer.

Functions

fn freeAndExit(self: *ThreadCompletion) noreturn

Calls munmap(mapped.ptr, mapped.len) then exit(1) without touching the stack (which lives in mapped.ptr). Ported over from musl libc's pthread detached implementation: https://github.com/ifduyue/musl/search?q=__unmapself

File

Thread.zig:1141
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
    const ThreadCompletion = struct {
        completion: Completion = Completion.init(.running),
        child_tid: std.atomic.Value(i32) = std.atomic.Value(i32).init(1),
        parent_tid: i32 = undefined,
        mapped: []align(std.heap.page_size_min) u8,

        // On SPARC, the kernel needs to be able to restore the current register window from the
        // stack when returning from a syscall. That presents a bit of a problem in `freeAndExit`
        // since we're deallocating the stack! The good news is that, since we do not care about
        // the contents of the incoming and local registers at that point, we can just tell the
        // kernel that our stack is this undefined global buffer.
        var sparc_exit_stack: [192]u8 align(16) = undefined;

        /// Calls `munmap(mapped.ptr, mapped.len)` then `exit(1)` without touching the stack (which lives in `mapped.ptr`).
        /// Ported over from musl libc's pthread detached implementation:
        /// https://github.com/ifduyue/musl/search?q=__unmapself
        fn freeAndExit(self: *ThreadCompletion) noreturn {
            // If we do not reset the child_tidptr to null here, the kernel would later write the
            // value zero to that address, which is inside the block we're unmapping below, after
            // our thread exits.  This can sometimes corrupt memory in other mmap blocks from
            // unrelated concurrent threads.
            _ = linux.set_tid_address(null);
            // If a signal were delivered between SYS_munmap and SYS_exit, any installed signal
            // handler would immediately segfault due to the stack being unmapped. To avoid this,
            // we need to mask all signals before entering the inline asm.
            posix.sigprocmask(std.posix.SIG.BLOCK, &std.os.linux.sigfillset(), null);
            switch (target.cpu.arch) {
                .x86 => asm volatile (
                    \\ movl $91, %%eax # SYS_munmap
                    \\ int $128
                    \\ movl $1, %%eax # SYS_exit
                    \\ movl $0, %%ebx
                    \\ int $128
                    :
                    : [ptr] "{ebx}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{ecx}" (self.mapped.len),
                ),
                .x86_64 => asm volatile (switch (target.abi) {
                        .gnux32, .muslx32, .x32 =>
                        \\ movl $0x4000000b, %%eax # SYS_munmap
                        \\ syscall
                        \\ movl $0x4000003c, %%eax # SYS_exit
                        \\ xor %%rdi, %%rdi
                        \\ syscall
                        ,
                        else =>
                        \\ movl $11, %%eax # SYS_munmap
                        \\ syscall
                        \\ movl $60, %%eax # SYS_exit
                        \\ xor %%rdi, %%rdi
                        \\ syscall
                        ,
                    }
                    :
                    : [ptr] "{rdi}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{rsi}" (self.mapped.len),
                ),
                .arm, .armeb, .thumb, .thumbeb => asm volatile (
                    \\ mov r7, #91 // SYS_munmap
                    \\ svc 0
                    \\ mov r7, #1 // SYS_exit
                    \\ mov r0, #0
                    \\ svc 0
                    :
                    : [ptr] "{r0}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r1}" (self.mapped.len),
                ),
                .aarch64, .aarch64_be => asm volatile (
                    \\ mov x8, #215 // SYS_munmap
                    \\ svc 0
                    \\ mov x8, #93 // SYS_exit
                    \\ mov x0, #0
                    \\ svc 0
                    :
                    : [ptr] "{x0}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{x1}" (self.mapped.len),
                ),
                .alpha => asm volatile (
                    \\ ldi $0, 73 # SYS_munmap
                    \\ callsys
                    \\ ldi $0, 1 # SYS_exit
                    \\ ldi $16, 0
                    \\ callsys
                    :
                    : [ptr] "{$16}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{$17}" (self.mapped.len),
                ),
                .arc, .arceb => asm volatile (
                    \\ mov r8, 215 # SYS_munmap
                    \\ trap_s 0
                    \\ mov r8, 93 # SYS_exit
                    \\ mov r0, 0
                    \\ trap_s 0
                    :
                    : [ptr] "{r0}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r1}" (self.mapped.len),
                ),
                .hexagon => asm volatile (
                    \\ r6 = #215 // SYS_munmap
                    \\ trap0(#1)
                    \\ r6 = #93 // SYS_exit
                    \\ r0 = #0
                    \\ trap0(#1)
                    :
                    : [ptr] "{r0}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r1}" (self.mapped.len),
                ),
                .hppa => asm volatile (
                    \\ ble 0x100(%%sr2, %%r0)
                    \\  ldi 91, %%r20 /* SYS_munmap */
                    \\ ldi 0, %%r26
                    \\ ble 0x100(%%sr2, %%r0)
                    \\  ldi 1, %%r20 /* SYS_exit */
                    :
                    : [ptr] "{r26}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r25}" (self.mapped.len),
                ),
                .m68k => asm volatile (
                    \\ move.l #91, %%d0 // SYS_munmap
                    \\ trap #0
                    \\ move.l #1, %%d0 // SYS_exit
                    \\ move.l #0, %%d1
                    \\ trap #0
                    :
                    : [ptr] "{d1}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{d2}" (self.mapped.len),
                ),
                .microblaze, .microblazeel => asm volatile (
                    \\ ori r12, r0, 91 # SYS_munmap
                    \\ brki r14, 0x8
                    \\ ori r12, r0, 1 # SYS_exit
                    \\ ori r5, r0, 0
                    \\ brki r14, 0x8
                    :
                    : [ptr] "{r5}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r6}" (self.mapped.len),
                ),
                .mips, .mipsel => asm volatile (
                    \\ li $v0, 4091 # SYS_munmap
                    \\ syscall
                    \\ li $v0, 4001 # SYS_exit
                    \\ li $a0, 0
                    \\ syscall
                    :
                    : [ptr] "{$4}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{$5}" (self.mapped.len),
                ),
                .mips64, .mips64el => asm volatile (switch (target.abi) {
                        .gnuabin32, .muslabin32, .abin32 =>
                        \\ li $v0, 6011 # SYS_munmap
                        \\ syscall
                        \\ li $v0, 6058 # SYS_exit
                        \\ li $a0, 0
                        \\ syscall
                        ,
                        else =>
                        \\ li $v0, 5011 # SYS_munmap
                        \\ syscall
                        \\ li $v0, 5058 # SYS_exit
                        \\ li $a0, 0
                        \\ syscall
                        ,
                    }
                    :
                    : [ptr] "{$4}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{$5}" (self.mapped.len),
                ),
                .or1k => asm volatile (
                    \\ l.ori r11, r0, 215 # SYS_munmap
                    \\ l.sys 1
                    \\ l.ori r11, r0, 93 # SYS_exit
                    \\ l.ori r3, r0, 0
                    \\ l.sys 1
                    :
                    : [ptr] "{r3}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r4}" (self.mapped.len),
                ),
                .powerpc, .powerpcle, .powerpc64, .powerpc64le => asm volatile (
                    \\ li 0, 91 # SYS_munmap
                    \\ sc
                    \\ li 0, 1 # SYS_exit
                    \\ li 3, 0
                    \\ sc
                    :
                    : [ptr] "{r3}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r4}" (self.mapped.len),
                ),
                .riscv32, .riscv64 => asm volatile (
                    \\ li a7, 215 # SYS_munmap
                    \\ ecall
                    \\ li a7, 93 # SYS_exit
                    \\ mv a0, zero
                    \\ ecall
                    :
                    : [ptr] "{a0}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{a1}" (self.mapped.len),
                ),
                .s390x => asm volatile (
                    \\ svc 91 # SYS_munmap
                    \\ lghi %%r2, 0
                    \\ svc 1 # SYS_exit
                    :
                    : [ptr] "{r2}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r3}" (self.mapped.len),
                ),
                .sh, .sheb => asm volatile (
                    \\ mov #91, r3 ! SYS_munmap
                    \\ trapa #31
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ mov #1, r3 ! SYS_exit
                    \\ mov #0, r4
                    \\ trapa #31
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ or r0, r0
                    \\ or r0, r0
                    :
                    : [ptr] "{r4}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r5}" (self.mapped.len),
                ),
                .sparc => asm volatile (
                    \\ // See sparc64 comments below.
                    \\ t 0x3 // ST_FLUSH_WINDOWS
                    \\ mov %%g3, %%sp
                    \\ mov %%g1, %%o0
                    \\ mov %%g2, %%o1
                    \\ mov 73, %%g1 // SYS_munmap
                    \\ t 0x10
                    \\ mov 1, %%g1 // SYS_exit
                    \\ mov 0, %%o0
                    \\ t 0x10
                    :
                    : [ptr] "{g1}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{g2}" (self.mapped.len),
                      [stack] "{g3}" (&sparc_exit_stack),
                ),
                .sparc64 => asm volatile (
                    \\ // Ensure that the kernel only has to flush the current register window.
                    \\ flushw
                    \\ // Set up a fake stack for the syscall to restore l/i registers from. Local
                    \\ // and incoming registers must be treated as effectively garbage past this
                    \\ // instruction!
                    \\ sub %%g3, 2047, %%sp
                    \\ mov %%g1, %%o0
                    \\ mov %%g2, %%o1
                    \\ mov 73, %%g1 // SYS_munmap
                    \\ t 0x6d
                    \\ mov 1, %%g1 // SYS_exit
                    \\ mov 0, %%o0
                    \\ t 0x6d
                    :
                    : [ptr] "{g1}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{g2}" (self.mapped.len),
                      [stack] "{g3}" (&sparc_exit_stack),
                ),
                .loongarch32, .loongarch64 => asm volatile (
                    \\ ori     $a7, $zero, 215     # SYS_munmap
                    \\ syscall 0                   # call munmap
                    \\ ori     $a0, $zero, 0
                    \\ ori     $a7, $zero, 93      # SYS_exit
                    \\ syscall 0                   # call exit
                    :
                    : [ptr] "{r4}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r5}" (self.mapped.len),
                ),
                .csky => asm volatile (
                    \\ movi r7, 215 # SYS_munmap
                    \\ trap 0
                    \\ movi r7, 93 # SYS_exit
                    \\ movi r0, 0
                    \\ trap 0
                    :
                    : [ptr] "{r0}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{r1}" (self.mapped.len),
                ),
                .xtensa, .xtensaeb => asm volatile (
                    \\ movi a2, 81 // SYS_munmap
                    \\ syscall
                    \\ movi a6, 0
                    \\ movi a2, 118 // SYS_exit
                    \\ syscall
                    :
                    : [ptr] "{a6}" (@intFromPtr(self.mapped.ptr)),
                      [len] "{a3}" (self.mapped.len),
                ),
                else => |cpu_arch| @compileError("Unsupported linux arch: " ++ @tagName(cpu_arch)),
            }
            unreachable;
        }
    }