From 16575441523edc859b4b8ce0dc1464676ce0b9d7 Mon Sep 17 00:00:00 2001 From: Peter Fang Date: Tue, 25 Sep 2018 16:13:14 -0700 Subject: [PATCH] dm: vrtc: add memory configuration in RTC CMOS Some firmware (e.g. UEFI) uses RTC CMOS to fetch the system's memory configuration. Put lowmem / highmem info in the designated area. This is a port of Bhyve vRTC's user-space logic. v1 -> v2: * move KB/MB/GB to macros.h * move nvram offset definitions to rtc.h Tracked-On: #1390 Signed-off-by: Peter Fang Acked-by: Anthony Xu --- devicemodel/hw/platform/rtc.c | 24 ++++++++++++++++++++++++ devicemodel/include/macros.h | 4 ++++ devicemodel/include/rtc.h | 6 ++++++ devicemodel/include/sw_load.h | 4 ---- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/devicemodel/hw/platform/rtc.c b/devicemodel/hw/platform/rtc.c index f8e7dff32..0d424f9c0 100644 --- a/devicemodel/hw/platform/rtc.c +++ b/devicemodel/hw/platform/rtc.c @@ -1147,6 +1147,8 @@ int vrtc_init(struct vmctx *ctx) { struct vrtc *vrtc; + size_t lomem, himem; + int err; struct rtcdev *rtc; time_t curtime; sigset_t mask; @@ -1160,6 +1162,28 @@ vrtc_init(struct vmctx *ctx) pthread_mutex_init(&vrtc->mtx, NULL); + /* + * Report guest memory size in nvram cells as required by UEFI. + * Little-endian encoding. + * 0x34/0x35 - 64KB chunks above 16MB, below 4GB + * 0x5b/0x5c/0x5d - 64KB chunks above 4GB + */ + lomem = vm_get_lowmem_size(ctx); + assert(lomem >= 16 * MB); + lomem = (lomem - 16 * MB) / (64 * KB); + err = vrtc_nvram_write(vrtc, RTC_LMEM_LSB, lomem); + assert(err == 0); + err = vrtc_nvram_write(vrtc, RTC_LMEM_MSB, lomem >> 8); + assert(err == 0); + + himem = vm_get_highmem_size(ctx) / (64 * KB); + err = vrtc_nvram_write(vrtc, RTC_HMEM_LSB, himem); + assert(err == 0); + err = vrtc_nvram_write(vrtc, RTC_HMEM_SB, himem >> 8); + assert(err == 0); + err = vrtc_nvram_write(vrtc, RTC_HMEM_MSB, himem >> 16); + assert(err == 0); + memset(&rtc_addr, 0, sizeof(struct inout_port)); memset(&rtc_data, 0, sizeof(struct inout_port)); /*register io port handler for rtc addr*/ diff --git a/devicemodel/include/macros.h b/devicemodel/include/macros.h index effd83f93..35691e226 100644 --- a/devicemodel/include/macros.h +++ b/devicemodel/include/macros.h @@ -13,4 +13,8 @@ #define _CONCAT_(a, b) a ## b #define __CONCAT(a, b) _CONCAT_(a, b) +#define KB (1024UL) +#define MB (1024 * 1024UL) +#define GB (1024 * 1024 * 1024UL) + #endif diff --git a/devicemodel/include/rtc.h b/devicemodel/include/rtc.h index d6b8ff777..08bfdbb5e 100644 --- a/devicemodel/include/rtc.h +++ b/devicemodel/include/rtc.h @@ -33,6 +33,12 @@ #define IO_RTC 0x070 /* RTC */ +#define RTC_LMEM_LSB 0x34 +#define RTC_LMEM_MSB 0x35 +#define RTC_HMEM_LSB 0x5b +#define RTC_HMEM_SB 0x5c +#define RTC_HMEM_MSB 0x5d + struct vrtc; struct vmctx; diff --git a/devicemodel/include/sw_load.h b/devicemodel/include/sw_load.h index 3c2d9db1b..533cff448 100644 --- a/devicemodel/include/sw_load.h +++ b/devicemodel/include/sw_load.h @@ -30,10 +30,6 @@ #define STR_LEN 1024 -#define KB (1024UL) -#define MB (1024 * 1024UL) -#define GB (1024 * 1024 * 1024UL) - /* E820 memory types */ #define E820_TYPE_RAM 1 /* EFI 1, 2, 3, 4, 5, 6, 7 */ /* EFI 0, 11, 12, 13 (everything not used elsewhere) */