|
5 | 5 | * Copyright (C) 2018, Google LLC.
|
6 | 6 | */
|
7 | 7 |
|
| 8 | +#include "linux/bitmap.h" |
8 | 9 | #include "test_util.h"
|
9 | 10 | #include "kvm_util.h"
|
10 | 11 | #include "processor.h"
|
@@ -573,14 +574,29 @@ struct kvm_vcpu *vm_arch_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id,
|
573 | 574 | DEFAULT_GUEST_STACK_VADDR_MIN,
|
574 | 575 | MEM_REGION_DATA);
|
575 | 576 |
|
| 577 | + stack_vaddr += DEFAULT_STACK_PGS * getpagesize(); |
| 578 | + |
| 579 | + /* |
| 580 | + * Align stack to match calling sequence requirements in section "The |
| 581 | + * Stack Frame" of the System V ABI AMD64 Architecture Processor |
| 582 | + * Supplement, which requires the value (%rsp + 8) to be a multiple of |
| 583 | + * 16 when control is transferred to the function entry point. |
| 584 | + * |
| 585 | + * If this code is ever used to launch a vCPU with 32-bit entry point it |
| 586 | + * may need to subtract 4 bytes instead of 8 bytes. |
| 587 | + */ |
| 588 | + TEST_ASSERT(IS_ALIGNED(stack_vaddr, PAGE_SIZE), |
| 589 | + "__vm_vaddr_alloc() did not provide a page-aligned address"); |
| 590 | + stack_vaddr -= 8; |
| 591 | + |
576 | 592 | vcpu = __vm_vcpu_add(vm, vcpu_id);
|
577 | 593 | vcpu_init_cpuid(vcpu, kvm_get_supported_cpuid());
|
578 | 594 | vcpu_setup(vm, vcpu);
|
579 | 595 |
|
580 | 596 | /* Setup guest general purpose registers */
|
581 | 597 | vcpu_regs_get(vcpu, ®s);
|
582 | 598 | regs.rflags = regs.rflags | 0x2;
|
583 |
| - regs.rsp = stack_vaddr + (DEFAULT_STACK_PGS * getpagesize()); |
| 599 | + regs.rsp = stack_vaddr; |
584 | 600 | regs.rip = (unsigned long) guest_code;
|
585 | 601 | vcpu_regs_set(vcpu, ®s);
|
586 | 602 |
|
|
0 commit comments