Dynamic allocation of memory regions and IBM vTPM v2
v2 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
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 | 151 +++++++++ grub-core/commands/memtools.c | 160 ++++++++++ grub-core/kern/ieee1275/cmain.c | 3 + grub-core/kern/ieee1275/init.c | 443 +++++++++++++++++++++++++- grub-core/kern/mm.c | 4 + include/grub/ieee1275/ieee1275.h | 11 + 10 files changed, 779 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
PATCH v2 1/6 ieee1275: request memory with ibm, client-architecture-support
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 | 3 + grub-core/kern/ieee1275/init.c | 165 +++++++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 8 ++ 3 files changed, 176 insertions(+)
grub-core/kern/ieee1275/cmain.c +3 -0
@@ -123,6 +123,9 @@ grub_ieee1275_find_options (void)break ; } } + +if (grub_strncmp (tmp,"IBM," ,4 ) ==0 ) +grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY ); }if (is_smartfirmware)
grub-core/kern/ieee1275/init.c +165 -0
176 lines not on this page · raw message
include/grub/ieee1275/ieee1275.h +8 -0
@@ -128,6 +128,14 @@ enum grub_ieee1275_flagGRUB_IEEE1275_FLAG_CURSORONOFF_ANSI_BROKEN ,GRUB_IEEE1275_FLAG_RAW_DEVNAMES , + +/* +* 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, };extern int EXPORT_FUNC (grub_ieee1275_test_flag ) (enum grub_ieee1275_flag flag);
Signature · 1 line
2.25.1
Re: [PATCH v2 1/6] ieee1275: request memory with ibm, client-architecture-support
Quoted 86 lines · 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.)
Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Stefan Berger <[email protected]>--- grub-core/kern/ieee1275/cmain.c | 3 + grub-core/kern/ieee1275/init.c | 165 +++++++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 8 ++ 3 files changed, 176 insertions(+)grub-core/kern/ieee1275/cmain.c +0
-0
grub-core/kern/ieee1275/cmain.c grub_ieee1275_find_options (void) · line 126
Quoted 4 lines
grub-core/kern/ieee1275/cmain.c +1
-0@@ -123,6 +123,9 @@ grub_ieee1275_find_options (void)break ; } } +
#if defined(__powerpc__) ?
grub-core/kern/ieee1275/cmain.c grub_ieee1275_find_options (void) · line 128
Quoted 2 lines
grub-core/kern/ieee1275/cmain.c +2
-0+if (grub_strncmp (tmp,"IBM," ,4 ) ==0 ) +grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY );
#endif
Quoted 189 lines
grub-core/kern/ieee1275/cmain.c +0
-0}if (is_smartfirmware)grub-core/kern/ieee1275/init.c +165
-0@@ -200,11 +200,176 @@ heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, return 0; } +/* +* How much memory does OF believe it has? (regardless of whether +* it's accessible or not) +*/ +static grub_err_t +grub_ieee1275_total_mem (grub_uint64_t *total) +{ +grub_ieee1275_phandle_t root; +grub_ieee1275_phandle_t memory; +grub_uint32_t reg[4 ]; +grub_ssize_t reg_size; +grub_uint32_t address_cells =1 ; +grub_uint32_t size_cells =1 ; +grub_uint64_t size; + +/* If we fail to get to the end, report 0. */ + *total =0 ; + +/* Determine the format of each entry in `reg'. */ +if (grub_ieee1275_finddevice ("/" , &root)) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't find / node" ); +if (grub_ieee1275_get_integer_property (root,"#address-cells" , &address_cells, +sizeof (address_cells),0 )) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't examine #address-cells" ); +if (grub_ieee1275_get_integer_property (root,"#size-cells" , &size_cells, +sizeof (size_cells),0 )) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't examine #size-cells" ); + +if (size_cells > address_cells) + address_cells = size_cells; + +/* Load `/memory/reg'. */ +if (grub_ieee1275_finddevice ("/memory" , &memory)) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't find /memory node" ); +if (grub_ieee1275_get_integer_property (memory,"reg" , reg, +sizeof (reg), ®_size)) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"couldn't examine /memory/reg property" ); +if (reg_size <0 || (grub_size_t ) reg_size >sizeof (reg)) +return grub_error (GRUB_ERR_UNKNOWN_DEVICE ,"/memory response buffer exceeded" ); + +if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS )) + { + address_cells =1 ; + size_cells =1 ; + } + +/* Decode only the size */ + size = reg[address_cells]; +if (size_cells ==2 ) + size = (size <<32 ) | reg[address_cells +1 ]; + + *total = size; + +return grub_errno; +} + +#if defined(__powerpc__) + +/* See PAPR or arch/powerpc/kernel/prom_init.c */ +struct option_vector2 +{ +grub_uint8_t byte1; +grub_uint16_t reserved; +grub_uint32_t real_base; +grub_uint32_t real_size; +grub_uint32_t virt_base; +grub_uint32_t virt_size; +grub_uint32_t load_base; +grub_uint32_t min_rma; +grub_uint32_t min_load; +grub_uint8_t min_rma_percent; +grub_uint8_t max_pft_size; +}GRUB_PACKED ; + +struct pvr_entry +{ +grub_uint32_t mask; +grub_uint32_t entry; +}; + +struct cas_vector +{ +struct + { +struct pvr_entry terminal; + } pvr_list; +grub_uint8_t num_vecs; +grub_uint8_t vec1_size; +grub_uint8_t vec1; +grub_uint8_t vec2_size; +struct option_vector2 vec2; +grub_uint8_t vec3_size; +grub_uint16_t vec3; +grub_uint8_t vec4_size; +grub_uint16_t vec4; +}GRUB_PACKED ; + +/* +* Call ibm,client-architecture-support to try to get more RMA. +* We ask for 512MB which should be enough to verify a distro kernel. +* We ignore most errors: if we don't succeed we'll proceed with whatever +* memory we have. +*/ +static void +grub_ieee1275_ibm_cas (void ) +{ +int rc; +grub_ieee1275_ihandle_t root; +struct cas_args + { +struct grub_ieee1275_common_hdr common; +grub_ieee1275_cell_t method; +grub_ieee1275_ihandle_t ihandle; +grub_ieee1275_cell_t cas_addr; +grub_ieee1275_cell_t result; + } args; +struct cas_vector vector = + { + .pvr_list = { {0x00000000 ,0xffffffff } },/* any processor */ + .num_vecs =4 -1 , + .vec1_size =0 , + .vec1 =0x80 ,/* ignore */ + .vec2_size =1 +sizeof (struct option_vector2 ) -2 , + .vec2 = { +0 ,0 ,-1 ,-1 ,-1 ,-1 ,-1 ,512 ,-1 ,0 ,48 + }, + .vec3_size =2 -1 , + .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 */ + }; + +INIT_IEEE1275_COMMON (&args.common,"call-method" ,3 ,2 ); + args.method = (grub_ieee1275_cell_t )"ibm,client-architecture-support" ; + rc =grub_ieee1275_open ("/" , &root); +if (rc) + { +grub_error (GRUB_ERR_IO ,"could not open root when trying to call CAS" ); +return ; + } + args.ihandle = root; + args.cas_addr = (grub_ieee1275_cell_t) &vector; + +grub_printf ("Calling ibm,client-architecture-support from grub..." ); +IEEE1275_CALL_ENTRY_FN (&args); +grub_printf ("done\n" ); + +grub_ieee1275_close (root); +} + +#endif /* __powerpc__ */ +static void grub_claim_heap (void ) {unsigned long total =0 ; +#if defined(__powerpc__) +if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY )) + { +grub_uint64_t rma_size; +grub_err_t err; + + err =grub_ieee1275_total_mem (&rma_size); +/* if we have an error, don't call CAS, just hope for the best */ +if (err ==GRUB_ERR_NONE && rma_size < (512 *1024 *1024 )) +grub_ieee1275_ibm_cas (); + } +#endif +grub_machine_mmap_iterate (heap_init, &total); }#endif include/grub/ieee1275/ieee1275.h +0
-0
include/grub/ieee1275/ieee1275.h enum grub_ieee1275_flag · line 137
Quoted 10 lines
include/grub/ieee1275/ieee1275.h +7
-0@@ -128,6 +128,14 @@ enum grub_ieee1275_flagGRUB_IEEE1275_FLAG_CURSORONOFF_ANSI_BROKEN ,GRUB_IEEE1275_FLAG_RAW_DEVNAMES , + +/* +* 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. + */
#if defined(__powerpc__) ?
include/grub/ieee1275/ieee1275.h enum grub_ieee1275_flag · line 138
Quoted 1 line
include/grub/ieee1275/ieee1275.h +1
-0+ GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY,
#endif
Otherwise Reviewed-by: Daniel Kiper <[email protected]>...
Daniel
Re: [PATCH v2 1/6] ieee1275: request memory with ibm, client-architecture-support
In reply to Stefan Bergergrub-core/kern/ieee1275/cmain.c grub_ieee1275_find_options (void) · line 129
Quoted 4 lines
grub-core/kern/ieee1275/cmain.c +3
-0+ +if (grub_strncmp (tmp,"IBM," ,4 ) ==0 ) +grub_ieee1275_set_flag (GRUB_IEEE1275_FLAG_CAN_TRY_CAS_FOR_MORE_MEMORY ); }
Does it work on all IBM? IBM has a long history of PPC machines. What happens when it doesn't?
PATCH v2 2/6 ieee1275: drop len -= 1 quirk in heap_init
In reply to Stefan BergerFrom: 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
@@ -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
Re: [PATCH v2 2/6] ieee1275: drop len -= 1 quirk in heap_init
Quoted 8 lines · Stefan Berger
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.
Quoted 7 lines
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.
Signed-off-by Daniel Axtens <[email protected]>
Daniel
Re: [PATCH v2 2/6] ieee1275: drop len -= 1 quirk in heap_init
In reply to Stefan BergerWhat problem are you trying to solve? Is losing 32 bytes a problem?
Quoted 40 lines · Stefan Berger
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.
Signed-off-by Daniel Axtens <[email protected]>--- grub-core/kern/ieee1275/init.c | 1 - 1 file changed, 1 deletion(-)grub-core/kern/ieee1275/init.c +0
-2@@ -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 ) --2.25.1
_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Signature · 2 lines
Regards Vladimir 'phcoder' Serbinenko
Re: [PATCH v2 2/6] ieee1275: drop len -= 1 quirk in heap_init
In reply to Stefan BergerQuoted 5 lines · Stefan Berger
From: Daniel Axtens <[email protected]>
This was apparently 'required by some firmware': commit
dc9468500919("2007-02-12 Hollis Blanchard <[email protected]>").
Quoted 8 lines
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.
I can test this on my iBook G4 but not this week as I am currently not at home.
Adrian
Signature · 4 lines
.''`. John Paul Adrian Glaubitz : :' : Debian Developer `. `' Physicist `- GPG: 62FF 8A75 84E0 2956 9546 0006 7426 3B37 F5B5 F913
PATCH v2 3/6 ieee1275: support runtime memory claiming
In reply to Stefan BergerFrom: 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: ~30MBSo 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 | 268 ++++++++++++++++++++++++++++++--- 2 files changed, 255 insertions(+), 20 deletions(-)
docs/grub-dev.texi +5 -2
@@ -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 +250 -18
327 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v2 3/6] ieee1275: support runtime memory claiming
Quoted 61 lines · 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. Seearch/powerpc/kernel/prom_init.cas 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: ~30MBSo 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.
Tested-by Stefan Berger <[email protected]>Signed-off-by Daniel Axtens <[email protected]>
[...]
Quoted 1 line
+static
grub_err_tgrub_ieee1275_mm_add_region(grub_size_tsize, unsigned int flags)
static grub_err_tgrub_ieee1275_mm_add_region (grub_size_t size, unsigned int flags)
Quoted 17 lines
grub-core/kern/ieee1275/init.c +17
-0+{ +grub_uint32_t total = size; +grub_uint32_t free_memory =0 ; + +grub_dprintf ("ieee1275" ,"mm requested region of size %x, flags %x\n" , + size, flags); + +/* +* Update free memory each time, which is a bit inefficient but guards us +* against a situation where some OF driver goes out to firmware for +* memory and we don't realise. +*/ +grub_machine_mmap_iterate (count_free, &free_memory); + +/* Ensure we leave enough space to boot. */ +if (free_memory <=RUNTIME_MIN_SPACE + size) + {
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 392
Quoted 10 lines
grub-core/kern/ieee1275/init.c +10
-0+grub_dprintf ("ieee1275" ,"Cannot satisfy allocation and retain minimum runtime space\n" ); +return GRUB_ERR_OUT_OF_MEMORY ; + } + +grub_dprintf ("ieee1275" ,"free = 0x%x\n" , free_memory); + +if (flags &GRUB_MM_ADD_REGION_CONSECUTIVE ) + { +/* first try rounding up hard for the sake of speed */ + total =grub_max (ALIGN_UP (size,1024 *1024 ) +1024 *1024 ,32 *1024 *1024 );
s/ALIGN_UP(/ALIGN_UP (/
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 400
Quoted 8 lines
grub-core/kern/ieee1275/init.c +8
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"looking for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"get memory from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + +/* fallback: requested size + padding for a grub_mm_header_t and region */
This...
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 403
Quoted 3 lines
grub-core/kern/ieee1275/init.c +3
-0+if (total !=0 ) + { + total = size +48 ;
... and this should be dropped. I think this patch set, [1], [2], solves this kinds of problems in general. Please take a look...
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 415
Quoted 12 lines
grub-core/kern/ieee1275/init.c +12
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"fallback for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"fallback from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + } + } +else + { +/* provide padding for a grub_mm_header_t and region */ + total = size +48 ;
Ditto... And then probably total can be dropped too...
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 425
Quoted 10 lines
grub-core/kern/ieee1275/init.c +10
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); +grub_machine_mmap_iterate (heap_init, &total); +grub_dprintf ("ieee1275" ,"get noncontig memory from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + } + +if (total ==0 ) +return GRUB_ERR_NONE ; +else +return GRUB_ERR_OUT_OF_MEMORY ; +}
Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2022-11/msg00147.html
[2] https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00115.html
Re: [PATCH v2 3/6] ieee1275: support runtime memory claiming
Quoted 20 lines · Daniel Kiper
Quoted 11 lines · 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.grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 392
Quoted 5 lines
grub-core/kern/ieee1275/init.c +5
-0+ +if (flags &GRUB_MM_ADD_REGION_CONSECUTIVE ) + { +/* first try rounding up hard for the sake of speed */ + total =grub_max (ALIGN_UP (size,1024 *1024 ) +1024 *1024 ,32 *1024 *1024 );s/ALIGN_UP(/ALIGN_UP (/
Quoted 10 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 400
Quoted 8 lines
grub-core/kern/ieee1275/init.c +8
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"looking for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"get memory from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + +/* fallback: requested size + padding for a grub_mm_header_t and region */ This...
With 'this' you mean the comment above or which exact part?
Quoted 6 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 403
Quoted 4 lines
grub-core/kern/ieee1275/init.c +3
-0+if (total !=0 ) + { + total = size +48 ;... and this should be dropped. I think this patch set, [1], [2], solves
You mean adding 48 to it can be dropped?
Quoted 1 line
this kinds of problems in general. Please take a look...
I looked at them and I don't know how they are related.
Quoted 15 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 415
Quoted 13 lines
grub-core/kern/ieee1275/init.c +12
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"fallback for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"fallback from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + } + } +else + { +/* provide padding for a grub_mm_header_t and region */ + total = size +48 ;Ditto... And then probably total can be dropped too...
Don't we need a parameter to grub_machine_mmap_iterate (region_claim, &total) ?
Stefan
Quoted 21 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 425
Quoted 11 lines
grub-core/kern/ieee1275/init.c +10
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); +grub_machine_mmap_iterate (heap_init, &total); +grub_dprintf ("ieee1275" ,"get noncontig memory from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + } + +if (total ==0 ) +return GRUB_ERR_NONE ; +else +return GRUB_ERR_OUT_OF_MEMORY ; +}Daniel
[1] https://lists.gnu.org/archive/html/grub-devel/2022-11/msg00147.html
[2] https://lists.gnu.org/archive/html/grub-devel/2022-12/msg00115.html_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
Re: [PATCH v2 3/6] ieee1275: support runtime memory claiming
Adding Zhang...
Quoted 32 lines · Stefan Berger
Quoted 20 lines · Daniel Kiper
Quoted 11 lines · 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.grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 392
Quoted 5 lines
grub-core/kern/ieee1275/init.c +5
-0+ +if (flags &GRUB_MM_ADD_REGION_CONSECUTIVE ) + { +/* first try rounding up hard for the sake of speed */ + total =grub_max (ALIGN_UP (size,1024 *1024 ) +1024 *1024 ,32 *1024 *1024 );s/ALIGN_UP(/ALIGN_UP (/
Quoted 10 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 400
Quoted 8 lines
grub-core/kern/ieee1275/init.c +8
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"looking for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"get memory from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + +/* fallback: requested size + padding for a grub_mm_header_t and region */ This...
Quoted 1 line
With 'this' you mean the comment above or which exact part?
Yes, exactly...
Quoted 7 lines
Quoted 5 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 403
Quoted 3 lines
grub-core/kern/ieee1275/init.c +3
-0+if (total !=0 ) + { + total = size +48 ;... and this should be dropped. I think this patch set, [1], [2], solves
You mean adding 48 to it can be dropped?
Yep... Sorry for being imprecise...
Quoted 3 lines
Quoted 1 line
this kinds of problems in general. Please take a look...
I looked at them and I don't know how they are related.
If I do not miss anything your patch set does not take into account size needed for *grub_mm_region_t and *grub_mm_header structs. They are needed for mm mgmt. So, grub_mm_add_region_fn() can be called twice to fulfill request or memory addition may fail. The "mm: Preallocate some space when adding new regions" patch extends the region size further to not call firmware too often to fulfill small allocations. Due to that I think your series should be merged into master together with Zhang one. I hope he will repost updated mm series soon.
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
grub-core/kern/ieee1275/init.c +6
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"fallback for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"fallback from fw %s\n" , total ==0 ?"succeeded" :"failed" );Quoted 8 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 415
Quoted 6 lines
grub-core/kern/ieee1275/init.c +6
-0+ } + } +else + { +/* provide padding for a grub_mm_header_t and region */ + total = size +48 ;Ditto... And then probably total can be dropped too...
Quoted 1 line
Don't we need a parameter to
grub_machine_mmap_iterate(region_claim, &total) ?
I think you can use size variable instead of total then.
Daniel
Re: [PATCH v2 3/6] ieee1275: support runtime memory claiming
Hi,
Quoted 53 lines · Daniel Kiper
Adding Zhang...
Quoted 32 lines · Stefan Berger
Quoted 20 lines · Daniel Kiper
Quoted 11 lines · 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.grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 392
Quoted 5 lines
grub-core/kern/ieee1275/init.c +5
-0+ +if (flags &GRUB_MM_ADD_REGION_CONSECUTIVE ) + { +/* first try rounding up hard for the sake of speed */ + total =grub_max (ALIGN_UP (size,1024 *1024 ) +1024 *1024 ,32 *1024 *1024 );s/ALIGN_UP(/ALIGN_UP (/
Quoted 10 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 400
Quoted 8 lines
grub-core/kern/ieee1275/init.c +8
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"looking for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"get memory from fw %s\n" , total ==0 ?"succeeded" :"failed" ); + +/* fallback: requested size + padding for a grub_mm_header_t and region */ This...
Quoted 1 line
With 'this' you mean the comment above or which exact part?
Yes, exactly...
Quoted 7 lines
Quoted 5 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 403
Quoted 3 lines
grub-core/kern/ieee1275/init.c +3
-0+if (total !=0 ) + { + total = size +48 ;... and this should be dropped. I think this patch set, [1], [2], solves
You mean adding 48 to it can be dropped?
Yep... Sorry for being imprecise...
Quoted 3 lines
Quoted 1 line
this kinds of problems in general. Please take a look...
I looked at them and I don't know how they are related.
Quoted 9 lines
If I do not miss anything your patch set does not take into account size needed for
*grub_mm_region_tand*grub_mm_headerstructs. They are needed for mm mgmt. So,grub_mm_add_region_fn()can be called twice to fulfill request or memory addition may fail. The "mm: Preallocate some space when adding new regions" patch extends the region size further to not call firmware too often to fulfill small allocations. Due to that I think your series should be merged into master together with Zhang one. I hope he will repost updated mm series soon.
I also think 48 can be removed, if 48 means sizeof(grub_mm_region)+sizeof(grub_mm_header). With my patchset, the "size" passed to grub_mm_add_region_fn() will include these overhead and there is no need to add them in arch specific code.
Best Regards,
Zhang Boyang
Quoted 20 lines
Quoted 15 lines
Quoted 6 lines
Quoted 6 lines
grub-core/kern/ieee1275/init.c +6
-0+ total =grub_min (free_memory -RUNTIME_MIN_SPACE , total); + +grub_dprintf ("ieee1275" ,"fallback for %x bytes of memory (%x requested)\n" , total, size); + +grub_machine_mmap_iterate (region_claim, &total); +grub_dprintf ("ieee1275" ,"fallback from fw %s\n" , total ==0 ?"succeeded" :"failed" );Quoted 8 lines
grub-core/kern/ieee1275/init.c heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type, · line 415
Quoted 6 lines
grub-core/kern/ieee1275/init.c +6
-0+ } + } +else + { +/* provide padding for a grub_mm_header_t and region */ + total = size +48 ;Ditto... And then probably total can be dropped too...
Quoted 1 line
Don't we need a parameter to
grub_machine_mmap_iterate(region_claim, &total) ?I think you can use size variable instead of total then.
Daniel
PATCH v2 4/6 ieee1275: implement vec5 for cas negotiation
In reply to Stefan BergerFrom: 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
@@ -498,6 +498,19 @@ struct option_vector2grub_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;
@@ -519,6 +532,8 @@ struct cas_vectorgrub_uint16_t vec3;grub_uint8_t vec4_size;grub_uint16_t vec4; +grub_uint8_t vec5_size; +struct option_vector5 vec5; }GRUB_PACKED ;/*
@@ -543,7 +558,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 ,
@@ -554,6 +569,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
Re: [PATCH v2 4/6] ieee1275: implement vec5 for cas negotiation
Quoted 6 lines · 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.
Quoted 6 lines
This patch implements the vector 5 and set the MAX CPUs to 256 while setting the others values to 0 (default).
Signed-off-by Diego Domingos <[email protected]>Acked-by Daniel Axtens <[email protected]>Signed-off-by Stefan Berger <[email protected]>
Daniel
PATCH v2 5/6 Add memtool module with memory allocation stress-test
In reply to Stefan BergerFrom: 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
@@ -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
@@ -2542,3 +2542,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
@@ -664,6 +664,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
@@ -691,6 +693,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
Re: [PATCH v2 5/6] Add memtool module with memory allocation stress-test
Quoted 10 lines · 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
Quoted 8 lines
*
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 + a100kB alloc.Signed-off-by Daniel Axtens <[email protected]>Signed-off-by Stefan Berger <[email protected]>
Daniel
PATCH v2 6/6 ibmvtpm: Add support for trusted boot using a vTPM 2.0
In reply to Stefan BergerAdd 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.
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 | 151 ++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 grub-core/commands/ieee1275/ibmvtpm.c
docs/grub.texi +2 -1
@@ -6413,7 +6413,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
@@ -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 +151 -0
151 lines not on this page · raw message
include/grub/ieee1275/ieee1275.h +3 -0
@@ -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
Re: [PATCH v2 6/6] ibmvtpm: Add support for trusted boot using a vTPM 2.0
Quoted 194 lines · 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_64platform does.This patch requires Daniel Axtens's patches for claiming more memory.
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.
Cc Eric Snowberg <[email protected]>Signed-off-by Stefan Berger <[email protected]>Signed-off-by Daniel Axtens <[email protected]>--- docs/grub.texi | 3 +- grub-core/Makefile.core.def | 7 ++ grub-core/commands/ieee1275/ibmvtpm.c | 151 ++++++++++++++++++++++++++ include/grub/ieee1275/ieee1275.h | 3 + 4 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 grub-core/commands/ieee1275/ibmvtpm.cdocs/grub.texi +2
-1@@ -6413,7 +6413,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 setupgrub-core/Makefile.core.def +7
-0@@ -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 +128
-0@@ -0,0 +1,151 @@ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2022 Free Software Foundation, Inc. +* Copyright (C) 2022 IBM Corporation +* +* 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/>. +* +* IBM vTPM support code. +*/ + +#include <grub/err.h> +#include <grub/types.h> +#include <grub/tpm.h> +#include <grub/ieee1275/ieee1275.h> +#include <grub/mm.h> +#include <grub/misc.h> + +static grub_ieee1275_ihandle_t tpm_ihandle; +static grub_uint8_t tpm_version; + +#define IEEE1275_IHANDLE_INVALID ((grub_ieee1275_ihandle_t) 0) + +static void +tpm_get_tpm_version (void ) +{ +grub_ieee1275_phandle_t vtpm; +char buffer[20 ]; + +if (!grub_ieee1275_finddevice ("/vdevice/vtpm" , &vtpm) && + !grub_ieee1275_get_property (vtpm,"compatible" , buffer, +sizeof (buffer),NULL ) && + !grub_strcmp (buffer,"IBM,vtpm20" )) + tpm_version =2 ; +} + +static grub_err_t +tpm_init (void ) +{ +static int init_success =0 ; + +if (!init_success) + { +if (grub_ieee1275_open ("/vdevice/vtpm" , &tpm_ihandle) <0 ) + { + tpm_ihandle =IEEE1275_IHANDLE_INVALID ; +return GRUB_ERR_UNKNOWN_DEVICE ; + } + + init_success =1 ; + +tpm_get_tpm_version (); + } + +return GRUB_ERR_NONE ; +} + +static int +ibmvtpm_2hash_ext_log (grub_uint8_t pcrindex, +grub_uint32_t eventtype, +const char *description, +grub_size_t description_size, +void *buf,grub_size_t size) +{ +struct tpm_2hash_ext_log + { +struct grub_ieee1275_common_hdr common; +grub_ieee1275_cell_t method; +grub_ieee1275_cell_t ihandle; +grub_ieee1275_cell_t size; +grub_ieee1275_cell_t buf; +grub_ieee1275_cell_t description_size; +grub_ieee1275_cell_t description; +grub_ieee1275_cell_t eventtype; +grub_ieee1275_cell_t pcrindex; +grub_ieee1275_cell_t catch_result; +grub_ieee1275_cell_t rc; + }; +struct tpm_2hash_ext_log args; + +INIT_IEEE1275_COMMON (&args.common,"call-method" ,8 ,2 ); + args.method = (grub_ieee1275_cell_t )"2hash-ext-log" ; + args.ihandle = tpm_ihandle; + args.pcrindex = pcrindex; + args.eventtype = eventtype; + args.description = (grub_ieee1275_cell_t ) description; + args.description_size = description_size; + args.buf = (grub_ieee1275_cell_t ) buf; + args.size = (grub_ieee1275_cell_t ) size; + +if (IEEE1275_CALL_ENTRY_FN (&args) ==-1 ) +return -1 ; + +/* +* catch_result is set if firmware does not support 2hash-ext-log +* rc is GRUB_IEEE1275_CELL_FALSE (0) on failure +*/ +if ((args.catch_result) || args.rc ==GRUB_IEEE1275_CELL_FALSE ) +return -1 ; + +return 0 ; +} + +static grub_err_t +tpm2_log_event (unsigned char *buf,grub_size_t size,grub_uint8_t pcr, +const char *description) +{ +static int error_displayed =0 ; +int rc; + + rc =ibmvtpm_2hash_ext_log (pcr,EV_IPL , + description,grub_strlen (description) +1 , + buf, size); +if (rc && !error_displayed) + { + error_displayed++; +return grub_error (GRUB_ERR_BAD_DEVICE , +"2HASH-EXT-LOG failed: Firmware is likely too old.\n" );
grub-core/commands/ieee1275/ibmvtpm.c line 138
Quoted 10 lines
grub-core/commands/ieee1275/ibmvtpm.c new file +10
-0+ } + +return GRUB_ERR_NONE ; +} + +grub_err_t +grub_tpm_measure (unsigned char *buf,grub_size_t size,grub_uint8_t pcr, +const char *description) +{ +grub_err_t err =tpm_init ();
This should happen on module load. Then code in tpm_init() and here should be much simpler.
grub-core/commands/ieee1275/ibmvtpm.c line 151
Quoted 12 lines
grub-core/commands/ieee1275/ibmvtpm.c new file +12
-0+/* Absence of a TPM isn't a failure. */ +if (err !=GRUB_ERR_NONE ) +return GRUB_ERR_NONE ; + +grub_dprintf ("tpm" ,"log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE", %s\n" , + pcr, size, description); + +if (tpm_version ==2 ) +return tpm2_log_event (buf, size, pcr, description); + +return GRUB_ERR_NONE ; +}
Daniel
Re: [PATCH v2 6/6] ibmvtpm: Add support for trusted boot using a vTPM 2.0
Quoted 7 lines · Daniel Kiper
Quoted 5 lines · 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_64platform does.
Quoted 10 lines
grub-core/commands/ieee1275/ibmvtpm.c line 138
Quoted 7 lines
grub-core/commands/ieee1275/ibmvtpm.c new file +7
-0+} + +grub_err_t +grub_tpm_measure (unsigned char *buf,grub_size_t size,grub_uint8_t pcr, +const char *description) +{ +grub_err_t err =tpm_init ();This should happen on module load. Then code in
tpm_init()and here should be much simpler.
I tried moving this into GRUB_MOD_INIT() but at that point it doesn't succeed to find the device it seems. I have to repeat the tpm_init () call then later on in this function here when an actual measurement is to be done and then it is able to find the device. I'd rather leave it as-is now.
It looked like this:
GRUB_MOD_INIT (ibmvtpm)
{
init_success = tpm_init ();
}
GRUB_MOD_FINI (ibmvtpm)
{
if (tpm_ihandle != IEEE1275_IHANDLE_INVALID)
grub_ieee1275_close (tpm_ihandle); }
Stefan
Quoted 15 lines
grub-core/commands/ieee1275/ibmvtpm.c line 151
Quoted 13 lines
grub-core/commands/ieee1275/ibmvtpm.c new file +12
-0+/* Absence of a TPM isn't a failure. */ +if (err !=GRUB_ERR_NONE ) +return GRUB_ERR_NONE ; + +grub_dprintf ("tpm" ,"log_event, pcr = %d, size = 0x%" PRIxGRUB_SIZE", %s\n" , + pcr, size, description); + +if (tpm_version ==2 ) +return tpm2_log_event (buf, size, pcr, description); + +return GRUB_ERR_NONE ; +}Daniel
Re: [PATCH v2 6/6] ibmvtpm: Add support for trusted boot using a vTPM 2.0
Quoted 17 lines · Stefan Berger
Quoted 15 lines · Daniel Kiper
grub-core/commands/ieee1275/ibmvtpm.c line 138
Quoted 11 lines · 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_64platform does.grub-core/commands/ieee1275/ibmvtpm.c new file +7
-0+} + +grub_err_t +grub_tpm_measure (unsigned char *buf,grub_size_t size,grub_uint8_t pcr, +const char *description) +{ +grub_err_t err =tpm_init ();This should happen on module load. Then code in
tpm_init()and here should be much simpler.
Quoted 5 lines
I tried moving this into
GRUB_MOD_INIT()but at that point it doesn't succeed to find the device it seems. I have to repeat thetpm_init() call then later on in this function here when an actual measurement is to be done and then it is able to find the device. I'd rather leave it as-is now.
Hmmm... It looks like a bug in a firmware or initial TPM communication fails/timeouts for some reason. Do not you loose some initial measurements this way? Could you check what will happen when you call tpm_init() from GRUB_MOD_INIT 10, 50, 100 times?
Quoted 7 lines
It looked like this:
GRUB_MOD_INIT(ibmvtpm)
{
init_success=tpm_init();
}
Quoted 5 lines
GRUB_MOD_FINI(ibmvtpm)
{
if (tpm_ihandle!=IEEE1275_IHANDLE_INVALID)grub_ieee1275_close (tpm_ihandle); }
LGTM... Weird...
Daniel
Re: [PATCH v2 6/6] ibmvtpm: Add support for trusted boot using a vTPM 2.0
Quoted 18 lines · Daniel Kiper
Quoted 17 lines · Stefan Berger
Quoted 15 lines · Daniel Kiper
grub-core/commands/ieee1275/ibmvtpm.c line 138
Quoted 11 lines · 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_64platform does.grub-core/commands/ieee1275/ibmvtpm.c new file +7
-0+} + +grub_err_t +grub_tpm_measure (unsigned char *buf,grub_size_t size,grub_uint8_t pcr, +const char *description) +{ +grub_err_t err =tpm_init ();This should happen on module load. Then code in
tpm_init()and here should be much simpler.
Quoted 8 lines
Quoted 5 lines
I tried moving this into
GRUB_MOD_INIT()but at that point it doesn't succeed to find the device it seems. I have to repeat thetpm_init() call then later on in this function here when an actual measurement is to be done and then it is able to find the device. I'd rather leave it as-is now.Hmmm... It looks like a bug in a firmware or initial TPM communication fails/timeouts for some reason. Do not you loose some initial
tpm_init() , which also calls tpm_get_tpm_version(), don't even talk to the TPM but they only access the OpenFirmware device tree and don't seem to find what they are looking for when run that early.
Quoted 1 line
measurements this way? Could you check what will happen when you call
No measurements are lost from what I can tell. The late initialization seems to find the OF node it needs.
Quoted 1 line
tpm_init()fromGRUB_MOD_INIT10, 50, 100 times?
I tried it now with this loop here. Well, not unexpected but the loop doesn't change anything in this case since no timing is involved -- 'found', which I print later on, stays at 0. Something related to the OpenFirmware tree seems to not be initialized at this point.
GRUB_MOD_INIT (ibmvtpm)
{
grub_ieee1275_phandle_t vtpm;
int i;
for (i = 0; i < 100; i++) {
if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm))
found++; }
}
Stefan
Quoted 17 lines
Quoted 8 lines
It looked like this:
GRUB_MOD_INIT(ibmvtpm){ init_success = tpm_init (); }Quoted 5 lines
GRUB_MOD_FINI(ibmvtpm){ if (tpm_ihandle != IEEE1275_IHANDLE_INVALID) grub_ieee1275_close (tpm_ihandle); }LGTM... Weird...
Daniel
Re: [PATCH v2 6/6] ibmvtpm: Add support for trusted boot using a vTPM 2.0
Quoted 48 lines · Stefan Berger
Quoted 18 lines · Daniel Kiper
Quoted 17 lines · Stefan Berger
Quoted 15 lines · Daniel Kiper
grub-core/commands/ieee1275/ibmvtpm.c line 138
Quoted 11 lines · 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_64platform does.grub-core/commands/ieee1275/ibmvtpm.c new file +7
-0+} + +grub_err_t +grub_tpm_measure (unsigned char *buf,grub_size_t size,grub_uint8_t pcr, +const char *description) +{ +grub_err_t err =tpm_init ();This should happen on module load. Then code in
tpm_init()and here should be much simpler.Quoted 8 lines
Quoted 5 lines
I tried moving this into
GRUB_MOD_INIT()but at that point it doesn't succeed to find the device it seems. I have to repeat thetpm_init() call then later on in this function here when an actual measurement is to be done and then it is able to find the device. I'd rather leave it as-is now.Hmmm... It looks like a bug in a firmware or initial TPM communication fails/timeouts for some reason. Do not you loose some initial
tpm_init(), which also callstpm_get_tpm_version(), don't even talk to the TPM but they only access the OpenFirmware device tree and don't seem to find what they are looking for when run that early.Quoted 1 line
measurements this way? Could you check what will happen when you call
No measurements are lost from what I can tell. The late initialization seems to find the OF node it needs.
Quoted 1 line
tpm_init()fromGRUB_MOD_INIT10, 50, 100 times?I tried it now with this loop here. Well, not unexpected but the loop doesn't change anything in this case since no timing is involved -- 'found', which I print later on, stays at 0. Something related to the OpenFirmware tree seems to not be initialized at this point.
GRUB_MOD_INIT(ibmvtpm)
{
grub_ieee1275_phandle_tvtpm;
int i;
Quoted 5 lines
for (i = 0; i < 100; i++) { if (!grub_ieee1275_finddevice ("/vdevice/vtpm", &vtpm)) found++;}
}
Huh! Seems like a bug. Of course I would prefer GRUB_MOD_INIT() approach but as I can see EFI version does not have it either. So, due to the potential bug in the firmware and to be inline with existing EFI code let's leave it as is. Though please add a sentence or two to the commit message and make a comment before tpm_init() why we cannot use GRUB_MOD_INIT() approach (due to the bug). If you do that feel free to add my RB to this patch.
Daniel