Skip to content

Commit 1657544

Browse files
peterfangNanlinXie
authored andcommitted
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 <peter.fang@intel.com> Acked-by: Anthony Xu <anthony.xu@intel.com>
1 parent 373e79b commit 1657544

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

devicemodel/hw/platform/rtc.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@ int
11471147
vrtc_init(struct vmctx *ctx)
11481148
{
11491149
struct vrtc *vrtc;
1150+
size_t lomem, himem;
1151+
int err;
11501152
struct rtcdev *rtc;
11511153
time_t curtime;
11521154
sigset_t mask;
@@ -1160,6 +1162,28 @@ vrtc_init(struct vmctx *ctx)
11601162

11611163
pthread_mutex_init(&vrtc->mtx, NULL);
11621164

1165+
/*
1166+
* Report guest memory size in nvram cells as required by UEFI.
1167+
* Little-endian encoding.
1168+
* 0x34/0x35 - 64KB chunks above 16MB, below 4GB
1169+
* 0x5b/0x5c/0x5d - 64KB chunks above 4GB
1170+
*/
1171+
lomem = vm_get_lowmem_size(ctx);
1172+
assert(lomem >= 16 * MB);
1173+
lomem = (lomem - 16 * MB) / (64 * KB);
1174+
err = vrtc_nvram_write(vrtc, RTC_LMEM_LSB, lomem);
1175+
assert(err == 0);
1176+
err = vrtc_nvram_write(vrtc, RTC_LMEM_MSB, lomem >> 8);
1177+
assert(err == 0);
1178+
1179+
himem = vm_get_highmem_size(ctx) / (64 * KB);
1180+
err = vrtc_nvram_write(vrtc, RTC_HMEM_LSB, himem);
1181+
assert(err == 0);
1182+
err = vrtc_nvram_write(vrtc, RTC_HMEM_SB, himem >> 8);
1183+
assert(err == 0);
1184+
err = vrtc_nvram_write(vrtc, RTC_HMEM_MSB, himem >> 16);
1185+
assert(err == 0);
1186+
11631187
memset(&rtc_addr, 0, sizeof(struct inout_port));
11641188
memset(&rtc_data, 0, sizeof(struct inout_port));
11651189
/*register io port handler for rtc addr*/

devicemodel/include/macros.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@
1313
#define _CONCAT_(a, b) a ## b
1414
#define __CONCAT(a, b) _CONCAT_(a, b)
1515

16+
#define KB (1024UL)
17+
#define MB (1024 * 1024UL)
18+
#define GB (1024 * 1024 * 1024UL)
19+
1620
#endif

devicemodel/include/rtc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333

3434
#define IO_RTC 0x070 /* RTC */
3535

36+
#define RTC_LMEM_LSB 0x34
37+
#define RTC_LMEM_MSB 0x35
38+
#define RTC_HMEM_LSB 0x5b
39+
#define RTC_HMEM_SB 0x5c
40+
#define RTC_HMEM_MSB 0x5d
41+
3642
struct vrtc;
3743
struct vmctx;
3844

devicemodel/include/sw_load.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@
3030

3131
#define STR_LEN 1024
3232

33-
#define KB (1024UL)
34-
#define MB (1024 * 1024UL)
35-
#define GB (1024 * 1024 * 1024UL)
36-
3733
/* E820 memory types */
3834
#define E820_TYPE_RAM 1 /* EFI 1, 2, 3, 4, 5, 6, 7 */
3935
/* EFI 0, 11, 12, 13 (everything not used elsewhere) */

0 commit comments

Comments
 (0)