From: =?utf-8?B?6Zmz6Z+L5Lu7IChXZWktUmVuIENoZW4p?= Subject: Re: How KVM sync guest page table with corresponding shadow page table? Date: Fri, 24 Aug 2012 15:29:16 +0800 Message-ID: <20120824072916.GA67464@cs.nctu.edu.tw> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE To: kvm@vger.kernel.org Return-path: Received: from csmailer.cs.nctu.edu.tw ([140.113.235.130]:47500 "EHLO csmailer.cs.nctu.edu.tw" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751382Ab2HXH3V convert rfc822-to-8bit (ORCPT ); Fri, 24 Aug 2012 03:29:21 -0400 Received: from csmailer.cs.nctu.edu.tw (localhost [127.0.0.1]) by csmailer.cs.nctu.edu.tw (Postfix) with ESMTP id DA168529 for ; Fri, 24 Aug 2012 15:29:16 +0800 (CST) Received: from alumni.cs.nctu.edu.tw (alumni.cs.nctu.edu.tw [140.113.235.116]) by csmailer.cs.nctu.edu.tw (Postfix) with ESMTP id AE38D528 for ; Fri, 24 Aug 2012 15:29:16 +0800 (CST) Received: (from chenwj@localhost) by alumni.cs.nctu.edu.tw (8.14.4/8.14.4/Submit) id q7O7TGCF068662 for kvm@vger.kernel.org; Fri, 24 Aug 2012 15:29:16 +0800 (CST) (envelope-from chenwj) Content-Disposition: inline Sender: kvm-owner@vger.kernel.org List-ID: Hi Guangrong, I am not familiar with the term used in paging world, so I need to as= k some dumb questions. > It is controlled by shadow page table, guest-page-tables are write-pr= otected > on shadow pages (the W bit on PTE is cleared). O.K. =20 > There has a special case, called unsync shadow page, if the page only= used > as guest page structure on the lowest level (level =3D 1), we allow i= t to be > writable, it will be sync-ed when the guest flush the tlb (e.g: CR3 r= eload, > invlpg...) because according to x86 TLB rules, it needs to flush tlb = to apply > the change. What "guest page structure on the lowest level" means? Take a simple = two-level page hierarchy as an example, do you mean the page table (not page dire= ctory)? In short, if the architecture requires when OS modifies Nth-level page = table entry, the OS has to flush tlb, then we can unsync shadow page since th= e modifying operations can be trapped by executing tlb flush instruction. Is that r= ight? > In the normal case, guest writes its page table will generate #PF sin= ce the > page is write-protected as we mention above. Let me try to illustrate what happen after HW generating #PF, please = correct me if I am wrong. :) #PF causes an VMExit, and the control returned to KVM. KVM will use g= uest virtual address (I think this information is accompanied with #PF, stor= ed in cr4 on x86 for example) to walk guest page table, and KVM will find guest a= llows this access, so something must be wrong in shadow page table. Then KVM = will go through shadow page table, found the corresponding entry has been se= t to write-protected. Now KVM knows it has to sync guest page table and t= he shadow page table, but how KVM knows what guest OS want to write into g= uest page table entry?=20 Let's assume KVM knows, what's next? KVM will make the guest page tab= le writable by setting shadow page table entry, contine to execute guest instruction which triggers #PF, then sync shadow page table at the same time? It would be great if you can point me out where should I look int= o in KVM source code as a starting code, that makes me understand shadow page table stuff more concrete, and I don't have to guess how things work. :) Thanks! Regards, chenwj --=20 Wei-Ren Chen (=E9=99=B3=E9=9F=8B=E4=BB=BB) Computer Systems Lab, Institute of Information Science, Academia Sinica, Taiwan (R.O.C.) Tel:886-2-2788-3799 #1667 Homepage: http://people.cs.nctu.edu.tw/~chenwj