Re: [PATCH v5 10/11] xen: modify page table construction

2016-02-25 Thread Daniel Kiper
On Thu, Feb 25, 2016 at 12:33:36PM +0100, Juergen Gross wrote:
> Modify the page table construction to allow multiple virtual regions
> to be mapped. This is done as preparation for removing the p2m list
> from the initial kernel mapping in order to support huge pv domains.
>
> This allows a cleaner approach for mapping the relocator page by
> using this capability.
>
> The interface to the assembler level of the relocator has to be changed
> in order to be able to process multiple page table areas.
>
> Signed-off-by: Juergen Gross 

Reviewed-by: Daniel Kiper 

Daniel

___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


[PATCH v5 10/11] xen: modify page table construction

2016-02-25 Thread Juergen Gross
Modify the page table construction to allow multiple virtual regions
to be mapped. This is done as preparation for removing the p2m list
from the initial kernel mapping in order to support huge pv domains.

This allows a cleaner approach for mapping the relocator page by
using this capability.

The interface to the assembler level of the relocator has to be changed
in order to be able to process multiple page table areas.

Signed-off-by: Juergen Gross 
---
V4: align variables in assembly sources
use separate structure define as requested by Daniel Kiper

V3: use constants instead of numbers as requested by Daniel Kiper
add lots of comments to assembly code as requested by Daniel Kiper
---
 grub-core/lib/i386/xen/relocator.S   |  88 ++
 grub-core/lib/x86_64/xen/relocator.S | 135 ++
 grub-core/lib/xen/relocator.c|  28 ++-
 grub-core/loader/i386/xen.c  | 329 +++
 include/grub/i386/memory.h   |   7 +
 include/grub/xen/relocator.h |   6 +-
 6 files changed, 360 insertions(+), 233 deletions(-)

diff --git a/grub-core/lib/i386/xen/relocator.S 
b/grub-core/lib/i386/xen/relocator.S
index 694a54c..f8d135a 100644
--- a/grub-core/lib/i386/xen/relocator.S
+++ b/grub-core/lib/i386/xen/relocator.S
@@ -16,6 +16,8 @@
  *  along with GRUB.  If not, see .
  */
 
+#include 
+#include 
 #include 
 #include 
 
@@ -23,78 +25,86 @@
 
 VARIABLE(grub_relocator_xen_remap_start)
 LOCAL(base):
-   /* mov imm32, %ebx */
+   /* Remap the remapper to it's new address. */
+   /* mov imm32, %ebx - %ebx: new virtual address of remapper */
.byte   0xbb
 VARIABLE(grub_relocator_xen_remapper_virt)
.long   0
 
-   /* mov imm32, %ecx */
+   /* mov imm32, %ecx - %ecx: low part of page table entry */
.byte   0xb9
 VARIABLE(grub_relocator_xen_remapper_map)
.long   0
 
-   /* mov imm32, %edx */
+   /* mov imm32, %edx  - %edx: high part of page table entry */
.byte   0xba
 VARIABLE(grub_relocator_xen_remapper_map_high)
.long   0
 
-   movl%ebx, %ebp
+   movl%ebx, %ebp  /* %ebx is clobbered by hypercall */
 
-   movl$2, %esi
+   movl$UVMF_INVLPG, %esi  /* esi: flags (inv. single entry) */
movl$__HYPERVISOR_update_va_mapping, %eax
int $0x82
 
movl%ebp, %ebx
addl   $(LOCAL(cont) - LOCAL(base)), %ebx
 
-   jmp *%ebx
+   jmp *%ebx   /* Continue with new virtual address */
 
 LOCAL(cont):
-   xorl%eax, %eax
-   movl%eax, %ebp
+   /* Modify mappings of new page tables to be read-only. */
+   /* mov imm32, %eax */
+   .byte   0xb8
+VARIABLE(grub_relocator_xen_paging_areas_addr)
+   .long   0
+   movl%eax, %ebx
 1:
+   movl0(%ebx), %ebp   /* Get start pfn of the current area */
+   movlGRUB_TARGET_SIZEOF_LONG(%ebx), %ecx /* Get # of pg tables */
+   testl   %ecx, %ecx  /* 0 -> last area reached */
+   jz  3f
+   addl$(2 * GRUB_TARGET_SIZEOF_LONG), %ebx
+   movl%ebx, %esp  /* Save current area pointer */
 
+2:
+   movl%ecx, %edi
/* mov imm32, %eax */
.byte   0xb8
 VARIABLE(grub_relocator_xen_mfn_list)
.long   0
-   movl%eax, %edi
-   movl%ebp, %eax
-   movl0(%edi, %eax, 4), %ecx
-
-   /* mov imm32, %ebx */
-   .byte   0xbb
-VARIABLE(grub_relocator_xen_paging_start)
-   .long   0
-   shll$12, %eax
-   addl%eax, %ebx
+   movl0(%eax, %ebp, 4), %ecx  /* mfn */
+   movl%ebp, %ebx
+   shll$PAGE_SHIFT, %ebx   /* virtual address (1:1 mapping) */
movl%ecx, %edx
-   shll$12,  %ecx
-   shrl$20,  %edx
-   orl $5, %ecx
-   movl$2, %esi
+   shll$PAGE_SHIFT,  %ecx  /* prepare pte low part */
+   shrl$(32 - PAGE_SHIFT),  %edx   /* pte high part */
+   orl $(GRUB_PAGE_PRESENT | GRUB_PAGE_USER), %ecx /* pte low */
+   movl$UVMF_INVLPG, %esi
movl$__HYPERVISOR_update_va_mapping, %eax
-   int $0x82
+   int $0x82   /* parameters: eax, ebx, ecx, edx, esi */
 
-   incl%ebp
-   /* mov imm32, %ecx */
-   .byte   0xb9
-VARIABLE(grub_relocator_xen_paging_size)
-   .long   0
-   cmpl%ebp, %ecx
+   incl%ebp/* next pfn */
+   movl%edi, %ecx
 
-   ja  1b
+   loop2b
 
+   mov %esp, %ebx  /* restore area poniter */
+   jmp 1b
+
+3:
+   /* Switch page tables: pin new L3 pt, load cr3, unpin old L3. */
/* mov imm32, %ebx */
.byte   0xbb
 VARIABLE(grub_relocator_xen_mmu_op_addr)
.long  0
-   movl   $3, %ecx
-   movl   $0, %edx
-   movl   $0x7FF0, %esi
+   movl   $3, %ecx /* 3 mmu ops */
+   movl   $0, %edx