Skip to content

Commit 124910b

Browse files
zhenggenjren1
authored andcommitted
vm load: fix bug in loading kernel
According to the explaination for pref_address in Documentation/x86/boot.txt, a relocating bootloader should attempt to load kernel at pref_address if possible. But due to a non-relocatable kernel will unconditionally move itself and to run at perf address, no need to copy kernel to perf_address by bootloader. Signed-off-by: Zheng, Gen <gen.zheng@intel.com>
1 parent b6d73be commit 124910b

File tree

4 files changed

+87
-27
lines changed

4 files changed

+87
-27
lines changed

hypervisor/arch/x86/guest/guest.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <acrn_hv_defs.h>
3838
#include <hv_debug.h>
3939
#include <multiboot.h>
40+
#include <zeropage.h>
4041

4142
#define ACRN_DBG_GUEST 6
4243

hypervisor/boot/multiboot.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <acrn_hv_defs.h>
3838
#include <hv_debug.h>
3939
#include <multiboot.h>
40+
#include <zeropage.h>
4041

4142
#define BOOT_ARGS_LOAD_ADDR 0x24EFC000
4243

@@ -102,6 +103,25 @@ static void parse_other_modules(struct vm *vm,
102103
}
103104
}
104105

106+
static void *get_kernel_load_addr(void *kernel_src_addr)
107+
{
108+
struct zero_page *zeropage;
109+
110+
/* According to the explaination for pref_address
111+
* in Documentation/x86/boot.txt, a relocating
112+
* bootloader should attempt to load kernel at pref_address
113+
* if possible. A non-relocatable kernel will unconditionally
114+
* move itself and to run at this address, so no need to copy
115+
* kernel to perf_address by bootloader, if kernel is
116+
* non-relocatable.
117+
*/
118+
zeropage = (struct zero_page *)kernel_src_addr;
119+
if (zeropage->hdr.relocatable_kernel)
120+
return (void *)zeropage->hdr.pref_addr;
121+
122+
return kernel_src_addr;
123+
}
124+
105125
int init_vm0_boot_info(struct vm *vm)
106126
{
107127
struct multiboot_module *mods = NULL;
@@ -141,7 +161,7 @@ int init_vm0_boot_info(struct vm *vm)
141161
vm->sw.kernel_info.kernel_size =
142162
mods[0].mm_mod_end - mods[0].mm_mod_start;
143163
vm->sw.kernel_info.kernel_load_addr =
144-
(void *)(uint64_t)mods[0].mm_mod_start;
164+
get_kernel_load_addr(vm->sw.kernel_info.kernel_src_addr);
145165

146166
vm->sw.linux_info.bootargs_src_addr =
147167
(void *)(uint64_t)mods[0].mm_string;

hypervisor/common/vm_load.c

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,32 +34,7 @@
3434
#include <hv_arch.h>
3535
#include <bsp_extern.h>
3636
#include <hv_debug.h>
37-
38-
struct zero_page {
39-
uint8_t pad1[0x1e8]; /* 0x000 */
40-
uint8_t e820_nentries; /* 0x1e8 */
41-
uint8_t pad2[0x8]; /* 0x1e9 */
42-
43-
struct {
44-
uint8_t setup_sects; /* 0x1f1 */
45-
uint8_t hdr_pad1[0x1e]; /* 0x1f2 */
46-
uint8_t loader_type; /* 0x210 */
47-
uint8_t load_flags; /* 0x211 */
48-
uint8_t hdr_pad2[0x6]; /* 0x212 */
49-
uint32_t ramdisk_addr; /* 0x218 */
50-
uint32_t ramdisk_size; /* 0x21c */
51-
uint8_t hdr_pad3[0x8]; /* 0x220 */
52-
uint32_t bootargs_addr; /* 0x228 */
53-
uint8_t hdr_pad4[0x1c]; /* 0x22c */
54-
uint32_t payload_offset;/* 0x248 */
55-
uint32_t payload_length;/* 0x24c */
56-
uint8_t hdr_pad5[0x18]; /* 0x250 */
57-
} __packed hdr;
58-
59-
uint8_t pad3[0x68]; /* 0x268 */
60-
struct e820_entry e820[0x80]; /* 0x2d0 */
61-
uint8_t pad4[0x330]; /* 0xcd0 */
62-
} __packed;
37+
#include <zeropage.h>
6338

6439
static uint32_t create_e820_table(struct e820_entry *_e820)
6540
{
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions
6+
* are met:
7+
*
8+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
* * Neither the name of Intel Corporation nor the names of its
15+
* contributors may be used to endorse or promote products derived
16+
* from this software without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#ifndef ZEROPAGE_H
32+
#define ZEROPAGE_H
33+
34+
struct zero_page {
35+
uint8_t pad1[0x1e8]; /* 0x000 */
36+
uint8_t e820_nentries; /* 0x1e8 */
37+
uint8_t pad2[0x8]; /* 0x1e9 */
38+
39+
struct {
40+
uint8_t setup_sects; /* 0x1f1 */
41+
uint8_t hdr_pad1[0x1e]; /* 0x1f2 */
42+
uint8_t loader_type; /* 0x210 */
43+
uint8_t load_flags; /* 0x211 */
44+
uint8_t hdr_pad2[0x6]; /* 0x212 */
45+
uint32_t ramdisk_addr; /* 0x218 */
46+
uint32_t ramdisk_size; /* 0x21c */
47+
uint8_t hdr_pad3[0x8]; /* 0x220 */
48+
uint32_t bootargs_addr; /* 0x228 */
49+
uint8_t hdr_pad4[0x8]; /* 0x22c */
50+
uint8_t relocatable_kernel; /* 0x234 */
51+
uint8_t hdr_pad5[0x13]; /* 0x235 */
52+
uint32_t payload_offset;/* 0x248 */
53+
uint32_t payload_length;/* 0x24c */
54+
uint8_t hdr_pad6[0x8]; /* 0x250 */
55+
uint64_t pref_addr; /* 0x258 */
56+
uint8_t hdr_pad7[8]; /* 0x260 */
57+
} __packed hdr;
58+
59+
uint8_t pad3[0x68]; /* 0x268 */
60+
struct e820_entry e820[0x80]; /* 0x2d0 */
61+
uint8_t pad4[0x330]; /* 0xcd0 */
62+
} __packed;
63+
64+
#endif

0 commit comments

Comments
 (0)