Skip to contentSkip to conversation

Dynamic allocation of memory regions and IBM vTPM v2

PATCH Superseded Stefan Berger 1 participant 7 messages Jan 30, 2023

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Dynamic allocation of memory regions and IBM vTPM v2

Hello,

This is an addition to the series sent from Daniel Axtens
(https://lists.gnu.org/archive/html/grub-devel/2022-04/msg00064.html).

Patch 'ieee1275: request memory with ibm,client-architecture-support' implements vectors 1-4 of client-architecture-support negotiation However, during some tests, we found this can be a problem if:

- we have more than 64 CPUs
- Hardware Management Console (HMC) is configured to minimum of CPUs >64 (for example, min of 200 CPUs)
- Grub needs to request memory.

If vector 5 is not implemented, Power Hypervisor will consider the default value for vector 5 and 64 will bet set as the maximum
number of CPUs supported by the OS, causing the machine to fail to init. Today we support 256 CPUs (max) on Power, so we need to implement vector 5 and set the MAX CPUs bits to this value.

Stefan

v3:
- Added #if defined(__powerpc__) [1/6]
- Address possible integer underlow [3/6]
- Added comment why tpm_init() function cannot be called from GRUB_MOD_INIT()
- Added Reviewed-by tags to {1,2,5,6}/6

v2:
- Followed Daniel K.'s list of suggestions checking the list more than twice and adjusted formatting, line breaks, etc. in all patches
- Conditional compilation of ppc64-specific code using #ifdef __powerpc__
- Fixing of installation of memtool; adding of warning message for __powerpc__ since it requires a reboot after running memory stress test
- Tested it on i386-pc and PowerKVM

Daniel Axtens (4):
ieee1275: request memory with ibm, client-architecture-support
ieee1275: drop len -= 1 quirk in heap_init
ieee1275: support runtime memory claiming
Add memtool module with memory allocation stress-test

Diego Domingos (1):
ieee1275: implement vec5 for cas negotiation

Stefan Berger (1):
ibmvtpm: Add support for trusted boot using a vTPM 2.0

 configure.ac                          |   1 +
 docs/grub-dev.texi                    |   7 +-
 docs/grub.texi                        |   3 +-
 grub-core/Makefile.core.def           |  13 +
 grub-core/commands/ieee1275/ibmvtpm.c | 155 +++++++++
 grub-core/commands/memtools.c         | 160 +++++++++
 grub-core/kern/ieee1275/cmain.c       |   5 +
 grub-core/kern/ieee1275/init.c        | 445 +++++++++++++++++++++++++-
 grub-core/kern/mm.c                   |   4 +
 include/grub/ieee1275/ieee1275.h      |  13 +
 10 files changed, 789 insertions(+), 17 deletions(-)
 create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
 create mode 100644 grub-core/commands/memtools.c
Signature · 1 line
2.25.1
#2 lore raw

PATCH v3 2/6 ieee1275: drop len -= 1 quirk in heap_init

From: Daniel Axtens <[email protected]>

This was apparently 'required by some firmware': commit dc9468500919 ("2007-02-12 Hollis Blanchard <[email protected]>").

It's not clear what firmware that was, and what platform from 14 years ago which exhibited the bug then is still both in use and buggy now.

It doesn't cause issues on qemu (mac99 or pseries) or under PFW for Power8.

I don't have access to old Mac hardware, but if anyone feels especially strongly we can put it under some feature flag. I really want to disable it under pseries because it will mess with region merging.

---
 grub-core/kern/ieee1275/init.c | 1 -
 1 file changed, 1 deletion(-)
grub-core/kern/ieee1275/init.c +0 -1
index 0bc571e3e..bb234b268 100644
@@ -166,7 +166,6 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
 	  addr = 0x180000;
 	}
     }
-  len -= 1; /* Required for some firmware.  */
 
   /* Never exceed HEAP_MAX_SIZE  */
   if (*total + len > HEAP_MAX_SIZE)
Signature · 1 line
2.25.1
#1 lore raw

PATCH v3 1/6 ieee1275: request memory with ibm, client-architecture-support

In reply to Stefan Berger

From: Daniel Axtens <[email protected]>

On PowerVM, the first time we boot a Linux partition, we may only get 256MB of real memory area, even if the partition has more memory.

This isn't enough to reliably verify a kernel. Fortunately, the Power Architecture Platform Reference (PAPR) defines a method we can call to ask for more memory: the broad and powerful ibm,client-architecture-support (CAS) method.

CAS can do an enormous amount of things on a PAPR platform: as well as asking for memory, you can set the supported processor level, the interrupt controller, hash vs radix mmu, and so on.

If:

- we are running under what we think is PowerVM (compatible property of / begins with "IBM"), and

- the full amount of RMA is less than 512MB (as determined by the reg property of /memory)

then call CAS as follows: (refer to the Linux on Power Architecture Reference, LoPAR, which is public, at B.5.2.3):

- Use the "any" PVR value and supply 2 option vectors.

- Set option vector 1 (PowerPC Server Processor Architecture Level) to "ignore".

- Set option vector 2 with default or Linux-like options, including a min-rma-size of 512MB.

- Set option vector 3 to request Floating Point, VMX and Decimal Floating point, but don't abort the boot if we can't get them.

- Set option vector 4 to request a minimum VP percentage to 1%, which is what Linux requests, and is below the default of 10%. Without this, some systems with very large or very small configurations fail to boot.

This will cause a CAS reboot and the partition will restart with 512MB of RMA. Importantly, grub will notice the 512MB and not call CAS again.

Notes about the choices of parameters:

- A partition can be configured with only 256MB of memory, which would mean this request couldn't be satisfied, but PFW refuses to load with only 256MB of memory, so it's a bit moot. SLOF will run fine with 256MB, but we will never call CAS under qemu/SLOF because /compatible won't begin with "IBM".)

- unspecified CAS vectors take on default values. Some of these values might restrict the ability of certain hardware configurations to boot. This is why we need to specify the VP percentage in vector 4, which is in turn why we need to specify vector 3.

Finally, we should have enough memory to verify a kernel, and we will reach Linux. One of the first things Linux does while still running under OpenFirmware is to call CAS with a much fuller set of options (including asking for 512MB of memory). Linux includes a much more restrictive set of PVR values and processor support levels, and this CAS invocation will likely induce another reboot. On this reboot grub will again notice the higher RMA, and not call CAS. We will get to Linux again, Linux will call CAS again, but because the values are now set for Linux this will not induce another CAS reboot and we will finally boot all the way to userspace.

On all subsequent boots, everything will be configured with 512MB of RMA, so there will be no further CAS reboots from grub. (phyp is super sticky with the RMA size - it persists even on cold boots. So if you've ever booted Linux in a partition, you'll probably never have grub call CAS. It'll only ever fire the first time a partition loads grub, or if you deliberately lower the amount of memory your partition has below 512MB.)

---
 grub-core/kern/ieee1275/cmain.c  |   5 +
 grub-core/kern/ieee1275/init.c   | 165 +++++++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h |  10 ++
 3 files changed, 180 insertions(+)
grub-core/kern/ieee1275/cmain.c +5 -0
index 4442b6a83..400d85d28 100644
@@ -123,6 +123,11 @@ grub_ieee1275_find_options (void)
 	      break;
 	    }
 	}
+
+#if defined(__powerpc__)
+      if (grub_strncmp (tmp, "IBM,", 4) == 0)
+	grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY);
+#endif
     }
 
   if (is_smartfirmware)
grub-core/kern/ieee1275/init.c +165 -0

176 lines not on this page · raw message

include/grub/ieee1275/ieee1275.h +10 -0
index f53228703..77e4e61e5 100644
@@ -128,6 +128,16 @@ enum grub_ieee1275_flag
   GRUB_IEEE1275_FLAG_CURSORONOFF_ANSI_BROKEN,
 
   GRUB_IEEE1275_FLAG_RAW_DEVNAMES,
+
+#if defined(__powerpc__)
+  /*
+   * On PFW, the first time we boot a Linux partition, we may only get 256MB of
+   * real memory area, even if the partition has more memory. Set this flag if
+   * we think we're running under PFW. Then, if this flag is set, and the RMA is
+   * only 256MB in size, try asking for more with CAS.
+   */
+  GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
+#endif
 };
 
 extern int EXPORT_FUNC(grub_ieee1275_test_flag) (enum grub_ieee1275_flag flag);
Signature · 1 line
2.25.1
#6 lore raw

PATCH v3 6/6 ibmvtpm: Add support for trusted boot using a vTPM 2.0

In reply to Stefan Berger

Add support for trusted boot using a vTPM 2.0 on the IBM IEEE1275 PowerPC platform. With this patch grub now measures text and binary data into the TPM's PCRs 8 and 9 in the same way as the x86_64 platform does.

This patch requires Daniel Axtens's patches for claiming more memory.

Note: The tpm_init() function cannot be called from GRUB_MOD_INIT() since it does not find the device nodes upon module initialization and therefore the call to tpm_init() must be deferred to grub_tpm_measure().

For vTPM support to work on PowerVM, system driver levels 1010.30 or 1020.00 are required.

Note: Previous versions of firmware levels with the 2hash-ext-log API call have a bug that, once this API call is invoked, has the effect of disabling the vTPM driver under Linux causing an error message to be displayed in the Linux kernel log. Those users will have to update their machines to the firmware levels mentioned above.

---
 docs/grub.texi                        |   3 +-
 grub-core/Makefile.core.def           |   7 ++
 grub-core/commands/ieee1275/ibmvtpm.c | 155 ++++++++++++++++++++++++++
 include/grub/ieee1275/ieee1275.h      |   3 +
 4 files changed, 167 insertions(+), 1 deletion(-)
 create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
docs/grub.texi +2 -1
index 659885388..a3e9ce2d1 100644
@@ -6525,7 +6525,8 @@ tpm module is loaded. As such it is recommended that the tpm module be built
 into @file{core.img} in order to avoid a potential gap in measurement between
 @file{core.img} being loaded and the tpm module being loaded.
 
-Measured boot is currently only supported on EFI platforms.
+Measured boot is currently only supported on EFI and IBM IEEE1275 PowerPC
+platforms.
 
 @node Lockdown
 @section Lockdown when booting on a secure setup
grub-core/Makefile.core.def +7 -0
index 6d30fd11f..038797ee9 100644
@@ -1133,6 +1133,13 @@ module = {
   enable = powerpc_ieee1275;
 };
 
+module = {
+  name = tpm;
+  common = commands/tpm.c;
+  ieee1275 = commands/ieee1275/ibmvtpm.c;
+  enable = powerpc_ieee1275;
+};
+
 module = {
   name = terminal;
   common = commands/terminal.c;
grub-core/commands/ieee1275/ibmvtpm.c new file +155 -0

155 lines not on this page · raw message

include/grub/ieee1275/ieee1275.h +3 -0
index 77e4e61e5..9f669fbee 100644
@@ -24,6 +24,9 @@
 #include <grub/types.h>
 #include <grub/machine/ieee1275.h>
 
+#define GRUB_IEEE1275_CELL_FALSE       ((grub_ieee1275_cell_t) 0)
+#define GRUB_IEEE1275_CELL_TRUE        ((grub_ieee1275_cell_t) -1)
+
 struct grub_ieee1275_mem_region
 {
   unsigned int start;
Signature · 1 line
2.25.1
#4 lore raw

PATCH v3 4/6 ieee1275: implement vec5 for cas negotiation

In reply to Stefan Berger

From: Diego Domingos <[email protected]>

As a legacy support, if the vector 5 is not implemented, Power Hypervisor will consider the max CPUs as 64 instead 256 currently supported during client-architecture-support negotiation.

This patch implements the vector 5 and set the MAX CPUs to 256 while setting the others values to 0 (default).

---
 grub-core/kern/ieee1275/init.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
grub-core/kern/ieee1275/init.c +20 -1
index 726fcdd36..22e99d425 100644
@@ -500,6 +500,19 @@ struct option_vector2
   grub_uint8_t max_pft_size;
 } GRUB_PACKED;
 
+struct option_vector5
+{
+  grub_uint8_t byte1;
+  grub_uint8_t byte2;
+  grub_uint8_t byte3;
+  grub_uint8_t cmo;
+  grub_uint8_t associativity;
+  grub_uint8_t bin_opts;
+  grub_uint8_t micro_checkpoint;
+  grub_uint8_t reserved0;
+  grub_uint32_t max_cpus;
+} GRUB_PACKED;
+
 struct pvr_entry
 {
   grub_uint32_t mask;
@@ -521,6 +534,8 @@ struct cas_vector
   grub_uint16_t vec3;
   grub_uint8_t vec4_size;
   grub_uint16_t vec4;
+  grub_uint8_t vec5_size;
+  struct option_vector5 vec5;
 } GRUB_PACKED;
 
 /*
@@ -545,7 +560,7 @@ grub_ieee1275_ibm_cas (void)
   struct cas_vector vector =
   {
     .pvr_list = { { 0x00000000, 0xffffffff } }, /* any processor */
-    .num_vecs = 4 - 1,
+    .num_vecs = 5 - 1,
     .vec1_size = 0,
     .vec1 = 0x80, /* ignore */
     .vec2_size = 1 + sizeof (struct option_vector2) - 2,
@@ -556,6 +571,10 @@ grub_ieee1275_ibm_cas (void)
     .vec3 = 0x00e0, /* ask for FP + VMX + DFP but don't halt if unsatisfied */
     .vec4_size = 2 - 1,
     .vec4 = 0x0001, /* set required minimum capacity % to the lowest value */
+    .vec5_size = 1 + sizeof (struct option_vector5) - 2,
+    .vec5 = {
+      0, 0, 0, 0, 0, 0, 0, 0, 256
+    }
   };
 
   INIT_IEEE1275_COMMON (&args.common, "call-method", 3, 2);
Signature · 1 line
2.25.1
#3 lore raw

PATCH v3 3/6 ieee1275: support runtime memory claiming

In reply to Stefan Berger

From: Daniel Axtens <[email protected]>

On powerpc-ieee1275, we are running out of memory trying to verify anything. This is because:

- we have to load an entire file into memory to verify it. This is difficult to change with appended signatures.
- We only have 32MB of heap.
- Distro kernels are now often around 30MB.

So we want to be able to claim more memory from OpenFirmware for our heap at runtime.

There are some complications:

- The grub mm code isn't the only thing that will make claims on memory from OpenFirmware:

* PFW/SLOF will have claimed some for their own use.

* The ieee1275 loader will try to find other bits of memory that we haven't claimed to place the kernel and initrd when we go to boot.

* Once we load Linux, it will also try to claim memory. It claims memory without any reference to /memory/available, it just starts at min(top of RMO, 768MB) and works down. So we need to avoid this area. See arch/powerpc/kernel/prom_init.c as of v5.11.

- The smallest amount of memory a ppc64 KVM guest can have is 256MB. It doesn't work with distro kernels but can work with custom kernels. We should maintain support for that. (ppc32 can boot with even less, and we shouldn't break that either.)

- Even if a VM has more memory, the memory OpenFirmware makes available as Real Memory Area can be restricted. Even with our CAS work, an LPAR on a PowerVM box is likely to have only 512MB available to OpenFirmware even if it has many gigabytes of memory allocated.

What should we do?

We don't know in advance how big the kernel and initrd are going to be, which makes figuring out how much memory we can take a bit tricky.

To figure out how much memory we should leave unused, I looked at:

- an Ubuntu 20.04.1 ppc64le pseries KVM guest:

    vmlinux: ~30MB
    initrd:  ~50MB

- a RHEL8.2 ppc64le pseries KVM guest:

    vmlinux: ~30MB
    initrd:  ~30MB

So to give us a little wriggle room, I think we want to leave at least 128MB for the loader to put vmlinux and initrd in memory and leave Linux with space to satisfy its early allocations.

Allow other space to be allocated at runtime.

---
 docs/grub-dev.texi             |   7 +-
 grub-core/kern/ieee1275/init.c | 270 ++++++++++++++++++++++++++++++---
 2 files changed, 257 insertions(+), 20 deletions(-)
docs/grub-dev.texi +5 -2
index f76fc658b..31eb99ea2 100644
@@ -1059,7 +1059,10 @@ space is limited to 4GiB. GRUB allocates pages from EFI for its heap, at most
 1.6 GiB.
 
 On i386-ieee1275 and powerpc-ieee1275 GRUB uses same stack as IEEE1275.
-It allocates at most 32MiB for its heap.
+
+On i386-ieee1275 and powerpc-ieee1275, GRUB will allocate 32MiB for its heap on
+startup. It may allocate more at runtime, as long as at least 128MiB remain free
+in OpenFirmware.
 
 On sparc64-ieee1275 stack is 256KiB and heap is 2MiB.
 
@@ -1087,7 +1090,7 @@ In short:
 @item i386-qemu               @tab 60 KiB  @tab < 4 GiB
 @item *-efi                   @tab ?       @tab < 1.6 GiB
 @item i386-ieee1275           @tab ?       @tab < 32 MiB
-@item powerpc-ieee1275        @tab ?       @tab < 32 MiB
+@item powerpc-ieee1275        @tab ?       @tab available memory - 128MiB
 @item sparc64-ieee1275        @tab 256KiB  @tab 2 MiB
 @item arm-uboot               @tab 256KiB  @tab 2 MiB
 @item mips(el)-qemu_mips      @tab 2MiB    @tab 253 MiB
grub-core/kern/ieee1275/init.c +252 -18

329 lines not on this page · raw message

Signature · 1 line
2.25.1
#5 lore raw

PATCH v3 5/6 Add memtool module with memory allocation stress-test

In reply to Stefan Berger

From: Daniel Axtens <[email protected]>

When working on memory, it's nice to be able to test your work.

Add a memtest module. When compiled with --enable-mm-debug, it exposes 3 commands:

* lsmem - print all allocations and free space in all regions
* lsfreemem - print free space in all regions

* stress_big_allocs - stress test large allocations:
- how much memory can we allocate in one chunk?
- how many 1MB chunks can we allocate?
- check that gap-filling works with a 1MB aligned 900kB alloc + a

     100kB alloc.
---
 configure.ac                  |   1 +
 grub-core/Makefile.core.def   |   6 ++
 grub-core/commands/memtools.c | 160 ++++++++++++++++++++++++++++++++++
 grub-core/kern/mm.c           |   4 +
 4 files changed, 171 insertions(+)
 create mode 100644 grub-core/commands/memtools.c
configure.ac +1 -0
index 93626b798..ca42ff8f7 100644
@@ -1520,6 +1520,7 @@ else
   MM_DEBUG=0
 fi
 AC_SUBST([MM_DEBUG])
+AM_CONDITIONAL([COND_MM_DEBUG], [test x$MM_DEBUG = x1])
 
 AC_ARG_ENABLE([cache-stats],
 	      AS_HELP_STRING([--enable-cache-stats],
grub-core/Makefile.core.def +6 -0
index 71093a100..6d30fd11f 100644
@@ -2548,3 +2548,9 @@ module = {
   common = commands/i386/wrmsr.c;
   enable = x86;
 };
+
+module = {
+  name = memtools;
+  common = commands/memtools.c;
+  condition = COND_MM_DEBUG;
+};
grub-core/commands/memtools.c new file +160 -0

160 lines not on this page · raw message

grub-core/kern/mm.c +4 -0
index 94eb29108..792ab0a83 100644
@@ -734,6 +734,8 @@ grub_mm_dump_free (void)
     {
       grub_mm_header_t p;
 
+      grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size);
+
       /* Follow the free list.  */
       p = r->first;
       do
@@ -761,6 +763,8 @@ grub_mm_dump (unsigned lineno)
     {
       grub_mm_header_t p;
 
+      grub_printf ("Region %p (size %" PRIuGRUB_SIZE ")\n\n", r, r->size);
+
       for (p = (grub_mm_header_t) ALIGN_UP ((grub_addr_t) (r + 1),
 					    GRUB_MM_ALIGN);
 	   (grub_addr_t) p < (grub_addr_t) (r+1) + r->size;
Signature · 1 line
2.25.1