Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
29bcdcf
std.os.uefi.tables: ziggify boot and runtime services
dotcarmen Apr 1, 2025
7e1f689
avoid T{} syntax
dotcarmen Apr 2, 2025
46daf84
misc fixes
dotcarmen Apr 2, 2025
8f1ab0d
work
dotcarmen Apr 3, 2025
ddecbd1
self-review quickfixes
dotcarmen Apr 3, 2025
64e9c9a
dont make MemoryMapSlice generic
dotcarmen Apr 3, 2025
33650ee
more review fixes, work
dotcarmen Apr 3, 2025
c868ccf
more work
dotcarmen Apr 3, 2025
597b6da
more work
dotcarmen Apr 4, 2025
06b6c8b
review fixes
dotcarmen Apr 4, 2025
136b10f
update boot/runtime services references throughout codebase
dotcarmen Apr 4, 2025
8f125ad
self-review fixes
dotcarmen Apr 4, 2025
2f65ded
couple of fixes i forgot to commit earlier
dotcarmen Apr 4, 2025
6d03cce
fixes from integrating in my own project
dotcarmen Apr 4, 2025
8e2d24c
fixes from refAllDeclsRecursive
dotcarmen Apr 4, 2025
003fbc7
Apply suggestions from code review
dotcarmen Apr 5, 2025
6d0e2bf
more fixes from review
dotcarmen Apr 5, 2025
5fc4b18
fixes from project integration
dotcarmen Apr 5, 2025
86c191f
make natural alignment of Guid align-8
dotcarmen Apr 13, 2025
347c003
EventRegistration is a new opaque type
dotcarmen Apr 13, 2025
ccc5f3d
fix getNextHighMonotonicCount
dotcarmen Apr 13, 2025
855cf17
fix locateProtocol
dotcarmen Apr 13, 2025
18600c1
fix exit
dotcarmen Apr 13, 2025
e316b7e
partly revert 7372d65
dotcarmen Apr 13, 2025
e426856
oops exit data_len is num of bytes
dotcarmen Apr 13, 2025
80cbffb
fixes from project integration
dotcarmen Apr 13, 2025
b2e35f4
MapInfo consistency, MemoryType update per review
dotcarmen Apr 18, 2025
99d7a34
turn EventRegistration back into a pointer
dotcarmen Apr 18, 2025
2fff399
forgot to finish updating MemoryType methods
dotcarmen Apr 18, 2025
cefa6cd
fix IntFittingRange calls
dotcarmen Apr 18, 2025
1027e0d
set uefi.Page nat alignment
dotcarmen Apr 19, 2025
3f32679
Back out "set uefi.Page nat alignment"
dotcarmen Apr 21, 2025
0ecda35
get rid of some error.NotFound-s
dotcarmen Apr 21, 2025
9f1bd86
fix .exit call in panic
dotcarmen Apr 21, 2025
e13de1c
review comments, add format method
dotcarmen May 12, 2025
6cc6434
fix resetSystem data alignment
dotcarmen May 12, 2025
819a6c6
oops, didnt do a final refAllDeclsRecursive i guess
dotcarmen Jun 5, 2025
a6c60c3
review comments
dotcarmen Jul 11, 2025
2517ac7
merge latest master
dotcarmen Jul 11, 2025
cc6f15a
writergate update MemoryType.format
dotcarmen Jul 11, 2025
8676b6a
fix rename
dotcarmen Jul 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/std/Thread.zig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub fn sleep(nanoseconds: u64) void {
const boot_services = std.os.uefi.system_table.boot_services.?;
const us_from_ns = nanoseconds / std.time.ns_per_us;
const us = math.cast(usize, us_from_ns) orelse math.maxInt(usize);
_ = boot_services.stall(us);
boot_services.stall(us) catch unreachable;
return;
}

Expand Down
5 changes: 2 additions & 3 deletions lib/std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,8 @@ pub fn defaultPanic(

if (uefi.system_table.boot_services) |bs| {
// ExitData buffer must be allocated using boot_services.allocatePool (spec: page 220)
const exit_data: []u16 = uefi.raw_pool_allocator.alloc(u16, exit_msg.len + 1) catch @trap();
@memcpy(exit_data, exit_msg[0..exit_data.len]); // Includes null terminator.
_ = bs.exit(uefi.handle, .aborted, exit_data.len, exit_data.ptr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

afaict this was a bug 😬 exit_data.len for number of u16s but the spec says it's the number of bytes

const exit_data = uefi.raw_pool_allocator.dupeZ(u16, exit_msg) catch @trap();
bs.exit(uefi.handle, .aborted, exit_data) catch {};
}
@trap();
},
Expand Down
52 changes: 49 additions & 3 deletions lib/std/os/uefi.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,51 @@ pub var handle: Handle = undefined;
/// A pointer to the EFI System Table that is passed to the EFI image's entry point.
pub var system_table: *tables.SystemTable = undefined;

/// UEFI's memory interfaces exclusively act on 4096-byte pages.
pub const Page = [4096]u8;

/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = *const opaque {};

pub const EventType = packed struct(u32) {
lo_context: u8 = 0,
/// If an event of this type is not already in the signaled state, then
/// the event’s NotificationFunction will be queued at the event’s NotifyTpl
/// whenever the event is being waited on via EFI_BOOT_SERVICES.WaitForEvent()
/// or EFI_BOOT_SERVICES.CheckEvent() .
wait: bool = false,
/// The event’s NotifyFunction is queued whenever the event is signaled.
signal: bool = false,
hi_context: u20 = 0,
/// The event is allocated from runtime memory. If an event is to be signaled
/// after the call to EFI_BOOT_SERVICES.ExitBootServices() the event’s data
/// structure and notification function need to be allocated from runtime
/// memory.
runtime: bool = false,
timer: bool = false,

/// This event should not be combined with any other event types. This event
/// type is functionally equivalent to the EFI_EVENT_GROUP_EXIT_BOOT_SERVICES
/// event group.
pub const signal_exit_boot_services: EventType = .{
.signal = true,
.lo_context = 1,
};

/// The event is to be notified by the system when SetVirtualAddressMap()
/// is performed. This event type is a composite of EVT_NOTIFY_SIGNAL,
/// EVT_RUNTIME, and EVT_RUNTIME_CONTEXT and should not be combined with
/// any other event types.
pub const signal_virtual_address_change: EventType = .{
.runtime = true,
.hi_context = 0x20000,
.signal = true,
.lo_context = 2,
};
};

/// The calling convention used for all external functions part of the UEFI API.
pub const cc: std.builtin.CallingConvention = switch (@import("builtin").target.cpu.arch) {
.x86_64 => .{ .x86_64_win = .{} },
Expand All @@ -52,15 +94,19 @@ pub const IpAddress = extern union {

/// GUIDs are align(8) unless otherwise specified.
pub const Guid = extern struct {
time_low: u32,
comptime {
std.debug.assert(std.mem.Alignment.of(Guid) == .@"8");
}

time_low: u32 align(8),
time_mid: u16,
time_high_and_version: u16,
clock_seq_high_and_reserved: u8,
clock_seq_low: u8,
node: [6]u8,

/// Format GUID into hexadecimal lowercase xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx format
pub fn format(self: @This(), writer: *std.io.Writer) std.io.Writer.Error!void {
pub fn format(self: Guid, writer: *std.io.Writer) std.io.Writer.Error!void {
const time_low = @byteSwap(self.time_low);
const time_mid = @byteSwap(self.time_mid);
const time_high_and_version = @byteSwap(self.time_high_and_version);
Expand All @@ -75,7 +121,7 @@ pub const Guid = extern struct {
});
}

pub fn eql(a: std.os.uefi.Guid, b: std.os.uefi.Guid) bool {
pub fn eql(a: Guid, b: Guid) bool {
return a.time_low == b.time_low and
a.time_mid == b.time_mid and
a.time_high_and_version == b.time_high_and_version and
Expand Down
24 changes: 14 additions & 10 deletions lib/std/os/uefi/pool_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@ const UefiPoolAllocator = struct {

const full_len = metadata_len + len;

var unaligned_ptr: [*]align(8) u8 = undefined;
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, full_len, &unaligned_ptr) != .success) return null;
const unaligned_slice = uefi.system_table.boot_services.?.allocatePool(
uefi.efi_pool_memory_type,
full_len,
) catch return null;

const unaligned_addr = @intFromPtr(unaligned_ptr);
const unaligned_addr = @intFromPtr(unaligned_slice.ptr);
const aligned_addr = mem.alignForward(usize, unaligned_addr + @sizeOf(usize), ptr_align);

const aligned_ptr = unaligned_ptr + (aligned_addr - unaligned_addr);
getHeader(aligned_ptr).* = unaligned_ptr;
const aligned_ptr = unaligned_slice.ptr + (aligned_addr - unaligned_addr);
getHeader(aligned_ptr).* = unaligned_slice.ptr;

return aligned_ptr;
}
Expand Down Expand Up @@ -76,7 +78,7 @@ const UefiPoolAllocator = struct {
) void {
_ = alignment;
_ = ret_addr;
_ = uefi.system_table.boot_services.?.freePool(getHeader(buf.ptr).*);
uefi.system_table.boot_services.?.freePool(getHeader(buf.ptr).*) catch unreachable;
}
};

Expand Down Expand Up @@ -117,10 +119,12 @@ fn uefi_alloc(

std.debug.assert(@intFromEnum(alignment) <= 3);

var ptr: [*]align(8) u8 = undefined;
if (uefi.system_table.boot_services.?.allocatePool(uefi.efi_pool_memory_type, len, &ptr) != .success) return null;
const slice = uefi.system_table.boot_services.?.allocatePool(
uefi.efi_pool_memory_type,
len,
) catch return null;

return ptr;
return slice.ptr;
}

fn uefi_resize(
Expand Down Expand Up @@ -161,5 +165,5 @@ fn uefi_free(
) void {
_ = alignment;
_ = ret_addr;
_ = uefi.system_table.boot_services.?.freePool(@alignCast(buf.ptr));
uefi.system_table.boot_services.?.freePool(@alignCast(buf.ptr)) catch unreachable;
}
Loading
Loading