  {
      se_mutex_lock(&s_device_mutex);
  
-@@ -382,22 +385,38 @@
+@@ -382,22 +385,48 @@
          }
          //The in-kernel driver does not do the base and size alignment. This is up to user mode to do it. 
          //Therefore enclave_size will be virtual_size*2. The unused region will be released by calling munmap later.

             if (status != SGX_SUCCESS) {
                 if (enclave_error != NULL)
                     *enclave_error = error_aesm2api(status);
@@ -842,6 +853,13 @@
     }
 
     se_mutex_lock(&s_enclave_mutex);

     {
         uint64_t aligned_addr = ((uint64_t)enclave_base + virtual_size - 1) & ~(virtual_size - 1);
         if(aligned_addr != (uint64_t)enclave_base)
@@ -566,10 +595,11 @@
     if (sec_info.flags & ENCLAVE_PAGE_UNVALIDATED)
         sec_info.flags ^= ENCLAVE_PAGE_UNVALIDATED;
 
+    int hfile = -1;
     size_t pages = target_size / SE_PAGE_SIZE;
     if (s_driver_type == SGX_DRIVER_IN_KERNEL)
     {
-        int hfile = get_file_handle_from_address(target_address);
+        hfile = get_file_handle_from_address(target_address);
 
         //todo - may need to check EADD parameters for better error reporting since the driver 
         //  may not do this (the driver will just take a fault on EADD)
@@ -699,8 +729,15 @@
     void* next_page = (void*)((uint64_t)enclave_mem_region->addr + (uint64_t)enclave_mem_region->len);
     if ((enclave_mem_region->prot != prot) || (target_address != next_page)) {
         if (enclave_mem_region->addr != 0) {
-            //the new load of enclave data either has a different protection or is not contiguous with the last one, mprotect the range stored in memory region structure
-            int ret = mprotect(enclave_mem_region->addr, enclave_mem_region->len, enclave_mem_region->prot);
+            //the new load of enclave data either has a different protection or is not contiguous with the last one, mprotect/mmap the range stored in memory region structure
+            int ret = 0;
+            if (hfile != -1) {
+                if (MAP_FAILED == mmap(enclave_mem_region->addr, enclave_mem_region->len,
+                    enclave_mem_region->prot, MAP_SHARED | MAP_FIXED, hfile, 0))
+                    ret=-1;
+            }
+            else
+                ret = mprotect(enclave_mem_region->addr, enclave_mem_region->len, enclave_mem_region->prot);
             if (0 != ret) {
                 if (enclave_error != NULL)
                     *enclave_error = ENCLAVE_UNEXPECTED;
@@ -742,6 +779,18 @@
             *enclave_error = ENCLAVE_INVALID_PARAMETER;
         return false;
     }
+    int hfile = -1;
+    if (s_driver_type == SGX_DRIVER_IN_KERNEL)
+    {
+        hfile = get_file_handle_from_address(base_address);
+        if (hfile == -1)
+        {
+            SE_TRACE(SE_TRACE_WARNING, "\nSGX_IOC_ENCLAVE_INIT failed - %p is not a valid enclave \n", base_address);
+            if (enclave_error != NULL)
+                *enclave_error = ENCLAVE_INVALID_ADDRESS;
+            return false;
+        }
+    }
 
     const enclave_init_sgx_t* enclave_init_sgx = (const enclave_init_sgx_t*)info;
     if (info_size == 0 || sizeof(*enclave_init_sgx) != info_size) {
@@ -755,8 +804,15 @@
     auto enclave_mem_region = &s_enclave_mem_region[base_address];
     se_mutex_unlock(&s_enclave_mutex);
     if (enclave_mem_region->addr != 0) {
-        //the new load of enclave data either has a different protection or is not contiguous with the last one, mprotect the range stored in memory region structure
-        if (0 != mprotect(enclave_mem_region->addr, enclave_mem_region->len, enclave_mem_region->prot)) {
+       //the new load of enclave data either has a different protection or is not contiguous with the last one, mprotect/mmap the range stored in memory region structure
+       int ret = 0;
+       if (hfile != -1) {
+           if (MAP_FAILED == mmap(enclave_mem_region->addr, enclave_mem_region->len, enclave_mem_region->prot, MAP_SHARED | MAP_FIXED, hfile, 0))
+               ret=-1;
+       }
+       else
+           ret= mprotect(enclave_mem_region->addr, enclave_mem_region->len, enclave_mem_region->prot);
+       if (0 != ret) {
             if (enclave_error != NULL)
                 *enclave_error = ENCLAVE_UNEXPECTED;
             return 0;
@@ -784,15 +840,7 @@
 
         enclave_css_t* enclave_css = (enclave_css_t*)enclave_init_sgx->sigstruct;
         if (0 == enclave_css->header.hw_version) {
