Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev
, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
std.os.uefi.tables: ziggify boot and runtime services by dotcarmen · Pull Request #23441 · ziglang/zig · GitHub
Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev
, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' std.os.uefi.tables: ziggify boot and runtime services by dotcarmen · Pull Request #23441 · ziglang/zig · GitHub
Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev
, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' std.os.uefi.tables: ziggify boot and runtime services by dotcarmen · Pull Request #23441 · ziglang/zig · GitHub
Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev
, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' std.os.uefi.tables: ziggify boot and runtime services by dotcarmen · Pull Request #23441 · ziglang/zig · GitHub
Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev
, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' std.os.uefi.tables: ziggify boot and runtime services by dotcarmen · Pull Request #23441 · ziglang/zig · GitHub
Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev
, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); std.os.uefi.tables: ziggify boot and runtime services by dotcarmen · Pull Request #23441 · ziglang/zig · GitHub
Skip to content

std.os.uefi.tables: ziggify boot and runtime services - #23441

Merged
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services
Jul 12, 2025
Merged

std.os.uefi.tables: ziggify boot and runtime services#23441
linusg merged 41 commits into
ziglang:masterfrom
dotcarmen:ziggify-uefi-boot-runtime-services

Conversation

@dotcarmen

Copy link
Copy Markdown
Contributor

this PR updates BootServices and RuntimeServices to an idiomatic interface similar to the pattern used in std.os.uefi.protocol. Part of my crusade towards making std.os.uefi more idiomatic

@dotcarmen

ghost commented Apr 2, 2025

Copy link
Copy Markdown
ContributorAuthor

@linusg
image

@linusg

ghost commented Apr 2, 2025

Copy link
Copy Markdown
Collaborator

another-one.png

It is appreciated! :^)

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from ee41a75 to e4487d5CompareApril 2, 2025 17:22

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a thorough review, LGTM at a high level

Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@linusglinusg mentioned this pull request Apr 2, 2025

/// Returns the current memory map.
getMemoryMap: *const fn (mmap_size: *usize, mmap: ?[*]MemoryDescriptor, map_key: *usize, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,
_getMemoryMap: *const fn (mmap_size: *usize, mmap: [*]MemoryDescriptor, map_key: *MemoryMapKey, descriptor_size: *usize, descriptor_version: *u32) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i figured it's safer to obscure map_key values since as far as i can tell it's meant to be assigned by the system (only retrievable via getMemoryMap)

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
interfaces: anytype,
) InstallProtocolInterfacesError!Handle {
var hdl: ?Handle = handle;
const args_tuple = protocolInterfaces(&hdl, interfaces);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

i couldn't find an easier way to manage dynamically constructing a tuple than this helper :/

};
};

fn protocolInterfaces(

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is kinda gross :/ suggestions are welcome

Comment threadlib/std/os/uefi/tables/boot_services.zig

ghost left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A few minor nitpicks, but since we're overhauling boot services it's time we remove [*]MemoryDescriptor and []MemoryDescriptor from std.os.uefi, they are never valid because the size of the zig struct is almost never the size of the descriptor the firmware returns.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines -35 to +48
raiseTpl: *const fn (new_tpl: usize) callconv(cc) usize,
raiseTpl: *const fn (new_tpl: TaskPriorityLevel) callconv(cc) TaskPriorityLevel,

/// Restores a task's priority level to its previous value.
restoreTpl: *const fn (old_tpl: usize) callconv(cc) void,
restoreTpl: *const fn (old_tpl: TaskPriorityLevel) callconv(cc) void,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

doesn't make sense to define wrappers for these functions imo

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
_createEventEx: *const fn (type: u32, notify_tpl: usize, notify_func: EventNotify, notify_ctx: *const anyopaque, event_group: *align(8) const Guid, event: *Event) callconv(cc) Status,

/// Opens a protocol with a structure as the loaded image for a UEFI application
pub fn openProtocolSt(self: *BootServices, comptime protocol: type, handle: Handle) !*protocol {

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

this function is now basically the handleProtocol function

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 50823da to 0733531CompareApril 4, 2025 14:02
Comment threadlib/std/Thread.zig
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/debug.zig
// 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
Copy Markdown
ContributorAuthor

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

@dotcarmen

ghost commented Apr 4, 2025

Copy link
Copy Markdown
ContributorAuthor

i feel like this is ready for a final review :) i haven't tried it with my own project yet though, i'll tackle that later

_setWatchdogTimer: *const fn (timeout: usize, watchdog_code: u64, data_size: usize, watchdog_data: ?[*]const u16) callconv(cc) Status,

/// Connects one or more drives to a controller.
_connectController: *const fn (controller_handle: Handle, driver_image_handle: ?[*:null]Handle, remaining_device_path: ?*const DevicePathProtocol, recursive: bool) callconv(cc) Status,

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

really feels like [*:null]*anyopaque should work :(

Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/pool_allocator.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen

ghost commented Apr 10, 2025

Copy link
Copy Markdown
ContributorAuthor

haven't forgotten or abandoned this, I'm dealing with immigration stuffs :) I'll pick it up again soon

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

ok i may have gone a little overboard with 7372d65 so i partly reverted it in 04e3728 to keep the number of files in the diff small :) those align(8)s aren't an error or anything, they're just unnecessary now :)

otherwise the rest of the PR comments have been addressed!

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I decided to not keep this as *const opaque{} since it might be nice to have the namespace eventually? idk

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

I actually feel like it'd be handy to do that for Event eventually 🙃

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 46d8e68 to d37be42CompareApril 13, 2025 21:45
@dotcarmen

ghost commented Apr 14, 2025

Copy link
Copy Markdown
ContributorAuthor

ah sorry i think i rebased and didn't mean to push the rebase 😅

here's the changes link for commits from last night: https://github.com/ziglang/zig/pull/23441/files/13858228112d8ee6798dd51032728f68c512bc5f..d37be4284b844684cd1f39b2fd543d8003dcb348

Comment threadlib/std/os/uefi.zig Outdated
/// A handle to an event structure.
pub const Event = *opaque {};

pub const EventRegistration = opaque {};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The standard describes an event registration as a VOID*, and the standard provides absolutely no methods to operate on the registration value other than passing around the VOID*, so I say it makes no sense to change this from a *opaque{}.

If for some reason someone finds a good reason to make this a namespace that would be a better time to make this change.

Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables/runtime_services.zig

ghost left a comment

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

decided to include a pattern that i've been experimenting with in another PR i'm working on for std.elf, wdyt?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +25 to +32
pub const InvalidValue = math.IntFittingRange(
@intFromEnum(MemoryType.invalid_start),
@intFromEnum(MemoryType.invalid_end),
);
pub const OemValue = math.IntFittingRange(
@intFromEnum(MemoryType.oem_start),
@intFromEnum(MemoryType.oem_end),
);
pub const VendorValue = math.IntFittingRange(
@intFromEnum(MemoryType.vendor_start),
@intFromEnum(MemoryType.vendor_end),
);

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. define IntFittingRange for the relevant value range

ghostApr 18, 2025

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

oh wait the start should be 0 😅 and the end should be _start - _end 😅

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

_end - _start *

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +71 to +70
pub fn invalid(value: InvalidValue) MemoryType {
const invalid_start = @intFromEnum(MemoryType.invalid_start);
return @enumFromInt(invalid_start + value);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for constructing such values ie pub const my_invalid: MemoryType = .invalid(0x2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why are we constructing invalid memory types?

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment on lines +76 to +82
pub fn getInvalid(memtype: MemoryType) ?InvalidValue {
const as_int = @intFromEnum(memtype);
const invalid_start = @intFromEnum(MemoryType.invalid_start);
if (as_int < invalid_start) return null;
if (as_int > @intFromEnum(MemoryType.invalid_end)) return null;
return @truncate(as_int - invalid_start);
}

Copy link
Copy Markdown
ContributorAuthor

Choose a reason for hiding this comment

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

  1. function for retrieving the value relative to the range's start

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

IMO, it is also a good idea to use _allocatePage in pool_allocator if alignment is 4096, as _allocatePool is slow.

@truemedian

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

@kouosi

ghost commented Apr 18, 2025

Copy link
Copy Markdown
Contributor

Not that that's a bad idea for the special case of exactly 4K alignment, I definitely would need a source to back up that allocatePool is significantly slower than allocatePages on at least a few firmware UEFI implementations

I don't have an exact source to back this up, but testing on some hardware I have available I found that allocatePage is significantly slower than allocatePool.

@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch from 51ed5dc to 819a6c6CompareJuly 2, 2025 11:10
@dotcarmen

ghost commented Jul 2, 2025

Copy link
Copy Markdown
ContributorAuthor

rebased to latest master :)

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Did another full review, I have only nitpicks left. Tested locally against a small UEFI app - not nearly exhaustive, but builds. Happy to merge after this!

Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi/tables.zig
Comment threadlib/std/os/uefi/tables.zig Outdated
Comment threadlib/std/os/uefi.zig
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/boot_services.zig Outdated
Comment threadlib/std/os/uefi/tables/configuration_table.zig Outdated
Comment threadlib/std/os/uefi/tables/runtime_services.zig Outdated
Comment threadlib/std/os/uefi/tables.zig Outdated
@dotcarmen
dotcarmenforce-pushed the ziggify-uefi-boot-runtime-services branch 3 times, most recently from 3065959 to 819a6c6CompareJuly 11, 2025 19:42
@dotcarmen

ghost commented Jul 11, 2025

Copy link
Copy Markdown
ContributorAuthor

^ tried to rebase on latest master but the diff got nasty, i'll merge. reverted the rebase-push

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

CI is failing

@dotcarmen

ghost commented Jul 12, 2025

Copy link
Copy Markdown
ContributorAuthor

yup, just saw that lol, looking into it now

@linusg

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Collaborator

Thanks for your patience with this! Let's merge 🎉

@linusg
linusg enabled auto-merge (squash) July 12, 2025 11:40
@linusg
linusg merged commit 5b4e982 into ziglang:masterJul 12, 2025
@RossComputerGuy

ghost commented Jul 12, 2025

Copy link
Copy Markdown
Contributor

LFG, glad to see this. I hope we can get #22226 next. I'll rebase it tonight after work, feel free to review.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants

@dotcarmen@linusg@kouosi@truemedian@RossComputerGuy@Khitiara@bnuuydev