Skip to content

Commit 1c0a3d9

Browse files
fyin1lijinxia
authored andcommitted
hv: Add API to set vcpu register
set_vcpu_regs function is added to set vcpu registers. Tracked-On: #1231 Signed-off-by: Yin Fengwei <fengwei.yin@intel.com>
1 parent 0e0dbba commit 1c0a3d9

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

hypervisor/arch/x86/guest/vcpu.c

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,72 @@ struct vcpu *get_ever_run_vcpu(uint16_t pcpu_id)
163163
return per_cpu(ever_run_vcpu, pcpu_id);
164164
}
165165

166+
void set_vcpu_regs(struct vcpu *vcpu, struct acrn_vcpu_regs *vcpu_regs)
167+
{
168+
struct ext_context *ectx;
169+
struct run_context *ctx;
170+
uint16_t *sel = &(vcpu_regs->cs_sel);
171+
struct segment_sel *seg;
172+
uint32_t limit, attr;
173+
174+
ectx = &(vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context].ext_ctx);
175+
ctx = &(vcpu->arch_vcpu.contexts[vcpu->arch_vcpu.cur_context].run_ctx);
176+
177+
if (vcpu_regs->cs_ar & (1U << 15U)) {
178+
limit = 0xFFFFFFFFU;
179+
} else {
180+
limit = 0xFFFFU;
181+
}
182+
183+
if (vcpu_regs->cr0 & CR0_PE) {
184+
attr = PROTECTED_MODE_DATA_SEG_AR;
185+
} else {
186+
attr = REAL_MODE_DATA_SEG_AR;
187+
}
188+
189+
for (seg = &(ectx->cs); seg <= &(ectx->gs); seg++) {
190+
seg->base = 0UL;
191+
seg->limit = limit;
192+
seg->attr = attr;
193+
seg->selector = *sel;
194+
sel++;
195+
}
196+
197+
/* override cs attr/base */
198+
ectx->cs.attr = vcpu_regs->cs_ar;
199+
ectx->cs.base = vcpu_regs->cs_base;
200+
201+
ectx->gdtr.base = vcpu_regs->gdt.base;
202+
ectx->gdtr.limit = vcpu_regs->gdt.limit;
203+
204+
ectx->idtr.base = vcpu_regs->idt.base;
205+
ectx->idtr.limit = vcpu_regs->idt.limit;
206+
207+
ectx->ldtr.selector = vcpu_regs->ldt_sel;
208+
ectx->tr.selector = vcpu_regs->tr_sel;
209+
210+
memcpy_s(&(ctx->guest_cpu_regs), sizeof(struct acrn_gp_regs),
211+
&(vcpu_regs->gprs), sizeof(struct acrn_gp_regs));
212+
213+
vcpu_set_rip(vcpu, vcpu_regs->rip);
214+
vcpu_set_efer(vcpu, vcpu_regs->ia32_efer);
215+
vcpu_set_rsp(vcpu, vcpu_regs->gprs.rsp);
216+
217+
if (vcpu_regs->rflags == 0UL) {
218+
vcpu_set_rflags(vcpu, 0x02UL);
219+
} else {
220+
vcpu_set_rflags(vcpu, vcpu_regs->rflags & ~(0x8d5UL));
221+
}
222+
223+
/* cr0, cr3 and cr4 needs be set without using API.
224+
* The real cr0/cr3/cr4 writing will be delayed to
225+
* init_vmcs
226+
*/
227+
ctx->cr0 = vcpu_regs->cr0;
228+
ectx->cr3 = vcpu_regs->cr3;
229+
ctx->cr4 = vcpu_regs->cr4;
230+
}
231+
166232
/***********************************************************************
167233
* vcpu_id/pcpu_id mapping table:
168234
*

hypervisor/include/arch/x86/guest/guest.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@
6262
exec_vmwrite32(SEG_NAME##_ATTR, (seg).attr); \
6363
}
6464

65+
/* Define segments constants for guest */
66+
#define REAL_MODE_BSP_INIT_CODE_SEL (0xf000U)
67+
#define REAL_MODE_DATA_SEG_AR (0x0093U)
68+
#define REAL_MODE_CODE_SEG_AR (0x009fU)
69+
#define PROTECTED_MODE_DATA_SEG_AR (0xc093U)
70+
#define PROTECTED_MODE_CODE_SEG_AR (0xc09bU)
71+
#define DR7_INIT_VALUE (0x400UL)
72+
#define LDTR_AR (0x0082U) /* LDT, type must be 2, refer to SDM Vol3 26.3.1.2 */
73+
#define TR_AR (0x008bU) /* TSS (busy), refer to SDM Vol3 26.3.1.2 */
74+
6575
struct e820_mem_params {
6676
uint64_t mem_bottom;
6777
uint64_t mem_top;

hypervisor/include/arch/x86/guest/vcpu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ uint64_t vcpu_get_cr4(struct vcpu *vcpu);
286286
void vcpu_set_cr4(struct vcpu *vcpu, uint64_t val);
287287
uint64_t vcpu_get_pat_ext(struct vcpu *vcpu);
288288
void vcpu_set_pat_ext(struct vcpu *vcpu, uint64_t val);
289+
void set_vcpu_regs(struct vcpu *vcpu, struct acrn_vcpu_regs *vcpu_regs);
289290

290291
struct vcpu* get_ever_run_vcpu(uint16_t pcpu_id);
291292
int create_vcpu(uint16_t pcpu_id, struct vm *vm, struct vcpu **rtn_vcpu_handle);

0 commit comments

Comments
 (0)