Skip to content

Commit 51bfafd

Browse files
JasonChenCJwenlingz
authored andcommitted
modularization: boot component -- move functions
Boot component prepares the very basic platform boot env. It finally call into platform initilization entries: - bsp_boot_init & cpu_secondary_init for start up - or restore_s3_context for wakeup this patch move functions for AP trampoline into trampoline.c from reloc.c Tracked-On: #1842 Signed-off-by: Jason Chen CJ <jason.cj.chen@intel.com>
1 parent 512dbb6 commit 51bfafd

File tree

7 files changed

+153
-133
lines changed

7 files changed

+153
-133
lines changed

hypervisor/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ C_SRCS += arch/x86/mtrr.c
157157
C_SRCS += arch/x86/pm.c
158158
S_SRCS += arch/x86/wakeup.S
159159
C_SRCS += arch/x86/static_checks.c
160+
C_SRCS += arch/x86/trampoline.c
160161
C_SRCS += arch/x86/guest/vcpu.c
161162
C_SRCS += arch/x86/guest/vm.c
162163
C_SRCS += arch/x86/guest/vlapic.c

hypervisor/arch/x86/cpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <hypervisor.h>
88
#include <schedule.h>
99
#include <version.h>
10-
#include <reloc.h>
10+
#include <trampoline.h>
1111

1212
spinlock_t trampoline_spinlock = {
1313
.head = 0U,

hypervisor/arch/x86/pm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: BSD-3-Clause
44
*/
55
#include <hypervisor.h>
6-
#include <reloc.h>
6+
#include <trampoline.h>
77

88
struct cpu_context cpu_ctx;
99

hypervisor/arch/x86/trampoline.c

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (C) 2018 Intel Corporation. All rights reserved.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
#include <hypervisor.h>
7+
#include <reloc.h>
8+
#include <trampoline.h>
9+
#include <vm0_boot.h>
10+
11+
uint64_t trampoline_start16_paddr;
12+
13+
/*
14+
* Because trampoline code is relocated in different way, if HV code
15+
* accesses trampoline using relative addressing, it needs to take
16+
* out the HV relocation delta
17+
*
18+
* This function is valid if:
19+
* - The hpa of HV code is always higher than trampoline code
20+
* - The HV code is always relocated to higher address, compared
21+
* with CONFIG_HV_RAM_START
22+
*/
23+
static uint64_t trampoline_relo_addr(const void *addr)
24+
{
25+
return (uint64_t)addr - get_hv_image_delta();
26+
}
27+
28+
uint64_t read_trampoline_sym(const void *sym)
29+
{
30+
uint64_t *hva;
31+
32+
hva = hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(sym);
33+
return *hva;
34+
}
35+
36+
void write_trampoline_sym(const void *sym, uint64_t val)
37+
{
38+
uint64_t *hva;
39+
40+
hva = hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(sym);
41+
*hva = val;
42+
clflush(hva);
43+
}
44+
45+
static void update_trampoline_code_refs(uint64_t dest_pa)
46+
{
47+
void *ptr;
48+
uint64_t val;
49+
int i;
50+
51+
/*
52+
* calculate the fixup CS:IP according to fixup target address
53+
* dynamically.
54+
*
55+
* trampoline code starts in real mode,
56+
* so the target addres is HPA
57+
*/
58+
val = dest_pa + trampoline_relo_addr(&trampoline_fixup_target);
59+
60+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_fixup_cs));
61+
*(uint16_t *)(ptr) = (uint16_t)((val >> 4U) & 0xFFFFU);
62+
63+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_fixup_ip));
64+
*(uint16_t *)(ptr) = (uint16_t)(val & 0xfU);
65+
66+
/* Update temporary page tables */
67+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&cpu_boot_page_tables_ptr));
68+
*(uint32_t *)(ptr) += (uint32_t)dest_pa;
69+
70+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&cpu_boot_page_tables_start));
71+
*(uint64_t *)(ptr) += dest_pa;
72+
73+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_pdpt_addr));
74+
for (i = 0; i < 4; i++) {
75+
*(uint64_t *)(ptr + sizeof(uint64_t) * i) += dest_pa;
76+
}
77+
78+
/* update the gdt base pointer with relocated offset */
79+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_gdt_ptr));
80+
*(uint64_t *)(ptr + 2) += dest_pa;
81+
82+
/* update trampoline jump pointer with relocated offset */
83+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_start64_fixup));
84+
*(uint32_t *)ptr += (uint32_t)dest_pa;
85+
86+
/* update trampoline's main entry pointer */
87+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(main_entry));
88+
*(uint64_t *)ptr += get_hv_image_delta();
89+
90+
/* update trampoline's spinlock pointer */
91+
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_spinlock_ptr));
92+
*(uint64_t *)ptr += get_hv_image_delta();
93+
}
94+
95+
uint64_t prepare_trampoline(void)
96+
{
97+
uint64_t size, dest_pa, i;
98+
99+
size = (uint64_t)(&ld_trampoline_end - &ld_trampoline_start);
100+
#ifndef CONFIG_EFI_STUB
101+
dest_pa = e820_alloc_low_memory(CONFIG_LOW_RAM_SIZE);
102+
#else
103+
dest_pa = (uint64_t)get_ap_trampoline_buf();
104+
#endif
105+
106+
pr_dbg("trampoline code: %llx size %x", dest_pa, size);
107+
108+
/* Copy segment for AP initialization code below 1MB */
109+
(void)memcpy_s(hpa2hva(dest_pa), (size_t)size, &ld_trampoline_load,
110+
(size_t)size);
111+
update_trampoline_code_refs(dest_pa);
112+
113+
for (i = 0UL; i < size; i = i + CACHE_LINE_SIZE) {
114+
clflush(hpa2hva(dest_pa + i));
115+
}
116+
117+
trampoline_start16_paddr = dest_pa;
118+
119+
return dest_pa;
120+
}

hypervisor/boot/include/reloc.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,12 @@
99
extern void relocate(void);
1010
extern uint64_t get_hv_image_delta(void);
1111
extern uint64_t get_hv_image_base(void);
12-
extern uint64_t read_trampoline_sym(const void *sym);
13-
extern void write_trampoline_sym(const void *sym, uint64_t val);
14-
extern uint64_t prepare_trampoline(void);
1512

1613
/* external symbols that are helpful for relocation */
1714
extern uint8_t _DYNAMIC[1];
18-
extern const uint8_t ld_trampoline_load;
19-
extern uint8_t ld_trampoline_start;
2015
extern uint8_t ld_trampoline_end;
21-
extern const uint64_t ld_trampoline_size;
2216

2317
extern uint8_t cpu_primary_start_32;
2418
extern uint8_t cpu_primary_start_64;
2519

26-
extern uint8_t trampoline_fixup_cs;
27-
extern uint8_t trampoline_fixup_ip;
28-
extern uint8_t trampoline_fixup_target;
29-
extern uint8_t cpu_boot_page_tables_start;
30-
extern uint8_t cpu_boot_page_tables_ptr;
31-
extern uint8_t trampoline_pdpt_addr;
32-
extern uint8_t trampoline_gdt_ptr;
33-
extern uint8_t trampoline_start64_fixup;
34-
extern uint8_t trampoline_spinlock_ptr;
35-
36-
extern uint64_t trampoline_start16_paddr;
37-
3820
#endif /* RELOCATE_H */

hypervisor/boot/reloc.c

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
* SPDX-License-Identifier: BSD-3-Clause
55
*/
66

7-
#include <hypervisor.h>
7+
#include <types.h>
88
#include <reloc.h>
9-
#include <vm0_boot.h>
109

1110
#ifdef CONFIG_RELOC
1211
#define DT_NULL 0 /* end of .dynamic section */
@@ -33,8 +32,6 @@ static inline uint64_t elf64_r_type(uint64_t i)
3332

3433
#define R_X86_64_RELATIVE 8UL
3534

36-
uint64_t trampoline_start16_paddr;
37-
3835
/* get the delta between CONFIG_HV_RAM_START and the actual load address */
3936
uint64_t get_hv_image_delta(void)
4037
{
@@ -57,21 +54,6 @@ uint64_t get_hv_image_base(void)
5754
return (get_hv_image_delta() + CONFIG_HV_RAM_START);
5855
}
5956

60-
/*
61-
* Because trampoline code is relocated in different way, if HV code
62-
* accesses trampoline using relative addressing, it needs to take
63-
* out the HV relocation delta
64-
*
65-
* This function is valid if:
66-
* - The hpa of HV code is always higher than trampoline code
67-
* - The HV code is always relocated to higher address, compared
68-
* with CONFIG_HV_RAM_START
69-
*/
70-
static uint64_t trampoline_relo_addr(const void *addr)
71-
{
72-
return (uint64_t)addr - get_hv_image_delta();
73-
}
74-
7557
void relocate(void)
7658
{
7759
#ifdef CONFIG_RELOC
@@ -144,97 +126,3 @@ void relocate(void)
144126
}
145127
#endif
146128
}
147-
148-
uint64_t read_trampoline_sym(const void *sym)
149-
{
150-
uint64_t *hva;
151-
152-
hva = hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(sym);
153-
return *hva;
154-
}
155-
156-
void write_trampoline_sym(const void *sym, uint64_t val)
157-
{
158-
uint64_t *hva;
159-
160-
hva = hpa2hva(trampoline_start16_paddr) + trampoline_relo_addr(sym);
161-
*hva = val;
162-
clflush(hva);
163-
}
164-
165-
static void update_trampoline_code_refs(uint64_t dest_pa)
166-
{
167-
void *ptr;
168-
uint64_t val;
169-
int i;
170-
171-
/*
172-
* calculate the fixup CS:IP according to fixup target address
173-
* dynamically.
174-
*
175-
* trampoline code starts in real mode,
176-
* so the target addres is HPA
177-
*/
178-
val = dest_pa + trampoline_relo_addr(&trampoline_fixup_target);
179-
180-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_fixup_cs));
181-
*(uint16_t *)(ptr) = (uint16_t)((val >> 4U) & 0xFFFFU);
182-
183-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_fixup_ip));
184-
*(uint16_t *)(ptr) = (uint16_t)(val & 0xfU);
185-
186-
/* Update temporary page tables */
187-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&cpu_boot_page_tables_ptr));
188-
*(uint32_t *)(ptr) += (uint32_t)dest_pa;
189-
190-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&cpu_boot_page_tables_start));
191-
*(uint64_t *)(ptr) += dest_pa;
192-
193-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_pdpt_addr));
194-
for (i = 0; i < 4; i++) {
195-
*(uint64_t *)(ptr + sizeof(uint64_t) * i) += dest_pa;
196-
}
197-
198-
/* update the gdt base pointer with relocated offset */
199-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_gdt_ptr));
200-
*(uint64_t *)(ptr + 2) += dest_pa;
201-
202-
/* update trampoline jump pointer with relocated offset */
203-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_start64_fixup));
204-
*(uint32_t *)ptr += (uint32_t)dest_pa;
205-
206-
/* update trampoline's main entry pointer */
207-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(main_entry));
208-
*(uint64_t *)ptr += get_hv_image_delta();
209-
210-
/* update trampoline's spinlock pointer */
211-
ptr = hpa2hva(dest_pa + trampoline_relo_addr(&trampoline_spinlock_ptr));
212-
*(uint64_t *)ptr += get_hv_image_delta();
213-
}
214-
215-
uint64_t prepare_trampoline(void)
216-
{
217-
uint64_t size, dest_pa, i;
218-
219-
size = (uint64_t)(&ld_trampoline_end - &ld_trampoline_start);
220-
#ifndef CONFIG_EFI_STUB
221-
dest_pa = e820_alloc_low_memory(CONFIG_LOW_RAM_SIZE);
222-
#else
223-
dest_pa = (uint64_t)get_ap_trampoline_buf();
224-
#endif
225-
226-
pr_dbg("trampoline code: %llx size %x", dest_pa, size);
227-
228-
/* Copy segment for AP initialization code below 1MB */
229-
(void)memcpy_s(hpa2hva(dest_pa), (size_t)size, &ld_trampoline_load,
230-
(size_t)size);
231-
update_trampoline_code_refs(dest_pa);
232-
233-
for (i = 0UL; i < size; i = i + CACHE_LINE_SIZE) {
234-
clflush(hpa2hva(dest_pa + i));
235-
}
236-
237-
trampoline_start16_paddr = dest_pa;
238-
239-
return dest_pa;
240-
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) <2018> Intel Corporation
3+
* SPDX-License-Identifier: BSD-3-Clause
4+
*/
5+
6+
#ifndef TRAMPOLINE_H
7+
#define TRAMPOLINE_H
8+
9+
extern uint64_t read_trampoline_sym(const void *sym);
10+
extern void write_trampoline_sym(const void *sym, uint64_t val);
11+
extern uint64_t prepare_trampoline(void);
12+
13+
/* external symbols that are helpful for relocation */
14+
extern const uint8_t ld_trampoline_load;
15+
extern uint8_t ld_trampoline_start;
16+
17+
extern uint8_t trampoline_fixup_cs;
18+
extern uint8_t trampoline_fixup_ip;
19+
extern uint8_t trampoline_fixup_target;
20+
extern uint8_t cpu_boot_page_tables_start;
21+
extern uint8_t cpu_boot_page_tables_ptr;
22+
extern uint8_t trampoline_pdpt_addr;
23+
extern uint8_t trampoline_gdt_ptr;
24+
extern uint8_t trampoline_start64_fixup;
25+
extern uint8_t trampoline_spinlock_ptr;
26+
27+
extern uint64_t trampoline_start16_paddr;
28+
29+
#endif /* TRAMPOLINE_H */

0 commit comments

Comments
 (0)