freebsd-src icon indicating copy to clipboard operation
freebsd-src copied to clipboard

Centralize the kernel and module metadata types

Open VexedUXR opened this issue 1 year ago • 8 comments

Currently, the way we get the kernel metadata pointer is by calling preload_search_by_type with one of the following three: "elf kernel", "elf32 kernel" and "elf64 kernel". Which one(s) we use isn't consistent though. Sometimes we would only try "elf kernel", and other times we would try one of the latter two if the first failed. However, the loader only ever sets "elf kernel" as the kernel type. See: https://github.com/freebsd/freebsd-src/blob/8afae0caf4c4816eb56b732fcd1a4b185e86098a/stand/common/load_elf.c#L92

Instead, make the kmdp a global, preload_kmdp, and initialize it using preload_initkmdp in machdep.c (or machdep_boot.c on arm/64). preload_initkmdp takes a single boolean argument that tells us whether not finding kmdp is fatal or not.

Also, feel free to critique my commit message. I feel like it's not great, but that's the best I could word it.

VexedUXR avatar Aug 22 '24 19:08 VexedUXR

Fixed some style warnings

VexedUXR avatar Aug 22 '24 20:08 VexedUXR

I'll take a look. @kostikbel and @kevans91 should take a look too.

bsdimp avatar Aug 22 '24 20:08 bsdimp

My opinion is that this is an enormous churn.

Regardless of it, I think even the approach is not taken to the logical completion. All "elf kernel" literals must be replaced by the global const string which is used by reference, both in loader and in kernel.

Ideally, we would have some enumeration value used to identify type, instead of free-typed string, but for this change at least replacing all individual strings with the same reference to constant string is the least required step.

kostikbel avatar Aug 23 '24 09:08 kostikbel

My opinion is that this is an enormous churn.

Yes, but it centralizes where we get the metadata at least. It also removes a bunch of unnecessary checks where kmdp can't be NULL. IMO most of the changes are trivial.

Regardless of it, I think even the approach is not taken to the logical completion. All "elf kernel" literals must be replaced by the global const string which is used by reference, both in loader and in kernel.

You're right, they should be. But for the kernel, after this patch, I believe the only reference to "elf kernel" would be in preload_initkmdp.

VexedUXR avatar Aug 23 '24 12:08 VexedUXR

I'll take a closer look at this. It's basically OK, but I think I have some misgivings I need to put into an actionable form.

bsdimp avatar Aug 23 '24 17:08 bsdimp

Looking more into what @kostikbel said, I've decided to move "elf kernel" and friends to sys/linker.h as macros. The reason they're macros an no longer globals is so that they can be easily shared across both the loader and kernel code.

Other preload_search_by_type calls either use macros or use a single free-typed string (i.e it only has one reference), so I think this is better.

Also, I noticed that we also check for "elfN module" and "elfN obj module", so I removed those too.

VexedUXR avatar Aug 24 '24 17:08 VexedUXR

Alright, settled on modinfo.c at the end Also, needed to make file_loadraw accept a const char * for the type string instead of a plain char *

VexedUXR avatar Aug 31 '24 01:08 VexedUXR

Update and rebase after b538d4911004ca541507166b8ec9689d2e87d1aa

VexedUXR avatar Oct 02 '24 18:10 VexedUXR

landed.

bsdimp avatar Jan 24 '25 21:01 bsdimp

Thanks!

VexedUXR avatar Jan 25 '25 05:01 VexedUXR