From 938c35bb7b2de693fa1b808911b7b4f2f9fbf1eb Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sat, 27 Mar 2010 20:55:27 +0200 Subject: [PATCH] kvm: Introduce host_real_to_guest() helper function Introduce a new host_real_to_guest() helper function and use it to clean up code. Signed-off-by: Pekka Enberg --- tools/kvm/kvm.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/tools/kvm/kvm.c b/tools/kvm/kvm.c index 859d7567396b..d0870167caf6 100644 --- a/tools/kvm/kvm.c +++ b/tools/kvm/kvm.c @@ -48,11 +48,23 @@ const char *kvm_exit_reasons[] = { DEFINE_KVM_EXIT_REASON(KVM_EXIT_INTERNAL_ERROR), }; -static inline void *guest_addr_to_host(struct kvm *self, unsigned long offset) +static inline uint32_t segment_to_flat(uint16_t selector, uint16_t offset) +{ + return ((uint32_t)selector << 4) + (uint32_t) offset; +} + +static inline void *guest_flat_to_host(struct kvm *self, unsigned long offset) { return self->ram_start + offset; } +static inline void *guest_real_to_host(struct kvm *self, uint16_t selector, uint16_t offset) +{ + unsigned long flat = segment_to_flat(selector, offset); + + return guest_flat_to_host(self, flat); +} + static bool kvm__supports_extension(struct kvm *self, unsigned int extension) { int ret; @@ -148,11 +160,6 @@ void kvm__enable_singlestep(struct kvm *self) warning("KVM_SET_GUEST_DEBUG failed"); } -static inline uint32_t segment_to_flat(uint16_t selector, uint16_t offset) -{ - return ((uint32_t)selector << 4) + (uint32_t) offset; -} - #define BOOT_LOADER_SELECTOR 0x0100 #define BOOT_LOADER_IP 0x0000 #define BOOT_LOADER_SP 0x8000 @@ -165,7 +172,7 @@ static int load_flat_binary(struct kvm *self, int fd) if (lseek(fd, 0, SEEK_SET) < 0) die_perror("lseek"); - p = guest_addr_to_host(self, segment_to_flat(BOOT_LOADER_SELECTOR, BOOT_LOADER_IP)); + p = guest_real_to_host(self, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP); while ((nr = read(fd, p, 65536)) > 0) p += nr; @@ -216,12 +223,12 @@ static bool load_bzimage(struct kvm *self, int fd) setup_sects = BZ_DEFAULT_SETUP_SECTS; setup_size = setup_sects << 9; - p = guest_addr_to_host(self, segment_to_flat(BOOT_LOADER_SELECTOR, BOOT_LOADER_IP)); + p = guest_real_to_host(self, BOOT_LOADER_SELECTOR, BOOT_LOADER_IP); if (read(fd, p, setup_size) != setup_size) die_perror("read"); - p = guest_addr_to_host(self, BZ_KERNEL_START); + p = guest_flat_to_host(self, BZ_KERNEL_START); while ((nr = read(fd, p, 65536)) > 0) p += nr; @@ -485,14 +492,14 @@ void kvm__show_code(struct kvm *self) if (ioctl(self->vcpu_fd, KVM_GET_SREGS, &self->sregs) < 0) die("KVM_GET_SREGS failed"); - ip = guest_addr_to_host(self, ip_real_to_flat(self, self->regs.rip) - code_prologue); + ip = guest_flat_to_host(self, ip_real_to_flat(self, self->regs.rip) - code_prologue); printf("Code: "); for (i = 0; i < code_len; i++, ip++) { c = *ip; - if (ip == guest_addr_to_host(self, ip_real_to_flat(self, self->regs.rip))) + if (ip == guest_flat_to_host(self, ip_real_to_flat(self, self->regs.rip))) printf("<%02x> ", c); else printf("%02x ", c); -- 2.39.5