Re: [PATCH 0/2] templates/linux_xen: Properly handle multiple initrd files

2022-08-12 Thread Juergen Gross via Grub-devel
(module_map, mbi->mods_count);
 if ( bitmap_weight(module_map, mbi->mods_count) > 1 )
 printk(XENLOG_WARNING
"Multiple initrd candidates, picking module #%u\n",
initrdidx);
 ...
 /*
  * ... The second module, if present, is an initrd ramdisk.
  */
 dom0 = create_dom0(mod, modules_headroom,
initrdidx < mbi->mods_count ? mod + initrdidx : 
NULL,
kextra, loader);
 ...

Mauricio Faria de Oliveira (2):
   templates/linux_xen: Properly load multiple initrd files
   templates/linux_xen: Properly order the multiple initrd files

  util/grub.d/20_linux_xen.in | 11 +++
  1 file changed, 7 insertions(+), 4 deletions(-)



For both patches:

Acked-by: Juergen Gross 


Juergen


OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: grub2 + xen + lz4 kernels

2021-12-01 Thread Juergen Gross via Grub-devel

On 01.12.21 20:24, Shaun Reitan wrote:
Hi Daniel thanks for your reply!  You mentioned finding a new LZ4 
library but grub2 already looks to support lz4 compressed kernels. The 
issue is that they don't look to be supported under the Xen platform 
with a target of x86_64.


Grub does "support" lz4 compressed kernels by letting them decompress
themselves.

This can't work with Xen PV mode as grub needs to access the ELF notes
in the compressed part of the kernel, so grub needs to do some
decompressing itself.


Juergen


OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: grub2 + xen + lz4 kernels

2021-11-29 Thread Juergen Gross via Grub-devel

On 30.11.21 00:25, Shaun Reitan wrote:
I currently use XEN to boot PV (paravirt) virtual server instances for 
our customers. Grub2 introduced support for booting a xen kernel 
directly from a guests disk image which has worked great for years. We 
use the following command to build our image


grub-mkstandalone -O x86_64-xen -o grub2-x86_64.gz boot/grub/grub.cfg

What we have been seeing more and more is newer distros like Ubuntu 
20.04 using lz4 compressed kernel images which will not boot and 
displays the error "not xen image."


If i use the 
https://raw.githubusercontent.com/torvalds/linux/master/scripts/extract-vmlinux 
 utility 
to decompress the kernel image I am able to boot it without any issues 
using our current grub2 xen loader.


My question is how can I get lz4 support added into grub2 for xen? We 
are willing to pay for a dev to add this support if needed as I have 
limited time to really dig into this.


There are a few workarounds out their, most involve a hook that 
decompresses the kernel after an update but I'm not wanting to add 
complexity to the kernel update process. It leaves too much room for 
error and the possibility of a clients server failing to boot after what 
should of been a simple kernel upgrade.


Any help, even if just pointing me in the right direction would be 
appreciated!


Hmm, Grub2 already supports some compression methods, so adding LZ4
shouldn't be that hard. Especially as there already is some LZ4
support hidden in the ZFS handling.

I guess this LZ4 support wants to be put into a grub module of its
own and then be added to the filter list.

You should have a look at:

- GRUB_FILE_FILTER_GZIO for an example how a compression filter is
  added via grub_file_filter_register()

- the file grub-core/fs/zfs/zfs_lz4.c for current LZ4 support in
  Grub2

This should basically do the job.

I've added Daniel to Cc: as he might have some more thoughts.


Juergen


OpenPGP_0xB0DE9DD628BF132F.asc
Description: OpenPGP public key


OpenPGP_signature
Description: OpenPGP digital signature
___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel


Re: GRUB Xen PVH chainloading

2019-01-07 Thread Juergen Gross
On 07/01/2019 12:41, Colin Watson wrote:
> Hi,
> 
> I'm working on integrating the recently-merged PVH support for GRUB into
> the Debian packages.  As a result I find myself thinking about how to
> handle the two-stage boot loader scheme that our packages currently
> implement for PV.  I think that it would not be very hard to do this for
> PVH in the manner outlined below, but my x86 asm skills aren't quite up
> to some of the work needed in GRUB.  Assuming that nobody sees any
> obvious holes in this, does anyone fancy giving it a go?

Seems to be a very good idea.

> Background
> --
> 
> Around the time PV support was implemented in GRUB 2, we put together a
> scheme to minimise the coupling between the guest configuration file on
> the host and the boot loader configuration in the guest.  The scheme and
> its rationale are described here:
> 
>   
> https://wiki.xen.org/wiki/PvGrub2#Chainloading_guest_pvgrub2_from_domain_0_pvgrub2
> 
> Essentially the same rationale applies to the PVH case: it should be
> possible for the guest to declare its own booting arrangements (though
> of course some hosts may wish to just provide a grub.cfg and handle all
> that on the host side), and this should be decoupled from the GRUB image
> provided by the host as far as possible in order to minimise
> compatibility issues.
> 
> There seems to be no obstacle to this in principle: just as a PV boot
> loader can chainload another one from the guest's disk, so too could a
> PVH boot loader chainload another one from the guest's disk.
> 
> What needs to be done
> -
> 
> GRUB needs to support chainloading another PVH boot loader.  I think
> this involves observing the existence of the XEN_ELFNOTE_PHYS32_ENTRY
> note and following the machine state rules for the domain builder here:
> 
>   https://xenbits.xen.org/docs/unstable-staging/misc/pvh.html
> 
> (I had a brief go at implementing this, but foundered on my fairly
> minimal understanding of GRUB's relocator/boot code.)

The needed effort should indeed be rather small.

In the moment I have no idea when I'll be able to do it, as I have
plenty of other things to do. In case you want to try it and need some
hints, please feel free to ask (maybe I'm able to give an answer
without having to try to implement it myself ;-) ).

> We need to define a modification to
> https://xenbits.xen.org/docs/unstable-staging/misc/x86-xenpv-bootloader.html
> for PVH boot loaders.  I suggest the obvious: a second-stage PVH boot
> loader should be installed into the guest filesystem as
> /boot/xen/pvhboot-.elf, and otherwise things generally behave the
> same way.  I'd be happy to draft a patch to the protocol specification
> once a proof-of-concept exists.
> 
> The as-yet-unmerged GRUB patch to support the existing PV boot protocol
> (https://salsa.debian.org/grub-team/grub/blob/master/debian/patches/grub-install-pvxen-paths.patch)
> needs to be extended to support the amended protocol.  This is trivial
> given the above.

Would you mind trying to upstream this patch? I have CC-ed Daniel Kiper
one of the grub2 maintainers), as I guess with his Xen skills he will be
the one looking at the patch.


Juergen

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


[PATCH v7 15/20] xen_pvh: add build runes for grub-core

2018-12-07 Thread Juergen Gross
Add the modifications to the build system needed to build a xen_pvh
grub.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: sorted some filenames (Daniel Kiper)
V4: add bus/pci.c to xen_pvh
---
 gentpl.py   |  4 ++--
 grub-core/Makefile.am   | 12 
 grub-core/Makefile.core.def | 35 +++
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/gentpl.py b/gentpl.py
index da67965a4..e8439484a 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -28,7 +28,7 @@ import re
 
 GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
"i386_multiboot", "i386_ieee1275", "x86_64_efi",
-   "i386_xen", "x86_64_xen",
+   "i386_xen", "x86_64_xen", "i386_xen_pvh",
"mips_loongson", "sparc64_ieee1275",
"powerpc_ieee1275", "mips_arc", "ia64_efi",
"mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi",
@@ -71,7 +71,7 @@ GROUPS["videomodules"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
 
 # Similar for terminfo
-GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips" ] + GROUPS["xen"] + GROUPS["ieee1275"] + GROUPS["uboot"];
+GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips", "i386_xen_pvh" ] + GROUPS["xen"] + GROUPS["ieee1275"] + 
GROUPS["uboot"];
 GROUPS["terminfomodule"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
 
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index a27786ab0..df8dbe2fb 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -101,6 +101,18 @@ KERNEL_HEADER_FILES += 
$(top_builddir)/include/grub/machine/int.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
+if COND_i386_xen_pvh
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/loader.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/xen.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/xen/hypercall.h
+endif
+
 if COND_i386_efi
 KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 2d75c4daf..f31301e04 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -79,6 +79,8 @@ kernel = {
   i386_xen_ldflags = '$(TARGET_IMG_BASE_LDOPT),0';
   x86_64_xen_ldflags   = '$(TARGET_IMG_LDFLAGS)';
   x86_64_xen_ldflags   = '$(TARGET_IMG_BASE_LDOPT),0';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_LDFLAGS)';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x10';
 
   mips_loongson_ldflags= '-Wl,-Ttext,0x8020';
   powerpc_ieee1275_ldflags = '-Wl,-Ttext,0x20';
@@ -100,6 +102,7 @@ kernel = {
   x86_64_efi_startup = kern/x86_64/efi/startup.S;
   i386_xen_startup = kern/i386/xen/startup.S;
   x86_64_xen_startup = kern/x86_64/xen/startup.S;
+  i386_xen_pvh_startup = kern/i386/xen/startup_pvh.S;
   i386_qemu_startup = kern/i386/qemu/startup.S;
   i386_ieee1275_startup = kern/i386/ieee1275/startup.S;
   i386_coreboot_startup = kern/i386/coreboot/startup.S;
@@ -177,6 +180,7 @@ kernel = {
 
   i386 = kern/i386/dl.c;
   i386_xen = kern/i386/dl.c;
+  i386_xen_pvh = kern/i386/dl.c;
 
   i386_coreboot = kern/i386/coreboot/init.c;
   i386_multiboot = kern/i386/coreboot/init.c;
@@ -222,6 +226,14 @@ kernel = {
   xen = disk/xen/xendisk.c;
   xen = commands/boot.c;
 
+  i386_xen_pvh = commands/boot.c;
+  i386_xen_pvh = disk/xen/xendisk.c;
+  i386_xen_pvh = kern/i386/tsc.c;
+  i386_xen_pvh = kern/i386/xen/tsc.c;
+  i386_xen_pvh = kern/i386/xen/pvh.c;
+  i386_xen_pvh = kern/xen/init.c;
+  i386_xen_pvh = term/xen/console.c;
+
   ia64_efi = kern/ia64/efi/startup.S;
   ia64_efi = kern/ia64/efi/init.c;
   ia64_efi = kern/ia64/dl.c;
@@ -801,6 +813,7 @@ module = {
   name = cpuid;
   common = commands/i386/cpuid.c;
   enable = x86;
+  enable = i386_xen_pvh;
   enable = i386_xen;
   enable = x86_64_xen;
 };
@@ -860,6 +873,7 @@ module = {
   i386_co

[PATCH v7 14/20] xen: init memory regions for PVH

2018-12-07 Thread Juergen Gross
Add all usable memory regions to grub memory management and add the
needed mmap iterate code, which will be used by grub core (e.g.
grub-core/lib/relocator.c or grub-core/mmap/mmap.c).

As we are running in 32-bit mode don't add memory above 4GB.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: 0x1ULL -> 1ULL << 32 (Roger Pau Monné)
---
 grub-core/kern/i386/xen/pvh.c | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 472085ed1..91fbca859 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -241,6 +241,30 @@ grub_xen_set_mmap (void)
  (grub_uint32_t) (), 0, 0, 0, 0);
 }
 
+static void
+grub_xen_mm_init_regions (void)
+{
+  grub_uint64_t modend, from, to;
+  unsigned int i;
+
+  modend = grub_modules_get_end ();
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+continue;
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (from < modend)
+from = modend;
+  if (from >= to || from >= (1ULL << 32))
+continue;
+  if (to > (1ULL << 32))
+to = 1ULL << 32;
+  grub_mm_init_region ((void *) (grub_addr_t) from, to - from);
+}
+}
+
 static grub_uint64_t
 grub_xen_find_page (grub_uint64_t start)
 {
@@ -325,10 +349,21 @@ grub_xen_setup_pvh (void)
   (void *) par);
   grub_xen_set_mmap ();
 
+  grub_xen_mm_init_regions ();
+
   grub_rsdp_addr = pvh_start_info->rsdp_paddr;
 }
 
 grub_err_t
 grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 {
+  unsigned int i;
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].len && hook (map[i].addr, map[i].len, map[i].type, hook_data))
+break;
+}
+
+  return GRUB_ERR_NONE;
 }
-- 
2.16.4


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


Re: [PATCH v6.1 09/20] xen: add basic hooks for PVH in current code

2018-12-07 Thread Juergen Gross
On 07/12/2018 12:50, Daniel Kiper wrote:
> On Fri, Dec 07, 2018 at 08:35:26AM +0100, Juergen Gross wrote:
>> Add the hooks to current code needed for Xen PVH. They will be filled
>> with code later when the related functionality is being added.
>>
>> loader/i386/linux.c needs to include machine/kernel.h now as it needs
>> to get GRUB_KERNEL_USE_RSDP_ADDR from there. This in turn requires to
>> add an empty kernel.h header for some i386 platforms (efi, coreboot,
>> ieee1275, xen).
>>
>> Signed-off-by: Juergen Gross 
>> Reviewed-by: Daniel Kiper 
>> ---
>> V3: xenpvh->xen_pvh (Daniel Kiper)
>> adjust copyright date (Roger Pau Monné)
>> V5: update commit message (Daniel Kiper)
>> move including xen/hvm/start_info.h to the sources really needing
>>   it (Daniel Kiper)
>> V6.1: add empty kernel.h headers for i386 platforms
> 
> After this patch "xen_pvh: add build runes for grub-core" does not apply
> any longer. May I ask you to rebase whole patch series on latest master
> and resend it as v7?

Okay.

> 
>> It should be noted that i386_efi build is broken even without this
>> patch, but this is clearly beyond the scope of this series.
> 
> Could you tell me how exactly building fails?

No. It doesn't now. I head to do a "git clean" in order to be able to
rebase my patches on the most actual master. Seems as if there was some
stale file in my tree causing the build to fail.

> Have you tested x86_64 efi and xen builds?

x86_64 efi needs the header, too. Will be in V7.

Xen builds are working (now, with your fixup patches in place).


Juergen

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


[PATCH v7 17/20] xen: use elfnote defines instead of plain numbers

2018-12-07 Thread Juergen Gross
In order to avoid using plain integers for the ELF notes use the
available Xen include instead.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: new patch (Daniel Kiper)
---
 util/grub-mkimagexx.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a483c674c..784ed1a52 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -48,6 +48,8 @@
 #include 
 #include 
 
+#include 
+
 #pragma GCC diagnostic ignored "-Wcast-align"
 
 #define GRUB_MKIMAGEXX
@@ -341,7 +343,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof (PACKAGE_NAME));
-  note_ptr->n_type = grub_host_to_target32 (6);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_GUEST_OS);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -352,7 +354,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("generic"));
-  note_ptr->n_type = grub_host_to_target32 (8);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_LOADER);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -363,7 +365,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("xen-3.0"));
-  note_ptr->n_type = grub_host_to_target32 (5);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_XEN_VERSION);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -374,7 +376,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
-  note_ptr->n_type = grub_host_to_target32 (1);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_ENTRY);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -385,7 +387,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
-  note_ptr->n_type = grub_host_to_target32 (3);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_VIRT_BASE);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -398,7 +400,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
  note_ptr = (Elf_Nhdr *) ptr;
  note_ptr->n_namesz = grub_host_to_target32 (sizeof 
(GRUB_XEN_NOTE_NAME));
  note_ptr->n_descsz = grub_host_to_target32 (sizeof ("yes,bimodal"));
- note_ptr->n_type = grub_host_to_target32 (9);
+ note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PAE_MODE);
  ptr += sizeof (Elf_Nhdr);
  memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
-- 
2.16.4


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


[PATCH v7 12/20] xen: get memory map from hypervisor for PVH

2018-12-07 Thread Juergen Gross
Retrieve the memory map from the hypervisor and normalize it to contain
no overlapping entries and to be sorted by address.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: use grub_e820_mmap_entry instead of own struct (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 94 +++
 1 file changed, 94 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index a2554fb1d..2b68ac333 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+
+#define XEN_MEMORY_MAP_SIZE   128
 
 grub_uint64_t grub_rsdp_addr;
 
@@ -32,6 +37,8 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
+static unsigned int nr_map_entries;
 
 static void
 grub_xen_cons_msg (const char *msg)
@@ -104,11 +111,98 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t 
a0,
   return res;
 }
 
+static void
+grub_xen_sort_mmap (void)
+{
+  grub_uint64_t from, to;
+  unsigned int i;
+  struct grub_e820_mmap_entry tmp;
+
+  /* Align map entries to page boundaries. */
+  for (i = 0; i < nr_map_entries; i++)
+{
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (map[i].type == GRUB_MEMORY_AVAILABLE)
+   {
+ from = ALIGN_UP (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_DOWN (to, GRUB_XEN_PAGE_SIZE);
+   }
+  else
+   {
+ from = ALIGN_DOWN (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_UP (to, GRUB_XEN_PAGE_SIZE);
+   }
+  map[i].addr = from;
+  map[i].len = to - from;
+}
+
+ again:
+  /* Sort entries by start address. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr)
+   continue;
+  tmp = map[i];
+  map[i] = map[i - 1];
+  map[i - 1] = tmp;
+  i = 0;
+}
+
+  /* Detect overlapping areas. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr + map[i - 1].len)
+   continue;
+  tmp = map[i - 1];
+  map[i - 1].len = map[i].addr - map[i - 1].addr;
+  if (map[i].addr + map[i].len >= tmp.addr + tmp.len)
+   continue;
+  if (nr_map_entries < ARRAY_SIZE (map))
+   {
+ map[nr_map_entries].addr = map[i].addr + map[i].len;
+ map[nr_map_entries].len = tmp.addr + tmp.len - 
map[nr_map_entries].addr;
+ map[nr_map_entries].type = tmp.type;
+ nr_map_entries++;
+ goto again;
+   }
+}
+
+  /* Merge adjacent entries. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].type == map[i - 1].type &&
+ map[i].addr == map[i - 1].addr + map[i - 1].len)
+   {
+ map[i - 1].len += map[i].len;
+ map[i] = map[nr_map_entries - 1];
+ nr_map_entries--;
+ goto again;
+   }
+}
+}
+
+static void
+grub_xen_get_mmap (void)
+{
+  struct xen_memory_map memmap;
+
+  memmap.nr_entries = ARRAY_SIZE (map);
+  set_xen_guest_handle (memmap.buffer, map);
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Could not get memory map from Xen!\n");
+  nr_map_entries = memmap.nr_entries;
+
+  grub_xen_sort_mmap ();
+}
+
 void
 grub_xen_setup_pvh (void)
 {
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
+  grub_xen_get_mmap ();
 }
 
 grub_err_t
-- 
2.16.4


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


[PATCH v7 18/20] xen_pvh: support building a standalone image

2018-12-07 Thread Juergen Gross
Support mkimage for xen_pvh.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: some style adjustments (Daniel Kiper)
use defines for elf-notes (Daniel Kiper)
V5: move elf-note define usage into new patch (Daniel Kiper)

I didn't replace the 4096 by a PAGE_SIZE macro as requested by Daniel,
as there isn't such a macro easily available for util/mkimage.c and
I didn't introduce its usage.
---
 include/grub/util/mkimage.h |  3 ++-
 util/grub-mkimage32.c   |  4 +++-
 util/grub-mkimage64.c   |  4 +++-
 util/grub-mkimagexx.c   | 44 
 util/mkimage.c  | 23 ++-
 5 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/include/grub/util/mkimage.h b/include/grub/util/mkimage.h
index b3a5ca132..ba9f568f6 100644
--- a/include/grub/util/mkimage.h
+++ b/include/grub/util/mkimage.h
@@ -71,7 +71,8 @@ struct grub_install_image_target_desc
 IMAGE_I386_IEEE1275,
 IMAGE_LOONGSON_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH,
 IMAGE_FULOONG2F_FLASH, IMAGE_I386_PC_PXE, IMAGE_MIPS_ARC,
-IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO
+IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO,
+IMAGE_XEN_PVH
   } id;
   enum
 {
diff --git a/util/grub-mkimage32.c b/util/grub-mkimage32.c
index 1f2d2..026a2dd59 100644
--- a/util/grub-mkimage32.c
+++ b/util/grub-mkimage32.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF32_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF32_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF32_ST_TYPE(val)
-#define XEN_NOTE_SIZE 132
+
+#define XEN_NOTE_SIZE  132
+#define XEN_PVH_NOTE_SIZE  20
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimage64.c b/util/grub-mkimage64.c
index 4ff72a625..170defb40 100644
--- a/util/grub-mkimage64.c
+++ b/util/grub-mkimage64.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF64_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF64_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF64_ST_TYPE(val)
-#define XEN_NOTE_SIZE 120
+
+#define XEN_NOTE_SIZE  120
+#define XEN_PVH_NOTE_SIZE  24
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index 784ed1a52..e94a721b4 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -229,12 +229,12 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phnum++;
   footer_size += sizeof (struct grub_ieee1275_note);
 }
-  if (image_target->id == IMAGE_XEN)
+  if (image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH)
 {
   phnum++;
   shnum++;
   string_size += sizeof (".xen");
-  footer_size += XEN_NOTE_SIZE;
+  footer_size += (image_target->id == IMAGE_XEN) ? XEN_NOTE_SIZE : 
XEN_PVH_NOTE_SIZE;
 }
   header_size = ALIGN_UP (sizeof (*ehdr) + phnum * sizeof (*phdr)
  + shnum * sizeof (*shdr) + string_size, 
layout->align);
@@ -421,6 +421,39 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phdr->p_offset = grub_host_to_target32 (header_size + program_size);
 }
 
+  if (image_target->id == IMAGE_XEN_PVH)
+{
+  char *note_start = (elf_img + program_size + header_size);
+  Elf_Nhdr *note_ptr;
+  char *ptr = (char *) note_start;
+
+  grub_util_info ("adding XEN NOTE segment");
+
+  /* Phys32 Entry.  */
+  note_ptr = (Elf_Nhdr *) ptr;
+  note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
+  note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PHYS32_ENTRY);
+  ptr += sizeof (Elf_Nhdr);
+  memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
+  ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
+  memset (ptr, 0, image_target->voidp_sizeof);
+  *(grub_uint32_t *) ptr = GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR;
+  ptr += image_target->voidp_sizeof;
+
+  assert (XEN_PVH_NOTE_SIZE == (ptr - note_start));
+
+  phdr++;
+  phdr->p_type = grub_host_to_target32 (PT_NOTE);
+  phdr->p_flags = grub_host_to_target32 (PF_R);
+  phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
+  phdr->p_vaddr = 0;
+  phdr->p_paddr = 0;
+  phdr->p_filesz = grub_host_to_target32 (XEN_PVH_NOTE_SIZE);
+  phdr->p_memsz = 0;
+  phdr->p_offset = grub_host_to_target32 (header_size + program_size);
+}
+
   if (note)
 {
   int note_size = sizeof (struct grub_ieee1275_note);
@@ -496,7 +529,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
 shdr->sh_entsize = grub_host_to_target32 (0);

[PATCH v7 13/20] xen: setup Xen specific data for PVH

2018-12-07 Thread Juergen Gross
Initialize the needed Xen specific data. This is:

- the Xen start of day page containing the console and Xenstore ring
  page PFN and event channel
- the grant table
- the shared info page

Write back the possibly modified memory map to the hypervisor in case
the guest is reading it from there again.

Set the RSDP address for the guest from the start_info page passed
as boot parameter.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
Reviewed-by: Roger Pau Monné 
---
V4: write back memory map to Xen (Roger Pau Monné)
V5: add comment (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 123 ++
 1 file changed, 123 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 2b68ac333..472085ed1 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -27,6 +27,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #define XEN_MEMORY_MAP_SIZE   128
@@ -37,6 +38,7 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct start_info grub_xen_start_page;
 static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
 static unsigned int nr_map_entries;
 
@@ -111,6 +113,36 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
   return res;
 }
 
+static grub_uint32_t
+grub_xen_get_param (int idx)
+{
+  struct xen_hvm_param xhv;
+  int r;
+
+  xhv.domid = DOMID_SELF;
+  xhv.index = idx;
+  r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+  if (r < 0)
+grub_xen_panic ("Could not get parameter from Xen!\n");
+  return xhv.value;
+}
+
+static void *
+grub_xen_add_physmap (unsigned int space, void *addr)
+{
+  struct xen_add_to_physmap xatp;
+
+  xatp.domid = DOMID_SELF;
+  xatp.idx = 0;
+  xatp.space = space;
+  xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Memory_op hypercall failed!\n");
+  return addr;
+}
+
 static void
 grub_xen_sort_mmap (void)
 {
@@ -197,12 +229,103 @@ grub_xen_get_mmap (void)
   grub_xen_sort_mmap ();
 }
 
+static void
+grub_xen_set_mmap (void)
+{
+  struct xen_foreign_memory_map memmap;
+
+  memmap.domid = DOMID_SELF;
+  memmap.map.nr_entries = nr_map_entries;
+  set_xen_guest_handle (memmap.map.buffer, map);
+  grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_set_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+}
+
+static grub_uint64_t
+grub_xen_find_page (grub_uint64_t start)
+{
+  unsigned int i, j;
+  grub_uint64_t last = start;
+
+  /*
+   * Try to find a e820 map hole below 4G.
+   * Relies on page-aligned entries (addr and len) and input (start).
+   */
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (last > map[i].addr + map[i].len)
+   continue;
+  if (last < map[i].addr)
+   return last;
+  if ((map[i].addr >> 32) || ((map[i].addr + map[i].len) >> 32))
+   break;
+  last = map[i].addr + map[i].len;
+}
+if (i == nr_map_entries)
+  return last;
+
+  /* No hole found, use the highest RAM page below 4G and reserve it. */
+  if (nr_map_entries == ARRAY_SIZE (map))
+grub_xen_panic ("Memory map size limit reached!\n");
+  for (i = 0, j = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+   continue;
+  if (map[i].addr >> 32)
+   break;
+  j = i;
+  if ((map[i].addr + map[i].len) >> 32)
+   break;
+}
+  if (map[j].type != GRUB_MEMORY_AVAILABLE)
+grub_xen_panic ("No free memory page found!\n");
+  if ((map[j].addr + map[j].len) >> 32)
+last = (1ULL << 32) - GRUB_XEN_PAGE_SIZE;
+  else
+last = map[j].addr + map[j].len - GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].addr = last;
+  map[nr_map_entries].len = GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
+  nr_map_entries++;
+  grub_xen_sort_mmap ();
+
+  return last;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_addr_t par;
+
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
   grub_xen_get_mmap ();
+
+  /* Setup Xen data. */
+  grub_xen_start_page_addr = _xen_start_page;
+
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_PFN);
+  grub_xen_start_page_addr->console.domU.mfn = par;
+  grub_xen_xcons = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_EVTCHN);
+  grub_xen_start_page_addr->console.domU.evtchn = par;
+
+  par = grub_xen_get_param (HVM_PARAM_STORE_PFN);
+  grub_xen_start_page_addr->store_mfn = par;
+  grub_xen_xenstore = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_STOR

[PATCH v7 19/20] xen_pvh: support grub-install for xen_pvh

2018-12-07 Thread Juergen Gross
Add xen_pvh support to grub-install.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 include/grub/util/install.h | 1 +
 util/grub-install-common.c  | 1 +
 util/grub-install.c | 7 +++
 3 files changed, 9 insertions(+)

diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 0dba8b67f..af2bf65d7 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -100,6 +100,7 @@ enum grub_install_plat
 GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS,
 GRUB_INSTALL_PLATFORM_I386_XEN,
 GRUB_INSTALL_PLATFORM_X86_64_XEN,
+GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
 GRUB_INSTALL_PLATFORM_ARM64_EFI,
 GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
 GRUB_INSTALL_PLATFORM_MAX
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
index 0a2e24a79..1b1cb43b4 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -716,6 +716,7 @@ static struct
 [GRUB_INSTALL_PLATFORM_X86_64_EFI] =   { "x86_64",  "efi"   },
 [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386","xen"   },
 [GRUB_INSTALL_PLATFORM_X86_64_XEN] =   { "x86_64",  "xen"   },
+[GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386","xen_pvh"   },
 [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",  "loongson"  },
 [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",  "qemu_mips" },
 [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips","qemu_mips" },
diff --git a/util/grub-install.c b/util/grub-install.c
index 4375c1619..743296f36 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -496,6 +496,7 @@ have_bootdev (enum grub_install_plat pl)
 
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   return 0;
 
   /* pacify warning.  */
@@ -913,6 +914,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
 
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
@@ -960,6 +962,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   free (install_device);
   install_device = NULL;
   break;
@@ -1477,6 +1480,7 @@ main (int argc, char *argv[])
  case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  case GRUB_INSTALL_PLATFORM_I386_XEN:
  case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+ case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
grub_util_warn ("%s", _("no hints available for your 
platform. Expect reduced performance"));
break;
/* pacify warning.  */
@@ -1568,6 +1572,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   core_name = "core.elf";
   snprintf (mkimage_target, sizeof (mkimage_target),
"%s-%s",
@@ -1660,6 +1665,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
   /* pacify warning.  */
 case GRUB_INSTALL_PLATFORM_MAX:
@@ -1926,6 +1932,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   grub_util_warn ("%s",
  _("WARNING: no platform-specific install was performed"));
   break;
-- 
2.16.4


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


[PATCH v7 16/20] grub-module-verifier: Ignore all_video for xenpvh

2018-12-07 Thread Juergen Gross
From: Hans van Kranenburg 

This solves the build failing with "Error: no symbol table and no
.moddeps section"

Also see:
- 6371e9c10433578bb236a8284ddb9ce9e201eb59
- https://savannah.gnu.org/bugs/?49012

Signed-off-by: Hans van Kranenburg 
Reviewed-by: Daniel Kiper 
---
V2: new patch
Signed-off-by: Juergen Gross 
---
 util/grub-module-verifier.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index 03ba1ab43..97cb9 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -129,6 +129,7 @@ struct platform_whitelist {
 
 static struct platform_whitelist whitelists[] = {
   {"i386", "xen", (const char *[]) {"all_video", 0}},
+  {"i386", "xen_pvh", (const char *[]) {"all_video", 0}},
   {"x86_64", "xen", (const char *[]) {"all_video", 0}},
   {"sparc64", "ieee1275", (const char *[]) {"all_video", 0}},
 
-- 
2.16.4


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


[PATCH v7 11/20] xen: setup hypercall page for PVH

2018-12-07 Thread Juergen Gross
Add the needed code to setup the hypercall page for calling into the
Xen hypervisor.

Import the XEN_HVM_DEBUGCONS_IOPORT define from Xen unstable into
include/xen/arch-x86/xen.h

Signed-off-by: Juergen Gross 
Reviewed-by: Roger Pau Monné 
---
V3: grub_xen_early_halt->grub_xen_panic (Roger Pau Monné)
issue panic message (Roger Pau Monné)
rewrite grub_xen_hypercall to avoid register variables (Daniel Kiper)
V5: Use XEN_HVM_DEBUGCONS_IOPORT from Xen unstable (Roger Pau Monné)
Issue "System halted!" in panic (Daniel Kiper)
Clear interrupts and loop for halting (Roger Pau Monné, Daniel Kiper)
Use only one dummy variable for hypercall asm statement
V6: Added some comments (Daniel Kiper)
Use "+x" constraints instead of dummy variable (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 80 +++
 include/xen/arch-x86/xen.h|  7 
 2 files changed, 87 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 4f629b15e..a2554fb1d 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -20,15 +20,95 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
 
 grub_uint64_t grub_rsdp_addr;
 
+static char hypercall_page[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+static grub_uint32_t xen_cpuid_base;
+
+static void
+grub_xen_cons_msg (const char *msg)
+{
+  const char *c;
+
+  for (c = msg; *c; c++)
+grub_outb (*c, XEN_HVM_DEBUGCONS_IOPORT);
+}
+
+static void
+grub_xen_panic (const char *msg)
+{
+  grub_xen_cons_msg (msg);
+  grub_xen_cons_msg ("System halted!\n");
+
+  asm volatile ("cli");
+
+  while (1)
+{
+  asm volatile ("hlt");
+}
+}
+
+static void
+grub_xen_cpuid_base (void)
+{
+  grub_uint32_t base, eax, signature[3];
+
+  for (base = 0x4000; base < 0x4001; base += 0x100)
+{
+  grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
+  if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
+   {
+ xen_cpuid_base = base;
+ return;
+   }
+}
+
+  grub_xen_panic ("Found no Xen signature!\n");
+}
+
+static void
+grub_xen_setup_hypercall_page (void)
+{
+  grub_uint32_t msr, addr, eax, ebx, ecx, edx;
+
+  /* Get base address of Xen-specific MSRs. */
+  grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
+  msr = ebx;
+  addr = (grub_uint32_t) (_page);
+
+  /* Specify hypercall page address for Xen. */
+  asm volatile ("wrmsr" : : "c" (msr), "a" (addr), "d" (0) : "memory");
+}
+
+int
+grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
+   grub_uint32_t a1, grub_uint32_t a2,
+   grub_uint32_t a3, grub_uint32_t a4,
+   grub_uint32_t a5 __attribute__ ((unused)))
+{
+  grub_uint32_t res;
+
+  asm volatile ("call *%[callno]"
+   : "=a" (res), "+b" (a0), "+c" (a1), "+d" (a2),
+ "+S" (a3), "+D" (a4)
+   : [callno] "a" (_page[callno * 32])
+   : "memory");
+  return res;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_xen_cpuid_base ();
+  grub_xen_setup_hypercall_page ();
 }
 
 grub_err_t
diff --git a/include/xen/arch-x86/xen.h b/include/xen/arch-x86/xen.h
index f35804b88..56be26cb6 100644
--- a/include/xen/arch-x86/xen.h
+++ b/include/xen/arch-x86/xen.h
@@ -260,6 +260,13 @@ typedef struct arch_shared_info arch_shared_info_t;
 #define XEN_CPUID  XEN_EMULATE_PREFIX "cpuid"
 #endif
 
+/*
+ * Debug console IO port, also called "port E9 hack". Each character written
+ * to this IO port will be printed on the hypervisor console, subject to log
+ * level restrictions.
+ */
+#define XEN_HVM_DEBUGCONS_IOPORT 0xe9
+
 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_H__ */
 
 /*
-- 
2.16.4


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


[PATCH v7 09/20] xen: add basic hooks for PVH in current code

2018-12-07 Thread Juergen Gross
Add the hooks to current code needed for Xen PVH. They will be filled
with code later when the related functionality is being added.

loader/i386/linux.c needs to include machine/kernel.h now as it needs
to get GRUB_KERNEL_USE_RSDP_ADDR from there. This in turn requires to
add an empty kernel.h header for some i386 platforms (efi, coreboot,
ieee1275, xen) and for x86_64 efi.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: xenpvh->xen_pvh (Daniel Kiper)
adjust copyright date (Roger Pau Monné)
V5: update commit message (Daniel Kiper)
move including xen/hvm/start_info.h to the sources really needing
  it (Daniel Kiper)
V6.1: add empty kernel.h headers for i386 platforms
V7: added empty kernel.h for x86_64 efi.
---
 grub-core/Makefile.am |  6 ++
 grub-core/kern/i386/xen/pvh.c | 37 +++
 grub-core/kern/i386/xen/startup_pvh.S | 29 +++
 grub-core/kern/xen/init.c |  4 
 grub-core/loader/i386/linux.c |  1 +
 include/grub/i386/coreboot/kernel.h   |  0
 include/grub/i386/efi/kernel.h|  0
 include/grub/i386/ieee1275/kernel.h   |  0
 include/grub/i386/xen/kernel.h|  0
 include/grub/i386/xen_pvh/kernel.h| 30 
 include/grub/x86_64/efi/kernel.h  |  0
 include/grub/xen.h|  5 +
 12 files changed, 112 insertions(+)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/coreboot/kernel.h
 create mode 100644 include/grub/i386/efi/kernel.h
 create mode 100644 include/grub/i386/ieee1275/kernel.h
 create mode 100644 include/grub/i386/xen/kernel.h
 create mode 100644 include/grub/i386/xen_pvh/kernel.h
 create mode 100644 include/grub/x86_64/efi/kernel.h

diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index f4ff62b76..a27786ab0 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -102,6 +102,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_efi
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
@@ -111,6 +112,7 @@ KERNEL_HEADER_FILES += 
$(top_srcdir)/include/grub/i386/pmtimer.h
 endif
 
 if COND_i386_coreboot
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/coreboot/lbio.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/video.h
@@ -122,6 +124,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 endif
 
 if COND_i386_multiboot
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 endif
@@ -132,6 +135,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_ieee1275
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
@@ -140,6 +144,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_xen
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/xen.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/xen/hypercall.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
@@ -158,6 +163,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/loader.h
 endif
 
 if COND_x86_64_efi
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
new file mode 100644
index 0..4f629b15e
--- /dev/null
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -0,0 +1,37 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have recei

[PATCH v7 07/20] xen: modify grub_xen_ptr2mfn() for xen-pvh

2018-12-07 Thread Juergen Gross
grub_xen_ptr2mfn() returns the machine frame number for a given pointer
value. For Xen-PVH guests this is just the PFN. Add the PVH specific
variant.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: new patch (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 10007b411..a23dad633 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -45,6 +45,8 @@ grub_xen_ptr2mfn (void *ptr)
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#else
+  return (grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE;
 #endif
 }
 
-- 
2.16.4


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


[PATCH v7 06/20] xen: rearrange xen/init.c to prepare it for Xen PVH mode

2018-12-07 Thread Juergen Gross
Rearrange grub-core/kern/xen/init.c to prepare adding PVH mode support
to it. This includes putting some code under #ifdef GRUB_MACHINE_XEN
as it will not be used when running as PVH.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/xen/init.c | 60 +++
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 29f5bc23d..10007b411 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -41,9 +41,11 @@ grub_size_t grub_xen_n_allocated_shared_pages;
 static grub_xen_mfn_t
 grub_xen_ptr2mfn (void *ptr)
 {
+#ifdef GRUB_MACHINE_XEN
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#endif
 }
 
 void *
@@ -104,18 +106,6 @@ grub_machine_get_bootlocation (char **device __attribute__ 
((unused)),
 {
 }
 
-static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
-  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
-
-#ifdef __x86_64__
-#define NUMBER_OF_LEVELS 4
-#else
-#define NUMBER_OF_LEVELS 3
-#endif
-
-#define LOG_POINTERS_PER_PAGE 9
-#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
-
 void
 grub_xen_store_send (const void *buf_, grub_size_t len)
 {
@@ -337,6 +327,19 @@ grub_xen_setup_gnttab (void)
   grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
 }
 
+#ifdef GRUB_MACHINE_XEN
+static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+#ifdef __x86_64__
+#define NUMBER_OF_LEVELS 4
+#else
+#define NUMBER_OF_LEVELS 3
+#endif
+
+#define LOG_POINTERS_PER_PAGE 9
+#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -529,13 +532,30 @@ map_all_pages (void)
   grub_mm_init_region ((void *) heap_start, heap_end - heap_start);
 }
 
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
+  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
+  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
+return GRUB_ERR_NONE;
+
+  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
+   GRUB_MEMORY_RESERVED, hook_data);
+
+  return GRUB_ERR_NONE;
+}
+#endif
+
 extern char _end[];
 
 void
 grub_machine_init (void)
 {
+#ifdef GRUB_MACHINE_XEN
 #ifdef __i386__
   grub_xen_vm_assist (VMASST_CMD_enable, VMASST_TYPE_pae_extended_cr3);
+#endif
 #endif
 
   grub_modbase = ALIGN_UP ((grub_addr_t) _end
@@ -544,7 +564,9 @@ grub_machine_init (void)
 
   grub_xen_setup_gnttab ();
 
+#ifdef GRUB_MACHINE_XEN
   map_all_pages ();
+#endif
 
   grub_console_init ();
 
@@ -571,17 +593,3 @@ grub_machine_fini (int flags __attribute__ ((unused)))
   grub_xendisk_fini ();
   grub_boot_fini ();
 }
-
-grub_err_t
-grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
-{
-  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
-  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
-  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
-return GRUB_ERR_NONE;
-
-  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
-   GRUB_MEMORY_RESERVED, hook_data);
-
-  return GRUB_ERR_NONE;
-}
-- 
2.16.4


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


[PATCH v7 10/20] xen: add PVH boot entry code

2018-12-07 Thread Juergen Gross
Add the code for the Xen PVH mode boot entry.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: clear %fs and %gs, too (Daniel Kiper)
use GRUB_MEMORY_MACHINE_PROT_STACK_SIZE for stack size (Daniel Kiper)
V5: reorder clearing segment regs (Daniel Kiper)
---
 grub-core/kern/i386/xen/startup_pvh.S | 52 +++
 1 file changed, 52 insertions(+)

diff --git a/grub-core/kern/i386/xen/startup_pvh.S 
b/grub-core/kern/i386/xen/startup_pvh.S
index 69b8fdcca..363c31858 100644
--- a/grub-core/kern/i386/xen/startup_pvh.S
+++ b/grub-core/kern/i386/xen/startup_pvh.S
@@ -19,11 +19,63 @@
 
 #include 
 #include 
+#include 
 
.file   "startup_pvh.S"
.text
+   .globl  start, _start
+   .code32
 
+start:
+_start:
+   cld
+   lgdtgdtdesc
+   ljmp$GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $1f
+1:
+   movl$GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %eax
+   mov %eax, %ds
+   mov %eax, %es
+   mov %eax, %fs
+   mov %eax, %gs
+   mov %eax, %ss
+   lealLOCAL(stack_end), %esp
+
+   /* Save address of start info structure. */
+   mov %ebx, pvh_start_info
+   callEXT_C(grub_main)
+   /* Doesn't return. */
+
+   .p2align3
+gdt:
+   .word   0, 0
+   .byte   0, 0, 0, 0
+
+   /* -- code segment --
+* base = 0x, limit = 0xF (4 KiB Granularity), present
+* type = 32bit code execute/read, DPL = 0
+*/
+   .word   0x, 0
+   .byte   0, 0x9A, 0xCF, 0
+
+   /* -- data segment --
+* base = 0x, limit 0xF (4 KiB Granularity), present
+* type = 32 bit data read/write, DPL = 0
+*/
+   .word   0x, 0
+   .byte   0, 0x92, 0xCF, 0
+
+   .p2align3
+/* this is the GDT descriptor */
+gdtdesc:
+   .word   0x17/* limit */
+   .long   gdt /* addr */
+
+   .p2align2
 /* Saved pointer to start info structure. */
.globl  pvh_start_info
 pvh_start_info:
.long   0
+
+   .bss
+   .space  GRUB_MEMORY_MACHINE_PROT_STACK_SIZE
+LOCAL(stack_end):
-- 
2.16.4


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


[PATCH v7 01/20] xen: add some xen headers

2018-12-07 Thread Juergen Gross
In order to support grub2 in Xen PVH environment some additional Xen
headers are needed as grub2 will be started in PVH mode requiring to
use several HVM hypercalls and structures.

Add the needed headers from Xen 4.10 being the first Xen version with
full (not only experimental) PVH guest support.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 include/xen/hvm/hvm_op.h | 296 +++
 include/xen/hvm/params.h | 284 ++
 include/xen/hvm/start_info.h |  98 +++
 include/xen/memory.h | 665 +++
 include/xen/physdev.h| 387 +
 include/xen/trace.h  | 339 ++
 include/xen/xen.h| 104 +--
 7 files changed, 2142 insertions(+), 31 deletions(-)
 create mode 100644 include/xen/hvm/hvm_op.h
 create mode 100644 include/xen/hvm/params.h
 create mode 100644 include/xen/hvm/start_info.h
 create mode 100644 include/xen/memory.h
 create mode 100644 include/xen/physdev.h
 create mode 100644 include/xen/trace.h

diff --git a/include/xen/hvm/hvm_op.h b/include/xen/hvm/hvm_op.h
new file mode 100644
index 0..0bdafdf59
--- /dev/null
+++ b/include/xen/hvm/hvm_op.h
@@ -0,0 +1,296 @@
+/*
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2007, Keir Fraser
+ */
+
+#ifndef __XEN_PUBLIC_HVM_HVM_OP_H__
+#define __XEN_PUBLIC_HVM_HVM_OP_H__
+
+#include "../xen.h"
+#include "../trace.h"
+#include "../event_channel.h"
+
+/* Get/set subcommands: extra argument == pointer to xen_hvm_param struct. */
+#define HVMOP_set_param   0
+#define HVMOP_get_param   1
+struct xen_hvm_param {
+domid_t  domid;/* IN */
+uint32_t index;/* IN */
+uint64_t value;/* IN/OUT */
+};
+typedef struct xen_hvm_param xen_hvm_param_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_param_t);
+
+#if __XEN_INTERFACE_VERSION__ < 0x00040900
+
+/* Set the logical level of one of a domain's PCI INTx wires. */
+#define HVMOP_set_pci_intx_level  2
+struct xen_hvm_set_pci_intx_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI INTx identification in PCI topology (domain:bus:device:intx). */
+uint8_t  domain, bus, device, intx;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_pci_intx_level xen_hvm_set_pci_intx_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_intx_level_t);
+
+/* Set the logical level of one of a domain's ISA IRQ wires. */
+#define HVMOP_set_isa_irq_level   3
+struct xen_hvm_set_isa_irq_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* ISA device identification, by ISA IRQ (0-15). */
+uint8_t  isa_irq;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_isa_irq_level xen_hvm_set_isa_irq_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_isa_irq_level_t);
+
+#define HVMOP_set_pci_link_route  4
+struct xen_hvm_set_pci_link_route {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI link identifier (0-3). */
+uint8_t  link;
+/* ISA IRQ (1-15), or 0 (disable link). */
+uint8_t  isa_irq;
+};
+typedef struct xen_hvm_set_pci_link_route xen_hvm_set_pci_link_route_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_link_route_t);
+
+#endif /* __XEN_INTERFACE_VERSION__ < 0x00040900 */
+
+/* Flushes all VCPU TLBs: @arg must be NULL. */
+#define HVMOP_flush_tlbs  5
+
+typedef enum {
+HVMMEM_ram_rw, /* Normal read/write guest RAM */
+HVMMEM_ram_ro, /* Read-only; writes are discarded */
+HVMMEM_mmio_dm,/* Reads and write go to the device model */
+#if __XEN_INTERFACE_VERSION__ < 0x00040700
+HVMMEM_mmio_write_dm,  /* Read-only; writes go to the device model */
+#else
+HVMMEM_unus

[PATCH v7 04/20] xen: prepare common code for Xen PVH support

2018-12-07 Thread Juergen Gross
Some common code needs to be special cased for Xen PVH mode. This hits
mostly Xen PV mode specific areas.

Split include/grub/i386/pc/int_types.h off from
include/grub/i386/pc/int.h to support including this file later from
xen_pvh code without the grub_bios_interrupt definition.

Move definition of struct grub_e820_mmap_entry from
grub-core/mmap/i386/pc/mmap.c to include/grub/i386/memory.h in order
to make it usable from xen_pvh code.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: GRUB_MACHINE_XENPVH -> GRUB_MACHINE_XEN_PVH (Daniel Kiper)
split include/grub/i386/pc/int.h (Daniel Kiper)
move struct grub_e820_mmap_entry definition to header file
V5: minor style adjustments (Daniel Kiper)
---
 grub-core/kern/i386/tsc.c |  2 +-
 grub-core/mmap/i386/pc/mmap.c |  8 --
 include/grub/i386/memory.h|  7 +
 include/grub/i386/pc/int.h| 36 +---
 include/grub/i386/pc/int_types.h  | 59 +++
 include/grub/i386/tsc.h   |  2 +-
 include/grub/i386/xen/hypercall.h |  5 +++-
 include/grub/kernel.h |  4 ++-
 8 files changed, 76 insertions(+), 47 deletions(-)
 create mode 100644 include/grub/i386/pc/int_types.h

diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
index f266eb131..9293b161d 100644
--- a/grub-core/kern/i386/tsc.c
+++ b/grub-core/kern/i386/tsc.c
@@ -65,7 +65,7 @@ grub_tsc_init (void)
 
   tsc_boot_time = grub_get_tsc ();
 
-#ifdef GRUB_MACHINE_XEN
+#if defined (GRUB_MACHINE_XEN) || defined (GRUB_MACHINE_XEN_PVH)
   (void) (grub_tsc_calibrate_from_xen () || calibrate_tsc_hardcode());
 #elif defined (GRUB_MACHINE_EFI)
   (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () 
|| grub_tsc_calibrate_from_efi() || calibrate_tsc_hardcode());
diff --git a/grub-core/mmap/i386/pc/mmap.c b/grub-core/mmap/i386/pc/mmap.c
index 609994516..6ab4f6730 100644
--- a/grub-core/mmap/i386/pc/mmap.c
+++ b/grub-core/mmap/i386/pc/mmap.c
@@ -42,14 +42,6 @@ extern grub_uint16_t grub_machine_mmaphook_kblow;
 extern grub_uint16_t grub_machine_mmaphook_kbin16mb;
 extern grub_uint16_t grub_machine_mmaphook_64kbin4gb;
 
-struct grub_e820_mmap_entry
-{
-  grub_uint64_t addr;
-  grub_uint64_t len;
-  grub_uint32_t type;
-} GRUB_PACKED;
-
-
 /* Helper for preboot.  */
 static int fill_hook (grub_uint64_t addr, grub_uint64_t size,
  grub_memory_type_t type, void *data)
diff --git a/include/grub/i386/memory.h b/include/grub/i386/memory.h
index 8bb6e1cbb..5cb607fb4 100644
--- a/include/grub/i386/memory.h
+++ b/include/grub/i386/memory.h
@@ -44,6 +44,13 @@
 
 #include 
 
+struct grub_e820_mmap_entry
+{
+  grub_uint64_t addr;
+  grub_uint64_t len;
+  grub_uint32_t type;
+} GRUB_PACKED;
+
 grub_uint64_t grub_mmap_get_upper (void);
 grub_uint64_t grub_mmap_get_lower (void);
 grub_uint64_t grub_mmap_get_post64 (void);
diff --git a/include/grub/i386/pc/int.h b/include/grub/i386/pc/int.h
index 16a53e4fe..a60104001 100644
--- a/include/grub/i386/pc/int.h
+++ b/include/grub/i386/pc/int.h
@@ -20,45 +20,11 @@
 #define GRUB_INTERRUPT_MACHINE_HEADER  1
 
 #include 
-#include 
-
-struct grub_bios_int_registers
-{
-  grub_uint32_t eax;
-  grub_uint16_t es;
-  grub_uint16_t ds;
-  grub_uint16_t flags;
-  grub_uint16_t dummy;
-  grub_uint32_t ebx;
-  grub_uint32_t ecx;
-  grub_uint32_t edi;
-  grub_uint32_t esi;
-  grub_uint32_t edx;
-};
-
-#define  GRUB_CPU_INT_FLAGS_CARRY 0x1
-#define  GRUB_CPU_INT_FLAGS_PARITY0x4
-#define  GRUB_CPU_INT_FLAGS_ADJUST0x10
-#define  GRUB_CPU_INT_FLAGS_ZERO  0x40
-#define  GRUB_CPU_INT_FLAGS_SIGN  0x80
-#define  GRUB_CPU_INT_FLAGS_TRAP  0x100
-#define  GRUB_CPU_INT_FLAGS_INTERRUPT 0x200
-#define  GRUB_CPU_INT_FLAGS_DIRECTION 0x400
-#define  GRUB_CPU_INT_FLAGS_OVERFLOW  0x800
-#ifdef GRUB_MACHINE_PCBIOS
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   GRUB_CPU_INT_FLAGS_INTERRUPT
-#else
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   0
-#endif
+#include 
 
 void EXPORT_FUNC (grub_bios_interrupt) (grub_uint8_t intno,
struct grub_bios_int_registers *regs)
  __attribute__ ((regparm(3)));
-struct grub_i386_idt
-{
-  grub_uint16_t limit;
-  grub_uint32_t base;
-} GRUB_PACKED;
 
 #ifdef GRUB_MACHINE_PCBIOS
 extern struct grub_i386_idt *EXPORT_VAR(grub_realidt);
diff --git a/include/grub/i386/pc/int_types.h b/include/grub/i386/pc/int_types.h
new file mode 100644
index 0..2c5a69b63
--- /dev/null
+++ b/include/grub/i386/pc/int_types.h
@@ -0,0 +1,59 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRA

[PATCH v7 20/20] xen_pvh: add support to configure

2018-12-07 Thread Juergen Gross
Support platform i386/xen_pvh in configure.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 configure.ac | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.ac b/configure.ac
index 5e63c4af3..81a19afd5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -151,6 +151,7 @@ case "$target_cpu"-"$platform" in
   i386-efi) ;;
   x86_64-efi) ;;
   i386-xen) ;;
+  i386-xen_pvh) ;;
   x86_64-xen) ;;
   i386-pc) ;;
   i386-multiboot) ;;
@@ -219,6 +220,7 @@ case "$platform" in
   multiboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MULTIBOOT=1" 
;;
   efi) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EFI=1" ;;
   xen) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN=1" ;;
+  xen_pvh) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN_PVH=1" ;;
   ieee1275)machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" 
;;
   uboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
   qemu)machine_CPPFLAGS="$machine_CPPFLAGS 
-DGRUB_MACHINE_QEMU=1" ;;
@@ -1897,6 +1899,7 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = 
xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform 
= xmultiboot])
 AM_CONDITIONAL([COND_x86_64_efi], [test x$target_cpu = xx86_64 -a x$platform = 
xefi])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = 
xxen])
+AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = 
xxen_pvh])
 AM_CONDITIONAL([COND_x86_64_xen], [test x$target_cpu = xx86_64 -a x$platform = 
xxen])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a 
x$platform = xloongson])
 AM_CONDITIONAL([COND_mips_qemu_mips], [test "(" x$target_cpu = xmips -o 
x$target_cpu = xmipsel ")"  -a x$platform = xqemu_mips])
-- 
2.16.4


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


[PATCH v7 05/20] xen: add some dummy headers for PVH mode

2018-12-07 Thread Juergen Gross
With Xen PVH mode adding a new machine type the machine related headers
need to be present for the build to succeed. Most of the headers just
need to include the related common i386 headers. Add those to the tree.

Note that xen_pvh/int.h needs to include pc/int_types.h instead of
pc/int.h in order to avoid the definition of grub_bios_interrupt().

xen_pvh/memory.h needs to include coreboot/memory.h (like some other
/memory.h do as well) as this contains just the needed stubs.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: updated commit message (Daniel Kiper)
xenpvh->xen_pvh (Daniel Kiper)
V5: updated commit message (Daniel Kiper)
---
 include/grub/i386/xen_pvh/boot.h| 1 +
 include/grub/i386/xen_pvh/console.h | 1 +
 include/grub/i386/xen_pvh/int.h | 1 +
 include/grub/i386/xen_pvh/memory.h  | 1 +
 include/grub/i386/xen_pvh/time.h| 1 +
 5 files changed, 5 insertions(+)
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/memory.h
 create mode 100644 include/grub/i386/xen_pvh/time.h

diff --git a/include/grub/i386/xen_pvh/boot.h b/include/grub/i386/xen_pvh/boot.h
new file mode 100644
index 0..6cd23aa83
--- /dev/null
+++ b/include/grub/i386/xen_pvh/boot.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/console.h 
b/include/grub/i386/xen_pvh/console.h
new file mode 100644
index 0..305a46d8e
--- /dev/null
+++ b/include/grub/i386/xen_pvh/console.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/int.h b/include/grub/i386/xen_pvh/int.h
new file mode 100644
index 0..0f1f9ee62
--- /dev/null
+++ b/include/grub/i386/xen_pvh/int.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/memory.h 
b/include/grub/i386/xen_pvh/memory.h
new file mode 100644
index 0..8dd6f7c8c
--- /dev/null
+++ b/include/grub/i386/xen_pvh/memory.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/time.h b/include/grub/i386/xen_pvh/time.h
new file mode 100644
index 0..2298ee8f4
--- /dev/null
+++ b/include/grub/i386/xen_pvh/time.h
@@ -0,0 +1 @@
+#include 
-- 
2.16.4


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


[PATCH v7 03/20] xen: carve out grant tab initialization into dedicated function

2018-12-07 Thread Juergen Gross
Initialize the grant tab in a dedicated function. This will enable
using it for PVH guests, too.

Call the new function from grub_machine_init() as this will later
be common between Xen PV and Xen PVH mode.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 35 +--
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 0559c033c..29f5bc23d 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -318,6 +318,25 @@ grub_xenstore_dir (const char *dir,
 
 unsigned long gntframe = 0;
 
+static void
+grub_xen_setup_gnttab (void)
+{
+  struct gnttab_set_version gnttab_setver;
+  struct gnttab_setup_table gnttab_setup;
+
+  grub_memset (_setver, 0, sizeof (gnttab_setver));
+
+  gnttab_setver.version = 1;
+  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
+
+  grub_memset (_setup, 0, sizeof (gnttab_setup));
+  gnttab_setup.dom = DOMID_SELF;
+  gnttab_setup.nr_frames = 1;
+  gnttab_setup.frame_list.p = 
+
+  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
+}
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -357,26 +376,12 @@ map_all_pages (void)
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   grub_uint64_t *pg = (grub_uint64_t *) window;
   grub_uint64_t oldpgstart, oldpgend;
-  struct gnttab_setup_table gnttab_setup;
-  struct gnttab_set_version gnttab_setver;
   grub_size_t n_unusable_pages = 0;
   struct mmu_update m2p_updates[2 * MAX_N_UNUSABLE_PAGES];
 
   if (total_pages > MAX_TOTAL_PAGES - 4)
 total_pages = MAX_TOTAL_PAGES - 4;
 
-  grub_memset (_setver, 0, sizeof (gnttab_setver));
-
-  gnttab_setver.version = 1;
-  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
-
-  grub_memset (_setup, 0, sizeof (gnttab_setup));
-  gnttab_setup.dom = DOMID_SELF;
-  gnttab_setup.nr_frames = 1;
-  gnttab_setup.frame_list.p = 
-
-  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
-
   for (j = 0; j < total_pages - n_unusable_pages; j++)
 while (!grub_xen_is_page_usable (mfn_list[j]))
   {
@@ -537,6 +542,8 @@ grub_machine_init (void)
   + GRUB_KERNEL_MACHINE_MOD_GAP,
   GRUB_KERNEL_MACHINE_MOD_ALIGN);
 
+  grub_xen_setup_gnttab ();
+
   map_all_pages ();
 
   grub_console_init ();
-- 
2.16.4


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


[PATCH v7 02/20] loader/linux: support passing rsdp address via boot params

2018-12-07 Thread Juergen Gross
Xen PVH guests will have the RSDP at an arbitrary address. Support that
by passing the RSDP address via the boot parameters to Linux.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: add oring 0x8000 to version field
V3: move including machine/kernel.h to patch 8 (Daniel Kiper)
V5: move acpi_rsdp_addr to struct linux_kernel_params (Peter Anvin)
---
 grub-core/loader/i386/linux.c | 4 
 include/grub/i386/linux.h | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index c408b10d8..375ee80dc 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -508,6 +508,10 @@ grub_linux_boot (void)
}
 }
 
+#ifdef GRUB_KERNEL_USE_RSDP_ADDR
+  linux_params.acpi_rsdp_addr = grub_le_to_cpu64 (grub_rsdp_addr);
+#endif
+
   mmap_size = find_mmap_size ();
   /* Make sure that each size is aligned to a page boundary.  */
   cl_offset = ALIGN_UP (mmap_size + sizeof (linux_params), 4096);
diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
index 60c7c3b5e..a96059311 100644
--- a/include/grub/i386/linux.h
+++ b/include/grub/i386/linux.h
@@ -210,8 +210,9 @@ struct linux_kernel_params
   grub_uint32_t ist_command;   /* 64 */
   grub_uint32_t ist_event; /* 68 */
   grub_uint32_t ist_perf_level;/* 6c */
+  grub_uint64_t acpi_rsdp_addr;/* 70 */
 
-  grub_uint8_t padding5[0x80 - 0x70];
+  grub_uint8_t padding5[0x80 - 0x78];
 
   grub_uint8_t hd0_drive_info[0x10];   /* 80 */
   grub_uint8_t hd1_drive_info[0x10];   /* 90 */
-- 
2.16.4


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


[PATCH v7 00/20] xen: add pvh guest support

2018-12-07 Thread Juergen Gross
This patch series adds support for booting Linux as PVH guest.

Similar to i386/xen and x86_64/xen platforms the new i386/xenpvh
platform grub is booted as a standalone image directly by Xen.

For booting Linux kernel it is using the standard linux kernel
loader. The only modification of the linux loader is to pass the
ACPI RSDP address via boot parameters to the kernel, as that table
might not be located at the usual physical address just below 1MB.

The related Linux kernel patches have been accepted for 4.20-rc4

Changes in V7:
- fix build failures for various targets (patch 9)

Changes in V6:
- addressed a few comments, see individual patches (only minor changes)

Changes in V5:
- added new patches 7, 17
- removed (old) patch 12 again
- modified boot params interface for passing rsdp address on request
  by Peter Anvin (patch 2)
- addressed various comments, see individual patches

Changes in V4:
- added new patch 12
- write modified memory map back to Xen

Changes in V3:
- addressed lots of comments, most by Daniel and some by Roger,
  details are mentioned in the individual patches

Changes in V2:
- extend linux boot protocol version 2.14 to report back the protocol
  version supported by grub2 (needed as there are grub2 downstream
  variants in some distros which clobber data after the setup_header)
- split former patch 4 into 10 patches
- fix memmap handling for >4GB of memory
- added new patch from Hans (thanks for that one)
- addressed most of Daniel's comments (see individual patches)

Hans van Kranenburg (1):
  grub-module-verifier: Ignore all_video for xenpvh

Juergen Gross (19):
  xen: add some xen headers
  loader/linux: support passing rsdp address via boot params
  xen: carve out grant tab initialization into dedicated function
  xen: prepare common code for Xen PVH support
  xen: add some dummy headers for PVH mode
  xen: rearrange xen/init.c to prepare it for Xen PVH mode
  xen: modify grub_xen_ptr2mfn() for xen-pvh
  xen: add PVH specific defines to offset.h
  xen: add basic hooks for PVH in current code
  xen: add PVH boot entry code
  xen: setup hypercall page for PVH
  xen: get memory map from hypervisor for PVH
  xen: setup Xen specific data for PVH
  xen: init memory regions for PVH
  xen_pvh: add build runes for grub-core
  xen: use elfnote defines instead of plain numbers
  xen_pvh: support building a standalone image
  xen_pvh: support grub-install for xen_pvh
  xen_pvh: add support to configure

 configure.ac  |   3 +
 gentpl.py |   4 +-
 grub-core/Makefile.am |  18 +
 grub-core/Makefile.core.def   |  35 ++
 grub-core/kern/i386/tsc.c |   2 +-
 grub-core/kern/i386/xen/pvh.c | 369 +++
 grub-core/kern/i386/xen/startup_pvh.S |  81 +
 grub-core/kern/xen/init.c | 101 --
 grub-core/loader/i386/linux.c |   5 +
 grub-core/mmap/i386/pc/mmap.c |   8 -
 include/grub/i386/coreboot/kernel.h   |   0
 include/grub/i386/efi/kernel.h|   0
 include/grub/i386/ieee1275/kernel.h   |   0
 include/grub/i386/linux.h |   3 +-
 include/grub/i386/memory.h|   7 +
 include/grub/i386/pc/int.h|  36 +-
 include/grub/i386/pc/int_types.h  |  59 +++
 include/grub/i386/tsc.h   |   2 +-
 include/grub/i386/xen/hypercall.h |   5 +-
 include/grub/i386/xen/kernel.h|   0
 include/grub/i386/xen_pvh/boot.h  |   1 +
 include/grub/i386/xen_pvh/console.h   |   1 +
 include/grub/i386/xen_pvh/int.h   |   1 +
 include/grub/i386/xen_pvh/kernel.h|  30 ++
 include/grub/i386/xen_pvh/memory.h|   1 +
 include/grub/i386/xen_pvh/time.h  |   1 +
 include/grub/kernel.h |   4 +-
 include/grub/offsets.h|  21 +-
 include/grub/util/install.h   |   1 +
 include/grub/util/mkimage.h   |   3 +-
 include/grub/x86_64/efi/kernel.h  |   0
 include/grub/xen.h|   5 +
 include/xen/arch-x86/xen.h|   7 +
 include/xen/hvm/hvm_op.h  | 296 +++
 include/xen/hvm/params.h  | 284 +++
 include/xen/hvm/start_info.h  |  98 +
 include/xen/memory.h  | 665 ++
 include/xen/physdev.h | 387 
 include/xen/trace.h   | 339 +
 include/xen/xen.h | 104 --
 util/grub-install-common.c|   1 +
 util/grub-install.c   |   7 +
 util/grub-mkimage32.c |   4 +-
 util/grub-mkimage64.c |   4 +-
 util/grub-mkimagexx.c |  58 ++-
 util/grub-module-verifier.c   |   1 +
 util/mkimage.c|  23 +-
 47 files changed, 2941 insertions(+), 144 deletions(-)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create m

[PATCH v7 08/20] xen: add PVH specific defines to offset.h

2018-12-07 Thread Juergen Gross
include/grub/offsets.h needs some defines for Xen PVH mode.

Add them. While at it line up the values in the surrounding lines to
start at the same column.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: XENPVH->XEN_PVH (Daniel Kiper)
---
 include/grub/offsets.h | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/include/grub/offsets.h b/include/grub/offsets.h
index 330e4c707..871e1cd4c 100644
--- a/include/grub/offsets.h
+++ b/include/grub/offsets.h
@@ -36,9 +36,10 @@
 #define GRUB_DECOMPRESSOR_I386_PC_MAX_DECOMPRESSOR_SIZE (0x9000-0x8200)
 
 /* The segment where the kernel is loaded.  */
-#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
+#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
 
-#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR 0x10
 
 /* The upper memory area (starting at 640 kiB).  */
 #define GRUB_MEMORY_I386_PC_UPPER  0xa
@@ -101,15 +102,17 @@
 #define GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN   
GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN
 
 #define GRUB_KERNEL_X86_64_XEN_MOD_ALIGN   0x8
-#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_ALIGN 0x8
 
 /* Non-zero value is only needed for PowerMacs.  */
-#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP 0x0
-#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP 0x0
+#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
+#define GRUB_KERNEL_I386_XEN_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP  0x0
+#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP  0x0
+#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP   0x0
+#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP  0x0
 
 #define GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN 0x1000
 #define GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN 3
-- 
2.16.4


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


[PATCH v6.1 09/20] xen: add basic hooks for PVH in current code

2018-12-06 Thread Juergen Gross
Add the hooks to current code needed for Xen PVH. They will be filled
with code later when the related functionality is being added.

loader/i386/linux.c needs to include machine/kernel.h now as it needs
to get GRUB_KERNEL_USE_RSDP_ADDR from there. This in turn requires to
add an empty kernel.h header for some i386 platforms (efi, coreboot,
ieee1275, xen).

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: xenpvh->xen_pvh (Daniel Kiper)
adjust copyright date (Roger Pau Monné)
V5: update commit message (Daniel Kiper)
move including xen/hvm/start_info.h to the sources really needing
  it (Daniel Kiper)
V6.1: add empty kernel.h headers for i386 platforms
It should be noted that i386_efi build is broken even without this
patch, but this is clearly beyond the scope of this series.
---
 grub-core/Makefile.am |  5 +
 grub-core/kern/i386/xen/pvh.c | 37 +++
 grub-core/kern/i386/xen/startup_pvh.S | 29 +++
 grub-core/kern/xen/init.c |  4 
 grub-core/loader/i386/linux.c |  1 +
 include/grub/i386/coreboot/kernel.h   |  0
 include/grub/i386/ieee1275/kernel.h   |  0
 include/grub/i386/xen/kernel.h|  0
 include/grub/i386/xen_pvh/kernel.h| 30 
 include/grub/xen.h|  5 +
 10 files changed, 111 insertions(+)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/coreboot/kernel.h
 create mode 100644 include/grub/i386/ieee1275/kernel.h
 create mode 100644 include/grub/i386/xen/kernel.h
 create mode 100644 include/grub/i386/xen_pvh/kernel.h

diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index f4ff62b76..89433f498 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -102,6 +102,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_efi
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
@@ -111,6 +112,7 @@ KERNEL_HEADER_FILES += 
$(top_srcdir)/include/grub/i386/pmtimer.h
 endif
 
 if COND_i386_coreboot
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/coreboot/lbio.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/video.h
@@ -122,6 +124,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 endif
 
 if COND_i386_multiboot
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/acpi.h
 endif
@@ -132,6 +135,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_ieee1275
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/ieee1275/ieee1275.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
@@ -140,6 +144,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
 if COND_i386_xen
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/xen.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/xen/hypercall.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
new file mode 100644
index 0..4f629b15e
--- /dev/null
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -0,0 +1,37 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+grub_uint64_t grub_rsdp_addr;
+
+void
+grub_xen_setup_pvh (void)
+{
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+}
diff --git a/grub-core/kern/i386/xen/startup_pvh.S 
b/grub-core/kern/i386/xen/startup_pvh.S
new file mode 100644
index 

[PATCH v6 16/20] grub-module-verifier: Ignore all_video for xenpvh

2018-11-28 Thread Juergen Gross
From: Hans van Kranenburg 

This solves the build failing with "Error: no symbol table and no
.moddeps section"

Also see:
- 6371e9c10433578bb236a8284ddb9ce9e201eb59
- https://savannah.gnu.org/bugs/?49012

Signed-off-by: Hans van Kranenburg 
Reviewed-by: Daniel Kiper 
---
V2: new patch
Signed-off-by: Juergen Gross 
---
 util/grub-module-verifier.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index 03ba1ab43..97cb9 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -129,6 +129,7 @@ struct platform_whitelist {
 
 static struct platform_whitelist whitelists[] = {
   {"i386", "xen", (const char *[]) {"all_video", 0}},
+  {"i386", "xen_pvh", (const char *[]) {"all_video", 0}},
   {"x86_64", "xen", (const char *[]) {"all_video", 0}},
   {"sparc64", "ieee1275", (const char *[]) {"all_video", 0}},
 
-- 
2.16.4


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


[PATCH v6 18/20] xen_pvh: support building a standalone image

2018-11-28 Thread Juergen Gross
Support mkimage for xen_pvh.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: some style adjustments (Daniel Kiper)
use defines for elf-notes (Daniel Kiper)
V5: move elf-note define usage into new patch (Daniel Kiper)

I didn't replace the 4096 by a PAGE_SIZE macro as requested by Daniel,
as there isn't such a macro easily available for util/mkimage.c and
I didn't introduce its usage.
---
 include/grub/util/mkimage.h |  3 ++-
 util/grub-mkimage32.c   |  4 +++-
 util/grub-mkimage64.c   |  4 +++-
 util/grub-mkimagexx.c   | 44 
 util/mkimage.c  | 23 ++-
 5 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/include/grub/util/mkimage.h b/include/grub/util/mkimage.h
index b3a5ca132..ba9f568f6 100644
--- a/include/grub/util/mkimage.h
+++ b/include/grub/util/mkimage.h
@@ -71,7 +71,8 @@ struct grub_install_image_target_desc
 IMAGE_I386_IEEE1275,
 IMAGE_LOONGSON_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH,
 IMAGE_FULOONG2F_FLASH, IMAGE_I386_PC_PXE, IMAGE_MIPS_ARC,
-IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO
+IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO,
+IMAGE_XEN_PVH
   } id;
   enum
 {
diff --git a/util/grub-mkimage32.c b/util/grub-mkimage32.c
index 1f2d2..026a2dd59 100644
--- a/util/grub-mkimage32.c
+++ b/util/grub-mkimage32.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF32_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF32_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF32_ST_TYPE(val)
-#define XEN_NOTE_SIZE 132
+
+#define XEN_NOTE_SIZE  132
+#define XEN_PVH_NOTE_SIZE  20
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimage64.c b/util/grub-mkimage64.c
index 4ff72a625..170defb40 100644
--- a/util/grub-mkimage64.c
+++ b/util/grub-mkimage64.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF64_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF64_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF64_ST_TYPE(val)
-#define XEN_NOTE_SIZE 120
+
+#define XEN_NOTE_SIZE  120
+#define XEN_PVH_NOTE_SIZE  24
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index 784ed1a52..e94a721b4 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -229,12 +229,12 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phnum++;
   footer_size += sizeof (struct grub_ieee1275_note);
 }
-  if (image_target->id == IMAGE_XEN)
+  if (image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH)
 {
   phnum++;
   shnum++;
   string_size += sizeof (".xen");
-  footer_size += XEN_NOTE_SIZE;
+  footer_size += (image_target->id == IMAGE_XEN) ? XEN_NOTE_SIZE : 
XEN_PVH_NOTE_SIZE;
 }
   header_size = ALIGN_UP (sizeof (*ehdr) + phnum * sizeof (*phdr)
  + shnum * sizeof (*shdr) + string_size, 
layout->align);
@@ -421,6 +421,39 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phdr->p_offset = grub_host_to_target32 (header_size + program_size);
 }
 
+  if (image_target->id == IMAGE_XEN_PVH)
+{
+  char *note_start = (elf_img + program_size + header_size);
+  Elf_Nhdr *note_ptr;
+  char *ptr = (char *) note_start;
+
+  grub_util_info ("adding XEN NOTE segment");
+
+  /* Phys32 Entry.  */
+  note_ptr = (Elf_Nhdr *) ptr;
+  note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
+  note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PHYS32_ENTRY);
+  ptr += sizeof (Elf_Nhdr);
+  memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
+  ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
+  memset (ptr, 0, image_target->voidp_sizeof);
+  *(grub_uint32_t *) ptr = GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR;
+  ptr += image_target->voidp_sizeof;
+
+  assert (XEN_PVH_NOTE_SIZE == (ptr - note_start));
+
+  phdr++;
+  phdr->p_type = grub_host_to_target32 (PT_NOTE);
+  phdr->p_flags = grub_host_to_target32 (PF_R);
+  phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
+  phdr->p_vaddr = 0;
+  phdr->p_paddr = 0;
+  phdr->p_filesz = grub_host_to_target32 (XEN_PVH_NOTE_SIZE);
+  phdr->p_memsz = 0;
+  phdr->p_offset = grub_host_to_target32 (header_size + program_size);
+}
+
   if (note)
 {
   int note_size = sizeof (struct grub_ieee1275_note);
@@ -496,7 +529,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
 shdr->sh_entsize = grub_host_to_target32 (0);

[PATCH v6 19/20] xen_pvh: support grub-install for xen_pvh

2018-11-28 Thread Juergen Gross
Add xen_pvh support to grub-install.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 include/grub/util/install.h | 1 +
 util/grub-install-common.c  | 1 +
 util/grub-install.c | 7 +++
 3 files changed, 9 insertions(+)

diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 0dba8b67f..af2bf65d7 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -100,6 +100,7 @@ enum grub_install_plat
 GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS,
 GRUB_INSTALL_PLATFORM_I386_XEN,
 GRUB_INSTALL_PLATFORM_X86_64_XEN,
+GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
 GRUB_INSTALL_PLATFORM_ARM64_EFI,
 GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
 GRUB_INSTALL_PLATFORM_MAX
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
index 0a2e24a79..1b1cb43b4 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -716,6 +716,7 @@ static struct
 [GRUB_INSTALL_PLATFORM_X86_64_EFI] =   { "x86_64",  "efi"   },
 [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386","xen"   },
 [GRUB_INSTALL_PLATFORM_X86_64_XEN] =   { "x86_64",  "xen"   },
+[GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386","xen_pvh"   },
 [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",  "loongson"  },
 [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",  "qemu_mips" },
 [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips","qemu_mips" },
diff --git a/util/grub-install.c b/util/grub-install.c
index 4375c1619..743296f36 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -496,6 +496,7 @@ have_bootdev (enum grub_install_plat pl)
 
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   return 0;
 
   /* pacify warning.  */
@@ -913,6 +914,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
 
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
@@ -960,6 +962,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   free (install_device);
   install_device = NULL;
   break;
@@ -1477,6 +1480,7 @@ main (int argc, char *argv[])
  case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  case GRUB_INSTALL_PLATFORM_I386_XEN:
  case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+ case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
grub_util_warn ("%s", _("no hints available for your 
platform. Expect reduced performance"));
break;
/* pacify warning.  */
@@ -1568,6 +1572,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   core_name = "core.elf";
   snprintf (mkimage_target, sizeof (mkimage_target),
"%s-%s",
@@ -1660,6 +1665,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
   /* pacify warning.  */
 case GRUB_INSTALL_PLATFORM_MAX:
@@ -1926,6 +1932,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   grub_util_warn ("%s",
  _("WARNING: no platform-specific install was performed"));
   break;
-- 
2.16.4


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


[PATCH v6 15/20] xen_pvh: add build runes for grub-core

2018-11-28 Thread Juergen Gross
Add the modifications to the build system needed to build a xen_pvh
grub.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: sorted some filenames (Daniel Kiper)
V4: add bus/pci.c to xen_pvh
---
 gentpl.py   |  4 ++--
 grub-core/Makefile.am   | 12 
 grub-core/Makefile.core.def | 35 +++
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/gentpl.py b/gentpl.py
index da67965a4..e8439484a 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -28,7 +28,7 @@ import re
 
 GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
"i386_multiboot", "i386_ieee1275", "x86_64_efi",
-   "i386_xen", "x86_64_xen",
+   "i386_xen", "x86_64_xen", "i386_xen_pvh",
"mips_loongson", "sparc64_ieee1275",
"powerpc_ieee1275", "mips_arc", "ia64_efi",
"mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi",
@@ -71,7 +71,7 @@ GROUPS["videomodules"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
 
 # Similar for terminfo
-GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips" ] + GROUPS["xen"] + GROUPS["ieee1275"] + GROUPS["uboot"];
+GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips", "i386_xen_pvh" ] + GROUPS["xen"] + GROUPS["ieee1275"] + 
GROUPS["uboot"];
 GROUPS["terminfomodule"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
 
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index f4ff62b76..02cb5e33d 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -101,6 +101,18 @@ KERNEL_HEADER_FILES += 
$(top_builddir)/include/grub/machine/int.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
+if COND_i386_xen_pvh
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/loader.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/xen.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/xen/hypercall.h
+endif
+
 if COND_i386_efi
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 2d75c4daf..f31301e04 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -79,6 +79,8 @@ kernel = {
   i386_xen_ldflags = '$(TARGET_IMG_BASE_LDOPT),0';
   x86_64_xen_ldflags   = '$(TARGET_IMG_LDFLAGS)';
   x86_64_xen_ldflags   = '$(TARGET_IMG_BASE_LDOPT),0';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_LDFLAGS)';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x10';
 
   mips_loongson_ldflags= '-Wl,-Ttext,0x8020';
   powerpc_ieee1275_ldflags = '-Wl,-Ttext,0x20';
@@ -100,6 +102,7 @@ kernel = {
   x86_64_efi_startup = kern/x86_64/efi/startup.S;
   i386_xen_startup = kern/i386/xen/startup.S;
   x86_64_xen_startup = kern/x86_64/xen/startup.S;
+  i386_xen_pvh_startup = kern/i386/xen/startup_pvh.S;
   i386_qemu_startup = kern/i386/qemu/startup.S;
   i386_ieee1275_startup = kern/i386/ieee1275/startup.S;
   i386_coreboot_startup = kern/i386/coreboot/startup.S;
@@ -177,6 +180,7 @@ kernel = {
 
   i386 = kern/i386/dl.c;
   i386_xen = kern/i386/dl.c;
+  i386_xen_pvh = kern/i386/dl.c;
 
   i386_coreboot = kern/i386/coreboot/init.c;
   i386_multiboot = kern/i386/coreboot/init.c;
@@ -222,6 +226,14 @@ kernel = {
   xen = disk/xen/xendisk.c;
   xen = commands/boot.c;
 
+  i386_xen_pvh = commands/boot.c;
+  i386_xen_pvh = disk/xen/xendisk.c;
+  i386_xen_pvh = kern/i386/tsc.c;
+  i386_xen_pvh = kern/i386/xen/tsc.c;
+  i386_xen_pvh = kern/i386/xen/pvh.c;
+  i386_xen_pvh = kern/xen/init.c;
+  i386_xen_pvh = term/xen/console.c;
+
   ia64_efi = kern/ia64/efi/startup.S;
   ia64_efi = kern/ia64/efi/init.c;
   ia64_efi = kern/ia64/dl.c;
@@ -801,6 +813,7 @@ module = {
   name = cpuid;
   common = commands/i386/cpuid.c;
   enable = x86;
+  enable = i386_xen_pvh;
   enable = i386_xen;
   enable = x86_64_xen;
 };
@@ -860,6 +873,7 @@ module = {
   i386_coreboot = 

[PATCH v6 17/20] xen: use elfnote defines instead of plain numbers

2018-11-28 Thread Juergen Gross
In order to avoid using plain integers for the ELF notes use the
available Xen include instead.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: new patch (Daniel Kiper)
---
 util/grub-mkimagexx.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a483c674c..784ed1a52 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -48,6 +48,8 @@
 #include 
 #include 
 
+#include 
+
 #pragma GCC diagnostic ignored "-Wcast-align"
 
 #define GRUB_MKIMAGEXX
@@ -341,7 +343,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof (PACKAGE_NAME));
-  note_ptr->n_type = grub_host_to_target32 (6);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_GUEST_OS);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -352,7 +354,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("generic"));
-  note_ptr->n_type = grub_host_to_target32 (8);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_LOADER);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -363,7 +365,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("xen-3.0"));
-  note_ptr->n_type = grub_host_to_target32 (5);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_XEN_VERSION);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -374,7 +376,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
-  note_ptr->n_type = grub_host_to_target32 (1);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_ENTRY);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -385,7 +387,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
-  note_ptr->n_type = grub_host_to_target32 (3);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_VIRT_BASE);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -398,7 +400,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
  note_ptr = (Elf_Nhdr *) ptr;
  note_ptr->n_namesz = grub_host_to_target32 (sizeof 
(GRUB_XEN_NOTE_NAME));
  note_ptr->n_descsz = grub_host_to_target32 (sizeof ("yes,bimodal"));
- note_ptr->n_type = grub_host_to_target32 (9);
+ note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PAE_MODE);
  ptr += sizeof (Elf_Nhdr);
  memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
-- 
2.16.4


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


[PATCH v6 01/20] xen: add some xen headers

2018-11-28 Thread Juergen Gross
In order to support grub2 in Xen PVH environment some additional Xen
headers are needed as grub2 will be started in PVH mode requiring to
use several HVM hypercalls and structures.

Add the needed headers from Xen 4.10 being the first Xen version with
full (not only experimental) PVH guest support.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 include/xen/hvm/hvm_op.h | 296 +++
 include/xen/hvm/params.h | 284 ++
 include/xen/hvm/start_info.h |  98 +++
 include/xen/memory.h | 665 +++
 include/xen/physdev.h| 387 +
 include/xen/trace.h  | 339 ++
 include/xen/xen.h| 104 +--
 7 files changed, 2142 insertions(+), 31 deletions(-)
 create mode 100644 include/xen/hvm/hvm_op.h
 create mode 100644 include/xen/hvm/params.h
 create mode 100644 include/xen/hvm/start_info.h
 create mode 100644 include/xen/memory.h
 create mode 100644 include/xen/physdev.h
 create mode 100644 include/xen/trace.h

diff --git a/include/xen/hvm/hvm_op.h b/include/xen/hvm/hvm_op.h
new file mode 100644
index 0..0bdafdf59
--- /dev/null
+++ b/include/xen/hvm/hvm_op.h
@@ -0,0 +1,296 @@
+/*
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2007, Keir Fraser
+ */
+
+#ifndef __XEN_PUBLIC_HVM_HVM_OP_H__
+#define __XEN_PUBLIC_HVM_HVM_OP_H__
+
+#include "../xen.h"
+#include "../trace.h"
+#include "../event_channel.h"
+
+/* Get/set subcommands: extra argument == pointer to xen_hvm_param struct. */
+#define HVMOP_set_param   0
+#define HVMOP_get_param   1
+struct xen_hvm_param {
+domid_t  domid;/* IN */
+uint32_t index;/* IN */
+uint64_t value;/* IN/OUT */
+};
+typedef struct xen_hvm_param xen_hvm_param_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_param_t);
+
+#if __XEN_INTERFACE_VERSION__ < 0x00040900
+
+/* Set the logical level of one of a domain's PCI INTx wires. */
+#define HVMOP_set_pci_intx_level  2
+struct xen_hvm_set_pci_intx_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI INTx identification in PCI topology (domain:bus:device:intx). */
+uint8_t  domain, bus, device, intx;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_pci_intx_level xen_hvm_set_pci_intx_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_intx_level_t);
+
+/* Set the logical level of one of a domain's ISA IRQ wires. */
+#define HVMOP_set_isa_irq_level   3
+struct xen_hvm_set_isa_irq_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* ISA device identification, by ISA IRQ (0-15). */
+uint8_t  isa_irq;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_isa_irq_level xen_hvm_set_isa_irq_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_isa_irq_level_t);
+
+#define HVMOP_set_pci_link_route  4
+struct xen_hvm_set_pci_link_route {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI link identifier (0-3). */
+uint8_t  link;
+/* ISA IRQ (1-15), or 0 (disable link). */
+uint8_t  isa_irq;
+};
+typedef struct xen_hvm_set_pci_link_route xen_hvm_set_pci_link_route_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_link_route_t);
+
+#endif /* __XEN_INTERFACE_VERSION__ < 0x00040900 */
+
+/* Flushes all VCPU TLBs: @arg must be NULL. */
+#define HVMOP_flush_tlbs  5
+
+typedef enum {
+HVMMEM_ram_rw, /* Normal read/write guest RAM */
+HVMMEM_ram_ro, /* Read-only; writes are discarded */
+HVMMEM_mmio_dm,/* Reads and write go to the device model */
+#if __XEN_INTERFACE_VERSION__ < 0x00040700
+HVMMEM_mmio_write_dm,  /* Read-only; writes go to the device model */
+#else
+HVMMEM_unus

[PATCH v6 20/20] xen_pvh: add support to configure

2018-11-28 Thread Juergen Gross
Support platform i386/xen_pvh in configure.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 configure.ac | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.ac b/configure.ac
index 5e63c4af3..81a19afd5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -151,6 +151,7 @@ case "$target_cpu"-"$platform" in
   i386-efi) ;;
   x86_64-efi) ;;
   i386-xen) ;;
+  i386-xen_pvh) ;;
   x86_64-xen) ;;
   i386-pc) ;;
   i386-multiboot) ;;
@@ -219,6 +220,7 @@ case "$platform" in
   multiboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MULTIBOOT=1" 
;;
   efi) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EFI=1" ;;
   xen) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN=1" ;;
+  xen_pvh) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN_PVH=1" ;;
   ieee1275)machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" 
;;
   uboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
   qemu)machine_CPPFLAGS="$machine_CPPFLAGS 
-DGRUB_MACHINE_QEMU=1" ;;
@@ -1897,6 +1899,7 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = 
xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform 
= xmultiboot])
 AM_CONDITIONAL([COND_x86_64_efi], [test x$target_cpu = xx86_64 -a x$platform = 
xefi])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = 
xxen])
+AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = 
xxen_pvh])
 AM_CONDITIONAL([COND_x86_64_xen], [test x$target_cpu = xx86_64 -a x$platform = 
xxen])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a 
x$platform = xloongson])
 AM_CONDITIONAL([COND_mips_qemu_mips], [test "(" x$target_cpu = xmips -o 
x$target_cpu = xmipsel ")"  -a x$platform = xqemu_mips])
-- 
2.16.4


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


[PATCH v6 06/20] xen: rearrange xen/init.c to prepare it for Xen PVH mode

2018-11-28 Thread Juergen Gross
Rearrange grub-core/kern/xen/init.c to prepare adding PVH mode support
to it. This includes putting some code under #ifdef GRUB_MACHINE_XEN
as it will not be used when running as PVH.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/xen/init.c | 60 +++
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 29f5bc23d..10007b411 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -41,9 +41,11 @@ grub_size_t grub_xen_n_allocated_shared_pages;
 static grub_xen_mfn_t
 grub_xen_ptr2mfn (void *ptr)
 {
+#ifdef GRUB_MACHINE_XEN
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#endif
 }
 
 void *
@@ -104,18 +106,6 @@ grub_machine_get_bootlocation (char **device __attribute__ 
((unused)),
 {
 }
 
-static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
-  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
-
-#ifdef __x86_64__
-#define NUMBER_OF_LEVELS 4
-#else
-#define NUMBER_OF_LEVELS 3
-#endif
-
-#define LOG_POINTERS_PER_PAGE 9
-#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
-
 void
 grub_xen_store_send (const void *buf_, grub_size_t len)
 {
@@ -337,6 +327,19 @@ grub_xen_setup_gnttab (void)
   grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
 }
 
+#ifdef GRUB_MACHINE_XEN
+static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+#ifdef __x86_64__
+#define NUMBER_OF_LEVELS 4
+#else
+#define NUMBER_OF_LEVELS 3
+#endif
+
+#define LOG_POINTERS_PER_PAGE 9
+#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -529,13 +532,30 @@ map_all_pages (void)
   grub_mm_init_region ((void *) heap_start, heap_end - heap_start);
 }
 
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
+  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
+  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
+return GRUB_ERR_NONE;
+
+  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
+   GRUB_MEMORY_RESERVED, hook_data);
+
+  return GRUB_ERR_NONE;
+}
+#endif
+
 extern char _end[];
 
 void
 grub_machine_init (void)
 {
+#ifdef GRUB_MACHINE_XEN
 #ifdef __i386__
   grub_xen_vm_assist (VMASST_CMD_enable, VMASST_TYPE_pae_extended_cr3);
+#endif
 #endif
 
   grub_modbase = ALIGN_UP ((grub_addr_t) _end
@@ -544,7 +564,9 @@ grub_machine_init (void)
 
   grub_xen_setup_gnttab ();
 
+#ifdef GRUB_MACHINE_XEN
   map_all_pages ();
+#endif
 
   grub_console_init ();
 
@@ -571,17 +593,3 @@ grub_machine_fini (int flags __attribute__ ((unused)))
   grub_xendisk_fini ();
   grub_boot_fini ();
 }
-
-grub_err_t
-grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
-{
-  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
-  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
-  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
-return GRUB_ERR_NONE;
-
-  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
-   GRUB_MEMORY_RESERVED, hook_data);
-
-  return GRUB_ERR_NONE;
-}
-- 
2.16.4


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


[PATCH v6 12/20] xen: get memory map from hypervisor for PVH

2018-11-28 Thread Juergen Gross
Retrieve the memory map from the hypervisor and normalize it to contain
no overlapping entries and to be sorted by address.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: use grub_e820_mmap_entry instead of own struct (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 94 +++
 1 file changed, 94 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index a2554fb1d..2b68ac333 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+
+#define XEN_MEMORY_MAP_SIZE   128
 
 grub_uint64_t grub_rsdp_addr;
 
@@ -32,6 +37,8 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
+static unsigned int nr_map_entries;
 
 static void
 grub_xen_cons_msg (const char *msg)
@@ -104,11 +111,98 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t 
a0,
   return res;
 }
 
+static void
+grub_xen_sort_mmap (void)
+{
+  grub_uint64_t from, to;
+  unsigned int i;
+  struct grub_e820_mmap_entry tmp;
+
+  /* Align map entries to page boundaries. */
+  for (i = 0; i < nr_map_entries; i++)
+{
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (map[i].type == GRUB_MEMORY_AVAILABLE)
+   {
+ from = ALIGN_UP (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_DOWN (to, GRUB_XEN_PAGE_SIZE);
+   }
+  else
+   {
+ from = ALIGN_DOWN (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_UP (to, GRUB_XEN_PAGE_SIZE);
+   }
+  map[i].addr = from;
+  map[i].len = to - from;
+}
+
+ again:
+  /* Sort entries by start address. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr)
+   continue;
+  tmp = map[i];
+  map[i] = map[i - 1];
+  map[i - 1] = tmp;
+  i = 0;
+}
+
+  /* Detect overlapping areas. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr + map[i - 1].len)
+   continue;
+  tmp = map[i - 1];
+  map[i - 1].len = map[i].addr - map[i - 1].addr;
+  if (map[i].addr + map[i].len >= tmp.addr + tmp.len)
+   continue;
+  if (nr_map_entries < ARRAY_SIZE (map))
+   {
+ map[nr_map_entries].addr = map[i].addr + map[i].len;
+ map[nr_map_entries].len = tmp.addr + tmp.len - 
map[nr_map_entries].addr;
+ map[nr_map_entries].type = tmp.type;
+ nr_map_entries++;
+ goto again;
+   }
+}
+
+  /* Merge adjacent entries. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].type == map[i - 1].type &&
+ map[i].addr == map[i - 1].addr + map[i - 1].len)
+   {
+ map[i - 1].len += map[i].len;
+ map[i] = map[nr_map_entries - 1];
+ nr_map_entries--;
+ goto again;
+   }
+}
+}
+
+static void
+grub_xen_get_mmap (void)
+{
+  struct xen_memory_map memmap;
+
+  memmap.nr_entries = ARRAY_SIZE (map);
+  set_xen_guest_handle (memmap.buffer, map);
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Could not get memory map from Xen!\n");
+  nr_map_entries = memmap.nr_entries;
+
+  grub_xen_sort_mmap ();
+}
+
 void
 grub_xen_setup_pvh (void)
 {
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
+  grub_xen_get_mmap ();
 }
 
 grub_err_t
-- 
2.16.4


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


[PATCH v6 09/20] xen: add basic hooks for PVH in current code

2018-11-28 Thread Juergen Gross
Add the hooks to current code needed for Xen PVH. They will be filled
with code later when the related functionality is being added.

loader/i386/linux.c needs to include machine/kernel.h now as it needs
to get GRUB_KERNEL_USE_RSDP_ADDR from there.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: xenpvh->xen_pvh (Daniel Kiper)
adjust copyright date (Roger Pau Monné)
V5: update commit message (Daniel Kiper)
move including xen/hvm/start_info.h to the sources really needing
  it (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 37 +++
 grub-core/kern/i386/xen/startup_pvh.S | 29 +++
 grub-core/kern/xen/init.c |  4 
 grub-core/loader/i386/linux.c |  1 +
 include/grub/i386/xen_pvh/kernel.h| 30 
 include/grub/xen.h|  5 +
 6 files changed, 106 insertions(+)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/xen_pvh/kernel.h

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
new file mode 100644
index 0..4f629b15e
--- /dev/null
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -0,0 +1,37 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+grub_uint64_t grub_rsdp_addr;
+
+void
+grub_xen_setup_pvh (void)
+{
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+}
diff --git a/grub-core/kern/i386/xen/startup_pvh.S 
b/grub-core/kern/i386/xen/startup_pvh.S
new file mode 100644
index 0..69b8fdcca
--- /dev/null
+++ b/grub-core/kern/i386/xen/startup_pvh.S
@@ -0,0 +1,29 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+   .file   "startup_pvh.S"
+   .text
+
+/* Saved pointer to start info structure. */
+   .globl  pvh_start_info
+pvh_start_info:
+   .long   0
diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index a23dad633..782ca7295 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -564,6 +564,10 @@ grub_machine_init (void)
   + GRUB_KERNEL_MACHINE_MOD_GAP,
   GRUB_KERNEL_MACHINE_MOD_ALIGN);
 
+#ifdef GRUB_MACHINE_XEN_PVH
+  grub_xen_setup_pvh ();
+#endif
+
   grub_xen_setup_gnttab ();
 
 #ifdef GRUB_MACHINE_XEN
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index 375ee80dc..b6015913b 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -35,6 +35,7 @@
 #include 
 #include 
 #include 
+#include 
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
diff --git a/include/grub/i386/xen_pvh/kernel.h 
b/include/grub/i386/xen_pvh/kernel.h
new file mode 100644
index 0..2b7b8a129
--- /dev/null
+++ b/include/grub/i386/xen_pvh/kernel.h
@@ -0,0 +1,30 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOS

[PATCH v6 07/20] xen: modify grub_xen_ptr2mfn() for xen-pvh

2018-11-28 Thread Juergen Gross
grub_xen_ptr2mfn() returns the machine frame number for a given pointer
value. For Xen-PVH guests this is just the PFN. Add the PVH specific
variant.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: new patch (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 10007b411..a23dad633 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -45,6 +45,8 @@ grub_xen_ptr2mfn (void *ptr)
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#else
+  return (grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE;
 #endif
 }
 
-- 
2.16.4


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


[PATCH v6 13/20] xen: setup Xen specific data for PVH

2018-11-28 Thread Juergen Gross
Initialize the needed Xen specific data. This is:

- the Xen start of day page containing the console and Xenstore ring
  page PFN and event channel
- the grant table
- the shared info page

Write back the possibly modified memory map to the hypervisor in case
the guest is reading it from there again.

Set the RSDP address for the guest from the start_info page passed
as boot parameter.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
Reviewed-by: Roger Pau Monné 
---
V4: write back memory map to Xen (Roger Pau Monné)
V5: add comment (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 123 ++
 1 file changed, 123 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 2b68ac333..472085ed1 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -27,6 +27,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #define XEN_MEMORY_MAP_SIZE   128
@@ -37,6 +38,7 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct start_info grub_xen_start_page;
 static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
 static unsigned int nr_map_entries;
 
@@ -111,6 +113,36 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
   return res;
 }
 
+static grub_uint32_t
+grub_xen_get_param (int idx)
+{
+  struct xen_hvm_param xhv;
+  int r;
+
+  xhv.domid = DOMID_SELF;
+  xhv.index = idx;
+  r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+  if (r < 0)
+grub_xen_panic ("Could not get parameter from Xen!\n");
+  return xhv.value;
+}
+
+static void *
+grub_xen_add_physmap (unsigned int space, void *addr)
+{
+  struct xen_add_to_physmap xatp;
+
+  xatp.domid = DOMID_SELF;
+  xatp.idx = 0;
+  xatp.space = space;
+  xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Memory_op hypercall failed!\n");
+  return addr;
+}
+
 static void
 grub_xen_sort_mmap (void)
 {
@@ -197,12 +229,103 @@ grub_xen_get_mmap (void)
   grub_xen_sort_mmap ();
 }
 
+static void
+grub_xen_set_mmap (void)
+{
+  struct xen_foreign_memory_map memmap;
+
+  memmap.domid = DOMID_SELF;
+  memmap.map.nr_entries = nr_map_entries;
+  set_xen_guest_handle (memmap.map.buffer, map);
+  grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_set_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+}
+
+static grub_uint64_t
+grub_xen_find_page (grub_uint64_t start)
+{
+  unsigned int i, j;
+  grub_uint64_t last = start;
+
+  /*
+   * Try to find a e820 map hole below 4G.
+   * Relies on page-aligned entries (addr and len) and input (start).
+   */
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (last > map[i].addr + map[i].len)
+   continue;
+  if (last < map[i].addr)
+   return last;
+  if ((map[i].addr >> 32) || ((map[i].addr + map[i].len) >> 32))
+   break;
+  last = map[i].addr + map[i].len;
+}
+if (i == nr_map_entries)
+  return last;
+
+  /* No hole found, use the highest RAM page below 4G and reserve it. */
+  if (nr_map_entries == ARRAY_SIZE (map))
+grub_xen_panic ("Memory map size limit reached!\n");
+  for (i = 0, j = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+   continue;
+  if (map[i].addr >> 32)
+   break;
+  j = i;
+  if ((map[i].addr + map[i].len) >> 32)
+   break;
+}
+  if (map[j].type != GRUB_MEMORY_AVAILABLE)
+grub_xen_panic ("No free memory page found!\n");
+  if ((map[j].addr + map[j].len) >> 32)
+last = (1ULL << 32) - GRUB_XEN_PAGE_SIZE;
+  else
+last = map[j].addr + map[j].len - GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].addr = last;
+  map[nr_map_entries].len = GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
+  nr_map_entries++;
+  grub_xen_sort_mmap ();
+
+  return last;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_addr_t par;
+
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
   grub_xen_get_mmap ();
+
+  /* Setup Xen data. */
+  grub_xen_start_page_addr = _xen_start_page;
+
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_PFN);
+  grub_xen_start_page_addr->console.domU.mfn = par;
+  grub_xen_xcons = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_EVTCHN);
+  grub_xen_start_page_addr->console.domU.evtchn = par;
+
+  par = grub_xen_get_param (HVM_PARAM_STORE_PFN);
+  grub_xen_start_page_addr->store_mfn = par;
+  grub_xen_xenstore = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_STOR

[PATCH v6 05/20] xen: add some dummy headers for PVH mode

2018-11-28 Thread Juergen Gross
With Xen PVH mode adding a new machine type the machine related headers
need to be present for the build to succeed. Most of the headers just
need to include the related common i386 headers. Add those to the tree.

Note that xen_pvh/int.h needs to include pc/int_types.h instead of
pc/int.h in order to avoid the definition of grub_bios_interrupt().

xen_pvh/memory.h needs to include coreboot/memory.h (like some other
/memory.h do as well) as this contains just the needed stubs.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: updated commit message (Daniel Kiper)
xenpvh->xen_pvh (Daniel Kiper)
V5: updated commit message (Daniel Kiper)
---
 include/grub/i386/xen_pvh/boot.h| 1 +
 include/grub/i386/xen_pvh/console.h | 1 +
 include/grub/i386/xen_pvh/int.h | 1 +
 include/grub/i386/xen_pvh/memory.h  | 1 +
 include/grub/i386/xen_pvh/time.h| 1 +
 5 files changed, 5 insertions(+)
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/memory.h
 create mode 100644 include/grub/i386/xen_pvh/time.h

diff --git a/include/grub/i386/xen_pvh/boot.h b/include/grub/i386/xen_pvh/boot.h
new file mode 100644
index 0..6cd23aa83
--- /dev/null
+++ b/include/grub/i386/xen_pvh/boot.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/console.h 
b/include/grub/i386/xen_pvh/console.h
new file mode 100644
index 0..305a46d8e
--- /dev/null
+++ b/include/grub/i386/xen_pvh/console.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/int.h b/include/grub/i386/xen_pvh/int.h
new file mode 100644
index 0..0f1f9ee62
--- /dev/null
+++ b/include/grub/i386/xen_pvh/int.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/memory.h 
b/include/grub/i386/xen_pvh/memory.h
new file mode 100644
index 0..8dd6f7c8c
--- /dev/null
+++ b/include/grub/i386/xen_pvh/memory.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/time.h b/include/grub/i386/xen_pvh/time.h
new file mode 100644
index 0..2298ee8f4
--- /dev/null
+++ b/include/grub/i386/xen_pvh/time.h
@@ -0,0 +1 @@
+#include 
-- 
2.16.4


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


[PATCH v6 00/20] xen: add pvh guest support

2018-11-28 Thread Juergen Gross
This patch series adds support for booting Linux as PVH guest.

Similar to i386/xen and x86_64/xen platforms the new i386/xenpvh
platform grub is booted as a standalone image directly by Xen.

For booting Linux kernel it is using the standard linux kernel
loader. The only modification of the linux loader is to pass the
ACPI RSDP address via boot parameters to the kernel, as that table
might not be located at the usual physical address just below 1MB.

The related Linux kernel patches have been accepted for 4.20-rc4

Changes in V6:
- addressed a few comments, see individual patches (only minor changes)

Changes in V5:
- added new patches 7, 17
- removed (old) patch 12 again
- modified boot params interface for passing rsdp address on request
  by Peter Anvin (patch 2)
- addressed various comments, see individual patches

Changes in V4:
- added new patch 12
- write modified memory map back to Xen

Changes in V3:
- addressed lots of comments, most by Daniel and some by Roger,
  details are mentioned in the individual patches

Changes in V2:
- extend linux boot protocol version 2.14 to report back the protocol
  version supported by grub2 (needed as there are grub2 downstream
  variants in some distros which clobber data after the setup_header)
- split former patch 4 into 10 patches
- fix memmap handling for >4GB of memory
- added new patch from Hans (thanks for that one)
- addressed most of Daniel's comments (see individual patches)

Hans van Kranenburg (1):
  grub-module-verifier: Ignore all_video for xenpvh

Juergen Gross (19):
  xen: add some xen headers
  loader/linux: support passing rsdp address via boot params
  xen: carve out grant tab initialization into dedicated function
  xen: prepare common code for Xen PVH support
  xen: add some dummy headers for PVH mode
  xen: rearrange xen/init.c to prepare it for Xen PVH mode
  xen: modify grub_xen_ptr2mfn() for xen-pvh
  xen: add PVH specific defines to offset.h
  xen: add basic hooks for PVH in current code
  xen: add PVH boot entry code
  xen: setup hypercall page for PVH
  xen: get memory map from hypervisor for PVH
  xen: setup Xen specific data for PVH
  xen: init memory regions for PVH
  xen_pvh: add build runes for grub-core
  xen: use elfnote defines instead of plain numbers
  xen_pvh: support building a standalone image
  xen_pvh: support grub-install for xen_pvh
  xen_pvh: add support to configure

 configure.ac  |   3 +
 gentpl.py |   4 +-
 grub-core/Makefile.am |  12 +
 grub-core/Makefile.core.def   |  35 ++
 grub-core/kern/i386/tsc.c |   2 +-
 grub-core/kern/i386/xen/pvh.c | 369 +++
 grub-core/kern/i386/xen/startup_pvh.S |  81 +
 grub-core/kern/xen/init.c | 101 --
 grub-core/loader/i386/linux.c |   5 +
 grub-core/mmap/i386/pc/mmap.c |   8 -
 include/grub/i386/linux.h |   3 +-
 include/grub/i386/memory.h|   7 +
 include/grub/i386/pc/int.h|  36 +-
 include/grub/i386/pc/int_types.h  |  59 +++
 include/grub/i386/tsc.h   |   2 +-
 include/grub/i386/xen/hypercall.h |   5 +-
 include/grub/i386/xen_pvh/boot.h  |   1 +
 include/grub/i386/xen_pvh/console.h   |   1 +
 include/grub/i386/xen_pvh/int.h   |   1 +
 include/grub/i386/xen_pvh/kernel.h|  30 ++
 include/grub/i386/xen_pvh/memory.h|   1 +
 include/grub/i386/xen_pvh/time.h  |   1 +
 include/grub/kernel.h |   4 +-
 include/grub/offsets.h|  21 +-
 include/grub/util/install.h   |   1 +
 include/grub/util/mkimage.h   |   3 +-
 include/grub/xen.h|   5 +
 include/xen/arch-x86/xen.h|   7 +
 include/xen/hvm/hvm_op.h  | 296 +++
 include/xen/hvm/params.h  | 284 +++
 include/xen/hvm/start_info.h  |  98 +
 include/xen/memory.h  | 665 ++
 include/xen/physdev.h | 387 
 include/xen/trace.h   | 339 +
 include/xen/xen.h | 104 --
 util/grub-install-common.c|   1 +
 util/grub-install.c   |   7 +
 util/grub-mkimage32.c |   4 +-
 util/grub-mkimage64.c |   4 +-
 util/grub-mkimagexx.c |  58 ++-
 util/grub-module-verifier.c   |   1 +
 util/mkimage.c|  23 +-
 42 files changed, 2935 insertions(+), 144 deletions(-)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/pc/int_types.h
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/kernel.h
 create mode 100644 include/grub/i

[PATCH v6 08/20] xen: add PVH specific defines to offset.h

2018-11-28 Thread Juergen Gross
include/grub/offsets.h needs some defines for Xen PVH mode.

Add them. While at it line up the values in the surrounding lines to
start at the same column.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: XENPVH->XEN_PVH (Daniel Kiper)
---
 include/grub/offsets.h | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/include/grub/offsets.h b/include/grub/offsets.h
index 330e4c707..871e1cd4c 100644
--- a/include/grub/offsets.h
+++ b/include/grub/offsets.h
@@ -36,9 +36,10 @@
 #define GRUB_DECOMPRESSOR_I386_PC_MAX_DECOMPRESSOR_SIZE (0x9000-0x8200)
 
 /* The segment where the kernel is loaded.  */
-#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
+#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
 
-#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR 0x10
 
 /* The upper memory area (starting at 640 kiB).  */
 #define GRUB_MEMORY_I386_PC_UPPER  0xa
@@ -101,15 +102,17 @@
 #define GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN   
GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN
 
 #define GRUB_KERNEL_X86_64_XEN_MOD_ALIGN   0x8
-#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_ALIGN 0x8
 
 /* Non-zero value is only needed for PowerMacs.  */
-#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP 0x0
-#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP 0x0
+#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
+#define GRUB_KERNEL_I386_XEN_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP  0x0
+#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP  0x0
+#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP   0x0
+#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP  0x0
 
 #define GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN 0x1000
 #define GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN 3
-- 
2.16.4


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


[PATCH v6 14/20] xen: init memory regions for PVH

2018-11-28 Thread Juergen Gross
Add all usable memory regions to grub memory management and add the
needed mmap iterate code, which will be used by grub core (e.g.
grub-core/lib/relocator.c or grub-core/mmap/mmap.c).

As we are running in 32-bit mode don't add memory above 4GB.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: 0x1ULL -> 1ULL << 32 (Roger Pau Monné)
---
 grub-core/kern/i386/xen/pvh.c | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 472085ed1..91fbca859 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -241,6 +241,30 @@ grub_xen_set_mmap (void)
  (grub_uint32_t) (), 0, 0, 0, 0);
 }
 
+static void
+grub_xen_mm_init_regions (void)
+{
+  grub_uint64_t modend, from, to;
+  unsigned int i;
+
+  modend = grub_modules_get_end ();
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+continue;
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (from < modend)
+from = modend;
+  if (from >= to || from >= (1ULL << 32))
+continue;
+  if (to > (1ULL << 32))
+to = 1ULL << 32;
+  grub_mm_init_region ((void *) (grub_addr_t) from, to - from);
+}
+}
+
 static grub_uint64_t
 grub_xen_find_page (grub_uint64_t start)
 {
@@ -325,10 +349,21 @@ grub_xen_setup_pvh (void)
   (void *) par);
   grub_xen_set_mmap ();
 
+  grub_xen_mm_init_regions ();
+
   grub_rsdp_addr = pvh_start_info->rsdp_paddr;
 }
 
 grub_err_t
 grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 {
+  unsigned int i;
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].len && hook (map[i].addr, map[i].len, map[i].type, hook_data))
+break;
+}
+
+  return GRUB_ERR_NONE;
 }
-- 
2.16.4


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


[PATCH v6 10/20] xen: add PVH boot entry code

2018-11-28 Thread Juergen Gross
Add the code for the Xen PVH mode boot entry.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: clear %fs and %gs, too (Daniel Kiper)
use GRUB_MEMORY_MACHINE_PROT_STACK_SIZE for stack size (Daniel Kiper)
V5: reorder clearing segment regs (Daniel Kiper)
---
 grub-core/kern/i386/xen/startup_pvh.S | 52 +++
 1 file changed, 52 insertions(+)

diff --git a/grub-core/kern/i386/xen/startup_pvh.S 
b/grub-core/kern/i386/xen/startup_pvh.S
index 69b8fdcca..363c31858 100644
--- a/grub-core/kern/i386/xen/startup_pvh.S
+++ b/grub-core/kern/i386/xen/startup_pvh.S
@@ -19,11 +19,63 @@
 
 #include 
 #include 
+#include 
 
.file   "startup_pvh.S"
.text
+   .globl  start, _start
+   .code32
 
+start:
+_start:
+   cld
+   lgdtgdtdesc
+   ljmp$GRUB_MEMORY_MACHINE_PROT_MODE_CSEG, $1f
+1:
+   movl$GRUB_MEMORY_MACHINE_PROT_MODE_DSEG, %eax
+   mov %eax, %ds
+   mov %eax, %es
+   mov %eax, %fs
+   mov %eax, %gs
+   mov %eax, %ss
+   lealLOCAL(stack_end), %esp
+
+   /* Save address of start info structure. */
+   mov %ebx, pvh_start_info
+   callEXT_C(grub_main)
+   /* Doesn't return. */
+
+   .p2align3
+gdt:
+   .word   0, 0
+   .byte   0, 0, 0, 0
+
+   /* -- code segment --
+* base = 0x, limit = 0xF (4 KiB Granularity), present
+* type = 32bit code execute/read, DPL = 0
+*/
+   .word   0x, 0
+   .byte   0, 0x9A, 0xCF, 0
+
+   /* -- data segment --
+* base = 0x, limit 0xF (4 KiB Granularity), present
+* type = 32 bit data read/write, DPL = 0
+*/
+   .word   0x, 0
+   .byte   0, 0x92, 0xCF, 0
+
+   .p2align3
+/* this is the GDT descriptor */
+gdtdesc:
+   .word   0x17/* limit */
+   .long   gdt /* addr */
+
+   .p2align2
 /* Saved pointer to start info structure. */
.globl  pvh_start_info
 pvh_start_info:
.long   0
+
+   .bss
+   .space  GRUB_MEMORY_MACHINE_PROT_STACK_SIZE
+LOCAL(stack_end):
-- 
2.16.4


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


[PATCH v6 11/20] xen: setup hypercall page for PVH

2018-11-28 Thread Juergen Gross
Add the needed code to setup the hypercall page for calling into the
Xen hypervisor.

Import the XEN_HVM_DEBUGCONS_IOPORT define from Xen unstable into
include/xen/arch-x86/xen.h

Signed-off-by: Juergen Gross 
Reviewed-by: Roger Pau Monné 
---
V3: grub_xen_early_halt->grub_xen_panic (Roger Pau Monné)
issue panic message (Roger Pau Monné)
rewrite grub_xen_hypercall to avoid register variables (Daniel Kiper)
V5: Use XEN_HVM_DEBUGCONS_IOPORT from Xen unstable (Roger Pau Monné)
Issue "System halted!" in panic (Daniel Kiper)
Clear interrupts and loop for halting (Roger Pau Monné, Daniel Kiper)
Use only one dummy variable for hypercall asm statement
V6: Added some comments (Daniel Kiper)
Use "+x" constraints instead of dummy variable (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 80 +++
 include/xen/arch-x86/xen.h|  7 
 2 files changed, 87 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 4f629b15e..a2554fb1d 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -20,15 +20,95 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
 
 grub_uint64_t grub_rsdp_addr;
 
+static char hypercall_page[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+static grub_uint32_t xen_cpuid_base;
+
+static void
+grub_xen_cons_msg (const char *msg)
+{
+  const char *c;
+
+  for (c = msg; *c; c++)
+grub_outb (*c, XEN_HVM_DEBUGCONS_IOPORT);
+}
+
+static void
+grub_xen_panic (const char *msg)
+{
+  grub_xen_cons_msg (msg);
+  grub_xen_cons_msg ("System halted!\n");
+
+  asm volatile ("cli");
+
+  while (1)
+{
+  asm volatile ("hlt");
+}
+}
+
+static void
+grub_xen_cpuid_base (void)
+{
+  grub_uint32_t base, eax, signature[3];
+
+  for (base = 0x4000; base < 0x4001; base += 0x100)
+{
+  grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
+  if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
+   {
+ xen_cpuid_base = base;
+ return;
+   }
+}
+
+  grub_xen_panic ("Found no Xen signature!\n");
+}
+
+static void
+grub_xen_setup_hypercall_page (void)
+{
+  grub_uint32_t msr, addr, eax, ebx, ecx, edx;
+
+  /* Get base address of Xen-specific MSRs. */
+  grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
+  msr = ebx;
+  addr = (grub_uint32_t) (_page);
+
+  /* Specify hypercall page address for Xen. */
+  asm volatile ("wrmsr" : : "c" (msr), "a" (addr), "d" (0) : "memory");
+}
+
+int
+grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
+   grub_uint32_t a1, grub_uint32_t a2,
+   grub_uint32_t a3, grub_uint32_t a4,
+   grub_uint32_t a5 __attribute__ ((unused)))
+{
+  grub_uint32_t res;
+
+  asm volatile ("call *%[callno]"
+   : "=a" (res), "+b" (a0), "+c" (a1), "+d" (a2),
+ "+S" (a3), "+D" (a4)
+   : [callno] "a" (_page[callno * 32])
+   : "memory");
+  return res;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_xen_cpuid_base ();
+  grub_xen_setup_hypercall_page ();
 }
 
 grub_err_t
diff --git a/include/xen/arch-x86/xen.h b/include/xen/arch-x86/xen.h
index f35804b88..56be26cb6 100644
--- a/include/xen/arch-x86/xen.h
+++ b/include/xen/arch-x86/xen.h
@@ -260,6 +260,13 @@ typedef struct arch_shared_info arch_shared_info_t;
 #define XEN_CPUID  XEN_EMULATE_PREFIX "cpuid"
 #endif
 
+/*
+ * Debug console IO port, also called "port E9 hack". Each character written
+ * to this IO port will be printed on the hypervisor console, subject to log
+ * level restrictions.
+ */
+#define XEN_HVM_DEBUGCONS_IOPORT 0xe9
+
 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_H__ */
 
 /*
-- 
2.16.4


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


[PATCH v6 04/20] xen: prepare common code for Xen PVH support

2018-11-28 Thread Juergen Gross
Some common code needs to be special cased for Xen PVH mode. This hits
mostly Xen PV mode specific areas.

Split include/grub/i386/pc/int_types.h off from
include/grub/i386/pc/int.h to support including this file later from
xen_pvh code without the grub_bios_interrupt definition.

Move definition of struct grub_e820_mmap_entry from
grub-core/mmap/i386/pc/mmap.c to include/grub/i386/memory.h in order
to make it usable from xen_pvh code.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: GRUB_MACHINE_XENPVH -> GRUB_MACHINE_XEN_PVH (Daniel Kiper)
split include/grub/i386/pc/int.h (Daniel Kiper)
move struct grub_e820_mmap_entry definition to header file
V5: minor style adjustments (Daniel Kiper)
---
 grub-core/kern/i386/tsc.c |  2 +-
 grub-core/mmap/i386/pc/mmap.c |  8 --
 include/grub/i386/memory.h|  7 +
 include/grub/i386/pc/int.h| 36 +---
 include/grub/i386/pc/int_types.h  | 59 +++
 include/grub/i386/tsc.h   |  2 +-
 include/grub/i386/xen/hypercall.h |  5 +++-
 include/grub/kernel.h |  4 ++-
 8 files changed, 76 insertions(+), 47 deletions(-)
 create mode 100644 include/grub/i386/pc/int_types.h

diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
index f266eb131..9293b161d 100644
--- a/grub-core/kern/i386/tsc.c
+++ b/grub-core/kern/i386/tsc.c
@@ -65,7 +65,7 @@ grub_tsc_init (void)
 
   tsc_boot_time = grub_get_tsc ();
 
-#ifdef GRUB_MACHINE_XEN
+#if defined (GRUB_MACHINE_XEN) || defined (GRUB_MACHINE_XEN_PVH)
   (void) (grub_tsc_calibrate_from_xen () || calibrate_tsc_hardcode());
 #elif defined (GRUB_MACHINE_EFI)
   (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () 
|| grub_tsc_calibrate_from_efi() || calibrate_tsc_hardcode());
diff --git a/grub-core/mmap/i386/pc/mmap.c b/grub-core/mmap/i386/pc/mmap.c
index 609994516..6ab4f6730 100644
--- a/grub-core/mmap/i386/pc/mmap.c
+++ b/grub-core/mmap/i386/pc/mmap.c
@@ -42,14 +42,6 @@ extern grub_uint16_t grub_machine_mmaphook_kblow;
 extern grub_uint16_t grub_machine_mmaphook_kbin16mb;
 extern grub_uint16_t grub_machine_mmaphook_64kbin4gb;
 
-struct grub_e820_mmap_entry
-{
-  grub_uint64_t addr;
-  grub_uint64_t len;
-  grub_uint32_t type;
-} GRUB_PACKED;
-
-
 /* Helper for preboot.  */
 static int fill_hook (grub_uint64_t addr, grub_uint64_t size,
  grub_memory_type_t type, void *data)
diff --git a/include/grub/i386/memory.h b/include/grub/i386/memory.h
index 8bb6e1cbb..5cb607fb4 100644
--- a/include/grub/i386/memory.h
+++ b/include/grub/i386/memory.h
@@ -44,6 +44,13 @@
 
 #include 
 
+struct grub_e820_mmap_entry
+{
+  grub_uint64_t addr;
+  grub_uint64_t len;
+  grub_uint32_t type;
+} GRUB_PACKED;
+
 grub_uint64_t grub_mmap_get_upper (void);
 grub_uint64_t grub_mmap_get_lower (void);
 grub_uint64_t grub_mmap_get_post64 (void);
diff --git a/include/grub/i386/pc/int.h b/include/grub/i386/pc/int.h
index 16a53e4fe..a60104001 100644
--- a/include/grub/i386/pc/int.h
+++ b/include/grub/i386/pc/int.h
@@ -20,45 +20,11 @@
 #define GRUB_INTERRUPT_MACHINE_HEADER  1
 
 #include 
-#include 
-
-struct grub_bios_int_registers
-{
-  grub_uint32_t eax;
-  grub_uint16_t es;
-  grub_uint16_t ds;
-  grub_uint16_t flags;
-  grub_uint16_t dummy;
-  grub_uint32_t ebx;
-  grub_uint32_t ecx;
-  grub_uint32_t edi;
-  grub_uint32_t esi;
-  grub_uint32_t edx;
-};
-
-#define  GRUB_CPU_INT_FLAGS_CARRY 0x1
-#define  GRUB_CPU_INT_FLAGS_PARITY0x4
-#define  GRUB_CPU_INT_FLAGS_ADJUST0x10
-#define  GRUB_CPU_INT_FLAGS_ZERO  0x40
-#define  GRUB_CPU_INT_FLAGS_SIGN  0x80
-#define  GRUB_CPU_INT_FLAGS_TRAP  0x100
-#define  GRUB_CPU_INT_FLAGS_INTERRUPT 0x200
-#define  GRUB_CPU_INT_FLAGS_DIRECTION 0x400
-#define  GRUB_CPU_INT_FLAGS_OVERFLOW  0x800
-#ifdef GRUB_MACHINE_PCBIOS
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   GRUB_CPU_INT_FLAGS_INTERRUPT
-#else
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   0
-#endif
+#include 
 
 void EXPORT_FUNC (grub_bios_interrupt) (grub_uint8_t intno,
struct grub_bios_int_registers *regs)
  __attribute__ ((regparm(3)));
-struct grub_i386_idt
-{
-  grub_uint16_t limit;
-  grub_uint32_t base;
-} GRUB_PACKED;
 
 #ifdef GRUB_MACHINE_PCBIOS
 extern struct grub_i386_idt *EXPORT_VAR(grub_realidt);
diff --git a/include/grub/i386/pc/int_types.h b/include/grub/i386/pc/int_types.h
new file mode 100644
index 0..2c5a69b63
--- /dev/null
+++ b/include/grub/i386/pc/int_types.h
@@ -0,0 +1,59 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRA

[PATCH v6 03/20] xen: carve out grant tab initialization into dedicated function

2018-11-28 Thread Juergen Gross
Initialize the grant tab in a dedicated function. This will enable
using it for PVH guests, too.

Call the new function from grub_machine_init() as this will later
be common between Xen PV and Xen PVH mode.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 35 +--
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 0559c033c..29f5bc23d 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -318,6 +318,25 @@ grub_xenstore_dir (const char *dir,
 
 unsigned long gntframe = 0;
 
+static void
+grub_xen_setup_gnttab (void)
+{
+  struct gnttab_set_version gnttab_setver;
+  struct gnttab_setup_table gnttab_setup;
+
+  grub_memset (_setver, 0, sizeof (gnttab_setver));
+
+  gnttab_setver.version = 1;
+  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
+
+  grub_memset (_setup, 0, sizeof (gnttab_setup));
+  gnttab_setup.dom = DOMID_SELF;
+  gnttab_setup.nr_frames = 1;
+  gnttab_setup.frame_list.p = 
+
+  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
+}
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -357,26 +376,12 @@ map_all_pages (void)
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   grub_uint64_t *pg = (grub_uint64_t *) window;
   grub_uint64_t oldpgstart, oldpgend;
-  struct gnttab_setup_table gnttab_setup;
-  struct gnttab_set_version gnttab_setver;
   grub_size_t n_unusable_pages = 0;
   struct mmu_update m2p_updates[2 * MAX_N_UNUSABLE_PAGES];
 
   if (total_pages > MAX_TOTAL_PAGES - 4)
 total_pages = MAX_TOTAL_PAGES - 4;
 
-  grub_memset (_setver, 0, sizeof (gnttab_setver));
-
-  gnttab_setver.version = 1;
-  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
-
-  grub_memset (_setup, 0, sizeof (gnttab_setup));
-  gnttab_setup.dom = DOMID_SELF;
-  gnttab_setup.nr_frames = 1;
-  gnttab_setup.frame_list.p = 
-
-  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
-
   for (j = 0; j < total_pages - n_unusable_pages; j++)
 while (!grub_xen_is_page_usable (mfn_list[j]))
   {
@@ -537,6 +542,8 @@ grub_machine_init (void)
   + GRUB_KERNEL_MACHINE_MOD_GAP,
   GRUB_KERNEL_MACHINE_MOD_ALIGN);
 
+  grub_xen_setup_gnttab ();
+
   map_all_pages ();
 
   grub_console_init ();
-- 
2.16.4


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


[PATCH v6 02/20] loader/linux: support passing rsdp address via boot params

2018-11-28 Thread Juergen Gross
Xen PVH guests will have the RSDP at an arbitrary address. Support that
by passing the RSDP address via the boot parameters to Linux.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: add oring 0x8000 to version field
V3: move including machine/kernel.h to patch 8 (Daniel Kiper)
V5: move acpi_rsdp_addr to struct linux_kernel_params (Peter Anvin)
---
 grub-core/loader/i386/linux.c | 4 
 include/grub/i386/linux.h | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index c408b10d8..375ee80dc 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -508,6 +508,10 @@ grub_linux_boot (void)
}
 }
 
+#ifdef GRUB_KERNEL_USE_RSDP_ADDR
+  linux_params.acpi_rsdp_addr = grub_le_to_cpu64 (grub_rsdp_addr);
+#endif
+
   mmap_size = find_mmap_size ();
   /* Make sure that each size is aligned to a page boundary.  */
   cl_offset = ALIGN_UP (mmap_size + sizeof (linux_params), 4096);
diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
index 60c7c3b5e..a96059311 100644
--- a/include/grub/i386/linux.h
+++ b/include/grub/i386/linux.h
@@ -210,8 +210,9 @@ struct linux_kernel_params
   grub_uint32_t ist_command;   /* 64 */
   grub_uint32_t ist_event; /* 68 */
   grub_uint32_t ist_perf_level;/* 6c */
+  grub_uint64_t acpi_rsdp_addr;/* 70 */
 
-  grub_uint8_t padding5[0x80 - 0x70];
+  grub_uint8_t padding5[0x80 - 0x78];
 
   grub_uint8_t hd0_drive_info[0x10];   /* 80 */
   grub_uint8_t hd1_drive_info[0x10];   /* 90 */
-- 
2.16.4


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


Re: [PATCH v5 15/20] xen_pvh: add build runes for grub-core

2018-11-28 Thread Juergen Gross
On 27/11/2018 22:17, Daniel Kiper wrote:
> On Wed, Nov 21, 2018 at 03:28:50PM +0100, Juergen Gross wrote:
>> Add the modifications to the build system needed to build a xen_pvh
>> grub.
>>
>> Signed-off-by: Juergen Gross 
>> Reviewed-by: Daniel Kiper 
>> ---
>> V3: sorted some filenames (Daniel Kiper)
>> V4: add bus/pci.c to xen_pvh
> 
> V5 drops bus/pci.c from xen_pvh. Is it intentional or mistake?

As the patch needing this has been dropped again this was
intentional.


Juergen


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


Re: [PATCH v5 01/20] xen: add some xen headers

2018-11-28 Thread Juergen Gross
On 28/11/2018 12:31, Daniel Kiper wrote:
> On Wed, Nov 21, 2018 at 03:28:36PM +0100, Juergen Gross wrote:
>> In order to support grub2 in Xen PVH environment some additional Xen
>> headers are needed as grub2 will be started in PVH mode requiring to
>> use several HVM hypercalls and structures.
>>
>> Add the needed headers from Xen 4.10 being the first Xen version with
>> full (not only experimental) PVH guest support.
>>
>> Signed-off-by: Juergen Gross 
>> Reviewed-by: Daniel Kiper 
> 
> When I apply this patch I get this:
> 
>   Applying: xen: add some xen headers
>   .git/rebase-apply/patch:723: trailing whitespace.
>*
>   .git/rebase-apply/patch:725: trailing whitespace.
>*
>   .git/rebase-apply/patch:764: trailing whitespace.
>* Maximum # bits addressable by the user of the allocated region (e.g., I/O
>   .git/rebase-apply/patch:765: trailing whitespace.
>* devices often have a 32-bit limitation even in 64-bit systems). If zero
>   .git/rebase-apply/patch:766: trailing whitespace.
>* then the user has no addressing restriction. This field is not used by
>   warning: squelched 7 whitespace errors
>   warning: 12 lines add whitespace errors.
> 
> Could you fix this?

Yes.


Juergen


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


Re: [PATCH v5 11/20] xen: setup hypercall page for PVH

2018-11-28 Thread Juergen Gross
On 28/11/2018 12:28, Daniel Kiper wrote:
> On Tue, Nov 27, 2018 at 09:31:10PM +0100, Daniel Kiper wrote:
>> On Wed, Nov 21, 2018 at 03:28:46PM +0100, Juergen Gross wrote:
>>> Add the needed code to setup the hypercall page for calling into the
>>> Xen hypervisor.
>>>
>>> Import the XEN_HVM_DEBUGCONS_IOPORT define from Xen unstable into
>>> include/xen/arch-x86/xen.h
>>>
>>> Signed-off-by: Juergen Gross 
> 
> [...]
> 
>>> +int
>>> +grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
>>> +   grub_uint32_t a1, grub_uint32_t a2,
>>> +   grub_uint32_t a3, grub_uint32_t a4,
>>> +   grub_uint32_t a5 __attribute__ ((unused)))
>>> +{
>>> +  grub_uint32_t __res, dummy;
>>> +
>>> +  asm volatile ("call *%[callno]"
>>> +   : "=a" (__res), "=b" (dummy), "=c" (dummy), "=d" (dummy),
>>> + "=S" (dummy), "=D" (dummy)
>>> +   : "1" (a0), "2" (a1), "3" (a2), "4" (a3), "5" (a4),
>>> + [callno] "a" (_page[callno * 32])
>>> +   : "memory");
>>
>> Have you tried "+b", "+c", ... instead of "=b", "=c", ...?
> 
> I have done some experiments. Your code:
> 
>   grub_uint32_t __res, dummy;
> 
>   asm volatile ("call *%[callno]"
> : "=a" (__res), "=b" (dummy), "=c" (dummy), "=d" (dummy),
>   "=S" (dummy), "=D" (dummy)
> : "1" (a0), "2" (a1), "3" (a2), "4" (a3), "5" (a4),
>   [callno] "a" (_page[callno * 32])
> : "memory");
> 
> ... generates this assembly:
> 
> 048e :
>  48e:   55  push   %ebp
>  48f:   89 e5   mov%esp,%ebp
>  491:   57  push   %edi
>  492:   56  push   %esi
>  493:   53  push   %ebx
>  494:   c1 e0 05shl$0x5,%eax
>  497:   05 00 10 00 00  add$0x1000,%eax
>  49c:   89 d3   mov%edx,%ebx
>  49e:   8b 55 08mov0x8(%ebp),%edx
>  4a1:   8b 75 0cmov0xc(%ebp),%esi
>  4a4:   8b 7d 10mov0x10(%ebp),%edi
>  4a7:   ff d0   call   *%eax
>  4a9:   5b  pop%ebx
>  4aa:   5e  pop%esi
>  4ab:   5f  pop%edi
>  4ac:   5d  pop%ebp
>  4ad:   c2 10 00ret$0x10
> 
> Mine code:
> 
>   grub_uint32_t __res;
> 
>   asm volatile ("call *%[callno]"
> : "=a" (__res), "+b" (a0), "+c" (a1), "+d" (a2), "+S" (a3), 
> "+D" (a4)
> : [callno] "rm" (_page[callno * 32])
> : "memory");
> 
> ... generates this assembly:
> 
> 048e :
>  48e:   55  push   %ebp
>  48f:   89 e5   mov%esp,%ebp
>  491:   57  push   %edi
>  492:   56  push   %esi
>  493:   53  push   %ebx
>  494:   c1 e0 05shl$0x5,%eax
>  497:   05 00 10 00 00  add$0x1000,%eax
>  49c:   89 d3   mov%edx,%ebx
>  49e:   8b 55 08mov0x8(%ebp),%edx
>  4a1:   8b 75 0cmov0xc(%ebp),%esi
>  4a4:   8b 7d 10mov0x10(%ebp),%edi
>  4a7:   ff d0   call   *%eax
>  4a9:   5b  pop%ebx
>  4aa:   5e  pop%esi
>  4ab:   5f  pop%edi
>  4ac:   5d  pop%ebp
>  4ad:   c2 10 00ret$0x10
> 
> So, both are equal but mine seems a bit simpler.
> 
> And I think that you can drop "__" from __res variable.

Okay.


Juergen

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


[PATCH v5 15/20] xen_pvh: add build runes for grub-core

2018-11-21 Thread Juergen Gross
Add the modifications to the build system needed to build a xen_pvh
grub.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: sorted some filenames (Daniel Kiper)
V4: add bus/pci.c to xen_pvh
---
 gentpl.py   |  4 ++--
 grub-core/Makefile.am   | 12 
 grub-core/Makefile.core.def | 35 +++
 3 files changed, 49 insertions(+), 2 deletions(-)

diff --git a/gentpl.py b/gentpl.py
index da67965a4..e8439484a 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -28,7 +28,7 @@ import re
 
 GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
"i386_multiboot", "i386_ieee1275", "x86_64_efi",
-   "i386_xen", "x86_64_xen",
+   "i386_xen", "x86_64_xen", "i386_xen_pvh",
"mips_loongson", "sparc64_ieee1275",
"powerpc_ieee1275", "mips_arc", "ia64_efi",
"mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi",
@@ -71,7 +71,7 @@ GROUPS["videomodules"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
 
 # Similar for terminfo
-GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips" ] + GROUPS["xen"] + GROUPS["ieee1275"] + GROUPS["uboot"];
+GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips", "i386_xen_pvh" ] + GROUPS["xen"] + GROUPS["ieee1275"] + 
GROUPS["uboot"];
 GROUPS["terminfomodule"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
 
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index f4ff62b76..02cb5e33d 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -101,6 +101,18 @@ KERNEL_HEADER_FILES += 
$(top_builddir)/include/grub/machine/int.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
+if COND_i386_xen_pvh
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/loader.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/xen.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/xen/hypercall.h
+endif
+
 if COND_i386_efi
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 6e2cc8444..db3231f77 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -79,6 +79,8 @@ kernel = {
   i386_xen_ldflags = '$(TARGET_IMG_BASE_LDOPT),0';
   x86_64_xen_ldflags   = '$(TARGET_IMG_LDFLAGS)';
   x86_64_xen_ldflags   = '$(TARGET_IMG_BASE_LDOPT),0';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_LDFLAGS)';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x10';
 
   mips_loongson_ldflags= '-Wl,-Ttext,0x8020';
   powerpc_ieee1275_ldflags = '-Wl,-Ttext,0x20';
@@ -100,6 +102,7 @@ kernel = {
   x86_64_efi_startup = kern/x86_64/efi/startup.S;
   i386_xen_startup = kern/i386/xen/startup.S;
   x86_64_xen_startup = kern/x86_64/xen/startup.S;
+  i386_xen_pvh_startup = kern/i386/xen/startup_pvh.S;
   i386_qemu_startup = kern/i386/qemu/startup.S;
   i386_ieee1275_startup = kern/i386/ieee1275/startup.S;
   i386_coreboot_startup = kern/i386/coreboot/startup.S;
@@ -177,6 +180,7 @@ kernel = {
 
   i386 = kern/i386/dl.c;
   i386_xen = kern/i386/dl.c;
+  i386_xen_pvh = kern/i386/dl.c;
 
   i386_coreboot = kern/i386/coreboot/init.c;
   i386_multiboot = kern/i386/coreboot/init.c;
@@ -222,6 +226,14 @@ kernel = {
   xen = disk/xen/xendisk.c;
   xen = commands/boot.c;
 
+  i386_xen_pvh = commands/boot.c;
+  i386_xen_pvh = disk/xen/xendisk.c;
+  i386_xen_pvh = kern/i386/tsc.c;
+  i386_xen_pvh = kern/i386/xen/tsc.c;
+  i386_xen_pvh = kern/i386/xen/pvh.c;
+  i386_xen_pvh = kern/xen/init.c;
+  i386_xen_pvh = term/xen/console.c;
+
   ia64_efi = kern/ia64/efi/startup.S;
   ia64_efi = kern/ia64/efi/init.c;
   ia64_efi = kern/ia64/dl.c;
@@ -801,6 +813,7 @@ module = {
   name = cpuid;
   common = commands/i386/cpuid.c;
   enable = x86;
+  enable = i386_xen_pvh;
   enable = i386_xen;
   enable = x86_64_xen;
 };
@@ -860,6 +873,7 @@ module = {
   i386_coreboot = 

[PATCH v5 11/20] xen: setup hypercall page for PVH

2018-11-21 Thread Juergen Gross
Add the needed code to setup the hypercall page for calling into the
Xen hypervisor.

Import the XEN_HVM_DEBUGCONS_IOPORT define from Xen unstable into
include/xen/arch-x86/xen.h

Signed-off-by: Juergen Gross 
---
V3: grub_xen_early_halt->grub_xen_panic (Roger Pau Monné)
issue panic message (Roger Pau Monné)
rewrite grub_xen_hypercall to avoid register variables (Daniel Kiper)
V5: Use XEN_HVM_DEBUGCONS_IOPORT from Xen unstable (Roger Pau Monné)
Issue "System halted!" in panic (Daniel Kiper)
Clear interrupts and loop for halting (Roger Pau Monné, Daniel Kiper)
Use only one dummy variable for hypercall asm statement
---
 grub-core/kern/i386/xen/pvh.c | 79 +++
 include/xen/arch-x86/xen.h|  7 
 2 files changed, 86 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 4f629b15e..478cef0d1 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -20,15 +20,94 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 #include 
 
 grub_uint64_t grub_rsdp_addr;
 
+static char hypercall_page[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+static grub_uint32_t xen_cpuid_base;
+
+static void
+grub_xen_cons_msg (const char *msg)
+{
+  const char *c;
+
+  for (c = msg; *c; c++)
+grub_outb (*c, XEN_HVM_DEBUGCONS_IOPORT);
+}
+
+static void
+grub_xen_panic (const char *msg)
+{
+  grub_xen_cons_msg (msg);
+  grub_xen_cons_msg ("System halted!\n");
+
+  asm volatile ("cli");
+
+  while (1)
+{
+  asm volatile ("hlt");
+}
+}
+
+static void
+grub_xen_cpuid_base (void)
+{
+  grub_uint32_t base, eax, signature[3];
+
+  for (base = 0x4000; base < 0x4001; base += 0x100)
+{
+  grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
+  if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
+   {
+ xen_cpuid_base = base;
+ return;
+   }
+}
+
+  grub_xen_panic ("Found no Xen signature!\n");
+}
+
+static void
+grub_xen_setup_hypercall_page (void)
+{
+  grub_uint32_t msr, pfn, eax, ebx, ecx, edx;
+
+  grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
+  msr = ebx;
+  pfn = (grub_uint32_t) (_page);
+
+  asm volatile ("wrmsr" : : "c" (msr), "a" (pfn), "d" (0) : "memory");
+}
+
+int
+grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
+   grub_uint32_t a1, grub_uint32_t a2,
+   grub_uint32_t a3, grub_uint32_t a4,
+   grub_uint32_t a5 __attribute__ ((unused)))
+{
+  grub_uint32_t __res, dummy;
+
+  asm volatile ("call *%[callno]"
+   : "=a" (__res), "=b" (dummy), "=c" (dummy), "=d" (dummy),
+ "=S" (dummy), "=D" (dummy)
+   : "1" (a0), "2" (a1), "3" (a2), "4" (a3), "5" (a4),
+ [callno] "a" (_page[callno * 32])
+   : "memory");
+  return __res;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_xen_cpuid_base ();
+  grub_xen_setup_hypercall_page ();
 }
 
 grub_err_t
diff --git a/include/xen/arch-x86/xen.h b/include/xen/arch-x86/xen.h
index f35804b88..56be26cb6 100644
--- a/include/xen/arch-x86/xen.h
+++ b/include/xen/arch-x86/xen.h
@@ -260,6 +260,13 @@ typedef struct arch_shared_info arch_shared_info_t;
 #define XEN_CPUID  XEN_EMULATE_PREFIX "cpuid"
 #endif
 
+/*
+ * Debug console IO port, also called "port E9 hack". Each character written
+ * to this IO port will be printed on the hypervisor console, subject to log
+ * level restrictions.
+ */
+#define XEN_HVM_DEBUGCONS_IOPORT 0xe9
+
 #endif /* __XEN_PUBLIC_ARCH_X86_XEN_H__ */
 
 /*
-- 
2.16.4


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


[PATCH v5 14/20] xen: init memory regions for PVH

2018-11-21 Thread Juergen Gross
Add all usable memory regions to grub memory management and add the
needed mmap iterate code, which will be used by grub core (e.g.
grub-core/lib/relocator.c or grub-core/mmap/mmap.c).

As we are running in 32-bit mode don't add memory above 4GB.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: 0x1ULL -> 1ULL << 32 (Roger Pau Monné)
---
 grub-core/kern/i386/xen/pvh.c | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 6de84eb8e..1bd503c4a 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -240,6 +240,30 @@ grub_xen_set_mmap (void)
  (grub_uint32_t) (), 0, 0, 0, 0);
 }
 
+static void
+grub_xen_mm_init_regions (void)
+{
+  grub_uint64_t modend, from, to;
+  unsigned int i;
+
+  modend = grub_modules_get_end ();
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+continue;
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (from < modend)
+from = modend;
+  if (from >= to || from >= (1ULL << 32))
+continue;
+  if (to > (1ULL << 32))
+to = 1ULL << 32;
+  grub_mm_init_region ((void *) (grub_addr_t) from, to - from);
+}
+}
+
 static grub_uint64_t
 grub_xen_find_page (grub_uint64_t start)
 {
@@ -321,10 +345,21 @@ grub_xen_setup_pvh (void)
   (void *) par);
   grub_xen_set_mmap ();
 
+  grub_xen_mm_init_regions ();
+
   grub_rsdp_addr = pvh_start_info->rsdp_paddr;
 }
 
 grub_err_t
 grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 {
+  unsigned int i;
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].len && hook (map[i].addr, map[i].len, map[i].type, hook_data))
+break;
+}
+
+  return GRUB_ERR_NONE;
 }
-- 
2.16.4


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


[PATCH v5 19/20] xen_pvh: support grub-install for xen_pvh

2018-11-21 Thread Juergen Gross
Add xen_pvh support to grub-install.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 include/grub/util/install.h | 1 +
 util/grub-install-common.c  | 1 +
 util/grub-install.c | 7 +++
 3 files changed, 9 insertions(+)

diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 0dba8b67f..af2bf65d7 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -100,6 +100,7 @@ enum grub_install_plat
 GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS,
 GRUB_INSTALL_PLATFORM_I386_XEN,
 GRUB_INSTALL_PLATFORM_X86_64_XEN,
+GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
 GRUB_INSTALL_PLATFORM_ARM64_EFI,
 GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
 GRUB_INSTALL_PLATFORM_MAX
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
index 0a2e24a79..1b1cb43b4 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -716,6 +716,7 @@ static struct
 [GRUB_INSTALL_PLATFORM_X86_64_EFI] =   { "x86_64",  "efi"   },
 [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386","xen"   },
 [GRUB_INSTALL_PLATFORM_X86_64_XEN] =   { "x86_64",  "xen"   },
+[GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386","xen_pvh"   },
 [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",  "loongson"  },
 [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",  "qemu_mips" },
 [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips","qemu_mips" },
diff --git a/util/grub-install.c b/util/grub-install.c
index 4375c1619..743296f36 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -496,6 +496,7 @@ have_bootdev (enum grub_install_plat pl)
 
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   return 0;
 
   /* pacify warning.  */
@@ -913,6 +914,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
 
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
@@ -960,6 +962,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   free (install_device);
   install_device = NULL;
   break;
@@ -1477,6 +1480,7 @@ main (int argc, char *argv[])
  case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  case GRUB_INSTALL_PLATFORM_I386_XEN:
  case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+ case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
grub_util_warn ("%s", _("no hints available for your 
platform. Expect reduced performance"));
break;
/* pacify warning.  */
@@ -1568,6 +1572,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   core_name = "core.elf";
   snprintf (mkimage_target, sizeof (mkimage_target),
"%s-%s",
@@ -1660,6 +1665,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
   /* pacify warning.  */
 case GRUB_INSTALL_PLATFORM_MAX:
@@ -1926,6 +1932,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   grub_util_warn ("%s",
  _("WARNING: no platform-specific install was performed"));
   break;
-- 
2.16.4


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


[PATCH v5 20/20] xen_pvh: add support to configure

2018-11-21 Thread Juergen Gross
Support platform i386/xen_pvh in configure.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 configure.ac | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.ac b/configure.ac
index 5e63c4af3..81a19afd5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -151,6 +151,7 @@ case "$target_cpu"-"$platform" in
   i386-efi) ;;
   x86_64-efi) ;;
   i386-xen) ;;
+  i386-xen_pvh) ;;
   x86_64-xen) ;;
   i386-pc) ;;
   i386-multiboot) ;;
@@ -219,6 +220,7 @@ case "$platform" in
   multiboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MULTIBOOT=1" 
;;
   efi) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EFI=1" ;;
   xen) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN=1" ;;
+  xen_pvh) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN_PVH=1" ;;
   ieee1275)machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" 
;;
   uboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
   qemu)machine_CPPFLAGS="$machine_CPPFLAGS 
-DGRUB_MACHINE_QEMU=1" ;;
@@ -1897,6 +1899,7 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = 
xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform 
= xmultiboot])
 AM_CONDITIONAL([COND_x86_64_efi], [test x$target_cpu = xx86_64 -a x$platform = 
xefi])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = 
xxen])
+AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = 
xxen_pvh])
 AM_CONDITIONAL([COND_x86_64_xen], [test x$target_cpu = xx86_64 -a x$platform = 
xxen])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a 
x$platform = xloongson])
 AM_CONDITIONAL([COND_mips_qemu_mips], [test "(" x$target_cpu = xmips -o 
x$target_cpu = xmipsel ")"  -a x$platform = xqemu_mips])
-- 
2.16.4


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


[PATCH v5 18/20] xen_pvh: support building a standalone image

2018-11-21 Thread Juergen Gross
Support mkimage for xen_pvh.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: some style adjustments (Daniel Kiper)
use defines for elf-notes (Daniel Kiper)
V5: move elf-note define usage into new patch (Daniel Kiper)

I didn't replace the 4096 by a PAGE_SIZE macro as requested by Daniel,
as there isn't such a macro easily available for util/mkimage.c and
I didn't introduce its usage.
---
 include/grub/util/mkimage.h |  3 ++-
 util/grub-mkimage32.c   |  4 +++-
 util/grub-mkimage64.c   |  4 +++-
 util/grub-mkimagexx.c   | 44 
 util/mkimage.c  | 23 ++-
 5 files changed, 70 insertions(+), 8 deletions(-)

diff --git a/include/grub/util/mkimage.h b/include/grub/util/mkimage.h
index b3a5ca132..ba9f568f6 100644
--- a/include/grub/util/mkimage.h
+++ b/include/grub/util/mkimage.h
@@ -71,7 +71,8 @@ struct grub_install_image_target_desc
 IMAGE_I386_IEEE1275,
 IMAGE_LOONGSON_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH,
 IMAGE_FULOONG2F_FLASH, IMAGE_I386_PC_PXE, IMAGE_MIPS_ARC,
-IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO
+IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO,
+IMAGE_XEN_PVH
   } id;
   enum
 {
diff --git a/util/grub-mkimage32.c b/util/grub-mkimage32.c
index 1f2d2..026a2dd59 100644
--- a/util/grub-mkimage32.c
+++ b/util/grub-mkimage32.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF32_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF32_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF32_ST_TYPE(val)
-#define XEN_NOTE_SIZE 132
+
+#define XEN_NOTE_SIZE  132
+#define XEN_PVH_NOTE_SIZE  20
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimage64.c b/util/grub-mkimage64.c
index 4ff72a625..170defb40 100644
--- a/util/grub-mkimage64.c
+++ b/util/grub-mkimage64.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF64_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF64_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF64_ST_TYPE(val)
-#define XEN_NOTE_SIZE 120
+
+#define XEN_NOTE_SIZE  120
+#define XEN_PVH_NOTE_SIZE  24
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index 784ed1a52..e94a721b4 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -229,12 +229,12 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phnum++;
   footer_size += sizeof (struct grub_ieee1275_note);
 }
-  if (image_target->id == IMAGE_XEN)
+  if (image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH)
 {
   phnum++;
   shnum++;
   string_size += sizeof (".xen");
-  footer_size += XEN_NOTE_SIZE;
+  footer_size += (image_target->id == IMAGE_XEN) ? XEN_NOTE_SIZE : 
XEN_PVH_NOTE_SIZE;
 }
   header_size = ALIGN_UP (sizeof (*ehdr) + phnum * sizeof (*phdr)
  + shnum * sizeof (*shdr) + string_size, 
layout->align);
@@ -421,6 +421,39 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phdr->p_offset = grub_host_to_target32 (header_size + program_size);
 }
 
+  if (image_target->id == IMAGE_XEN_PVH)
+{
+  char *note_start = (elf_img + program_size + header_size);
+  Elf_Nhdr *note_ptr;
+  char *ptr = (char *) note_start;
+
+  grub_util_info ("adding XEN NOTE segment");
+
+  /* Phys32 Entry.  */
+  note_ptr = (Elf_Nhdr *) ptr;
+  note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
+  note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PHYS32_ENTRY);
+  ptr += sizeof (Elf_Nhdr);
+  memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
+  ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
+  memset (ptr, 0, image_target->voidp_sizeof);
+  *(grub_uint32_t *) ptr = GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR;
+  ptr += image_target->voidp_sizeof;
+
+  assert (XEN_PVH_NOTE_SIZE == (ptr - note_start));
+
+  phdr++;
+  phdr->p_type = grub_host_to_target32 (PT_NOTE);
+  phdr->p_flags = grub_host_to_target32 (PF_R);
+  phdr->p_align = grub_host_to_target32 (image_target->voidp_sizeof);
+  phdr->p_vaddr = 0;
+  phdr->p_paddr = 0;
+  phdr->p_filesz = grub_host_to_target32 (XEN_PVH_NOTE_SIZE);
+  phdr->p_memsz = 0;
+  phdr->p_offset = grub_host_to_target32 (header_size + program_size);
+}
+
   if (note)
 {
   int note_size = sizeof (struct grub_ieee1275_note);
@@ -496,7 +529,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
 shdr->sh_entsize = grub_host_to_target32 (0);

[PATCH v5 01/20] xen: add some xen headers

2018-11-21 Thread Juergen Gross
In order to support grub2 in Xen PVH environment some additional Xen
headers are needed as grub2 will be started in PVH mode requiring to
use several HVM hypercalls and structures.

Add the needed headers from Xen 4.10 being the first Xen version with
full (not only experimental) PVH guest support.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 include/xen/hvm/hvm_op.h | 296 +++
 include/xen/hvm/params.h | 284 ++
 include/xen/hvm/start_info.h |  98 +++
 include/xen/memory.h | 665 +++
 include/xen/physdev.h| 387 +
 include/xen/trace.h  | 339 ++
 include/xen/xen.h| 104 +--
 7 files changed, 2142 insertions(+), 31 deletions(-)
 create mode 100644 include/xen/hvm/hvm_op.h
 create mode 100644 include/xen/hvm/params.h
 create mode 100644 include/xen/hvm/start_info.h
 create mode 100644 include/xen/memory.h
 create mode 100644 include/xen/physdev.h
 create mode 100644 include/xen/trace.h

diff --git a/include/xen/hvm/hvm_op.h b/include/xen/hvm/hvm_op.h
new file mode 100644
index 0..0bdafdf59
--- /dev/null
+++ b/include/xen/hvm/hvm_op.h
@@ -0,0 +1,296 @@
+/*
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2007, Keir Fraser
+ */
+
+#ifndef __XEN_PUBLIC_HVM_HVM_OP_H__
+#define __XEN_PUBLIC_HVM_HVM_OP_H__
+
+#include "../xen.h"
+#include "../trace.h"
+#include "../event_channel.h"
+
+/* Get/set subcommands: extra argument == pointer to xen_hvm_param struct. */
+#define HVMOP_set_param   0
+#define HVMOP_get_param   1
+struct xen_hvm_param {
+domid_t  domid;/* IN */
+uint32_t index;/* IN */
+uint64_t value;/* IN/OUT */
+};
+typedef struct xen_hvm_param xen_hvm_param_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_param_t);
+
+#if __XEN_INTERFACE_VERSION__ < 0x00040900
+
+/* Set the logical level of one of a domain's PCI INTx wires. */
+#define HVMOP_set_pci_intx_level  2
+struct xen_hvm_set_pci_intx_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI INTx identification in PCI topology (domain:bus:device:intx). */
+uint8_t  domain, bus, device, intx;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_pci_intx_level xen_hvm_set_pci_intx_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_intx_level_t);
+
+/* Set the logical level of one of a domain's ISA IRQ wires. */
+#define HVMOP_set_isa_irq_level   3
+struct xen_hvm_set_isa_irq_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* ISA device identification, by ISA IRQ (0-15). */
+uint8_t  isa_irq;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_isa_irq_level xen_hvm_set_isa_irq_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_isa_irq_level_t);
+
+#define HVMOP_set_pci_link_route  4
+struct xen_hvm_set_pci_link_route {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI link identifier (0-3). */
+uint8_t  link;
+/* ISA IRQ (1-15), or 0 (disable link). */
+uint8_t  isa_irq;
+};
+typedef struct xen_hvm_set_pci_link_route xen_hvm_set_pci_link_route_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_link_route_t);
+
+#endif /* __XEN_INTERFACE_VERSION__ < 0x00040900 */
+
+/* Flushes all VCPU TLBs: @arg must be NULL. */
+#define HVMOP_flush_tlbs  5
+
+typedef enum {
+HVMMEM_ram_rw, /* Normal read/write guest RAM */
+HVMMEM_ram_ro, /* Read-only; writes are discarded */
+HVMMEM_mmio_dm,/* Reads and write go to the device model */
+#if __XEN_INTERFACE_VERSION__ < 0x00040700
+HVMMEM_mmio_write_dm,  /* Read-only; writes go to the device model */
+#else
+HVMMEM_unus

[PATCH v5 17/20] xen: use elfnote defines instead of plain numbers

2018-11-21 Thread Juergen Gross
In order to avoid using plain integers for the ELF notes use the
available Xen include instead.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V5: new patch (Daniel Kiper)
---
 util/grub-mkimagexx.c | 14 --
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a483c674c..784ed1a52 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -48,6 +48,8 @@
 #include 
 #include 
 
+#include 
+
 #pragma GCC diagnostic ignored "-Wcast-align"
 
 #define GRUB_MKIMAGEXX
@@ -341,7 +343,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof (PACKAGE_NAME));
-  note_ptr->n_type = grub_host_to_target32 (6);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_GUEST_OS);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -352,7 +354,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("generic"));
-  note_ptr->n_type = grub_host_to_target32 (8);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_LOADER);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -363,7 +365,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("xen-3.0"));
-  note_ptr->n_type = grub_host_to_target32 (5);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_XEN_VERSION);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -374,7 +376,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
-  note_ptr->n_type = grub_host_to_target32 (1);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_ENTRY);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -385,7 +387,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (image_target->voidp_sizeof);
-  note_ptr->n_type = grub_host_to_target32 (3);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_VIRT_BASE);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -398,7 +400,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
  note_ptr = (Elf_Nhdr *) ptr;
  note_ptr->n_namesz = grub_host_to_target32 (sizeof 
(GRUB_XEN_NOTE_NAME));
  note_ptr->n_descsz = grub_host_to_target32 (sizeof ("yes,bimodal"));
- note_ptr->n_type = grub_host_to_target32 (9);
+ note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_PAE_MODE);
  ptr += sizeof (Elf_Nhdr);
  memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
  ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
-- 
2.16.4


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


[PATCH v5 13/20] xen: setup Xen specific data for PVH

2018-11-21 Thread Juergen Gross
Initialize the needed Xen specific data. This is:

- the Xen start of day page containing the console and Xenstore ring
  page PFN and event channel
- the grant table
- the shared info page

Write back the possibly modified memory map to the hypervisor in case
the guest is reading it from there again.

Set the RSDP address for the guest from the start_info page passed
as boot parameter.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V4: write back memory map to Xen (Roger Pau Monné)
V5: add comment (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 120 ++
 1 file changed, 120 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index bb90874b3..6de84eb8e 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -27,6 +27,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #define XEN_MEMORY_MAP_SIZE   128
@@ -37,6 +38,7 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct start_info grub_xen_start_page;
 static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
 static unsigned int nr_map_entries;
 
@@ -110,6 +112,36 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
   return __res;
 }
 
+static grub_uint32_t
+grub_xen_get_param (int idx)
+{
+  struct xen_hvm_param xhv;
+  int r;
+
+  xhv.domid = DOMID_SELF;
+  xhv.index = idx;
+  r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+  if (r < 0)
+grub_xen_panic ("Could not get parameter from Xen!\n");
+  return xhv.value;
+}
+
+static void *
+grub_xen_add_physmap (unsigned int space, void *addr)
+{
+  struct xen_add_to_physmap xatp;
+
+  xatp.domid = DOMID_SELF;
+  xatp.idx = 0;
+  xatp.space = space;
+  xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Memory_op hypercall failed!\n");
+  return addr;
+}
+
 static void
 grub_xen_sort_mmap (void)
 {
@@ -196,12 +228,100 @@ grub_xen_get_mmap (void)
   grub_xen_sort_mmap ();
 }
 
+static void
+grub_xen_set_mmap (void)
+{
+  struct xen_foreign_memory_map memmap;
+
+  memmap.domid = DOMID_SELF;
+  memmap.map.nr_entries = nr_map_entries;
+  set_xen_guest_handle (memmap.map.buffer, map);
+  grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_set_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+}
+
+static grub_uint64_t
+grub_xen_find_page (grub_uint64_t start)
+{
+  unsigned int i, j;
+  grub_uint64_t last = start;
+
+  /* Try to find a e820 map hole below 4G. */
+  /* Relies on page-aligned entries (addr and len) and input (start). */
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (last > map[i].addr + map[i].len)
+   continue;
+  if (last < map[i].addr)
+   return last;
+  if ((map[i].addr >> 32) || ((map[i].addr + map[i].len) >> 32))
+   break;
+  last = map[i].addr + map[i].len;
+}
+if (i == nr_map_entries)
+  return last;
+
+  /* No hole found, use the highest RAM page below 4G and reserve it. */
+  if (nr_map_entries == ARRAY_SIZE (map))
+grub_xen_panic ("Memory map size limit reached!\n");
+  for (i = 0, j = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+   continue;
+  if (map[i].addr >> 32)
+   break;
+  j = i;
+  if ((map[i].addr + map[i].len) >> 32)
+   break;
+}
+  if (map[j].type != GRUB_MEMORY_AVAILABLE)
+grub_xen_panic ("No free memory page found!\n");
+  if ((map[j].addr + map[j].len) >> 32)
+last = (1ULL << 32) - GRUB_XEN_PAGE_SIZE;
+  else
+last = map[j].addr + map[j].len - GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].addr = last;
+  map[nr_map_entries].len = GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
+  nr_map_entries++;
+  grub_xen_sort_mmap ();
+
+  return last;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_addr_t par;
+
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
   grub_xen_get_mmap ();
+
+  /* Setup Xen data. */
+  grub_xen_start_page_addr = _xen_start_page;
+
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_PFN);
+  grub_xen_start_page_addr->console.domU.mfn = par;
+  grub_xen_xcons = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_EVTCHN);
+  grub_xen_start_page_addr->console.domU.evtchn = par;
+
+  par = grub_xen_get_param (HVM_PARAM_STORE_PFN);
+  grub_xen_start_page_addr->store_mfn = par;
+  grub_xen_xenstore = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_STORE_EVTCHN);
+  grub_xen_

[PATCH v5 12/20] xen: get memory map from hypervisor for PVH

2018-11-21 Thread Juergen Gross
Retrieve the memory map from the hypervisor and normalize it to contain
no overlapping entries and to be sorted by address.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: use grub_e820_mmap_entry instead of own struct (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 94 +++
 1 file changed, 94 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 478cef0d1..bb90874b3 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -24,7 +24,12 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
+
+#define XEN_MEMORY_MAP_SIZE   128
 
 grub_uint64_t grub_rsdp_addr;
 
@@ -32,6 +37,8 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
+static unsigned int nr_map_entries;
 
 static void
 grub_xen_cons_msg (const char *msg)
@@ -103,11 +110,98 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t 
a0,
   return __res;
 }
 
+static void
+grub_xen_sort_mmap (void)
+{
+  grub_uint64_t from, to;
+  unsigned int i;
+  struct grub_e820_mmap_entry tmp;
+
+  /* Align map entries to page boundaries. */
+  for (i = 0; i < nr_map_entries; i++)
+{
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (map[i].type == GRUB_MEMORY_AVAILABLE)
+   {
+ from = ALIGN_UP (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_DOWN (to, GRUB_XEN_PAGE_SIZE);
+   }
+  else
+   {
+ from = ALIGN_DOWN (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_UP (to, GRUB_XEN_PAGE_SIZE);
+   }
+  map[i].addr = from;
+  map[i].len = to - from;
+}
+
+ again:
+  /* Sort entries by start address. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr)
+   continue;
+  tmp = map[i];
+  map[i] = map[i - 1];
+  map[i - 1] = tmp;
+  i = 0;
+}
+
+  /* Detect overlapping areas. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr + map[i - 1].len)
+   continue;
+  tmp = map[i - 1];
+  map[i - 1].len = map[i].addr - map[i - 1].addr;
+  if (map[i].addr + map[i].len >= tmp.addr + tmp.len)
+   continue;
+  if (nr_map_entries < ARRAY_SIZE (map))
+   {
+ map[nr_map_entries].addr = map[i].addr + map[i].len;
+ map[nr_map_entries].len = tmp.addr + tmp.len - 
map[nr_map_entries].addr;
+ map[nr_map_entries].type = tmp.type;
+ nr_map_entries++;
+ goto again;
+   }
+}
+
+  /* Merge adjacent entries. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].type == map[i - 1].type &&
+ map[i].addr == map[i - 1].addr + map[i - 1].len)
+   {
+ map[i - 1].len += map[i].len;
+ map[i] = map[nr_map_entries - 1];
+ nr_map_entries--;
+ goto again;
+   }
+}
+}
+
+static void
+grub_xen_get_mmap (void)
+{
+  struct xen_memory_map memmap;
+
+  memmap.nr_entries = ARRAY_SIZE (map);
+  set_xen_guest_handle (memmap.buffer, map);
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Could not get memory map from Xen!\n");
+  nr_map_entries = memmap.nr_entries;
+
+  grub_xen_sort_mmap ();
+}
+
 void
 grub_xen_setup_pvh (void)
 {
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
+  grub_xen_get_mmap ();
 }
 
 grub_err_t
-- 
2.16.4


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


[PATCH v5 08/20] xen: add PVH specific defines to offset.h

2018-11-21 Thread Juergen Gross
include/grub/offsets.h needs some defines for Xen PVH mode.

Add them. While at it line up the values in the surrounding lines to
start at the same column.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: XENPVH->XEN_PVH (Daniel Kiper)
---
 include/grub/offsets.h | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/include/grub/offsets.h b/include/grub/offsets.h
index 330e4c707..871e1cd4c 100644
--- a/include/grub/offsets.h
+++ b/include/grub/offsets.h
@@ -36,9 +36,10 @@
 #define GRUB_DECOMPRESSOR_I386_PC_MAX_DECOMPRESSOR_SIZE (0x9000-0x8200)
 
 /* The segment where the kernel is loaded.  */
-#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
+#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
 
-#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR 0x10
 
 /* The upper memory area (starting at 640 kiB).  */
 #define GRUB_MEMORY_I386_PC_UPPER  0xa
@@ -101,15 +102,17 @@
 #define GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN   
GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN
 
 #define GRUB_KERNEL_X86_64_XEN_MOD_ALIGN   0x8
-#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_ALIGN 0x8
 
 /* Non-zero value is only needed for PowerMacs.  */
-#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP 0x0
-#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP 0x0
+#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
+#define GRUB_KERNEL_I386_XEN_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP  0x0
+#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP  0x0
+#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP   0x0
+#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP  0x0
 
 #define GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN 0x1000
 #define GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN 3
-- 
2.16.4


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


[PATCH v5 16/20] grub-module-verifier: Ignore all_video for xenpvh

2018-11-21 Thread Juergen Gross
From: Hans van Kranenburg 

This solves the build failing with "Error: no symbol table and no
.moddeps section"

Also see:
- 6371e9c10433578bb236a8284ddb9ce9e201eb59
- https://savannah.gnu.org/bugs/?49012

Signed-off-by: Hans van Kranenburg 
Reviewed-by: Daniel Kiper 
---
V2: new patch
Signed-off-by: Juergen Gross 
---
 util/grub-module-verifier.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index 03ba1ab43..97cb9 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -129,6 +129,7 @@ struct platform_whitelist {
 
 static struct platform_whitelist whitelists[] = {
   {"i386", "xen", (const char *[]) {"all_video", 0}},
+  {"i386", "xen_pvh", (const char *[]) {"all_video", 0}},
   {"x86_64", "xen", (const char *[]) {"all_video", 0}},
   {"sparc64", "ieee1275", (const char *[]) {"all_video", 0}},
 
-- 
2.16.4


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


[PATCH v5 05/20] xen: add some dummy headers for PVH mode

2018-11-21 Thread Juergen Gross
With Xen PVH mode adding a new machine type the machine related headers
need to be present for the build to succeed. Most of the headers just
need to include the related common i386 headers. Add those to the tree.

Note that xen_pvh/int.h needs to include pc/int_types.h instead of
pc/int.h in order to avoid the definition of grub_bios_interrupt().

xen_pvh/memory.h needs to include coreboot/memory.h (like some other
/memory.h do as well) as this contains just the needed stubs.

Signed-off-by: Juergen Gross 
---
V3: updated commit message (Daniel Kiper)
xenpvh->xen_pvh (Daniel Kiper)
V5: updated commit message (Daniel Kiper)
---
 include/grub/i386/xen_pvh/boot.h| 1 +
 include/grub/i386/xen_pvh/console.h | 1 +
 include/grub/i386/xen_pvh/int.h | 1 +
 include/grub/i386/xen_pvh/memory.h  | 1 +
 include/grub/i386/xen_pvh/time.h| 1 +
 5 files changed, 5 insertions(+)
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/memory.h
 create mode 100644 include/grub/i386/xen_pvh/time.h

diff --git a/include/grub/i386/xen_pvh/boot.h b/include/grub/i386/xen_pvh/boot.h
new file mode 100644
index 0..6cd23aa83
--- /dev/null
+++ b/include/grub/i386/xen_pvh/boot.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/console.h 
b/include/grub/i386/xen_pvh/console.h
new file mode 100644
index 0..305a46d8e
--- /dev/null
+++ b/include/grub/i386/xen_pvh/console.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/int.h b/include/grub/i386/xen_pvh/int.h
new file mode 100644
index 0..0f1f9ee62
--- /dev/null
+++ b/include/grub/i386/xen_pvh/int.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/memory.h 
b/include/grub/i386/xen_pvh/memory.h
new file mode 100644
index 0..8dd6f7c8c
--- /dev/null
+++ b/include/grub/i386/xen_pvh/memory.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/time.h b/include/grub/i386/xen_pvh/time.h
new file mode 100644
index 0..2298ee8f4
--- /dev/null
+++ b/include/grub/i386/xen_pvh/time.h
@@ -0,0 +1 @@
+#include 
-- 
2.16.4


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


[PATCH v5 04/20] xen: prepare common code for Xen PVH support

2018-11-21 Thread Juergen Gross
Some common code needs to be special cased for Xen PVH mode. This hits
mostly Xen PV mode specific areas.

Split include/grub/i386/pc/int_types.h off from
include/grub/i386/pc/int.h to support including this file later from
xen_pvh code without the grub_bios_interrupt definition.

Move definition of struct grub_e820_mmap_entry from
grub-core/mmap/i386/pc/mmap.c to include/grub/i386/memory.h in order
to make it usable from xen_pvh code.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: GRUB_MACHINE_XENPVH -> GRUB_MACHINE_XEN_PVH (Daniel Kiper)
split include/grub/i386/pc/int.h (Daniel Kiper)
move struct grub_e820_mmap_entry definition to header file
V5: minor style adjustments (Daniel Kiper)
---
 grub-core/kern/i386/tsc.c |  2 +-
 grub-core/mmap/i386/pc/mmap.c |  8 --
 include/grub/i386/memory.h|  7 +
 include/grub/i386/pc/int.h| 36 +---
 include/grub/i386/pc/int_types.h  | 59 +++
 include/grub/i386/tsc.h   |  2 +-
 include/grub/i386/xen/hypercall.h |  5 +++-
 include/grub/kernel.h |  4 ++-
 8 files changed, 76 insertions(+), 47 deletions(-)
 create mode 100644 include/grub/i386/pc/int_types.h

diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
index f266eb131..9293b161d 100644
--- a/grub-core/kern/i386/tsc.c
+++ b/grub-core/kern/i386/tsc.c
@@ -65,7 +65,7 @@ grub_tsc_init (void)
 
   tsc_boot_time = grub_get_tsc ();
 
-#ifdef GRUB_MACHINE_XEN
+#if defined (GRUB_MACHINE_XEN) || defined (GRUB_MACHINE_XEN_PVH)
   (void) (grub_tsc_calibrate_from_xen () || calibrate_tsc_hardcode());
 #elif defined (GRUB_MACHINE_EFI)
   (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () 
|| grub_tsc_calibrate_from_efi() || calibrate_tsc_hardcode());
diff --git a/grub-core/mmap/i386/pc/mmap.c b/grub-core/mmap/i386/pc/mmap.c
index 609994516..6ab4f6730 100644
--- a/grub-core/mmap/i386/pc/mmap.c
+++ b/grub-core/mmap/i386/pc/mmap.c
@@ -42,14 +42,6 @@ extern grub_uint16_t grub_machine_mmaphook_kblow;
 extern grub_uint16_t grub_machine_mmaphook_kbin16mb;
 extern grub_uint16_t grub_machine_mmaphook_64kbin4gb;
 
-struct grub_e820_mmap_entry
-{
-  grub_uint64_t addr;
-  grub_uint64_t len;
-  grub_uint32_t type;
-} GRUB_PACKED;
-
-
 /* Helper for preboot.  */
 static int fill_hook (grub_uint64_t addr, grub_uint64_t size,
  grub_memory_type_t type, void *data)
diff --git a/include/grub/i386/memory.h b/include/grub/i386/memory.h
index 8bb6e1cbb..5cb607fb4 100644
--- a/include/grub/i386/memory.h
+++ b/include/grub/i386/memory.h
@@ -44,6 +44,13 @@
 
 #include 
 
+struct grub_e820_mmap_entry
+{
+  grub_uint64_t addr;
+  grub_uint64_t len;
+  grub_uint32_t type;
+} GRUB_PACKED;
+
 grub_uint64_t grub_mmap_get_upper (void);
 grub_uint64_t grub_mmap_get_lower (void);
 grub_uint64_t grub_mmap_get_post64 (void);
diff --git a/include/grub/i386/pc/int.h b/include/grub/i386/pc/int.h
index 16a53e4fe..a60104001 100644
--- a/include/grub/i386/pc/int.h
+++ b/include/grub/i386/pc/int.h
@@ -20,45 +20,11 @@
 #define GRUB_INTERRUPT_MACHINE_HEADER  1
 
 #include 
-#include 
-
-struct grub_bios_int_registers
-{
-  grub_uint32_t eax;
-  grub_uint16_t es;
-  grub_uint16_t ds;
-  grub_uint16_t flags;
-  grub_uint16_t dummy;
-  grub_uint32_t ebx;
-  grub_uint32_t ecx;
-  grub_uint32_t edi;
-  grub_uint32_t esi;
-  grub_uint32_t edx;
-};
-
-#define  GRUB_CPU_INT_FLAGS_CARRY 0x1
-#define  GRUB_CPU_INT_FLAGS_PARITY0x4
-#define  GRUB_CPU_INT_FLAGS_ADJUST0x10
-#define  GRUB_CPU_INT_FLAGS_ZERO  0x40
-#define  GRUB_CPU_INT_FLAGS_SIGN  0x80
-#define  GRUB_CPU_INT_FLAGS_TRAP  0x100
-#define  GRUB_CPU_INT_FLAGS_INTERRUPT 0x200
-#define  GRUB_CPU_INT_FLAGS_DIRECTION 0x400
-#define  GRUB_CPU_INT_FLAGS_OVERFLOW  0x800
-#ifdef GRUB_MACHINE_PCBIOS
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   GRUB_CPU_INT_FLAGS_INTERRUPT
-#else
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   0
-#endif
+#include 
 
 void EXPORT_FUNC (grub_bios_interrupt) (grub_uint8_t intno,
struct grub_bios_int_registers *regs)
  __attribute__ ((regparm(3)));
-struct grub_i386_idt
-{
-  grub_uint16_t limit;
-  grub_uint32_t base;
-} GRUB_PACKED;
 
 #ifdef GRUB_MACHINE_PCBIOS
 extern struct grub_i386_idt *EXPORT_VAR(grub_realidt);
diff --git a/include/grub/i386/pc/int_types.h b/include/grub/i386/pc/int_types.h
new file mode 100644
index 0..2c5a69b63
--- /dev/null
+++ b/include/grub/i386/pc/int_types.h
@@ -0,0 +1,59 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRA

[PATCH v5 09/20] xen: add basic hooks for PVH in current code

2018-11-21 Thread Juergen Gross
Add the hooks to current code needed for Xen PVH. They will be filled
with code later when the related functionality is being added.

loader/i386/linux.c needs to include machine/kernel.h now as it needs
to get GRUB_KERNEL_USE_RSDP_ADDR from there.

Signed-off-by: Juergen Gross 
---
V3: xenpvh->xen_pvh (Daniel Kiper)
adjust copyright date (Roger Pau Monné)
V5: update commit message (Daniel Kiper)
move including xen/hvm/start_info.h to the sources really needing
  it (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 37 +++
 grub-core/kern/i386/xen/startup_pvh.S | 29 +++
 grub-core/kern/xen/init.c |  4 
 grub-core/loader/i386/linux.c |  1 +
 include/grub/i386/xen_pvh/kernel.h| 30 
 include/grub/xen.h|  5 +
 6 files changed, 106 insertions(+)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/xen_pvh/kernel.h

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
new file mode 100644
index 0..4f629b15e
--- /dev/null
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -0,0 +1,37 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+grub_uint64_t grub_rsdp_addr;
+
+void
+grub_xen_setup_pvh (void)
+{
+}
+
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+}
diff --git a/grub-core/kern/i386/xen/startup_pvh.S 
b/grub-core/kern/i386/xen/startup_pvh.S
new file mode 100644
index 0..69b8fdcca
--- /dev/null
+++ b/grub-core/kern/i386/xen/startup_pvh.S
@@ -0,0 +1,29 @@
+/* startup.S - bootstrap GRUB itself */
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include 
+#include 
+
+   .file   "startup_pvh.S"
+   .text
+
+/* Saved pointer to start info structure. */
+   .globl  pvh_start_info
+pvh_start_info:
+   .long   0
diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index a23dad633..782ca7295 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -564,6 +564,10 @@ grub_machine_init (void)
   + GRUB_KERNEL_MACHINE_MOD_GAP,
   GRUB_KERNEL_MACHINE_MOD_ALIGN);
 
+#ifdef GRUB_MACHINE_XEN_PVH
+  grub_xen_setup_pvh ();
+#endif
+
   grub_xen_setup_gnttab ();
 
 #ifdef GRUB_MACHINE_XEN
diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index 375ee80dc..b6015913b 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -35,6 +35,7 @@
 #include 
 #include 
 #include 
+#include 
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
diff --git a/include/grub/i386/xen_pvh/kernel.h 
b/include/grub/i386/xen_pvh/kernel.h
new file mode 100644
index 0..2b7b8a129
--- /dev/null
+++ b/include/grub/i386/xen_pvh/kernel.h
@@ -0,0 +1,30 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Gene

[PATCH v5 02/20] loader/linux: support passing rsdp address via boot params

2018-11-21 Thread Juergen Gross
Xen PVH guests will have the RSDP at an arbitrary address. Support that
by passing the RSDP address via the boot parameters to Linux.

Signed-off-by: Juergen Gross 
---
V2: add oring 0x8000 to version field
V3: move including machine/kernel.h to patch 8 (Daniel Kiper)
V5: move acpi_rsdp_addr to struct linux_kernel_params (Peter Anvin)
---
 grub-core/loader/i386/linux.c | 4 
 include/grub/i386/linux.h | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/grub-core/loader/i386/linux.c b/grub-core/loader/i386/linux.c
index c408b10d8..375ee80dc 100644
--- a/grub-core/loader/i386/linux.c
+++ b/grub-core/loader/i386/linux.c
@@ -508,6 +508,10 @@ grub_linux_boot (void)
}
 }
 
+#ifdef GRUB_KERNEL_USE_RSDP_ADDR
+  linux_params.acpi_rsdp_addr = grub_le_to_cpu64 (grub_rsdp_addr);
+#endif
+
   mmap_size = find_mmap_size ();
   /* Make sure that each size is aligned to a page boundary.  */
   cl_offset = ALIGN_UP (mmap_size + sizeof (linux_params), 4096);
diff --git a/include/grub/i386/linux.h b/include/grub/i386/linux.h
index 60c7c3b5e..a96059311 100644
--- a/include/grub/i386/linux.h
+++ b/include/grub/i386/linux.h
@@ -210,8 +210,9 @@ struct linux_kernel_params
   grub_uint32_t ist_command;   /* 64 */
   grub_uint32_t ist_event; /* 68 */
   grub_uint32_t ist_perf_level;/* 6c */
+  grub_uint64_t acpi_rsdp_addr;/* 70 */
 
-  grub_uint8_t padding5[0x80 - 0x70];
+  grub_uint8_t padding5[0x80 - 0x78];
 
   grub_uint8_t hd0_drive_info[0x10];   /* 80 */
   grub_uint8_t hd1_drive_info[0x10];   /* 90 */
-- 
2.16.4


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


[PATCH v5 06/20] xen: rearrange xen/init.c to prepare it for Xen PVH mode

2018-11-21 Thread Juergen Gross
Rearrange grub-core/kern/xen/init.c to prepare adding PVH mode support
to it. This includes putting some code under #ifdef GRUB_MACHINE_XEN
as it will not be used when running as PVH.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/xen/init.c | 60 +++
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 29f5bc23d..10007b411 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -41,9 +41,11 @@ grub_size_t grub_xen_n_allocated_shared_pages;
 static grub_xen_mfn_t
 grub_xen_ptr2mfn (void *ptr)
 {
+#ifdef GRUB_MACHINE_XEN
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#endif
 }
 
 void *
@@ -104,18 +106,6 @@ grub_machine_get_bootlocation (char **device __attribute__ 
((unused)),
 {
 }
 
-static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
-  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
-
-#ifdef __x86_64__
-#define NUMBER_OF_LEVELS 4
-#else
-#define NUMBER_OF_LEVELS 3
-#endif
-
-#define LOG_POINTERS_PER_PAGE 9
-#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
-
 void
 grub_xen_store_send (const void *buf_, grub_size_t len)
 {
@@ -337,6 +327,19 @@ grub_xen_setup_gnttab (void)
   grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
 }
 
+#ifdef GRUB_MACHINE_XEN
+static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+#ifdef __x86_64__
+#define NUMBER_OF_LEVELS 4
+#else
+#define NUMBER_OF_LEVELS 3
+#endif
+
+#define LOG_POINTERS_PER_PAGE 9
+#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -529,13 +532,30 @@ map_all_pages (void)
   grub_mm_init_region ((void *) heap_start, heap_end - heap_start);
 }
 
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
+  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
+  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
+return GRUB_ERR_NONE;
+
+  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
+   GRUB_MEMORY_RESERVED, hook_data);
+
+  return GRUB_ERR_NONE;
+}
+#endif
+
 extern char _end[];
 
 void
 grub_machine_init (void)
 {
+#ifdef GRUB_MACHINE_XEN
 #ifdef __i386__
   grub_xen_vm_assist (VMASST_CMD_enable, VMASST_TYPE_pae_extended_cr3);
+#endif
 #endif
 
   grub_modbase = ALIGN_UP ((grub_addr_t) _end
@@ -544,7 +564,9 @@ grub_machine_init (void)
 
   grub_xen_setup_gnttab ();
 
+#ifdef GRUB_MACHINE_XEN
   map_all_pages ();
+#endif
 
   grub_console_init ();
 
@@ -571,17 +593,3 @@ grub_machine_fini (int flags __attribute__ ((unused)))
   grub_xendisk_fini ();
   grub_boot_fini ();
 }
-
-grub_err_t
-grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
-{
-  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
-  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
-  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
-return GRUB_ERR_NONE;
-
-  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
-   GRUB_MEMORY_RESERVED, hook_data);
-
-  return GRUB_ERR_NONE;
-}
-- 
2.16.4


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


[PATCH v5 07/20] xen: modify grub_xen_ptr2mfn() for xen-pvh

2018-11-21 Thread Juergen Gross
grub_xen_ptr2mfn() returns the machine frame number for a given pointer
value. For Xen-PVH guests this is just the PFN. Add the PVH specific
variant.

Signed-off-by: Juergen Gross 
---
V5: new patch (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 10007b411..a23dad633 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -45,6 +45,8 @@ grub_xen_ptr2mfn (void *ptr)
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#else
+  return (grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE;
 #endif
 }
 
-- 
2.16.4


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


[PATCH v5 03/20] xen: carve out grant tab initialization into dedicated function

2018-11-21 Thread Juergen Gross
Initialize the grant tab in a dedicated function. This will enable
using it for PVH guests, too.

Call the new function from grub_machine_init() as this will later
be common between Xen PV and Xen PVH mode.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 35 +--
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 0559c033c..29f5bc23d 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -318,6 +318,25 @@ grub_xenstore_dir (const char *dir,
 
 unsigned long gntframe = 0;
 
+static void
+grub_xen_setup_gnttab (void)
+{
+  struct gnttab_set_version gnttab_setver;
+  struct gnttab_setup_table gnttab_setup;
+
+  grub_memset (_setver, 0, sizeof (gnttab_setver));
+
+  gnttab_setver.version = 1;
+  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
+
+  grub_memset (_setup, 0, sizeof (gnttab_setup));
+  gnttab_setup.dom = DOMID_SELF;
+  gnttab_setup.nr_frames = 1;
+  gnttab_setup.frame_list.p = 
+
+  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
+}
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -357,26 +376,12 @@ map_all_pages (void)
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   grub_uint64_t *pg = (grub_uint64_t *) window;
   grub_uint64_t oldpgstart, oldpgend;
-  struct gnttab_setup_table gnttab_setup;
-  struct gnttab_set_version gnttab_setver;
   grub_size_t n_unusable_pages = 0;
   struct mmu_update m2p_updates[2 * MAX_N_UNUSABLE_PAGES];
 
   if (total_pages > MAX_TOTAL_PAGES - 4)
 total_pages = MAX_TOTAL_PAGES - 4;
 
-  grub_memset (_setver, 0, sizeof (gnttab_setver));
-
-  gnttab_setver.version = 1;
-  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
-
-  grub_memset (_setup, 0, sizeof (gnttab_setup));
-  gnttab_setup.dom = DOMID_SELF;
-  gnttab_setup.nr_frames = 1;
-  gnttab_setup.frame_list.p = 
-
-  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
-
   for (j = 0; j < total_pages - n_unusable_pages; j++)
 while (!grub_xen_is_page_usable (mfn_list[j]))
   {
@@ -537,6 +542,8 @@ grub_machine_init (void)
   + GRUB_KERNEL_MACHINE_MOD_GAP,
   GRUB_KERNEL_MACHINE_MOD_ALIGN);
 
+  grub_xen_setup_gnttab ();
+
   map_all_pages ();
 
   grub_console_init ();
-- 
2.16.4


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


Re: [Xen-devel] [PATCH v4 10/19] xen: setup hypercall page for PVH

2018-11-21 Thread Juergen Gross
On 20/11/2018 10:27, Juergen Gross wrote:
> On 09/11/2018 19:12, Daniel Kiper wrote:
>> On Fri, Nov 02, 2018 at 01:37:29PM +0100, Juergen Gross wrote:>>> +
>>> +  asm volatile ("wrmsr" : : "c" (msr), "a" (pfn), "d" (0) : "memory");
>>> +}
>>> +
>>> +int
>>> +grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
>>> +   grub_uint32_t a1, grub_uint32_t a2,
>>> +   grub_uint32_t a3, grub_uint32_t a4,
>>> +   grub_uint32_t a5 __attribute__ ((unused)))
>>> +{
>>> +  grub_uint32_t __res, __ign0, __ign1, __ign2, __ign3, __ign4;
>>> +
>>> +  asm volatile ("call *%[callno]"
>>> +   : "=a" (__res), "=b" (__ign0), "=c" (__ign1), "=d" (__ign2),
>>> + "=S" (__ign3), "=D" (__ign4)
>>> +   : "1" (a0), "2" (a1), "3" (a2), "4" (a3), "5" (a4),
>>
>> I think that you can drop all __ign* variables if you specify proper
>> registers in input argument. If this does not work you can use "+"
>> modifier instead of "=" in the output argument.
> 
> I'll have a try.

Doesn't work.

Specifying only the input registers neglects the hypercall
clobbering those.

Using the "+" modifier lets gcc complain.

I'll just use a single dummy variable.


Juergen

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


Re: [Xen-devel] [PATCH v4 13/19] xen: setup Xen specific data for PVH

2018-11-21 Thread Juergen Gross
On 15/11/2018 11:03, Roger Pau Monné wrote:
> On Fri, Nov 02, 2018 at 01:37:32PM +0100, Juergen Gross wrote:
>> Initialize the needed Xen specific data. This is:
>>
>> - the Xen start of day page containing the console and Xenstore ring
>>   page PFN and event channel
>> - the grant table
>> - the shared info page
>>
>> Write back the possibly modified memory map to the hypervisor in case
>> the guest is reading it from there again.
>>
>> Set the RSDP address for the guest from the start_info page passed
>> as boot parameter.
>>
>> Signed-off-by: Juergen Gross 
>> ---
>> V4: write back memory map to Xen (Roger Pau Monné)
>> ---
>>  grub-core/kern/i386/xen/pvh.c | 119 
>> ++
>>  1 file changed, 119 insertions(+)
>>
>> diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
>> +static grub_uint64_t
>> +grub_xen_find_page (grub_uint64_t start)
>> +{
>> +  unsigned int i, j;
>> +  grub_uint64_t last = start;
>> +
>> +  /* Try to find a e820 map hole below 4G. */
>> +  for (i = 0; i < nr_map_entries; i++)
>> +{
>> +  if (last > map[i].addr + map[i].len)
>> +continue;
>> +  if (last < map[i].addr)
> 
> Shouldn't this be last + PAGE_SIZE <= map[i].addr?
> 
> grub_xen_sort_mmap already aligns memory map entries to page
> boundaries, so the result will be correct as long as 'start' is also
> page aligned.

Correct, page alignment is a requirement (which is met currently).
I'll add a comment to point that out.


Juergen


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


Re: [Xen-devel] [PATCH v4 12/19] xen: add PCI MMIO areas to memory map

2018-11-21 Thread Juergen Gross
On 14/11/2018 13:48, Roger Pau Monné wrote:
> On Fri, Nov 02, 2018 at 01:37:31PM +0100, Juergen Gross wrote:
>> Add possible PCI space MMIO areas as "Reserved" to the memory map in
>> order to avoid using those areas for special Xen pages later.
> 
> TBH, I'm not sure this is the best way to solve the issues related to
> where to map stuff in the physmap without colliding with either
> emulated or passed through MMIO regions.
> 
> IMO I think the guest should be able to query this from Xen, overall I
> would defer this patch until there's a discussion about where to map
> stuff safely in the physmap for autotranslated guests.

Okay, I'll drop the patch for now.


Juergen

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


Re: [Xen-devel] [PATCH v4 11/19] xen: get memory map from hypervisor for PVH

2018-11-21 Thread Juergen Gross
On 15/11/2018 10:36, Roger Pau Monné wrote:
> On Fri, Nov 02, 2018 at 01:37:30PM +0100, Juergen Gross wrote:
>> Retrieve the memory map from the hypervisor and normalize it to contain
>> no overlapping entries and to be sorted by address.
>>
>> Signed-off-by: Juergen Gross 
>> ---
>> V3: use grub_e820_mmap_entry instead of own struct (Daniel Kiper)
>> ---
>>  grub-core/kern/i386/xen/pvh.c | 96 
>> ++-
>>  1 file changed, 95 insertions(+), 1 deletion(-)
>>
>> diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
>> index 7e90a4538..58e6fefd5 100644
>> --- a/grub-core/kern/i386/xen/pvh.c
>> +++ b/grub-core/kern/i386/xen/pvh.c
>> @@ -23,9 +23,14 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>>  #include 
>> +#include 
>> +#include 
>>  
>> -#define XEN_CONSOLE_PORT   0xe9
>> +#define XEN_CONSOLE_PORT  0xe9
>> +
>> +#define XEN_MEMORY_MAP_SIZE   128
>>  
>>  grub_uint64_t grub_rsdp_addr;
>>  
>> @@ -33,6 +38,8 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
>>__attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
>>  
>>  static grub_uint32_t xen_cpuid_base;
>> +static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
>> +static unsigned int nr_map_entries;
>>  
>>  static void
>>  grub_xen_panic (const char *msg)
>> @@ -92,11 +99,98 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t 
>> a0,
>>return __res;
>>  }
>>  
>> +static void
>> +grub_xen_sort_mmap (void)
> 
> There's nothing Xen-specific about this function, shouldn't it reside
> in some x86 related file?

As long as there is no other potential user of it I don't think so. Why
should we bloat up grub for other machine types without a need?


Juergen


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


Re: [PATCH v4 10/19] xen: setup hypercall page for PVH

2018-11-20 Thread Juergen Gross
On 09/11/2018 19:12, Daniel Kiper wrote:
> On Fri, Nov 02, 2018 at 01:37:29PM +0100, Juergen Gross wrote:
>> Add the needed code to setup the hypercall page for calling into the
>> Xen hypervisor.
>>
>> Signed-off-by: Juergen Gross 
>> ---
>> V3: grub_xen_early_halt->grub_xen_panic (Roger Pau Monné)
>> issue panic message (Roger Pau Monné)
>> rewrite grub_xen_hypercall to avoid register variables (Daniel Kiper)
>> ---
>>  grub-core/kern/i386/xen/pvh.c | 69 
>> +++
>>  1 file changed, 69 insertions(+)
>>
>> diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
>> index ac6181f4e..7e90a4538 100644
>> --- a/grub-core/kern/i386/xen/pvh.c
>> +++ b/grub-core/kern/i386/xen/pvh.c
>> @@ -20,14 +20,83 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>> +#include 
>>  #include 
>>  #include 
>>
>> +#define XEN_CONSOLE_PORT   0xe9
> 
> I think that this is not PVH specific thing. Could you move this to
> more generic Xen header?
> 
>>  grub_uint64_t grub_rsdp_addr;
> 
> Hmmm... It seems to me that immediately after patch #8 GRUB2 build is
> broken. Is not it?

No. xen-pvh isn't yet selectable.

> 
>> +static char hypercall_page[GRUB_XEN_PAGE_SIZE]
>> +  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
>> +
>> +static grub_uint32_t xen_cpuid_base;
>> +
>> +static void
>> +grub_xen_panic (const char *msg)
>> +{
>> +  const char *c;
>> +
>> +  for (c = msg; *c; c++)
>> +grub_outb (*c, XEN_CONSOLE_PORT);
>> +
>> +  asm volatile ("hlt");
> 
> Should not you do something similar to grub-core/lib/i386/halt.c:stop() here?

Yes, as already replied to Roger.

> 
>> +}
>> +
>> +static void
>> +grub_xen_cpuid_base (void)
>> +{
>> +  grub_uint32_t base, eax, signature[3];
>> +
>> +  for (base = 0x4000; base < 0x4001; base += 0x100)
>> +{
>> +  grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
>> +  if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
>> +{
>> +  xen_cpuid_base = base;
>> +  return;
>> +}
>> +}
>> +
>> +  grub_xen_panic ("Found no Xen signature.\n");
> 
> "Found no Xen signature!\nSystem halted!\n"
> 
> Or maybe grub_xen_panic() should always add "System halted!\n".

Okay (the latter).

> 
>> +}
>> +
>> +static void
>> +grub_xen_setup_hypercall_page (void)
>> +{
>> +  grub_uint32_t msr, pfn, eax, ebx, ecx, edx;
>> +
>> +  grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
>> +  msr = ebx;
>> +  pfn = (grub_uint32_t) (_page[0]);
> 
> Could not you use hypercall_page alone here?

Hmm, yes.

> 
>> +
>> +  asm volatile ("wrmsr" : : "c" (msr), "a" (pfn), "d" (0) : "memory");
>> +}
>> +
>> +int
>> +grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
>> +grub_uint32_t a1, grub_uint32_t a2,
>> +grub_uint32_t a3, grub_uint32_t a4,
>> +grub_uint32_t a5 __attribute__ ((unused)))
>> +{
>> +  grub_uint32_t __res, __ign0, __ign1, __ign2, __ign3, __ign4;
>> +
>> +  asm volatile ("call *%[callno]"
>> +: "=a" (__res), "=b" (__ign0), "=c" (__ign1), "=d" (__ign2),
>> +  "=S" (__ign3), "=D" (__ign4)
>> +: "1" (a0), "2" (a1), "3" (a2), "4" (a3), "5" (a4),
> 
> I think that you can drop all __ign* variables if you specify proper
> registers in input argument. If this does not work you can use "+"
> modifier instead of "=" in the output argument.

I'll have a try.


Juergen


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


Re: [Xen-devel] [PATCH v4 10/19] xen: setup hypercall page for PVH

2018-11-20 Thread Juergen Gross
On 15/11/2018 09:33, Roger Pau Monné wrote:
> On Fri, Nov 02, 2018 at 01:37:29PM +0100, Juergen Gross wrote:
>> Add the needed code to setup the hypercall page for calling into the
>> Xen hypervisor.
>>
>> Signed-off-by: Juergen Gross 
> 
> Thanks, LGTM, just one suggestion and one question.
> 
>> ---
>> V3: grub_xen_early_halt->grub_xen_panic (Roger Pau Monné)
>> issue panic message (Roger Pau Monné)
>> rewrite grub_xen_hypercall to avoid register variables (Daniel Kiper)
>> ---
>>  grub-core/kern/i386/xen/pvh.c | 69 
>> +++
>>  1 file changed, 69 insertions(+)
>>
>> diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
>> index ac6181f4e..7e90a4538 100644
>> --- a/grub-core/kern/i386/xen/pvh.c
>> +++ b/grub-core/kern/i386/xen/pvh.c
>> @@ -20,14 +20,83 @@
>>  #include 
>>  #include 
>>  #include 
>> +#include 
>> +#include 
>>  #include 
>>  #include 
>>  
>> +#define XEN_CONSOLE_PORT   0xe9
> 
> This is now part of the public headers (XEN_HVM_DEBUGCONS_IOPORT) if
> you wish to pick it up from there.

I don't want to import headers from Xen unstable, so I'll add just that
define (and the associated comment) to include/xen/arch-x86/xen.h

> 
>> +
>>  grub_uint64_t grub_rsdp_addr;
>>  
>> +static char hypercall_page[GRUB_XEN_PAGE_SIZE]
>> +  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
>> +
>> +static grub_uint32_t xen_cpuid_base;
>> +
>> +static void
>> +grub_xen_panic (const char *msg)
>> +{
>> +  const char *c;
>> +
>> +  for (c = msg; *c; c++)
>> +grub_outb (*c, XEN_CONSOLE_PORT);
>> +
>> +  asm volatile ("hlt");
> 
> Should this be "cli; hlt;", or does grub always runs with interrupt flag
> clear?

Good point. Maybe I should even add a loop around the hlt.


Juergen


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


Re: [Xen-devel] [PATCH v4 08/19] xen: add basic hooks for PVH in current code

2018-11-08 Thread Juergen Gross
On 08/11/2018 16:45, Daniel Kiper wrote:
> On Fri, Nov 02, 2018 at 01:37:27PM +0100, Juergen Gross wrote:
>> Add the hooks to current code needed for Xen PVH. They will be filled
>> with code later when the related functionality is being added.
>>
>> Signed-off-by: Juergen Gross 
>> ---
>> V3: xenpvh->xen_pvh (Daniel Kiper)
>> adjust copyright date (Roger Pau Monné)
>> ---
>>  grub-core/kern/i386/xen/pvh.c | 36 
>> +++
>>  grub-core/kern/i386/xen/startup_pvh.S | 29 
>>  grub-core/kern/xen/init.c |  6 ++
>>  grub-core/loader/i386/linux.c |  1 +
>>  include/grub/i386/xen_pvh/kernel.h| 30 +
>>  include/grub/xen.h|  6 ++
>>  6 files changed, 108 insertions(+)
>>  create mode 100644 grub-core/kern/i386/xen/pvh.c
>>  create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
>>  create mode 100644 include/grub/i386/xen_pvh/kernel.h
>>
>> diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
>> new file mode 100644
>> index 0..ac6181f4e
>> --- /dev/null
>> +++ b/grub-core/kern/i386/xen/pvh.c
>> @@ -0,0 +1,36 @@
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2018  Free Software Foundation, Inc.
>> + *
>> + *  GRUB is free software: you can redistribute it and/or modify
>> + *  it under the terms of the GNU General Public License as published by
>> + *  the Free Software Foundation, either version 3 of the License, or
>> + *  (at your option) any later version.
>> + *
>> + *  GRUB is distributed in the hope that it will be useful,
>> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + *  GNU General Public License for more details.
>> + *
>> + *  You should have received a copy of the GNU General Public License
>> + *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
>> + */
>> +
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>> +
>> +grub_uint64_t grub_rsdp_addr;
>> +
>> +void
>> +grub_xen_setup_pvh (void)
>> +{
>> +}
>> +
>> +grub_err_t
>> +grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
>> +{
>> +}
>> diff --git a/grub-core/kern/i386/xen/startup_pvh.S 
>> b/grub-core/kern/i386/xen/startup_pvh.S
>> new file mode 100644
>> index 0..69b8fdcca
>> --- /dev/null
>> +++ b/grub-core/kern/i386/xen/startup_pvh.S
>> @@ -0,0 +1,29 @@
>> +/* startup.S - bootstrap GRUB itself */
>> +/*
>> + *  GRUB  --  GRand Unified Bootloader
>> + *  Copyright (C) 2018  Free Software Foundation, Inc.
>> + *
>> + *  GRUB is free software: you can redistribute it and/or modify
>> + *  it under the terms of the GNU General Public License as published by
>> + *  the Free Software Foundation, either version 3 of the License, or
>> + *  (at your option) any later version.
>> + *
>> + *  GRUB is distributed in the hope that it will be useful,
>> + *  but WITHOUT ANY WARRANTY; without even the implied warranty of
>> + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>> + *  GNU General Public License for more details.
>> + *
>> + *  You should have received a copy of the GNU General Public License
>> + *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
>> + */
>> +
>> +#include 
>> +#include 
>> +
>> +.file   "startup_pvh.S"
>> +.text
>> +
>> +/* Saved pointer to start info structure. */
>> +.globl  pvh_start_info
>> +pvh_start_info:
>> +.long   0
>> diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
>> index 10007b411..782ca7295 100644
>> --- a/grub-core/kern/xen/init.c
>> +++ b/grub-core/kern/xen/init.c
>> @@ -45,6 +45,8 @@ grub_xen_ptr2mfn (void *ptr)
>>grub_xen_mfn_t *mfn_list =
>>  (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
>>return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
>> +#else
>> +  return (grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE;
> 
> It seems to me that this change does not belong to this patch.

Hmm, in which patch would you put it?

I wanted to have all modifications to current grub-core code in place
after this patch. This includes all needed #ifdefs.

> 
>>  #endif
>

Re: [PATCH v4 05/19] xen: add some dummy headers for PVH mode

2018-11-08 Thread Juergen Gross
On 08/11/2018 16:36, Daniel Kiper wrote:
> On Wed, Nov 07, 2018 at 03:49:51PM +0100, Juergen Gross wrote:
>> On 07/11/2018 13:21, Daniel Kiper wrote:
>>> On Fri, Nov 02, 2018 at 01:37:24PM +0100, Juergen Gross wrote:
>>>> With Xen PVH mode adding a new machine type the machine related headers
>>>> need to be present for the build to succeed. Most of the headers just
>>>> need to include the related common i386 headers. Add those to the tree.
>>>>
>>>> Signed-off-by: Juergen Gross 
>>>> ---
>>>> V3: updated commit message (Daniel Kiper)
>>>> xenpvh->xen_pvh (Daniel Kiper)
>>>> ---
>>>>  include/grub/i386/xen_pvh/boot.h| 1 +
>>>>  include/grub/i386/xen_pvh/console.h | 1 +
>>>>  include/grub/i386/xen_pvh/int.h | 1 +
>>>>  include/grub/i386/xen_pvh/memory.h  | 1 +
>>>>  include/grub/i386/xen_pvh/time.h| 1 +
>>>>  5 files changed, 5 insertions(+)
>>>>  create mode 100644 include/grub/i386/xen_pvh/boot.h
>>>>  create mode 100644 include/grub/i386/xen_pvh/console.h
>>>>  create mode 100644 include/grub/i386/xen_pvh/int.h
>>>>  create mode 100644 include/grub/i386/xen_pvh/memory.h
>>>>  create mode 100644 include/grub/i386/xen_pvh/time.h
>>>>
>>>> diff --git a/include/grub/i386/xen_pvh/boot.h 
>>>> b/include/grub/i386/xen_pvh/boot.h
>>>> new file mode 100644
>>>> index 0..6cd23aa83
>>>> --- /dev/null
>>>> +++ b/include/grub/i386/xen_pvh/boot.h
>>>> @@ -0,0 +1 @@
>>>> +#include 
>>>> diff --git a/include/grub/i386/xen_pvh/console.h 
>>>> b/include/grub/i386/xen_pvh/console.h
>>>> new file mode 100644
>>>> index 0..305a46d8e
>>>> --- /dev/null
>>>> +++ b/include/grub/i386/xen_pvh/console.h
>>>> @@ -0,0 +1 @@
>>>> +#include 
>>>> diff --git a/include/grub/i386/xen_pvh/int.h 
>>>> b/include/grub/i386/xen_pvh/int.h
>>>> new file mode 100644
>>>> index 0..0f1f9ee62
>>>> --- /dev/null
>>>> +++ b/include/grub/i386/xen_pvh/int.h
>>>> @@ -0,0 +1 @@
>>>> +#include 
>>>
>>> I think that this begs for explanation in the commit message
>>> why not grub/i386/pc/int.h.
>>
>> Okay.
>>
>>>
>>>> diff --git a/include/grub/i386/xen_pvh/memory.h 
>>>> b/include/grub/i386/xen_pvh/memory.h
>>>> new file mode 100644
>>>> index 0..8dd6f7c8c
>>>> --- /dev/null
>>>> +++ b/include/grub/i386/xen_pvh/memory.h
>>>> @@ -0,0 +1 @@
>>>> +#include 
>>>
>>> Hmmm... Why not include/grub/i386/pc/memory.h?
>>
>> The coreboot variant is containing the stubs grub_machine_mmap_register
>> and grub_machine_mmap_unregister I need, with the pc variant I'd have to
>> add those to xen-pvh code.
>>
>> Using the coreboot variant for that purpose seems to be common practice:
>> include/grub/i386/qemu/memory.h
>> include/grub/i386/ieee1275/memory.h
>> include/grub/i386/multiboot/memory.h
>> are doing exactly the same.
> 
> I am OK with it then. However, please say about that in the commit
> message. Otherwise it looks like a mistake.

Will do.


Juergen

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


Re: [PATCH v4 05/19] xen: add some dummy headers for PVH mode

2018-11-07 Thread Juergen Gross
On 07/11/2018 13:21, Daniel Kiper wrote:
> On Fri, Nov 02, 2018 at 01:37:24PM +0100, Juergen Gross wrote:
>> With Xen PVH mode adding a new machine type the machine related headers
>> need to be present for the build to succeed. Most of the headers just
>> need to include the related common i386 headers. Add those to the tree.
>>
>> Signed-off-by: Juergen Gross 
>> ---
>> V3: updated commit message (Daniel Kiper)
>> xenpvh->xen_pvh (Daniel Kiper)
>> ---
>>  include/grub/i386/xen_pvh/boot.h| 1 +
>>  include/grub/i386/xen_pvh/console.h | 1 +
>>  include/grub/i386/xen_pvh/int.h | 1 +
>>  include/grub/i386/xen_pvh/memory.h  | 1 +
>>  include/grub/i386/xen_pvh/time.h| 1 +
>>  5 files changed, 5 insertions(+)
>>  create mode 100644 include/grub/i386/xen_pvh/boot.h
>>  create mode 100644 include/grub/i386/xen_pvh/console.h
>>  create mode 100644 include/grub/i386/xen_pvh/int.h
>>  create mode 100644 include/grub/i386/xen_pvh/memory.h
>>  create mode 100644 include/grub/i386/xen_pvh/time.h
>>
>> diff --git a/include/grub/i386/xen_pvh/boot.h 
>> b/include/grub/i386/xen_pvh/boot.h
>> new file mode 100644
>> index 0..6cd23aa83
>> --- /dev/null
>> +++ b/include/grub/i386/xen_pvh/boot.h
>> @@ -0,0 +1 @@
>> +#include 
>> diff --git a/include/grub/i386/xen_pvh/console.h 
>> b/include/grub/i386/xen_pvh/console.h
>> new file mode 100644
>> index 0..305a46d8e
>> --- /dev/null
>> +++ b/include/grub/i386/xen_pvh/console.h
>> @@ -0,0 +1 @@
>> +#include 
>> diff --git a/include/grub/i386/xen_pvh/int.h 
>> b/include/grub/i386/xen_pvh/int.h
>> new file mode 100644
>> index 0..0f1f9ee62
>> --- /dev/null
>> +++ b/include/grub/i386/xen_pvh/int.h
>> @@ -0,0 +1 @@
>> +#include 
> 
> I think that this begs for explanation in the commit message
> why not grub/i386/pc/int.h.

Okay.

> 
>> diff --git a/include/grub/i386/xen_pvh/memory.h 
>> b/include/grub/i386/xen_pvh/memory.h
>> new file mode 100644
>> index 0..8dd6f7c8c
>> --- /dev/null
>> +++ b/include/grub/i386/xen_pvh/memory.h
>> @@ -0,0 +1 @@
>> +#include 
> 
> Hmmm... Why not include/grub/i386/pc/memory.h?

The coreboot variant is containing the stubs grub_machine_mmap_register
and grub_machine_mmap_unregister I need, with the pc variant I'd have to
add those to xen-pvh code.

Using the coreboot variant for that purpose seems to be common practice:
include/grub/i386/qemu/memory.h
include/grub/i386/ieee1275/memory.h
include/grub/i386/multiboot/memory.h
are doing exactly the same.


Juergen

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


[PATCH v4 04/19] xen: prepare common code for Xen PVH support

2018-11-02 Thread Juergen Gross
Some common code needs to be special cased for Xen PVH mode. This hits
mostly Xen PV mode specific areas.

Split include/grub/i386/pc/int_types.h off from
include/grub/i386/pc/int.h to support including this file later from
xen_pvh code without the grub_bios_interrupt definition.

Move definition of struct grub_e820_mmap_entry from
grub-core/mmap/i386/pc/mmap.c to include/grub/i386/memory.h in order
to make it usable from xen_pvh code.

Signed-off-by: Juergen Gross 
---
V3: GRUB_MACHINE_XENPVH -> GRUB_MACHINE_XEN_PVH (Daniel Kiper)
split include/grub/i386/pc/int.h (Daniel Kiper)
move struct grub_e820_mmap_entry definition to header file
---
 grub-core/kern/i386/tsc.c |  2 +-
 grub-core/mmap/i386/pc/mmap.c |  7 -
 include/grub/i386/memory.h|  7 +
 include/grub/i386/pc/int.h| 36 +---
 include/grub/i386/pc/int_types.h  | 59 +++
 include/grub/i386/tsc.h   |  2 +-
 include/grub/i386/xen/hypercall.h |  5 +++-
 include/grub/kernel.h |  4 ++-
 8 files changed, 76 insertions(+), 46 deletions(-)
 create mode 100644 include/grub/i386/pc/int_types.h

diff --git a/grub-core/kern/i386/tsc.c b/grub-core/kern/i386/tsc.c
index f266eb131..9293b161d 100644
--- a/grub-core/kern/i386/tsc.c
+++ b/grub-core/kern/i386/tsc.c
@@ -65,7 +65,7 @@ grub_tsc_init (void)
 
   tsc_boot_time = grub_get_tsc ();
 
-#ifdef GRUB_MACHINE_XEN
+#if defined (GRUB_MACHINE_XEN) || defined (GRUB_MACHINE_XEN_PVH)
   (void) (grub_tsc_calibrate_from_xen () || calibrate_tsc_hardcode());
 #elif defined (GRUB_MACHINE_EFI)
   (void) (grub_tsc_calibrate_from_pmtimer () || grub_tsc_calibrate_from_pit () 
|| grub_tsc_calibrate_from_efi() || calibrate_tsc_hardcode());
diff --git a/grub-core/mmap/i386/pc/mmap.c b/grub-core/mmap/i386/pc/mmap.c
index 609994516..bcb097c38 100644
--- a/grub-core/mmap/i386/pc/mmap.c
+++ b/grub-core/mmap/i386/pc/mmap.c
@@ -42,13 +42,6 @@ extern grub_uint16_t grub_machine_mmaphook_kblow;
 extern grub_uint16_t grub_machine_mmaphook_kbin16mb;
 extern grub_uint16_t grub_machine_mmaphook_64kbin4gb;
 
-struct grub_e820_mmap_entry
-{
-  grub_uint64_t addr;
-  grub_uint64_t len;
-  grub_uint32_t type;
-} GRUB_PACKED;
-
 
 /* Helper for preboot.  */
 static int fill_hook (grub_uint64_t addr, grub_uint64_t size,
diff --git a/include/grub/i386/memory.h b/include/grub/i386/memory.h
index 8bb6e1cbb..5cb607fb4 100644
--- a/include/grub/i386/memory.h
+++ b/include/grub/i386/memory.h
@@ -44,6 +44,13 @@
 
 #include 
 
+struct grub_e820_mmap_entry
+{
+  grub_uint64_t addr;
+  grub_uint64_t len;
+  grub_uint32_t type;
+} GRUB_PACKED;
+
 grub_uint64_t grub_mmap_get_upper (void);
 grub_uint64_t grub_mmap_get_lower (void);
 grub_uint64_t grub_mmap_get_post64 (void);
diff --git a/include/grub/i386/pc/int.h b/include/grub/i386/pc/int.h
index 16a53e4fe..a60104001 100644
--- a/include/grub/i386/pc/int.h
+++ b/include/grub/i386/pc/int.h
@@ -20,45 +20,11 @@
 #define GRUB_INTERRUPT_MACHINE_HEADER  1
 
 #include 
-#include 
-
-struct grub_bios_int_registers
-{
-  grub_uint32_t eax;
-  grub_uint16_t es;
-  grub_uint16_t ds;
-  grub_uint16_t flags;
-  grub_uint16_t dummy;
-  grub_uint32_t ebx;
-  grub_uint32_t ecx;
-  grub_uint32_t edi;
-  grub_uint32_t esi;
-  grub_uint32_t edx;
-};
-
-#define  GRUB_CPU_INT_FLAGS_CARRY 0x1
-#define  GRUB_CPU_INT_FLAGS_PARITY0x4
-#define  GRUB_CPU_INT_FLAGS_ADJUST0x10
-#define  GRUB_CPU_INT_FLAGS_ZERO  0x40
-#define  GRUB_CPU_INT_FLAGS_SIGN  0x80
-#define  GRUB_CPU_INT_FLAGS_TRAP  0x100
-#define  GRUB_CPU_INT_FLAGS_INTERRUPT 0x200
-#define  GRUB_CPU_INT_FLAGS_DIRECTION 0x400
-#define  GRUB_CPU_INT_FLAGS_OVERFLOW  0x800
-#ifdef GRUB_MACHINE_PCBIOS
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   GRUB_CPU_INT_FLAGS_INTERRUPT
-#else
-#define  GRUB_CPU_INT_FLAGS_DEFAULT   0
-#endif
+#include 
 
 void EXPORT_FUNC (grub_bios_interrupt) (grub_uint8_t intno,
struct grub_bios_int_registers *regs)
  __attribute__ ((regparm(3)));
-struct grub_i386_idt
-{
-  grub_uint16_t limit;
-  grub_uint32_t base;
-} GRUB_PACKED;
 
 #ifdef GRUB_MACHINE_PCBIOS
 extern struct grub_i386_idt *EXPORT_VAR(grub_realidt);
diff --git a/include/grub/i386/pc/int_types.h b/include/grub/i386/pc/int_types.h
new file mode 100644
index 0..35a4b5087
--- /dev/null
+++ b/include/grub/i386/pc/int_types.h
@@ -0,0 +1,59 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2018  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Pub

[PATCH v4 07/19] xen: add PVH specific defines to offset.h

2018-11-02 Thread Juergen Gross
include/grub/offsets.h needs some defines for Xen PVH mode.

Add them. While at it line up the values in the surrounding lines to
start at the same column.

Signed-off-by: Juergen Gross 
---
V3: XENPVH->XEN_PVH (Daniel Kiper)
---
 include/grub/offsets.h | 21 -
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/include/grub/offsets.h b/include/grub/offsets.h
index 330e4c707..871e1cd4c 100644
--- a/include/grub/offsets.h
+++ b/include/grub/offsets.h
@@ -36,9 +36,10 @@
 #define GRUB_DECOMPRESSOR_I386_PC_MAX_DECOMPRESSOR_SIZE (0x9000-0x8200)
 
 /* The segment where the kernel is loaded.  */
-#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
+#define GRUB_BOOT_I386_PC_KERNEL_SEG   0x800
 
-#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_PC_LINK_ADDR  0x9000
+#define GRUB_KERNEL_I386_XEN_PVH_LINK_ADDR 0x10
 
 /* The upper memory area (starting at 640 kiB).  */
 #define GRUB_MEMORY_I386_PC_UPPER  0xa
@@ -101,15 +102,17 @@
 #define GRUB_KERNEL_I386_MULTIBOOT_MOD_ALIGN   
GRUB_KERNEL_I386_COREBOOT_MOD_ALIGN
 
 #define GRUB_KERNEL_X86_64_XEN_MOD_ALIGN   0x8
-#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_MOD_ALIGN 0x8
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_ALIGN 0x8
 
 /* Non-zero value is only needed for PowerMacs.  */
-#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_XEN_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP 0x0
-#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP 0x0
-#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP 0x0
+#define GRUB_KERNEL_X86_64_XEN_MOD_GAP 0x0
+#define GRUB_KERNEL_I386_XEN_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_XEN_PVH_MOD_GAP   0x0
+#define GRUB_KERNEL_I386_IEEE1275_MOD_GAP  0x0
+#define GRUB_KERNEL_I386_COREBOOT_MOD_GAP  0x0
+#define GRUB_KERNEL_SPARC64_IEEE1275_MOD_GAP   0x0
+#define GRUB_KERNEL_ARM_UBOOT_MOD_GAP  0x0
 
 #define GRUB_KERNEL_POWERPC_IEEE1275_MOD_ALIGN 0x1000
 #define GRUB_KERNEL_SPARC64_IEEE1275_LOG_MOD_ALIGN 3
-- 
2.16.4


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


[PATCH v4 06/19] xen: rearrange xen/init.c to prepare it for Xen PVH mode

2018-11-02 Thread Juergen Gross
Rearrange grub-core/kern/xen/init.c to prepare adding PVH mode support
to it. This includes putting some code under #ifdef GRUB_MACHINE_XEN
as it will not be used when running as PVH.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 grub-core/kern/xen/init.c | 60 +++
 1 file changed, 34 insertions(+), 26 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 29f5bc23d..10007b411 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -41,9 +41,11 @@ grub_size_t grub_xen_n_allocated_shared_pages;
 static grub_xen_mfn_t
 grub_xen_ptr2mfn (void *ptr)
 {
+#ifdef GRUB_MACHINE_XEN
   grub_xen_mfn_t *mfn_list =
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   return mfn_list[(grub_addr_t) ptr >> GRUB_XEN_LOG_PAGE_SIZE];
+#endif
 }
 
 void *
@@ -104,18 +106,6 @@ grub_machine_get_bootlocation (char **device __attribute__ 
((unused)),
 {
 }
 
-static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
-  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
-
-#ifdef __x86_64__
-#define NUMBER_OF_LEVELS 4
-#else
-#define NUMBER_OF_LEVELS 3
-#endif
-
-#define LOG_POINTERS_PER_PAGE 9
-#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
-
 void
 grub_xen_store_send (const void *buf_, grub_size_t len)
 {
@@ -337,6 +327,19 @@ grub_xen_setup_gnttab (void)
   grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
 }
 
+#ifdef GRUB_MACHINE_XEN
+static grub_uint8_t window[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+#ifdef __x86_64__
+#define NUMBER_OF_LEVELS 4
+#else
+#define NUMBER_OF_LEVELS 3
+#endif
+
+#define LOG_POINTERS_PER_PAGE 9
+#define POINTERS_PER_PAGE (1 << LOG_POINTERS_PER_PAGE)
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -529,13 +532,30 @@ map_all_pages (void)
   grub_mm_init_region ((void *) heap_start, heap_end - heap_start);
 }
 
+grub_err_t
+grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
+{
+  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
+  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
+  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
+return GRUB_ERR_NONE;
+
+  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
+   GRUB_MEMORY_RESERVED, hook_data);
+
+  return GRUB_ERR_NONE;
+}
+#endif
+
 extern char _end[];
 
 void
 grub_machine_init (void)
 {
+#ifdef GRUB_MACHINE_XEN
 #ifdef __i386__
   grub_xen_vm_assist (VMASST_CMD_enable, VMASST_TYPE_pae_extended_cr3);
+#endif
 #endif
 
   grub_modbase = ALIGN_UP ((grub_addr_t) _end
@@ -544,7 +564,9 @@ grub_machine_init (void)
 
   grub_xen_setup_gnttab ();
 
+#ifdef GRUB_MACHINE_XEN
   map_all_pages ();
+#endif
 
   grub_console_init ();
 
@@ -571,17 +593,3 @@ grub_machine_fini (int flags __attribute__ ((unused)))
   grub_xendisk_fini ();
   grub_boot_fini ();
 }
-
-grub_err_t
-grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
-{
-  grub_uint64_t total_pages = grub_xen_start_page_addr->nr_pages;
-  grub_uint64_t usable_pages = grub_xen_start_page_addr->pt_base >> 12;
-  if (hook (0, page2offset (usable_pages), GRUB_MEMORY_AVAILABLE, hook_data))
-return GRUB_ERR_NONE;
-
-  hook (page2offset (usable_pages), page2offset (total_pages - usable_pages),
-   GRUB_MEMORY_RESERVED, hook_data);
-
-  return GRUB_ERR_NONE;
-}
-- 
2.16.4


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


[PATCH v4 05/19] xen: add some dummy headers for PVH mode

2018-11-02 Thread Juergen Gross
With Xen PVH mode adding a new machine type the machine related headers
need to be present for the build to succeed. Most of the headers just
need to include the related common i386 headers. Add those to the tree.

Signed-off-by: Juergen Gross 
---
V3: updated commit message (Daniel Kiper)
xenpvh->xen_pvh (Daniel Kiper)
---
 include/grub/i386/xen_pvh/boot.h| 1 +
 include/grub/i386/xen_pvh/console.h | 1 +
 include/grub/i386/xen_pvh/int.h | 1 +
 include/grub/i386/xen_pvh/memory.h  | 1 +
 include/grub/i386/xen_pvh/time.h| 1 +
 5 files changed, 5 insertions(+)
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/memory.h
 create mode 100644 include/grub/i386/xen_pvh/time.h

diff --git a/include/grub/i386/xen_pvh/boot.h b/include/grub/i386/xen_pvh/boot.h
new file mode 100644
index 0..6cd23aa83
--- /dev/null
+++ b/include/grub/i386/xen_pvh/boot.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/console.h 
b/include/grub/i386/xen_pvh/console.h
new file mode 100644
index 0..305a46d8e
--- /dev/null
+++ b/include/grub/i386/xen_pvh/console.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/int.h b/include/grub/i386/xen_pvh/int.h
new file mode 100644
index 0..0f1f9ee62
--- /dev/null
+++ b/include/grub/i386/xen_pvh/int.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/memory.h 
b/include/grub/i386/xen_pvh/memory.h
new file mode 100644
index 0..8dd6f7c8c
--- /dev/null
+++ b/include/grub/i386/xen_pvh/memory.h
@@ -0,0 +1 @@
+#include 
diff --git a/include/grub/i386/xen_pvh/time.h b/include/grub/i386/xen_pvh/time.h
new file mode 100644
index 0..2298ee8f4
--- /dev/null
+++ b/include/grub/i386/xen_pvh/time.h
@@ -0,0 +1 @@
+#include 
-- 
2.16.4


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


[PATCH v4 13/19] xen: setup Xen specific data for PVH

2018-11-02 Thread Juergen Gross
Initialize the needed Xen specific data. This is:

- the Xen start of day page containing the console and Xenstore ring
  page PFN and event channel
- the grant table
- the shared info page

Write back the possibly modified memory map to the hypervisor in case
the guest is reading it from there again.

Set the RSDP address for the guest from the start_info page passed
as boot parameter.

Signed-off-by: Juergen Gross 
---
V4: write back memory map to Xen (Roger Pau Monné)
---
 grub-core/kern/i386/xen/pvh.c | 119 ++
 1 file changed, 119 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 442351d1d..d74301f92 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -27,6 +27,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #define XEN_CONSOLE_PORT  0xe9
@@ -39,6 +40,7 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct start_info grub_xen_start_page;
 static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
 static unsigned int nr_map_entries;
 
@@ -100,6 +102,36 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
   return __res;
 }
 
+static grub_uint32_t
+grub_xen_get_param (int idx)
+{
+  struct xen_hvm_param xhv;
+  int r;
+
+  xhv.domid = DOMID_SELF;
+  xhv.index = idx;
+  r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+  if (r < 0)
+grub_xen_panic ("Could not get parameter from Xen.\n");
+  return xhv.value;
+}
+
+static void *
+grub_xen_add_physmap (unsigned int space, void *addr)
+{
+  struct xen_add_to_physmap xatp;
+
+  xatp.domid = DOMID_SELF;
+  xatp.idx = 0;
+  xatp.space = space;
+  xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Memory_op hypercall failed.\n");
+  return addr;
+}
+
 static void
 grub_xen_sort_mmap (void)
 {
@@ -255,12 +287,99 @@ grub_xen_get_mmap (void)
   grub_xen_sort_mmap ();
 }
 
+static void
+grub_xen_set_mmap (void)
+{
+  struct xen_foreign_memory_map memmap;
+
+  memmap.domid = DOMID_SELF;
+  memmap.map.nr_entries = nr_map_entries;
+  set_xen_guest_handle (memmap.map.buffer, map);
+  grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_set_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0);
+}
+
+static grub_uint64_t
+grub_xen_find_page (grub_uint64_t start)
+{
+  unsigned int i, j;
+  grub_uint64_t last = start;
+
+  /* Try to find a e820 map hole below 4G. */
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (last > map[i].addr + map[i].len)
+   continue;
+  if (last < map[i].addr)
+   return last;
+  if ((map[i].addr >> 32) || ((map[i].addr + map[i].len) >> 32))
+   break;
+  last = map[i].addr + map[i].len;
+}
+if (i == nr_map_entries)
+  return last;
+
+  /* No hole found, use the highest RAM page below 4G and reserve it. */
+  if (nr_map_entries == ARRAY_SIZE (map))
+grub_xen_panic ("Memory map size limit reached.\n");
+  for (i = 0, j = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+   continue;
+  if (map[i].addr >> 32)
+   break;
+  j = i;
+  if ((map[i].addr + map[i].len) >> 32)
+   break;
+}
+  if (map[j].type != GRUB_MEMORY_AVAILABLE)
+grub_xen_panic ("No free memory page found.\n");
+  if ((map[j].addr + map[j].len) >> 32)
+last = (1ULL << 32) - GRUB_XEN_PAGE_SIZE;
+  else
+last = map[j].addr + map[j].len - GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].addr = last;
+  map[nr_map_entries].len = GRUB_XEN_PAGE_SIZE;
+  map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
+  nr_map_entries++;
+  grub_xen_sort_mmap ();
+
+  return last;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_addr_t par;
+
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
   grub_xen_get_mmap ();
+
+  /* Setup Xen data. */
+  grub_xen_start_page_addr = _xen_start_page;
+
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_PFN);
+  grub_xen_start_page_addr->console.domU.mfn = par;
+  grub_xen_xcons = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_CONSOLE_EVTCHN);
+  grub_xen_start_page_addr->console.domU.evtchn = par;
+
+  par = grub_xen_get_param (HVM_PARAM_STORE_PFN);
+  grub_xen_start_page_addr->store_mfn = par;
+  grub_xen_xenstore = (void *) (grub_addr_t) (par << GRUB_XEN_LOG_PAGE_SIZE);
+  par = grub_xen_get_param (HVM_PARAM_STORE_EVTCHN);
+  grub_xen_start_page_addr->store_evtchn = par;
+
+  par = grub_xen_find_page (0);
+  grub_xen_grant_table = grub_xen_add_physmap (XENMAPSPACE_grant_t

[PATCH v4 16/19] grub-module-verifier: Ignore all_video for xenpvh

2018-11-02 Thread Juergen Gross
From: Hans van Kranenburg 

This solves the build failing with "Error: no symbol table and no
.moddeps section"

Also see:
- 6371e9c10433578bb236a8284ddb9ce9e201eb59
- https://savannah.gnu.org/bugs/?49012

Signed-off-by: Hans van Kranenburg 
Reviewed-by: Daniel Kiper 
---
V2: new patch
Signed-off-by: Juergen Gross 
---
 util/grub-module-verifier.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/util/grub-module-verifier.c b/util/grub-module-verifier.c
index 03ba1ab43..97cb9 100644
--- a/util/grub-module-verifier.c
+++ b/util/grub-module-verifier.c
@@ -129,6 +129,7 @@ struct platform_whitelist {
 
 static struct platform_whitelist whitelists[] = {
   {"i386", "xen", (const char *[]) {"all_video", 0}},
+  {"i386", "xen_pvh", (const char *[]) {"all_video", 0}},
   {"x86_64", "xen", (const char *[]) {"all_video", 0}},
   {"sparc64", "ieee1275", (const char *[]) {"all_video", 0}},
 
-- 
2.16.4


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


[PATCH v4 10/19] xen: setup hypercall page for PVH

2018-11-02 Thread Juergen Gross
Add the needed code to setup the hypercall page for calling into the
Xen hypervisor.

Signed-off-by: Juergen Gross 
---
V3: grub_xen_early_halt->grub_xen_panic (Roger Pau Monné)
issue panic message (Roger Pau Monné)
rewrite grub_xen_hypercall to avoid register variables (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 69 +++
 1 file changed, 69 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index ac6181f4e..7e90a4538 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -20,14 +20,83 @@
 #include 
 #include 
 #include 
+#include 
+#include 
 #include 
 #include 
 
+#define XEN_CONSOLE_PORT   0xe9
+
 grub_uint64_t grub_rsdp_addr;
 
+static char hypercall_page[GRUB_XEN_PAGE_SIZE]
+  __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
+
+static grub_uint32_t xen_cpuid_base;
+
+static void
+grub_xen_panic (const char *msg)
+{
+  const char *c;
+
+  for (c = msg; *c; c++)
+grub_outb (*c, XEN_CONSOLE_PORT);
+
+  asm volatile ("hlt");
+}
+
+static void
+grub_xen_cpuid_base (void)
+{
+  grub_uint32_t base, eax, signature[3];
+
+  for (base = 0x4000; base < 0x4001; base += 0x100)
+{
+  grub_cpuid (base, eax, signature[0], signature[1], signature[2]);
+  if (!grub_memcmp ("XenVMMXenVMM", signature, 12) && (eax - base) >= 2)
+   {
+ xen_cpuid_base = base;
+ return;
+   }
+}
+
+  grub_xen_panic ("Found no Xen signature.\n");
+}
+
+static void
+grub_xen_setup_hypercall_page (void)
+{
+  grub_uint32_t msr, pfn, eax, ebx, ecx, edx;
+
+  grub_cpuid (xen_cpuid_base + 2, eax, ebx, ecx, edx);
+  msr = ebx;
+  pfn = (grub_uint32_t) (_page[0]);
+
+  asm volatile ("wrmsr" : : "c" (msr), "a" (pfn), "d" (0) : "memory");
+}
+
+int
+grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
+   grub_uint32_t a1, grub_uint32_t a2,
+   grub_uint32_t a3, grub_uint32_t a4,
+   grub_uint32_t a5 __attribute__ ((unused)))
+{
+  grub_uint32_t __res, __ign0, __ign1, __ign2, __ign3, __ign4;
+
+  asm volatile ("call *%[callno]"
+   : "=a" (__res), "=b" (__ign0), "=c" (__ign1), "=d" (__ign2),
+ "=S" (__ign3), "=D" (__ign4)
+   : "1" (a0), "2" (a1), "3" (a2), "4" (a3), "5" (a4),
+ [callno] "a" (_page[callno * 32])
+   : "memory");
+  return __res;
+}
+
 void
 grub_xen_setup_pvh (void)
 {
+  grub_xen_cpuid_base ();
+  grub_xen_setup_hypercall_page ();
 }
 
 grub_err_t
-- 
2.16.4


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


[PATCH v4 00/19] xen: add pvh guest support

2018-11-02 Thread Juergen Gross
This patch series adds support for booting Linux as PVH guest.

Similar to i386/xen and x86_64/xen platforms the new i386/xenpvh
platform grub is booted as a standalone image directly by Xen.

For booting Linux kernel it is using the standard linux kernel
loader. The only modification of the linux loader is to pass the
ACPI RSDP address via boot parameters to the kernel, as that table
might not be located at the usual physical address just below 1MB.

The related Linux kernel patches have been accepted in the merge
window for post 4.19 (4.20 or 5.0, whatever it will be called).

Changes in V4:
- added new patch 12
- write modified memory map back to Xen

Changes in V3:
- addressed lots of comments, most by Daniel and some by Roger,
  details are mentioned in the individual patches

Changes in V2:
- extend linux boot protocol version 2.14 to report back the protocol
  version supported by grub2 (needed as there are grub2 downstream
  variants in some distros which clobber data after the setup_header)
- split former patch 4 into 10 patches
- fix memmap handling for >4GB of memory
- added new patch from Hans (thanks for that one)
- addressed most of Daniel's comments (see individual patches)

Hans van Kranenburg (1):
  grub-module-verifier: Ignore all_video for xenpvh

Juergen Gross (18):
  xen: add some xen headers
  loader/linux: support passing rsdp address via boot params
  xen: carve out grant tab initialization into dedicated function
  xen: prepare common code for Xen PVH support
  xen: add some dummy headers for PVH mode
  xen: rearrange xen/init.c to prepare it for Xen PVH mode
  xen: add PVH specific defines to offset.h
  xen: add basic hooks for PVH in current code
  xen: add PVH boot entry code
  xen: setup hypercall page for PVH
  xen: get memory map from hypervisor for PVH
  xen: add PCI MMIO areas to memory map
  xen: setup Xen specific data for PVH
  xen: init memory regions for PVH
  xen_pvh: add build runes for grub-core
  xen_pvh: support building a standalone image
  xen_pvh: support grub-install for xen_pvh
  xen_pvh: add support to configure

 configure.ac  |   3 +
 gentpl.py |   4 +-
 grub-core/Makefile.am |  12 +
 grub-core/Makefile.core.def   |  36 ++
 grub-core/kern/i386/tsc.c |   2 +-
 grub-core/kern/i386/xen/pvh.c | 423 +
 grub-core/kern/i386/xen/startup_pvh.S |  81 +
 grub-core/kern/xen/init.c | 101 --
 grub-core/loader/i386/linux.c |   9 +
 grub-core/mmap/i386/pc/mmap.c |   7 -
 include/grub/i386/linux.h |   5 +-
 include/grub/i386/memory.h|   7 +
 include/grub/i386/pc/int.h|  36 +-
 include/grub/i386/pc/int_types.h  |  59 +++
 include/grub/i386/tsc.h   |   2 +-
 include/grub/i386/xen/hypercall.h |   5 +-
 include/grub/i386/xen_pvh/boot.h  |   1 +
 include/grub/i386/xen_pvh/console.h   |   1 +
 include/grub/i386/xen_pvh/int.h   |   1 +
 include/grub/i386/xen_pvh/kernel.h|  30 ++
 include/grub/i386/xen_pvh/memory.h|   1 +
 include/grub/i386/xen_pvh/time.h  |   1 +
 include/grub/kernel.h |   4 +-
 include/grub/offsets.h|  21 +-
 include/grub/util/install.h   |   1 +
 include/grub/util/mkimage.h   |   3 +-
 include/grub/xen.h|   6 +
 include/xen/hvm/hvm_op.h  | 296 +++
 include/xen/hvm/params.h  | 284 +++
 include/xen/hvm/start_info.h  |  98 +
 include/xen/memory.h  | 665 ++
 include/xen/physdev.h | 387 
 include/xen/trace.h   | 339 +
 include/xen/xen.h | 104 --
 util/grub-install-common.c|   1 +
 util/grub-install.c   |   7 +
 util/grub-mkimage32.c |   4 +-
 util/grub-mkimage64.c |   4 +-
 util/grub-mkimagexx.c |  58 ++-
 util/grub-module-verifier.c   |   1 +
 util/mkimage.c|  23 +-
 41 files changed, 2990 insertions(+), 143 deletions(-)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/pc/int_types.h
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/kernel.h
 create mode 100644 include/grub/i386/xen_pvh/memory.h
 create mode 100644 include/grub/i386/xen_pvh/time.h
 create mode 100644 include/xen/hvm/hvm_op.h
 create mode 100644 include/xen/hvm/params.h
 create mode 100644 include/xen/hvm/start_info.h
 create mode 100644 include/xen/memory.h
 create mode 100644 include/xen/physdev.h
 create mode 100644 include/xen/trace.h

-- 
2.1

[PATCH v4 14/19] xen: init memory regions for PVH

2018-11-02 Thread Juergen Gross
Add all usable memory regions to grub memory management and add the
needed mmap iterate code, which will be used by grub core (e.g.
grub-core/lib/relocator.c or grub-core/mmap/mmap.c).

As we are running in 32-bit mode don't add memory above 4GB.

Signed-off-by: Juergen Gross 
---
 grub-core/kern/i386/xen/pvh.c | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index d74301f92..67952ea91 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -299,6 +299,30 @@ grub_xen_set_mmap (void)
  (grub_uint32_t) (), 0, 0, 0, 0);
 }
 
+static void
+grub_xen_mm_init_regions (void)
+{
+  grub_uint64_t modend, from, to;
+  unsigned int i;
+
+  modend = grub_modules_get_end ();
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].type != GRUB_MEMORY_AVAILABLE)
+continue;
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (from < modend)
+from = modend;
+  if (from >= to || from >= 0x1ULL)
+continue;
+  if (to > 0x1ULL)
+to = 0x1ULL;
+  grub_mm_init_region ((void *) (grub_addr_t) from, to - from);
+}
+}
+
 static grub_uint64_t
 grub_xen_find_page (grub_uint64_t start)
 {
@@ -379,10 +403,21 @@ grub_xen_setup_pvh (void)
   (void *) par);
   grub_xen_set_mmap ();
 
+  grub_xen_mm_init_regions ();
+
   grub_rsdp_addr = pvh_start_info->rsdp_paddr;
 }
 
 grub_err_t
 grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
 {
+  unsigned int i;
+
+  for (i = 0; i < nr_map_entries; i++)
+{
+  if (map[i].len && hook (map[i].addr, map[i].len, map[i].type, hook_data))
+break;
+}
+
+  return GRUB_ERR_NONE;
 }
-- 
2.16.4


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


[PATCH v4 18/19] xen_pvh: support grub-install for xen_pvh

2018-11-02 Thread Juergen Gross
Add xen_pvh support to grub-install.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 include/grub/util/install.h | 1 +
 util/grub-install-common.c  | 1 +
 util/grub-install.c | 7 +++
 3 files changed, 9 insertions(+)

diff --git a/include/grub/util/install.h b/include/grub/util/install.h
index 0dba8b67f..af2bf65d7 100644
--- a/include/grub/util/install.h
+++ b/include/grub/util/install.h
@@ -100,6 +100,7 @@ enum grub_install_plat
 GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS,
 GRUB_INSTALL_PLATFORM_I386_XEN,
 GRUB_INSTALL_PLATFORM_X86_64_XEN,
+GRUB_INSTALL_PLATFORM_I386_XEN_PVH,
 GRUB_INSTALL_PLATFORM_ARM64_EFI,
 GRUB_INSTALL_PLATFORM_ARM_COREBOOT,
 GRUB_INSTALL_PLATFORM_MAX
diff --git a/util/grub-install-common.c b/util/grub-install-common.c
index 0a2e24a79..1b1cb43b4 100644
--- a/util/grub-install-common.c
+++ b/util/grub-install-common.c
@@ -716,6 +716,7 @@ static struct
 [GRUB_INSTALL_PLATFORM_X86_64_EFI] =   { "x86_64",  "efi"   },
 [GRUB_INSTALL_PLATFORM_I386_XEN] = { "i386","xen"   },
 [GRUB_INSTALL_PLATFORM_X86_64_XEN] =   { "x86_64",  "xen"   },
+[GRUB_INSTALL_PLATFORM_I386_XEN_PVH] = { "i386","xen_pvh"   },
 [GRUB_INSTALL_PLATFORM_MIPSEL_LOONGSON] =  { "mipsel",  "loongson"  },
 [GRUB_INSTALL_PLATFORM_MIPSEL_QEMU_MIPS] = { "mipsel",  "qemu_mips" },
 [GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS] =   { "mips","qemu_mips" },
diff --git a/util/grub-install.c b/util/grub-install.c
index 4375c1619..743296f36 100644
--- a/util/grub-install.c
+++ b/util/grub-install.c
@@ -496,6 +496,7 @@ have_bootdev (enum grub_install_plat pl)
 
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   return 0;
 
   /* pacify warning.  */
@@ -913,6 +914,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
 
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
@@ -960,6 +962,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_MIPS_QEMU_MIPS:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   free (install_device);
   install_device = NULL;
   break;
@@ -1477,6 +1480,7 @@ main (int argc, char *argv[])
  case GRUB_INSTALL_PLATFORM_ARM_UBOOT:
  case GRUB_INSTALL_PLATFORM_I386_XEN:
  case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+ case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
grub_util_warn ("%s", _("no hints available for your 
platform. Expect reduced performance"));
break;
/* pacify warning.  */
@@ -1568,6 +1572,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_POWERPC_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   core_name = "core.elf";
   snprintf (mkimage_target, sizeof (mkimage_target),
"%s-%s",
@@ -1660,6 +1665,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_SPARC64_IEEE1275:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   break;
   /* pacify warning.  */
 case GRUB_INSTALL_PLATFORM_MAX:
@@ -1926,6 +1932,7 @@ main (int argc, char *argv[])
 case GRUB_INSTALL_PLATFORM_I386_QEMU:
 case GRUB_INSTALL_PLATFORM_I386_XEN:
 case GRUB_INSTALL_PLATFORM_X86_64_XEN:
+case GRUB_INSTALL_PLATFORM_I386_XEN_PVH:
   grub_util_warn ("%s",
  _("WARNING: no platform-specific install was performed"));
   break;
-- 
2.16.4


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


[PATCH v4 01/19] xen: add some xen headers

2018-11-02 Thread Juergen Gross
In order to support grub2 in Xen PVH environment some additional Xen
headers are needed as grub2 will be started in PVH mode requiring to
use several HVM hypercalls and structures.

Add the needed headers from Xen 4.10 being the first Xen version with
full (not only experimental) PVH guest support.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 include/xen/hvm/hvm_op.h | 296 +++
 include/xen/hvm/params.h | 284 ++
 include/xen/hvm/start_info.h |  98 +++
 include/xen/memory.h | 665 +++
 include/xen/physdev.h| 387 +
 include/xen/trace.h  | 339 ++
 include/xen/xen.h| 104 +--
 7 files changed, 2142 insertions(+), 31 deletions(-)
 create mode 100644 include/xen/hvm/hvm_op.h
 create mode 100644 include/xen/hvm/params.h
 create mode 100644 include/xen/hvm/start_info.h
 create mode 100644 include/xen/memory.h
 create mode 100644 include/xen/physdev.h
 create mode 100644 include/xen/trace.h

diff --git a/include/xen/hvm/hvm_op.h b/include/xen/hvm/hvm_op.h
new file mode 100644
index 0..0bdafdf59
--- /dev/null
+++ b/include/xen/hvm/hvm_op.h
@@ -0,0 +1,296 @@
+/*
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ * Copyright (c) 2007, Keir Fraser
+ */
+
+#ifndef __XEN_PUBLIC_HVM_HVM_OP_H__
+#define __XEN_PUBLIC_HVM_HVM_OP_H__
+
+#include "../xen.h"
+#include "../trace.h"
+#include "../event_channel.h"
+
+/* Get/set subcommands: extra argument == pointer to xen_hvm_param struct. */
+#define HVMOP_set_param   0
+#define HVMOP_get_param   1
+struct xen_hvm_param {
+domid_t  domid;/* IN */
+uint32_t index;/* IN */
+uint64_t value;/* IN/OUT */
+};
+typedef struct xen_hvm_param xen_hvm_param_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_param_t);
+
+#if __XEN_INTERFACE_VERSION__ < 0x00040900
+
+/* Set the logical level of one of a domain's PCI INTx wires. */
+#define HVMOP_set_pci_intx_level  2
+struct xen_hvm_set_pci_intx_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI INTx identification in PCI topology (domain:bus:device:intx). */
+uint8_t  domain, bus, device, intx;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_pci_intx_level xen_hvm_set_pci_intx_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_intx_level_t);
+
+/* Set the logical level of one of a domain's ISA IRQ wires. */
+#define HVMOP_set_isa_irq_level   3
+struct xen_hvm_set_isa_irq_level {
+/* Domain to be updated. */
+domid_t  domid;
+/* ISA device identification, by ISA IRQ (0-15). */
+uint8_t  isa_irq;
+/* Assertion level (0 = unasserted, 1 = asserted). */
+uint8_t  level;
+};
+typedef struct xen_hvm_set_isa_irq_level xen_hvm_set_isa_irq_level_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_isa_irq_level_t);
+
+#define HVMOP_set_pci_link_route  4
+struct xen_hvm_set_pci_link_route {
+/* Domain to be updated. */
+domid_t  domid;
+/* PCI link identifier (0-3). */
+uint8_t  link;
+/* ISA IRQ (1-15), or 0 (disable link). */
+uint8_t  isa_irq;
+};
+typedef struct xen_hvm_set_pci_link_route xen_hvm_set_pci_link_route_t;
+DEFINE_XEN_GUEST_HANDLE(xen_hvm_set_pci_link_route_t);
+
+#endif /* __XEN_INTERFACE_VERSION__ < 0x00040900 */
+
+/* Flushes all VCPU TLBs: @arg must be NULL. */
+#define HVMOP_flush_tlbs  5
+
+typedef enum {
+HVMMEM_ram_rw, /* Normal read/write guest RAM */
+HVMMEM_ram_ro, /* Read-only; writes are discarded */
+HVMMEM_mmio_dm,/* Reads and write go to the device model */
+#if __XEN_INTERFACE_VERSION__ < 0x00040700
+HVMMEM_mmio_write_dm,  /* Read-only; writes go to the device model */
+#else
+HVMMEM_unus

[PATCH v4 15/19] xen_pvh: add build runes for grub-core

2018-11-02 Thread Juergen Gross
Add the modifications to the build system needed to build a xen_pvh
grub.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V3: sorted some filenames (Daniel Kiper)
V4: add bus/pci.c to xen_pvh
---
 gentpl.py   |  4 ++--
 grub-core/Makefile.am   | 12 
 grub-core/Makefile.core.def | 36 
 3 files changed, 50 insertions(+), 2 deletions(-)

diff --git a/gentpl.py b/gentpl.py
index da67965a4..e8439484a 100644
--- a/gentpl.py
+++ b/gentpl.py
@@ -28,7 +28,7 @@ import re
 
 GRUB_PLATFORMS = [ "emu", "i386_pc", "i386_efi", "i386_qemu", "i386_coreboot",
"i386_multiboot", "i386_ieee1275", "x86_64_efi",
-   "i386_xen", "x86_64_xen",
+   "i386_xen", "x86_64_xen", "i386_xen_pvh",
"mips_loongson", "sparc64_ieee1275",
"powerpc_ieee1275", "mips_arc", "ia64_efi",
"mips_qemu_mips", "arm_uboot", "arm_efi", "arm64_efi",
@@ -71,7 +71,7 @@ GROUPS["videomodules"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["videoinkernel"]: GROUPS["videomodules"].remove(i)
 
 # Similar for terminfo
-GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips" ] + GROUPS["xen"] + GROUPS["ieee1275"] + GROUPS["uboot"];
+GROUPS["terminfoinkernel"] = [ "emu", "mips_loongson", "mips_arc", 
"mips_qemu_mips", "i386_xen_pvh" ] + GROUPS["xen"] + GROUPS["ieee1275"] + 
GROUPS["uboot"];
 GROUPS["terminfomodule"]   = GRUB_PLATFORMS[:];
 for i in GROUPS["terminfoinkernel"]: GROUPS["terminfomodule"].remove(i)
 
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
index f4ff62b76..02cb5e33d 100644
--- a/grub-core/Makefile.am
+++ b/grub-core/Makefile.am
@@ -101,6 +101,18 @@ KERNEL_HEADER_FILES += 
$(top_builddir)/include/grub/machine/int.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
 endif
 
+if COND_i386_xen_pvh
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/kernel.h
+KERNEL_HEADER_FILES += $(top_builddir)/include/grub/machine/int.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/tsc.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/terminfo.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/loader.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/lib/arg.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/xen.h
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/i386/xen/hypercall.h
+endif
+
 if COND_i386_efi
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/efi.h
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/efi/disk.h
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 9590e87d9..6f5936a10 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -79,6 +79,8 @@ kernel = {
   i386_xen_ldflags = '$(TARGET_IMG_BASE_LDOPT),0';
   x86_64_xen_ldflags   = '$(TARGET_IMG_LDFLAGS)';
   x86_64_xen_ldflags   = '$(TARGET_IMG_BASE_LDOPT),0';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_LDFLAGS)';
+  i386_xen_pvh_ldflags = '$(TARGET_IMG_BASE_LDOPT),0x10';
 
   mips_loongson_ldflags= '-Wl,-Ttext,0x8020';
   powerpc_ieee1275_ldflags = '-Wl,-Ttext,0x20';
@@ -100,6 +102,7 @@ kernel = {
   x86_64_efi_startup = kern/x86_64/efi/startup.S;
   i386_xen_startup = kern/i386/xen/startup.S;
   x86_64_xen_startup = kern/x86_64/xen/startup.S;
+  i386_xen_pvh_startup = kern/i386/xen/startup_pvh.S;
   i386_qemu_startup = kern/i386/qemu/startup.S;
   i386_ieee1275_startup = kern/i386/ieee1275/startup.S;
   i386_coreboot_startup = kern/i386/coreboot/startup.S;
@@ -177,6 +180,7 @@ kernel = {
 
   i386 = kern/i386/dl.c;
   i386_xen = kern/i386/dl.c;
+  i386_xen_pvh = kern/i386/dl.c;
 
   i386_coreboot = kern/i386/coreboot/init.c;
   i386_multiboot = kern/i386/coreboot/init.c;
@@ -222,6 +226,15 @@ kernel = {
   xen = disk/xen/xendisk.c;
   xen = commands/boot.c;
 
+  i386_xen_pvh = bus/pci.c;
+  i386_xen_pvh = commands/boot.c;
+  i386_xen_pvh = disk/xen/xendisk.c;
+  i386_xen_pvh = kern/i386/tsc.c;
+  i386_xen_pvh = kern/i386/xen/tsc.c;
+  i386_xen_pvh = kern/i386/xen/pvh.c;
+  i386_xen_pvh = kern/xen/init.c;
+  i386_xen_pvh = term/xen/console.c;
+
   ia64_efi = kern/ia64/efi/startup.S;
   ia64_efi = kern/ia64/efi/init.c;
   ia64_efi = kern/ia64/dl.c;
@@ -801,6 +814,7 @@ module = {
   name = cpuid;
   common = commands/i386/cpuid.c;
   enable = x86;
+  enable = i386_xen_pvh;
   enable = i386_xen;
   enable = x86_64_xen;
 };
@@ -860,6 +8

[PATCH v4 17/19] xen_pvh: support building a standalone image

2018-11-02 Thread Juergen Gross
Support mkimage for xen_pvh.

In order to avoid using plain integers for the ELF notes use the
available Xen include instead. While at it replace the plain numbers
for Xen PV mode, too.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: some style adjustments (Daniel Kiper)
use defines for elf-notes (Daniel Kiper)

I didn't replace the 4096 by a PAGE_SIZE macro as requested by Daniel,
as there isn't such a macro easily available for util/mkimage.c and
I didn't introduce its usage.
---
 include/grub/util/mkimage.h |  3 ++-
 util/grub-mkimage32.c   |  4 +++-
 util/grub-mkimage64.c   |  4 +++-
 util/grub-mkimagexx.c   | 58 +
 util/mkimage.c  | 23 +-
 5 files changed, 78 insertions(+), 14 deletions(-)

diff --git a/include/grub/util/mkimage.h b/include/grub/util/mkimage.h
index b3a5ca132..ba9f568f6 100644
--- a/include/grub/util/mkimage.h
+++ b/include/grub/util/mkimage.h
@@ -71,7 +71,8 @@ struct grub_install_image_target_desc
 IMAGE_I386_IEEE1275,
 IMAGE_LOONGSON_ELF, IMAGE_QEMU, IMAGE_PPC, IMAGE_YEELOONG_FLASH,
 IMAGE_FULOONG2F_FLASH, IMAGE_I386_PC_PXE, IMAGE_MIPS_ARC,
-IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO
+IMAGE_QEMU_MIPS_FLASH, IMAGE_UBOOT, IMAGE_XEN, IMAGE_I386_PC_ELTORITO,
+IMAGE_XEN_PVH
   } id;
   enum
 {
diff --git a/util/grub-mkimage32.c b/util/grub-mkimage32.c
index 1f2d2..026a2dd59 100644
--- a/util/grub-mkimage32.c
+++ b/util/grub-mkimage32.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF32_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF32_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF32_ST_TYPE(val)
-#define XEN_NOTE_SIZE 132
+
+#define XEN_NOTE_SIZE  132
+#define XEN_PVH_NOTE_SIZE  20
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimage64.c b/util/grub-mkimage64.c
index 4ff72a625..170defb40 100644
--- a/util/grub-mkimage64.c
+++ b/util/grub-mkimage64.c
@@ -17,7 +17,9 @@
 # define ELF_R_SYM(val)ELF64_R_SYM(val)
 # define ELF_R_TYPE(val)   ELF64_R_TYPE(val)
 # define ELF_ST_TYPE(val)  ELF64_ST_TYPE(val)
-#define XEN_NOTE_SIZE 120
+
+#define XEN_NOTE_SIZE  120
+#define XEN_PVH_NOTE_SIZE  24
 
 #ifndef GRUB_MKIMAGEXX
 #include "grub-mkimagexx.c"
diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c
index a483c674c..e94a721b4 100644
--- a/util/grub-mkimagexx.c
+++ b/util/grub-mkimagexx.c
@@ -48,6 +48,8 @@
 #include 
 #include 
 
+#include 
+
 #pragma GCC diagnostic ignored "-Wcast-align"
 
 #define GRUB_MKIMAGEXX
@@ -227,12 +229,12 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   phnum++;
   footer_size += sizeof (struct grub_ieee1275_note);
 }
-  if (image_target->id == IMAGE_XEN)
+  if (image_target->id == IMAGE_XEN || image_target->id == IMAGE_XEN_PVH)
 {
   phnum++;
   shnum++;
   string_size += sizeof (".xen");
-  footer_size += XEN_NOTE_SIZE;
+  footer_size += (image_target->id == IMAGE_XEN) ? XEN_NOTE_SIZE : 
XEN_PVH_NOTE_SIZE;
 }
   header_size = ALIGN_UP (sizeof (*ehdr) + phnum * sizeof (*phdr)
  + shnum * sizeof (*shdr) + string_size, 
layout->align);
@@ -341,7 +343,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof (PACKAGE_NAME));
-  note_ptr->n_type = grub_host_to_target32 (6);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_GUEST_OS);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -352,7 +354,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("generic"));
-  note_ptr->n_type = grub_host_to_target32 (8);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_LOADER);
   ptr += sizeof (Elf_Nhdr);
   memcpy (ptr, GRUB_XEN_NOTE_NAME, sizeof (GRUB_XEN_NOTE_NAME));
   ptr += ALIGN_UP (sizeof (GRUB_XEN_NOTE_NAME), 4);
@@ -363,7 +365,7 @@ SUFFIX (grub_mkimage_generate_elf) (const struct 
grub_install_image_target_desc
   note_ptr = (Elf_Nhdr *) ptr;
   note_ptr->n_namesz = grub_host_to_target32 (sizeof (GRUB_XEN_NOTE_NAME));
   note_ptr->n_descsz = grub_host_to_target32 (sizeof ("xen-3.0"));
-  note_ptr->n_type = grub_host_to_target32 (5);
+  note_ptr->n_type = grub_host_to_target32 (XEN_ELFNOTE_XE

[PATCH v4 03/19] xen: carve out grant tab initialization into dedicated function

2018-11-02 Thread Juergen Gross
Initialize the grant tab in a dedicated function. This will enable
using it for PVH guests, too.

Call the new function from grub_machine_init() as this will later
be common between Xen PV and Xen PVH mode.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
V2: update commit message (Daniel Kiper)
---
 grub-core/kern/xen/init.c | 35 +--
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/grub-core/kern/xen/init.c b/grub-core/kern/xen/init.c
index 0559c033c..29f5bc23d 100644
--- a/grub-core/kern/xen/init.c
+++ b/grub-core/kern/xen/init.c
@@ -318,6 +318,25 @@ grub_xenstore_dir (const char *dir,
 
 unsigned long gntframe = 0;
 
+static void
+grub_xen_setup_gnttab (void)
+{
+  struct gnttab_set_version gnttab_setver;
+  struct gnttab_setup_table gnttab_setup;
+
+  grub_memset (_setver, 0, sizeof (gnttab_setver));
+
+  gnttab_setver.version = 1;
+  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
+
+  grub_memset (_setup, 0, sizeof (gnttab_setup));
+  gnttab_setup.dom = DOMID_SELF;
+  gnttab_setup.nr_frames = 1;
+  gnttab_setup.frame_list.p = 
+
+  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
+}
+
 #define MAX_N_UNUSABLE_PAGES 4
 
 static int
@@ -357,26 +376,12 @@ map_all_pages (void)
 (grub_xen_mfn_t *) grub_xen_start_page_addr->mfn_list;
   grub_uint64_t *pg = (grub_uint64_t *) window;
   grub_uint64_t oldpgstart, oldpgend;
-  struct gnttab_setup_table gnttab_setup;
-  struct gnttab_set_version gnttab_setver;
   grub_size_t n_unusable_pages = 0;
   struct mmu_update m2p_updates[2 * MAX_N_UNUSABLE_PAGES];
 
   if (total_pages > MAX_TOTAL_PAGES - 4)
 total_pages = MAX_TOTAL_PAGES - 4;
 
-  grub_memset (_setver, 0, sizeof (gnttab_setver));
-
-  gnttab_setver.version = 1;
-  grub_xen_grant_table_op (GNTTABOP_set_version, _setver, 1);
-
-  grub_memset (_setup, 0, sizeof (gnttab_setup));
-  gnttab_setup.dom = DOMID_SELF;
-  gnttab_setup.nr_frames = 1;
-  gnttab_setup.frame_list.p = 
-
-  grub_xen_grant_table_op (GNTTABOP_setup_table, _setup, 1);
-
   for (j = 0; j < total_pages - n_unusable_pages; j++)
 while (!grub_xen_is_page_usable (mfn_list[j]))
   {
@@ -537,6 +542,8 @@ grub_machine_init (void)
   + GRUB_KERNEL_MACHINE_MOD_GAP,
   GRUB_KERNEL_MACHINE_MOD_ALIGN);
 
+  grub_xen_setup_gnttab ();
+
   map_all_pages ();
 
   grub_console_init ();
-- 
2.16.4


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


[PATCH v4 11/19] xen: get memory map from hypervisor for PVH

2018-11-02 Thread Juergen Gross
Retrieve the memory map from the hypervisor and normalize it to contain
no overlapping entries and to be sorted by address.

Signed-off-by: Juergen Gross 
---
V3: use grub_e820_mmap_entry instead of own struct (Daniel Kiper)
---
 grub-core/kern/i386/xen/pvh.c | 96 ++-
 1 file changed, 95 insertions(+), 1 deletion(-)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 7e90a4538..58e6fefd5 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -23,9 +23,14 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
+#include 
 
-#define XEN_CONSOLE_PORT   0xe9
+#define XEN_CONSOLE_PORT  0xe9
+
+#define XEN_MEMORY_MAP_SIZE   128
 
 grub_uint64_t grub_rsdp_addr;
 
@@ -33,6 +38,8 @@ static char hypercall_page[GRUB_XEN_PAGE_SIZE]
   __attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
 
 static grub_uint32_t xen_cpuid_base;
+static struct grub_e820_mmap_entry map[XEN_MEMORY_MAP_SIZE];
+static unsigned int nr_map_entries;
 
 static void
 grub_xen_panic (const char *msg)
@@ -92,11 +99,98 @@ grub_xen_hypercall (grub_uint32_t callno, grub_uint32_t a0,
   return __res;
 }
 
+static void
+grub_xen_sort_mmap (void)
+{
+  grub_uint64_t from, to;
+  unsigned int i;
+  struct grub_e820_mmap_entry tmp;
+
+  /* Align map entries to page boundaries. */
+  for (i = 0; i < nr_map_entries; i++)
+{
+  from = map[i].addr;
+  to = from + map[i].len;
+  if (map[i].type == GRUB_MEMORY_AVAILABLE)
+   {
+ from = ALIGN_UP (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_DOWN (to, GRUB_XEN_PAGE_SIZE);
+   }
+  else
+   {
+ from = ALIGN_DOWN (from, GRUB_XEN_PAGE_SIZE);
+ to = ALIGN_UP (to, GRUB_XEN_PAGE_SIZE);
+   }
+  map[i].addr = from;
+  map[i].len = to - from;
+}
+
+ again:
+  /* Sort entries by start address. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr)
+   continue;
+  tmp = map[i];
+  map[i] = map[i - 1];
+  map[i - 1] = tmp;
+  i = 0;
+}
+
+  /* Detect overlapping areas. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].addr >= map[i - 1].addr + map[i - 1].len)
+   continue;
+  tmp = map[i - 1];
+  map[i - 1].len = map[i].addr - map[i - 1].addr;
+  if (map[i].addr + map[i].len >= tmp.addr + tmp.len)
+   continue;
+  if (nr_map_entries < ARRAY_SIZE (map))
+   {
+ map[nr_map_entries].addr = map[i].addr + map[i].len;
+ map[nr_map_entries].len = tmp.addr + tmp.len - 
map[nr_map_entries].addr;
+ map[nr_map_entries].type = tmp.type;
+ nr_map_entries++;
+ goto again;
+   }
+}
+
+  /* Merge adjacent entries. */
+  for (i = 1; i < nr_map_entries; i++)
+{
+  if (map[i].type == map[i - 1].type &&
+ map[i].addr == map[i - 1].addr + map[i - 1].len)
+   {
+ map[i - 1].len += map[i].len;
+ map[i] = map[nr_map_entries - 1];
+ nr_map_entries--;
+ goto again;
+   }
+}
+}
+
+static void
+grub_xen_get_mmap (void)
+{
+  struct xen_memory_map memmap;
+
+  memmap.nr_entries = ARRAY_SIZE (map);
+  set_xen_guest_handle (memmap.buffer, map);
+  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_memory_map,
+ (grub_uint32_t) (), 0, 0, 0, 0))
+grub_xen_panic ("Could not get memory map from Xen.\n");
+  nr_map_entries = memmap.nr_entries;
+
+  grub_xen_sort_mmap ();
+}
+
 void
 grub_xen_setup_pvh (void)
 {
   grub_xen_cpuid_base ();
   grub_xen_setup_hypercall_page ();
+  grub_xen_get_mmap ();
 }
 
 grub_err_t
-- 
2.16.4


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


[PATCH v4 12/19] xen: add PCI MMIO areas to memory map

2018-11-02 Thread Juergen Gross
Add possible PCI space MMIO areas as "Reserved" to the memory map in
order to avoid using those areas for special Xen pages later.

Signed-off-by: Juergen Gross 
---
V4: new patch (Roger Pau Monné)
---
 grub-core/kern/i386/xen/pvh.c | 70 +++
 1 file changed, 70 insertions(+)

diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
index 58e6fefd5..442351d1d 100644
--- a/grub-core/kern/i386/xen/pvh.c
+++ b/grub-core/kern/i386/xen/pvh.c
@@ -20,6 +20,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -170,6 +171,73 @@ grub_xen_sort_mmap (void)
 }
 }
 
+static grub_uint64_t
+grub_xen_pci_read (grub_pci_address_t addr, grub_uint32_t is_64bit)
+{
+  grub_uint64_t val;
+
+  val = grub_pci_read (addr);
+  if (is_64bit)
+{
+  addr += sizeof (grub_uint32_t);
+  val |= ((grub_uint64_t) grub_pci_read (addr)) << 32;
+}
+
+  return val;
+}
+
+static void
+grub_xen_pci_write (grub_pci_address_t addr, grub_uint64_t val,
+   grub_uint32_t is_64bit)
+{
+  grub_pci_write (addr, (grub_uint32_t) val);
+  if (is_64bit)
+{
+  addr += sizeof (grub_uint32_t);
+  grub_pci_write (addr, val >> 32);
+}
+}
+
+static int
+grub_xen_pci_mmap (grub_pci_device_t dev,
+  grub_pci_id_t pciid __attribute__ ((unused)),
+  void *data __attribute__ ((unused)))
+{
+  int reg;
+  grub_pci_address_t addr;
+  grub_uint32_t val;
+  grub_uint64_t mmio_addr, mmio_size;
+
+  if (nr_map_entries == ARRAY_SIZE (map))
+return 1;
+
+  for (reg = GRUB_PCI_REG_ADDRESSES; reg < GRUB_PCI_REG_CIS_POINTER;
+   reg += sizeof (grub_uint32_t))
+{
+  addr = grub_pci_make_address (dev, reg);
+  val = grub_pci_read (addr);
+  if (val == 0 ||
+ (val & GRUB_PCI_ADDR_SPACE_MASK) != GRUB_PCI_ADDR_SPACE_MEMORY)
+   continue;
+
+  val &= GRUB_PCI_ADDR_MEM_TYPE_MASK;
+  mmio_addr = grub_xen_pci_read (addr, val);
+  grub_xen_pci_write (addr, ~0ULL, val);
+  mmio_size = ~(grub_xen_pci_read (addr, val) & ~0x0fULL) + 1;
+  grub_xen_pci_write (addr, mmio_addr, val);
+
+  map[nr_map_entries].type = GRUB_MEMORY_RESERVED;
+  map[nr_map_entries].addr = mmio_addr;
+  map[nr_map_entries].len = mmio_size;
+  nr_map_entries++;
+
+  if (val)
+   reg += sizeof (grub_uint32_t);
+}
+
+  return 0;
+}
+
 static void
 grub_xen_get_mmap (void)
 {
@@ -182,6 +250,8 @@ grub_xen_get_mmap (void)
 grub_xen_panic ("Could not get memory map from Xen.\n");
   nr_map_entries = memmap.nr_entries;
 
+  grub_pci_iterate (grub_xen_pci_mmap, NULL);
+
   grub_xen_sort_mmap ();
 }
 
-- 
2.16.4


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


[PATCH v4 19/19] xen_pvh: add support to configure

2018-11-02 Thread Juergen Gross
Support platform i386/xen_pvh in configure.

Signed-off-by: Juergen Gross 
Reviewed-by: Daniel Kiper 
---
 configure.ac | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.ac b/configure.ac
index 5e63c4af3..81a19afd5 100644
--- a/configure.ac
+++ b/configure.ac
@@ -151,6 +151,7 @@ case "$target_cpu"-"$platform" in
   i386-efi) ;;
   x86_64-efi) ;;
   i386-xen) ;;
+  i386-xen_pvh) ;;
   x86_64-xen) ;;
   i386-pc) ;;
   i386-multiboot) ;;
@@ -219,6 +220,7 @@ case "$platform" in
   multiboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_MULTIBOOT=1" 
;;
   efi) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_EFI=1" ;;
   xen) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN=1" ;;
+  xen_pvh) machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_XEN_PVH=1" ;;
   ieee1275)machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_IEEE1275=1" 
;;
   uboot)   machine_CPPFLAGS="$machine_CPPFLAGS -DGRUB_MACHINE_UBOOT=1" ;;
   qemu)machine_CPPFLAGS="$machine_CPPFLAGS 
-DGRUB_MACHINE_QEMU=1" ;;
@@ -1897,6 +1899,7 @@ AM_CONDITIONAL([COND_i386_coreboot], [test x$target_cpu = 
xi386 -a x$platform =
 AM_CONDITIONAL([COND_i386_multiboot], [test x$target_cpu = xi386 -a x$platform 
= xmultiboot])
 AM_CONDITIONAL([COND_x86_64_efi], [test x$target_cpu = xx86_64 -a x$platform = 
xefi])
 AM_CONDITIONAL([COND_i386_xen], [test x$target_cpu = xi386 -a x$platform = 
xxen])
+AM_CONDITIONAL([COND_i386_xen_pvh], [test x$target_cpu = xi386 -a x$platform = 
xxen_pvh])
 AM_CONDITIONAL([COND_x86_64_xen], [test x$target_cpu = xx86_64 -a x$platform = 
xxen])
 AM_CONDITIONAL([COND_mips_loongson], [test x$target_cpu = xmipsel -a 
x$platform = xloongson])
 AM_CONDITIONAL([COND_mips_qemu_mips], [test "(" x$target_cpu = xmips -o 
x$target_cpu = xmipsel ")"  -a x$platform = xqemu_mips])
-- 
2.16.4


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


Re: [Xen-devel] [PATCH v2 12/18] xen: setup Xen specific data for PVH

2018-10-30 Thread Juergen Gross
On 30/10/2018 12:23, Roger Pau Monné wrote:
> On Mon, Oct 29, 2018 at 03:19:34PM +0100, Juergen Gross wrote:
>> On 29/10/2018 13:57, Roger Pau Monné wrote:
>>> On Fri, Oct 19, 2018 at 06:39:50PM +0200, Juergen Gross wrote:
>>>> On 19/10/2018 18:10, Roger Pau Monné wrote:
>>>>> On Tue, Oct 09, 2018 at 01:03:11PM +0200, Juergen Gross wrote:
>>>>>> Initialize the needed Xen specific data. This is:
>>>>>>
>>>>>> - the Xen start of day page containing the console and Xenstore ring
>>>>>>   page PFN and event channel
>>>>>> - the grant table
>>>>>> - the shared info page
>>>>>>
>>>>>> Set the RSDP address for the guest from the start_info page passed
>>>>>> as boot parameter.
>>>>>>
>>>>>> Signed-off-by: Juergen Gross 
>>>>>> ---
>>>>>>  grub-core/kern/i386/xen/pvh.c | 107 
>>>>>> ++
>>>>>>  1 file changed, 107 insertions(+)
>>>>>>
>>>>>> diff --git a/grub-core/kern/i386/xen/pvh.c 
>>>>>> b/grub-core/kern/i386/xen/pvh.c
>>>>>> index b4933b454..93ed68245 100644
>>>>>> --- a/grub-core/kern/i386/xen/pvh.c
>>>>>> +++ b/grub-core/kern/i386/xen/pvh.c
>>>>>> @@ -24,6 +24,7 @@
>>>>>>  #include 
>>>>>>  #include 
>>>>>>  #include 
>>>>>> +#include 
>>>>>>  #include 
>>>>>>  
>>>>>>  struct xen_machine_mmap_entry
>>>>>> @@ -39,6 +40,7 @@ static struct { char _entry[32]; } hypercall_page[128]
>>>>>>__attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
>>>>>>  
>>>>>>  static grub_uint32_t xen_cpuid_base;
>>>>>> +static struct start_info grub_xen_start_page;
>>>>>>  static struct xen_machine_mmap_entry map[128];
>>>>>>  static unsigned int nr_map_entries;
>>>>>>  
>>>>>> @@ -104,6 +106,36 @@ grub_xen_hypercall (grub_uint32_t callno, 
>>>>>> grub_uint32_t a0,
>>>>>>return __res;
>>>>>>  }
>>>>>>  
>>>>>> +static grub_uint32_t
>>>>>> +grub_xen_get_param (int idx)
>>>>>> +{
>>>>>> +  struct xen_hvm_param xhv;
>>>>>> +  int r;
>>>>>> +
>>>>>> +  xhv.domid = DOMID_SELF;
>>>>>> +  xhv.index = idx;
>>>>>> +  r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
>>>>>> +  (grub_uint32_t) (), 0, 0, 0, 0);
>>>>>> +  if (r < 0)
>>>>>> +grub_xen_early_halt ();
>>>>>> +  return xhv.value;
>>>>>> +}
>>>>>> +
>>>>>> +static void *
>>>>>> +grub_xen_add_physmap (unsigned int space, void *addr)
>>>>>> +{
>>>>>> +  struct xen_add_to_physmap xatp;
>>>>>> +
>>>>>> +  xatp.domid = DOMID_SELF;
>>>>>> +  xatp.idx = 0;
>>>>>> +  xatp.space = space;
>>>>>> +  xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
>>>>>> +  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
>>>>>> +  (grub_uint32_t) (), 0, 0, 0, 0))
>>>>>> +grub_xen_early_halt ();
>>>>>> +  return addr;
>>>>>> +}
>>>>>> +
>>>>>>  static void
>>>>>>  grub_xen_sort_mmap (void)
>>>>>>  {
>>>>>> @@ -190,12 +222,87 @@ grub_xen_get_mmap (void)
>>>>>>grub_xen_sort_mmap ();
>>>>>>  }
>>>>>>  
>>>>>> +static grub_uint64_t
>>>>>> +grub_xen_find_page (grub_uint64_t start)
>>>>>> +{
>>>>>> +  unsigned int i, j;
>>>>>> +  grub_uint64_t last = start;
>>>>>> +
>>>>>> +  /* Try to find a e820 map hole below 4G. */
>>>>>
>>>>> Doing this is kind of dangerous, what if you end up placing something
>>>>> on top of an MMIO region (either emulated or from a real passthrough
>>>>> device)?
>>>>
>>>> Shouldn't those be marked as "Reserved" in the memory map?
>>>
>>> I don't think BARs are guaranteed to be in areas marked as reserved in
>>> the memory map. Unless you also scan for PCI devices and make sure
>>> there's no device with a BAR in the area you are attempting to
>>> populate I think the above is not safe.
>>
>> So either I can add scanning the PCI bus (which wouldn't be too
>> hard IMO), or we require Xen tools to add memory map entries with
>> "Reserved" attribute for passed-through PCI device's MMIO-areas
>> (we can still do that as PCI pass-through for PVH isn't possible
>> yet AFAIK).
> 
> Ideally (and that's kind of far away from what we are now), I would
> like to have an interface to request Xen for a range of gfns available
> to map stuff in them (grants/foreign mappings/shared page...). That
> interface could be a simple hypercall that would return such range, or
> an hypercall that could be used to fetch something akin to an
> 'extended memory map' with specific Xen information (like such
> regions).
> 
> In both cases this requires Xen having a clearer picture of the p2m,
> because any of the above solutions cannot rely on scanning the p2m
> table in order to figure out what's where.
> 
> So the only change I would request is that if you use a RAM page you
> update the memory map stored in Xen to match the new layout, by using
> the XENMEM_set_memory_map hypercall.

Okay, this is simple.

Nevertheless I'm adding the already finished patch for scanning the
PCI devices to find MMIO areas.


Juergen

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


Re: [Xen-devel] [PATCH v2 12/18] xen: setup Xen specific data for PVH

2018-10-29 Thread Juergen Gross
On 29/10/2018 13:57, Roger Pau Monné wrote:
> On Fri, Oct 19, 2018 at 06:39:50PM +0200, Juergen Gross wrote:
>> On 19/10/2018 18:10, Roger Pau Monné wrote:
>>> On Tue, Oct 09, 2018 at 01:03:11PM +0200, Juergen Gross wrote:
>>>> Initialize the needed Xen specific data. This is:
>>>>
>>>> - the Xen start of day page containing the console and Xenstore ring
>>>>   page PFN and event channel
>>>> - the grant table
>>>> - the shared info page
>>>>
>>>> Set the RSDP address for the guest from the start_info page passed
>>>> as boot parameter.
>>>>
>>>> Signed-off-by: Juergen Gross 
>>>> ---
>>>>  grub-core/kern/i386/xen/pvh.c | 107 
>>>> ++
>>>>  1 file changed, 107 insertions(+)
>>>>
>>>> diff --git a/grub-core/kern/i386/xen/pvh.c b/grub-core/kern/i386/xen/pvh.c
>>>> index b4933b454..93ed68245 100644
>>>> --- a/grub-core/kern/i386/xen/pvh.c
>>>> +++ b/grub-core/kern/i386/xen/pvh.c
>>>> @@ -24,6 +24,7 @@
>>>>  #include 
>>>>  #include 
>>>>  #include 
>>>> +#include 
>>>>  #include 
>>>>  
>>>>  struct xen_machine_mmap_entry
>>>> @@ -39,6 +40,7 @@ static struct { char _entry[32]; } hypercall_page[128]
>>>>__attribute__ ((aligned (GRUB_XEN_PAGE_SIZE)));
>>>>  
>>>>  static grub_uint32_t xen_cpuid_base;
>>>> +static struct start_info grub_xen_start_page;
>>>>  static struct xen_machine_mmap_entry map[128];
>>>>  static unsigned int nr_map_entries;
>>>>  
>>>> @@ -104,6 +106,36 @@ grub_xen_hypercall (grub_uint32_t callno, 
>>>> grub_uint32_t a0,
>>>>return __res;
>>>>  }
>>>>  
>>>> +static grub_uint32_t
>>>> +grub_xen_get_param (int idx)
>>>> +{
>>>> +  struct xen_hvm_param xhv;
>>>> +  int r;
>>>> +
>>>> +  xhv.domid = DOMID_SELF;
>>>> +  xhv.index = idx;
>>>> +  r = grub_xen_hypercall (__HYPERVISOR_hvm_op, HVMOP_get_param,
>>>> +(grub_uint32_t) (), 0, 0, 0, 0);
>>>> +  if (r < 0)
>>>> +grub_xen_early_halt ();
>>>> +  return xhv.value;
>>>> +}
>>>> +
>>>> +static void *
>>>> +grub_xen_add_physmap (unsigned int space, void *addr)
>>>> +{
>>>> +  struct xen_add_to_physmap xatp;
>>>> +
>>>> +  xatp.domid = DOMID_SELF;
>>>> +  xatp.idx = 0;
>>>> +  xatp.space = space;
>>>> +  xatp.gpfn = (grub_addr_t) addr >> GRUB_XEN_LOG_PAGE_SIZE;
>>>> +  if (grub_xen_hypercall (__HYPERVISOR_memory_op, XENMEM_add_to_physmap,
>>>> +(grub_uint32_t) (), 0, 0, 0, 0))
>>>> +grub_xen_early_halt ();
>>>> +  return addr;
>>>> +}
>>>> +
>>>>  static void
>>>>  grub_xen_sort_mmap (void)
>>>>  {
>>>> @@ -190,12 +222,87 @@ grub_xen_get_mmap (void)
>>>>grub_xen_sort_mmap ();
>>>>  }
>>>>  
>>>> +static grub_uint64_t
>>>> +grub_xen_find_page (grub_uint64_t start)
>>>> +{
>>>> +  unsigned int i, j;
>>>> +  grub_uint64_t last = start;
>>>> +
>>>> +  /* Try to find a e820 map hole below 4G. */
>>>
>>> Doing this is kind of dangerous, what if you end up placing something
>>> on top of an MMIO region (either emulated or from a real passthrough
>>> device)?
>>
>> Shouldn't those be marked as "Reserved" in the memory map?
> 
> I don't think BARs are guaranteed to be in areas marked as reserved in
> the memory map. Unless you also scan for PCI devices and make sure
> there's no device with a BAR in the area you are attempting to
> populate I think the above is not safe.

So either I can add scanning the PCI bus (which wouldn't be too
hard IMO), or we require Xen tools to add memory map entries with
"Reserved" attribute for passed-through PCI device's MMIO-areas
(we can still do that as PCI pass-through for PVH isn't possible
yet AFAIK).


Juergen

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


Re: [Xen-devel] [PATCH v3 00/18] xen: add pvh guest support

2018-10-24 Thread Juergen Gross
On 24/10/2018 22:52, Hans van Kranenburg wrote:
> Hi,
> 
> On 10/24/2018 04:54 PM, Juergen Gross wrote:
>> This patch series adds support for booting Linux as PVH guest.
>>
>> Similar to i386/xen and x86_64/xen platforms the new i386/xenpvh
>> platform grub is booted as a standalone image directly by Xen.
>>
>> For booting Linux kernel it is using the standard linux kernel
>> loader. The only modification of the linux loader is to pass the
>> ACPI RSDP address via boot parameters to the kernel, as that table
>> might not be located at the usual physical address just below 1MB.
>>
>> The related Linux kernel patches have been accepted in the merge
>> window for post 4.19 (4.20 or 5.0, whatever it will be called).
>>
>> Changes in V3:
>> - addressed lots of comments, most by Daniel and some by Roger,
>>   details are mentioned in the individual patches
>>
>> [...]
> 
> Thanks for the hard work.
> 
> FWIW, a test report from an end user.

Thanks for the detailed report!


Juergen

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


[PATCH v3 00/18] xen: add pvh guest support

2018-10-24 Thread Juergen Gross
This patch series adds support for booting Linux as PVH guest.

Similar to i386/xen and x86_64/xen platforms the new i386/xenpvh
platform grub is booted as a standalone image directly by Xen.

For booting Linux kernel it is using the standard linux kernel
loader. The only modification of the linux loader is to pass the
ACPI RSDP address via boot parameters to the kernel, as that table
might not be located at the usual physical address just below 1MB.

The related Linux kernel patches have been accepted in the merge
window for post 4.19 (4.20 or 5.0, whatever it will be called).

Changes in V3:
- addressed lots of comments, most by Daniel and some by Roger,
  details are mentioned in the individual patches

Changes in V2:
- extend linux boot protocol version 2.14 to report back the protocol
  version supported by grub2 (needed as there are grub2 downstream
  variants in some distros which clobber data after the setup_header)
- split former patch 4 into 10 patches
- fix memmap handling for >4GB of memory
- added new patch from Hans (thanks for that one)
- addressed most of Daniel's comments (see individual patches)

Hans van Kranenburg (1):
  grub-module-verifier: Ignore all_video for xenpvh

Juergen Gross (17):
  xen: add some xen headers
  loader/linux: support passing rsdp address via boot params
  xen: carve out grant tab initialization into dedicated function
  xen: prepare common code for Xen PVH support
  xen: add some dummy headers for PVH mode
  xen: rearrange xen/init.c to prepare it for Xen PVH mode
  xen: add PVH specific defines to offset.h
  xen: add basic hooks for PVH in current code
  xen: add PVH boot entry code
  xen: setup hypercall page for PVH
  xen: get memory map from hypervisor for PVH
  xen: setup Xen specific data for PVH
  xen: init memory regions for PVH
  xen_pvh: add build runes for grub-core
  xen_pvh: support building a standalone image
  xen_pvh: support grub-install for xen_pvh
  xen_pvh: add support to configure

 configure.ac  |   3 +
 gentpl.py |   4 +-
 grub-core/Makefile.am |  12 +
 grub-core/Makefile.core.def   |  35 ++
 grub-core/kern/i386/tsc.c |   2 +-
 grub-core/kern/i386/xen/pvh.c | 340 +
 grub-core/kern/i386/xen/startup_pvh.S |  81 +
 grub-core/kern/xen/init.c | 101 --
 grub-core/loader/i386/linux.c |   9 +
 grub-core/mmap/i386/pc/mmap.c |   7 -
 include/grub/i386/linux.h |   5 +-
 include/grub/i386/memory.h|   7 +
 include/grub/i386/pc/int.h|  36 +-
 include/grub/i386/pc/int_types.h  |  59 +++
 include/grub/i386/tsc.h   |   2 +-
 include/grub/i386/xen/hypercall.h |   5 +-
 include/grub/i386/xen_pvh/boot.h  |   1 +
 include/grub/i386/xen_pvh/console.h   |   1 +
 include/grub/i386/xen_pvh/int.h   |   1 +
 include/grub/i386/xen_pvh/kernel.h|  30 ++
 include/grub/i386/xen_pvh/memory.h|   1 +
 include/grub/i386/xen_pvh/time.h  |   1 +
 include/grub/kernel.h |   4 +-
 include/grub/offsets.h|  21 +-
 include/grub/util/install.h   |   1 +
 include/grub/util/mkimage.h   |   3 +-
 include/grub/xen.h|   6 +
 include/xen/hvm/hvm_op.h  | 296 +++
 include/xen/hvm/params.h  | 284 +++
 include/xen/hvm/start_info.h  |  98 +
 include/xen/memory.h  | 665 ++
 include/xen/physdev.h | 387 
 include/xen/trace.h   | 339 +
 include/xen/xen.h | 104 --
 util/grub-install-common.c|   1 +
 util/grub-install.c   |   7 +
 util/grub-mkimage32.c |   4 +-
 util/grub-mkimage64.c |   4 +-
 util/grub-mkimagexx.c |  58 ++-
 util/grub-module-verifier.c   |   1 +
 util/mkimage.c|  23 +-
 41 files changed, 2906 insertions(+), 143 deletions(-)
 create mode 100644 grub-core/kern/i386/xen/pvh.c
 create mode 100644 grub-core/kern/i386/xen/startup_pvh.S
 create mode 100644 include/grub/i386/pc/int_types.h
 create mode 100644 include/grub/i386/xen_pvh/boot.h
 create mode 100644 include/grub/i386/xen_pvh/console.h
 create mode 100644 include/grub/i386/xen_pvh/int.h
 create mode 100644 include/grub/i386/xen_pvh/kernel.h
 create mode 100644 include/grub/i386/xen_pvh/memory.h
 create mode 100644 include/grub/i386/xen_pvh/time.h
 create mode 100644 include/xen/hvm/hvm_op.h
 create mode 100644 include/xen/hvm/params.h
 create mode 100644 include/xen/hvm/start_info.h
 create mode 100644 include/xen/memory.h
 create mode 100644 include/xen/physdev.h
 create mode 100644 include/xen/trace.h

-- 
2.16.4


___
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mail

  1   2   3   >