switching to new pagemap causes 0xe and more exceptions

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

theres a lot of vmm region corruption going on here

i might just give up at this point idfk
MichaelPetch
Member
Member
Posts: 791
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: switching to new pagemap causes 0xe and more exceptions

Post by MichaelPetch »

I am still baffled by your find_pte even after your changes which really didn't do anything. You have

Code: Select all

uint64_t *find_pte(uint64_t *pt, uint64_t virt) {
  uint64_t shift = 48;
  for (int i = 0; i < 4; i++) {
    shift -= 9;
    uint64_t idx = (virt >> shift) & 0x1ff;
    uint64_t *page_table =
        (uint64_t *)((uint64_t)pt + hhdm_request.response->offset);
    if (i == 2) {
      return page_table + idx;
    }
    if (!(page_table[idx] & PRESENT)) {
      if (page_table[idx] & PAGE2MB) {
        panic("This shall not happen.");
      }
      return page_table + idx;
    } else {
      pt = (uint64_t *)(page_table[idx] & 0x000ffffffffff000);
    }
  }
  return 0;
}
For instance:

Code: Select all

    if (i == 2) {
      return page_table + idx;
    }
Why do you use `i == 2` and stop at the Page Directory? What happens when you have a 4KiB page? wouldn't you have to descend one level further to the Page Table??
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

ill remove that right now
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

its changed look at git
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

so now im trying to implment ksan and theres a bunch of faults going on

read my code its in src/mem/ksan.c
MichaelPetch
Member
Member
Posts: 791
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: switching to new pagemap causes 0xe and more exceptions

Post by MichaelPetch »

I have made a pull request with a fix for `find_pte` here: https://github.com/rayanmargham/NyauxKC/pull/1 . The pull request disables your ksan. I didn't have time to investigate the reason but with your ksan implementation the stack overflows and the result is the pml4_table is corrupted and a page fault occurs.
MichaelPetch
Member
Member
Posts: 791
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: switching to new pagemap causes 0xe and more exceptions

Post by MichaelPetch »

I discovered another bug in `vmm_init` . I had noticed page faults when building with `-O0` and discovered a bug in a switch statement:

Code: Select all

    default:
      uint64_t disalignz = entry->base % 2097152;
      entry->base = align_down(entry->base, 2097152);
      uint64_t page_amountz =
          align_up(entry->length - disalignz, 2097152) / 2097152;
      for (uint64_t j = 0; j != page_amountz; j++) {
        map2mb(ker_map.pml4, entry->base + (j * 2097152),
               hhdm_request.response->offset + entry->base + (j * 2097152),
               PRESENT | RWALLOWED);
      }
      hhdm_pages += page_amount;
      break;
    }
The bug is specifically `hhdm_pages += page_amount;`. This should be `hhdm_pages += page_amountz;`. It is possible to use an uninitialized variable defined in another case statement (at the same scope). This is allowed in older versions of C. I have updated my pull request with a bug fix that fixes this issue in a cleaner way by putting the case statements in a new scope using {}. My pull request is here: https://github.com/rayanmargham/NyauxKC/pull/1
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

merged!
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

thank you so much!!!! actually. everything is solved (except for ksan) do u have any idea why ksan isnt working?
MichaelPetch
Member
Member
Posts: 791
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: switching to new pagemap causes 0xe and more exceptions

Post by MichaelPetch »

When I get a chance this weekend I will look at your kasan implementation. What I know is that the kasan issue is not related to any bugs you were currently hunting down and was a unique problem specific to that code.

I also have another pull request coming that cleans up your IDT stubs and handler, and also sets a default handler for idt entries that you aren't handling. I noticed in your code if you didn't set up a handler your unhandled interrupts would cause yet another exception leading to possible double and triple faults.

Edit: New pull request is here: https://github.com/rayanmargham/NyauxKC/pull/2
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

Thank you! merged
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

Let me know when u get a chance to look at ksan, Thanks!
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

any update?
MichaelPetch
Member
Member
Posts: 791
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: switching to new pagemap causes 0xe and more exceptions

Post by MichaelPetch »

Didn't have time. You'll have to investigate.
RayanMargham
Member
Member
Posts: 43
Joined: Tue Jul 05, 2022 12:37 pm

Re: switching to new pagemap causes 0xe and more exceptions

Post by RayanMargham »

also ive found a bug

the handler for page fault says its a unhandled exception... what???? even tho its changed????
Post Reply