Skip to content

Do not emit AVX instructions for UEFI target #7885

@justinian

Description

@justinian

While working on porting my bootloader to Zig, I hit issues with #UD (undefined opcode) exceptions in my program. The following minimal Zig code will cause the error when not compiled in one of the release modes.

const std = @import("std");
const uefi = std.os.uefi;

pub fn main() uefi.Status {
    var buf: [64]u16 = undefined;
    while (true) {}
}

The issue is with the following assembly, which looks to be doing a memset-like routine on buf using AVX isntructions:

// main()
 470:   55                      push   rbp
 471:   48 81 ec b0 00 00 00    sub    rsp,0xb0
 478:   48 8d ac 24 80 00 00    lea    rbp,[rsp+0x80]
 47f:   00
 480:   48 8d 45 a8             lea    rax,[rbp-0x58]
 484:   48 89 c1                mov    rcx,rax
 487:   ba aa 00 00 00          mov    edx,0xaa
 48c:   41 b8 80 00 00 00       mov    r8d,0x80
 492:   e8 b9 00 00 00          call   0x550
 497:   eb fe                   jmp    0x497
 499:   0f 1f 80 00 00 00 00    nop    DWORD PTR [rax+0x0]

// memset
 550:   48 89 c8                mov    rax,rcx
 553:   4d 85 c0                test   r8,r8
 556:   74 64                   je     0x5bc
 558:   49 81 f8 80 00 00 00    cmp    r8,0x80
 55f:   73 05                   jae    0x566
 561:   45 31 c9                xor    r9d,r9d
 564:   eb 4a                   jmp    0x5b0
 566:   c5 f9 6e c2             vmovd  xmm0,edx
 56a:   4d 89 c1                mov    r9,r8
 56d:   31 c9                   xor    ecx,ecx
 56f:   49 83 e1 80             and    r9,0xffffffffffffff80
 573:   c4 e2 7d 78 c0          vpbroadcastb ymm0,xmm0    // ===== #UD EXCEPTION HERE =====
 578:   0f 1f 84 00 00 00 00    nop    DWORD PTR [rax+rax*1+0x0]
 57f:   00
 580:   c5 fe 7f 04 08          vmovdqu YMMWORD PTR [rax+rcx*1],ymm0
 585:   c5 fe 7f 44 08 20       vmovdqu YMMWORD PTR [rax+rcx*1+0x20],ymm0
 58b:   c5 fe 7f 44 08 40       vmovdqu YMMWORD PTR [rax+rcx*1+0x40],ymm0
 591:   c5 fe 7f 44 08 60       vmovdqu YMMWORD PTR [rax+rcx*1+0x60],ymm0
 597:   48 83 e9 80             sub    rcx,0xffffffffffffff80
 59b:   49 39 c9                cmp    r9,rcx
 59e:   0f 85 dc ff ff ff       jne    0x580
 5a4:   4d 39 c1                cmp    r9,r8
 5a7:   0f 84 0f 00 00 00       je     0x5bc
 5ad:   0f 1f 00                nop    DWORD PTR [rax]
 5b0:   42 88 14 08             mov    BYTE PTR [rax+r9*1],dl
 5b4:   49 ff c1                inc    r9
 5b7:   4d 39 c8                cmp    r8,r9
 5ba:   75 f4                   jne    0x5b0
 5bc:   c5 f8 77                vzeroupper
 5bf:   c3                      ret

Since enabling AVX isn't a part of the state for x64 UEFI apps defined by the spec, this target should probably default to not using AVX.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavioros-uefiUnified Extensible Firmware Interface

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions