From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MnECg-0007uH-J4 for qemu-devel@nongnu.org; Mon, 14 Sep 2009 12:14:58 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MnECb-0007rF-VF for qemu-devel@nongnu.org; Mon, 14 Sep 2009 12:14:57 -0400 Received: from [199.232.76.173] (port=55408 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MnECb-0007r9-MX for qemu-devel@nongnu.org; Mon, 14 Sep 2009 12:14:53 -0400 Received: from mail-ew0-f221.google.com ([209.85.219.221]:44416) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MnECb-000382-8H for qemu-devel@nongnu.org; Mon, 14 Sep 2009 12:14:53 -0400 Received: by ewy21 with SMTP id 21so2895687ewy.8 for ; Mon, 14 Sep 2009 09:14:52 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20090914090001.99230@gmx.net> References: <20090914090001.99230@gmx.net> From: Blue Swirl Date: Mon, 14 Sep 2009 19:14:32 +0300 Message-ID: Subject: Re: [Qemu-devel] tlb_update_dirty() question Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Johannes Luber Cc: qemu-devel@nongnu.org On Mon, Sep 14, 2009 at 12:00 PM, Johannes Luber wrote: > Hi, > > Before I state my question I describe my assumptions how Qemu works inter= nally. If I'm wrong there you'll notice it sooner. > > The pointers of the emulation layer are transformed into physical address= es is a two-steps process. The emulated machine itself uses virtual address= es which are represented by the type ram_addr_t. These virtual addresses ar= e different from the one the host OS, in fact they are a completely interna= l representation. > > To actually work with ram_addr_t pointers, these have to be transformed i= nto host virtual addresses. These are represented by target_phys_addr_t poi= nters. To access with the host virtual memory the physical memory, the host= OS does its own magic which is no functionality of Qemu itself. > > Taking all assumptions into account it is certainly possibly that ram_add= r_t can be smaller than target_phys_addr_t. E.g., a 32-bit target system ca= n access only 4 GB of memory while its 64-bit host put that memory anywhere= in the whole address range. > > But then I stumbled over these snippets: > > static inline void tlb_update_dirty(CPUTLBEntry *tlb_entry) > { > =C2=A0 =C2=A0ram_addr_t ram_addr; > =C2=A0 =C2=A0void *p; > > =C2=A0 =C2=A0if ((tlb_entry->addr_write & ~TARGET_PAGE_MASK) =3D=3D IO_ME= M_RAM) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0p =3D (void *)(unsigned long)((tlb_entry->addr= _write & > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 TARGET_PAGE_MASK) + tlb_entry->= addend); > =C2=A0 =C2=A0 =C2=A0 =C2=A0ram_addr =3D qemu_ram_addr_from_host(p); > =C2=A0 =C2=A0 =C2=A0 =C2=A0if (!cpu_physical_memory_is_dirty(ram_addr)) { > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0tlb_entry->addr_write |=3D TLB_N= OTDIRTY; > =C2=A0 =C2=A0 =C2=A0 =C2=A0} > =C2=A0 =C2=A0} > } > > /* Some of the softmmu routines need to translate from a host pointer > =C2=A0 (typically a TLB entry) back to a ram offset. =C2=A0*/ > ram_addr_t qemu_ram_addr_from_host(void *ptr) > { > ... > } > > The comment is particularly insightful. p is supposed to be a host pointe= r yet the initialization code uses "(unsigned long)" in a cast for an expre= ssion which has the type target_phys_addr_t because the struct variable "ad= dend" has this type. The addend is target_phys_addr_t type, because then we can get back to host address ranges on 32 bit host. Consider for example guest address at 8G backed by host memory at 1G: the addend is -7G. > This cast assumes that unsigned long is at least as big as target_phys_ad= dr_t. Under Unix this may be true, but Windows C compilers treat long =3D= =3D int and int remains a 32-bit type. Why isn't simply target_phys_addr_t = used as cast? target_phys_addr_t does support max(target pointer size, host= pointer size), doesn't it? Or is there another option? No, the cast assumes that sum of guest addr and addend is a valid host address, which should be true. For memory, the resulting address is simply pointer to host memory. If any of the lowest bits of the sum are set, the area is MMIO.