From: Simon Kagstrom Subject: [PATCH] libxc and domain.c changes backported from xen-unstable to reenable debugging Date: Thu, 22 Jun 2006 16:36:43 +0200 Message-ID: <87bqslgsuc.wl%simon.kagstrom@bth.se> Mime-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: multipart/mixed; boundary="Multipart_Thu_Jun_22_16:36:43_2006-1" Return-path: List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xensource.com Errors-To: xen-devel-bounces@lists.xensource.com To: xen-devel list , Keir.Fraser@cl.cam.ac.uk List-Id: xen-devel@lists.xenproject.org --Multipart_Thu_Jun_22_16:36:43_2006-1 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable The following two patches reenable debugging of paravirtualized and HVM domains for the xen-3.0-testing.hg tree. The patches are backported from xen-unstable.hg, http://xenbits.xensource.com/xen-unstable.hg?cs=3D93379554837c http://xenbits.xensource.com/xen-unstable.hg?cs=3Ded696467fe64 The first patch exports the cr0 value to the guest in Xen and the second abstracts away the differences between HVM and paravirtualized guests with regard to physical/machine addresses in xc_ptrace.c. Signed-off-by: Simon K=E5gstr=F6m --Multipart_Thu_Jun_22_16:36:43_2006-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="domain.patch" Content-Transfer-Encoding: quoted-printable diff -r c2a8f93d0dc8 xen/arch/x86/domain.c --- a/xen/arch/x86/domain.c Tue Jun 20 18:29:46 2006 +0100 +++ b/xen/arch/x86/domain.c Thu Jun 22 16:29:58 2006 +0200 @@ -379,6 +379,11 @@ int arch_set_info_guest( v->arch.flags |=3D TF_kernel_mode; =20 memcpy(&v->arch.guest_context, c, sizeof(*c)); + + /* Only CR0.TS is modifiable by guest or admin. */ + v->arch.guest_context.ctrlreg[0] &=3D X86_CR0_TS; + v->arch.guest_context.ctrlreg[0] |=3D read_cr0() & ~X86_CR0_TS; + init_int80_direct_trap(v); =20 if ( !(c->flags & VGCF_HVM_GUEST) ) --Multipart_Thu_Jun_22_16:36:43_2006-1 Content-Type: text/plain; charset=US-ASCII --Multipart_Thu_Jun_22_16:36:43_2006-1 Content-Type: application/octet-stream; type=patch Content-Disposition: attachment; filename="libxc.patch" Content-Transfer-Encoding: quoted-printable diff -r c2a8f93d0dc8 tools/libxc/xc_ptrace.c --- a/tools/libxc/xc_ptrace.c Tue Jun 20 18:29:46 2006 +0100 +++ b/tools/libxc/xc_ptrace.c Thu Jun 22 16:29:06 2006 +0200 @@ -157,6 +157,27 @@ static long nr_pages static long nr_pages =3D 0; static unsigned long *page_array =3D NULL; =20 + +/* + * Translates physical addresses to machine addresses for HVM + * guests. For paravirtual domains the function will just return the + * given address. + * + * This function should be used when reading page directories/page + * tables. + * + */ +static unsigned long +to_ma(int cpu, + unsigned long in_addr) +{ + unsigned long maddr =3D in_addr; + + if ( (ctxt[cpu].flags & VGCF_HVM_GUEST) && paging_enabled(&ctxt[cpu]) ) + maddr =3D page_array[maddr >> PAGE_SHIFT] << PAGE_SHIFT; + return maddr; +} + static void * map_domain_va_32( int xc_handle, @@ -164,66 +185,37 @@ map_domain_va_32( void *guest_va, int perm) { - unsigned long pde, page; - unsigned long va =3D (unsigned long)guest_va; - - static unsigned long cr3_phys[MAX_VIRT_CPUS]; - static uint32_t *cr3_virt[MAX_VIRT_CPUS]; - static unsigned long pde_phys[MAX_VIRT_CPUS]; - static uint32_t *pde_virt[MAX_VIRT_CPUS]; - static unsigned long page_phys[MAX_VIRT_CPUS]; - static uint32_t *page_virt[MAX_VIRT_CPUS]; =20 - static int prev_perm[MAX_VIRT_CPUS]; - - if (ctxt[cpu].ctrlreg[3] =3D=3D 0) - return NULL; - if ( ctxt[cpu].ctrlreg[3] !=3D cr3_phys[cpu] ) - { - cr3_phys[cpu] =3D ctxt[cpu].ctrlreg[3]; - if ( cr3_virt[cpu] ) - munmap(cr3_virt[cpu], PAGE_SIZE); - cr3_virt[cpu] =3D xc_map_foreign_range( - xc_handle, current_domid, PAGE_SIZE, PROT_READ, - cr3_phys[cpu] >> PAGE_SHIFT); - if ( cr3_virt[cpu] =3D=3D NULL ) - return NULL; - } - if ( (pde =3D cr3_virt[cpu][vtopdi(va)]) =3D=3D 0 ) - return NULL; - if ( (ctxt[cpu].flags & VGCF_HVM_GUEST) && paging_enabled(&ctxt[cpu]) ) - pde =3D page_array[pde >> PAGE_SHIFT] << PAGE_SHIFT; - if ( pde !=3D pde_phys[cpu] ) - { - pde_phys[cpu] =3D pde; - if ( pde_virt[cpu] ) - munmap(pde_virt[cpu], PAGE_SIZE); - pde_virt[cpu] =3D xc_map_foreign_range( - xc_handle, current_domid, PAGE_SIZE, PROT_READ, - pde_phys[cpu] >> PAGE_SHIFT); - if ( pde_virt[cpu] =3D=3D NULL ) - return NULL; - } - if ( (page =3D pde_virt[cpu][vtopti(va)]) =3D=3D 0 ) - return NULL; - if (ctxt[cpu].flags & VGCF_HVM_GUEST) - page =3D page_array[page >> PAGE_SHIFT] << PAGE_SHIFT; - if ( (page !=3D page_phys[cpu]) || (perm !=3D prev_perm[cpu]) ) - { - page_phys[cpu] =3D page; - if ( page_virt[cpu] ) - munmap(page_virt[cpu], PAGE_SIZE); - page_virt[cpu] =3D xc_map_foreign_range( - xc_handle, current_domid, PAGE_SIZE, perm, - page_phys[cpu] >> PAGE_SHIFT); - if ( page_virt[cpu] =3D=3D NULL ) - { - page_phys[cpu] =3D 0; - return NULL; - } - prev_perm[cpu] =3D perm; - }=20 - - return (void *)(((unsigned long)page_virt[cpu]) | (va & BSD_PAGE_MASK)= ); + unsigned long l2e, l1e, l1p, p, va =3D (unsigned long)guest_va; + uint32_t *l2, *l1; + static void *v[MAX_VIRT_CPUS]; + + l2 =3D xc_map_foreign_range( + xc_handle, current_domid, PAGE_SIZE, PROT_READ, + xen_cr3_to_pfn(ctxt[cpu].ctrlreg[3])); + if ( l2 =3D=3D NULL ) + return NULL; + + l2e =3D l2[l2_table_offset_i386(va)]; + munmap(l2, PAGE_SIZE); + if ( !(l2e & _PAGE_PRESENT) ) + return NULL; + l1p =3D to_ma(cpu, l2e); + l1 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l1p >> PAGE_SHIFT); + if ( l1 =3D=3D NULL ) + return NULL; + + l1e =3D l1[l1_table_offset_i386(va)]; + munmap(l1, PAGE_SIZE); + if ( !(l1e & _PAGE_PRESENT) ) + return NULL; + p =3D to_ma(cpu, l1e); + if ( v[cpu] !=3D NULL ) + munmap(v[cpu], PAGE_SIZE); + v[cpu] =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, p= erm, p >> PAGE_SHIFT); + if ( v[cpu] =3D=3D NULL ) + return NULL; + + return (void *)((unsigned long)v[cpu] | (va & (PAGE_SIZE - 1))); } =20 =20 @@ -234,39 +226,46 @@ map_domain_va_pae( void *guest_va, int perm) { - unsigned long l2p, l1p, p, va =3D (unsigned long)guest_va; + unsigned long l3e, l2e, l1e, l2p, l1p, p, va =3D (unsigned long)guest_= va; uint64_t *l3, *l2, *l1; - static void *v; + static void *v[MAX_VIRT_CPUS]; =20 l3 =3D xc_map_foreign_range( - xc_handle, current_domid, PAGE_SIZE, PROT_READ, ctxt[cpu].ctrlreg[= 3] >> PAGE_SHIFT); + xc_handle, current_domid, PAGE_SIZE, PROT_READ, + xen_cr3_to_pfn(ctxt[cpu].ctrlreg[3])); if ( l3 =3D=3D NULL ) return NULL; =20 - l2p =3D l3[l3_table_offset_pae(va)] >> PAGE_SHIFT; - l2p =3D page_array[l2p]; - l2 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l2p); + l3e =3D l3[l3_table_offset_pae(va)]; munmap(l3, PAGE_SIZE); + if ( !(l3e & _PAGE_PRESENT) ) + return NULL; + l2p =3D to_ma(cpu, l3e); + l2 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l2p >> PAGE_SHIFT); if ( l2 =3D=3D NULL ) return NULL; =20 - l1p =3D l2[l2_table_offset_pae(va)] >> PAGE_SHIFT; - l1p =3D page_array[l1p]; - l1 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, perm,= l1p); + l2e =3D l2[l2_table_offset_pae(va)]; munmap(l2, PAGE_SIZE); + if ( !(l2e & _PAGE_PRESENT) ) + return NULL; + l1p =3D to_ma(cpu, l2e); + l1 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, perm,= l1p >> PAGE_SHIFT); if ( l1 =3D=3D NULL ) return NULL; =20 - p =3D l1[l1_table_offset_pae(va)] >> PAGE_SHIFT; - p =3D page_array[p]; - if ( v !=3D NULL ) - munmap(v, PAGE_SIZE); - v =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, perm, = p); + l1e =3D l1[l1_table_offset_pae(va)]; munmap(l1, PAGE_SIZE); - if ( v =3D=3D NULL ) - return NULL; - - return (void *)((unsigned long)v | (va & (PAGE_SIZE - 1))); + if ( !(l1e & _PAGE_PRESENT) ) + return NULL; + p =3D to_ma(cpu, l1e); + if ( v[cpu] !=3D NULL ) + munmap(v[cpu], PAGE_SIZE); + v[cpu] =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, p= erm, p >> PAGE_SHIFT); + if ( v[cpu] =3D=3D NULL ) + return NULL; + + return (void *)((unsigned long)v[cpu] | (va & (PAGE_SIZE - 1))); } =20 #ifdef __x86_64__ @@ -277,56 +276,65 @@ map_domain_va_64( void *guest_va, int perm) { - unsigned long l3p, l2p, l1p, l1e, p, va =3D (unsigned long)guest_va; + unsigned long l4e, l3e, l2e, l1e, l3p, l2p, l1p, p, va =3D (unsigned l= ong)guest_va; uint64_t *l4, *l3, *l2, *l1; - static void *v; + static void *v[MAX_VIRT_CPUS]; + =20 if ((ctxt[cpu].ctrlreg[4] & 0x20) =3D=3D 0 ) /* legacy ia32 mode */ return map_domain_va_32(xc_handle, cpu, guest_va, perm); =20 - l4 =3D xc_map_foreign_range( xc_handle, current_domid, PAGE_SIZE,=20 - PROT_READ, ctxt[cpu].ctrlreg[3] >> PAGE_SHIFT); + l4 =3D xc_map_foreign_range( + xc_handle, current_domid, PAGE_SIZE, PROT_READ, + xen_cr3_to_pfn(ctxt[cpu].ctrlreg[3])); if ( l4 =3D=3D NULL ) return NULL; =20 - l3p =3D l4[l4_table_offset(va)] >> PAGE_SHIFT; - l3p =3D page_array[l3p]; - l3 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l3p); + l4e =3D l4[l4_table_offset(va)]; munmap(l4, PAGE_SIZE); + if ( !(l4e & _PAGE_PRESENT) ) + return NULL; + l3p =3D to_ma(cpu, l4e); + l3 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l3p >> PAGE_SHIFT); if ( l3 =3D=3D NULL ) return NULL; =20 - l2p =3D l3[l3_table_offset(va)] >> PAGE_SHIFT; - l2p =3D page_array[l2p]; - l2 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l2p); + l3e =3D l3[l3_table_offset(va)]; munmap(l3, PAGE_SIZE); + if ( !(l3e & _PAGE_PRESENT) ) + return NULL; + l2p =3D to_ma(cpu, l3e); + l2 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, PROT_= READ, l2p >> PAGE_SHIFT); if ( l2 =3D=3D NULL ) return NULL; =20 l1 =3D NULL; - l1e =3D l2[l2_table_offset(va)]; - l1p =3D l1e >> PAGE_SHIFT; - if (l1e & 0x80) { /* 2M pages */ - p =3D (l1p + l1_table_offset(va)); + l2e =3D l2[l2_table_offset(va)]; + munmap(l2, PAGE_SIZE); + if ( !(l2e & _PAGE_PRESENT) ) + return NULL; + l1p =3D to_ma(cpu, l2e); + if (l2e & 0x80) { /* 2M pages */ + p =3D to_ma(cpu, (l1p + l1_table_offset(va)) << PAGE_SHIFT); } else { /* 4K pages */ - l1p =3D page_array[l1p]; - l1 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, p= erm, l1p); - munmap(l2, PAGE_SIZE); + l1 =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, p= erm, l1p >> PAGE_SHIFT); if ( l1 =3D=3D NULL ) return NULL; =20 - p =3D l1[l1_table_offset(va)] >> PAGE_SHIFT; - } - p =3D page_array[p]; - if ( v !=3D NULL ) - munmap(v, PAGE_SIZE); - v =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, perm, = p); + l1e =3D l1[l1_table_offset(va)]; + if ( !(l1e & _PAGE_PRESENT) ) + return NULL; + p =3D to_ma(cpu, l1e); + } + if ( v[cpu] !=3D NULL ) + munmap(v[cpu], PAGE_SIZE); + v[cpu] =3D xc_map_foreign_range(xc_handle, current_domid, PAGE_SIZE, p= erm, p >> PAGE_SHIFT); if (l1) munmap(l1, PAGE_SIZE); - if ( v =3D=3D NULL ) - return NULL; - - return (void *)((unsigned long)v | (va & (PAGE_SIZE - 1))); + if ( v[cpu] =3D=3D NULL ) + return NULL; + + return (void *)((unsigned long)v[cpu] | (va & (PAGE_SIZE - 1))); } #endif =20 @@ -381,7 +389,7 @@ map_domain_va( if ( v !=3D NULL ) munmap(v, PAGE_SIZE); =20 - page =3D page_array[va >> PAGE_SHIFT] << PAGE_SHIFT; + page =3D to_ma(cpu, page_array[va >> PAGE_SHIFT]); =20 v =3D xc_map_foreign_range( xc_handle, current_domid, PAGE_SIZE,=20 perm, page >> PAGE_SHIFT); diff -r c2a8f93d0dc8 tools/libxc/xg_private.h --- a/tools/libxc/xg_private.h Tue Jun 20 18:29:46 2006 +0100 +++ b/tools/libxc/xg_private.h Thu Jun 22 16:29:06 2006 +0200 @@ -48,6 +48,8 @@ unsigned long csum_page (void * page); #define L2_PAGETABLE_SHIFT_PAE 21 #define L3_PAGETABLE_SHIFT_PAE 30 =20 +#define L2_PAGETABLE_SHIFT_I386 22 + #if defined(__i386__) #define L1_PAGETABLE_SHIFT 12 #define L2_PAGETABLE_SHIFT 22 @@ -62,7 +64,10 @@ unsigned long csum_page (void * page); #define L2_PAGETABLE_ENTRIES_PAE 512 #define L3_PAGETABLE_ENTRIES_PAE 4 =20 -#if defined(__i386__)=20 +#define L1_PAGETABLE_ENTRIES_I386 1024 +#define L2_PAGETABLE_ENTRIES_I386 1024 + +#if defined(__i386__) #define L1_PAGETABLE_ENTRIES 1024 #define L2_PAGETABLE_ENTRIES 1024 #elif defined(__x86_64__) @@ -95,6 +100,11 @@ typedef unsigned long l4_pgentry_t; #define l3_table_offset_pae(_a) \ (((_a) >> L3_PAGETABLE_SHIFT_PAE) & (L3_PAGETABLE_ENTRIES_PAE - 1)) =20 +#define l1_table_offset_i386(_a) \ + (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES_I386 - 1)) +#define l2_table_offset_i386(_a) \ + (((_a) >> L2_PAGETABLE_SHIFT_I386) & (L2_PAGETABLE_ENTRIES_I386 - 1)) + #if defined(__i386__) #define l1_table_offset(_a) \ (((_a) >> L1_PAGETABLE_SHIFT) & (L1_PAGETABLE_ENTRIES - 1)) --Multipart_Thu_Jun_22_16:36:43_2006-1 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Xen-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel --Multipart_Thu_Jun_22_16:36:43_2006-1--