Skip to content

Conversation

@mazunki
Copy link
Contributor

@mazunki mazunki commented Oct 28, 2025

The old kernel/memory.hpp had several responsibilities, and albeit not being super long it felt hard to read. It comprised at least two fairly different parts:

  • Setting up a mapping from virtual memory to physical pages (now mem/vmap.hpp)
  • Setting up the allocator for the heap (now mem/alloc.hpp)

Furthermore, I've also created mem/flags.hpp since it doesn't quite fit either of them entirely, and it facilitates plans going forward.

Currently, we're assuming architecture-specific/hardware protection flags match IncludeOS-allocation flags. While this is currently the case, I do intend to change this in the future for the sake of type safety. src/arch/x86_64/paging.cpp uses os::mem::Access referring to hardware flags, while src/kernel/multiboot.cpp or src/kernel/elf.cpp refers to IncludeOS semantics. I intend to create a per-architecture translation mapping for this (constexpr, so it won't affect any performance, of course).

The responsibility separation between mem/vmap and mem/alloc seems to be represented in the usages across the code too (most of the results are non-overlapping):

$ rg mem/vmap.hpp | wc -l
18
$ rg mem/alloc.hpp | wc -l
14

This PR essentially makes #2329 obsolete.

Copy link
Contributor

@alfreb alfreb left a comment

Choose a reason for hiding this comment

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

This does a lot more than it says on the label. Adds a new allocator without any tests, introduces duplicate string representations of error codes, introduces new mmap functionality in a C patch - lots of things. We can't bundle all this together.

Mapping<Fl>::Mapping(uintptr_t linear, uintptr_t physical, Fl fl, size_t sz)
: lin{linear}, phys{physical}, flags{fl}, size{sz},
page_sizes{any_size} {}
inline Mapping<Fl>::Mapping(uintptr_t linear, uintptr_t physical, Fl fl, size_t sz)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why inline this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't seem to matter anymore, but I was running into ODR issues at one point. My understanding is that this is redundant but equivalent now.

api/mem/vmap.hpp Outdated

const bool isseq = __builtin_popcount(page_sizes) == 1;

std::string s = std::format( "lin {} -> phys {}, size {}, flags {:#x}",
Copy link
Contributor

Choose a reason for hiding this comment

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

So using std::string means this will only work if the default allocator is ready at this point, because constructing a string allocates. Are we going to commit to that or is there a scenario where you'd want to do memory mapping before the default allocator is ready?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for bringing this up, this could be a reason why I was running into some trouble later down. That said, I didn't intend to push this here, so this isn't relevant at this point.

+void *__includeos_mmap(void *start, size_t len, int prot, int flags, int fd, off_t off)
+{
+ long ret;
+ if (flags & MAP_FIXED) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this functionality in the patch and not in our mmap implementation? I don't remember what __vm_wait does, so I don't understand this, sorry. Please explain.

Copy link
Contributor Author

@mazunki mazunki Nov 8, 2025

Choose a reason for hiding this comment

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

Again, not relevant to this PR. Didn't intend to push this.

That said, __vm_wait() is used by the original musl code, I am just mimicking that behaviour. I didn't dig too deep into why it's necessary. The patch fixes errno return codes, but I might have a better solution for it anyway.

auto* res = kalloc(length);
void *res = nullptr;

if (util::has_flag(flags, Flags::FixedOverride) or util::has_flag(flags, Flags::FixedFriendly)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait - this is not just reorganizing stuff it it? Is this copy-pasted in from somewhere else or is it new functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, these changes will come later.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a new allocator? The PR says that this is just splitting a header into multiple parts. If you want to commit a new allocator that has to come in its own PR with unit tests and also be plugged in to an integration test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, you're right. Sorry about that. Commit 9d8cc5e seems to have included a bunch of testing stuff I didn't intend to include. Let me fix that.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is reinventing strerror - please separate out if you think that's the right way to go and explain why.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. I did not intend to add this here. The difference between this and strerror is tracked in #2338.

@mazunki
Copy link
Contributor Author

mazunki commented Nov 8, 2025

Welp, this should be much cleaner and easier to review now. I was continuing work on the same branch without branching out, and accidentally pushed some unfinished changes here without realizing.

Sign up for free to 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.

2 participants