Browse Source

project: convert hv.m to hv.c

osy 2 years ago
parent
commit
d8bf784294
2 changed files with 22 additions and 19 deletions
  1. 4 6
      Hypervisor.xcodeproj/project.pbxproj
  2. 18 13
      hv.c

+ 4 - 6
Hypervisor.xcodeproj/project.pbxproj

@@ -7,7 +7,7 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		84C505C728C94540007CE8FF /* hv.m in Sources */ = {isa = PBXBuildFile; fileRef = 84C505C328C9453F007CE8FF /* hv.m */; };
+		84C505C728C94540007CE8FF /* hv.c in Sources */ = {isa = PBXBuildFile; fileRef = 84C505C328C9453F007CE8FF /* hv.c */; };
 		84C505C828C94540007CE8FF /* hv_kernel_structs.h in Headers */ = {isa = PBXBuildFile; fileRef = 84C505C428C9453F007CE8FF /* hv_kernel_structs.h */; };
 		84C505C928C94540007CE8FF /* sysreg_offsets.h in Headers */ = {isa = PBXBuildFile; fileRef = 84C505C528C9453F007CE8FF /* sysreg_offsets.h */; };
 		84C505D628C9456C007CE8FF /* hv_demo.m in Sources */ = {isa = PBXBuildFile; fileRef = 84C505C628C94540007CE8FF /* hv_demo.m */; };
@@ -66,7 +66,7 @@
 
 /* Begin PBXFileReference section */
 		84C505B928C944C8007CE8FF /* Hypervisor.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Hypervisor.framework; sourceTree = BUILT_PRODUCTS_DIR; };
-		84C505C328C9453F007CE8FF /* hv.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = hv.m; sourceTree = "<group>"; };
+		84C505C328C9453F007CE8FF /* hv.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = hv.c; sourceTree = "<group>"; };
 		84C505C428C9453F007CE8FF /* hv_kernel_structs.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = hv_kernel_structs.h; sourceTree = "<group>"; };
 		84C505C528C9453F007CE8FF /* sysreg_offsets.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = sysreg_offsets.h; sourceTree = "<group>"; };
 		84C505C628C94540007CE8FF /* hv_demo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = hv_demo.m; sourceTree = "<group>"; };
@@ -117,7 +117,7 @@
 				84C505FB28C9476E007CE8FF /* Modules */,
 				84C505C628C94540007CE8FF /* hv_demo.m */,
 				84C505C428C9453F007CE8FF /* hv_kernel_structs.h */,
-				84C505C328C9453F007CE8FF /* hv.m */,
+				84C505C328C9453F007CE8FF /* hv.c */,
 				84C5068128C9591A007CE8FF /* hv.entitlements */,
 				84C505C528C9453F007CE8FF /* sysreg_offsets.h */,
 				84C505BA28C944C8007CE8FF /* Products */,
@@ -313,7 +313,7 @@
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				84C505C728C94540007CE8FF /* hv.m in Sources */,
+				84C505C728C94540007CE8FF /* hv.c in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -458,7 +458,6 @@
 		84C505C128C944C8007CE8FF /* Debug */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				CLANG_ENABLE_OBJC_ARC = NO;
 				CODE_SIGN_STYLE = Automatic;
 				CURRENT_PROJECT_VERSION = 1;
 				DEFINES_MODULE = YES;
@@ -491,7 +490,6 @@
 		84C505C228C944C8007CE8FF /* Release */ = {
 			isa = XCBuildConfiguration;
 			buildSettings = {
-				CLANG_ENABLE_OBJC_ARC = NO;
 				CODE_SIGN_STYLE = Automatic;
 				CURRENT_PROJECT_VERSION = 1;
 				DEFINES_MODULE = YES;

+ 18 - 13
hv.m → hv.c

@@ -1,10 +1,12 @@
 // Decompiled by hand (based-ish on a Ghidra decompile) from Hypervisor.framework on macOS 12.0b1
 // 06/09/22: updated for 12.5.1
-@import Darwin;
-@import Dispatch;
-#include <Hypervisor/Hypervisor.h>
 #include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <dispatch/dispatch.h>
+#include <mach/vm_types.h>
 #include "hv_kernel_structs.h"
+#include "hv_vm_types.h"
 
 static_assert(sizeof(hv_vcpu_exit_t) == 0x20, "hv_vcpu_exit");
 
@@ -85,7 +87,7 @@ struct hv_vcpu_data_feature_regs {
 
 static hv_return_t _hv_vcpu_config_get_feature_regs(
     struct hv_vcpu_data_feature_regs* feature_regs) {
-  hv_capabilities_t* caps = nil;
+  hv_capabilities_t* caps = NULL;
   hv_return_t err = _hv_get_capabilities(&caps);
   if (err) {
     return err;
@@ -164,13 +166,13 @@ hv_return_t hv_vm_map(void* addr, hv_ipa_t ipa, size_t size, hv_memory_flags_t f
 
 hv_return_t hv_vm_unmap(hv_ipa_t ipa, size_t size) {
   struct hv_vm_map_kernel_args args = {
-      .addr = nil, .ipa = ipa, .size = size, .flags = 0, .asid = 0};
+      .addr = NULL, .ipa = ipa, .size = size, .flags = 0, .asid = 0};
   return hv_trap(HV_CALL_VM_UNMAP, &args);
 }
 
 hv_return_t hv_vm_protect(hv_ipa_t ipa, size_t size, hv_memory_flags_t flags) {
   struct hv_vm_map_kernel_args args = {
-      .addr = nil, .ipa = ipa, .size = size, .flags = flags, .asid = 0};
+      .addr = NULL, .ipa = ipa, .size = size, .flags = flags, .asid = 0};
   return hv_trap(HV_CALL_VM_PROTECT, &args);
 }
 
@@ -238,7 +240,7 @@ hv_return_t hv_vcpu_create(hv_vcpu_t* vcpu, hv_vcpu_exit_t** exit, hv_vcpu_confi
   if (args.output_vcpu_zone->ro.ver != kHvVcpuMagic) {
     printf("Invalid magic! expected %llx, got %llx\n", kHvVcpuMagic, args.output_vcpu_zone->ro.ver);
 #ifndef USE_KERNEL_BYPASS_CHECKS
-    hv_trap(HV_CALL_VCPU_DESTROY, nil);
+    hv_trap(HV_CALL_VCPU_DESTROY, NULL);
     pthread_mutex_unlock(&vcpus_mutex);
     return HV_UNSUPPORTED;
 #else
@@ -265,21 +267,24 @@ hv_return_t hv_vcpu_create(hv_vcpu_t* vcpu, hv_vcpu_exit_t** exit, hv_vcpu_confi
   // No, I don't know why Apple doesn't just use HDFGRTR_EL2 or MDCR_EL2
   vcpu_data->vcpu_zone->rw.controls.hacr_el2 |= 1ull << 56;
   // TID3: trap the feature regs so we can handle these ourselves
-  // TODO(zhuowei): or not... we don't handle these yet!
-  // vcpu_data->vcpu_zone->rw.controls.hcr_el2 |= 0x40000ull;
-  // TODO(zhuowei): if ro hacr has a bit set, clear rw hcr_el2 TIDCP?!
+  vcpu_data->vcpu_zone->rw.controls.hcr_el2 |= 0x40000ull;
+  // if ro hacr has a bit set, clear rw hcr_el2 TIDCP?!
+  if ((vcpu_data->vcpu_zone->ro.controls.hacr_el2 >> 4 & 1) != 0) {
+    vcpu_data->vcpu_zone->rw.controls.hcr_el2 &= ~0x100000;
+  }
+  vcpu_data->vcpu_zone->rw.controls.hcr_el2 |= 0x80000;
   vcpu_data->vcpu_zone->rw.state_dirty |= 0x4;
   return 0;
 }
 
 hv_return_t hv_vcpu_destroy(hv_vcpu_t vcpu) {
-  kern_return_t err = hv_trap(HV_CALL_VCPU_DESTROY, nil);
+  kern_return_t err = hv_trap(HV_CALL_VCPU_DESTROY, NULL);
   if (err) {
     return err;
   }
   pthread_mutex_lock(&vcpus_mutex);
   struct hv_vcpu_data* vcpu_data = &vcpus[vcpu];
-  vcpu_data->vcpu_zone = nil;
+  vcpu_data->vcpu_zone = NULL;
   vcpu_data->pending_interrupts = 0;
   pthread_mutex_unlock(&vcpus_mutex);
   return 0;
@@ -299,7 +304,7 @@ hv_return_t hv_vcpu_run(hv_vcpu_t vcpu) {
   }
   vcpu_data->timer_enabled = vcpu_data->vcpu_zone->rw.controls.timer & 1;
   while (true) {
-    hv_return_t err = hv_trap(HV_CALL_VCPU_RUN, nil);
+    hv_return_t err = hv_trap(HV_CALL_VCPU_RUN, NULL);
     if (err) {
       return err;
     }