wbenny / hvpp

hvpp is a lightweight Intel x64/VT-x hypervisor written in C++ focused primarily on virtualization of already running operating system

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A bugfix I would like to contribute back

syslaz opened this issue · comments

@wbenny
Related to issue: #37

For some reason on 6th gen Intels and newer this does not cause any issue, however on older Intel CPUs it will cause system instability/freezing. The function below is responsible for the determining the memory cache type that each page on the EPT is configured to. The bug in the code lies in fact that the original code's default result is "memory_type::uncacheable" even though a valid mttr descriptor for that memory range was found.

I spent weeks looking into every line of hvpp until I finally stumbled upon this one bug that was fixed by adding just 3 lines.

In hvpp/lib/mttr_descriptor.h:

     memory_type type(pa_t pa) const noexcept
      {
        memory_type result = memory_type::invalid;

        for (auto mtrr_item : *this)
        {
          if (mtrr_item.range.contains(pa))
          {
            if (is_fixed(mtrr_item) || mtrr_item.type == memory_type::uncacheable)
            {
              result = mtrr_item.type;
              break;
            }

            if ( result == memory_type::write_back && (result == memory_type::write_through || mtrr_item.type == memory_type::write_through))
            {
              result = memory_type::write_through;
            } else
            {
                result = mtrr_item.type; // TODO bruhh
            }
          }
        }

        if (result == memory_type::invalid)
        {
          result = default_memory_type_;
        }

        return result;
      }

Other than that I've found that hvpp is a really well written hypervisor and has a lot of potential. Thanks!

thanks, i also find this bug