Skip to content

Commit 097786b

Browse files
mdouchapevik
authored andcommitted
kvm_svm_vmrun(): Simplify VM state save/load with macros
Link: https://lore.kernel.org/ltp/[email protected]/ Acked-by: Petr Vorel <[email protected]> Signed-off-by: Martin Doucha <[email protected]>
1 parent 10eb8b8 commit 097786b

File tree

2 files changed

+98
-58
lines changed

2 files changed

+98
-58
lines changed

testcases/kernel/kvm/bootstrap_x86.S

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,34 @@ kvm_svm_guest_entry:
361361
1: hlt
362362
jmp 1b
363363

364+
/* vcpu structure address must be in %rdi */
365+
.macro load_vcpu_regs
366+
movl 0x04(%edi), %eax
367+
movl 0x0c(%edi), %ebx
368+
movl 0x14(%edi), %ecx
369+
movl 0x1c(%edi), %edx
370+
/* save %edi last */
371+
movl 0x2c(%edi), %esi
372+
movl 0x34(%edi), %ebp
373+
/* skip %esp */
374+
movl 0x24(%edi), %edi
375+
.endm
376+
377+
/* vcpu structure address must be on top of the stack */
378+
.macro save_vcpu_regs
379+
push %edi
380+
movl 4(%esp), %edi
381+
movl %eax, 0x04(%edi)
382+
movl %ebx, 0x0c(%edi)
383+
movl %ecx, 0x14(%edi)
384+
movl %edx, 0x1c(%edi)
385+
pop %eax
386+
movl %eax, 0x24(%edi)
387+
movl %esi, 0x2c(%edi)
388+
movl %ebp, 0x34(%edi)
389+
/* skip %esp */
390+
.endm
391+
364392
.global kvm_svm_vmrun
365393
kvm_svm_vmrun:
366394
push %edi
@@ -377,44 +405,35 @@ kvm_svm_vmrun:
377405
vmsave
378406
push %eax
379407

380-
/* Load guest registers */
381408
push %edi
382-
movl (%edi), %eax
383-
/* %eax is loaded by vmrun from VMCB */
384-
movl 0x0c(%edi), %ebx
385-
movl 0x14(%edi), %ecx
386-
movl 0x1c(%edi), %edx
387-
movl 0x2c(%edi), %esi
388-
movl 0x34(%edi), %ebp
389-
/* %esp is loaded by vmrun from VMCB */
390-
movl 0x24(%edi), %edi
409+
load_vcpu_regs
410+
/* %eax = vcpu->vmcb; */
411+
movl (%esp), %eax
412+
movl (%eax), %eax
391413

392414
vmload
393415
vmrun
394416
vmsave
395417

396418
/* Clear guest register buffer */
397419
push %edi
420+
push %eax
398421
push %ecx
399-
movl 8(%esp), %edi
422+
movl 12(%esp), %edi
400423
addl $4, %edi
401424
xorl %eax, %eax
402425
mov $32, %ecx
403426
pushfl
404427
cld
405428
rep stosl
406429
popfl
407-
408-
/* Save guest registers */
409430
pop %ecx
410431
pop %eax
411432
pop %edi
412-
movl %ebx, 0x0c(%edi)
413-
movl %ecx, 0x14(%edi)
414-
movl %edx, 0x1c(%edi)
415-
movl %eax, 0x24(%edi)
416-
movl %esi, 0x2c(%edi)
417-
movl %ebp, 0x34(%edi)
433+
434+
save_vcpu_regs
435+
pop %edi
436+
418437
/* Copy %eax and %esp from VMCB */
419438
movl (%edi), %esi
420439
movl 0x5f8(%esi), %eax

testcases/kernel/kvm/bootstrap_x86_64.S

Lines changed: 60 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -484,35 +484,16 @@ kvm_svm_guest_entry:
484484
1: hlt
485485
jmp 1b
486486

487-
.global kvm_svm_vmrun
488-
kvm_svm_vmrun:
489-
pushq %rbx
490-
pushq %rbp
491-
pushq %r12
492-
pushq %r13
493-
pushq %r14
494-
pushq %r15
495-
496-
clgi
497-
498-
/* Save full host state */
499-
movq $MSR_VM_HSAVE_PA, %rcx
500-
rdmsr
501-
shlq $32, %rdx
502-
orq %rdx, %rax
503-
vmsave
504-
pushq %rax
505-
506-
/* Load guest registers */
507-
pushq %rdi
508-
movq (%rdi), %rax
509-
/* %rax is loaded by vmrun from VMCB */
487+
/* vcpu structure address must be in %rdi */
488+
.macro load_vcpu_regs
489+
movq 0x08(%rdi), %rax
510490
movq 0x10(%rdi), %rbx
511491
movq 0x18(%rdi), %rcx
512492
movq 0x20(%rdi), %rdx
493+
/* load %rdi last */
513494
movq 0x30(%rdi), %rsi
514495
movq 0x38(%rdi), %rbp
515-
/* %rsp is loaded by vmrun from VMCB */
496+
/* skip %rsp */
516497
movq 0x48(%rdi), %r8
517498
movq 0x50(%rdi), %r9
518499
movq 0x58(%rdi), %r10
@@ -522,21 +503,21 @@ kvm_svm_vmrun:
522503
movq 0x78(%rdi), %r14
523504
movq 0x80(%rdi), %r15
524505
movq 0x28(%rdi), %rdi
506+
.endm
525507

526-
vmload
527-
vmrun
528-
vmsave
529-
530-
/* Save guest registers */
531-
movq %rdi, %rax
532-
popq %rdi
508+
/* vcpu structure address must be on top of the stack */
509+
.macro save_vcpu_regs
510+
pushq %rdi
511+
movq 8(%rsp), %rdi
512+
movq %rax, 0x08(%rdi)
533513
movq %rbx, 0x10(%rdi)
534514
movq %rcx, 0x18(%rdi)
535515
movq %rdx, 0x20(%rdi)
536-
/* %rax contains guest %rdi */
516+
popq %rax
537517
movq %rax, 0x28(%rdi)
538518
movq %rsi, 0x30(%rdi)
539519
movq %rbp, 0x38(%rdi)
520+
/* skip %rsp */
540521
movq %r8, 0x48(%rdi)
541522
movq %r9, 0x50(%rdi)
542523
movq %r10, 0x58(%rdi)
@@ -545,6 +526,52 @@ kvm_svm_vmrun:
545526
movq %r13, 0x70(%rdi)
546527
movq %r14, 0x78(%rdi)
547528
movq %r15, 0x80(%rdi)
529+
.endm
530+
531+
.macro push_local
532+
pushq %rbx
533+
pushq %rbp
534+
pushq %r12
535+
pushq %r13
536+
pushq %r14
537+
pushq %r15
538+
.endm
539+
540+
.macro pop_local
541+
popq %r15
542+
popq %r14
543+
popq %r13
544+
popq %r12
545+
popq %rbp
546+
popq %rbx
547+
.endm
548+
549+
.global kvm_svm_vmrun
550+
kvm_svm_vmrun:
551+
push_local
552+
clgi
553+
554+
/* Save full host state */
555+
movq $MSR_VM_HSAVE_PA, %rcx
556+
rdmsr
557+
shlq $32, %rdx
558+
orq %rdx, %rax
559+
vmsave
560+
pushq %rax
561+
562+
pushq %rdi
563+
load_vcpu_regs
564+
/* %rax = vcpu->vmcb; */
565+
movq (%rsp), %rax
566+
movq (%rax), %rax
567+
568+
vmload
569+
vmrun
570+
vmsave
571+
572+
save_vcpu_regs
573+
popq %rdi
574+
548575
/* copy guest %rax and %rsp from VMCB*/
549576
movq (%rdi), %rsi
550577
movq 0x5f8(%rsi), %rax
@@ -557,13 +584,7 @@ kvm_svm_vmrun:
557584
vmload
558585

559586
stgi
560-
561-
popq %r15
562-
popq %r14
563-
popq %r13
564-
popq %r12
565-
popq %rbp
566-
popq %rbx
587+
pop_local
567588
retq
568589

569590
.section .bss.pgtables, "aw", @nobits

0 commit comments

Comments
 (0)