<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet href='http://feed.feedsky.com/styles/feedsky0.xsl' type='text/xsl' ?><!--这是一个由Feedsy提供技术支持的Feed，为了提高读者阅读的体验，以及满足用户美化自己Feed的需要，我们设计了多种精美的Feed模板，提供给大家选择，所有最终呈现出来的样式，皆由用户自愿选择使用，未经许可，任何团体和个人，请不要擅自修改样式或者盗用，这是对于用户选择权的尊重。--><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:fs="http://www.feedsky.com/namespace/feed" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link href="http://feed.feedsky.com/vm-kernel" type="application/rss+xml" rel="self"></atom:link><fs:self_link href="http://feed.feedsky.com/vm-kernel" type="application/rss+xml"></fs:self_link><lastBuildDate>Fri, 16 Apr 2010 13:20:42 GMT</lastBuildDate><title>vm-kernel</title><description>emulation and virtualization</description><link>http://vm-kernel.org/blog</link><sy:updatePeriod>hourly</sy:updatePeriod><sy:updateFrequency>1</sy:updateFrequency><language>en</language><pubDate>Sun, 31 Oct 2010 16:07:03 GMT</pubDate><item><title>Gdium linux kernel support status</title><link>http://vm-kernel.org/blog/2010/04/16/gdium-linux-kernel-support-status/</link><content:encoded>&lt;p&gt;After several days working, the 2.6.34-rc2 kernel is working on gdium expect sound. Of course most of the codes are from mandriva and Philippe's work.&lt;/p&gt;
&lt;p&gt;I will make some code clean and make the sound work in the next few days. It seems the sm501 sound driver needs a hardcoded 8051 firmware to work. Damn it. After these works are done, I will send the patches to&lt;a href=&quot;http://groups.google.com/group/loongson-dev&quot;&gt; loongson-dev&lt;/a&gt; maillist and merge it to &lt;a href=&quot;http://dev.lemote.com/code/linux-loongson-community&quot;&gt;linux-loongson-community&lt;/a&gt; and linux-mips mainline at last.&lt;/p&gt;
&lt;p&gt;I am keeping moving....... Please wait.&lt;/p&gt;
&lt;p&gt;ps: The linux kernel for gdium repository is &lt;a href=&quot;http://repo.or.cz/w/linux-2.6/linux-mips/linux-gdium.git&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;kill-bill:~# uname -a&lt;br /&gt;
Linux kill-bill 2.6.34-rc2 #24 PREEMPT Fri Apr 16 21:01:51 CST 2010 mips64 GNU/Linux&lt;br /&gt;
kill-bill:~# cat /proc/cpuinfo&lt;br /&gt;
system type             : dexxon-gdium-2f-10inches&lt;br /&gt;
processor               : 0&lt;br /&gt;
cpu model               : ICT Loongson-2 V0.3  FPU V0.1&lt;br /&gt;
BogoMIPS                : 598.01&lt;br /&gt;
wait instruction        : no&lt;br /&gt;
microsecond timers      : yes&lt;br /&gt;
tlb_entries             : 64&lt;br /&gt;
extra interrupt vector  : no&lt;br /&gt;
hardware watchpoint     : yes, count: 0, address/irw mask: []&lt;br /&gt;
ASEs implemented        :&lt;br /&gt;
shadow register sets    : 1&lt;br /&gt;
core                    : 0&lt;br /&gt;
VCED exceptions         : not available&lt;br /&gt;
VCEI exceptions         : not available
&lt;/p&gt;&lt;/blockquote&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560590/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2010/04/16/gdium-linux-kernel-support-status/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560590/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560590/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2010/04/16/gdium-linux-kernel-support-status/feed/</wfw:commentRss><slash:comments>1</slash:comments><description>After several days working, the 2.6.34-rc2 kernel is working on gdium expect sound. Of course most of the codes are from mandriva and Philippe's work.
I will make some code clean and make the sound work in the next few days. It seems the sm501 sound driver needs a hardcoded 8051 firmware to work. Damn it. [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560590/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2010/04/16/gdium-linux-kernel-support-status/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560590/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560590/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>linux</category><category>gdium</category><category>loongson</category><category>kernel</category><pubDate>Fri, 16 Apr 2010 21:20:42 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2010/04/16/gdium-linux-kernel-support-status/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/?p=227</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2010/04/16/gdium-linux-kernel-support-status/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560590/5290193</fs:itemid></item><item><title>Install debian lenny on yeeloong 8089/8101</title><link>http://vm-kernel.org/blog/2010/03/27/install-debian-lenny-on-yeeloong-80898101/</link><content:encoded>&lt;p&gt;NOTICE/TIPS:&lt;br /&gt;
[For one want to install the debian 6.0, there is a more easy way. See the following link.&lt;br /&gt;
&lt;a href=&quot;http://www.anheng.com.cn/loongson/install/readme.txt&quot;&gt;http://www.anheng.com.cn/loongson/install/readme.txt&lt;/a&gt; (In Chinese).]&lt;/p&gt;
&lt;p&gt;Yesterday I installed the debian lenny on yeeloong 8101, the 10.1 inch notebook based on loongson 2F CPU for a friend. Then I find there is less English document describing how to do this. So I write the process down to anyone who is interested in installing debian on yeloong. There are many ways to do it I choose the way of using a debian network installer. Please make sure you have a internet connection first.&lt;/p&gt;
&lt;p&gt;1. First download the kernel and initrd to your PC.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;wget http://dev.lemote.com/drupal/sites/default/files/kernel-2.6.27-LM8089.tar.gz&lt;br /&gt;
wget http://dev.lemote.com/drupal/sites/default/files/initrd_yl_netboot.gz&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;2. Decompress kernel on your PC.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;tar zxvf kernel-2.6.27-LM8089.tar.gz&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;You will get the kernel vmlinux and the directory named lib. The lib directory contains all the kernel modules.&lt;/p&gt;
&lt;p&gt;3. Format your USB disk with ext2 partition and copy vmlinux, directory lib and initrd_yl_netboot.gz to the usb disk.&lt;/p&gt;
&lt;p&gt;4. Insert the usb disk to your netbook and boot it&lt;/p&gt;
&lt;p&gt;5. Enter the PMON command line.&lt;/p&gt;
&lt;p&gt;There are two ways to enter the PMON(the bootloader of yeeloong) command line. One is press DEL when booting. The other way is click C when you see the boot menu.&lt;/p&gt;
&lt;p&gt;Use the following commands to load the kernel and initrd, which contains the debian network installer.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;load /dev/fs/ext2@usb0/vmlinux&lt;br /&gt;
initrd /dev/fs/ext2@usb0/initrd_yl_netboot.gz&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Please be patient. The initrd command may need more than 5 miniutes to be finished.&lt;/p&gt;
&lt;p&gt;Sometimes the PMON bootloader may hang when you boot with a usb disk inserted. I do not know why. The workaround is booting into the default linux system and inserting the usb disk and then rebooting. Or you can use a tftp method to load the kernel and initrd.&lt;/p&gt;
&lt;p&gt;At last use the following command to launch the debian network installer.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;g console=tty no_auto_cmd&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Then just install the debian as normal.&lt;/p&gt;
&lt;p&gt;6. Install debian lenny&lt;/p&gt;
&lt;p&gt;After debian configurating the DHCP, it will complain about &quot;no kernel modules were found&quot; and will let you choose &quot;continue the install without loading kernel modules?&quot;, just choose Yes(the default answer is No) to continue.&lt;/p&gt;
&lt;p&gt;When in the part of Partition disks, it will complain about &quot;The current kernel doesn't support the Logical Volume Manager. You may need to load the lvm-mod modules&quot; and the background becomes red. Do not be scared. Just click continue. &lt;img src='http://vm-kernel.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /&gt; &lt;/p&gt;
&lt;p&gt;Then everything goes as it should be. But at last, debian installer will say &quot;no installable kernel was found in the defined APT sources.... Continue without installing a kernel&quot;. Do not click Yes too quickly. We need to copy the kernel and all the modules into new system first. Please make sure that the USB disk is still inserting on the notebook. Use ALT+F2 to active a console. Mount the use disk and copy kernel and libs.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;mount /dev/sda1 /target/mnt&lt;br /&gt;
cp /mnt/vmlinux /target/boot&lt;br /&gt;
cp -rf /mnt/lib/modules /target/lib/&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Then click ALT+F1 return to the debian installer. Click Yes to continue installing.&lt;/p&gt;
&lt;p&gt;7. Install Desktop environment&lt;/p&gt;
&lt;p&gt;You can install LXDE or gnome as your desktop. I prefer LXDE because it is light.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;apt-get install lxde&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Install the X server driver.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;wget http://www.anheng.com.cn/loongson2f/lenny/xorg-server/xserver-xorg-video-siliconmotion_2.2.8-lemote.r04_mipsel.deb&lt;br /&gt;
dpkg -i xserver-xorg-video-siliconmotion_2.2.8-lemote.r04_mipsel.deb&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Change the xorg.conf according to &lt;a href=&quot;http://wiki.gnewsense.org/Projects/GNewSenseToMIPSYeeloongXorgConf&quot;&gt;this link&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;8. Trouble shooting&lt;/p&gt;
&lt;p&gt;(1) My wifi does not work&lt;/p&gt;
&lt;p&gt;You can see &quot;rtl8187: rtl8187_open process failed because radio off&quot; if you use dmesg to see the message. Use FN+F5 to turn on the wifi first. You will see such message &quot;&lt;span style=&quot;font-family: Courier New;&quot;&gt;rtl8187: SCI interrupt Methord Will Turn Radio On&lt;/span&gt;&quot; on your console.&lt;/p&gt;
&lt;p&gt;(2) My sound does not work&lt;/p&gt;
&lt;p&gt;Use alsamixer to adjust the volume. But install alsa-utils first.&lt;/p&gt;
&lt;p&gt;(3) OOPS, I forget to copy kernel to my new installed system. I can not boot it now. What should I do?&lt;/p&gt;
&lt;p&gt;You can load the kernel using tftp method.&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560591/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2010/03/27/install-debian-lenny-on-yeeloong-80898101/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560591/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560591/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2010/03/27/install-debian-lenny-on-yeeloong-80898101/feed/</wfw:commentRss><slash:comments>7</slash:comments><description>NOTICE/TIPS:
[For one want to install the debian 6.0, there is a more easy way. See the following link.
http://www.anheng.com.cn/loongson/install/readme.txt (In Chinese).]
Yesterday I installed the debian lenny on yeeloong 8101, the 10.1 inch notebook based on loongson 2F CPU for a friend. Then I find there is less English document describing how to do this. So I [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560591/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2010/03/27/install-debian-lenny-on-yeeloong-80898101/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560591/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560591/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>yeeloong</category><category>debian</category><category>loongson</category><pubDate>Sat, 27 Mar 2010 14:28:51 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2010/03/27/install-debian-lenny-on-yeeloong-80898101/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/?p=221</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2010/03/27/install-debian-lenny-on-yeeloong-80898101/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560591/5290193</fs:itemid></item><item><title>UNSW Advanced OS about L4</title><link>http://vm-kernel.org/blog/2010/01/22/unsw-advanced-os/</link><content:encoded>&lt;p&gt;For someone who is interested in OS and micro-kernel especially L4.&lt;/p&gt;
&lt;p&gt;Part 1:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/v/KtyIFdPd0bU&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/v/KtyIFdPd0bU&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Part 2:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/v/3ZN40MyiGuk&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/v/3ZN40MyiGuk&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;&lt;span id=&quot;more-206&quot;&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Part 3:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/v/pWnzaGezzmw&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/v/pWnzaGezzmw&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Part 4:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/v/NdztxXTG-AQ&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/v/NdztxXTG-AQ&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Part 5:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/v/YhMEBQPZ__U&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/v/YhMEBQPZ__U&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Part 6:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/l/cz-uhtvDLk0&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/l/cz-uhtvDLk0&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Part 7:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/l/cz-uhtvDLk0&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/l/cz-uhtvDLk0&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;
&lt;p&gt;Part 8:&lt;/p&gt;
&lt;p&gt;&lt;object classid=&quot;clsid:d27cdb6e-ae6d-11cf-96b8-444553540000&quot; width=&quot;420&quot; height=&quot;363&quot; codebase=&quot;http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0&quot;&gt;&lt;param name=&quot;allowFullScreen&quot; value=&quot;true&quot; /&gt;&lt;param name=&quot;allowscriptaccess&quot; value=&quot;always&quot; /&gt;&lt;param name=&quot;wmode&quot; value=&quot;opaque&quot; /&gt;&lt;param name=&quot;src&quot; value=&quot;http://www.tudou.com/l/cz-uhtvDLk0&quot; /&gt;&lt;param name=&quot;allowfullscreen&quot; value=&quot;true&quot; /&gt;&lt;embed type=&quot;application/x-shockwave-flash&quot; width=&quot;420&quot; height=&quot;363&quot; src=&quot;http://www.tudou.com/l/cz-uhtvDLk0&quot; wmode=&quot;opaque&quot; allowscriptaccess=&quot;always&quot; allowfullscreen=&quot;true&quot;&gt;&lt;/embed&gt;&lt;/object&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560592/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2010/01/22/unsw-advanced-os/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560592/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560592/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2010/01/22/unsw-advanced-os/feed/</wfw:commentRss><slash:comments>0</slash:comments><description>For someone who is interested in OS and micro-kernel especially L4.
Part 1:

Part 2:


Part 3:

Part 4:

Part 5:

Part 6:

Part 7:

Part 8:&lt;img src=&quot;http://www1.feedsky.com/t1/430560592/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2010/01/22/unsw-advanced-os/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560592/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560592/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>OS</category><category>L4</category><category>Micro-kernel</category><category>Mircro-kernel</category><pubDate>Fri, 22 Jan 2010 16:18:05 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2010/01/22/unsw-advanced-os/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/?p=206</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2010/01/22/unsw-advanced-os/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560592/5290193</fs:itemid></item><item><title>performance of loongson 2f</title><link>http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/</link><content:encoded>&lt;p&gt;Some friends ask me about the performance of loongson2f. They want to know whether the performance of loongson 2f can surpass Marvell Sheeva CPU. Well I can not just say it's better or worse without giving the benchmark data.&lt;br /&gt;
Since there is a &lt;a href=&quot;http://computingplugs.com/index.php/SheevaPlug_Performance#CPU_performance_with_nbench-byte-2.2.3&quot;&gt;benchmark result&lt;/a&gt; of Marvell Sheeva CPU, we can run the same benchmark program on loongson 2f. The benchmark program is nbench.&lt;br /&gt;
Machine: gdium&lt;br /&gt;
OS: Debian squeeze&lt;br /&gt;
Kernel: Linux&lt;/p&gt;
&lt;p&gt;1. gcc-4.3.4&lt;br /&gt;
CFLAGS = -s -static -Wall -O3&lt;/p&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;TEST&lt;/td&gt;
&lt;td&gt;Iterations/sec.&lt;/td&gt;
&lt;td&gt;Old Index&lt;/td&gt;
&lt;td&gt;New Index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td&gt;Pentium 90*&lt;/td&gt;
&lt;td&gt;AMD K6/233*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NUMERIC SORT&lt;/td&gt;
&lt;td&gt;358.24.&lt;/td&gt;
&lt;td&gt;9.19&lt;/td&gt;
&lt;td&gt;3.02&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STRING SORT&lt;/td&gt;
&lt;td&gt;33.041&lt;/td&gt;
&lt;td&gt;14.76&lt;/td&gt;
&lt;td&gt;2.29&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BITFIELD&lt;/td&gt;
&lt;td&gt;5.5164e+07&lt;/td&gt;
&lt;td&gt;9.46&lt;/td&gt;
&lt;td&gt;1.98&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FP EMULATION&lt;/td&gt;
&lt;td&gt;47.402&lt;/td&gt;
&lt;td&gt;22.75&lt;/td&gt;
&lt;td&gt;5.25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FOURIER&lt;/td&gt;
&lt;td&gt;4721.1&lt;/td&gt;
&lt;td&gt;5.37&lt;/td&gt;
&lt;td&gt;3.02&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ASSIGNMENT&lt;/td&gt;
&lt;td&gt;7.0534&lt;/td&gt;
&lt;td&gt;26.84&lt;/td&gt;
&lt;td&gt;6.96&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDEA&lt;/td&gt;
&lt;td&gt;1597.4&lt;/td&gt;
&lt;td&gt;24.43&lt;/td&gt;
&lt;td&gt;7.25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HUFFMAN&lt;/td&gt;
&lt;td&gt;575.17&lt;/td&gt;
&lt;td&gt;15.95&lt;/td&gt;
&lt;td&gt;5.09&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NEURAL NET&lt;/td&gt;
&lt;td&gt;4.2065&lt;/td&gt;
&lt;td&gt;6.76&lt;/td&gt;
&lt;td&gt;2.84&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LU DECOMPOSITION&lt;/td&gt;
&lt;td&gt;107.28&lt;/td&gt;
&lt;td&gt;5.56&lt;/td&gt;
&lt;td&gt;4.01&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;==========ORIGINAL BYTEMARK RESULTS========&lt;br /&gt;
INTEGER INDEX : 16.297&lt;br /&gt;
FLOATING-POINT INDEX: 5.864&lt;br /&gt;
Baseline (MSDOS*) : Pentium* 90, 256 KB L2-cache, Watcom* compiler 10.0&lt;br /&gt;
==========LINUX DATA BELOW============&lt;br /&gt;
CPU                 :&lt;br /&gt;
L2 Cache            :&lt;br /&gt;
OS                  : Linux 2.6.24-gdium-1&lt;br /&gt;
C compiler          : gcc version 4.3.4 (Debian 4.3.4-5)&lt;br /&gt;
libc                : libc-2.9.so&lt;br /&gt;
MEMORY INDEX        : 3.156&lt;br /&gt;
INTEGER INDEX       : 4.918&lt;br /&gt;
FLOATING-POINT INDEX: 3.252&lt;br /&gt;
Baseline (LINUX)    : AMD K6/233*, 512 KB L2-cache, gcc 2.7.2.3, libc-5.4.38&lt;br /&gt;
* Trademarks are property of their respective holder.&lt;/p&gt;
&lt;p&gt;2. gcc-4.4&lt;br /&gt;
CFLAGS = -s -static -Wall -O3 -fomit-frame-pointer -funroll-loops&lt;br /&gt;
CFLAGS += -march=loongson2f  -mtune=loongson2f  -mabi=n32&lt;/p&gt;
&lt;table border=&quot;1&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;TEST&lt;/td&gt;
&lt;td&gt;Iterations/sec.&lt;/td&gt;
&lt;td&gt;Old Index&lt;/td&gt;
&lt;td&gt;New Index&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td&gt; &lt;/td&gt;
&lt;td&gt;Pentium 90*&lt;/td&gt;
&lt;td&gt;AMD K6/233*&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NUMERIC SORT&lt;/td&gt;
&lt;td&gt;366.08&lt;/td&gt;
&lt;td&gt;9.39&lt;/td&gt;
&lt;td&gt;3.08&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;STRING SORT&lt;/td&gt;
&lt;td&gt;46.686&lt;/td&gt;
&lt;td&gt;20.86&lt;/td&gt;
&lt;td&gt;3.23&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;BITFIELD&lt;/td&gt;
&lt;td&gt;4.764e+07&lt;/td&gt;
&lt;td&gt;8.17&lt;/td&gt;
&lt;td&gt;1.71&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FP EMULATION&lt;/td&gt;
&lt;td&gt;90.2&lt;/td&gt;
&lt;td&gt;43.28&lt;/td&gt;
&lt;td&gt;9.99&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;FOURIER&lt;/td&gt;
&lt;td&gt;5171.9&lt;/td&gt;
&lt;td&gt;5.88&lt;/td&gt;
&lt;td&gt;3.30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ASSIGNMENT&lt;/td&gt;
&lt;td&gt;11.094&lt;/td&gt;
&lt;td&gt;42.21&lt;/td&gt;
&lt;td&gt;10.95&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IDEA&lt;/td&gt;
&lt;td&gt;1726.9&lt;/td&gt;
&lt;td&gt;26.41&lt;/td&gt;
&lt;td&gt;7.84&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HUFFMAN&lt;/td&gt;
&lt;td&gt;605&lt;/td&gt;
&lt;td&gt;16.78&lt;/td&gt;
&lt;td&gt;5.36&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NEURAL NET&lt;/td&gt;
&lt;td&gt;9.761&lt;/td&gt;
&lt;td&gt;15.68&lt;/td&gt;
&lt;td&gt;6.60&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;LU DECOMPOSITION&lt;/td&gt;
&lt;td&gt;215.64&lt;/td&gt;
&lt;td&gt;11.17&lt;/td&gt;
&lt;td&gt;8.07&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;==========ORIGINAL BYTEMARK RESULTS========&lt;br /&gt;
INTEGER INDEX       : 20.035&lt;br /&gt;
FLOATING-POINT INDEX: 10.100&lt;br /&gt;
Baseline (MSDOS*)   : Pentium* 90, 256 KB L2-cache, Watcom* compiler 10.0&lt;br /&gt;
==========LINUX DATA BELOW============&lt;br /&gt;
CPU                 :&lt;br /&gt;
L2 Cache            :&lt;br /&gt;
OS                  : Linux 2.6.24-gdium-1&lt;br /&gt;
C compiler          : gcc-4.4&lt;br /&gt;
libc                : libc-2.9.so&lt;br /&gt;
MEMORY INDEX        : 3.922&lt;br /&gt;
INTEGER INDEX       : 5.997&lt;br /&gt;
FLOATING-POINT INDEX: 5.602&lt;br /&gt;
Baseline (LINUX)    : AMD K6/233*, 512 KB L2-cache, gcc 2.7.2.3, libc-5.4.38&lt;br /&gt;
* Trademarks are property of their respective holder.&lt;/p&gt;
&lt;div class=&quot;zemanta-pixie&quot;&gt;&lt;img class=&quot;zemanta-pixie-img&quot; src=&quot;http://img.zemanta.com/pixy.gif?x-id=fe9d2c4f-b064-8a94-9f49-f0e49d152a7a&quot; alt=&quot;&quot; /&gt;&lt;/div&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560593/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560593/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560593/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/feed/</wfw:commentRss><slash:comments>1</slash:comments><description>Some friends ask me about the performance of loongson2f. They want to know whether the performance of loongson 2f can surpass Marvell Sheeva CPU. Well I can not just say it's better or worse without giving the benchmark data.
Since there is a benchmark result of Marvell Sheeva CPU, we can run the same benchmark program [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560593/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560593/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560593/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>gdium</category><category>loongson</category><category>nbench</category><category>loongson 2f</category><pubDate>Fri, 30 Oct 2009 17:11:31 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/10/30/performance-of-loongson-2f/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560593/5290193</fs:itemid></item><item><title>qemu mips msub instruction emulation bug</title><link>http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/</link><content:encoded>&lt;p&gt;It's really a long time since last post. Now I am working on the android mips porting project. I want to run android on the MIPS emulator.&lt;/p&gt;
&lt;p&gt;The problem is that when I run mips-android on qemu, it hangs when executing init program in the initramfs root file-system. Then I use the remote gdb to debug the init and finds out that it it because pa_workspace is not initiated.&lt;/p&gt;
&lt;p&gt;Function ashmem_create_region will open /dev/ashmem and return the fd if succeed. However, it returns -1 and the errno is 19 which means NO SUCH DEVICES.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;fd = ashmem_create_region(&quot;system_properties&quot;, size);&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The problem is who is responsible for creating /dev/ashmem?&lt;/p&gt;
&lt;p&gt;In fact, in android it uses udev mechanism to create devices in /dev when executing function device_init. The full cold patch to create a device is as following:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;device_init-&amp;gt;coldboot-&amp;gt;do_coldboot-&amp;gt;write(fd, &quot;add\n&quot;, 4)-&amp;gt;handle_device_fd-&amp;gt;handle_device_event-&amp;gt;make_device&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;In function parse_event, it will parse the uevent msg and then pass uevent to handle_device_event. However, I find that the uevent message is a little weird. I use remote gdb to dump this message.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;0x7ff3d250:     &quot;add@/class/tty/console&quot;&lt;br /&gt;
0x7ff3d267:     &quot;ACTION=add&quot;&lt;br /&gt;
0x7ff3d272:     &quot;DEVPATH=/class/tty/console&quot;&lt;br /&gt;
0x7ff3d28d:     &quot;SUBSYSTEM=tty&quot;&lt;br /&gt;
0x7ff3d29b:     &quot;MAJOR=+&quot;&lt;br /&gt;
0x7ff3d2a3:     &quot;MINOR=/&quot;&lt;br /&gt;
0x7ff3d2ab:     &quot;SEQNUM=31+&quot;&lt;br /&gt;
0x7ff3d2b6:     &quot;&quot;&lt;br /&gt;
0x7ff3d2b7:     &quot;&quot;&lt;br /&gt;
0x7ff3d2b8:     &quot;&quot;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;You see, in the message the MAJOR is +. It will confuse the parse_event so that the corresponding device won't be created.&lt;/p&gt;
&lt;p&gt;It looks the kernel passes wrong uevent message to user space. So the question is who has messed up the uevent message?&lt;/p&gt;
&lt;p&gt;Then I recall that when booting linux kernel, there are some weird messages.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Primary instruction cache .kB, VIPT, 2-way, linesize 1* bytes.&lt;br /&gt;
Primary data cache .kB, 2-way, VIPT, no aliases, linesize 1* bytes&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;You see the instruction cache is .kB and linesize is 1* bytes, not a valid number at all.&lt;/p&gt;
&lt;p&gt;Then I suspect that something is wrong in kernel when parsing the numbers. So I use the remote gdb to debug the kernel again.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;r4k_cache_init-&amp;gt;probe_pcache-&amp;gt;printk-&amp;gt;vprintk-&amp;gt;vscnprintf-&amp;gt;vsnprintf-&amp;gt;number-&amp;gt;put_dec-&amp;gt;put_dec_trunc&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Function put_dec_trunc uses a unsigned int between[0,99999] as input and outputs the number as a string. But I find that when the input is 2, the output is '.', not the expected '2'. So maybe this function is the bad boy.&lt;/p&gt;
&lt;p&gt;In the following function I assume q=2.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;277 static char* put_dec_trunc(char *buf, unsigned q)&lt;br /&gt;
278 {&lt;br /&gt;
279         unsigned d3, d2, d1, d0;&lt;br /&gt;
280         d1 = (q&amp;gt;&amp;gt;4) &amp;amp; 0xf;            /*d1=0*/&lt;br /&gt;
281         d2 = (q&amp;gt;&amp;gt;8) &amp;amp; 0xf;            /*d2=0*/&lt;br /&gt;
282         d3 = (q&amp;gt;&amp;gt;12);                 /*d3=0*/&lt;br /&gt;
283&lt;br /&gt;
284         d0 = 6*(d3 + d2 + d1) + (q &amp;amp; 0xf);  /*d0=2*/&lt;br /&gt;
285         q = (d0 * 0xcd) &amp;gt;&amp;gt; 11;              /*q=0*/&lt;br /&gt;
286         d0 = d0 - 10*q;                     /*d==2*/&lt;br /&gt;
287         *buf++ = d0 + ''; /* least significant digit */&lt;br /&gt;
288         d1 = q + 9*d3 + 5*d2 + d1;   /*d1=0*/&lt;br /&gt;
289         if (d1 != 0) {               /* it is false so we won't get into here.*/&lt;br /&gt;
290                 q = (d1 * 0xcd) &amp;gt;&amp;gt; 11;&lt;br /&gt;
291                 d1 = d1 - 10*q;&lt;br /&gt;
292                 *buf++ = d1 + ''; /* next digit */&lt;br /&gt;
293&lt;br /&gt;
294                 d2 = q + 2*d2;&lt;br /&gt;
295                 if ((d2 != 0) || (d3 != 0)) {&lt;br /&gt;
296                         q = (d2 * 0xd) &amp;gt;&amp;gt; 7;&lt;br /&gt;
297                         d2 = d2 - 10*q;&lt;br /&gt;
298                         *buf++ = d2 + ''; /* next digit */&lt;br /&gt;
299&lt;br /&gt;
300                         d3 = q + 4*d3;&lt;br /&gt;
301                         if (d3 != 0) {&lt;br /&gt;
302                                 q = (d3 * 0xcd) &amp;gt;&amp;gt; 11;&lt;br /&gt;
303                                 d3 = d3 - 10*q;&lt;br /&gt;
304                                 *buf++ = d3 + '';  /* next digit */&lt;br /&gt;
305                                 if (q != 0)&lt;br /&gt;
306                                         *buf++ = q + '';  /* most sign. digit */&lt;br /&gt;
307                         }&lt;br /&gt;
308                 }&lt;br /&gt;
309         }    &lt;br /&gt;
310         return buf;&lt;br /&gt;
311 }&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;/*  MIPS uses a0/a1 to pass arguments.&lt;br /&gt;
 *  a0= address of bu&lt;br /&gt;
 *  a1= q = 2&lt;br /&gt;
 */&lt;br /&gt;
8015a040 &amp;lt;put_dec_trunc&amp;gt;:&lt;br /&gt;
8015a040:    00051202     srl    v0,a1,0x8        /* v0= q&amp;gt;&amp;gt;8*/&lt;br /&gt;
8015a044:    00053102     srl    a2,a1,0x4        /* a2= q&amp;gt;&amp;gt;4*/&lt;br /&gt;
8015a048:    30c6000f     andi    a2,a2,0xf        /* a2= (q&amp;gt;&amp;gt;4) &amp;amp; 0xf = d1 in line 280*/&lt;br /&gt;
8015a04c:    3048000f     andi    t0,v0,0xf        /* t0 = (q&amp;gt;&amp;gt;8) &amp;amp; 0xf = d2 in line 281*/&lt;br /&gt;
8015a050:    00054b02     srl    t1,a1,0xc        /* t1= (q&amp;gt;&amp;gt;12) = d3 in line 282*/&lt;br /&gt;
8015a054:    00c81021     addu    v0,a2,t0        &lt;br /&gt;
8015a058:    00491021     addu    v0,v0,t1          /* v0 = d1+d2+d3 in line 284*/&lt;br /&gt;
8015a05c:    24030006     li    v1,6&lt;br /&gt;
8015a060:    70433802     mul    a3,v0,v1          /*a3= 6*(d3 + d2 + d1)*/&lt;br /&gt;
8015a064:    30a5000f     andi    a1,a1,0xf         /*a1= q &amp;amp; 0xf*/&lt;br /&gt;
8015a068:    24020009     li    v0,9               /*v0=9*/&lt;br /&gt;
8015a06c:    00e56021     addu    t4,a3,a1          /*t4= 6*(d3 + d2 + d1) + (q &amp;amp; 0xf) = d0 in line 284*/&lt;br /&gt;
8015a070:    240b00cd     li    t3,205            /*t3= 0xcd*/&lt;br /&gt;
8015a074:    71223802     mul    a3,t1,v0          /*a3= 9*d3 in line 288. Apparently gcc has reordered the code.*/   &lt;br /&gt;
8015a078:    718b1802     mul    v1,t4,t3          /*v1= (d0*0xcd)*/&lt;br /&gt;
8015a07c:    24020005     li    v0,5&lt;br /&gt;
8015a080:    00e62821     addu    a1,a3,a2          /*a1= 9*d3 + d1 in line 288*/&lt;br /&gt;
8015a084:    71023002     mul    a2,t0,v0          /*a2= 5*d2 in line 288*/&lt;br /&gt;
8015a088:    00031ac2     srl    v1,v1,0xb         /*v1= (d0*0xcd)&amp;gt;&amp;gt;11 in line 285*/&lt;br /&gt;
8015a08c:    240a000a     li    t2,10             /*t2=10*/&lt;br /&gt;
8015a090:    01800013     mtlo    t4                /*put t4-&amp;gt;lo. lo=t4= 6*(d3 + d2 + d1) + (q &amp;amp; 0xf) = d0 in line 284 */&lt;br /&gt;
8015a094:    706a0004     msub    v1,t2             /*hilo = v1*t2 - hilo = 10*(q) - hilo in line 286*/&lt;br /&gt;
8015a098:    00c51021     addu    v0,a2,a1&lt;br /&gt;
8015a09c:    00002812     mflo    a1                /*lo-&amp;gt;a1*/    &lt;br /&gt;
8015a0a0:    00431821     addu    v1,v0,v1&lt;br /&gt;
8015a0a4:    24a20030     addiu    v0,a1,48         &lt;br /&gt;
8015a0a8:    00803821     move    a3,a0&lt;br /&gt;
8015a0ac:    a0820000     sb    v0,0(a0)&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;We just need to see the instruction in 0x8015a094, it is a msub instruction. The defination of msub is as following:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;(HI,LO) = (HI,LO) - (GRP[RS]*GPR[RT])&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Then after executing the instruction in 0x8015a094, the HI/LO should be 0/2. But qemu produces the value 0xffffffff/0xfffffffe, which is -2 indeed. Maybe this is the problem.&lt;/p&gt;
&lt;p&gt;Then I need to find how qemu emulates msub instruction.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;2178     case OPC_MSUB:&lt;br /&gt;
2179         {&lt;br /&gt;
2180             TCGv r_tmp1 = tcg_temp_new(TCG_TYPE_I64);&lt;br /&gt;
2181             TCGv r_tmp2 = tcg_temp_new(TCG_TYPE_I64);&lt;br /&gt;
2182             TCGv r_tmp3 = tcg_temp_new(TCG_TYPE_I64);&lt;br /&gt;
2183&lt;br /&gt;
2184             tcg_gen_ext32s_tl(t0, t0);&lt;br /&gt;
2185             tcg_gen_ext32s_tl(t1, t1);&lt;br /&gt;
2186             tcg_gen_ext_tl_i64(r_tmp1, t0);&lt;br /&gt;
2187             tcg_gen_ext_tl_i64(r_tmp2, t1);&lt;br /&gt;
2188             tcg_gen_mul_i64(r_tmp1, r_tmp1, r_tmp2);  /*r_tmp1= gpr[rs]*gpr[rt] */&lt;br /&gt;
2189             gen_load_LO(t0, 0);                /*t0 &amp;lt;- lo*/&lt;br /&gt;
2190             gen_load_HI(t1, 0);                /*t1 &amp;lt;- hi*/&lt;br /&gt;
2191             tcg_gen_extu_tl_i64(r_tmp2, t0);   /*r_tmp2 = 64bit expand of lo*/&lt;br /&gt;
2192             tcg_gen_extu_tl_i64(r_tmp3, t1);   /*r_tmp3 = 64bit expand of hi*/&lt;br /&gt;
2193             tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);&lt;br /&gt;
2194             tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);&lt;br /&gt;
2195             tcg_temp_free(r_tmp3);&lt;br /&gt;
2196             tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2); /*r_tmp1= r_tmp1 - r_tmp2 = gpr[rs]*gpr[rt] - HI/LO */&lt;br /&gt;
2197             tcg_temp_free(r_tmp2);&lt;br /&gt;
2198             tcg_gen_trunc_i64_tl(t0, r_tmp1);&lt;br /&gt;
2199             tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);&lt;br /&gt;
2200             tcg_gen_trunc_i64_tl(t1, r_tmp1);&lt;br /&gt;
2201             tcg_temp_free(r_tmp1);&lt;br /&gt;
2202             tcg_gen_ext32s_tl(t0, t0);&lt;br /&gt;
2203             tcg_gen_ext32s_tl(t1, t1);&lt;br /&gt;
2204             gen_store_LO(t0, 0);&lt;br /&gt;
2205             gen_store_HI(t1, 0);&lt;br /&gt;
2206         }&lt;br /&gt;
2207         opn = &quot;msub&quot;;&lt;br /&gt;
2208         break;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;You see, qemu makes an error emulation of msub instruction. It uses gpr[rs]*gpr[rt]-HI/LO and then put the results to HI/LO, which is different from the defination of msub instruction. I patched the qemu code and it works.&lt;/p&gt;
&lt;p&gt;BTW: MIPS32 4KTM Processor Core Family Software User’s Manual version MD00016 gives an error operation of msub instruction on papge 253.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Operation:&lt;br /&gt;
   temp ← (HI || LO) - (GPR[rs] * GPR[rt])&lt;br /&gt;
   HI ← temp63..32&lt;br /&gt;
   LO ← temp31..0&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Maybe this misleads the qemu developers. The latest qemu version has fixed this bug. We can see this instruction emulation in qemu-svn-20091014.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;2195     case OPC_MSUB:&lt;br /&gt;
2196         {&lt;br /&gt;
2197             TCGv_i64 t2 = tcg_temp_new_i64();&lt;br /&gt;
2198             TCGv_i64 t3 = tcg_temp_new_i64();&lt;br /&gt;
2199&lt;br /&gt;
2200             tcg_gen_ext_tl_i64(t2, t0);&lt;br /&gt;
2201             tcg_gen_ext_tl_i64(t3, t1);&lt;br /&gt;
2202             tcg_gen_mul_i64(t2, t2, t3);  /*t2=GPR[RS]*GPR[RT] */&lt;br /&gt;
2203             tcg_gen_concat_tl_i64(t3, cpu_LO[0], cpu_HI[0]);  /*t3= HI/LO*/&lt;br /&gt;
2204             tcg_gen_sub_i64(t2, t3, t2); /*t2= HI/LO - GPR[RS]*GPR[RT] */&lt;br /&gt;
2205             tcg_temp_free_i64(t3);&lt;br /&gt;
2206             tcg_gen_trunc_i64_tl(t0, t2);&lt;br /&gt;
2207             tcg_gen_shri_i64(t2, t2, 32);&lt;br /&gt;
2208             tcg_gen_trunc_i64_tl(t1, t2);&lt;br /&gt;
2209             tcg_temp_free_i64(t2);&lt;br /&gt;
2210             tcg_gen_ext32s_tl(cpu_LO[0], t0);&lt;br /&gt;
2211             tcg_gen_ext32s_tl(cpu_HI[0], t1);&lt;br /&gt;
2212         }&lt;br /&gt;
2213         opn = &quot;msub&quot;;&lt;br /&gt;
2214         break;&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;See &lt;a href=&quot;http://www.archivum.info/qemu-devel@nongnu.org/2009-07/00011/%5BQemu-devel%5D_%5BPATCH%5D_target-mips:_fix_MADD_and_MSUB_MSUBU_instructions&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; for more information.&lt;/p&gt;
&lt;p&gt;So finding this bug is really not easy. I have to dig dig and dig from userland to linux kernel and then to qemu until catching this bad qemu bug. Thanks to the remote gdb and gdb stub in qemu, it makes life easier.&lt;/p&gt;
&lt;p&gt;Following is the patch of qemu.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;diff --git a/target-mips/translate.c b/target-mips/translate.c&lt;br /&gt;
index 3dded6c..0a1b461 100644&lt;br /&gt;
--- a/target-mips/translate.c&lt;br /&gt;
+++ b/target-mips/translate.c&lt;br /&gt;
@@ -2193,7 +2193,11 @@ static void gen_muldiv (DisasContext *ctx, uint32_t opc,&lt;br /&gt;
             tcg_gen_shli_i64(r_tmp3, r_tmp3, 32);&lt;br /&gt;
             tcg_gen_or_i64(r_tmp2, r_tmp2, r_tmp3);&lt;br /&gt;
             tcg_temp_free(r_tmp3);&lt;br /&gt;
-            tcg_gen_sub_i64(r_tmp1, r_tmp1, r_tmp2);&lt;br /&gt;
+            /* msub means HI/LO = HI/LO - GPR[RS]*GPR[RT],&lt;br /&gt;
+             * not HI/LO = GPR[RS]*GPR[RT] - HI/LO&lt;br /&gt;
+             */&lt;br /&gt;
+            //tcg_gen_sub_i64(r_tmp1, r_tmp2, r_tmp2);&lt;br /&gt;
+            tcg_gen_sub_i64(r_tmp1, r_tmp2, r_tmp1);&lt;br /&gt;
             tcg_temp_free(r_tmp2);&lt;br /&gt;
             tcg_gen_trunc_i64_tl(t0, r_tmp1);&lt;br /&gt;
             tcg_gen_shri_i64(r_tmp1, r_tmp1, 32);&lt;/p&gt;&lt;/blockquote&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560594/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560594/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560594/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/feed/</wfw:commentRss><slash:comments>0</slash:comments><description>It's really a long time since last post. Now I am working on the android mips porting project. I want to run android on the MIPS emulator.
The problem is that when I run mips-android on qemu, it hangs when executing init program in the initramfs root file-system. Then I use the remote gdb to debug [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560594/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560594/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560594/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>ARM/MIPS</category><category>emulation</category><pubDate>Thu, 15 Oct 2009 13:06:22 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/10/15/qemu-mips-msub-instruction-emulation-bug/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560594/5290193</fs:itemid></item><item><title>qemu internal part 3: memory watchpoint</title><link>http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/</link><content:encoded>&lt;p align=&quot;justify&quot;&gt;In qemu there is an amazing feature – memory watchpoint. It can watch all the memory access including memory read, write or both of them. When guest os/application touches the memory region watched by qemu, a registered function will be called and you can do everything as you want in this function. The gdb stub in qemu uses it to implement the memory watch command.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;The implemention of memory watchpoint is tricky in qemu. &lt;a href=&quot;http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/&quot;&gt;In last article of qemu internal&lt;/a&gt;, we know that when emulating memory access, qemu needs to distinguish the normal RAM read/write from memory mapped I/O read/write. If it is a memory mapped I/O address access, qemu will dispatch this access to the registered I/O emulation functions. Qemu use this mechanism to implement the memory watchpoint. When accessing the memory address watched by qemu, qemu will dispatch this access to the registered memory watch functions, even if this address is normal guest RAM address or memory mapped I/O address! Qemu will do all the magic things in these memory watch functions.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;In the following, I will use an example to explain the whole process of memory watch implement of qemu.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;80103c60 &amp;lt;memcpy&amp;gt;:     &lt;br /&gt;80103c60:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 00801021&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; move&amp;#160;&amp;#160;&amp;#160; v0,a0      &lt;br /&gt;80103c64 &amp;lt;__copy_user&amp;gt;:      &lt;br /&gt;80103c64:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 2cca0004&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; sltiu&amp;#160;&amp;#160; t2,a2,4      &lt;br /&gt;80103c68:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 30890003&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; andi&amp;#160;&amp;#160;&amp;#160; t1,a0,0x3      &lt;br /&gt;80103c6c:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 15400068&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bnez&amp;#160;&amp;#160;&amp;#160; t2,80103e10 &amp;lt;__copy_user+0x1ac&amp;gt;      &lt;br /&gt;80103c70:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 30a80003&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; andi&amp;#160;&amp;#160;&amp;#160; t0,a1,0x3      &lt;br /&gt;80103c74:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 1520003d&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bnez&amp;#160;&amp;#160;&amp;#160; t1,80103d6c &amp;lt;__copy_user+0x108&amp;gt;      &lt;br /&gt;80103c78:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 00000000&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nop      &lt;br /&gt;80103c7c:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 15000046&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bnez&amp;#160;&amp;#160;&amp;#160; t0,80103d98 &amp;lt;__copy_user+0x134&amp;gt;      &lt;br /&gt;80103c80:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 00064142&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; srl&amp;#160;&amp;#160;&amp;#160;&amp;#160; t0,a2,0x5      &lt;br /&gt;80103c84:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 11000017&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; beqz&amp;#160;&amp;#160;&amp;#160; t0,80103ce4 &amp;lt;__copy_user+0x80&amp;gt;      &lt;br /&gt;80103c88:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 30d8001f&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; andi&amp;#160;&amp;#160;&amp;#160; t8,a2,0x1f      &lt;br /&gt;80103c8c:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 00000000&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; nop      &lt;br /&gt;80103c90:&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; 8ca80000&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; lw&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; t0,0(a1)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p align=&quot;justify&quot;&gt;These asm lines are objdumped from linux 2.6.30 kernel for mips malta. Assume that I want to&amp;#160; watch the memory access of virtual address 0x804cd000(&lt;em&gt;swapper_pg_dir&lt;/em&gt; in linux kernel).&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;First I insert the watchpoint into cpu. &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;cpu_watchpoint_insert(env, 0x804cd000, 4, BP_GDB | BP_MEM_ACCESS,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; NULL);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;And then I need to register the vm state changing call back functions.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;qemu_add_vm_change_state_handler(spy_vm_state_change, NULL);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p align=&quot;justify&quot;&gt;If register a1=0x804cd000, guest linux kernel will touch the watched memory region when pc is 0x80103c90, then qemu dispatches this access to the registered memory watch function, even if this access is a noram guest RAM access.The memory watch functions in qemu are in array &lt;em&gt;watch_mem_read/watch_mem_write&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;exec.c&lt;/p&gt;
&lt;p&gt;2649 static CPUReadMemoryFunc *watch_mem_read[3] = {     &lt;br /&gt;2650&amp;#160;&amp;#160;&amp;#160;&amp;#160; watch_mem_readb,      &lt;br /&gt;2651&amp;#160;&amp;#160;&amp;#160;&amp;#160; watch_mem_readw,      &lt;br /&gt;2652&amp;#160;&amp;#160;&amp;#160;&amp;#160; watch_mem_readl,      &lt;br /&gt;2653 };      &lt;br /&gt;2654       &lt;br /&gt;2655 static CPUWriteMemoryFunc *watch_mem_write[3] = {      &lt;br /&gt;2656&amp;#160;&amp;#160;&amp;#160;&amp;#160; watch_mem_writeb,      &lt;br /&gt;2657&amp;#160;&amp;#160;&amp;#160;&amp;#160; watch_mem_writew,      &lt;br /&gt;2658&amp;#160;&amp;#160;&amp;#160;&amp;#160; watch_mem_writel,      &lt;br /&gt;2659 };&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In function &lt;em&gt;watch_mem_readl&lt;/em&gt;, it will call function &lt;em&gt;check_watchpoint&lt;/em&gt; first.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;exec.c&lt;/p&gt;
&lt;p&gt;2622 static uint32_t watch_mem_readl(void *opaque, target_phys_addr_t addr)     &lt;br /&gt;2623 {      &lt;br /&gt;2624&amp;#160;&amp;#160;&amp;#160;&amp;#160; check_watchpoint(addr &amp;amp; ~TARGET_PAGE_MASK, ~0x3, BP_MEM_READ);      &lt;br /&gt;2625&amp;#160;&amp;#160;&amp;#160;&amp;#160; return ldl_phys(addr);      &lt;br /&gt;2626 }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;2563 static void check_watchpoint(int offset, int len_mask, int flags)     &lt;br /&gt;2564 {      &lt;br /&gt;2565&amp;#160;&amp;#160;&amp;#160;&amp;#160; CPUState *env = cpu_single_env;      &lt;br /&gt;2566&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_ulong pc, cs_base;      &lt;br /&gt;2567&amp;#160;&amp;#160;&amp;#160;&amp;#160; TranslationBlock *tb;      &lt;br /&gt;2568&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_ulong vaddr;      &lt;br /&gt;2569&amp;#160;&amp;#160;&amp;#160;&amp;#160; CPUWatchpoint *wp;      &lt;br /&gt;2570&amp;#160;&amp;#160;&amp;#160;&amp;#160; int cpu_flags;      &lt;br /&gt;2571       &lt;br /&gt;2572&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (env-&amp;gt;watchpoint_hit) {      &lt;br /&gt;2573&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* We re-entered the check after replacing the TB. Now raise      &lt;br /&gt;2574&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * the debug interrupt so that is will trigger after the      &lt;br /&gt;2575&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; * current instruction. */      &lt;br /&gt;2576&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_interrupt(env, CPU_INTERRUPT_DEBUG);      &lt;br /&gt;2577&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return;      &lt;br /&gt;2578&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;2579&amp;#160;&amp;#160;&amp;#160;&amp;#160; vaddr = (env-&amp;gt;mem_io_vaddr &amp;amp; TARGET_PAGE_MASK) + offset;      &lt;br /&gt;2580&amp;#160;&amp;#160;&amp;#160;&amp;#160; TAILQ_FOREACH(wp, &amp;amp;env-&amp;gt;watchpoints, entry) {      &lt;br /&gt;2581&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ((vaddr == (wp-&amp;gt;vaddr &amp;amp; len_mask) ||      &lt;br /&gt;2582&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (vaddr &amp;amp; wp-&amp;gt;len_mask) == wp-&amp;gt;vaddr) &amp;amp;&amp;amp; (wp-&amp;gt;flags &amp;amp; flags)) {      &lt;br /&gt;2583&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wp-&amp;gt;flags |= BP_WATCHPOINT_HIT;      &lt;br /&gt;2584&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (!env-&amp;gt;watchpoint_hit) {      &lt;br /&gt;2585&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; env-&amp;gt;watchpoint_hit = wp;      &lt;br /&gt;2586&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tb = tb_find_pc(env-&amp;gt;mem_io_pc);      &lt;br /&gt;2587&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (!tb) {      &lt;br /&gt;2588&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_abort(env, &amp;quot;check_watchpoint: could not find TB for &amp;quot;      &lt;br /&gt;2589&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;quot;pc=%p&amp;quot;, (void *)env-&amp;gt;mem_io_pc);      &lt;br /&gt;2590&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;2591&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_restore_state(tb, env, env-&amp;gt;mem_io_pc, NULL);      &lt;br /&gt;2592&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tb_phys_invalidate(tb, -1);      &lt;br /&gt;2593&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (wp-&amp;gt;flags &amp;amp; BP_STOP_BEFORE_ACCESS) {      &lt;br /&gt;2594&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; env-&amp;gt;exception_index = EXCP_DEBUG;      &lt;br /&gt;2595&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {      &lt;br /&gt;2596&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_get_tb_cpu_state(env, &amp;amp;pc, &amp;amp;cs_base, &amp;amp;cpu_flags);      &lt;br /&gt;2597&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tb_gen_code(env, pc, cs_base, cpu_flags, 1);      &lt;br /&gt;2598&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;2599&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_resume_from_signal(env, NULL);      &lt;br /&gt;2600&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;2601&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {      &lt;br /&gt;2602&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; wp-&amp;gt;flags &amp;amp;= ~BP_WATCHPOINT_HIT;      &lt;br /&gt;2603&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;2604&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;2605 }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p align=&quot;justify&quot;&gt;When &lt;em&gt;check_watchpoint&lt;/em&gt; is executed in the first time, &lt;em&gt;env-&amp;gt;watchpoint_hit&lt;/em&gt; is null. Then it will check whether the address is a watched address. If so, set the flag BP_WATCHPOINT_HIT in &lt;em&gt;wp-&amp;gt;flags&lt;/em&gt;(line 2583) and set &lt;em&gt;env-&amp;gt;watchpoint_hit&lt;/em&gt; to &lt;em&gt;wp&lt;/em&gt;. Then it will find and invalidate the current translation block(line 2586-2592). If the flag BP_STOP_BEFORE_ACCESS in &lt;em&gt;wp&lt;/em&gt; is not set, then qemu will translate the code from current pc(line 2596-2597) and resume the guest instruction emulation(line 2599). Function &lt;em&gt;cpu_resume_from_signal&lt;/em&gt; will jump to line 256 in cpu-exec.c and rerun the emulation process from the lw instruction(pc=0x80103c90). &lt;/p&gt;
&lt;blockquote&gt;&lt;p align=&quot;justify&quot;&gt;cpu-exec.c&lt;/p&gt;
&lt;p&gt;255&amp;#160;&amp;#160;&amp;#160;&amp;#160; for(;;) {     &lt;br /&gt;256&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (setjmp(env-&amp;gt;jmp_env) == 0) {      &lt;br /&gt;257&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; env-&amp;gt;current_tb = NULL;      &lt;br /&gt;258&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* if an exception is pending, we execute it here */      &lt;br /&gt;259&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (env-&amp;gt;exception_index &amp;gt;= 0) {      &lt;br /&gt;260&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (env-&amp;gt;exception_index &amp;gt;= EXCP_INTERRUPT) {      &lt;br /&gt;261&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* exit request from the cpu execution loop */      &lt;br /&gt;262&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ret = env-&amp;gt;exception_index;      &lt;br /&gt;263&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (ret == EXCP_DEBUG)      &lt;br /&gt;264&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_handle_debug_exception(env);      &lt;br /&gt;265&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;      &lt;br /&gt;266&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p align=&quot;justify&quot;&gt;Why do qemu need to invalidate current translation block and regenerate the code? Because this memory access(pc=0x80103c90) is in the middle of a translation block. If we want to rerun this instruction, we need to regenerate the code from this instruction(pc=0x80103c90). Moreover before invalidating the translation block, qemu needs to sync the cpu state to guest cpu(&lt;em&gt;cpu_restore_state&lt;/em&gt;). That’s because the cpu state in the middle of translation block is different from the actual cpu state. Understanding this process needs some knowledge of binary translation. If you find it is hard to understand, just ignore it.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;Now qemu rerun the guest os from pc=0x80103c90. Because the memory address is a watched memory address, qemu will call &lt;em&gt;watch_mem_readl-&amp;gt;check_watchpoint&lt;/em&gt; again. But this time, &lt;em&gt;env-&amp;gt;watchpoint_hit&lt;/em&gt; is not null(qemu set it in last call), then it will call &lt;em&gt;cpu_interrupt&lt;/em&gt; and return from function &lt;em&gt;check_watchpoint&lt;/em&gt;. Then in &lt;em&gt;watch_mem_readl&lt;/em&gt; it will call &lt;em&gt;ldl_phys&lt;/em&gt; to fetch the value from guest RAM. Function &lt;em&gt;cpu_interrupt&lt;/em&gt; in &lt;em&gt;check_watchpoint&lt;/em&gt;&amp;#160; sets the &lt;em&gt;CPU_INTERRUPT_DEBUG&lt;/em&gt; to flag to &lt;em&gt;env-&amp;gt;interrupt_request&lt;/em&gt;.&amp;#160; &lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;Then qemu runs normally just like nothing has happened. Because the &lt;em&gt;CPU_INTERRUPT_DEBUG&lt;/em&gt; has been set in &lt;em&gt;env-&amp;gt;interrupt_request&lt;/em&gt;, the main loop of cpu emulation will return.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;cpu-exec.c&lt;/p&gt;
&lt;p&gt;355&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (interrupt_request &amp;amp; CPU_INTERRUPT_DEBUG) {     &lt;br /&gt;356&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; env-&amp;gt;interrupt_request &amp;amp;= ~CPU_INTERRUPT_DEBUG;      &lt;br /&gt;357&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; env-&amp;gt;exception_index = EXCP_DEBUG;      &lt;br /&gt;358&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_loop_exit();      &lt;br /&gt;359&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;
&lt;p&gt; 54 void cpu_loop_exit(void)     &lt;br /&gt; 55 {      &lt;br /&gt; 56&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* NOTE: the register at this point must be saved by hand because      &lt;br /&gt; 57&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; longjmp restore them */      &lt;br /&gt; 58&amp;#160;&amp;#160;&amp;#160;&amp;#160; regs_to_env();      &lt;br /&gt; 59&amp;#160;&amp;#160;&amp;#160;&amp;#160; longjmp(env-&amp;gt;jmp_env, 1);      &lt;br /&gt; 60 }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p align=&quot;justify&quot;&gt;Function &lt;em&gt;cpu_loop_exit&lt;/em&gt; will do longjmp to line 256 in cpu-exec.c. Because &lt;em&gt;env-&amp;gt;exception_index&lt;/em&gt; is &lt;em&gt;EXCP_DEBUG&lt;/em&gt;, it will break from the loop of function &lt;em&gt;cpu_exec&lt;/em&gt;. Function &lt;em&gt;cpu_exec&lt;/em&gt; returns to &lt;em&gt;main_loop&lt;/em&gt; in vl.c.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;vl.c&lt;/p&gt;
&lt;p&gt;3800&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ret = cpu_exec(env);&lt;/p&gt;
&lt;p&gt;3850&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (unlikely(ret == EXCP_DEBUG)) {     &lt;br /&gt;3851&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gdb_set_stop_cpu(cur_cpu);      &lt;br /&gt;3852&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; vm_stop(EXCP_DEBUG);      &lt;br /&gt;3853&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p align=&quot;justify&quot;&gt;It will call &lt;em&gt;gdb_set_stop_cpu&lt;/em&gt; and then &lt;em&gt;vm_stop&lt;/em&gt; to stop the qemu. It the virtual state is changed, qemu will the call the callback functions registered by &lt;em&gt;qemu_add_vm_change_state_handler&lt;/em&gt;. So the function &lt;em&gt;spy_vm_state_change&lt;/em&gt; will be called.&lt;/p&gt;
&lt;p align=&quot;justify&quot;&gt;In sum, when accessing the watched memory address, the memory watch functions will be called. It will call function&lt;em&gt; check_watchpoint&lt;/em&gt;. Function &lt;em&gt;check_watchpoint&lt;/em&gt; will set &lt;em&gt;env-&amp;gt;watchpoint_hit&lt;/em&gt; to current watchpoint and rerun the guest os/applicaton from current pc. Then memory watched functions will be called again. It will call &lt;em&gt;function check_watchpoint&lt;/em&gt;. This time, function &lt;em&gt;check_watchpoint&lt;/em&gt; just set the flag in &lt;em&gt;env-&amp;gt;interrupt_request&lt;/em&gt; and tells cpu to interrupt the emulation process. And then qemu will return to the &lt;em&gt;main_loop&lt;/em&gt; and stop the vm. At last it will call the registered vm change state callback functions. &lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560595/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560595/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560595/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/feed/</wfw:commentRss><slash:comments>1</slash:comments><description>In qemu there is an amazing feature – memory watchpoint. It can watch all the memory access including memory read, write or both of them. When guest os/application touches the memory region watched by qemu, a registered function will be called and you can do everything as you want in this function. The gdb stub [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560595/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560595/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560595/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>watchpoint</category><category>qemu</category><category>emulation</category><pubDate>Wed, 15 Jul 2009 17:31:00 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/07/15/qemu-internal-part-3-memory-watchpoint/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560595/5290193</fs:itemid></item><item><title>qemu internal part 2: softmmu</title><link>http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/</link><content:encoded>&lt;p&gt;Qemu uses softmmu to accelerate the process of finding the mapping between guest physical address and host virtual address and the mapping between guest I/O region and qemu I/O emulation functions. In this article, I assume the guest page table size is 4K.&lt;/p&gt;
&lt;p&gt;1. the two level guest physical page descriptor table&lt;/p&gt;
&lt;p&gt;Qemu uses a two level guest physical page descriptor table to maintain the guest memory space and MMIO space. The table is pointed by &lt;em&gt;l1_phys_map&lt;/em&gt;. Bits [31:22] is used to index first level entry and bits [21:12] is used to index the second level entry. The entry of the second level table is &lt;em&gt;PhysPageDesc&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;exec.c&lt;/p&gt;
&lt;p&gt;146 typedef struct PhysPageDesc {      &lt;br /&gt;147&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* offset in host memory of the page + io_index in the low bits */       &lt;br /&gt;148&amp;#160;&amp;#160;&amp;#160;&amp;#160; ram_addr_t phys_offset;       &lt;br /&gt;149&amp;#160;&amp;#160;&amp;#160;&amp;#160; ram_addr_t region_offset;       &lt;br /&gt;150 } PhysPageDesc;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;If the memory region is RAM, then the bits [31:12] of &lt;em&gt;phys_offset&lt;/em&gt; means the offset of this page in emulated physical memory. If the memory region is memory mapped I/O, then the bits of [11:3] of &lt;em&gt;phys_offset&lt;/em&gt; means the index in &lt;em&gt;io_mem_write/io_mem_read&lt;/em&gt; array. When accessing this memory region, the functions in &lt;em&gt;io_mem_write/io_mem_read&lt;/em&gt; of index phys_offset will be called.&lt;/p&gt;
&lt;p&gt;2. register the guest physical memory&lt;/p&gt;
&lt;p&gt;Function &lt;em&gt;cpu_register_physical_memory&lt;/em&gt; is used to register a guest memory region. If &lt;em&gt;phys_offset&lt;/em&gt; is IO_MEM_RAM then it means this region is guest RAM space. If the &lt;em&gt;phys_offset&lt;/em&gt; &amp;gt;IO_MEM_ROM, then it means this memory region is MMIO space.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;898 static inline void cpu_register_physical_memory(target_phys_addr_t start_addr,      &lt;br /&gt;899&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ram_addr_t size,       &lt;br /&gt;900&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ram_addr_t phys_offset)       &lt;br /&gt;901 {       &lt;br /&gt;902&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_register_physical_memory_offset(start_addr, size, phys_offset, 0);       &lt;br /&gt;903 }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Function &lt;em&gt;cpu_register_physical_memory_offset&lt;/em&gt; will first find the PhysPageDesc in table &lt;em&gt;l1_phys_map&lt;/em&gt; using the given guest physical address&lt;em&gt;.&lt;/em&gt; If finding the entry, qemu will update the entry. If not finding the entry, then qemu creates a new entry and updates its value and insert this entry to the table at last.&lt;/p&gt;
&lt;p&gt;In malta emulation, the following is the code to register malta RAM space.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;hw/mips_malta.c&lt;/p&gt;
&lt;p&gt;811&amp;#160;&amp;#160;&amp;#160;&amp;#160; cpu_register_physical_memory(0, ram_size, IO_MEM_RAM);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;3. register the mmio space&lt;/p&gt;
&lt;p&gt;Before registering mmio space using &lt;em&gt;cpu_register_physical_memory,&lt;/em&gt; qemu uses the function &lt;em&gt;cpu_register_io_memory&lt;/em&gt; to register the I/O emulation functions to array &lt;em&gt;io_mem_write/io_mem_read.&lt;/em&gt; &lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;exec.c&lt;/p&gt;
&lt;p&gt;2851 int cpu_register_io_memory(int io_index,      &lt;br /&gt;2852&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CPUReadMemoryFunc **mem_read,       &lt;br /&gt;2853&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; CPUWriteMemoryFunc **mem_write,       &lt;br /&gt;2854&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; void *opaque)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This function will return the index in array io_mem_write/io_mem_read and this index will be passed to function &lt;em&gt;cpu_register_physical_memory&lt;/em&gt; via parameter &lt;em&gt;phys_offset&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;hw/mips_malta.c&lt;/p&gt;
&lt;p&gt;malta = cpu_register_io_memory(0, malta_fpga_read,      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; malta_fpga_write, s); &lt;/p&gt;
&lt;p&gt;cpu_register_physical_memory(base, 0x900, malta);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;4. softmmu&lt;/p&gt;
&lt;p&gt;Given the guest virtual address, how does qemu find the corresponding host virtual address? First qemu needs to translate the guest virtual address to guest physical address. Then qemu needs to find the PhysPageDesc entry in table &lt;em&gt;l1_phys_map&lt;/em&gt; and get the &lt;em&gt;phys_offset&lt;/em&gt;. At last qemu should add &lt;em&gt;phys_offset&lt;/em&gt; to &lt;em&gt;phys_ram_base&lt;/em&gt; to get the host virtual address.&lt;/p&gt;
&lt;p&gt;Qemu uses a softmmu model to speed up this process. Its main idea is storing the offset of guest virtual address to host virtual address in a TLB table. When translating the guest virtual address to host virtual address, it will search this TLB table firstly. If there is an entry in the table, then qemu can add this offset to guest virtual address to get the host virtual address directly. Otherwise, it needs to search the l1_phys_map table and then fill the corresponding entry to the TLB table. The index of this TLB table is bits [19:12] of guest virtual address and there is no asid field in tlb entry. This means the TLB table needs to be flushed in process switch!&lt;/p&gt;
&lt;p&gt;This TLB table idea is just like the most traditional hardware TLB. However, to MIPS cpu, there is another mmu model in qemu. Unlike x86 cpu, MIPS does &lt;strong&gt;NOT&lt;/strong&gt; care about hardware page table. Instead it uses hardware TLB which is &lt;strong&gt;NOT&lt;/strong&gt; transparent to software. Maybe It is another topic I will explain in another article. What we need to understand here is that the softmmu model in this article is not the mmu model of MIPS cpu itself.&lt;/p&gt;
&lt;p&gt;Moreover, besides helping speed up the process of translating guest virtual address to host virtual address, this softmmu model can speed up the process of dispatching I/O emulation functions according to guest virtual address too. In this case, the idex of I/O emulation functions in &lt;em&gt;io_mem_write/io_mem_read&lt;/em&gt; is stored in iotlb.&lt;/p&gt;
&lt;p&gt;The format of TLB entry is as flowing:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;cpu-defs.h&lt;/p&gt;
&lt;p&gt;176&amp;#160;&amp;#160;&amp;#160;&amp;#160; CPUTLBEntry tlb_table[NB_MMU_MODES][CPU_TLB_SIZE];&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; \      &lt;br /&gt;177&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_phys_addr_t iotlb[NB_MMU_MODES][CPU_TLB_SIZE];&amp;#160;&amp;#160; &lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;108 typedef struct CPUTLBEntry {      &lt;br /&gt;109&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* bit TARGET_LONG_BITS to TARGET_PAGE_BITS : virtual address       &lt;br /&gt;110&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bit TARGET_PAGE_BITS-1..4&amp;#160; : Nonzero for accesses that should not       &lt;br /&gt;111&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; go directly to ram.       &lt;br /&gt;112&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bit 3&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : indicates that the entry is invalid       &lt;br /&gt;113&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; bit 2..0&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : zero       &lt;br /&gt;114&amp;#160;&amp;#160;&amp;#160;&amp;#160; */       &lt;br /&gt;115&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_ulong addr_read;       &lt;br /&gt;116&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_ulong addr_write;       &lt;br /&gt;117&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_ulong addr_code;       &lt;br /&gt;124&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_phys_addr_t addend;       &lt;br /&gt;131 } CPUTLBEntry;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Field &lt;em&gt;addr_read/write/code&lt;/em&gt; stores the guest virtual address for TLB entry. It is the tag of this entry. Filed a&lt;em&gt;ddend&lt;/em&gt; is the offset of host virtual address to guest virtual address. We can add this value to guest virtual address to get the host virtual address.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;addend = host_virtual_address – guest_virtual_address&lt;/p&gt;
&lt;p&gt;host_virtual_address = &lt;em&gt;&lt;strong&gt;phys_ram_base(qemu variable)&lt;/strong&gt;&lt;/em&gt; + guest_physical_address – guest_physical_address_base(0 in MIPS) &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The &lt;em&gt;iotlb&lt;/em&gt; stores the index of I/O emulation function in &lt;em&gt;io_mem_write/io_mem_read.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Function __ldb_mmu/__ldl_mmu/__ldw_mmu is used to translating the guest virtual address to host virtual address or dispatching guest virtual address to I/O emulation functions.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;softmmu_template.h&lt;/p&gt;
&lt;p&gt; 86 DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,     &lt;br /&gt; 87&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int mmu_idx)      &lt;br /&gt; 88 {      &lt;br /&gt; 89&amp;#160;&amp;#160;&amp;#160;&amp;#160; DATA_TYPE res;      &lt;br /&gt; 90&amp;#160;&amp;#160;&amp;#160;&amp;#160; int index;      &lt;br /&gt; 91&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_ulong tlb_addr;      &lt;br /&gt; 92&amp;#160;&amp;#160;&amp;#160;&amp;#160; target_phys_addr_t addend;      &lt;br /&gt; 93&amp;#160;&amp;#160;&amp;#160;&amp;#160; void *retaddr;      &lt;br /&gt; 94       &lt;br /&gt; 95&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* test if there is match for unaligned or IO access */      &lt;br /&gt; 96&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* XXX: could done more in memory macro in a non portable way */      &lt;br /&gt; 97&amp;#160;&amp;#160;&amp;#160;&amp;#160; index = (addr &amp;gt;&amp;gt; TARGET_PAGE_BITS) &amp;amp; (CPU_TLB_SIZE - 1);      &lt;br /&gt; 98&amp;#160; redo:      &lt;br /&gt; 99&amp;#160;&amp;#160;&amp;#160;&amp;#160; tlb_addr = env-&amp;gt;tlb_table[mmu_idx][index].ADDR_READ;      &lt;br /&gt;100&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ((addr &amp;amp; TARGET_PAGE_MASK) == (tlb_addr &amp;amp; (TARGET_PAGE_MASK | TLB_INVALID_MASK))) {      &lt;br /&gt;101&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if (tlb_addr &amp;amp; ~TARGET_PAGE_MASK) {      &lt;br /&gt;102&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* IO access */      &lt;br /&gt;103&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ((addr &amp;amp; (DATA_SIZE - 1)) != 0)      &lt;br /&gt;104&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; goto do_unaligned_access;      &lt;br /&gt;105&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; retaddr = GETPC();      &lt;br /&gt;106&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; addend = env-&amp;gt;iotlb[mmu_idx][index];      &lt;br /&gt;107&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; res = glue(io_read, SUFFIX)(addend, addr, retaddr);      &lt;br /&gt;108&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else if (((addr &amp;amp; ~TARGET_PAGE_MASK) + DATA_SIZE - 1) &amp;gt;= TARGET_PAGE_SIZE) {      &lt;br /&gt;109&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* slow unaligned access (it spans two pages or IO) */      &lt;br /&gt;110&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; do_unaligned_access:      &lt;br /&gt;111&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; retaddr = GETPC();      &lt;br /&gt;112 #ifdef ALIGNED_ONLY      &lt;br /&gt;113&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);      &lt;br /&gt;114 #endif      &lt;br /&gt;115&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; res = glue(glue(slow_ld, SUFFIX), MMUSUFFIX)(addr,      &lt;br /&gt;116&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; mmu_idx, retaddr);      &lt;br /&gt;117&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {      &lt;br /&gt;118&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* unaligned/aligned access in the same page */      &lt;br /&gt;119 #ifdef ALIGNED_ONLY      &lt;br /&gt;120&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ((addr &amp;amp; (DATA_SIZE - 1)) != 0) {      &lt;br /&gt;121&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; retaddr = GETPC();      &lt;br /&gt;122&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);      &lt;br /&gt;123&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;124 #endif      &lt;br /&gt;125&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; addend = env-&amp;gt;tlb_table[mmu_idx][index].addend;      &lt;br /&gt;126&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; res = glue(glue(ld, USUFFIX), _raw)((uint8_t *)(long)(addr+addend));      &lt;br /&gt;127&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;128&amp;#160;&amp;#160;&amp;#160;&amp;#160; } else {      &lt;br /&gt;129&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; /* the page is not in the TLB : fill it */      &lt;br /&gt;130&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; retaddr = GETPC();      &lt;br /&gt;131 #ifdef ALIGNED_ONLY      &lt;br /&gt;132&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if ((addr &amp;amp; (DATA_SIZE - 1)) != 0)      &lt;br /&gt;133&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; do_unaligned_access(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);      &lt;br /&gt;134 #endif      &lt;br /&gt;135&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tlb_fill(addr, READ_ACCESS_TYPE, mmu_idx, retaddr);      &lt;br /&gt;136&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; goto redo;      &lt;br /&gt;137&amp;#160;&amp;#160;&amp;#160;&amp;#160; }      &lt;br /&gt;138&amp;#160;&amp;#160;&amp;#160;&amp;#160; return res;      &lt;br /&gt;139 }      &lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In this function, it will get the index of TLB table and compare the guest virtual address with the address stored in this tlb entry(line 97-100). If these two addresses match, it means this guest virtual address hits the tlb entry. Then qemu will determine this virtual address is a MMIO address or RAM address. If it is a MMIO address, get the index of IO emulation functions from &lt;em&gt;env-&amp;gt;iotlb &lt;/em&gt;and call these functions(line 103-117). If it is a RAM space, add the guest virtual address to addend to get the host virtual address(line 118-128). If there is no matched tlb entry, then fietch the entry from table &lt;em&gt;l1_phys_map &lt;/em&gt;and insert the entry to tlb table(line 135).&lt;/p&gt;
&lt;p&gt;5. an example&lt;/p&gt;
&lt;p&gt;When fetching code from guest memory, the whole code path is as flowing:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;cpu_exec-&amp;gt;tb_find_fast-&amp;gt;tb_find_slow-&amp;gt;get_phys_addr_code-&amp;gt;(if tlb not match)ldub_code(softmmu_header.h)-&amp;gt;__ldl_mmu(softmmu_template.h)-&amp;gt;tlb_fill-&amp;gt;cpu_mips_handle_mmu_fault-&amp;gt;tlb_set_page-&amp;gt;tlb_set_page_exec&lt;/p&gt;
&lt;/blockquote&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560596/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560596/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560596/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/feed/</wfw:commentRss><slash:comments>7</slash:comments><description>Qemu uses softmmu to accelerate the process of finding the mapping between guest physical address and host virtual address and the mapping between guest I/O region and qemu I/O emulation functions. In this article, I assume the guest page table size is 4K.
1. the two level guest physical page descriptor table
Qemu uses a two level [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560596/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560596/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560596/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>qemu</category><category>softmmu</category><category>emulation</category><pubDate>Fri, 10 Jul 2009 11:14:00 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/07/10/qemu-internal-part-2-softmmu/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560596/5290193</fs:itemid></item><item><title>qemu internal part 1: the code path of memory load emulation</title><link>http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/</link><content:encoded>&lt;p&gt;In qemu, there are two different meanings of target. The first meaning of ‘target’ means the emulated target machine architecture. For example, when emulating mips machine on x86, the target is mips and host is x86. However, in tcg(tiny code generator), target has a different meaning. It means the generated binary architecture. In the example of emulating mips on x86, in tcg the target means x86 because tcg will generate x86 binary.&lt;/p&gt;
&lt;p&gt;This article is based on qemu version 0.10.5 and target machine emulated is little endian mips. I will summarize the code path of mips lw instruction emulation in qemu.&lt;/p&gt;
&lt;p&gt;Function &lt;i&gt;decode_opc&lt;/i&gt; is used for decoding all the fetched instructions before tcg generating the target binary.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;target-mips/translate.c&lt;/p&gt;
&lt;p&gt;7566 static void decode_opc (CPUState *env, DisasContext *ctx)&lt;/p&gt;
&lt;p&gt;7960&amp;#160;&amp;#160;&amp;#160;&amp;#160; case OPC_LB ... OPC_LWR: /* Load and stores */      &lt;br /&gt;7961&amp;#160;&amp;#160;&amp;#160;&amp;#160; case OPC_SB ... OPC_SW:       &lt;br /&gt;7962&amp;#160;&amp;#160;&amp;#160;&amp;#160; case OPC_SWR:       &lt;br /&gt;7963&amp;#160;&amp;#160;&amp;#160;&amp;#160; case OPC_LL:       &lt;br /&gt;7964&amp;#160;&amp;#160;&amp;#160;&amp;#160; case OPC_SC:       &lt;br /&gt;7965&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gen_ldst(ctx, op, rt, rs, imm);       &lt;br /&gt;7966&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;It will call function &lt;em&gt;gen_ldst&lt;/em&gt; which is also in &lt;em&gt;target-mips/translate.c&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote style=&quot;margin-right: 0px&quot; dir=&quot;ltr&quot;&gt;&lt;p&gt;target-mips/translate.c&lt;/p&gt;
&lt;p&gt;973 static void gen_ldst (DisasContext *ctx, uint32_t opc, int rt,      &lt;br /&gt;974&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int base, int16_t offset)&lt;/p&gt;
&lt;p&gt;1046&amp;#160;&amp;#160;&amp;#160;&amp;#160; case OPC_LW:      &lt;br /&gt;1047&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; op_ldst_lw(t0, ctx);       &lt;br /&gt;1048&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; gen_store_gpr(t0, rt);       &lt;br /&gt;1049&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; opn = &amp;quot;lw&amp;quot;;       &lt;br /&gt;1050&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Function &lt;em&gt;op_ldst_lw&lt;/em&gt; will generate the target binary which fetches the value from the emulated guest memory and &lt;em&gt;gen_store_gpr&lt;/em&gt; will store this value to the emulated cpu’s general register rt.&lt;/p&gt;
&lt;p&gt;Function &lt;em&gt;op_ldst_lw &lt;/em&gt;is generated by the macro &lt;em&gt;OP_LD&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;target-mips/translate.c&lt;/p&gt;
&lt;p&gt;901 #define OP_LD(insn,fname)&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; \      &lt;br /&gt;902 static inline void op_ldst_##insn(TCGv t0, DisasContext *ctx)&amp;#160;&amp;#160;&amp;#160; \       &lt;br /&gt;903 {&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; \       &lt;br /&gt;904&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_gen_qemu_##fname(t0, t0, ctx-&amp;gt;mem_idx);&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; \       &lt;br /&gt;905 }&lt;/p&gt;
&lt;p&gt;910 OP_LD(lw,ld32s);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We can find that &lt;em&gt;op_ldst_lw &lt;/em&gt;is a function which calls function &lt;em&gt;tcg_gen_qemu_ld32s&lt;/em&gt;. It will output the OPC(&lt;em&gt;INDEX_op_qemu_ld32u&lt;/em&gt;) and args to &lt;em&gt;gen_opc_ptr&lt;/em&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;tcg/tcg-op.h&lt;/p&gt;
&lt;p&gt;1793 static inline void tcg_gen_qemu_ld32s(TCGv ret, TCGv addr, int mem_index)      &lt;br /&gt;1794 {       &lt;br /&gt;1795 #if TARGET_LONG_BITS == 32       &lt;br /&gt;1796&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_gen_op3i_i32(INDEX_op_qemu_ld32u, ret, addr, mem_index);       &lt;br /&gt;1797 #else       &lt;br /&gt;1798&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_gen_op4i_i32(INDEX_op_qemu_ld32u, TCGV_LOW(ret), TCGV_LOW(addr),       &lt;br /&gt;1799&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TCGV_HIGH(addr), mem_index);       &lt;br /&gt;1800&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_gen_sari_i32(TCGV_HIGH(ret), TCGV_LOW(ret), 31);       &lt;br /&gt;1801 #endif       &lt;br /&gt;1802 }&lt;/p&gt;
&lt;p&gt;99 static inline void tcg_gen_op3i_i32(int opc, TCGv_i32 arg1, TCGv_i32 arg2,      &lt;br /&gt;100&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; TCGArg arg3)       &lt;br /&gt;101 {       &lt;br /&gt;102&amp;#160;&amp;#160;&amp;#160;&amp;#160; *gen_opc_ptr++ = opc;       &lt;br /&gt;103&amp;#160;&amp;#160;&amp;#160;&amp;#160; *gen_opparam_ptr++ = GET_TCGV_I32(arg1);       &lt;br /&gt;104&amp;#160;&amp;#160;&amp;#160;&amp;#160; *gen_opparam_ptr++ = GET_TCGV_I32(arg2);       &lt;br /&gt;105&amp;#160;&amp;#160;&amp;#160;&amp;#160; *gen_opparam_ptr++ = arg3;       &lt;br /&gt;106 }&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The path of generation of target binary code of tcg is as following.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;cpu_gen_code-&amp;gt;tcg_gen_code-&amp;gt;tcg_gen_code_common-&amp;gt;tcg_reg_alloc_op-&amp;gt;tcg_out_op&lt;/p&gt;
&lt;/blockquote&gt;
&lt;blockquote&gt;&lt;p&gt;tcg/i386/tcg-target.c&lt;/p&gt;
&lt;p&gt;856 static inline void tcg_out_op(TCGContext *s, int opc,      &lt;br /&gt;857&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; const TCGArg *args, const int *const_args)&lt;/p&gt;
&lt;p&gt;1041&amp;#160;&amp;#160;&amp;#160;&amp;#160; case INDEX_op_qemu_ld32u:     &lt;br /&gt;1042&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_out_qemu_ld(s, args, 2);      &lt;br /&gt;1043&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; break;&lt;/p&gt;
&lt;p&gt;431 static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args,     &lt;br /&gt;432&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int opc)&lt;/p&gt;
&lt;p&gt;508 #if TARGET_LONG_BITS == 32     &lt;br /&gt;509&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_EDX, mem_index);      &lt;br /&gt;510 #else      &lt;br /&gt;511&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_out_mov(s, TCG_REG_EDX, addr_reg2);      &lt;br /&gt;512&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_out_movi(s, TCG_TYPE_I32, TCG_REG_ECX, mem_index);      &lt;br /&gt;513 #endif      &lt;br /&gt;514&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_out8(s, 0xe8);      &lt;br /&gt;515&amp;#160;&amp;#160;&amp;#160;&amp;#160; tcg_out32(s, (tcg_target_long)qemu_ld_helpers[s_bits] -       &lt;br /&gt;516&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; (tcg_target_long)s-&amp;gt;code_ptr - 4);&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In line 514, tcg outputs 0xe8 which means a call instruction in x86. It will call the functions in array &lt;em&gt;qemu_ld_helpers.&lt;/em&gt; The args to the functions is passed by registers EAX,EDX and ECX.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;tcg/i386/tcg-target.c&lt;/p&gt;
&lt;p&gt;413 static void *qemu_ld_helpers[4] = {     &lt;br /&gt;414&amp;#160;&amp;#160;&amp;#160;&amp;#160; __ldb_mmu,      &lt;br /&gt;415&amp;#160;&amp;#160;&amp;#160;&amp;#160; __ldw_mmu,      &lt;br /&gt;416&amp;#160;&amp;#160;&amp;#160;&amp;#160; __ldl_mmu,      &lt;br /&gt;417&amp;#160;&amp;#160;&amp;#160;&amp;#160; __ldq_mmu,      &lt;br /&gt;418 };&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;These functions &lt;em&gt;__ldb_mmu/__ldw_mmu&lt;/em&gt; are defined in softmmu_template.h.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;softmmu_tempate.h&lt;/p&gt;
&lt;p&gt;DATA_TYPE REGPARM glue(glue(__ld, SUFFIX), MMUSUFFIX)(target_ulong addr,     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; int mmu_idx)&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;In sum, function &lt;em&gt;gen_ldst&lt;/em&gt; outputs the OPC(INDEX_op_qemu_ld32u) to &lt;em&gt;gen_opc_ptr&lt;/em&gt; and &lt;em&gt;tcg_out_op&lt;/em&gt; will generates the target binary according to the OPC. In the lw instruction emulation, it will generate the x86 binary calls the functions in &lt;em&gt;softmmu_template.h&lt;/em&gt;.&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560597/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560597/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560597/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/feed/</wfw:commentRss><slash:comments>0</slash:comments><description>In qemu, there are two different meanings of target. The first meaning of ‘target’ means the emulated target machine architecture. For example, when emulating mips machine on x86, the target is mips and host is x86. However, in tcg(tiny code generator), target has a different meaning. It means the generated binary architecture. In the example [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560597/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560597/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560597/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>tcg</category><category>qemu</category><category>emulation</category><pubDate>Wed, 08 Jul 2009 14:58:00 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/07/08/qemu-internal-part-1-the-code-path-of-memory-load-emulation/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560597/5290193</fs:itemid></item><item><title>a little progress on qemu-loongson</title><link>http://vm-kernel.org/blog/2009/06/10/a-little-progress-on-qemu-loongson/</link><content:encoded>&lt;p&gt;Hi guys, it is about one month since posting last blog entry. These days I am really very busy preparing the GRE and Tofel test. Moreover I have to work to support my life. So I have to spend less time on qemu-loongson.&lt;/p&gt;
&lt;p&gt;Anyway, there are progress these days. &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Rewrite the GPIO I2C emulation for gdium. Now it is more clear than before.&lt;/li&gt;
&lt;li&gt;add st4180 rtc emulation to qemu&lt;/li&gt;
&lt;li&gt;add stds75 temperature sensor emulation to qemu&lt;/li&gt;
&lt;li&gt;change a little in uart emulation to satisfy pmon’s uart probing process&lt;/li&gt;
&lt;li&gt;fix a little bug in pflash_cfi02.c&lt;/li&gt;
&lt;li&gt;fix gdb stub bug in qemu to support mips64&lt;/li&gt;
&lt;p&gt; Follow is the uart output of qemu-loongson.&lt;/p&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;blockquote dir=&quot;ltr&quot; style=&quot;margin-right: 0px;&quot;&gt;
&lt;p&gt;kill-bill:/home/root/sdd/gdium/qemu-loongson/mips64el-softmmu# ./qemu-system-mips64el -M gdium -pflash gzrom.bin.gdb -nographic -S -s &lt;br /&gt;Register sst39vf040&amp;nbsp; size 80000&amp;nbsp; at offset 08800000 addr 1fc00000 'pflash0' 80        &lt;br /&gt;devfn 70         &lt;br /&gt;unassigned_mem_readl Unassigned mem read 000000001fbffffc        &lt;br /&gt;unassigned_mem_readl Unassigned mem read 000000001fbffffc        &lt;br /&gt;new_sm502_mm_io 7000000 pci_mem _base 10000000        &lt;br /&gt;PMON2000 MIPS Initializing. Standby...        &lt;br /&gt; PRID=00006302        &lt;br /&gt;enable register space of MEMORY        &lt;br /&gt;DDR2 config begin_whd        &lt;br /&gt;DIMM read        &lt;br /&gt;0000008000000008read DIMM number of rows        &lt;br /&gt;read number of cols        &lt;br /&gt;module data width        &lt;br /&gt;DIMM SIZE=20000000        &lt;br /&gt;cols rows:        &lt;br /&gt;04030940DDR2 config end        &lt;br /&gt;DDR2 DLL locked        &lt;br /&gt;00000004        &lt;br /&gt;disable register space of MEMORY        &lt;br /&gt;jlliu : rom speed reg : 0x00000f8c        &lt;br /&gt;Init SDRAM Done!        &lt;br /&gt;Sizing caches...        &lt;br /&gt;Init caches...        &lt;br /&gt;godson2 caches found        &lt;br /&gt;Init caches done, cfg = 00030932        &lt;br /&gt;Copy PMON to execute location...        &lt;br /&gt;copy text section done.        &lt;br /&gt;Copy PMON to execute location done.        &lt;br /&gt;sp=80ffc000...............new_sm502_mm_io 6000000 pci_mem _base 10000000        &lt;br /&gt;cmd 7        &lt;br /&gt;mmio 6000000        &lt;br /&gt;FREQ        &lt;br /&gt;FREI        &lt;br /&gt;DONE        &lt;br /&gt;DEVI        &lt;br /&gt;ENVI        &lt;br /&gt;MAPV        &lt;br /&gt;nvram=bfc00000        &lt;br /&gt;NVRAM is invalid!        &lt;br /&gt;NVRAM@bfc00000        &lt;br /&gt;STDV        &lt;br /&gt;80100000: heap is already above this point        &lt;br /&gt;SBDD        &lt;br /&gt;P12PCIH&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;/p&gt;&lt;img src=&quot;http://www1.feedsky.com/t1/430560598/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/06/10/a-little-progress-on-qemu-loongson/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560598/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560598/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/06/10/a-little-progress-on-qemu-loongson/feed/</wfw:commentRss><slash:comments>0</slash:comments><description>Hi guys, it is about one month since posting last blog entry. These days I am really very busy preparing the GRE and Tofel test. Moreover I have to work to support my life. So I have to spend less time on qemu-loongson.
Anyway, there are progress these days. 

Rewrite the GPIO I2C emulation for gdium. [...]&lt;img src=&quot;http://www1.feedsky.com/t1/430560598/vm-kernel/feedsky/s.gif?r=http://vm-kernel.org/blog/2009/06/10/a-little-progress-on-qemu-loongson/&quot; border=&quot;0&quot; height=&quot;0&quot; width=&quot;0&quot; style=&quot;position:absolute&quot; /&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560598/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560598/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>emulatuion</category><category>qemu</category><category>qemu-loongson</category><category>emulation</category><pubDate>Wed, 10 Jun 2009 11:15:00 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/06/10/a-little-progress-on-qemu-loongson/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/07/08/a-little-progress-on-qemu-loongson/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/06/10/a-little-progress-on-qemu-loongson/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560598/5290193</fs:itemid></item><item><title>what's the difference between these two definitions</title><link>http://vm-kernel.org/blog/2009/04/23/whats-the-difference-between-these-two-definitions/</link><content:encoded>&lt;p&gt;I write this article because some guys are talking about it in &lt;a href=&quot;http://www.linuxforum.net/forum/showflat.php?Cat=&amp;amp;Board=embedded&amp;amp;Number=717266&amp;amp;page=0&amp;amp;view=collapsed&amp;amp;sb=5&amp;amp;o=0&amp;amp;fpart=&quot;&gt;CLF&lt;/a&gt;. The question is: what is the difference between the two following definitions:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;A. const char temp[]=&amp;quot;abc&amp;quot;;&lt;/p&gt;
&lt;p&gt;B. const char *temp=&amp;quot;abc&amp;quot;;&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;You may have your own answer already. But wait a moment, let me write some test cases first and you can see whether your answer is right or not. &lt;img src='http://vm-kernel.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /&gt; &lt;/p&gt;
&lt;p&gt;(1) Test case 1&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;const char temp[]=&amp;quot;abc&amp;quot;;      &lt;br /&gt;int main()       &lt;br /&gt;{       &lt;br /&gt;temp[0]='c';       &lt;br /&gt;printf(&amp;quot;temp %s \n&amp;quot;,temp);       &lt;br /&gt;}       &lt;br /&gt;debian:~# gcc -o test test.c       &lt;br /&gt;test.c: In function `main':       &lt;br /&gt;test.c:8: error: assignment of read-only location `temp[0]'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;(2) Test case 2&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;const char temp[]=&amp;quot;abc&amp;quot;;      &lt;br /&gt;char temp1[]=&amp;quot;def&amp;quot;;       &lt;br /&gt;int main()       &lt;br /&gt;{       &lt;br /&gt;temp = temp1;       &lt;br /&gt;printf(&amp;quot;temp %s \n&amp;quot;,temp);       &lt;br /&gt;}       &lt;br /&gt;debian:~# gcc -o test test.c       &lt;br /&gt;test.c: In function `main':       &lt;br /&gt;test.c:8: error: assignment of read-only variable `temp'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;(3) Test case 3&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;const char* temp=&amp;quot;abc&amp;quot;;      &lt;br /&gt;char temp1[]=&amp;quot;def&amp;quot;;       &lt;br /&gt;int main()       &lt;br /&gt;{       &lt;br /&gt;temp = temp1;       &lt;br /&gt;printf(&amp;quot;temp %s \n&amp;quot;,temp);       &lt;br /&gt;}       &lt;br /&gt;debian:~# gcc -o test test.c       &lt;br /&gt;debian:~# ./test       &lt;br /&gt;temp def&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;(4) Test case 4&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;const char* temp=&amp;quot;abc&amp;quot;;      &lt;br /&gt;int main()       &lt;br /&gt;{       &lt;br /&gt;temp[0] = 'd';       &lt;br /&gt;printf(&amp;quot;temp %s \n&amp;quot;,temp);       &lt;br /&gt;}       &lt;br /&gt;debian:~# gcc -o test test.c       &lt;br /&gt;test.c: In function `main':       &lt;br /&gt;test.c:8: error: assignment of read-only location `*temp'&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;So the definition A means both temp and array is const and you can not change it. Definition B means temp points to a const string, which you can not change its content. But you can change temp itself.&lt;/p&gt;&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560599/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560599/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</content:encoded><wfw:commentRss>http://vm-kernel.org/blog/2009/04/23/whats-the-difference-between-these-two-definitions/feed/</wfw:commentRss><slash:comments>0</slash:comments><description>I write this article because some guys are talking about it in CLF. The question is: what is the difference between the two following definitions:
A. const char temp[]=&amp;#34;abc&amp;#34;;
B. const char *temp=&amp;#34;abc&amp;#34;;

You may have your own answer already. But wait a moment, let me write some test cases first and you can see whether your answer [...]&lt;p class=&quot;fswww1&quot;&gt;&lt;a href=&quot;http://www1.feedsky.com/r/l/feedsky/vm-kernel/430560599/art01.html&quot; target=&quot;_blank&quot;&gt;&lt;img border=&quot;0&quot; ismap=&quot;ismap&quot; src=&quot;http://www1.feedsky.com/r/i/feedsky/vm-kernel/430560599/art01.gif&quot; onerror=&quot;this.style.display='none'&quot; /&gt;&lt;/a&gt;&lt;/p&gt;</description><category>kernel</category><pubDate>Thu, 23 Apr 2009 09:39:03 +0800</pubDate><author>yajin</author><comments>http://vm-kernel.org/blog/2009/04/23/whats-the-difference-between-these-two-definitions/#comments</comments><guid isPermaLink="false">http://vm-kernel.org/blog/2009/04/23/whats-the-difference-between-these-two-definitions/</guid><dc:creator>yajin</dc:creator><fs:srclink>http://vm-kernel.org/blog/2009/04/23/whats-the-difference-between-these-two-definitions/</fs:srclink><fs:srcfeed>http://vm-kernel.org/blog/feed</fs:srcfeed><fs:itemid>feedsky/vm-kernel/~7222264/430560599/5290193</fs:itemid></item></channel></rss>
