[PATCH 11 of 17] Move msrs functions to libkvm-x86.c

Jerone Young
2007-11-06T16:48:52Z

# Date 1194367653 21600
# Node ID cbe4ad07343fc1ef904f02734c68d0fb8506cb9e
# Parent b1154d12bcc774520ed334f0b84934f1f4db7ce4
Move msrs functions to libkvm-x86.c

This patch moves functions:
kvm_msr_list
move kvm_get_msrs
move kvm_set_msrs

Signed-off-by: Jerone Young <jyoung5-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>

diff --git a/libkvm/kvm-x86.h b/libkvm/kvm-x86.h
--- a/libkvm/kvm-x86.h
+++ b/libkvm/kvm-x86.h
@@ -42,4 +42,8 @@ int kvm_run_abi10(kvm_context_t kvm, int

void kvm_show_code(kvm_context_t kvm, int vcpu);

+struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
+int kvm_get_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
+int kvm_set_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
+
#endif
diff --git a/libkvm/libkvm-x86.c b/libkvm/libkvm-x86.c
--- a/libkvm/libkvm-x86.c
+++ b/libkvm/libkvm-x86.c
@@ -576,3 +576,71 @@ void kvm_show_code(kvm_context_t kvm, in
fprintf(stderr, "code:%s\n", code_str);
}

+
+/*
+ * Returns available msr list. User must free.
+ */
+struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
+{
+ struct kvm_msr_list sizer, *msrs;
+ int r, e;
+
+ sizer.nmsrs = 0;
+ r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, &sizer);
+ if (r == -1 && errno != E2BIG)
+ return NULL;
+ msrs = malloc(sizeof *msrs + sizer.nmsrs * sizeof *msrs->indices);
+ if (!msrs) {
+ errno = ENOMEM;
+ return NULL;
+ }
+ msrs->nmsrs = sizer.nmsrs;
+ r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, msrs);
+ if (r == -1) {
+ e = errno;
+ free(msrs);
+ errno = e;
+ return NULL;
+ }
+ return msrs;
+}
+
+int kvm_get_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
+ int n)
+{
+ struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
+ int r, e;
+
+ if (!kmsrs) {
+ errno = ENOMEM;
+ return -1;
+ }
+ kmsrs->nmsrs = n;
+ memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
+ r = ioctl(kvm->vcpu_fd[vcpu], KVM_GET_MSRS, kmsrs);
+ e = errno;
+ memcpy(msrs, kmsrs->entries, n * sizeof *msrs);
+ free(kmsrs);
+ errno = e;
+ return r;
+}
+
+int kvm_set_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
+ int n)
+{
+ struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
+ int r, e;
+
+ if (!kmsrs) {
+ errno = ENOMEM;
+ return -1;
+ }
+ kmsrs->nmsrs = n;
+ memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
+ r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_MSRS, kmsrs);
+ e = errno;
+ free(kmsrs);
+ errno = e;
+ return r;
+}
+
diff --git a/libkvm/libkvm.c b/libkvm/libkvm.c
--- a/libkvm/libkvm.c
+++ b/libkvm/libkvm.c
@@ -732,73 +732,6 @@ int kvm_set_sregs(kvm_context_t kvm, int
return ioctl(kvm->vcpu_fd[vcpu], KVM_SET_SREGS, sregs);
}

-/*
- * Returns available msr list. User must free.
- */
-struct kvm_msr_list *kvm_get_msr_list(kvm_context_t kvm)
-{
- struct kvm_msr_list sizer, *msrs;
- int r, e;
-
- sizer.nmsrs = 0;
- r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, &sizer);
- if (r == -1 && errno != E2BIG)
- return NULL;
- msrs = malloc(sizeof *msrs + sizer.nmsrs * sizeof *msrs->indices);
- if (!msrs) {
- errno = ENOMEM;
- return NULL;
- }
- msrs->nmsrs = sizer.nmsrs;
- r = ioctl(kvm->fd, KVM_GET_MSR_INDEX_LIST, msrs);
- if (r == -1) {
- e = errno;
- free(msrs);
- errno = e;
- return NULL;
- }
- return msrs;
-}
-
-int kvm_get_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
- int n)
-{
- struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
- int r, e;
-
- if (!kmsrs) {
- errno = ENOMEM;
- return -1;
- }
- kmsrs->nmsrs = n;
- memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
- r = ioctl(kvm->vcpu_fd[vcpu], KVM_GET_MSRS, kmsrs);
- e = errno;
- memcpy(msrs, kmsrs->entries, n * sizeof *msrs);
- free(kmsrs);
- errno = e;
- return r;
-}
-
-int kvm_set_msrs(kvm_context_t kvm, int vcpu, struct kvm_msr_entry *msrs,
- int n)
-{
- struct kvm_msrs *kmsrs = malloc(sizeof *kmsrs + n * sizeof *msrs);
- int r, e;
-
- if (!kmsrs) {
- errno = ENOMEM;
- return -1;
- }
- kmsrs->nmsrs = n;
- memcpy(kmsrs->entries, msrs, n * sizeof *msrs);
- r = ioctl(kvm->vcpu_fd[vcpu], KVM_SET_MSRS, kmsrs);
- e = errno;
- free(kmsrs);
- errno = e;
- return r;
-}
-
static void print_seg(FILE *file, const char *name, struct kvm_segment *seg)
{
fprintf(stderr,
diff --git a/libkvm/libkvm.h b/libkvm/libkvm.h
--- a/libkvm/libkvm.h
+++ b/libkvm/libkvm.h
@@ -335,10 +335,6 @@ int kvm_get_sregs(kvm_context_t kvm, int
*/
int kvm_set_sregs(kvm_context_t kvm, int vcpu, struct kvm_sregs *regs);

-struct kvm_msr_list *kvm_get_msr_list(kvm_context_t);
-int kvm_get_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
-int kvm_set_msrs(kvm_context_t, int vcpu, struct kvm_msr_entry *msrs, int n);
-
/*!
* \brief Simulate an external vectored interrupt
*