From patchwork Mon Jun 7 15:11:37 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 104781 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o57FBRtI021473 for ; Mon, 7 Jun 2010 15:11:27 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751387Ab0FGPLZ (ORCPT ); Mon, 7 Jun 2010 11:11:25 -0400 Received: from mail-pw0-f46.google.com ([209.85.160.46]:49282 "EHLO mail-pw0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751259Ab0FGPLY (ORCPT ); Mon, 7 Jun 2010 11:11:24 -0400 Received: by pwj5 with SMTP id 5so898740pwj.19 for ; Mon, 07 Jun 2010 08:11:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:from:to:subject:date :message-id:x-mailer; bh=1Vc738rVxeswsr0RaoBegF/JMCqWd0UiwS+F8m/v3IE=; b=XsSQa9QdoSHDv0Q0zlgYRYbs106SV2qsgDD3XDsf6t2XKt2inynrATyqdH+4nzove6 5uF3wjqj031K2zpwAbcmyqSK00gz8oelLdnGcViGmhneO9Cqwa5IZomsoYGDwyB47e5H XlgiSEaAoQBzsB7R1vcJtNErLl3qNOAW+p+Xo= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:subject:date:message-id:x-mailer; b=KF5awZdcPOIsxOwjy4hVR3sOqH0JEZa+vlSheZN9iJZvnxv4QbmtZFAygXG21Rlloj P17il41Eqf/fiB7Me3SDJVyQVdIacmfQyBYdp5AVlrBbG8GXBh83sn03hUc4Bv2WToE9 Bk90E+gzdqr5fTyf9inThZ4HXIDXHf+7zuhD0= Received: by 10.115.67.11 with SMTP id u11mr11536682wak.196.1275923482893; Mon, 07 Jun 2010 08:11:22 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id n32sm39551527wae.10.2010.06.07.08.11.18 (version=TLSv1/SSLv3 cipher=RC4-MD5); Mon, 07 Jun 2010 08:11:21 -0700 (PDT) From: Asias He To: kvm@vger.kernel.org, Avi Kivity , Naphtali Sprei , Marcelo Tosatti Subject: [PATCH 1/2] qemu-kvm tests: make x86/vm.c support 32-bit host Date: Mon, 7 Jun 2010 23:11:37 +0800 Message-Id: <1275923498-8999-1-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.1 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Mon, 07 Jun 2010 15:11:27 +0000 (UTC) diff --git a/kvm/test/x86/vm.c b/kvm/test/x86/vm.c index ec9c145..c9876e5 100644 --- a/kvm/test/x86/vm.c +++ b/kvm/test/x86/vm.c @@ -1,11 +1,16 @@ - #include "vm.h" - -void print(const char *s); +#include "libcflat.h" #define PAGE_SIZE 4096ul +#ifdef __x86_64__ #define LARGE_PAGE_SIZE (512 * PAGE_SIZE) +#else +#define LARGE_PAGE_SIZE (1024 * PAGE_SIZE) +#endif +#define X86_CR0_PE 0x00000001 +#define X86_CR0_PG 0x80000000 +#define X86_CR4_PSE 0x00000010 static void *free = 0; static void *vfree_top = 0; @@ -66,6 +71,16 @@ static unsigned long end_of_memory; #define PTE_WRITE (1ull << 1) #define PTE_ADDR (0xffffffffff000ull) +#ifdef __x86_64__ +#define PAGE_LEVEL 4 +#define PGDIR_WIDTH 9 +#define PGDIR_MASK 511 +#else +#define PAGE_LEVEL 2 +#define PGDIR_WIDTH 10 +#define PGDIR_MASK 1023 +#endif + static void install_pte(unsigned long *cr3, int pte_level, void *virt, @@ -75,8 +90,8 @@ static void install_pte(unsigned long *cr3, unsigned long *pt = cr3; unsigned offset; - for (level = 4; level > pte_level; --level) { - offset = ((unsigned long)virt >> ((level-1) * 9 + 12)) & 511; + for (level = PAGE_LEVEL; level > pte_level; --level) { + offset = ((unsigned long)virt >> ((level-1) * PGDIR_WIDTH + 12)) & PGDIR_MASK; if (!(pt[offset] & PTE_PRESENT)) { unsigned long *new_pt = alloc_page(); memset(new_pt, 0, PAGE_SIZE); @@ -84,7 +99,7 @@ static void install_pte(unsigned long *cr3, } pt = phys_to_virt(pt[offset] & 0xffffffffff000ull); } - offset = ((unsigned long)virt >> (((level-1) * 9) + 12)) & 511; + offset = ((unsigned long)virt >> ((level-1) * PGDIR_WIDTH + 12)) & PGDIR_MASK; pt[offset] = pte; } @@ -94,8 +109,8 @@ static unsigned long get_pte(unsigned long *cr3, void *virt) unsigned long *pt = cr3, pte; unsigned offset; - for (level = 4; level > 1; --level) { - offset = ((unsigned long)virt >> (((level-1) * 9) + 12)) & 511; + for (level = PAGE_LEVEL; level > 1; --level) { + offset = ((unsigned long)virt >> (((level-1) * PGDIR_WIDTH) + 12)) & PGDIR_MASK; pte = pt[offset]; if (!(pte & PTE_PRESENT)) return 0; @@ -103,7 +118,7 @@ static unsigned long get_pte(unsigned long *cr3, void *virt) return pte; pt = phys_to_virt(pte & 0xffffffffff000ull); } - offset = ((unsigned long)virt >> (((level-1) * 9) + 12)) & 511; + offset = ((unsigned long)virt >> (((level-1) * PGDIR_WIDTH) + 12)) & PGDIR_MASK; pte = pt[offset]; return pte; } @@ -189,8 +204,10 @@ static void setup_mmu(unsigned long len) unsigned long *cr3 = alloc_page(); unsigned long phys = 0; +#if 0 if (len < (1ul << 32)) len = 1ul << 32; /* map mmio 1:1 */ +#endif memset(cr3, 0, PAGE_SIZE); while (phys + LARGE_PAGE_SIZE <= len) { @@ -201,9 +218,16 @@ static void setup_mmu(unsigned long len) install_page(cr3, phys, (void *)phys); phys += PAGE_SIZE; } - load_cr3(virt_to_phys(cr3)); - print("paging enabled\n"); +#ifndef __x86_64__ + load_cr4(X86_CR4_PSE); +#endif + load_cr0(X86_CR0_PG |X86_CR0_PE); + + printf("paging enabled\n"); + printf("cr0 = %x\n", read_cr0()); + printf("cr3 = %x\n", read_cr3()); + printf("cr4 = %x\n", read_cr4()); } static unsigned int inl(unsigned short port)