run_qemu.sh: Introduce KERNEL_PAD_SIZE

Name the magic constant 128*MiB and parametrize it to implement what the
comment above describes.

Change-Id: I04437cc2abab20b13764561eec226100e8be85cc
diff --git a/aarch64/run_qemu.sh b/aarch64/run_qemu.sh
index edcf5b9..d8dda2f 100755
--- a/aarch64/run_qemu.sh
+++ b/aarch64/run_qemu.sh
@@ -99,6 +99,10 @@
 	printf "%${2-#}x\n" $1
 }
 
+function min() {
+	echo $(( $1 < $2 ? $1 : $2 ))
+}
+
 function pvmfw_overlay() {
 	local addr=$(hex "$1")
 	local size=$(hex "$2")
@@ -238,9 +242,11 @@
 	inject_overlay <(pvmfw_overlay 0x0 0x0) "${QEMU_DTB}" >"${DTB}"
 
 	# 128MiB (or RAM/2 if RAM<256MiB) is left to the kernel to decompress.
+	KERNEL_PAD_SIZE=$(min $(( RAM * MiB / 2 )) $(( 128 * MiB )) )
 	# This is followed by the ramdisk and then 2MiB-aligned DTB.
+	KERNEL_PAD_SIZE=$(align_up_pow2 ${KERNEL_PAD_SIZE} $(( 2 * MiB )))
 	# We place the pvmfw at the following page.
-	PVMFW_ADDR=$(( KERNEL_ADDR + 128*MiB + $(file_size "${DTB}") ))
+	PVMFW_ADDR=$(( KERNEL_ADDR + KERNEL_PAD_SIZE + $(file_size "${DTB}") ))
 	PVMFW_ADDR=$(hex $(align_page_size ${PVMFW_ADDR}))
 	PVMFW_SIZE=$(file_size "${PVMFW}")
 	# pKVM enforces a page-aligned size for the memory slot