From mboxrd@z Thu Jan 1 00:00:00 1970 From: Gleb Natapov Subject: Re: [PATCH 1/2] kvm-unit-tests: Add a func to run instruction in emulator Date: Tue, 18 Jun 2013 19:09:51 +0300 Message-ID: <20130618160951.GA22555@redhat.com> References: <1370532278-22063-1-git-send-email-yzt356@gmail.com> <51B8DF0E.3060105@redhat.com> <20130618124517.GA16557@redhat.com> <20130618154754.GE21032@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: QUOTED-PRINTABLE Cc: Paolo Bonzini , kvm To: =?utf-8?B?5p2O5pil5aWHIDxBcnRodXIgQ2h1bnFpIExpPg==?= Return-path: Received: from mx1.redhat.com ([209.132.183.28]:56410 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754090Ab3FRQJy convert rfc822-to-8bit (ORCPT ); Tue, 18 Jun 2013 12:09:54 -0400 Content-Disposition: inline In-Reply-To: Sender: kvm-owner@vger.kernel.org List-ID: On Tue, Jun 18, 2013 at 11:56:24PM +0800, =E6=9D=8E=E6=98=A5=E5=A5=87 <= Arthur Chunqi Li> wrote: > On Tue, Jun 18, 2013 at 11:47 PM, Gleb Natapov wrot= e: > > On Tue, Jun 18, 2013 at 10:28:59PM +0800, =C3=8A??=C3=8A?=E2=80=A2=C3= =82=E2=80=A2? wrote: > >> On Tue, Jun 18, 2013 at 8:45 PM, Gleb Natapov wr= ote: > >> > On Thu, Jun 13, 2013 at 05:30:03PM +0800, =C3=8A=C3=B9=C3=A9=C3=8A= =C3=B2=E2=80=A2=C3=82=E2=80=A2=C3=A1 wrote: > >> >> Hi Gleb, > >> >> I'm trying to solve these problems in the past days and meet ma= ny > >> >> difficulties. You want to save all the general registers in cal= ling > >> >> insn_page, so registers should be saved to (save) in insn_page. > >> >> Because all the instructions should be generated outside and co= py to > >> >> insn_page, and the instructions generated outside is RIP-relati= ve, so > >> >> inside insn_page (save) will be wrong pointed with RIP-relative= code. > >> >> > >> > They do not have to be generated outside. You can write code int= o > >> > insn_page directly. Something like this outside of any functions= : > >> > > >> > asm(".align 4096\n\t" > >> > ".global insn_page\n\t" > >> > ".global insn_page_end\n\t" > >> > ".global test_insn\n\t" > >> > ".global test_insn_end\n\t" > >> > "insn_page:" > >> > "mov %%rax, outregs \n\t" > >> > ... > >> > "test_insn:\n\t" > >> > "in (%ds), %al\n\t" > >> > ". =3D . + 31\n\t" > >> > "test_insn_end:\n\t" > >> > "mov outregs, %%rax\n\t" > >> > ... > >> > "ret\n\t" > >> > ".align 4096\n\t" > >> > "insn_page_end:\n\t"); > >> > > >> > Now you copy that into alt_insn_page, put instruction you want t= o test > >> > into test_insn offset and remap alt_insn_page into "insn_page" v= irtual address. > >> I used such codes: > >> > >> invlpg((void *)virt_to_phys(insn_page)); > > virt_to_phys? > This is a mistake, I changed it to "invlpg(insn_page)" but the result > is the same. > > > >> asm volatile("call *%0" : : "r"(insn_page)); > >> install_page(cr3, virt_to_phys(alt_insn_page), insn_page); > >> asm volatile("call *%0": : "r"(insn_page+1)); > > +1? > Here I put "ret" on the first byte of insn_page, so the first call of > "insn_page" can just return, and the second call of "insn_page+1=E2=80= =9C will > directly call the second byte, which is the real content of insn_page= =2E Send the code. -- Gleb.