About site: Emulators/Intel x86 Architecture - How Bochs works under the hood
Return to Computers also Computers
  About site: http://www.diku.dk/hjemmesider/studerende/firefly/bochsdoc.html

Title: Emulators/Intel x86 Architecture - How Bochs works under the hood A description of how Bochs is implemented internally (Work in progress).
Uplinkearth Offers Windows 2000 and NT hosting with support for ColdFusion and ASP. Located in the United States.

Zephrylus\'_Aneme_Page Focusing on the Anima style, tutorials utilizing Adobe Photoshop and Bryce graphic programs.

The_Database_Canvas Hosting with banners, calendars, mailing lists, MP3, site skins.

Tibco_acquires_Velosel_for_master_data_management_technology InfoWorld article by Ephraim Schwartz announcing Tibco's acquisition of Velosel. (October 24, 2005)

RFC_1308 Executive Introduction to Directory Services Using the X.500 Protocol. C. Weider, J. Reynolds. March 1992.

Yamada_Gaelic_Languages_Guide The Yamada WWW Language Guides are the definitive guide to language resources on the World Wide Web.


  Alexa statistic for http://www.diku.dk/hjemmesider/studerende/firefly/bochsdoc.html





Get your Google PageRank






Please visit: http://www.diku.dk/hjemmesider/studerende/firefly/bochsdoc.html


  Related sites for http://www.diku.dk/hjemmesider/studerende/firefly/bochsdoc.html
    Games_&_tools_at_TGD-Consulting Several games & tools based on REBOL/View including "Serve-It!", a full featured, fast HTTP & PROXY-Server.
    Free_Forums Provides free phpBB based hosting. Ad-supported.
    GH-Gold Links to information covering various coding styles and web graphics.
    Tools_For_Thought__Ex-Prodigies_and_Antiaircraft_Guns By Howard Rheingold. Online copy of well known 1985 book on the invention of modern computing; this chapter on Norbert Wiener, Cybernetics. Newer (c)2000 edition of the book is out, with follow-up int
    Nancy_McGough\'s_Page_on_Pine Information about configuring Pine. It includes a comprehensive list of links to more resources about Pine.
    RFC_1997_-_BGP_Communities_Attribute Standard defining an extension which allows destinations to be grouped, allowing group-based routing policies.
    Warsaw_Eagles Homepage of the 2003 ACM ICPC World Champions: Tomasz Czajka, Krzysztof Onak and Andrzej Gasienica-Samek.
    Micrprinters_com Features MICR printers from Kyocera Mita America, Troy Systems, and Rosetta Technologies that provide compliance with ANSI and ABA specifications.
    Netscape_Directory_SDK_for_Java__Source_Code_Release Enables you to write applications which access, manage, and update the information stored in an LDAP directory. C and Perl versions also available. [Open Source]
    Show_Your_Listings Offers design, development and hosting services.
    Ghost_Forest_Screen_Savers Children's colorful screen savers with beautiful hand painted pictures. Ghost Forest" is a story for children written by Zlatko Enev. [Macintosh/Windows]
    Easynet Accounting and estimating solutions for the sign industry.
    Http-tiny Very small C library to make http queries easily portable and embeddable. An equally tiny command line http client.
    Advanced_Maillist_Verify_(AMV) Verifies the validity of emails in databases, address books and mailing lists the email server addresses are extracted from the DNS. Supports the Socks5 proxy protocol, for use within a local area net
    RFC_1127 Perspective on the Host Requirements RFCs. R.T. Braden. October 1989.
    Background-Heaven Many seamless, original and free KPT-generated backgrounds to find here.
    Middlesex_University School of Computing Science.
    SGP_Systems Makes Baltie line of educational, graphic, object-oriented visual languages, for kids through adults; DOS and Windows versions. Czech Republic.
    Microsoft_Passport_to_Trouble Describes a security hole by which a hacker can gain access to a user's Passport shopping profile by stealing their Hotmail cookie.
    Titan_Project A game engine using OpenGL that is able to render Quake3 maps and supports most Quake3 features. [Modified BSD license]
This is websites2007.org cache of m/ as retrieved on 2008.08.30 websites2007.org's cache is the snapshot that we took of the page as we crawled the web. The page may have changed since that time.
How Bochs works under the hood

1 Overview

The Bochs virtual PC consists of many pieces of hardware. At a bare minimumthere are always a CPU, a PIT (Programmable Interval Timer), a PIC(Programmable Interrupt Controller), a DMA controller, some memory (thisincludes both RAM and BIOS ROMs), a video card (usually VGA), a keyboard port(also handles the mouse), an RTC with battery backed NVRAM, and some extramotherboard circuitry.There might also be a NE2K ethernet card, a PCI controller, a Sound Blaster16, an IDE controller (+ harddisks/CDROM), a SCSI controller (+ harddisks), afloppy controller, an APIC ..There may also be more than one CPU.Most of these pieces of hardware have their own C++ class - and if bochs isconfigured to have more than one piece of a type of hardware, each will haveits own object.The pieces of hardware communicates over a couple of buses with each other- some of the things that the buses carry are reads and writes in memoryspace, reads and writes in I/O space, interrupt requests, interruptacknowledges, DMA requests, DMA acknowledges, and NMI request/acknowledge. Howthat is simulated is explained later FIXME.Other important pieces of the puzzle are: the options object (reads/writesconfiguration files, can be written to and queried while bochs is running) andthe GUI object. There are many different but compatible implementations of theGUI object, depending on whether you compile for X (Unix/Linux), Win32,Macintosh (two versions: one for Mac OS X and one for older OS's), BeOS,Amiga, etc.And then there is the supporting cast: debugger, config menu, panichandler, disassembler, tracer, instrumentation.

2 Weird macros and other mysteries

Bochs has many macros with inscrutable names. One might even go as far asto say that bochs is macro infested.Some of them are gross speed hacks, to cover up the slow speed that C++causes. Others paper over differences between the simulated PCconfigurations.Many of the macros exhibit the same problem as C++ does: too much stuffhappens behind the programmer's back. More explicitness would be a bigwin.

2.1 static methods hack

C++ methods have an invisible parameter called the this pointer -otherwise the method wouldn't know which object to operate on. In many casesin Bochs, there will only ever be one object - so this flexibility isunnecessary. There is a hack that can be enabled by #defining BX_USE_CPU_SMFto 1 in config.h that makes most methods static, which means they have a"special relationship" with the class they are declared in but apartfrom that are normal C functions with no hidden parameters. Of course theystill need access to the internals of an object, so the single object of theirclass has a globally visible name that these functions use. It is all hiddenwith macros.Declaration of a class, from iodev/pic.h:...#if BX_USE_PIC_SMF# define BX_PIC_SMF static# define BX_PIC_THIS bx_pic.#else# define BX_PIC_SMF# define BX_PIC_THIS this->#endif...class bx_pic_c : public logfunctions {public: bx_pic_c(void); ~bx_pic_c(void); BX_PIC_SMF void init(bx_devices_c *); BX_PIC_SMF void lower_irq(unsigned irq_no); BX_PIC_SMF void raise_irq(unsigned irq_no);... };extern bx_pic_c bx_pic;And iodev/pic.cc:...bx_pic_c bx_pic;#if BX_USE_PIC_SMF#define this (&bx_pic)#endif... voidbx_pic_c::lower_irq(unsigned irq_no){ if ((irq_no <= 7) && (BX_PIC_THIS s.master_pic.IRQ_line[irq_no])) { BX_DEBUG(("IRQ line %d now low", (unsigned) irq_no)); BX_PIC_THIS s.master_pic.IRQ_line[irq_no] = 0; BX_PIC_THIS s.master_pic.irr &= ~(1 << irq_no); if ((BX_PIC_THIS s.master_pic.irr & ~BX_PIC_THIS s.master_pic.imr) == 0) { BX_SET_INTR(0); BX_PIC_THIS s.master_pic.INT = 0; } }... }}...Ugly, isn't it? If we use static methods, methods prefixed with BX_PIC_SMFare declared static and references to fields inside the object, whichare prefixed with BX_PIC_THIS, will use the globally visible object,bx_pic. If we don't use static methods, BX_PIC_SMF evaluates tonothing and BX_PIC_THIS becomes this->. Making it evaluate tonothing would be a lot cleaner, but then the scoping rules would changeslightly between the two bochs configurations, which would be a load of bugsjust waiting to happen.Some classes use BX_SMF, others have their own version of the macro, likeBX_PIC_SMF above.

2.2 CPU and memory objects in UP/SMPconfigurations

The CPU class is a special case of the above: if bochs is simulating a uni-processor machine then there is obviously only one bx_cpu_c object and thestatic methods trick can be used. If, on the other hand, bochs is simulatingan smp machine then we can't use the trick. The same seems to be true formemory: for some reason, we have a memory object for each CPU object. Thismight become relevant for NUMA machines, but they are not all that common --and even the existing IA-32 NUMA machines bend over backwards to hide thatfact: it should only be visible in slightly worse timing for non-local memoryand non-local peripherals. Other than that, the memory map and device mappresented to each CPU will be identical.In a UP configuration, the CPU object is declared as bx_cpu. In anSMP configuration it will be an array of pointers to CPU objects(bx_cpu_array[]). For memory that would be bx_mem andbx_mem_array[], respectively.Each CPU object contains a pointer to its associated memory object.Access of a CPU object often goes through the BX_CPU(x) macro,which either ignores the parameter and evaluates to &bx_cpu, orevaluates to bx_cpu_array[n], so the result will always be a pointer.The same goes for BX_MEM(x).If static methods are used then BX_CPU_THIS_PTR evaluates toBX_CPU(0)->. Ugly, isn't it?

2.3 BX_DEBUG/BX_INFO/BX_ERROR/BX_PANIC --logging macros

go through a generic tracing mechanism. Can be switched individuallyon/off. Might eat a lot of CPU time - I think there are some BX_INFO calls foreach instruction executed.

2.4 BX_TICK1, BX_TICKN(n),BX_TICK1_IF_SINGLE_PROCESSOR

BX_TICK1_IF_SINGLE_PROCESSOR, only used in cpu.cc -- and onlyconfuses the matter. It calls BX_TICK1 on a single-processor andnothing on SMP.

3 CHECK_MAX_INSTRUCTIONS(count) - onlyneeded on SMP configurations without debugger support. I am going to changethe CPU emulation a lot (hopefully cleaning it up in the process), so I'vedecided to lose every SMP thing that gets in the way for me. This is one ofthem. Later, when UP works faster and better, I fully intend to restore SMPfunctionality -- or work with somebody else who does.

3.1 BX_SIM_ID

When using cosimulation it has something to do with which simulator that isexecuting? In any case, I removed it from my own source tree.

3.2 BX_HRQ, BX_RAISE_HLDA, BX_INTR,BX_SET_INTR(b), BX_IAC()

3.3 Various macros associated with dynamictranslation

Relics of Kevin Lawton's initial attempts of using dynamic translation toIA-32 machine code instead of interpretive emulation. That developmentcontinued in Plex86, which seems to be more or less abandoned for the moment.Bochs will probably go in the direction of dynamic translation at some pointin the future but for we will concentrate on better GUIs, betterconfiguration, better hardware emulation and better support for reverseengineering. We would also very much like bochs to be faster but we will usesimpler methods for the foreseeable future. These relics will be cut out assoon as possible.

3.4 Cosimulation support

For debugging changes in the CPU emulation, especially really bigoptimizations, Kevin Lawton invented something he called"cosimulation". The idea is to run two different CPU emulators inlock-step and constantly compare their CPU state. The idea is very good-- and has been independently discovered by many people for decades -- but ishard to put into practice. As Kevin Lawton wrote in some early docs:fixme: something about every time he uses cosimulation he has to hack onthe code to make it work. I think the prudent thing would be to remove itfor the time being -- and hack in specific hooks the next time somebody wantsto use it. It should be maintained as a separate patch until we have found acleaner way of doing it.

4 Memory - An Introduction

Both RAM and BIOS'es. BIOSes can be loaded individually. physical_read(),physical_write(). All address translation and access checking has alreadytaken place in the CPU.Some hardware interaction takes place through this object: VGA. This isunfortunately hardcoded into the memory object at the moment :(

5 The Basic CPU

Simple CPU: no caches! Does have TLBs. Some real IA-32 implementationsdistinguish between TLBs for code and for data -- we don't. We save some timeon having 1024 TLB entries, which is a lot more than almost all real CPUs haveat the moment. Different CPU levels -- level 5 is not complete, yet.

5.1 Some of the things we have to emulate - TheIA-32

Real mode. Protected mode. 16-bit code, 32-bit code. segments, TLB,instruction prefetch queue, writes to memory can be executed"immediately" (makes things a bit harder for us later on),extraordinarily complex and varied instruction formats. Four differentprivilege levels, and then a system management mode on top of that for some ofthe CPUs. Six different segment registers (four on <386), capable ofoverriding the default segment register for the instruction, usually DS, butsometimes SS. Prefixes, address and operand size changes, tons of flags, tonsof special cases about which registers can be used for what purpose. Totallyfree alignment of both code and data. Instructions can be one to sixteenbytes. IO Privilege level, IO privilege map, V86 mode.Yada yada, you get the picture...

5.2 Some example instructions

(real mode, Intel syntax)INC AXMOV CX,[23+BX](protected mode, 32-bit default size, AT&T syntax)<something with two size prefixes, a 0x0F prefix, a lock prefix? and acomplicated address. LOCK ADD [BX*4 + CX*2 + DX + 1234], 17 ? >

5.3 Decoding instructions is *HARD*

On nicer processors, decoding instructions is an easy task. It's especiallynice on the MIPS and the Alpha.On IA-32 it's just about as lousy as it can get :/ In order to reduce thecomplexity a bit, all the decoding of the operand fields is done first, byBX_CPU_C::FetchDecode(), and then the instruction is executed by one of manyhundred small methods that don't have to care (much) about their operands.

5.4 BxInstruction_t

b1modr/mrep_usedimm8, imm16, imm32jmp...executeresolvemodrm16resolvemodrm32

5.5 The Main Loop - First cut

5.6 The Main Loop -Interrupts/Traps/Exceptions

5.7 The Main Loop - SMP

5.8 "Prefetching"

Should be called something else.

5.9 FetchDecode

5.10 Execute pointers

5.11 The Anatomy of Memory Accesses

segment : offset -> 32-bit linear -> 32-bit physicalsegments, segment caches, base + limit, type

5.12 The Main Loop -Interrupts/Exceptions/Traps

5.13 So how was the prefetching in detailagain?

prefetchrevalidate_prefetch_qinvalidate_prefetch_qwhen is it invalidated?when is it revalidated?when do we actually have to do any of these?

5.14 Things I lied about

A20, extend down segments, FPU, synchronization between CPU and(potentially external) FPU. Reset of the CPU by forcing a triple-fault.debugger interface, config interface temporary disabling of interrupts (afterSS changes) ;; might have to go below the following section

5.15 Flag handling

lazy flags, 5 32-bit ints to describe the operation. Some macros thatevaluate the flags on demand.

5.16 How are exceptions implemented?

all instructions restartable from the register state + BxInstruction_t.Commit EIP + ESP (why that?) after successful execution of the wholeinstruction.Never possible to generate exception /after/ changing the visiblestate.longjmp(), setjmp()

5.17 What if we trip on an assertion?

Lots of checks all over the place. Also deep inside routines called by thecpu main loop. Die/cont/alwayscont/quit in Control Panel - or a debugger. Howdoes it do that? Some variation on the exception scheme?

6 Specific tricks

6.1 4GB in real mode

What is the trick and how does bochs make sure that it works

6.2 Switching from protected mode to realmode

reset + cmos

6.3 Typical reset thru keyboard controller

6.4 Triple-fault reset

6.5 Fast reset gate

6.6 A20 change Should probably hitch a ride onthe TLB paging mechanism for speed.

6.7 "CMOS" NMI gate

6.8 V86

6.9 V86 with virtual interrupt flag

6.10 APIC: IRQ rerouting to NMI

6.11 SMP: IPI (Inter-Processor Interrupt)

6.12 SMP: cache bounces

6.13 SMP: locked read-modify-writes

6.14 SMP: spinlocks

6.15 SMP: TSC potentially out of synch

6.16 SMP: BIOS and necessary tables

6.17 SMM - System Management Mode

Not implemented yet. Required for ACPI, I think.

6.18 Huge amounts of memory

Don't want bochs to push out other programs - handle swapping manually.BIOS and memory size reporting PAE Small window - big memory file Only need toswap in/out when TLB changes Keep memory on 4K boundary and use mmap() Needs> IA-32 machine (e.g. Alpha or some other 64-bit behemoth) or LFS.

6.19 PnP

6.20 PCI - configuration

6.21 PCI controller

7 Things that make you go"hmmm"...

7.1 16K pages between 0xC0000 and 0xFFFFF withPCI

7.2 Whyread_RMW_virtual_(byte|word|dword)?

8 Optimization Ideas

8.1 Traces

"Almost all programming can be viewed as an exercise in caching" -- Terje Mathisenresolve16/32 can't be cached like this (example that uses registers togenerate an effective address)

8.2 Squish out flags handling

BX_NEED_FLAGS, BX_SETS_FLAGS

8.3 How to be lazy with addresses

only retranslate seg:ofs -> linear -> physical when strictlynecessary

8.4 Handle repeating instructions in biggerglobs

special versions of access_linear()

8.5 split access_linear into read and writeversions

8.6 combine segment limits with TLB pages

A bit that says if everything is ok or the address has to bereevaluated

8.7 Better branch prediction for execute ptrcalls

switch (len) { case 7: i[len-7].execute(i[len-7]); case 6: i[len-6].execute(i[len-6]); ... case 0: i[len-0].execute(i[len-0]);}

9 Communication Between Devices

9.1 Ticks and hardware emulation

The non-cpu hardware in the Bochs virtual PC needs to run some code once ina while to either do some real work, synchronize with the rest of the machineor interact with the host OS.Timers, based on simulated instructions retired count. The GUI is made likethis too -- that is probably a bad idea. BX_TICK1_IF_SINGLE_PROCESSOR()Examples of worker functions: xxxxx.

9.2 Interrupts

9.3 DMA

HOLDA

9.4 IRQ pins

ISA IRQ2/9, IRQ3, IRQ4, IRQ5, IRQ6, IRQ7, IRQ...PCI INTA, INTB, INTC, INTD - routing i PCI controller + on motherboard.

9.5 Interrupt routing

Level/edge triggered.PICAPICPCI controller

9.6 NMI

10 Communication between VGA and GUI

10.1 idle (HLT) and GUI

10.2 GUI and configuration

10.2.1 Floppy disk/dev/fd0A:<path to disk file>inserted/ejectedicon click -> set_status(inserted/injected)how to check with ioctl10.2.2 CD ROM/dev/cdromdisk changeHow does El Torito work?Only in BIOS? What about hardware ATAPI?

11 Various Hardware

11.1 CPU

11.2 CPU - SMP

11.3 APIC

11.4 PIT

11.5 PIC - master/slave

11.6 Slowdown

11.7 Realtime PIC

11.8 RTC + CMOS

11.9 FPU FWait, exception handling, somethingabout a weird exception + an IRQ reserved for the FPU.

11.10 Memory Some of the address range ishandled by the i440 PCI chipset, which may subdivide it further.

11.11 i440 PCI chipset Also handles shadowROMs.

11.12 AGP

11.13 DMA address bus sizes? built into PCIchipset? speed? limited to ISA bus speed? -- no. DMA happens as fast asdevices want, provided the CPU allows it.

11.14 Floppy Controller

11.15 IDE

11.16 Harddisk

11.17 CDROM

11.18 Speaker

11.19 Sound Blaster

11.20 NE2K NIC

11.21 Mouse

11.22 Keyboard

11.23 Parallel port

11.24 Serial Port

11.25 USB

11.26 SCSI

11.27 IRQ in general

11.28 Ordinary BIOS

11.29 VGA BIOS

11.30 LBE both some BIOS calls and some"hardware"

12 How to register a new device

13 How to make snapshots

14 How to suspend/resume

15 How to make configurations easier

#! .... bochs --help bochs -h bochs --version // also prints compile options bochs -V (version) bochs <config filename> bochs -v // tells us which config file is used + all the options read from // it.

16 Dreams for the future:

Suspend/resume - without APM (for debugging/wizards).Suspend/resume - with APM.Automatic floppy disk change detect.Automatic cdrom disk change detect.More than one boot device (.bochsrc -> CMOS, read by BIOS)Check that longjmp()/setjmp() doesn't violate C/C++ rules about whichvariables are valid after a jump.Net bridgeGTK+/Gnome GUISetup WizardDebugger interface in cTVisionBetter mouse + keyboard handling - copy VMWare with XGrabKey/XGrabMouseobviate the need for a client program to handle the mouse + keyboardLinux console API for the screenEasily run on real SVGA hardware, with only a thin debug/log layer inbetweenlikewise for other hardware - tell bochs what I/O, IRQ, DMA, mem resourcesthe hardware uses, let it negotiate with Linux to access and lock it. Mightneed a suid proxy. SVGA is a special case :)good term mode (bochs in curses - emulate MDA? CGA?)bidirectional parallel port - some API as Linux 2.4 and VMWaregood idle handling in all GUIsuse shared memory, so many bochs instances will share a pool of memorycut'n'paste between host and guestGNU lightning JIT'ingPort GNU lightning to AlphaUse Xft/Render so copying vga fonts won't be necessary anymoreCompressed harddisks and undo logs/checkpointsTænk hvis vores disk I/O ender med at blive hurtigere end VMWares ;)Autodetect hvilken mus og antal knapper og den slagsFloppy/cdrom/etc icons -> dialogs that let you choose images/Much/ better error messages!MIDI supportJoystick support - w/mouse, analog joystick, digital joystick, etc. asinput.USB - access to real usb netUSB - proxy the mouse + printer to bochs' usb net?USB - debugger/monitorUSB - tun/tap like interface to user provided simulated hardwaredecide on good and consistent configuration strategy: use CMOS orconfiguration file.

17 Error messages

Check that floppy/cdrom/disk are accessible with the current privileges andgive the poor user some sensible error messages if not, INCLUDING examples ofcommands to fix the problem(s).With bigmem support: check that 1) glibc supports LFS, 2) that the kernelsupports it, 3) that the file system supports it, 4) that there is room enoughin the designated directory.

18 Tools/links

nasm - also contains ndisasm, a nice disassembler for 8086 real mode and386 protected mode. http://nasm.sourceforge.net/bcc - Bruce's C Compiler, by Bruce Evans. Generates either 8086 real modecode or 386 protected mode code. Used to compile the BIOSes.as86 - Assembler for 8086 real mode code, by Bruce Evans. version xxx -older versions don't accept the -O (optimize forward jumps) flag. It is notquite Intel syntax (and very far from AT&T syntax). Usually included withld86 in a package called bin86. Built into cygwin (FIXME: true?)ld86 - Linker for 8086 real mode code, by Bruce Evans. See as86.PC Timing FAQ, by Kris Heidenstrom - his home page is athttp://home.clear.net.nz/pages/kheidens/ and has many interesting docs. Here'sthe link to the FAQ:ftp://ftp.simtel.net/pub/simtelnet/msdos/info/pctim003.zipSerial Port FAQ release 19, by Christian Blum:http://www.repairfaq.org/filipg/LINK/PORTS/F_The_Serial_Port.html There areother versions floating around on the net but this was the newest I couldfind. It's in HTML whereas the original I read so many years ago was a nice,single plain text file. If somebody finds a link to that version I'd like toknow.
 

A

description

of

how

Bochs

is

implemented

internally

(Work

in

progress).

http://www.diku.dk/hjemmesider/studerende/firefly/bochsdoc.html

How Bochs works under the hood 2008 August

dvd rental

dvd


A description of how Bochs is implemented internally (Work in progress).

Rules




© 2008 Internet Explorer 5+ or Netscape 6+

Recommended Sites: 1. Arts - Business - Computers - Games - Health - Home - Kids and Teens - News - Recreation - Reference - Regional - Science - Shopping - Society - Sports - World Miss Gallery - Top Anime Hentai - DVD rental by mail - New York Hotels - Fast Loans - Buy Anything On eBay - Loans - Power Tools
2008-08-30 12:48:36

Copyright 2005, 2006 by Webmaster
Websites is cool :) 20Pozycjonowanie Stron - Hotel Stuttgart - Bingo - Krakow Hotels - Kasy Fiskalne