Skip to content
This repository was archived by the owner on Jan 28, 2023. It is now read-only.

Conversation

@AlexAltea
Copy link
Contributor

@AlexAltea AlexAltea commented Oct 19, 2018

This patch allows for easier portability of HAXM to other platforms (see #108).
Builds successfully under Windows (32-bit and 64-bit configurations) and MacOS.

Changes

Main changes are:

  • Normalized casing: s/ASSERT/assert, s/TRUE/true, s/FALSE/false, since it was inconsistent (both uppercase and lowercase variants were used across the codebase).
  • Normalized integer types: The codebase mixes (u)intN_t and (u)intN. This commit picks the former variant and consistently use it. Unnecessary typedef's to the latter variant are removed.
  • Fixed missing declarations: Defined helper functions and macros to fix errors caused by undeclared non-standard functions, e.g.: min, max.
  • Generalized page-related constants: Platform-specific headers, e.g. hax_windows.h defined page-related constants such as page_size and page_shift that are identical in all systems since they are unique to the x86 architecture. They were moved tohax_types.h.
  • Custom arch/compiler/platform macros: Created unique HAX_ARCH_*, HAX_COMPILER_*, HAX_PLATFORM_* macros: The goal is avoiding dependance in compiler/platform-specific macros throughout the codebase by creating custom macros declared at a single location.
  • Fixed function prototypes: Changing function() to function(void), fixing -Wstrict-prototypes.

Full information in the messages of the individual commits.

@AlexAltea
Copy link
Contributor Author

I think I've found a bug in core/memory.c:332-344:

#if defined(__MACH__)
#ifdef __x86_64__
            hva = (uint64_t)pmem->kva + (cur_va - pmem->uva);
#else
            hva = (uint64_t)(uint32_t)pmem->kva + (cur_va - pmem->uva);
#endif
#else   // __MACH
#if defined(_WIN64)
            hva = (uint64_t)pmem->kva + (cur_va - pmem->uva);
#else
            hva = 0;
#endif
#endif

Note how Windows (32-bit) sets hva to 0, while ironically MacOS (32-bit! which doesn't even exist) sets it to a non-zero value. Probably these two lines should be swapped. This probably hasn't affected end users since this only triggers in the absence of CONFIG_HAX_EPT2.

If it's a bug, I could easily fix it with this (which show the usefulness of the new HAX_ARCH* macros):

#ifdef HAX_ARCH_X86_64
            hva = (uint64_t)pmem->kva + (cur_va - pmem->uva);
#else
            hva = (uint64_t)(uint32_t)pmem->kva + (cur_va - pmem->uva);
#endif

- Replaced ASSERT-with-assert, TRUE-with-true, FALSE-with-false, since they were inconsistent (both uppercase and lowercase variants were used across the codebase).

- Removed uneccesary macros for uppercase variants.

Signed-off-by: Alexandro Sanchez Bach <[email protected]>
Changing `function()` to `function(void)`, fixing `-Wstrict-prototypes`.

Signed-off-by: Alexandro Sanchez Bach <[email protected]>
The codebase mixes (u)intN_t and (u)intN. This commit picks the former variant and consistently use it. Unnecessary typedef's to the latter variant are removed.

Regex: "int([0-9]+)(?![_\d])" -> "int$1_t" (Mostly accurate).
Signed-off-by: Alexandro Sanchez Bach <[email protected]>
Platform-specific headers, e.g. hax_windows.h, defined page-related constants such as page_size and page_shift that are identical in all systems since they are unique to the x86 architecture.

This commit moves the definitions to hax_types.h, and:
- Makes these macros uppercase (as per coding convention).
- Adds the `HAX_` prefix (as per coding convention). This is also necessary to prevent clashes with kernel headers.

These macros are used in all of `core`. Platform-specific code is still allowed to use platform-specific macros like the ones defined in the WDK.

Signed-off-by: Alexandro Sanchez Bach <[email protected]>
- Replaced `1 << PAGE_SHIFT` with `PAGE_SIZE`.
- Added macros for undeclared non-standard functions: `min`, `max`.

Signed-off-by: Alexandro Sanchez Bach <[email protected]>
- Moved component_index_t definition from hax_types.h to vmx.h, since it's not a type consumed outside core.

- Created unique `HAX_ARCH_*`, `HAX_COMPILER_*`, `HAX_PLATFORM_*` macros: The goal is avoiding dependance in compiler/platform-specific macros throughout the codebase by creating custom macros declared at a single location.

- Changed some (wrongly assigned as Windows/MacOS-specific) macros into MSVC/Clang-specific macros, e.g. ALIGNED(x) and PACKED.

Signed-off-by: Alexandro Sanchez Bach <[email protected]>
@AlexAltea AlexAltea force-pushed the refactor branch 3 times, most recently from 0d07ac8 to a479c8a Compare October 19, 2018 03:38
@raphaelning
Copy link
Contributor

I think I've found a bug in core/memory.c:332-344:

Thanks! Indeed there's a bug that might affect Win32 hosts in the !CONFIG_HAX_EPT2 case (now obsolete), although it's not really in the code you quoted. According to git blame, this bug was introduced long before the CONFIG_HAX_EPT2 change, but somehow it didn't really cause any big problem for our Win32 users...

Anyway, hva = 0 seems to be intended for Win32 due to a limitation on the amount of physical RAM that can be mapped into kernel space. There used to be the following logic at line 348:

#if (defined(__MACH__) || defined(_WIN64))
            if (!hax_core_set_p2m(vm, gpfn, hpfn, hva, rom))
#else
           if (!hax_core_set_p2m(vm, gpfn, hpfn, hva, rom, cur_va))
#endif

But the #else case got deleted by accident.

BTW, HAXM used to support 32-bit Mac.

Copy link
Contributor

@raphaelning raphaelning left a comment

Choose a reason for hiding this comment

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

Regex: "int([0-9]+)(?![_\d])" -> "int$1_t" (Mostly accurate).

Does "mostly" imply that there were exceptions? Just curious.

I'm done with the review. Other than a few picky comments, everything looks good. Thanks!

// MSVC
#ifdef _MSC_VER
#define HAX_COMPILER_MSVC
#define PACKED
Copy link
Contributor

Choose a reason for hiding this comment

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

Apparently MSVC doesn't have a simple equivalent for PACKED. Could you add a FIXME comment for this?

- Throwing compile-time errors if no valid architecture/compiler/platform is found.

- Replacing platform-specific macros with the new HAX_ARCH_*/HAX_COMPILER_*/HAX_PLATFORM_* macros.

Signed-off-by: Alexandro Sanchez Bach <[email protected]>
@AlexAltea
Copy link
Contributor Author

AlexAltea commented Oct 19, 2018

All fixes applied!

Regex: "int([0-9]+)(?![_\d])" -> "int$1_t" (Mostly accurate).

Does "mostly" imply that there were exceptions? Just curious.

Yeah. The regex rule correctly replaced everything, but it accidentally hit SInt32 since the Visual Studio editor does case-insensitive replacements by default (even in regex mode). Toggling case sensitivity fixes it. Additionally, the rule isn't limited to just intN and uintN but will also replace foointN to foointN_t.

That note in the commit message is a mere reminder of what I did, and also a reminder to double-check if used ever again. :-)

Copy link
Contributor

@raphaelning raphaelning left a comment

Choose a reason for hiding this comment

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

Thanks!

}

#elif defined(__MACH__)
#elif defined(HAX_PLATFORM_DARWIN)
Copy link
Contributor

Choose a reason for hiding this comment

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

I guess this is probably to workaround a compiler issue (MSVC vs. Clang), because the Mac code differs from the Windows code only in an extra pair of parentheses. But since we've finished testing the PR, you can address this in a later patch when you add Linux support.

@wcwang wcwang merged commit 245bdd6 into intel:master Oct 19, 2018
@AlexAltea AlexAltea deleted the refactor branch October 19, 2018 12:20
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants