Compare commits
	
		
			269 Commits
		
	
	
		
			queue/ui-p
			...
			SLE11-SP4-
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					1aff087cee | ||
| 
						 | 
					b4f36774b7 | ||
| 
						 | 
					2531d7ee4d | ||
| 
						 | 
					6855c034e7 | ||
| 
						 | 
					51e5a6007e | ||
| 
						 | 
					e3806f5d57 | ||
| 
						 | 
					b8c2ea9776 | ||
| 0398a1b258 | |||
| 
						 | 
					4bf75ff6d6 | ||
| 
						 | 
					0447550926 | ||
| 
						 | 
					b2bdec338d | ||
| 
						 | 
					a586d7d202 | ||
| 
						 | 
					85dadfc305 | ||
| 
						 | 
					7b2dfb5d35 | ||
| 
						 | 
					686fab80bd | ||
| 
						 | 
					abfdc2e6ff | ||
| 
						 | 
					d05ce95406 | ||
| 
						 | 
					2c2261cfcc | ||
| 
						 | 
					c6616fd654 | ||
| 
						 | 
					efe2752601 | ||
| 
						 | 
					6e543d2c12 | ||
| 
						 | 
					de04929dd2 | ||
| 
						 | 
					670d5d7fee | ||
| 
						 | 
					94107cdfae | ||
| 
						 | 
					394084d080 | ||
| 
						 | 
					551fc996b5 | ||
| 
						 | 
					ee23dcb6c1 | ||
| 
						 | 
					b42aedacc9 | ||
| 215b18c9fc | |||
| 
						 | 
					402d0d1f0c | ||
| 
						 | 
					58b487a2bc | ||
| 
						 | 
					6d9a092479 | ||
| 
						 | 
					e87058cc54 | ||
| 
						 | 
					3b75987950 | ||
| 
						 | 
					37f7413027 | ||
| 
						 | 
					44a0871d54 | ||
| 
						 | 
					cb74d9ee43 | ||
| 
						 | 
					59cdffd5f1 | ||
| 
						 | 
					18f2150f92 | ||
| 
						 | 
					0625626e51 | ||
| 
						 | 
					1a66a3ca79 | ||
| 
						 | 
					158326e199 | ||
| 
						 | 
					fd67499caa | ||
| 
						 | 
					71149b3e14 | ||
| 
						 | 
					5f5aa07d16 | ||
| 
						 | 
					3a34ab453f | ||
| 
						 | 
					80ce3a7403 | ||
| 
						 | 
					7af0df9343 | ||
| 
						 | 
					094e9d9a91 | ||
| 
						 | 
					a819068104 | ||
| 
						 | 
					2ca9b4d153 | ||
| 
						 | 
					ff80ec1aab | ||
| 
						 | 
					4bf7b7da45 | ||
| 
						 | 
					5a6e91a399 | ||
| 
						 | 
					e8363b7738 | ||
| 
						 | 
					dd9169bc43 | ||
| 
						 | 
					877b642be0 | ||
| 
						 | 
					c181a409d4 | ||
| 
						 | 
					5626edc3f9 | ||
| 
						 | 
					2a57bae0d1 | ||
| 
						 | 
					cca58015c0 | ||
| 
						 | 
					a958839822 | ||
| 
						 | 
					e8a8f9f1c4 | ||
| 
						 | 
					0f4f9527d0 | ||
| 
						 | 
					408dc94b92 | ||
| 
						 | 
					d9593d1734 | ||
| 
						 | 
					301feb072e | ||
| 
						 | 
					9848148c9c | ||
| 
						 | 
					924eda5c4a | ||
| 
						 | 
					8c9ef11d8a | ||
| 
						 | 
					d5685a80ce | ||
| 
						 | 
					5e783ed780 | ||
| 
						 | 
					b27b5c305e | ||
| 
						 | 
					87559bfe5a | ||
| 
						 | 
					211bbf522c | ||
| 
						 | 
					7bae3c9587 | ||
| 
						 | 
					949fab98f8 | ||
| 
						 | 
					ab00d35ba6 | ||
| 
						 | 
					ccb16b84cd | ||
| 
						 | 
					7acfa7e9eb | ||
| 
						 | 
					0f55cd19aa | ||
| 
						 | 
					05fc570638 | ||
| 
						 | 
					a9041a3d9c | ||
| 
						 | 
					9639415b85 | ||
| 
						 | 
					7028f2bc09 | ||
| 
						 | 
					573bea06b3 | ||
| 
						 | 
					b0f69fb75c | ||
| 
						 | 
					3f5672ec57 | ||
| 
						 | 
					7eb2402026 | ||
| 
						 | 
					176d3f3351 | ||
| 
						 | 
					da96690d12 | ||
| 
						 | 
					e3bd9029dc | ||
| 
						 | 
					b4c60b7142 | ||
| 
						 | 
					57c36784f0 | ||
| 
						 | 
					3211c90e16 | ||
| 
						 | 
					ca59c611d6 | ||
| 
						 | 
					278ffa97d6 | ||
| 
						 | 
					617ae61fa9 | ||
| 
						 | 
					593f5a543b | ||
| 
						 | 
					8118864031 | ||
| 
						 | 
					b1fe4e34b1 | ||
| 
						 | 
					a3d9060d83 | ||
| 
						 | 
					44309c1775 | ||
| 
						 | 
					f5a19fb649 | ||
| 
						 | 
					1b65531708 | ||
| 
						 | 
					cf8285b5f4 | ||
| 
						 | 
					2509270b3b | ||
| 
						 | 
					9571fdcc96 | ||
| 
						 | 
					f01d1c4975 | ||
| 
						 | 
					33fc1224b0 | ||
| 
						 | 
					355d1697da | ||
| 
						 | 
					721dcef81a | ||
| 
						 | 
					68bdfae5e5 | ||
| 
						 | 
					698c02a4f7 | ||
| 
						 | 
					a39e5bb368 | ||
| 
						 | 
					3d62fd2ba0 | ||
| 
						 | 
					342e94e056 | ||
| 
						 | 
					4b1e5f667f | ||
| 
						 | 
					639373b494 | ||
| 
						 | 
					5116278b67 | ||
| 
						 | 
					bf2d7690fa | ||
| 
						 | 
					c6a1d6e329 | ||
| 
						 | 
					b9263558d9 | ||
| 
						 | 
					669fb73c5f | ||
| 
						 | 
					6ec86bb006 | ||
| 
						 | 
					90e32d9444 | ||
| 
						 | 
					63a4d51a78 | ||
| 
						 | 
					ddd66af037 | ||
| 
						 | 
					603c7238ff | ||
| 
						 | 
					2b87f79326 | ||
| 
						 | 
					3e812e702d | ||
| 
						 | 
					05061c843c | ||
| 
						 | 
					3dc60f68ae | ||
| 
						 | 
					8cfd98d3ae | ||
| 
						 | 
					a681202d4e | ||
| 
						 | 
					e3401412dc | ||
| 
						 | 
					fd5534832f | ||
| 
						 | 
					4c739e0d6d | ||
| 
						 | 
					300a4123d8 | ||
| 
						 | 
					1ec1ef8f31 | ||
| 
						 | 
					9a716057cd | ||
| 
						 | 
					3fc0198478 | ||
| 
						 | 
					7ece92f464 | ||
| 
						 | 
					d0ccd870a6 | ||
| 
						 | 
					46fbe7a783 | ||
| 
						 | 
					383f92e67d | ||
| 
						 | 
					383017b9bd | ||
| 
						 | 
					4bbf4e9d8e | ||
| 
						 | 
					e56ac60924 | ||
| 
						 | 
					57e3bf66c2 | ||
| 
						 | 
					50c6b143ff | ||
| 
						 | 
					61c8e3c532 | ||
| 
						 | 
					1380543027 | ||
| 
						 | 
					53b7a865ee | ||
| 
						 | 
					ed32292df6 | ||
| 
						 | 
					a9692822d8 | ||
| 
						 | 
					c173bbff64 | ||
| 
						 | 
					230c029d16 | ||
| 
						 | 
					1e5e5a10c3 | ||
| 
						 | 
					e57cc5a2f9 | ||
| 
						 | 
					f9cea35a81 | ||
| 
						 | 
					0ae2b92b29 | ||
| 
						 | 
					a4d378b1ce | ||
| 
						 | 
					de8526eefe | ||
| 
						 | 
					3ab66401c3 | ||
| 
						 | 
					f6af7357df | ||
| 
						 | 
					c8af02494d | ||
| 
						 | 
					1bb261f222 | ||
| 
						 | 
					f0922ef574 | ||
| 
						 | 
					a6aa2f9c54 | ||
| 
						 | 
					bf5b9c24c5 | ||
| 
						 | 
					63d9acbe99 | ||
| 
						 | 
					632f4958a0 | ||
| 
						 | 
					66db770b81 | ||
| 
						 | 
					363b2fc4f0 | ||
| 
						 | 
					287b7249b6 | ||
| 
						 | 
					ea55d53b1b | ||
| 
						 | 
					8e4eb52196 | ||
| 
						 | 
					5efb9ade7e | ||
| 
						 | 
					73aab0ebf2 | ||
| 
						 | 
					bad6f6bc3f | ||
| 
						 | 
					e536419507 | ||
| 
						 | 
					1bb6f0527b | ||
| 
						 | 
					a45b1c7069 | ||
| 
						 | 
					827326be7b | ||
| 
						 | 
					89400a80f5 | ||
| 
						 | 
					e85b521519 | ||
| 
						 | 
					f890185392 | ||
| 
						 | 
					745f6c0ef7 | ||
| 
						 | 
					0182df5ae5 | ||
| 
						 | 
					7f28f0f1f6 | ||
| 
						 | 
					45bbe1fa89 | ||
| 
						 | 
					06efdc4f4d | ||
| 
						 | 
					0c70b5ad59 | ||
| 
						 | 
					b90fd157f7 | ||
| 
						 | 
					7322cb17fa | ||
| 
						 | 
					1d7723ffc7 | ||
| 
						 | 
					67b460a404 | ||
| 
						 | 
					84247bbe28 | ||
| 
						 | 
					2ebcc590c9 | ||
| 
						 | 
					69001b3145 | ||
| 
						 | 
					3accab7365 | ||
| 
						 | 
					60259539ee | ||
| 
						 | 
					93399d0827 | ||
| 
						 | 
					074dd56a01 | ||
| 
						 | 
					d10d2510b9 | ||
| 
						 | 
					5613bda4ac | ||
| 
						 | 
					c5675a98bb | ||
| 
						 | 
					e355efd962 | ||
| 
						 | 
					4d7f4556fc | ||
| 
						 | 
					0486c27a36 | ||
| 
						 | 
					57105f7480 | ||
| 
						 | 
					6e8865313f | ||
| 
						 | 
					6d0b135a98 | ||
| 
						 | 
					d89f9ba43b | ||
| 
						 | 
					46f9071a23 | ||
| 
						 | 
					f85e082a36 | ||
| 
						 | 
					da78a1bc7a | ||
| 
						 | 
					2b92aa36d1 | ||
| 
						 | 
					e4cce2d3e9 | ||
| 
						 | 
					d15b1aa30c | ||
| 
						 | 
					65fe29ec00 | ||
| 
						 | 
					888e036eb4 | ||
| 
						 | 
					d019dd928c | ||
| 
						 | 
					dac077f0e6 | ||
| 
						 | 
					b09a673164 | ||
| 
						 | 
					79a4dd4085 | ||
| 
						 | 
					57e929c19c | ||
| 
						 | 
					27c71355fb | ||
| 
						 | 
					283b7de6a5 | ||
| 
						 | 
					a1cb89f3fe | ||
| 
						 | 
					68f9df5990 | ||
| 
						 | 
					0135796271 | ||
| 
						 | 
					799a34a48b | ||
| 
						 | 
					8378910554 | ||
| 
						 | 
					7a238b9fbd | ||
| 
						 | 
					02493ee490 | ||
| 
						 | 
					7d47b243d6 | ||
| 
						 | 
					02ea844746 | ||
| 
						 | 
					0fcf00b55c | ||
| 
						 | 
					5610ef5863 | ||
| 
						 | 
					7a687aed28 | ||
| 
						 | 
					b91aee5810 | ||
| 
						 | 
					e09b99b54f | ||
| 
						 | 
					611c7f2c3a | ||
| 
						 | 
					4e4566ce78 | ||
| 
						 | 
					43e00611bc | ||
| 
						 | 
					3c3de7c6b4 | ||
| 
						 | 
					b0da310a69 | ||
| 
						 | 
					d26efd2d39 | ||
| 
						 | 
					f305d504ab | ||
| 
						 | 
					d3652a1b28 | ||
| 
						 | 
					51943504d5 | ||
| 
						 | 
					4d1cdb9efd | ||
| 
						 | 
					c3b81e01b8 | ||
| 
						 | 
					99b1f39bd2 | ||
| 
						 | 
					f23ab037c7 | ||
| 
						 | 
					0c918dd600 | ||
| 
						 | 
					a8b090ef08 | ||
| 
						 | 
					4a38944326 | ||
| 
						 | 
					b7ff1a7a00 | ||
| 
						 | 
					d49fed4c55 | ||
| 
						 | 
					cebb8ebe41 | ||
| 
						 | 
					3b39a11cde | ||
| 
						 | 
					ec9f828341 | ||
| 
						 | 
					332e93417a | ||
| 
						 | 
					e6b795f34e | ||
| 
						 | 
					51968b8503 | ||
| 
						 | 
					80d8b5da48 | 
@@ -1,2 +0,0 @@
 | 
			
		||||
((c-mode . ((c-file-style . "stroustrup")
 | 
			
		||||
	    (indent-tabs-mode . nil))))
 | 
			
		||||
							
								
								
									
										8
									
								
								.gdbinit
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								.gdbinit
									
									
									
									
									
								
							@@ -1,8 +0,0 @@
 | 
			
		||||
# GDB may have ./.gdbinit loading disabled by default.  In that case you can
 | 
			
		||||
# follow the instructions it prints.  They boil down to adding the following to
 | 
			
		||||
# your home directory's ~/.gdbinit file:
 | 
			
		||||
#
 | 
			
		||||
#   add-auto-load-safe-path /path/to/qemu/.gdbinit
 | 
			
		||||
 | 
			
		||||
# Load QEMU-specific sub-commands and settings
 | 
			
		||||
source scripts/qemu-gdb.py
 | 
			
		||||
							
								
								
									
										187
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										187
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,71 +1,67 @@
 | 
			
		||||
/config-devices.*
 | 
			
		||||
/config-all-devices.*
 | 
			
		||||
/config-all-disas.*
 | 
			
		||||
/config-host.*
 | 
			
		||||
/config-target.*
 | 
			
		||||
/config.status
 | 
			
		||||
/config-temp
 | 
			
		||||
/trace-events-all
 | 
			
		||||
/trace/generated-events.h
 | 
			
		||||
/trace/generated-events.c
 | 
			
		||||
/trace/generated-helpers-wrappers.h
 | 
			
		||||
/trace/generated-helpers.h
 | 
			
		||||
/trace/generated-helpers.c
 | 
			
		||||
/trace/generated-tcg-tracers.h
 | 
			
		||||
/ui/shader/texture-blit-frag.h
 | 
			
		||||
/ui/shader/texture-blit-vert.h
 | 
			
		||||
config-devices.*
 | 
			
		||||
config-all-devices.*
 | 
			
		||||
config-all-disas.*
 | 
			
		||||
config-host.*
 | 
			
		||||
config-target.*
 | 
			
		||||
trace/generated-tracers.h
 | 
			
		||||
trace/generated-tracers.c
 | 
			
		||||
trace/generated-tracers-dtrace.h
 | 
			
		||||
trace/generated-tracers-dtrace.dtrace
 | 
			
		||||
libcacard/trace/generated-tracers.c
 | 
			
		||||
*-timestamp
 | 
			
		||||
/*-softmmu
 | 
			
		||||
/*-darwin-user
 | 
			
		||||
/*-linux-user
 | 
			
		||||
/*-bsd-user
 | 
			
		||||
/ivshmem-client
 | 
			
		||||
/ivshmem-server
 | 
			
		||||
/libdis*
 | 
			
		||||
/libuser
 | 
			
		||||
/linux-headers/asm
 | 
			
		||||
/qga/qapi-generated
 | 
			
		||||
/qapi-generated
 | 
			
		||||
/qapi-types.[ch]
 | 
			
		||||
/qapi-visit.[ch]
 | 
			
		||||
/qapi-event.[ch]
 | 
			
		||||
/qmp-commands.h
 | 
			
		||||
/qmp-introspect.[ch]
 | 
			
		||||
/qmp-marshal.c
 | 
			
		||||
/qemu-doc.html
 | 
			
		||||
/qemu-doc.info
 | 
			
		||||
/qemu-doc.txt
 | 
			
		||||
/qemu-img
 | 
			
		||||
/qemu-nbd
 | 
			
		||||
/qemu-options.def
 | 
			
		||||
/qemu-options.texi
 | 
			
		||||
/qemu-img-cmds.texi
 | 
			
		||||
/qemu-img-cmds.h
 | 
			
		||||
/qemu-io
 | 
			
		||||
/qemu-ga
 | 
			
		||||
/qemu-bridge-helper
 | 
			
		||||
/qemu-monitor.texi
 | 
			
		||||
/qemu-monitor-info.texi
 | 
			
		||||
/qemu-version.h
 | 
			
		||||
/qemu-version.h.tmp
 | 
			
		||||
/module_block.h
 | 
			
		||||
/vscclient
 | 
			
		||||
/vhost-user-scsi
 | 
			
		||||
/fsdev/virtfs-proxy-helper
 | 
			
		||||
*.[1-9]
 | 
			
		||||
*-softmmu
 | 
			
		||||
*-darwin-user
 | 
			
		||||
*-linux-user
 | 
			
		||||
*-bsd-user
 | 
			
		||||
libdis*
 | 
			
		||||
libuser
 | 
			
		||||
linux-headers/asm
 | 
			
		||||
qapi-generated
 | 
			
		||||
qapi-types.[ch]
 | 
			
		||||
qapi-visit.[ch]
 | 
			
		||||
qmp-commands.h
 | 
			
		||||
qmp-marshal.c
 | 
			
		||||
qemu-doc.html
 | 
			
		||||
qemu-tech.html
 | 
			
		||||
qemu-doc.info
 | 
			
		||||
qemu-tech.info
 | 
			
		||||
qemu.1
 | 
			
		||||
qemu.pod
 | 
			
		||||
qemu-img.1
 | 
			
		||||
qemu-img.pod
 | 
			
		||||
qemu-img
 | 
			
		||||
qemu-nbd
 | 
			
		||||
qemu-nbd.8
 | 
			
		||||
qemu-nbd.pod
 | 
			
		||||
qemu-options.def
 | 
			
		||||
qemu-options.texi
 | 
			
		||||
qemu-img-cmds.texi
 | 
			
		||||
qemu-img-cmds.h
 | 
			
		||||
qemu-io
 | 
			
		||||
qemu-ga
 | 
			
		||||
qemu-bridge-helper
 | 
			
		||||
qemu-monitor.texi
 | 
			
		||||
vscclient
 | 
			
		||||
QMP/qmp-commands.txt
 | 
			
		||||
test-coroutine
 | 
			
		||||
test-qmp-input-visitor
 | 
			
		||||
test-qmp-output-visitor
 | 
			
		||||
test-string-input-visitor
 | 
			
		||||
test-string-output-visitor
 | 
			
		||||
test-visitor-serialization
 | 
			
		||||
fsdev/virtfs-proxy-helper
 | 
			
		||||
fsdev/virtfs-proxy-helper.1
 | 
			
		||||
fsdev/virtfs-proxy-helper.pod
 | 
			
		||||
.gdbinit
 | 
			
		||||
*.a
 | 
			
		||||
*.aux
 | 
			
		||||
*.cp
 | 
			
		||||
*.dvi
 | 
			
		||||
*.exe
 | 
			
		||||
*.msi
 | 
			
		||||
*.dll
 | 
			
		||||
*.so
 | 
			
		||||
*.mo
 | 
			
		||||
*.fn
 | 
			
		||||
*.ky
 | 
			
		||||
*.log
 | 
			
		||||
*.pdf
 | 
			
		||||
*.pod
 | 
			
		||||
*.cps
 | 
			
		||||
*.fns
 | 
			
		||||
*.kys
 | 
			
		||||
@@ -75,60 +71,31 @@
 | 
			
		||||
*.tp
 | 
			
		||||
*.vr
 | 
			
		||||
*.d
 | 
			
		||||
!/scripts/qemu-guest-agent/fsfreeze-hook.d
 | 
			
		||||
!scripts/qemu-guest-agent/fsfreeze-hook.d
 | 
			
		||||
*.o
 | 
			
		||||
.sdk
 | 
			
		||||
*.gcda
 | 
			
		||||
*.gcno
 | 
			
		||||
/pc-bios/bios-pq/status
 | 
			
		||||
/pc-bios/vgabios-pq/status
 | 
			
		||||
/pc-bios/optionrom/linuxboot.asm
 | 
			
		||||
/pc-bios/optionrom/linuxboot.bin
 | 
			
		||||
/pc-bios/optionrom/linuxboot.raw
 | 
			
		||||
/pc-bios/optionrom/linuxboot.img
 | 
			
		||||
/pc-bios/optionrom/linuxboot_dma.asm
 | 
			
		||||
/pc-bios/optionrom/linuxboot_dma.bin
 | 
			
		||||
/pc-bios/optionrom/linuxboot_dma.raw
 | 
			
		||||
/pc-bios/optionrom/linuxboot_dma.img
 | 
			
		||||
/pc-bios/optionrom/multiboot.asm
 | 
			
		||||
/pc-bios/optionrom/multiboot.bin
 | 
			
		||||
/pc-bios/optionrom/multiboot.raw
 | 
			
		||||
/pc-bios/optionrom/multiboot.img
 | 
			
		||||
/pc-bios/optionrom/kvmvapic.asm
 | 
			
		||||
/pc-bios/optionrom/kvmvapic.bin
 | 
			
		||||
/pc-bios/optionrom/kvmvapic.raw
 | 
			
		||||
/pc-bios/optionrom/kvmvapic.img
 | 
			
		||||
/pc-bios/s390-ccw/s390-ccw.elf
 | 
			
		||||
/pc-bios/s390-ccw/s390-ccw.img
 | 
			
		||||
/docs/interop/qemu-ga-qapi.texi
 | 
			
		||||
/docs/interop/qemu-ga-ref.html
 | 
			
		||||
/docs/interop/qemu-ga-ref.info*
 | 
			
		||||
/docs/interop/qemu-ga-ref.txt
 | 
			
		||||
/docs/interop/qemu-qmp-qapi.texi
 | 
			
		||||
/docs/interop/qemu-qmp-ref.html
 | 
			
		||||
/docs/interop/qemu-qmp-ref.info*
 | 
			
		||||
/docs/interop/qemu-qmp-ref.txt
 | 
			
		||||
/docs/version.texi
 | 
			
		||||
*.tps
 | 
			
		||||
*.lo
 | 
			
		||||
*.la
 | 
			
		||||
*.pc
 | 
			
		||||
.libs
 | 
			
		||||
*.swp
 | 
			
		||||
*.orig
 | 
			
		||||
.pc
 | 
			
		||||
patches
 | 
			
		||||
pc-bios/bios-pq/status
 | 
			
		||||
pc-bios/vgabios-pq/status
 | 
			
		||||
pc-bios/optionrom/linuxboot.bin
 | 
			
		||||
pc-bios/optionrom/linuxboot.raw
 | 
			
		||||
pc-bios/optionrom/linuxboot.img
 | 
			
		||||
pc-bios/optionrom/multiboot.bin
 | 
			
		||||
pc-bios/optionrom/multiboot.raw
 | 
			
		||||
pc-bios/optionrom/multiboot.img
 | 
			
		||||
pc-bios/optionrom/kvmvapic.bin
 | 
			
		||||
pc-bios/optionrom/kvmvapic.raw
 | 
			
		||||
pc-bios/optionrom/kvmvapic.img
 | 
			
		||||
pc-bios/s390-ccw/s390-ccw.elf
 | 
			
		||||
pc-bios/s390-ccw/s390-ccw.img
 | 
			
		||||
.stgit-*
 | 
			
		||||
cscope.*
 | 
			
		||||
tags
 | 
			
		||||
TAGS
 | 
			
		||||
docker-src.*
 | 
			
		||||
*~
 | 
			
		||||
trace.h
 | 
			
		||||
trace.c
 | 
			
		||||
trace-ust.h
 | 
			
		||||
trace-ust.h
 | 
			
		||||
trace-dtrace.h
 | 
			
		||||
trace-dtrace.dtrace
 | 
			
		||||
trace-root.h
 | 
			
		||||
trace-root.c
 | 
			
		||||
trace-ust-root.h
 | 
			
		||||
trace-ust-root.h
 | 
			
		||||
trace-ust-all.h
 | 
			
		||||
trace-ust-all.c
 | 
			
		||||
trace-dtrace-root.h
 | 
			
		||||
trace-dtrace-root.dtrace
 | 
			
		||||
trace-ust-all.h
 | 
			
		||||
trace-ust-all.c
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										29
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										29
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							@@ -1,39 +1,24 @@
 | 
			
		||||
[submodule "roms/vgabios"]
 | 
			
		||||
	path = roms/vgabios
 | 
			
		||||
	url = git://git.qemu-project.org/vgabios.git/
 | 
			
		||||
	url = git://git.qemu.org/vgabios.git/
 | 
			
		||||
[submodule "roms/seabios"]
 | 
			
		||||
	path = roms/seabios
 | 
			
		||||
	url = git://git.qemu-project.org/seabios.git/
 | 
			
		||||
	url = git://git.qemu.org/seabios.git/
 | 
			
		||||
[submodule "roms/SLOF"]
 | 
			
		||||
	path = roms/SLOF
 | 
			
		||||
	url = git://git.qemu-project.org/SLOF.git
 | 
			
		||||
	url = git://git.qemu.org/SLOF.git
 | 
			
		||||
[submodule "roms/ipxe"]
 | 
			
		||||
	path = roms/ipxe
 | 
			
		||||
	url = git://git.qemu-project.org/ipxe.git
 | 
			
		||||
	url = git://git.qemu.org/ipxe.git
 | 
			
		||||
[submodule "roms/openbios"]
 | 
			
		||||
	path = roms/openbios
 | 
			
		||||
	url = git://git.qemu-project.org/openbios.git
 | 
			
		||||
[submodule "roms/openhackware"]
 | 
			
		||||
	path = roms/openhackware
 | 
			
		||||
	url = git://git.qemu-project.org/openhackware.git
 | 
			
		||||
	url = git://git.qemu.org/openbios.git
 | 
			
		||||
[submodule "roms/qemu-palcode"]
 | 
			
		||||
	path = roms/qemu-palcode
 | 
			
		||||
	url = git://github.com/rth7680/qemu-palcode.git
 | 
			
		||||
	url = git://repo.or.cz/qemu-palcode.git
 | 
			
		||||
[submodule "roms/sgabios"]
 | 
			
		||||
	path = roms/sgabios
 | 
			
		||||
	url = git://git.qemu-project.org/sgabios.git
 | 
			
		||||
	url = git://git.qemu.org/sgabios.git
 | 
			
		||||
[submodule "pixman"]
 | 
			
		||||
	path = pixman
 | 
			
		||||
	url = git://anongit.freedesktop.org/pixman
 | 
			
		||||
[submodule "dtc"]
 | 
			
		||||
	path = dtc
 | 
			
		||||
	url = git://git.qemu-project.org/dtc.git
 | 
			
		||||
[submodule "roms/u-boot"]
 | 
			
		||||
	path = roms/u-boot
 | 
			
		||||
	url = git://git.qemu-project.org/u-boot.git
 | 
			
		||||
[submodule "roms/skiboot"]
 | 
			
		||||
	path = roms/skiboot
 | 
			
		||||
	url = git://git.qemu.org/skiboot.git
 | 
			
		||||
[submodule "roms/QemuMacDrivers"]
 | 
			
		||||
	path = roms/QemuMacDrivers
 | 
			
		||||
	url = git://git.qemu.org/QemuMacDrivers.git
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										3
									
								
								.mailmap
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								.mailmap
									
									
									
									
									
								
							@@ -2,8 +2,7 @@
 | 
			
		||||
# into proper addresses so that they are counted properly in git shortlog output.
 | 
			
		||||
#
 | 
			
		||||
Andrzej Zaborowski <balrogg@gmail.com> balrog <balrog@c046a42c-6fe2-441c-8c8c-71466251a162>
 | 
			
		||||
Anthony Liguori <anthony@codemonkey.ws> aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>
 | 
			
		||||
Anthony Liguori <anthony@codemonkey.ws> Anthony Liguori <aliguori@us.ibm.com>
 | 
			
		||||
Anthony Liguori <aliguori@us.ibm.com> aliguori <aliguori@c046a42c-6fe2-441c-8c8c-71466251a162>
 | 
			
		||||
Aurelien Jarno <aurelien@aurel32.net> aurel32 <aurel32@c046a42c-6fe2-441c-8c8c-71466251a162>
 | 
			
		||||
Blue Swirl <blauwirbel@gmail.com> blueswir1 <blueswir1@c046a42c-6fe2-441c-8c8c-71466251a162>
 | 
			
		||||
Edgar E. Iglesias <edgar.iglesias@gmail.com> edgar_igl <edgar_igl@c046a42c-6fe2-441c-8c8c-71466251a162>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,21 +0,0 @@
 | 
			
		||||
language: c
 | 
			
		||||
env:
 | 
			
		||||
  matrix:
 | 
			
		||||
    - IMAGE=debian-armhf-cross
 | 
			
		||||
      TARGET_LIST=arm-softmmu,arm-linux-user
 | 
			
		||||
    - IMAGE=debian-arm64-cross
 | 
			
		||||
      TARGET_LIST=aarch64-softmmu,aarch64-linux-user
 | 
			
		||||
    - IMAGE=debian-s390x-cross
 | 
			
		||||
      TARGET_LIST=s390x-softmmu,s390x-linux-user
 | 
			
		||||
build:
 | 
			
		||||
  pre_ci:
 | 
			
		||||
    - make docker-image-${IMAGE}
 | 
			
		||||
  pre_ci_boot:
 | 
			
		||||
    image_name: qemu
 | 
			
		||||
    image_tag: ${IMAGE}
 | 
			
		||||
    pull: false
 | 
			
		||||
    options: "-e HOME=/root"
 | 
			
		||||
  ci:
 | 
			
		||||
    - unset CC
 | 
			
		||||
    - ./configure ${QEMU_CONFIGURE_OPTS} --target-list=${TARGET_LIST}
 | 
			
		||||
    - make -j2
 | 
			
		||||
							
								
								
									
										194
									
								
								.travis.yml
									
									
									
									
									
								
							
							
						
						
									
										194
									
								
								.travis.yml
									
									
									
									
									
								
							@@ -1,194 +0,0 @@
 | 
			
		||||
sudo: false
 | 
			
		||||
language: c
 | 
			
		||||
python:
 | 
			
		||||
  - "2.4"
 | 
			
		||||
compiler:
 | 
			
		||||
  - gcc
 | 
			
		||||
cache: ccache
 | 
			
		||||
addons:
 | 
			
		||||
  apt:
 | 
			
		||||
    packages:
 | 
			
		||||
      # Build dependencies
 | 
			
		||||
      - libaio-dev
 | 
			
		||||
      - libattr1-dev
 | 
			
		||||
      - libbrlapi-dev
 | 
			
		||||
      - libcap-ng-dev
 | 
			
		||||
      - libgnutls-dev
 | 
			
		||||
      - libgtk-3-dev
 | 
			
		||||
      - libiscsi-dev
 | 
			
		||||
      - liblttng-ust-dev
 | 
			
		||||
      - libnfs-dev
 | 
			
		||||
      - libncurses5-dev
 | 
			
		||||
      - libnss3-dev
 | 
			
		||||
      - libpixman-1-dev
 | 
			
		||||
      - libpng12-dev
 | 
			
		||||
      - librados-dev
 | 
			
		||||
      - libsdl1.2-dev
 | 
			
		||||
      - libseccomp-dev
 | 
			
		||||
      - libspice-protocol-dev
 | 
			
		||||
      - libspice-server-dev
 | 
			
		||||
      - libssh2-1-dev
 | 
			
		||||
      - liburcu-dev
 | 
			
		||||
      - libusb-1.0-0-dev
 | 
			
		||||
      - libvte-2.90-dev
 | 
			
		||||
      - sparse
 | 
			
		||||
      - uuid-dev
 | 
			
		||||
 | 
			
		||||
# The channel name "irc.oftc.net#qemu" is encrypted against qemu/qemu
 | 
			
		||||
# to prevent IRC notifications from forks. This was created using:
 | 
			
		||||
# $ travis encrypt -r "qemu/qemu" "irc.oftc.net#qemu"
 | 
			
		||||
notifications:
 | 
			
		||||
  irc:
 | 
			
		||||
    channels:
 | 
			
		||||
      - secure: "F7GDRgjuOo5IUyRLqSkmDL7kvdU4UcH3Lm/W2db2JnDHTGCqgEdaYEYKciyCLZ57vOTsTsOgesN8iUT7hNHBd1KWKjZe9KDTZWppWRYVwAwQMzVeSOsbbU4tRoJ6Pp+3qhH1Z0eGYR9ZgKYAoTumDFgSAYRp4IscKS8jkoedOqM="
 | 
			
		||||
    on_success: change
 | 
			
		||||
    on_failure: always
 | 
			
		||||
env:
 | 
			
		||||
  global:
 | 
			
		||||
    - TEST_CMD="make check"
 | 
			
		||||
  matrix:
 | 
			
		||||
    - CONFIG=""
 | 
			
		||||
    - CONFIG="--enable-debug --enable-debug-tcg --enable-trace-backends=log"
 | 
			
		||||
    - CONFIG="--disable-linux-aio --disable-cap-ng --disable-attr --disable-brlapi --disable-uuid --disable-libusb"
 | 
			
		||||
    - CONFIG="--enable-modules"
 | 
			
		||||
    - CONFIG="--with-coroutine=ucontext"
 | 
			
		||||
    - CONFIG="--with-coroutine=sigaltstack"
 | 
			
		||||
git:
 | 
			
		||||
  # we want to do this ourselves
 | 
			
		||||
  submodules: false
 | 
			
		||||
before_install:
 | 
			
		||||
  - if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew update ; fi
 | 
			
		||||
  - if [ "$TRAVIS_OS_NAME" == "osx" ]; then brew install libffi gettext glib pixman ; fi
 | 
			
		||||
  - wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
 | 
			
		||||
  - git submodule update --init --recursive
 | 
			
		||||
before_script:
 | 
			
		||||
  - ./configure ${CONFIG}
 | 
			
		||||
script:
 | 
			
		||||
  - make -j3 && ${TEST_CMD}
 | 
			
		||||
matrix:
 | 
			
		||||
  include:
 | 
			
		||||
    # Test with CLang for compile portability
 | 
			
		||||
    - env: CONFIG=""
 | 
			
		||||
      compiler: clang
 | 
			
		||||
    # gprof/gcov are GCC features
 | 
			
		||||
    - env: CONFIG="--enable-gprof --enable-gcov --disable-pie"
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
    # We manually include builds which we disable "make check" for
 | 
			
		||||
    - env: CONFIG="--enable-debug --enable-tcg-interpreter"
 | 
			
		||||
           TEST_CMD=""
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
    - env: CONFIG="--enable-trace-backends=simple"
 | 
			
		||||
           TEST_CMD=""
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
    - env: CONFIG="--enable-trace-backends=ftrace"
 | 
			
		||||
           TEST_CMD=""
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
    - env: CONFIG="--enable-trace-backends=ust"
 | 
			
		||||
           TEST_CMD=""
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
    - env: CONFIG=""
 | 
			
		||||
      os: osx
 | 
			
		||||
      compiler: clang
 | 
			
		||||
    # Plain Trusty System Build
 | 
			
		||||
    - env: CONFIG="--disable-linux-user"
 | 
			
		||||
      sudo: required
 | 
			
		||||
      addons:
 | 
			
		||||
      dist: trusty
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
      before_install:
 | 
			
		||||
        - sudo apt-get update -qq
 | 
			
		||||
        - sudo apt-get build-dep -qq qemu
 | 
			
		||||
        - wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
 | 
			
		||||
        - git submodule update --init --recursive
 | 
			
		||||
    # Plain Trusty Linux User Build
 | 
			
		||||
    - env: CONFIG="--disable-system"
 | 
			
		||||
      sudo: required
 | 
			
		||||
      addons:
 | 
			
		||||
      dist: trusty
 | 
			
		||||
      compiler: gcc
 | 
			
		||||
      before_install:
 | 
			
		||||
        - sudo apt-get update -qq
 | 
			
		||||
        - sudo apt-get build-dep -qq qemu
 | 
			
		||||
        - wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
 | 
			
		||||
        - git submodule update --init --recursive
 | 
			
		||||
    # Trusty System build with latest stable clang
 | 
			
		||||
    - sudo: required
 | 
			
		||||
      addons:
 | 
			
		||||
      dist: trusty
 | 
			
		||||
      language: generic
 | 
			
		||||
      compiler: none
 | 
			
		||||
      env:
 | 
			
		||||
        - COMPILER_NAME=clang CXX=clang++-3.9 CC=clang-3.9
 | 
			
		||||
        - CONFIG="--disable-linux-user --cc=clang-3.9 --cxx=clang++-3.9"
 | 
			
		||||
      before_install:
 | 
			
		||||
        - wget -nv -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -
 | 
			
		||||
        - sudo apt-add-repository -y 'deb http://llvm.org/apt/trusty llvm-toolchain-trusty-3.9 main'
 | 
			
		||||
        - sudo apt-get update -qq
 | 
			
		||||
        - sudo apt-get install -qq -y clang-3.9
 | 
			
		||||
        - sudo apt-get build-dep -qq qemu
 | 
			
		||||
        - wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
 | 
			
		||||
        - git submodule update --init --recursive
 | 
			
		||||
      before_script:
 | 
			
		||||
        - ./configure ${CONFIG} || cat config.log
 | 
			
		||||
    # Trusty Linux User build with latest stable clang
 | 
			
		||||
    - sudo: required
 | 
			
		||||
      addons:
 | 
			
		||||
      dist: trusty
 | 
			
		||||
      language: generic
 | 
			
		||||
      compiler: none
 | 
			
		||||
      env:
 | 
			
		||||
        - COMPILER_NAME=clang CXX=clang++-3.9 CC=clang-3.9
 | 
			
		||||
        - CONFIG="--disable-system --cc=clang-3.9 --cxx=clang++-3.9"
 | 
			
		||||
      before_install:
 | 
			
		||||
        - wget -nv -O - http://llvm.org/apt/llvm-snapshot.gpg.key | sudo apt-key add -
 | 
			
		||||
        - sudo apt-add-repository -y 'deb http://llvm.org/apt/trusty llvm-toolchain-trusty-3.9 main'
 | 
			
		||||
        - sudo apt-get update -qq
 | 
			
		||||
        - sudo apt-get install -qq -y clang-3.9
 | 
			
		||||
        - sudo apt-get build-dep -qq qemu
 | 
			
		||||
        - wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
 | 
			
		||||
        - git submodule update --init --recursive
 | 
			
		||||
      before_script:
 | 
			
		||||
        - ./configure ${CONFIG} || cat config.log
 | 
			
		||||
    # Using newer GCC with sanitizers
 | 
			
		||||
    - addons:
 | 
			
		||||
        apt:
 | 
			
		||||
          sources:
 | 
			
		||||
            # PPAs for newer toolchains
 | 
			
		||||
            - ubuntu-toolchain-r-test
 | 
			
		||||
          packages:
 | 
			
		||||
            # Extra toolchains
 | 
			
		||||
            - gcc-5
 | 
			
		||||
            - g++-5
 | 
			
		||||
            # Build dependencies
 | 
			
		||||
            - libaio-dev
 | 
			
		||||
            - libattr1-dev
 | 
			
		||||
            - libbrlapi-dev
 | 
			
		||||
            - libcap-ng-dev
 | 
			
		||||
            - libgnutls-dev
 | 
			
		||||
            - libgtk-3-dev
 | 
			
		||||
            - libiscsi-dev
 | 
			
		||||
            - liblttng-ust-dev
 | 
			
		||||
            - libnfs-dev
 | 
			
		||||
            - libncurses5-dev
 | 
			
		||||
            - libnss3-dev
 | 
			
		||||
            - libpixman-1-dev
 | 
			
		||||
            - libpng12-dev
 | 
			
		||||
            - librados-dev
 | 
			
		||||
            - libsdl1.2-dev
 | 
			
		||||
            - libseccomp-dev
 | 
			
		||||
            - libspice-protocol-dev
 | 
			
		||||
            - libspice-server-dev
 | 
			
		||||
            - libssh2-1-dev
 | 
			
		||||
            - liburcu-dev
 | 
			
		||||
            - libusb-1.0-0-dev
 | 
			
		||||
            - libvte-2.90-dev
 | 
			
		||||
            - sparse
 | 
			
		||||
            - uuid-dev
 | 
			
		||||
      language: generic
 | 
			
		||||
      compiler: none
 | 
			
		||||
      env:
 | 
			
		||||
        - COMPILER_NAME=gcc CXX=g++-5 CC=gcc-5
 | 
			
		||||
        - CONFIG="--cc=gcc-5 --cxx=g++-5 --disable-pie --disable-linux-user"
 | 
			
		||||
        - TEST_CMD=""
 | 
			
		||||
      before_script:
 | 
			
		||||
        - ./configure ${CONFIG} --extra-cflags="-g3 -O0 -fsanitize=thread -fuse-ld=gold" || cat config.log
 | 
			
		||||
							
								
								
									
										43
									
								
								CODING_STYLE
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								CODING_STYLE
									
									
									
									
									
								
							@@ -9,7 +9,7 @@ patches before submitting.
 | 
			
		||||
Of course, the most important aspect in any coding style is whitespace.
 | 
			
		||||
Crusty old coders who have trouble spotting the glasses on their noses
 | 
			
		||||
can tell the difference between a tab and eight spaces from a distance
 | 
			
		||||
of approximately fifteen parsecs.  Many a flamewar has been fought and
 | 
			
		||||
of approximately fifteen parsecs.  Many a flamewar have been fought and
 | 
			
		||||
lost on this issue.
 | 
			
		||||
 | 
			
		||||
QEMU indents are four spaces.  Tabs are never used, except in Makefiles
 | 
			
		||||
@@ -31,11 +31,7 @@ Do not leave whitespace dangling off the ends of lines.
 | 
			
		||||
 | 
			
		||||
2. Line width
 | 
			
		||||
 | 
			
		||||
Lines should be 80 characters; try not to make them longer.
 | 
			
		||||
 | 
			
		||||
Sometimes it is hard to do, especially when dealing with QEMU subsystems
 | 
			
		||||
that use long function or symbol names.  Even in that case, do not make
 | 
			
		||||
lines much longer than 80 characters.
 | 
			
		||||
Lines are 80 characters; not longer.
 | 
			
		||||
 | 
			
		||||
Rationale:
 | 
			
		||||
 - Some people like to tile their 24" screens with a 6x4 matrix of 80x24
 | 
			
		||||
@@ -43,8 +39,6 @@ Rationale:
 | 
			
		||||
   let them keep doing it.
 | 
			
		||||
 - Code and especially patches is much more readable if limited to a sane
 | 
			
		||||
   line length.  Eighty is traditional.
 | 
			
		||||
 - The four-space indentation makes the most common excuse ("But look
 | 
			
		||||
   at all that white space on the left!") moot.
 | 
			
		||||
 - It is the QEMU coding style.
 | 
			
		||||
 | 
			
		||||
3. Naming
 | 
			
		||||
@@ -90,36 +84,3 @@ and clarity it comes on a line by itself:
 | 
			
		||||
Rationale: a consistent (except for functions...) bracing style reduces
 | 
			
		||||
ambiguity and avoids needless churn when lines are added or removed.
 | 
			
		||||
Furthermore, it is the QEMU coding style.
 | 
			
		||||
 | 
			
		||||
5. Declarations
 | 
			
		||||
 | 
			
		||||
Mixed declarations (interleaving statements and declarations within
 | 
			
		||||
blocks) are generally not allowed; declarations should be at the beginning
 | 
			
		||||
of blocks.
 | 
			
		||||
 | 
			
		||||
Every now and then, an exception is made for declarations inside a
 | 
			
		||||
#ifdef or #ifndef block: if the code looks nicer, such declarations can
 | 
			
		||||
be placed at the top of the block even if there are statements above.
 | 
			
		||||
On the other hand, however, it's often best to move that #ifdef/#ifndef
 | 
			
		||||
block to a separate function altogether.
 | 
			
		||||
 | 
			
		||||
6. Conditional statements
 | 
			
		||||
 | 
			
		||||
When comparing a variable for (in)equality with a constant, list the
 | 
			
		||||
constant on the right, as in:
 | 
			
		||||
 | 
			
		||||
if (a == 1) {
 | 
			
		||||
    /* Reads like: "If a equals 1" */
 | 
			
		||||
    do_something();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Rationale: Yoda conditions (as in 'if (1 == a)') are awkward to read.
 | 
			
		||||
Besides, good compilers already warn users when '==' is mis-typed as '=',
 | 
			
		||||
even when the constant is on the right.
 | 
			
		||||
 | 
			
		||||
7. Comment style
 | 
			
		||||
 | 
			
		||||
We use traditional C-style /* */ comments and avoid // comments.
 | 
			
		||||
 | 
			
		||||
Rationale: The // form is valid in C99, so this is purely a matter of
 | 
			
		||||
consistency of style. The checkpatch script will warn you about this.
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
This file documents changes for QEMU releases 0.12 and earlier.
 | 
			
		||||
For changelog information for later releases, see
 | 
			
		||||
http://wiki.qemu-project.org/ChangeLog or look at the git history for
 | 
			
		||||
http://wiki.qemu.org/ChangeLog or look at the git history for
 | 
			
		||||
more detailed information.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										103
									
								
								HACKING
									
									
									
									
									
								
							
							
						
						
									
										103
									
								
								HACKING
									
									
									
									
									
								
							@@ -1,28 +1,10 @@
 | 
			
		||||
1. Preprocessor
 | 
			
		||||
 | 
			
		||||
1.1. Variadic macros
 | 
			
		||||
 | 
			
		||||
For variadic macros, stick with this C99-like syntax:
 | 
			
		||||
 | 
			
		||||
#define DPRINTF(fmt, ...)                                       \
 | 
			
		||||
    do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
 | 
			
		||||
 | 
			
		||||
1.2. Include directives
 | 
			
		||||
 | 
			
		||||
Order include directives as follows:
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"  /* Always first... */
 | 
			
		||||
#include <...>           /* then system headers... */
 | 
			
		||||
#include "..."           /* and finally QEMU headers. */
 | 
			
		||||
 | 
			
		||||
The "qemu/osdep.h" header contains preprocessor macros that affect the behavior
 | 
			
		||||
of core system headers like <stdint.h>.  It must be the first include so that
 | 
			
		||||
core system headers included by external libraries get the preprocessor macros
 | 
			
		||||
that QEMU depends on.
 | 
			
		||||
 | 
			
		||||
Do not include "qemu/osdep.h" from header files since the .c file will have
 | 
			
		||||
already included it.
 | 
			
		||||
 | 
			
		||||
2. C types
 | 
			
		||||
 | 
			
		||||
It should be common sense to use the right type, but we have collected
 | 
			
		||||
@@ -58,23 +40,8 @@ speaking, the size of guest memory can always fit into ram_addr_t but
 | 
			
		||||
it would not be correct to store an actual guest physical address in a
 | 
			
		||||
ram_addr_t.
 | 
			
		||||
 | 
			
		||||
For CPU virtual addresses there are several possible types.
 | 
			
		||||
vaddr is the best type to use to hold a CPU virtual address in
 | 
			
		||||
target-independent code. It is guaranteed to be large enough to hold a
 | 
			
		||||
virtual address for any target, and it does not change size from target
 | 
			
		||||
to target. It is always unsigned.
 | 
			
		||||
target_ulong is a type the size of a virtual address on the CPU; this means
 | 
			
		||||
it may be 32 or 64 bits depending on which target is being built. It should
 | 
			
		||||
therefore be used only in target-specific code, and in some
 | 
			
		||||
performance-critical built-per-target core code such as the TLB code.
 | 
			
		||||
There is also a signed version, target_long.
 | 
			
		||||
abi_ulong is for the *-user targets, and represents a type the size of
 | 
			
		||||
'void *' in that target's ABI. (This may not be the same as the size of a
 | 
			
		||||
full CPU virtual address in the case of target ABIs which use 32 bit pointers
 | 
			
		||||
on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match
 | 
			
		||||
the target's ABI must use this type for anything that on the target is defined
 | 
			
		||||
to be an 'unsigned long' or a pointer type.
 | 
			
		||||
There is also a signed version, abi_long.
 | 
			
		||||
Use target_ulong (or abi_ulong) for CPU virtual addresses, however
 | 
			
		||||
devices should not need to use target_ulong.
 | 
			
		||||
 | 
			
		||||
Of course, take all of the above with a grain of salt.  If you're about
 | 
			
		||||
to use some system interface that requires a type like size_t, pid_t or
 | 
			
		||||
@@ -111,15 +78,16 @@ avoided.
 | 
			
		||||
Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
 | 
			
		||||
APIs is not allowed in the QEMU codebase. Instead of these routines,
 | 
			
		||||
use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
 | 
			
		||||
g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
 | 
			
		||||
g_new0/g_realloc/g_free or QEMU's qemu_vmalloc/qemu_memalign/qemu_vfree
 | 
			
		||||
APIs.
 | 
			
		||||
 | 
			
		||||
Please note that g_malloc will exit on allocation failure, so there
 | 
			
		||||
is no need to test for failure (as you would have to with malloc).
 | 
			
		||||
Calling g_malloc with a zero size is valid and will return NULL.
 | 
			
		||||
 | 
			
		||||
Memory allocated by qemu_memalign or qemu_blockalign must be freed with
 | 
			
		||||
qemu_vfree, since breaking this will cause problems on Win32.
 | 
			
		||||
Memory allocated by qemu_vmalloc or qemu_memalign must be freed with
 | 
			
		||||
qemu_vfree, since breaking this will cause problems on Win32 and user
 | 
			
		||||
emulators.
 | 
			
		||||
 | 
			
		||||
4. String manipulation
 | 
			
		||||
 | 
			
		||||
@@ -175,62 +143,3 @@ painful. These are:
 | 
			
		||||
 * you may assume that integers are 2s complement representation
 | 
			
		||||
 * you may assume that right shift of a signed integer duplicates
 | 
			
		||||
   the sign bit (ie it is an arithmetic shift, not a logical shift)
 | 
			
		||||
 | 
			
		||||
In addition, QEMU assumes that the compiler does not use the latitude
 | 
			
		||||
given in C99 and C11 to treat aspects of signed '<<' as undefined, as
 | 
			
		||||
documented in the GNU Compiler Collection manual starting at version 4.0.
 | 
			
		||||
 | 
			
		||||
7. Error handling and reporting
 | 
			
		||||
 | 
			
		||||
7.1 Reporting errors to the human user
 | 
			
		||||
 | 
			
		||||
Do not use printf(), fprintf() or monitor_printf().  Instead, use
 | 
			
		||||
error_report() or error_vreport() from error-report.h.  This ensures the
 | 
			
		||||
error is reported in the right place (current monitor or stderr), and in
 | 
			
		||||
a uniform format.
 | 
			
		||||
 | 
			
		||||
Use error_printf() & friends to print additional information.
 | 
			
		||||
 | 
			
		||||
error_report() prints the current location.  In certain common cases
 | 
			
		||||
like command line parsing, the current location is tracked
 | 
			
		||||
automatically.  To manipulate it manually, use the loc_*() from
 | 
			
		||||
error-report.h.
 | 
			
		||||
 | 
			
		||||
7.2 Propagating errors
 | 
			
		||||
 | 
			
		||||
An error can't always be reported to the user right where it's detected,
 | 
			
		||||
but often needs to be propagated up the call chain to a place that can
 | 
			
		||||
handle it.  This can be done in various ways.
 | 
			
		||||
 | 
			
		||||
The most flexible one is Error objects.  See error.h for usage
 | 
			
		||||
information.
 | 
			
		||||
 | 
			
		||||
Use the simplest suitable method to communicate success / failure to
 | 
			
		||||
callers.  Stick to common methods: non-negative on success / -1 on
 | 
			
		||||
error, non-negative / -errno, non-null / null, or Error objects.
 | 
			
		||||
 | 
			
		||||
Example: when a function returns a non-null pointer on success, and it
 | 
			
		||||
can fail only in one way (as far as the caller is concerned), returning
 | 
			
		||||
null on failure is just fine, and certainly simpler and a lot easier on
 | 
			
		||||
the eyes than propagating an Error object through an Error ** parameter.
 | 
			
		||||
 | 
			
		||||
Example: when a function's callers need to report details on failure
 | 
			
		||||
only the function really knows, use Error **, and set suitable errors.
 | 
			
		||||
 | 
			
		||||
Do not report an error to the user when you're also returning an error
 | 
			
		||||
for somebody else to handle.  Leave the reporting to the place that
 | 
			
		||||
consumes the error returned.
 | 
			
		||||
 | 
			
		||||
7.3 Handling errors
 | 
			
		||||
 | 
			
		||||
Calling exit() is fine when handling configuration errors during
 | 
			
		||||
startup.  It's problematic during normal operation.  In particular,
 | 
			
		||||
monitor commands should never exit().
 | 
			
		||||
 | 
			
		||||
Do not call exit() or abort() to handle an error that can be triggered
 | 
			
		||||
by the guest (e.g., some unimplemented corner case in guest code
 | 
			
		||||
translation or device emulation).  Guests should not be able to
 | 
			
		||||
terminate QEMU.
 | 
			
		||||
 | 
			
		||||
Note that &error_fatal is just another way to exit(1), and &error_abort
 | 
			
		||||
is just another way to abort().
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										15
									
								
								LICENSE
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								LICENSE
									
									
									
									
									
								
							@@ -1,21 +1,16 @@
 | 
			
		||||
The following points clarify the QEMU license:
 | 
			
		||||
 | 
			
		||||
1) QEMU as a whole is released under the GNU General Public License,
 | 
			
		||||
version 2.
 | 
			
		||||
1) QEMU as a whole is released under the GNU General Public License
 | 
			
		||||
 | 
			
		||||
2) Parts of QEMU have specific licenses which are compatible with the
 | 
			
		||||
GNU General Public License, version 2. Hence each source file contains
 | 
			
		||||
its own licensing information.  Source files with no licensing information
 | 
			
		||||
are released under the GNU General Public License, version 2 or (at your
 | 
			
		||||
option) any later version.
 | 
			
		||||
GNU General Public License. Hence each source file contains its own
 | 
			
		||||
licensing information.
 | 
			
		||||
 | 
			
		||||
As of July 2013, contributions under version 2 of the GNU General Public
 | 
			
		||||
License (and no later version) are only accepted for the following files
 | 
			
		||||
or directories: bsd-user/, linux-user/, hw/vfio/, hw/xen/xen_pt*.
 | 
			
		||||
Many hardware device emulation sources are released under the BSD license.
 | 
			
		||||
 | 
			
		||||
3) The Tiny Code Generator (TCG) is released under the BSD license
 | 
			
		||||
   (see license headers in files).
 | 
			
		||||
 | 
			
		||||
4) QEMU is a trademark of Fabrice Bellard.
 | 
			
		||||
 | 
			
		||||
Fabrice Bellard and the QEMU team
 | 
			
		||||
Fabrice Bellard.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1560
									
								
								MAINTAINERS
									
									
									
									
									
								
							
							
						
						
									
										1560
									
								
								MAINTAINERS
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										738
									
								
								Makefile
									
									
									
									
									
								
							
							
						
						
									
										738
									
								
								Makefile
									
									
									
									
									
								
							@@ -3,11 +3,6 @@
 | 
			
		||||
# Always point to the root of the build tree (needs GNU make).
 | 
			
		||||
BUILD_DIR=$(CURDIR)
 | 
			
		||||
 | 
			
		||||
# Before including a proper config-host.mak, assume we are in the source tree
 | 
			
		||||
SRC_PATH=.
 | 
			
		||||
 | 
			
		||||
UNCHECKED_GOALS := %clean TAGS cscope ctags docker docker-%
 | 
			
		||||
 | 
			
		||||
# All following code might depend on configuration variables
 | 
			
		||||
ifneq ($(wildcard config-host.mak),)
 | 
			
		||||
# Put the all: rule here so that config-host.mak can contain dependencies.
 | 
			
		||||
@@ -24,180 +19,35 @@ seems to have been used for an in-tree build. You can fix this by running \
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
CONFIG_SOFTMMU := $(if $(filter %-softmmu,$(TARGET_DIRS)),y)
 | 
			
		||||
CONFIG_USER_ONLY := $(if $(filter %-user,$(TARGET_DIRS)),y)
 | 
			
		||||
CONFIG_XEN := $(CONFIG_XEN_BACKEND)
 | 
			
		||||
CONFIG_ALL=y
 | 
			
		||||
-include config-all-devices.mak
 | 
			
		||||
-include config-all-disas.mak
 | 
			
		||||
 | 
			
		||||
config-host.mak: $(SRC_PATH)/configure $(SRC_PATH)/pc-bios
 | 
			
		||||
include $(SRC_PATH)/rules.mak
 | 
			
		||||
config-host.mak: $(SRC_PATH)/configure
 | 
			
		||||
	@echo $@ is out-of-date, running configure
 | 
			
		||||
	@# TODO: The next lines include code which supports a smooth
 | 
			
		||||
	@# transition from old configurations without config.status.
 | 
			
		||||
	@# This code can be removed after QEMU 1.7.
 | 
			
		||||
	@if test -x config.status; then \
 | 
			
		||||
	    ./config.status; \
 | 
			
		||||
        else \
 | 
			
		||||
	    sed -n "/.*Configured with/s/[^:]*: //p" $@ | sh; \
 | 
			
		||||
	fi
 | 
			
		||||
	@sed -n "/.*Configured with/s/[^:]*: //p" $@ | sh
 | 
			
		||||
else
 | 
			
		||||
config-host.mak:
 | 
			
		||||
ifneq ($(filter-out $(UNCHECKED_GOALS),$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
ifneq ($(filter-out %clean,$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
	@echo "Please call configure before running make!"
 | 
			
		||||
	@exit 1
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
include $(SRC_PATH)/rules.mak
 | 
			
		||||
GENERATED_HEADERS = config-host.h qemu-options.def
 | 
			
		||||
GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h
 | 
			
		||||
GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES = qemu-version.h config-host.h qemu-options.def
 | 
			
		||||
GENERATED_FILES += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
 | 
			
		||||
GENERATED_FILES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c
 | 
			
		||||
GENERATED_FILES += qmp-introspect.h
 | 
			
		||||
GENERATED_FILES += qmp-introspect.c
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES += trace/generated-tcg-tracers.h
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES += trace/generated-helpers-wrappers.h
 | 
			
		||||
GENERATED_FILES += trace/generated-helpers.h
 | 
			
		||||
GENERATED_FILES += trace/generated-helpers.c
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_TRACE_UST
 | 
			
		||||
GENERATED_FILES += trace-ust-all.h
 | 
			
		||||
GENERATED_FILES += trace-ust-all.c
 | 
			
		||||
GENERATED_HEADERS += trace/generated-tracers.h
 | 
			
		||||
ifeq ($(TRACE_BACKEND),dtrace)
 | 
			
		||||
GENERATED_HEADERS += trace/generated-tracers-dtrace.h
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES += module_block.h
 | 
			
		||||
 | 
			
		||||
TRACE_HEADERS = trace-root.h $(trace-events-subdirs:%=%/trace.h)
 | 
			
		||||
TRACE_SOURCES = trace-root.c $(trace-events-subdirs:%=%/trace.c)
 | 
			
		||||
TRACE_DTRACE =
 | 
			
		||||
ifdef CONFIG_TRACE_DTRACE
 | 
			
		||||
TRACE_HEADERS += trace-dtrace-root.h $(trace-events-subdirs:%=%/trace-dtrace.h)
 | 
			
		||||
TRACE_DTRACE += trace-dtrace-root.dtrace $(trace-events-subdirs:%=%/trace-dtrace.dtrace)
 | 
			
		||||
endif
 | 
			
		||||
ifdef CONFIG_TRACE_UST
 | 
			
		||||
TRACE_HEADERS += trace-ust-root.h $(trace-events-subdirs:%=%/trace-ust.h)
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES += $(TRACE_HEADERS)
 | 
			
		||||
GENERATED_FILES += $(TRACE_SOURCES)
 | 
			
		||||
GENERATED_FILES += $(BUILD_DIR)/trace-events-all
 | 
			
		||||
 | 
			
		||||
trace-group-name = $(shell dirname $1 | sed -e 's/[^a-zA-Z0-9]/_/g')
 | 
			
		||||
 | 
			
		||||
tracetool-y = $(SRC_PATH)/scripts/tracetool.py
 | 
			
		||||
tracetool-y += $(shell find $(SRC_PATH)/scripts/tracetool -name "*.py")
 | 
			
		||||
 | 
			
		||||
%/trace.h: %/trace.h-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
%/trace.h-timestamp: $(SRC_PATH)/%/trace-events $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=$(call trace-group-name,$@) \
 | 
			
		||||
		--format=h \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
%/trace.c: %/trace.c-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
%/trace.c-timestamp: $(SRC_PATH)/%/trace-events $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=$(call trace-group-name,$@) \
 | 
			
		||||
		--format=c \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
%/trace-ust.h: %/trace-ust.h-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
%/trace-ust.h-timestamp: $(SRC_PATH)/%/trace-events $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=$(call trace-group-name,$@) \
 | 
			
		||||
		--format=ust-events-h \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
%/trace-dtrace.dtrace: %/trace-dtrace.dtrace-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
%/trace-dtrace.dtrace-timestamp: $(SRC_PATH)/%/trace-events $(BUILD_DIR)/config-host.mak $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=$(call trace-group-name,$@) \
 | 
			
		||||
		--format=d \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
%/trace-dtrace.h: %/trace-dtrace.dtrace $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,dtrace -o $@ -h -s $<, "GEN","$@")
 | 
			
		||||
 | 
			
		||||
%/trace-dtrace.o: %/trace-dtrace.dtrace $(tracetool-y)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
trace-root.h: trace-root.h-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
trace-root.h-timestamp: $(SRC_PATH)/trace-events $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=root \
 | 
			
		||||
		--format=h \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
trace-root.c: trace-root.c-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
trace-root.c-timestamp: $(SRC_PATH)/trace-events $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=root \
 | 
			
		||||
		--format=c \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
trace-ust-root.h: trace-ust-root.h-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
trace-ust-root.h-timestamp: $(SRC_PATH)/trace-events $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=root \
 | 
			
		||||
		--format=ust-events-h \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
trace-ust-all.h: trace-ust-all.h-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
trace-ust-all.h-timestamp: $(trace-events-files) $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=all \
 | 
			
		||||
		--format=ust-events-h \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$(trace-events-files) > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
trace-ust-all.c: trace-ust-all.c-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
trace-ust-all.c-timestamp: $(trace-events-files) $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=all \
 | 
			
		||||
		--format=ust-events-c \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$(trace-events-files) > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
trace-dtrace-root.dtrace: trace-dtrace-root.dtrace-timestamp
 | 
			
		||||
	@cmp $< $@ >/dev/null 2>&1 || cp $< $@
 | 
			
		||||
trace-dtrace-root.dtrace-timestamp: $(SRC_PATH)/trace-events $(BUILD_DIR)/config-host.mak $(tracetool-y)
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=root \
 | 
			
		||||
		--format=d \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		$< > $@,"GEN","$(@:%-timestamp=%)")
 | 
			
		||||
 | 
			
		||||
trace-dtrace-root.h: trace-dtrace-root.dtrace
 | 
			
		||||
	$(call quiet-command,dtrace -o $@ -h -s $<, "GEN","$@")
 | 
			
		||||
 | 
			
		||||
trace-dtrace-root.o: trace-dtrace-root.dtrace
 | 
			
		||||
GENERATED_SOURCES += trace/generated-tracers.c
 | 
			
		||||
 | 
			
		||||
# Don't try to regenerate Makefile or configure
 | 
			
		||||
# We don't generate any of them
 | 
			
		||||
Makefile: ;
 | 
			
		||||
configure: ;
 | 
			
		||||
 | 
			
		||||
.PHONY: all clean cscope distclean html info install install-doc \
 | 
			
		||||
	pdf txt recurse-all speed test dist msi FORCE
 | 
			
		||||
.PHONY: all clean cscope distclean dvi html info install install-doc \
 | 
			
		||||
	pdf recurse-all speed test dist
 | 
			
		||||
 | 
			
		||||
$(call set-vpath, $(SRC_PATH))
 | 
			
		||||
 | 
			
		||||
@@ -206,9 +56,7 @@ LIBS+=-lz $(LIBS_TOOLS)
 | 
			
		||||
HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
 | 
			
		||||
 | 
			
		||||
ifdef BUILD_DOCS
 | 
			
		||||
DOCS=qemu-doc.html qemu-doc.txt qemu.1 qemu-img.1 qemu-nbd.8 qemu-ga.8
 | 
			
		||||
DOCS+=docs/interop/qemu-qmp-ref.html docs/interop/qemu-qmp-ref.txt docs/interop/qemu-qmp-ref.7
 | 
			
		||||
DOCS+=docs/interop/qemu-ga-ref.html docs/interop/qemu-ga-ref.txt docs/interop/qemu-ga-ref.7
 | 
			
		||||
DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 QMP/qmp-commands.txt
 | 
			
		||||
ifdef CONFIG_VIRTFS
 | 
			
		||||
DOCS+=fsdev/virtfs-proxy-helper.1
 | 
			
		||||
endif
 | 
			
		||||
@@ -218,25 +66,21 @@ endif
 | 
			
		||||
 | 
			
		||||
SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory) BUILD_DIR=$(BUILD_DIR)
 | 
			
		||||
SUBDIR_DEVICES_MAK=$(patsubst %, %/config-devices.mak, $(TARGET_DIRS))
 | 
			
		||||
SUBDIR_DEVICES_MAK_DEP=$(patsubst %, %-config-devices.mak.d, $(TARGET_DIRS))
 | 
			
		||||
SUBDIR_DEVICES_MAK_DEP=$(patsubst %, %/config-devices.mak.d, $(TARGET_DIRS))
 | 
			
		||||
 | 
			
		||||
ifeq ($(SUBDIR_DEVICES_MAK),)
 | 
			
		||||
config-all-devices.mak:
 | 
			
		||||
	$(call quiet-command,echo '# no devices' > $@,"GEN","$@")
 | 
			
		||||
	$(call quiet-command,echo '# no devices' > $@,"  GEN   $@")
 | 
			
		||||
else
 | 
			
		||||
config-all-devices.mak: $(SUBDIR_DEVICES_MAK)
 | 
			
		||||
	$(call quiet-command, sed -n \
 | 
			
		||||
             's|^\([^=]*\)=\(.*\)$$|\1:=$$(findstring y,$$(\1)\2)|p' \
 | 
			
		||||
             $(SUBDIR_DEVICES_MAK) | sort -u > $@, \
 | 
			
		||||
             "GEN","$@")
 | 
			
		||||
	$(call quiet-command,cat $(SUBDIR_DEVICES_MAK) | grep =y | sort -u > $@,"  GEN   $@")
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
-include $(SUBDIR_DEVICES_MAK_DEP)
 | 
			
		||||
 | 
			
		||||
%/config-devices.mak: default-configs/%.mak $(SRC_PATH)/scripts/make_device_config.sh
 | 
			
		||||
	$(call quiet-command, \
 | 
			
		||||
            $(SHELL) $(SRC_PATH)/scripts/make_device_config.sh $< $*-config-devices.mak.d $@ > $@.tmp,"GEN","$@.tmp")
 | 
			
		||||
	$(call quiet-command, if test -f $@; then \
 | 
			
		||||
%/config-devices.mak: default-configs/%.mak
 | 
			
		||||
	$(call quiet-command,$(SHELL) $(SRC_PATH)/scripts/make_device_config.sh $@ $<, "  GEN   $@")
 | 
			
		||||
	@if test -f $@; then \
 | 
			
		||||
	  if cmp -s $@.old $@; then \
 | 
			
		||||
	    mv $@.tmp $@; \
 | 
			
		||||
	    cp -p $@ $@.old; \
 | 
			
		||||
@@ -252,77 +96,33 @@ endif
 | 
			
		||||
	 else \
 | 
			
		||||
	  mv $@.tmp $@; \
 | 
			
		||||
	  cp -p $@ $@.old; \
 | 
			
		||||
	 fi,"GEN","$@");
 | 
			
		||||
	 fi
 | 
			
		||||
 | 
			
		||||
defconfig:
 | 
			
		||||
	rm -f config-all-devices.mak $(SUBDIR_DEVICES_MAK)
 | 
			
		||||
 | 
			
		||||
-include config-all-devices.mak
 | 
			
		||||
-include config-all-disas.mak
 | 
			
		||||
CONFIG_SOFTMMU := $(if $(filter %-softmmu,$(TARGET_DIRS)),y)
 | 
			
		||||
CONFIG_USER_ONLY := $(if $(filter %-user,$(TARGET_DIRS)),y)
 | 
			
		||||
CONFIG_ALL=y
 | 
			
		||||
 | 
			
		||||
ifneq ($(wildcard config-host.mak),)
 | 
			
		||||
include $(SRC_PATH)/Makefile.objs
 | 
			
		||||
include $(SRC_PATH)/tests/Makefile
 | 
			
		||||
endif
 | 
			
		||||
ifeq ($(CONFIG_SMARTCARD_NSS),y)
 | 
			
		||||
include $(SRC_PATH)/libcacard/Makefile
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
dummy := $(call unnest-vars,, \
 | 
			
		||||
                stub-obj-y \
 | 
			
		||||
                chardev-obj-y \
 | 
			
		||||
                util-obj-y \
 | 
			
		||||
                qga-obj-y \
 | 
			
		||||
                ivshmem-client-obj-y \
 | 
			
		||||
                ivshmem-server-obj-y \
 | 
			
		||||
                libvhost-user-obj-y \
 | 
			
		||||
                vhost-user-scsi-obj-y \
 | 
			
		||||
                qga-vss-dll-obj-y \
 | 
			
		||||
                block-obj-y \
 | 
			
		||||
                block-obj-m \
 | 
			
		||||
                crypto-obj-y \
 | 
			
		||||
                crypto-aes-obj-y \
 | 
			
		||||
                qom-obj-y \
 | 
			
		||||
                io-obj-y \
 | 
			
		||||
                common-obj-y \
 | 
			
		||||
                common-obj-m \
 | 
			
		||||
                trace-obj-y)
 | 
			
		||||
 | 
			
		||||
ifneq ($(wildcard config-host.mak),)
 | 
			
		||||
include $(SRC_PATH)/tests/Makefile.include
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
 | 
			
		||||
 | 
			
		||||
qemu-version.h: FORCE
 | 
			
		||||
	$(call quiet-command, \
 | 
			
		||||
		(cd $(SRC_PATH); \
 | 
			
		||||
		printf '#define QEMU_PKGVERSION '; \
 | 
			
		||||
		if test -n "$(PKGVERSION)"; then \
 | 
			
		||||
			printf '"$(PKGVERSION)"\n'; \
 | 
			
		||||
		else \
 | 
			
		||||
			if test -d .git; then \
 | 
			
		||||
				printf '" ('; \
 | 
			
		||||
				git describe --match 'v*' 2>/dev/null | tr -d '\n'; \
 | 
			
		||||
				if ! git diff-index --quiet HEAD &>/dev/null; then \
 | 
			
		||||
					printf -- '-dirty'; \
 | 
			
		||||
				fi; \
 | 
			
		||||
				printf ')"\n'; \
 | 
			
		||||
			else \
 | 
			
		||||
				printf '""\n'; \
 | 
			
		||||
			fi; \
 | 
			
		||||
		fi) > $@.tmp)
 | 
			
		||||
	$(call quiet-command, if ! cmp -s $@ $@.tmp; then \
 | 
			
		||||
	  mv $@.tmp $@; \
 | 
			
		||||
	 else \
 | 
			
		||||
	  rm $@.tmp; \
 | 
			
		||||
	 fi)
 | 
			
		||||
all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all
 | 
			
		||||
 | 
			
		||||
config-host.h: config-host.h-timestamp
 | 
			
		||||
config-host.h-timestamp: config-host.mak
 | 
			
		||||
qemu-options.def: $(SRC_PATH)/qemu-options.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$@")
 | 
			
		||||
qemu-options.def: $(SRC_PATH)/qemu-options.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
 | 
			
		||||
SOFTMMU_SUBDIR_RULES=$(filter %-softmmu,$(SUBDIR_RULES))
 | 
			
		||||
 | 
			
		||||
$(SOFTMMU_SUBDIR_RULES): $(block-obj-y)
 | 
			
		||||
$(SOFTMMU_SUBDIR_RULES): $(crypto-obj-y)
 | 
			
		||||
$(SOFTMMU_SUBDIR_RULES): $(io-obj-y)
 | 
			
		||||
$(SOFTMMU_SUBDIR_RULES): config-all-devices.mak
 | 
			
		||||
 | 
			
		||||
subdir-%:
 | 
			
		||||
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)
 | 
			
		||||
@@ -336,56 +136,45 @@ pixman/Makefile: $(SRC_PATH)/pixman/configure
 | 
			
		||||
$(SRC_PATH)/pixman/configure:
 | 
			
		||||
	(cd $(SRC_PATH)/pixman; autoreconf -v --install)
 | 
			
		||||
 | 
			
		||||
DTC_MAKE_ARGS=-I$(SRC_PATH)/dtc VPATH=$(SRC_PATH)/dtc -C dtc V="$(V)" LIBFDT_srcdir=$(SRC_PATH)/dtc/libfdt
 | 
			
		||||
DTC_CFLAGS=$(CFLAGS) $(QEMU_CFLAGS)
 | 
			
		||||
DTC_CPPFLAGS=-I$(BUILD_DIR)/dtc -I$(SRC_PATH)/dtc -I$(SRC_PATH)/dtc/libfdt
 | 
			
		||||
 | 
			
		||||
subdir-dtc:dtc/libfdt dtc/tests
 | 
			
		||||
	$(call quiet-command,$(MAKE) $(DTC_MAKE_ARGS) CPPFLAGS="$(DTC_CPPFLAGS)" CFLAGS="$(DTC_CFLAGS)" LDFLAGS="$(LDFLAGS)" ARFLAGS="$(ARFLAGS)" CC="$(CC)" AR="$(AR)" LD="$(LD)" $(SUBDIR_MAKEFLAGS) libfdt/libfdt.a,)
 | 
			
		||||
 | 
			
		||||
dtc/%:
 | 
			
		||||
	mkdir -p $@
 | 
			
		||||
 | 
			
		||||
$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y) $(chardev-obj-y) \
 | 
			
		||||
	$(qom-obj-y) $(crypto-aes-obj-$(CONFIG_USER_ONLY))
 | 
			
		||||
$(SUBDIR_RULES): libqemuutil.a libqemustub.a $(common-obj-y)
 | 
			
		||||
 | 
			
		||||
ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
 | 
			
		||||
# Only keep -O and -g cflags
 | 
			
		||||
romsubdir-%:
 | 
			
		||||
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C pc-bios/$* V="$(V)" TARGET_DIR="$*/" CFLAGS="$(filter -O% -g%,$(CFLAGS))",)
 | 
			
		||||
	$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C pc-bios/$* V="$(V)" TARGET_DIR="$*/",)
 | 
			
		||||
 | 
			
		||||
ALL_SUBDIRS=$(TARGET_DIRS) $(patsubst %,pc-bios/%, $(ROMS))
 | 
			
		||||
 | 
			
		||||
recurse-all: $(SUBDIR_RULES) $(ROMSUBDIR_RULES)
 | 
			
		||||
 | 
			
		||||
$(BUILD_DIR)/version.o: $(SRC_PATH)/version.rc config-host.h
 | 
			
		||||
	$(call quiet-command,$(WINDRES) -I$(BUILD_DIR) -o $@ $<,"RC","version.o")
 | 
			
		||||
bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
 | 
			
		||||
 | 
			
		||||
version.o: $(SRC_PATH)/version.rc config-host.h
 | 
			
		||||
	$(call quiet-command,$(WINDRES) -I. -o $@ $<,"  RC    $(TARGET_DIR)$@")
 | 
			
		||||
 | 
			
		||||
version-obj-$(CONFIG_WIN32) += version.o
 | 
			
		||||
Makefile: $(version-obj-y)
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# Build libraries
 | 
			
		||||
 | 
			
		||||
libqemustub.a: $(stub-obj-y)
 | 
			
		||||
libqemuutil.a: $(util-obj-y) $(trace-obj-y)
 | 
			
		||||
libqemuutil.a: $(util-obj-y)
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
 | 
			
		||||
COMMON_LDADDS = libqemuutil.a libqemustub.a
 | 
			
		||||
 | 
			
		||||
qemu-img.o: qemu-img-cmds.h
 | 
			
		||||
 | 
			
		||||
qemu-img$(EXESUF): qemu-img.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
qemu-io$(EXESUF): qemu-io.o $(block-obj-y) $(crypto-obj-y) $(io-obj-y) $(qom-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
qemu-img$(EXESUF): qemu-img.o $(block-obj-y) libqemuutil.a libqemustub.a
 | 
			
		||||
qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) libqemuutil.a libqemustub.a
 | 
			
		||||
qemu-io$(EXESUF): qemu-io.o cmd.o $(block-obj-y) libqemuutil.a libqemustub.a
 | 
			
		||||
 | 
			
		||||
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o $(COMMON_LDADDS)
 | 
			
		||||
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
 | 
			
		||||
 | 
			
		||||
fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/9p-marshal.o fsdev/9p-iov-marshal.o $(COMMON_LDADDS)
 | 
			
		||||
fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/virtio-9p-marshal.o libqemuutil.a libqemustub.a
 | 
			
		||||
fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
 | 
			
		||||
 | 
			
		||||
qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$@")
 | 
			
		||||
qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qemu-ga$(EXESUF): LIBS = $(LIBS_QGA)
 | 
			
		||||
qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated
 | 
			
		||||
@@ -396,113 +185,51 @@ qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
 | 
			
		||||
 | 
			
		||||
qga/qapi-generated/qga-qapi-types.c qga/qapi-generated/qga-qapi-types.h :\
 | 
			
		||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o qga/qapi-generated -p "qga-" < $<, "  GEN   $@")
 | 
			
		||||
qga/qapi-generated/qga-qapi-visit.c qga/qapi-generated/qga-qapi-visit.h :\
 | 
			
		||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o qga/qapi-generated -p "qga-" < $<, "  GEN   $@")
 | 
			
		||||
qga/qapi-generated/qga-qmp-commands.h qga/qapi-generated/qga-qmp-marshal.c :\
 | 
			
		||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
 | 
			
		||||
		$(gen-out-type) -o qga/qapi-generated -p "qga-" $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
 | 
			
		||||
qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
 | 
			
		||||
               $(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
 | 
			
		||||
               $(SRC_PATH)/qapi/event.json $(SRC_PATH)/qapi/introspect.json \
 | 
			
		||||
               $(SRC_PATH)/qapi/crypto.json $(SRC_PATH)/qapi/rocker.json \
 | 
			
		||||
               $(SRC_PATH)/qapi/trace.json
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -o qga/qapi-generated -p "qga-" < $<, "  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qapi-types.c qapi-types.h :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
 | 
			
		||||
		$(gen-out-type) -o "." -b $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py $(gen-out-type) -o "." < $<, "  GEN   $@")
 | 
			
		||||
qapi-visit.c qapi-visit.h :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
 | 
			
		||||
		$(gen-out-type) -o "." -b $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
qapi-event.c qapi-event.h :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-event.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-event.py \
 | 
			
		||||
		$(gen-out-type) -o "." $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py $(gen-out-type) -o "."  < $<, "  GEN   $@")
 | 
			
		||||
qmp-commands.h qmp-marshal.c :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py \
 | 
			
		||||
		$(gen-out-type) -o "." $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
qmp-introspect.h qmp-introspect.c :\
 | 
			
		||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-introspect.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-introspect.py \
 | 
			
		||||
		$(gen-out-type) -o "." $<, \
 | 
			
		||||
		"GEN","$@")
 | 
			
		||||
$(SRC_PATH)/qapi-schema.json $(SRC_PATH)/scripts/qapi-commands.py $(qapi-py)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py $(gen-out-type) -m -o "." < $<, "  GEN   $@")
 | 
			
		||||
 | 
			
		||||
QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
 | 
			
		||||
$(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
 | 
			
		||||
 | 
			
		||||
qemu-ga$(EXESUF): $(qga-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a
 | 
			
		||||
	$(call LINK, $^)
 | 
			
		||||
 | 
			
		||||
ifdef QEMU_GA_MSI_ENABLED
 | 
			
		||||
QEMU_GA_MSI=qemu-ga-$(ARCH).msi
 | 
			
		||||
 | 
			
		||||
msi: $(QEMU_GA_MSI)
 | 
			
		||||
 | 
			
		||||
$(QEMU_GA_MSI): qemu-ga.exe $(QGA_VSS_PROVIDER)
 | 
			
		||||
 | 
			
		||||
$(QEMU_GA_MSI): config-host.mak
 | 
			
		||||
 | 
			
		||||
$(QEMU_GA_MSI):  $(SRC_PATH)/qga/installer/qemu-ga.wxs
 | 
			
		||||
	$(call quiet-command,QEMU_GA_VERSION="$(QEMU_GA_VERSION)" QEMU_GA_MANUFACTURER="$(QEMU_GA_MANUFACTURER)" QEMU_GA_DISTRO="$(QEMU_GA_DISTRO)" BUILD_DIR="$(BUILD_DIR)" \
 | 
			
		||||
	wixl -o $@ $(QEMU_GA_MSI_ARCH) $(QEMU_GA_MSI_WITH_VSS) $(QEMU_GA_MSI_MINGW_DLL_PATH) $<,"WIXL","$@")
 | 
			
		||||
else
 | 
			
		||||
msi:
 | 
			
		||||
	@echo "MSI build not configured or dependency resolution failed (reconfigure with --enable-guest-agent-msi option)"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
ifneq ($(EXESUF),)
 | 
			
		||||
.PHONY: qemu-ga
 | 
			
		||||
qemu-ga: qemu-ga$(EXESUF) $(QGA_VSS_PROVIDER) $(QEMU_GA_MSI)
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
ivshmem-client$(EXESUF): $(ivshmem-client-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
	$(call LINK, $^)
 | 
			
		||||
ivshmem-server$(EXESUF): $(ivshmem-server-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
	$(call LINK, $^)
 | 
			
		||||
vhost-user-scsi$(EXESUF): $(vhost-user-scsi-obj-y)
 | 
			
		||||
	$(call LINK, $^)
 | 
			
		||||
 | 
			
		||||
module_block.h: $(SRC_PATH)/scripts/modules/module_block.py config-host.mak
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $< $@ \
 | 
			
		||||
	$(addprefix $(SRC_PATH)/,$(patsubst %.mo,%.c,$(block-obj-m))), \
 | 
			
		||||
	"GEN","$@")
 | 
			
		||||
 | 
			
		||||
clean:
 | 
			
		||||
# avoid old build problems by removing potentially incorrect old files
 | 
			
		||||
	rm -f config.mak op-i386.h opc-i386.h gen-op-i386.h op-arm.h opc-arm.h gen-op-arm.h
 | 
			
		||||
	rm -f qemu-options.def
 | 
			
		||||
	rm -f *.msi
 | 
			
		||||
	find . \( -name '*.so' -o -name '*.dll' -o -name '*.mo' -o -name '*.[oda]' \) -type f -exec rm {} +
 | 
			
		||||
	rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 | 
			
		||||
	rm -f fsdev/*.pod
 | 
			
		||||
	find . -name '*.[oda]' -type f -exec rm -f {} +
 | 
			
		||||
	find . -name '*.l[oa]' -type f -exec rm -f {} +
 | 
			
		||||
	rm -f $(TOOLS) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
 | 
			
		||||
	rm -Rf .libs
 | 
			
		||||
	rm -f qemu-img-cmds.h
 | 
			
		||||
	rm -f ui/shader/*-vert.h ui/shader/*-frag.h
 | 
			
		||||
	@# May not be present in GENERATED_FILES
 | 
			
		||||
	@# May not be present in GENERATED_HEADERS
 | 
			
		||||
	rm -f trace/generated-tracers-dtrace.dtrace*
 | 
			
		||||
	rm -f trace/generated-tracers-dtrace.h*
 | 
			
		||||
	rm -f $(foreach f,$(GENERATED_FILES),$(f) $(f)-timestamp)
 | 
			
		||||
	rm -f $(foreach f,$(GENERATED_HEADERS),$(f) $(f)-timestamp)
 | 
			
		||||
	rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
 | 
			
		||||
	rm -rf qapi-generated
 | 
			
		||||
	rm -rf qga/qapi-generated
 | 
			
		||||
	$(MAKE) -C tests/tcg clean
 | 
			
		||||
	for d in $(ALL_SUBDIRS); do \
 | 
			
		||||
	if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
 | 
			
		||||
	rm -f $$d/qemu-options.def; \
 | 
			
		||||
        done
 | 
			
		||||
	rm -f $(SUBDIR_DEVICES_MAK) config-all-devices.mak
 | 
			
		||||
 | 
			
		||||
VERSION ?= $(shell cat VERSION)
 | 
			
		||||
 | 
			
		||||
@@ -512,79 +239,53 @@ qemu-%.tar.bz2:
 | 
			
		||||
	$(SRC_PATH)/scripts/make-release "$(SRC_PATH)" "$(patsubst qemu-%.tar.bz2,%,$@)"
 | 
			
		||||
 | 
			
		||||
distclean: clean
 | 
			
		||||
	rm -f config-host.mak config-host.h* config-host.ld $(DOCS) qemu-options.texi qemu-img-cmds.texi qemu-monitor.texi qemu-monitor-info.texi
 | 
			
		||||
	rm -f config-all-devices.mak config-all-disas.mak config.status
 | 
			
		||||
	rm -f po/*.mo tests/qemu-iotests/common.env
 | 
			
		||||
	rm -f config-host.mak config-host.h* config-host.ld $(DOCS) qemu-options.texi qemu-img-cmds.texi qemu-monitor.texi
 | 
			
		||||
	rm -f config-all-devices.mak config-all-disas.mak
 | 
			
		||||
	rm -f roms/seabios/config.mak roms/vgabios/config.mak
 | 
			
		||||
	rm -f qemu-doc.info qemu-doc.aux qemu-doc.cp qemu-doc.cps
 | 
			
		||||
	rm -f qemu-doc.info qemu-doc.aux qemu-doc.cp qemu-doc.cps qemu-doc.dvi
 | 
			
		||||
	rm -f qemu-doc.fn qemu-doc.fns qemu-doc.info qemu-doc.ky qemu-doc.kys
 | 
			
		||||
	rm -f qemu-doc.log qemu-doc.pdf qemu-doc.pg qemu-doc.toc qemu-doc.tp
 | 
			
		||||
	rm -f qemu-doc.vr qemu-doc.txt
 | 
			
		||||
	rm -f qemu-doc.vr
 | 
			
		||||
	rm -f config.log
 | 
			
		||||
	rm -f linux-headers/asm
 | 
			
		||||
	rm -f docs/version.texi
 | 
			
		||||
	rm -f docs/interop/qemu-ga-qapi.texi docs/interop/qemu-qmp-qapi.texi
 | 
			
		||||
	rm -f docs/interop/qemu-qmp-ref.7 docs/interop/qemu-ga-ref.7
 | 
			
		||||
	rm -f docs/interop/qemu-qmp-ref.txt docs/interop/qemu-ga-ref.txt
 | 
			
		||||
	rm -f docs/interop/qemu-qmp-ref.pdf docs/interop/qemu-ga-ref.pdf
 | 
			
		||||
	rm -f docs/interop/qemu-qmp-ref.html docs/interop/qemu-ga-ref.html
 | 
			
		||||
	rm -f qemu-tech.info qemu-tech.aux qemu-tech.cp qemu-tech.dvi qemu-tech.fn qemu-tech.info qemu-tech.ky qemu-tech.log qemu-tech.pdf qemu-tech.pg qemu-tech.toc qemu-tech.tp qemu-tech.vr
 | 
			
		||||
	for d in $(TARGET_DIRS); do \
 | 
			
		||||
	rm -rf $$d || exit 1 ; \
 | 
			
		||||
        done
 | 
			
		||||
	rm -Rf .sdk
 | 
			
		||||
	if test -f pixman/config.log; then $(MAKE) -C pixman distclean; fi
 | 
			
		||||
	if test -f dtc/version_gen.h; then $(MAKE) $(DTC_MAKE_ARGS) clean; fi
 | 
			
		||||
	if test -f pixman/config.log; then make -C pixman distclean; fi
 | 
			
		||||
 | 
			
		||||
KEYMAPS=da     en-gb  et  fr     fr-ch  is  lt  modifiers  no  pt-br  sv \
 | 
			
		||||
ar      de     en-us  fi  fr-be  hr     it  lv  nl         pl  ru     th \
 | 
			
		||||
common  de-ch  es     fo  fr-ca  hu     ja  mk  nl-be      pt  sl     tr \
 | 
			
		||||
bepo    cz
 | 
			
		||||
bepo
 | 
			
		||||
 | 
			
		||||
ifdef INSTALL_BLOBS
 | 
			
		||||
BLOBS=bios.bin bios-256k.bin sgabios.bin vgabios.bin vgabios-cirrus.bin \
 | 
			
		||||
vgabios-stdvga.bin vgabios-vmware.bin vgabios-qxl.bin vgabios-virtio.bin \
 | 
			
		||||
acpi-dsdt.aml \
 | 
			
		||||
ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc QEMU,tcx.bin QEMU,cgthree.bin \
 | 
			
		||||
BLOBS=bios.bin sgabios.bin vgabios.bin vgabios-cirrus.bin \
 | 
			
		||||
vgabios-stdvga.bin vgabios-vmware.bin vgabios-qxl.bin \
 | 
			
		||||
acpi-dsdt.aml q35-acpi-dsdt.aml \
 | 
			
		||||
ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc \
 | 
			
		||||
pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom \
 | 
			
		||||
pxe-pcnet.rom pxe-rtl8139.rom pxe-virtio.rom \
 | 
			
		||||
efi-e1000.rom efi-eepro100.rom efi-ne2k_pci.rom \
 | 
			
		||||
efi-pcnet.rom efi-rtl8139.rom efi-virtio.rom \
 | 
			
		||||
efi-e1000e.rom efi-vmxnet3.rom \
 | 
			
		||||
qemu-icon.bmp qemu_logo_no_text.svg \
 | 
			
		||||
qemu-icon.bmp \
 | 
			
		||||
bamboo.dtb petalogix-s3adsp1800.dtb petalogix-ml605.dtb \
 | 
			
		||||
multiboot.bin linuxboot.bin linuxboot_dma.bin kvmvapic.bin \
 | 
			
		||||
s390-ccw.img \
 | 
			
		||||
spapr-rtas.bin slof.bin skiboot.lid \
 | 
			
		||||
palcode-clipper \
 | 
			
		||||
u-boot.e500 \
 | 
			
		||||
qemu_vga.ndrv
 | 
			
		||||
multiboot.bin linuxboot.bin kvmvapic.bin \
 | 
			
		||||
s390-zipl.rom \
 | 
			
		||||
spapr-rtas.bin slof.bin \
 | 
			
		||||
palcode-clipper
 | 
			
		||||
else
 | 
			
		||||
BLOBS=
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
install-doc: $(DOCS)
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) qemu-doc.html "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) qemu-doc.txt "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) docs/interop/qemu-qmp-ref.html "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) docs/interop/qemu-qmp-ref.txt "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) qemu-doc.html  qemu-tech.html "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) QMP/qmp-commands.txt "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
ifdef CONFIG_POSIX
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
 | 
			
		||||
	$(INSTALL_DATA) qemu.1 "$(DESTDIR)$(mandir)/man1"
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man7"
 | 
			
		||||
	$(INSTALL_DATA) docs/interop/qemu-qmp-ref.7 "$(DESTDIR)$(mandir)/man7"
 | 
			
		||||
ifneq ($(TOOLS),)
 | 
			
		||||
	$(INSTALL_DATA) qemu-img.1 "$(DESTDIR)$(mandir)/man1"
 | 
			
		||||
	$(INSTALL_DATA) qemu.1 qemu-img.1 "$(DESTDIR)$(mandir)/man1"
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man8"
 | 
			
		||||
	$(INSTALL_DATA) qemu-nbd.8 "$(DESTDIR)$(mandir)/man8"
 | 
			
		||||
endif
 | 
			
		||||
ifneq (,$(findstring qemu-ga,$(TOOLS)))
 | 
			
		||||
	$(INSTALL_DATA) qemu-ga.8 "$(DESTDIR)$(mandir)/man8"
 | 
			
		||||
	$(INSTALL_DATA) docs/interop/qemu-ga-ref.html "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) docs/interop/qemu-ga-ref.txt "$(DESTDIR)$(qemu_docdir)"
 | 
			
		||||
	$(INSTALL_DATA) docs/interop/qemu-ga-ref.7 "$(DESTDIR)$(mandir)/man7"
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
ifdef CONFIG_VIRTFS
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
 | 
			
		||||
	$(INSTALL_DATA) fsdev/virtfs-proxy-helper.1 "$(DESTDIR)$(mandir)/man1"
 | 
			
		||||
@@ -593,260 +294,115 @@ endif
 | 
			
		||||
install-datadir:
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)"
 | 
			
		||||
 | 
			
		||||
install-localstatedir:
 | 
			
		||||
ifdef CONFIG_POSIX
 | 
			
		||||
ifneq (,$(findstring qemu-ga,$(TOOLS)))
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_localstatedir)"/run
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
install-confdir:
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_confdir)"
 | 
			
		||||
 | 
			
		||||
install-sysconfig: install-datadir install-confdir
 | 
			
		||||
	$(INSTALL_DATA) $(SRC_PATH)/sysconfigs/target/target-x86_64.conf "$(DESTDIR)$(qemu_confdir)"
 | 
			
		||||
 | 
			
		||||
install: all $(if $(BUILD_DOCS),install-doc) install-datadir install-localstatedir
 | 
			
		||||
install: all $(if $(BUILD_DOCS),install-doc) install-sysconfig install-datadir
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(bindir)"
 | 
			
		||||
ifneq ($(TOOLS),)
 | 
			
		||||
	$(call install-prog,$(subst qemu-ga,qemu-ga$(EXESUF),$(TOOLS)),$(DESTDIR)$(bindir))
 | 
			
		||||
endif
 | 
			
		||||
ifneq ($(CONFIG_MODULES),)
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_moddir)"
 | 
			
		||||
	for s in $(modules-m:.mo=$(DSOSUF)); do \
 | 
			
		||||
		t="$(DESTDIR)$(qemu_moddir)/$$(echo $$s | tr / -)"; \
 | 
			
		||||
		$(INSTALL_LIB) $$s "$$t"; \
 | 
			
		||||
		test -z "$(STRIP)" || $(STRIP) "$$t"; \
 | 
			
		||||
	done
 | 
			
		||||
	$(INSTALL_PROG) $(STRIP_OPT) $(TOOLS) "$(DESTDIR)$(bindir)"
 | 
			
		||||
endif
 | 
			
		||||
ifneq ($(HELPERS-y),)
 | 
			
		||||
	$(call install-prog,$(HELPERS-y),$(DESTDIR)$(libexecdir))
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(libexecdir)"
 | 
			
		||||
	$(INSTALL_PROG) $(STRIP_OPT) $(HELPERS-y) "$(DESTDIR)$(libexecdir)"
 | 
			
		||||
endif
 | 
			
		||||
ifneq ($(BLOBS),)
 | 
			
		||||
	set -e; for x in $(BLOBS); do \
 | 
			
		||||
		$(INSTALL_DATA) $(SRC_PATH)/pc-bios/$$x "$(DESTDIR)$(qemu_datadir)"; \
 | 
			
		||||
	done
 | 
			
		||||
endif
 | 
			
		||||
ifeq ($(CONFIG_GTK),y)
 | 
			
		||||
	$(MAKE) -C po $@
 | 
			
		||||
endif
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/keymaps"
 | 
			
		||||
	set -e; for x in $(KEYMAPS); do \
 | 
			
		||||
		$(INSTALL_DATA) $(SRC_PATH)/pc-bios/keymaps/$$x "$(DESTDIR)$(qemu_datadir)/keymaps"; \
 | 
			
		||||
	done
 | 
			
		||||
	$(INSTALL_DATA) $(BUILD_DIR)/trace-events-all "$(DESTDIR)$(qemu_datadir)/trace-events-all"
 | 
			
		||||
	for d in $(TARGET_DIRS); do \
 | 
			
		||||
	$(MAKE) $(SUBDIR_MAKEFLAGS) TARGET_DIR=$$d/ -C $$d $@ || exit 1 ; \
 | 
			
		||||
	$(MAKE) -C $$d $@ || exit 1 ; \
 | 
			
		||||
        done
 | 
			
		||||
 | 
			
		||||
# various test targets
 | 
			
		||||
test speed: all
 | 
			
		||||
	$(MAKE) -C tests/tcg $@
 | 
			
		||||
 | 
			
		||||
.PHONY: ctags
 | 
			
		||||
ctags:
 | 
			
		||||
	rm -f tags
 | 
			
		||||
	find "$(SRC_PATH)" -name '*.[hc]' -exec ctags --append {} +
 | 
			
		||||
 | 
			
		||||
.PHONY: TAGS
 | 
			
		||||
TAGS:
 | 
			
		||||
	rm -f TAGS
 | 
			
		||||
	find "$(SRC_PATH)" -name '*.[hc]' -exec etags --append {} +
 | 
			
		||||
	find "$(SRC_PATH)" -name '*.[hc]' -print0 | xargs -0 etags
 | 
			
		||||
 | 
			
		||||
cscope:
 | 
			
		||||
	rm -f "$(SRC_PATH)"/cscope.*
 | 
			
		||||
	find "$(SRC_PATH)/" -name "*.[chsS]" -print | sed 's,^\./,,' > "$(SRC_PATH)/cscope.files"
 | 
			
		||||
	cscope -b -i"$(SRC_PATH)/cscope.files"
 | 
			
		||||
 | 
			
		||||
# opengl shader programs
 | 
			
		||||
ui/shader/%-vert.h: $(SRC_PATH)/ui/shader/%.vert $(SRC_PATH)/scripts/shaderinclude.pl
 | 
			
		||||
	@mkdir -p $(dir $@)
 | 
			
		||||
	$(call quiet-command,\
 | 
			
		||||
		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
 | 
			
		||||
		"VERT","$@")
 | 
			
		||||
 | 
			
		||||
ui/shader/%-frag.h: $(SRC_PATH)/ui/shader/%.frag $(SRC_PATH)/scripts/shaderinclude.pl
 | 
			
		||||
	@mkdir -p $(dir $@)
 | 
			
		||||
	$(call quiet-command,\
 | 
			
		||||
		perl $(SRC_PATH)/scripts/shaderinclude.pl $< > $@,\
 | 
			
		||||
		"FRAG","$@")
 | 
			
		||||
 | 
			
		||||
ui/console-gl.o: $(SRC_PATH)/ui/console-gl.c \
 | 
			
		||||
	ui/shader/texture-blit-vert.h ui/shader/texture-blit-frag.h
 | 
			
		||||
	rm -f ./cscope.*
 | 
			
		||||
	find "$(SRC_PATH)" -name "*.[chsS]" -print | sed 's,^\./,,' > ./cscope.files
 | 
			
		||||
	cscope -b
 | 
			
		||||
 | 
			
		||||
# documentation
 | 
			
		||||
MAKEINFO=makeinfo
 | 
			
		||||
MAKEINFOINCLUDES= -I docs -I $(<D) -I $(@D)
 | 
			
		||||
MAKEINFOFLAGS=--no-split --number-sections $(MAKEINFOINCLUDES)
 | 
			
		||||
TEXI2PODFLAGS=$(MAKEINFOINCLUDES) "-DVERSION=$(VERSION)"
 | 
			
		||||
TEXI2PDFFLAGS=$(if $(V),,--quiet) -I $(SRC_PATH) $(MAKEINFOINCLUDES)
 | 
			
		||||
MAKEINFOFLAGS=--no-headers --no-split --number-sections
 | 
			
		||||
TEXIFLAG=$(if $(V),,--quiet)
 | 
			
		||||
%.dvi: %.texi
 | 
			
		||||
	$(call quiet-command,texi2dvi $(TEXIFLAG) -I . $<,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
docs/version.texi: $(SRC_PATH)/VERSION
 | 
			
		||||
	$(call quiet-command,echo "@set VERSION $(VERSION)" > $@,"GEN","$@")
 | 
			
		||||
%.html: %.texi
 | 
			
		||||
	$(call quiet-command,LC_ALL=C $(MAKEINFO) $(MAKEINFOFLAGS) --html $< -o $@, \
 | 
			
		||||
	"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
%.html: %.texi docs/version.texi
 | 
			
		||||
	$(call quiet-command,LC_ALL=C $(MAKEINFO) $(MAKEINFOFLAGS) --no-headers \
 | 
			
		||||
	--html $< -o $@,"GEN","$@")
 | 
			
		||||
%.info: %.texi
 | 
			
		||||
	$(call quiet-command,$(MAKEINFO) $< -o $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
%.info: %.texi docs/version.texi
 | 
			
		||||
	$(call quiet-command,$(MAKEINFO) $(MAKEINFOFLAGS) $< -o $@,"GEN","$@")
 | 
			
		||||
%.pdf: %.texi
 | 
			
		||||
	$(call quiet-command,texi2pdf $(TEXIFLAG) -I . $<,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
%.txt: %.texi docs/version.texi
 | 
			
		||||
	$(call quiet-command,LC_ALL=C $(MAKEINFO) $(MAKEINFOFLAGS) --no-headers \
 | 
			
		||||
	--plaintext $< -o $@,"GEN","$@")
 | 
			
		||||
qemu-options.texi: $(SRC_PATH)/qemu-options.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
%.pdf: %.texi docs/version.texi
 | 
			
		||||
	$(call quiet-command,texi2pdf $(TEXI2PDFFLAGS) $< -o $@,"GEN","$@")
 | 
			
		||||
qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qemu-options.texi: $(SRC_PATH)/qemu-options.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"GEN","$@")
 | 
			
		||||
QMP/qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"GEN","$@")
 | 
			
		||||
qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qemu-monitor-info.texi: $(SRC_PATH)/hmp-commands-info.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"GEN","$@")
 | 
			
		||||
qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi
 | 
			
		||||
	$(call quiet-command, \
 | 
			
		||||
	  perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu.pod && \
 | 
			
		||||
	  $(POD2MAN) --section=1 --center=" " --release=" " qemu.pod > $@, \
 | 
			
		||||
	  "  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@,"GEN","$@")
 | 
			
		||||
qemu-img.1: qemu-img.texi qemu-img-cmds.texi
 | 
			
		||||
	$(call quiet-command, \
 | 
			
		||||
	  perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-img.pod && \
 | 
			
		||||
	  $(POD2MAN) --section=1 --center=" " --release=" " qemu-img.pod > $@, \
 | 
			
		||||
	  "  GEN   $@")
 | 
			
		||||
 | 
			
		||||
docs/interop/qemu-qmp-qapi.texi docs/interop/qemu-ga-qapi.texi: $(SRC_PATH)/scripts/qapi2texi.py $(qapi-py)
 | 
			
		||||
 | 
			
		||||
docs/interop/qemu-qmp-qapi.texi: $(qapi-modules)
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi2texi.py $< > $@,"GEN","$@")
 | 
			
		||||
 | 
			
		||||
docs/interop/qemu-ga-qapi.texi: $(SRC_PATH)/qga/qapi-schema.json
 | 
			
		||||
	$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi2texi.py $< > $@,"GEN","$@")
 | 
			
		||||
 | 
			
		||||
qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi qemu-monitor-info.texi
 | 
			
		||||
qemu.1: qemu-option-trace.texi
 | 
			
		||||
qemu-img.1: qemu-img.texi qemu-option-trace.texi qemu-img-cmds.texi
 | 
			
		||||
fsdev/virtfs-proxy-helper.1: fsdev/virtfs-proxy-helper.texi
 | 
			
		||||
qemu-nbd.8: qemu-nbd.texi qemu-option-trace.texi
 | 
			
		||||
qemu-ga.8: qemu-ga.texi
 | 
			
		||||
	$(call quiet-command, \
 | 
			
		||||
	  perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< fsdev/virtfs-proxy-helper.pod && \
 | 
			
		||||
	  $(POD2MAN) --section=1 --center=" " --release=" " fsdev/virtfs-proxy-helper.pod > $@, \
 | 
			
		||||
	  "  GEN   $@")
 | 
			
		||||
 | 
			
		||||
html: qemu-doc.html docs/interop/qemu-qmp-ref.html docs/interop/qemu-ga-ref.html
 | 
			
		||||
info: qemu-doc.info docs/interop/qemu-qmp-ref.info docs/interop/qemu-ga-ref.info
 | 
			
		||||
pdf: qemu-doc.pdf docs/interop/qemu-qmp-ref.pdf docs/interop/qemu-ga-ref.pdf
 | 
			
		||||
txt: qemu-doc.txt docs/interop/qemu-qmp-ref.txt docs/interop/qemu-ga-ref.txt
 | 
			
		||||
qemu-nbd.8: qemu-nbd.texi
 | 
			
		||||
	$(call quiet-command, \
 | 
			
		||||
	  perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-nbd.pod && \
 | 
			
		||||
	  $(POD2MAN) --section=8 --center=" " --release=" " qemu-nbd.pod > $@, \
 | 
			
		||||
	  "  GEN   $@")
 | 
			
		||||
 | 
			
		||||
qemu-doc.html qemu-doc.info qemu-doc.pdf qemu-doc.txt: \
 | 
			
		||||
	qemu-img.texi qemu-nbd.texi qemu-options.texi qemu-option-trace.texi \
 | 
			
		||||
	qemu-monitor.texi qemu-img-cmds.texi qemu-ga.texi \
 | 
			
		||||
	qemu-monitor-info.texi
 | 
			
		||||
dvi: qemu-doc.dvi qemu-tech.dvi
 | 
			
		||||
html: qemu-doc.html qemu-tech.html
 | 
			
		||||
info: qemu-doc.info qemu-tech.info
 | 
			
		||||
pdf: qemu-doc.pdf qemu-tech.pdf
 | 
			
		||||
 | 
			
		||||
docs/interop/qemu-ga-ref.dvi docs/interop/qemu-ga-ref.html \
 | 
			
		||||
    docs/interop/qemu-ga-ref.info docs/interop/qemu-ga-ref.pdf \
 | 
			
		||||
    docs/interop/qemu-ga-ref.txt docs/interop/qemu-ga-ref.7: \
 | 
			
		||||
	docs/interop/qemu-ga-ref.texi docs/interop/qemu-ga-qapi.texi
 | 
			
		||||
 | 
			
		||||
docs/interop/qemu-qmp-ref.dvi docs/interop/qemu-qmp-ref.html \
 | 
			
		||||
    docs/interop/qemu-qmp-ref.info docs/interop/qemu-qmp-ref.pdf \
 | 
			
		||||
    docs/interop/qemu-qmp-ref.txt docs/interop/qemu-qmp-ref.7: \
 | 
			
		||||
	docs/interop/qemu-qmp-ref.texi docs/interop/qemu-qmp-qapi.texi
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_WIN32
 | 
			
		||||
 | 
			
		||||
INSTALLER = qemu-setup-$(VERSION)$(EXESUF)
 | 
			
		||||
 | 
			
		||||
nsisflags = -V2 -NOCD
 | 
			
		||||
 | 
			
		||||
ifneq ($(wildcard $(SRC_PATH)/dll),)
 | 
			
		||||
ifeq ($(ARCH),x86_64)
 | 
			
		||||
# 64 bit executables
 | 
			
		||||
DLL_PATH = $(SRC_PATH)/dll/w64
 | 
			
		||||
nsisflags += -DW64
 | 
			
		||||
else
 | 
			
		||||
# 32 bit executables
 | 
			
		||||
DLL_PATH = $(SRC_PATH)/dll/w32
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
.PHONY: installer
 | 
			
		||||
installer: $(INSTALLER)
 | 
			
		||||
 | 
			
		||||
INSTDIR=/tmp/qemu-nsis
 | 
			
		||||
 | 
			
		||||
$(INSTALLER): $(SRC_PATH)/qemu.nsi
 | 
			
		||||
	$(MAKE) install prefix=${INSTDIR}
 | 
			
		||||
ifdef SIGNCODE
 | 
			
		||||
	(cd ${INSTDIR}; \
 | 
			
		||||
         for i in *.exe; do \
 | 
			
		||||
           $(SIGNCODE) $${i}; \
 | 
			
		||||
         done \
 | 
			
		||||
        )
 | 
			
		||||
endif # SIGNCODE
 | 
			
		||||
	(cd ${INSTDIR}; \
 | 
			
		||||
         for i in qemu-system-*.exe; do \
 | 
			
		||||
           arch=$${i%.exe}; \
 | 
			
		||||
           arch=$${arch#qemu-system-}; \
 | 
			
		||||
           echo Section \"$$arch\" Section_$$arch; \
 | 
			
		||||
           echo SetOutPath \"\$$INSTDIR\"; \
 | 
			
		||||
           echo File \"\$${BINDIR}\\$$i\"; \
 | 
			
		||||
           echo SectionEnd; \
 | 
			
		||||
         done \
 | 
			
		||||
        ) >${INSTDIR}/system-emulations.nsh
 | 
			
		||||
	makensis $(nsisflags) \
 | 
			
		||||
                $(if $(BUILD_DOCS),-DCONFIG_DOCUMENTATION="y") \
 | 
			
		||||
                $(if $(CONFIG_GTK),-DCONFIG_GTK="y") \
 | 
			
		||||
                -DBINDIR="${INSTDIR}" \
 | 
			
		||||
                $(if $(DLL_PATH),-DDLLDIR="$(DLL_PATH)") \
 | 
			
		||||
                -DSRCDIR="$(SRC_PATH)" \
 | 
			
		||||
                -DOUTFILE="$(INSTALLER)" \
 | 
			
		||||
                -DDISPLAYVERSION="$(VERSION)" \
 | 
			
		||||
                $(SRC_PATH)/qemu.nsi
 | 
			
		||||
	rm -r ${INSTDIR}
 | 
			
		||||
ifdef SIGNCODE
 | 
			
		||||
	$(SIGNCODE) $(INSTALLER)
 | 
			
		||||
endif # SIGNCODE
 | 
			
		||||
endif # CONFIG_WIN
 | 
			
		||||
qemu-doc.dvi qemu-doc.html qemu-doc.info qemu-doc.pdf: \
 | 
			
		||||
	qemu-img.texi qemu-nbd.texi qemu-options.texi \
 | 
			
		||||
	qemu-monitor.texi qemu-img-cmds.texi
 | 
			
		||||
 | 
			
		||||
# Add a dependency on the generated files, so that they are always
 | 
			
		||||
# rebuilt before other object files
 | 
			
		||||
ifneq ($(wildcard config-host.mak),)
 | 
			
		||||
ifneq ($(filter-out $(UNCHECKED_GOALS),$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
Makefile: $(GENERATED_FILES)
 | 
			
		||||
ifneq ($(filter-out %clean,$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
 | 
			
		||||
Makefile: $(GENERATED_HEADERS)
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
.SECONDARY: $(TRACE_HEADERS) $(TRACE_HEADERS:%=%-timestamp) \
 | 
			
		||||
	$(TRACE_SOURCES) $(TRACE_SOURCES:%=%-timestamp) \
 | 
			
		||||
	$(TRACE_DTRACE) $(TRACE_DTRACE:%=%-timestamp)
 | 
			
		||||
 | 
			
		||||
# Include automatically generated dependency files
 | 
			
		||||
# Dependencies in Makefile.objs files come from our recursive subdir rules
 | 
			
		||||
-include $(wildcard *.d tests/*.d)
 | 
			
		||||
 | 
			
		||||
include $(SRC_PATH)/tests/docker/Makefile.include
 | 
			
		||||
 | 
			
		||||
.PHONY: help
 | 
			
		||||
help:
 | 
			
		||||
	@echo  'Generic targets:'
 | 
			
		||||
	@echo  '  all             - Build all'
 | 
			
		||||
	@echo  '  dir/file.o      - Build specified target only'
 | 
			
		||||
	@echo  '  install         - Install QEMU, documentation and tools'
 | 
			
		||||
	@echo  '  ctags/TAGS      - Generate tags file for editors'
 | 
			
		||||
	@echo  '  cscope          - Generate cscope index'
 | 
			
		||||
	@echo  ''
 | 
			
		||||
	@$(if $(TARGET_DIRS), \
 | 
			
		||||
		echo 'Architecture specific targets:'; \
 | 
			
		||||
		$(foreach t, $(TARGET_DIRS), \
 | 
			
		||||
		printf "  %-30s - Build for %s\\n" $(patsubst %,subdir-%,$(t)) $(t);) \
 | 
			
		||||
		echo '')
 | 
			
		||||
	@echo  'Cleaning targets:'
 | 
			
		||||
	@echo  '  clean           - Remove most generated files but keep the config'
 | 
			
		||||
	@echo  '  distclean       - Remove all generated files'
 | 
			
		||||
	@echo  '  dist            - Build a distributable tarball'
 | 
			
		||||
	@echo  ''
 | 
			
		||||
	@echo  'Test targets:'
 | 
			
		||||
	@echo  '  check           - Run all tests (check-help for details)'
 | 
			
		||||
	@echo  '  docker          - Help about targets running tests inside Docker containers'
 | 
			
		||||
	@echo  ''
 | 
			
		||||
	@echo  'Documentation targets:'
 | 
			
		||||
	@echo  '  html info pdf txt'
 | 
			
		||||
	@echo  '                  - Build documentation in specified format'
 | 
			
		||||
	@echo  ''
 | 
			
		||||
ifdef CONFIG_WIN32
 | 
			
		||||
	@echo  'Windows targets:'
 | 
			
		||||
	@echo  '  installer       - Build NSIS-based installer for QEMU'
 | 
			
		||||
ifdef QEMU_GA_MSI_ENABLED
 | 
			
		||||
	@echo  '  msi             - Build MSI-based installer for qemu-ga'
 | 
			
		||||
endif
 | 
			
		||||
	@echo  ''
 | 
			
		||||
endif
 | 
			
		||||
	@echo  '  make V=0|1 [targets] 0 => quiet build (default), 1 => verbose build'
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										165
									
								
								Makefile.objs
									
									
									
									
									
								
							
							
						
						
									
										165
									
								
								Makefile.objs
									
									
									
									
									
								
							@@ -1,37 +1,37 @@
 | 
			
		||||
#######################################################################
 | 
			
		||||
# Common libraries for tools and emulators
 | 
			
		||||
stub-obj-y = stubs/ crypto/
 | 
			
		||||
util-obj-y = util/ qobject/ qapi/
 | 
			
		||||
util-obj-y += qmp-introspect.o qapi-types.o qapi-visit.o qapi-event.o
 | 
			
		||||
 | 
			
		||||
chardev-obj-y = chardev/
 | 
			
		||||
stub-obj-y = stubs/
 | 
			
		||||
util-obj-y = util/ qobject/ qapi/ trace/
 | 
			
		||||
 | 
			
		||||
#######################################################################
 | 
			
		||||
# block-obj-y is code used by both qemu system emulation and qemu-img
 | 
			
		||||
 | 
			
		||||
block-obj-y += nbd/
 | 
			
		||||
block-obj-y += block.o blockjob.o
 | 
			
		||||
block-obj-y = async.o thread-pool.o
 | 
			
		||||
block-obj-y += nbd.o block.o blockjob.o
 | 
			
		||||
block-obj-y += main-loop.o iohandler.o qemu-timer.o
 | 
			
		||||
block-obj-$(CONFIG_POSIX) += aio-posix.o
 | 
			
		||||
block-obj-$(CONFIG_WIN32) += aio-win32.o
 | 
			
		||||
block-obj-y += block/
 | 
			
		||||
block-obj-y += qemu-io-cmds.o
 | 
			
		||||
block-obj-$(CONFIG_REPLICATION) += replication.o
 | 
			
		||||
block-obj-y += qapi-types.o qapi-visit.o
 | 
			
		||||
 | 
			
		||||
block-obj-m = block/
 | 
			
		||||
block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
 | 
			
		||||
block-obj-y += qemu-coroutine-sleep.o
 | 
			
		||||
block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
 | 
			
		||||
 | 
			
		||||
#######################################################################
 | 
			
		||||
# crypto-obj-y is code used by both qemu system emulation and qemu-img
 | 
			
		||||
ifeq ($(CONFIG_VIRTIO)$(CONFIG_VIRTFS)$(CONFIG_PCI),yyy)
 | 
			
		||||
# Lots of the fsdev/9pcode is pulled in by vl.c via qemu_fsdev_add.
 | 
			
		||||
# only pull in the actual virtio-9p device if we also enabled virtio.
 | 
			
		||||
CONFIG_REALLY_VIRTFS=y
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
crypto-obj-y = crypto/
 | 
			
		||||
crypto-aes-obj-y = crypto/
 | 
			
		||||
######################################################################
 | 
			
		||||
# smartcard
 | 
			
		||||
 | 
			
		||||
#######################################################################
 | 
			
		||||
# qom-obj-y is code used by both qemu system emulation and qemu-img
 | 
			
		||||
 | 
			
		||||
qom-obj-y = qom/
 | 
			
		||||
 | 
			
		||||
#######################################################################
 | 
			
		||||
# io-obj-y is code used by both qemu system emulation and qemu-img
 | 
			
		||||
 | 
			
		||||
io-obj-y = io/
 | 
			
		||||
libcacard-y += libcacard/cac.o libcacard/event.o
 | 
			
		||||
libcacard-y += libcacard/vcard.o libcacard/vreader.o
 | 
			
		||||
libcacard-y += libcacard/vcard_emul_nss.o
 | 
			
		||||
libcacard-y += libcacard/vcard_emul_type.o
 | 
			
		||||
libcacard-y += libcacard/card_7816.o
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# Target independent part of system emulation. The long term path is to
 | 
			
		||||
@@ -39,140 +39,73 @@ io-obj-y = io/
 | 
			
		||||
# single QEMU executable should support all CPUs and machines.
 | 
			
		||||
 | 
			
		||||
ifeq ($(CONFIG_SOFTMMU),y)
 | 
			
		||||
common-obj-y = blockdev.o blockdev-nbd.o block/
 | 
			
		||||
common-obj-y += iothread.o
 | 
			
		||||
common-obj-y = $(block-obj-y) blockdev.o blockdev-nbd.o block/
 | 
			
		||||
common-obj-y += net/
 | 
			
		||||
common-obj-y += qdev-monitor.o device-hotplug.o
 | 
			
		||||
common-obj-y += readline.o
 | 
			
		||||
common-obj-$(CONFIG_WIN32) += os-win32.o
 | 
			
		||||
common-obj-$(CONFIG_POSIX) += os-posix.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_LINUX) += fsdev/
 | 
			
		||||
 | 
			
		||||
common-obj-y += migration/
 | 
			
		||||
common-obj-y += migration.o migration-tcp.o
 | 
			
		||||
common-obj-y += qemu-char.o #aio.o
 | 
			
		||||
common-obj-y += block-migration.o
 | 
			
		||||
common-obj-y += page_cache.o xbzrle.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
 | 
			
		||||
 | 
			
		||||
common-obj-y += audio/
 | 
			
		||||
common-obj-y += hw/
 | 
			
		||||
 | 
			
		||||
common-obj-y += replay/
 | 
			
		||||
 | 
			
		||||
common-obj-y += ui/
 | 
			
		||||
common-obj-y += bt-host.o bt-vhci.o
 | 
			
		||||
bt-host.o-cflags := $(BLUEZ_CFLAGS)
 | 
			
		||||
 | 
			
		||||
common-obj-y += dma-helpers.o
 | 
			
		||||
common-obj-y += qtest.o
 | 
			
		||||
common-obj-y += vl.o
 | 
			
		||||
vl.o-cflags := $(GPROF_CFLAGS) $(SDL_CFLAGS)
 | 
			
		||||
common-obj-y += tpm.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_SLIRP) += slirp/
 | 
			
		||||
 | 
			
		||||
common-obj-y += backends/
 | 
			
		||||
common-obj-y += chardev/
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_SECCOMP) += qemu-seccomp.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_FDT) += device_tree.o
 | 
			
		||||
common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# qapi
 | 
			
		||||
 | 
			
		||||
common-obj-y += qmp-marshal.o
 | 
			
		||||
common-obj-y += qmp-introspect.o
 | 
			
		||||
common-obj-y += qmp-marshal.o qapi-visit.o qapi-types.o
 | 
			
		||||
common-obj-y += qmp.o hmp.o
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
#######################################################################
 | 
			
		||||
# Target-independent parts used in system and user emulation
 | 
			
		||||
common-obj-y += cpus-common.o
 | 
			
		||||
common-obj-y += qemu-log.o
 | 
			
		||||
common-obj-y += tcg-runtime.o
 | 
			
		||||
common-obj-y += hw/
 | 
			
		||||
common-obj-y += qom/
 | 
			
		||||
common-obj-y += disas/
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# Resource file for Windows executables
 | 
			
		||||
version-obj-$(CONFIG_WIN32) += $(BUILD_DIR)/version.o
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# tracing
 | 
			
		||||
util-obj-y +=  trace/
 | 
			
		||||
target-obj-y += trace/
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# guest agent
 | 
			
		||||
 | 
			
		||||
# FIXME: a few definitions from qapi-types.o/qapi-visit.o are needed
 | 
			
		||||
# by libqemuutil.a.  These should be moved to a separate .json schema.
 | 
			
		||||
qga-obj-y = qga/
 | 
			
		||||
qga-vss-dll-obj-y = qga/
 | 
			
		||||
qga-obj-y = qga/ qapi-types.o qapi-visit.o
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
# contrib
 | 
			
		||||
ivshmem-client-obj-y = contrib/ivshmem-client/
 | 
			
		||||
ivshmem-server-obj-y = contrib/ivshmem-server/
 | 
			
		||||
libvhost-user-obj-y = contrib/libvhost-user/
 | 
			
		||||
vhost-user-scsi.o-cflags := $(LIBISCSI_CFLAGS)
 | 
			
		||||
vhost-user-scsi.o-libs := $(LIBISCSI_LIBS)
 | 
			
		||||
vhost-user-scsi-obj-y = contrib/vhost-user-scsi/
 | 
			
		||||
vhost-user-scsi-obj-y += contrib/libvhost-user/libvhost-user.o
 | 
			
		||||
vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
 | 
			
		||||
 | 
			
		||||
######################################################################
 | 
			
		||||
trace-events-subdirs =
 | 
			
		||||
trace-events-subdirs += util
 | 
			
		||||
trace-events-subdirs += crypto
 | 
			
		||||
trace-events-subdirs += io
 | 
			
		||||
trace-events-subdirs += migration
 | 
			
		||||
trace-events-subdirs += block
 | 
			
		||||
trace-events-subdirs += backends
 | 
			
		||||
trace-events-subdirs += chardev
 | 
			
		||||
trace-events-subdirs += hw/block
 | 
			
		||||
trace-events-subdirs += hw/block/dataplane
 | 
			
		||||
trace-events-subdirs += hw/char
 | 
			
		||||
trace-events-subdirs += hw/intc
 | 
			
		||||
trace-events-subdirs += hw/net
 | 
			
		||||
trace-events-subdirs += hw/virtio
 | 
			
		||||
trace-events-subdirs += hw/audio
 | 
			
		||||
trace-events-subdirs += hw/misc
 | 
			
		||||
trace-events-subdirs += hw/usb
 | 
			
		||||
trace-events-subdirs += hw/scsi
 | 
			
		||||
trace-events-subdirs += hw/nvram
 | 
			
		||||
trace-events-subdirs += hw/display
 | 
			
		||||
trace-events-subdirs += hw/input
 | 
			
		||||
trace-events-subdirs += hw/timer
 | 
			
		||||
trace-events-subdirs += hw/dma
 | 
			
		||||
trace-events-subdirs += hw/sparc
 | 
			
		||||
trace-events-subdirs += hw/sd
 | 
			
		||||
trace-events-subdirs += hw/isa
 | 
			
		||||
trace-events-subdirs += hw/mem
 | 
			
		||||
trace-events-subdirs += hw/i386
 | 
			
		||||
trace-events-subdirs += hw/i386/xen
 | 
			
		||||
trace-events-subdirs += hw/9pfs
 | 
			
		||||
trace-events-subdirs += hw/ppc
 | 
			
		||||
trace-events-subdirs += hw/pci
 | 
			
		||||
trace-events-subdirs += hw/s390x
 | 
			
		||||
trace-events-subdirs += hw/vfio
 | 
			
		||||
trace-events-subdirs += hw/acpi
 | 
			
		||||
trace-events-subdirs += hw/arm
 | 
			
		||||
trace-events-subdirs += hw/alpha
 | 
			
		||||
trace-events-subdirs += hw/xen
 | 
			
		||||
trace-events-subdirs += ui
 | 
			
		||||
trace-events-subdirs += audio
 | 
			
		||||
trace-events-subdirs += net
 | 
			
		||||
trace-events-subdirs += target/arm
 | 
			
		||||
trace-events-subdirs += target/i386
 | 
			
		||||
trace-events-subdirs += target/mips
 | 
			
		||||
trace-events-subdirs += target/sparc
 | 
			
		||||
trace-events-subdirs += target/s390x
 | 
			
		||||
trace-events-subdirs += target/ppc
 | 
			
		||||
trace-events-subdirs += qom
 | 
			
		||||
trace-events-subdirs += linux-user
 | 
			
		||||
trace-events-subdirs += qapi
 | 
			
		||||
trace-events-subdirs += accel/tcg
 | 
			
		||||
trace-events-subdirs += accel/kvm
 | 
			
		||||
vl.o: QEMU_CFLAGS+=$(SDL_CFLAGS)
 | 
			
		||||
 | 
			
		||||
trace-events-files = $(SRC_PATH)/trace-events $(trace-events-subdirs:%=$(SRC_PATH)/%/trace-events)
 | 
			
		||||
QEMU_CFLAGS+=$(GLIB_CFLAGS)
 | 
			
		||||
 | 
			
		||||
trace-obj-y = trace-root.o
 | 
			
		||||
trace-obj-y += $(trace-events-subdirs:%=%/trace.o)
 | 
			
		||||
trace-obj-$(CONFIG_TRACE_UST) += trace-ust-all.o
 | 
			
		||||
trace-obj-$(CONFIG_TRACE_DTRACE) += trace-dtrace-root.o
 | 
			
		||||
trace-obj-$(CONFIG_TRACE_DTRACE) += $(trace-events-subdirs:%=%/trace-dtrace.o)
 | 
			
		||||
nested-vars += \
 | 
			
		||||
	stub-obj-y \
 | 
			
		||||
	util-obj-y \
 | 
			
		||||
	qga-obj-y \
 | 
			
		||||
	block-obj-y \
 | 
			
		||||
	common-obj-y
 | 
			
		||||
dummy := $(call unnest-vars)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										200
									
								
								Makefile.target
									
									
									
									
									
								
							
							
						
						
									
										200
									
								
								Makefile.target
									
									
									
									
									
								
							@@ -1,46 +1,45 @@
 | 
			
		||||
# -*- Mode: makefile -*-
 | 
			
		||||
 | 
			
		||||
BUILD_DIR?=$(CURDIR)/..
 | 
			
		||||
 | 
			
		||||
include ../config-host.mak
 | 
			
		||||
include config-target.mak
 | 
			
		||||
include config-devices.mak
 | 
			
		||||
include config-target.mak
 | 
			
		||||
include $(SRC_PATH)/rules.mak
 | 
			
		||||
 | 
			
		||||
$(call set-vpath, $(SRC_PATH):$(BUILD_DIR))
 | 
			
		||||
$(call set-vpath, $(SRC_PATH))
 | 
			
		||||
ifdef CONFIG_LINUX
 | 
			
		||||
QEMU_CFLAGS += -I../linux-headers
 | 
			
		||||
endif
 | 
			
		||||
QEMU_CFLAGS += -I.. -I$(SRC_PATH)/target/$(TARGET_BASE_ARCH) -DNEED_CPU_H
 | 
			
		||||
QEMU_CFLAGS += -I.. -I$(SRC_PATH)/target-$(TARGET_BASE_ARCH) -DNEED_CPU_H
 | 
			
		||||
 | 
			
		||||
QEMU_CFLAGS+=-I$(SRC_PATH)/include
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_USER_ONLY
 | 
			
		||||
# user emulator name
 | 
			
		||||
QEMU_PROG=qemu-$(TARGET_NAME)
 | 
			
		||||
QEMU_PROG_BUILD = $(QEMU_PROG)
 | 
			
		||||
QEMU_PROG=qemu-$(TARGET_ARCH2)
 | 
			
		||||
else
 | 
			
		||||
# system emulator name
 | 
			
		||||
QEMU_PROG=qemu-system-$(TARGET_NAME)$(EXESUF)
 | 
			
		||||
ifneq (,$(findstring -mwindows,$(libs_softmmu)))
 | 
			
		||||
ifneq (,$(findstring -mwindows,$(LIBS)))
 | 
			
		||||
# Terminate program name with a 'w' because the linker builds a windows executable.
 | 
			
		||||
QEMU_PROGW=qemu-system-$(TARGET_NAME)w$(EXESUF)
 | 
			
		||||
$(QEMU_PROG): $(QEMU_PROGW)
 | 
			
		||||
	$(call quiet-command,$(OBJCOPY) --subsystem console $(QEMU_PROGW) $(QEMU_PROG),"GEN","$(TARGET_DIR)$(QEMU_PROG)")
 | 
			
		||||
QEMU_PROG_BUILD = $(QEMU_PROGW)
 | 
			
		||||
else
 | 
			
		||||
QEMU_PROG_BUILD = $(QEMU_PROG)
 | 
			
		||||
endif
 | 
			
		||||
QEMU_PROGW=qemu-system-$(TARGET_ARCH2)w$(EXESUF)
 | 
			
		||||
endif # windows executable
 | 
			
		||||
QEMU_PROG=qemu-system-$(TARGET_ARCH2)$(EXESUF)
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
PROGS=$(QEMU_PROG) $(QEMU_PROGW)
 | 
			
		||||
PROGS=$(QEMU_PROG)
 | 
			
		||||
ifdef QEMU_PROGW
 | 
			
		||||
PROGS+=$(QEMU_PROGW)
 | 
			
		||||
endif
 | 
			
		||||
STPFILES=
 | 
			
		||||
 | 
			
		||||
ifndef CONFIG_HAIKU
 | 
			
		||||
LIBS+=-lm
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
config-target.h: config-target.h-timestamp
 | 
			
		||||
config-target.h-timestamp: config-target.mak
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_TRACE_SYSTEMTAP
 | 
			
		||||
stap: $(QEMU_PROG).stp-installed $(QEMU_PROG).stp $(QEMU_PROG)-simpletrace.stp
 | 
			
		||||
stap: $(QEMU_PROG).stp
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_USER_ONLY
 | 
			
		||||
TARGET_TYPE=user
 | 
			
		||||
@@ -48,38 +47,17 @@ else
 | 
			
		||||
TARGET_TYPE=system
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
$(QEMU_PROG).stp-installed: $(BUILD_DIR)/trace-events-all
 | 
			
		||||
$(QEMU_PROG).stp: $(SRC_PATH)/trace-events
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=all \
 | 
			
		||||
		--format=stap \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		--backend=$(TRACE_BACKEND) \
 | 
			
		||||
		--binary=$(bindir)/$(QEMU_PROG) \
 | 
			
		||||
		--target-name=$(TARGET_NAME) \
 | 
			
		||||
		--target-arch=$(TARGET_ARCH) \
 | 
			
		||||
		--target-type=$(TARGET_TYPE) \
 | 
			
		||||
		$< > $@,"GEN","$(TARGET_DIR)$(QEMU_PROG).stp-installed")
 | 
			
		||||
 | 
			
		||||
$(QEMU_PROG).stp: $(BUILD_DIR)/trace-events-all
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=all \
 | 
			
		||||
		--format=stap \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		--binary=$(realpath .)/$(QEMU_PROG) \
 | 
			
		||||
		--target-name=$(TARGET_NAME) \
 | 
			
		||||
		--target-type=$(TARGET_TYPE) \
 | 
			
		||||
		$< > $@,"GEN","$(TARGET_DIR)$(QEMU_PROG).stp")
 | 
			
		||||
 | 
			
		||||
$(QEMU_PROG)-simpletrace.stp: $(BUILD_DIR)/trace-events-all
 | 
			
		||||
	$(call quiet-command,$(TRACETOOL) \
 | 
			
		||||
		--group=all \
 | 
			
		||||
		--format=simpletrace-stap \
 | 
			
		||||
		--backends=$(TRACE_BACKENDS) \
 | 
			
		||||
		--probe-prefix=qemu.$(TARGET_TYPE).$(TARGET_NAME) \
 | 
			
		||||
		$< > $@,"GEN","$(TARGET_DIR)$(QEMU_PROG)-simpletrace.stp")
 | 
			
		||||
 | 
			
		||||
		< $< > $@,"  GEN   $(TARGET_DIR)$(QEMU_PROG).stp")
 | 
			
		||||
else
 | 
			
		||||
stap:
 | 
			
		||||
endif
 | 
			
		||||
.PHONY: stap
 | 
			
		||||
 | 
			
		||||
all: $(PROGS) stap
 | 
			
		||||
 | 
			
		||||
@@ -88,35 +66,24 @@ all: $(PROGS) stap
 | 
			
		||||
 | 
			
		||||
#########################################################
 | 
			
		||||
# cpu emulator library
 | 
			
		||||
obj-y += exec.o
 | 
			
		||||
obj-y += accel/
 | 
			
		||||
obj-y += tcg/tcg.o tcg/tcg-op.o tcg/optimize.o
 | 
			
		||||
obj-y += tcg/tcg-common.o tcg/tcg-runtime.o
 | 
			
		||||
obj-$(CONFIG_TCG_INTERPRETER) += tcg/tci.o
 | 
			
		||||
obj-y = exec.o translate-all.o cpu-exec.o
 | 
			
		||||
obj-y += tcg/tcg.o tcg/optimize.o
 | 
			
		||||
obj-$(CONFIG_TCG_INTERPRETER) += tci.o
 | 
			
		||||
obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o
 | 
			
		||||
obj-y += fpu/softfloat.o
 | 
			
		||||
obj-y += target/$(TARGET_BASE_ARCH)/
 | 
			
		||||
obj-y += target-$(TARGET_BASE_ARCH)/
 | 
			
		||||
obj-y += disas.o
 | 
			
		||||
obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o
 | 
			
		||||
obj-$(call lnot,$(CONFIG_HAX)) += hax-stub.o
 | 
			
		||||
 | 
			
		||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decContext.o
 | 
			
		||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decNumber.o
 | 
			
		||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal32.o
 | 
			
		||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal64.o
 | 
			
		||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal128.o
 | 
			
		||||
obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o
 | 
			
		||||
 | 
			
		||||
#########################################################
 | 
			
		||||
# Linux user emulator target
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_LINUX_USER
 | 
			
		||||
 | 
			
		||||
QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) \
 | 
			
		||||
             -I$(SRC_PATH)/linux-user/host/$(ARCH) \
 | 
			
		||||
             -I$(SRC_PATH)/linux-user
 | 
			
		||||
QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) -I$(SRC_PATH)/linux-user
 | 
			
		||||
 | 
			
		||||
obj-y += linux-user/
 | 
			
		||||
obj-y += gdbstub.o thunk.o user-exec.o user-exec-stub.o
 | 
			
		||||
obj-y += gdbstub.o thunk.o user-exec.o
 | 
			
		||||
 | 
			
		||||
endif #CONFIG_LINUX_USER
 | 
			
		||||
 | 
			
		||||
@@ -125,107 +92,100 @@ endif #CONFIG_LINUX_USER
 | 
			
		||||
 | 
			
		||||
ifdef CONFIG_BSD_USER
 | 
			
		||||
 | 
			
		||||
QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ABI_DIR) \
 | 
			
		||||
			 -I$(SRC_PATH)/bsd-user/$(HOST_VARIANT_DIR)
 | 
			
		||||
QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ARCH)
 | 
			
		||||
 | 
			
		||||
obj-y += bsd-user/
 | 
			
		||||
obj-y += gdbstub.o user-exec.o user-exec-stub.o
 | 
			
		||||
obj-y += gdbstub.o user-exec.o
 | 
			
		||||
 | 
			
		||||
endif #CONFIG_BSD_USER
 | 
			
		||||
 | 
			
		||||
#########################################################
 | 
			
		||||
# System emulator target
 | 
			
		||||
ifdef CONFIG_SOFTMMU
 | 
			
		||||
obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o numa.o
 | 
			
		||||
obj-y += qtest.o bootdevice.o
 | 
			
		||||
CONFIG_NO_PCI = $(if $(subst n,,$(CONFIG_PCI)),n,y)
 | 
			
		||||
CONFIG_NO_KVM = $(if $(subst n,,$(CONFIG_KVM)),n,y)
 | 
			
		||||
CONFIG_NO_XEN = $(if $(subst n,,$(CONFIG_XEN)),n,y)
 | 
			
		||||
CONFIG_NO_GET_MEMORY_MAPPING = $(if $(subst n,,$(CONFIG_HAVE_GET_MEMORY_MAPPING)),n,y)
 | 
			
		||||
CONFIG_NO_CORE_DUMP = $(if $(subst n,,$(CONFIG_HAVE_CORE_DUMP)),n,y)
 | 
			
		||||
 | 
			
		||||
obj-y += arch_init.o cpus.o monitor.o gdbstub.o balloon.o ioport.o
 | 
			
		||||
obj-y += hw/
 | 
			
		||||
obj-y += memory.o
 | 
			
		||||
obj-y += memory_mapping.o
 | 
			
		||||
obj-y += dump.o
 | 
			
		||||
obj-y += migration/ram.o
 | 
			
		||||
LIBS := $(libs_softmmu) $(LIBS)
 | 
			
		||||
obj-$(CONFIG_KVM) += kvm-all.o
 | 
			
		||||
obj-$(CONFIG_NO_KVM) += kvm-stub.o
 | 
			
		||||
obj-y += memory.o savevm.o cputlb.o
 | 
			
		||||
obj-$(CONFIG_HAVE_GET_MEMORY_MAPPING) += memory_mapping.o
 | 
			
		||||
obj-$(CONFIG_HAVE_CORE_DUMP) += dump.o
 | 
			
		||||
obj-$(CONFIG_NO_GET_MEMORY_MAPPING) += memory_mapping-stub.o
 | 
			
		||||
obj-$(CONFIG_NO_CORE_DUMP) += dump-stub.o
 | 
			
		||||
LIBS+=-lz
 | 
			
		||||
 | 
			
		||||
# xen support
 | 
			
		||||
obj-$(CONFIG_XEN) += xen-all.o xen-mapcache.o
 | 
			
		||||
obj-$(CONFIG_NO_XEN) += xen-stub.o
 | 
			
		||||
 | 
			
		||||
# Hardware support
 | 
			
		||||
ifeq ($(TARGET_NAME), sparc64)
 | 
			
		||||
ifeq ($(TARGET_ARCH), sparc64)
 | 
			
		||||
obj-y += hw/sparc64/
 | 
			
		||||
else
 | 
			
		||||
obj-y += hw/$(TARGET_BASE_ARCH)/
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES += hmp-commands.h hmp-commands-info.h
 | 
			
		||||
main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
 | 
			
		||||
 | 
			
		||||
GENERATED_HEADERS += hmp-commands.h qmp-commands-old.h
 | 
			
		||||
 | 
			
		||||
endif # CONFIG_SOFTMMU
 | 
			
		||||
 | 
			
		||||
# Workaround for http://gcc.gnu.org/PR55489, see configure.
 | 
			
		||||
%/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
 | 
			
		||||
 | 
			
		||||
dummy := $(call unnest-vars,,obj-y)
 | 
			
		||||
all-obj-y := $(obj-y)
 | 
			
		||||
nested-vars += obj-y
 | 
			
		||||
 | 
			
		||||
target-obj-y :=
 | 
			
		||||
block-obj-y :=
 | 
			
		||||
common-obj-y :=
 | 
			
		||||
chardev-obj-y :=
 | 
			
		||||
# This resolves all nested paths, so it must come last
 | 
			
		||||
include $(SRC_PATH)/Makefile.objs
 | 
			
		||||
dummy := $(call unnest-vars,,target-obj-y)
 | 
			
		||||
target-obj-y-save := $(target-obj-y)
 | 
			
		||||
dummy := $(call unnest-vars,.., \
 | 
			
		||||
               block-obj-y \
 | 
			
		||||
               block-obj-m \
 | 
			
		||||
               chardev-obj-y \
 | 
			
		||||
               crypto-obj-y \
 | 
			
		||||
               crypto-aes-obj-y \
 | 
			
		||||
               qom-obj-y \
 | 
			
		||||
               io-obj-y \
 | 
			
		||||
               common-obj-y \
 | 
			
		||||
               common-obj-m)
 | 
			
		||||
target-obj-y := $(target-obj-y-save)
 | 
			
		||||
all-obj-y += $(common-obj-y)
 | 
			
		||||
all-obj-y += $(target-obj-y)
 | 
			
		||||
all-obj-y += $(qom-obj-y)
 | 
			
		||||
all-obj-$(CONFIG_SOFTMMU) += $(block-obj-y) $(chardev-obj-y)
 | 
			
		||||
all-obj-$(CONFIG_USER_ONLY) += $(crypto-aes-obj-y)
 | 
			
		||||
all-obj-$(CONFIG_SOFTMMU) += $(crypto-obj-y)
 | 
			
		||||
all-obj-$(CONFIG_SOFTMMU) += $(io-obj-y)
 | 
			
		||||
 | 
			
		||||
$(QEMU_PROG_BUILD): config-devices.mak
 | 
			
		||||
all-obj-y = $(obj-y)
 | 
			
		||||
all-obj-y += $(addprefix ../, $(common-obj-y))
 | 
			
		||||
 | 
			
		||||
COMMON_LDADDS = ../libqemuutil.a ../libqemustub.a
 | 
			
		||||
 | 
			
		||||
# build either PROG or PROGW
 | 
			
		||||
$(QEMU_PROG_BUILD): $(all-obj-y) $(COMMON_LDADDS)
 | 
			
		||||
	$(call LINK, $(filter-out %.mak, $^))
 | 
			
		||||
ifdef CONFIG_DARWIN
 | 
			
		||||
	$(call quiet-command,Rez -append $(SRC_PATH)/pc-bios/qemu.rsrc -o $@,"REZ","$(TARGET_DIR)$@")
 | 
			
		||||
	$(call quiet-command,SetFile -a C $@,"SETFILE","$(TARGET_DIR)$@")
 | 
			
		||||
ifdef QEMU_PROGW
 | 
			
		||||
# The linker builds a windows executable. Make also a console executable.
 | 
			
		||||
$(QEMU_PROGW): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
 | 
			
		||||
	$(call LINK,$^)
 | 
			
		||||
$(QEMU_PROG): $(QEMU_PROGW)
 | 
			
		||||
	$(call quiet-command,$(OBJCOPY) --subsystem console $(QEMU_PROGW) $(QEMU_PROG),"  GEN   $(TARGET_DIR)$(QEMU_PROG)")
 | 
			
		||||
else
 | 
			
		||||
$(QEMU_PROG): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
 | 
			
		||||
	$(call LINK,$^)
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
 | 
			
		||||
	$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES),"GEN","$(TARGET_DIR)$@")
 | 
			
		||||
	$(call quiet-command,rm -f $@ && $(SHELL) $(SRC_PATH)/scripts/feature_to_c.sh $@ $(TARGET_XML_FILES),"  GEN   $(TARGET_DIR)$@")
 | 
			
		||||
 | 
			
		||||
hmp-commands.h: $(SRC_PATH)/hmp-commands.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$(TARGET_DIR)$@")
 | 
			
		||||
hmp-commands.h: $(SRC_PATH)/hmp-commands.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $(TARGET_DIR)$@")
 | 
			
		||||
 | 
			
		||||
hmp-commands-info.h: $(SRC_PATH)/hmp-commands-info.hx $(SRC_PATH)/scripts/hxtool
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"GEN","$(TARGET_DIR)$@")
 | 
			
		||||
qmp-commands-old.h: $(SRC_PATH)/qmp-commands.hx
 | 
			
		||||
	$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@,"  GEN   $(TARGET_DIR)$@")
 | 
			
		||||
 | 
			
		||||
clean: clean-target
 | 
			
		||||
clean:
 | 
			
		||||
	rm -f *.a *~ $(PROGS)
 | 
			
		||||
	rm -f $(shell find . -name '*.[od]')
 | 
			
		||||
	rm -f hmp-commands.h gdbstub-xml.c
 | 
			
		||||
	rm -f hmp-commands.h qmp-commands-old.h gdbstub-xml.c
 | 
			
		||||
ifdef CONFIG_TRACE_SYSTEMTAP
 | 
			
		||||
	rm -f *.stp
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
install: all
 | 
			
		||||
ifneq ($(PROGS),)
 | 
			
		||||
	$(call install-prog,$(PROGS),$(DESTDIR)$(bindir))
 | 
			
		||||
	$(INSTALL) -m 755 $(PROGS) "$(DESTDIR)$(bindir)"
 | 
			
		||||
ifneq ($(STRIP),)
 | 
			
		||||
	$(STRIP) $(patsubst %,"$(DESTDIR)$(bindir)/%",$(PROGS))
 | 
			
		||||
endif
 | 
			
		||||
endif
 | 
			
		||||
ifdef CONFIG_TRACE_SYSTEMTAP
 | 
			
		||||
	$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
 | 
			
		||||
	$(INSTALL_DATA) $(QEMU_PROG).stp-installed "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG).stp"
 | 
			
		||||
	$(INSTALL_DATA) $(QEMU_PROG)-simpletrace.stp "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG)-simpletrace.stp"
 | 
			
		||||
	$(INSTALL_DATA) $(QEMU_PROG).stp "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
GENERATED_FILES += config-target.h
 | 
			
		||||
Makefile: $(GENERATED_FILES)
 | 
			
		||||
GENERATED_HEADERS += config-target.h
 | 
			
		||||
Makefile: $(GENERATED_HEADERS)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										88
									
								
								QMP/README
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										88
									
								
								QMP/README
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,88 @@
 | 
			
		||||
                          QEMU Monitor Protocol
 | 
			
		||||
                          =====================
 | 
			
		||||
 | 
			
		||||
Introduction
 | 
			
		||||
-------------
 | 
			
		||||
 | 
			
		||||
The QEMU Monitor Protocol (QMP) allows applications to communicate with
 | 
			
		||||
QEMU's Monitor.
 | 
			
		||||
 | 
			
		||||
QMP is JSON[1] based and currently has the following features:
 | 
			
		||||
 | 
			
		||||
- Lightweight, text-based, easy to parse data format
 | 
			
		||||
- Asynchronous messages support (ie. events)
 | 
			
		||||
- Capabilities Negotiation
 | 
			
		||||
 | 
			
		||||
For detailed information on QMP's usage, please, refer to the following files:
 | 
			
		||||
 | 
			
		||||
o qmp-spec.txt      QEMU Monitor Protocol current specification
 | 
			
		||||
o qmp-commands.txt  QMP supported commands (auto-generated at build-time)
 | 
			
		||||
o qmp-events.txt    List of available asynchronous events
 | 
			
		||||
 | 
			
		||||
There is also a simple Python script called 'qmp-shell' available.
 | 
			
		||||
 | 
			
		||||
IMPORTANT: It's strongly recommended to read the 'Stability Considerations'
 | 
			
		||||
section in the qmp-commands.txt file before making any serious use of QMP.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
[1] http://www.json.org
 | 
			
		||||
 | 
			
		||||
Usage
 | 
			
		||||
-----
 | 
			
		||||
 | 
			
		||||
To enable QMP, you need a QEMU monitor instance in "control mode". There are
 | 
			
		||||
two ways of doing this.
 | 
			
		||||
 | 
			
		||||
The simplest one is using the '-qmp' command-line option. The following
 | 
			
		||||
example makes QMP available on localhost port 4444:
 | 
			
		||||
 | 
			
		||||
  $ qemu [...] -qmp tcp:localhost:4444,server
 | 
			
		||||
 | 
			
		||||
However, in order to have more complex combinations, like multiple monitors,
 | 
			
		||||
the '-mon' command-line option should be used along with the '-chardev' one.
 | 
			
		||||
For instance, the following example creates one user monitor on stdio and one
 | 
			
		||||
QMP monitor on localhost port 4444.
 | 
			
		||||
 | 
			
		||||
   $ qemu [...] -chardev stdio,id=mon0 -mon chardev=mon0,mode=readline \
 | 
			
		||||
                -chardev socket,id=mon1,host=localhost,port=4444,server \
 | 
			
		||||
                -mon chardev=mon1,mode=control
 | 
			
		||||
 | 
			
		||||
Please, refer to QEMU's manpage for more information.
 | 
			
		||||
 | 
			
		||||
Simple Testing
 | 
			
		||||
--------------
 | 
			
		||||
 | 
			
		||||
To manually test QMP one can connect with telnet and issue commands by hand:
 | 
			
		||||
 | 
			
		||||
$ telnet localhost 4444
 | 
			
		||||
Trying 127.0.0.1...
 | 
			
		||||
Connected to localhost.
 | 
			
		||||
Escape character is '^]'.
 | 
			
		||||
{"QMP": {"version": {"qemu": {"micro": 50, "minor": 13, "major": 0}, "package": ""}, "capabilities": []}}
 | 
			
		||||
{ "execute": "qmp_capabilities" }
 | 
			
		||||
{"return": {}}
 | 
			
		||||
{ "execute": "query-version" }
 | 
			
		||||
{"return": {"qemu": {"micro": 50, "minor": 13, "major": 0}, "package": ""}}
 | 
			
		||||
 | 
			
		||||
Development Process
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
When changing QMP's interface (by adding new commands, events or modifying
 | 
			
		||||
existing ones) it's mandatory to update the relevant documentation, which is
 | 
			
		||||
one (or more) of the files listed in the 'Introduction' section*.
 | 
			
		||||
 | 
			
		||||
Also, it's strongly recommended to send the documentation patch first, before
 | 
			
		||||
doing any code change. This is so because:
 | 
			
		||||
 | 
			
		||||
  1. Avoids the code dictating the interface
 | 
			
		||||
 | 
			
		||||
  2. Review can improve your interface.  Letting that happen before
 | 
			
		||||
     you implement it can save you work.
 | 
			
		||||
 | 
			
		||||
* The qmp-commands.txt file is generated from the qmp-commands.hx one, which
 | 
			
		||||
  is the file that should be edited.
 | 
			
		||||
 | 
			
		||||
Homepage
 | 
			
		||||
--------
 | 
			
		||||
 | 
			
		||||
http://wiki.qemu.org/QMP
 | 
			
		||||
@@ -33,7 +33,7 @@
 | 
			
		||||
# $ qemu-ga-client fsfreeze freeze
 | 
			
		||||
# 2 filesystems frozen
 | 
			
		||||
#
 | 
			
		||||
# See also: http://wiki.qemu-project.org/Features/QAPI/GuestAgent
 | 
			
		||||
# See also: http://wiki.qemu.org/Features/QAPI/GuestAgent
 | 
			
		||||
#
 | 
			
		||||
 | 
			
		||||
import base64
 | 
			
		||||
@@ -259,7 +259,7 @@ def main(address, cmd, args):
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        client = QemuGuestAgentClient(address)
 | 
			
		||||
    except QemuGuestAgent.error as e:
 | 
			
		||||
    except QemuGuestAgent.error, e:
 | 
			
		||||
        import errno
 | 
			
		||||
 | 
			
		||||
        print(e)
 | 
			
		||||
@@ -267,9 +267,7 @@ def main(address, cmd, args):
 | 
			
		||||
            print('Hint: qemu is not running?')
 | 
			
		||||
        sys.exit(1)
 | 
			
		||||
 | 
			
		||||
    if cmd == 'fsfreeze' and args[0] == 'freeze':
 | 
			
		||||
        client.sync(60)
 | 
			
		||||
    elif cmd != 'ping':
 | 
			
		||||
    if cmd != 'ping':
 | 
			
		||||
        client.sync()
 | 
			
		||||
 | 
			
		||||
    globals()['_cmd_' + cmd](client, args)
 | 
			
		||||
@@ -91,8 +91,8 @@ def main(args):
 | 
			
		||||
        try:
 | 
			
		||||
            os.environ['QMP_PATH'] = path
 | 
			
		||||
            os.execvp(fullcmd, [fullcmd] + args)
 | 
			
		||||
        except OSError as exc:
 | 
			
		||||
            if exc.errno == 2:
 | 
			
		||||
        except OSError, (errno, msg):
 | 
			
		||||
            if errno == 2:
 | 
			
		||||
                print 'Command "%s" not found.' % (fullcmd)
 | 
			
		||||
                return 1
 | 
			
		||||
            raise
 | 
			
		||||
							
								
								
									
										430
									
								
								QMP/qmp-events.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										430
									
								
								QMP/qmp-events.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,430 @@
 | 
			
		||||
                   QEMU Monitor Protocol Events
 | 
			
		||||
                   ============================
 | 
			
		||||
 | 
			
		||||
BALLOON_CHANGE
 | 
			
		||||
--------------
 | 
			
		||||
 | 
			
		||||
Emitted when the guest changes the actual BALLOON level. This
 | 
			
		||||
value is equivalent to the 'actual' field return by the
 | 
			
		||||
'query-balloon' command
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "actual": actual level of the guest memory balloon in bytes (json-number)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "BALLOON_CHANGE",
 | 
			
		||||
    "data": { "actual": 944766976 },
 | 
			
		||||
    "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
 | 
			
		||||
 | 
			
		||||
BLOCK_IO_ERROR
 | 
			
		||||
--------------
 | 
			
		||||
 | 
			
		||||
Emitted when a disk I/O error occurs.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "device": device name (json-string)
 | 
			
		||||
- "operation": I/O operation (json-string, "read" or "write")
 | 
			
		||||
- "action": action that has been taken, it's one of the following (json-string):
 | 
			
		||||
    "ignore": error has been ignored
 | 
			
		||||
    "report": error has been reported to the device
 | 
			
		||||
    "stop": error caused VM to be stopped
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "BLOCK_IO_ERROR",
 | 
			
		||||
    "data": { "device": "ide0-hd1",
 | 
			
		||||
              "operation": "write",
 | 
			
		||||
              "action": "stop" },
 | 
			
		||||
    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
 | 
			
		||||
 | 
			
		||||
Note: If action is "stop", a STOP event will eventually follow the
 | 
			
		||||
BLOCK_IO_ERROR event.
 | 
			
		||||
 | 
			
		||||
BLOCK_JOB_CANCELLED
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
Emitted when a block job has been cancelled.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "type":     Job type (json-string; "stream" for image streaming
 | 
			
		||||
                                     "commit" for block commit)
 | 
			
		||||
- "device":   Device name (json-string)
 | 
			
		||||
- "len":      Maximum progress value (json-int)
 | 
			
		||||
- "offset":   Current progress value (json-int)
 | 
			
		||||
              On success this is equal to len.
 | 
			
		||||
              On failure this is less than len.
 | 
			
		||||
- "speed":    Rate limit, bytes per second (json-int)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "BLOCK_JOB_CANCELLED",
 | 
			
		||||
     "data": { "type": "stream", "device": "virtio-disk0",
 | 
			
		||||
               "len": 10737418240, "offset": 134217728,
 | 
			
		||||
               "speed": 0 },
 | 
			
		||||
     "timestamp": { "seconds": 1267061043, "microseconds": 959568 } }
 | 
			
		||||
 | 
			
		||||
BLOCK_JOB_COMPLETED
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
Emitted when a block job has completed.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "type":     Job type (json-string; "stream" for image streaming
 | 
			
		||||
                                     "commit" for block commit)
 | 
			
		||||
- "device":   Device name (json-string)
 | 
			
		||||
- "len":      Maximum progress value (json-int)
 | 
			
		||||
- "offset":   Current progress value (json-int)
 | 
			
		||||
              On success this is equal to len.
 | 
			
		||||
              On failure this is less than len.
 | 
			
		||||
- "speed":    Rate limit, bytes per second (json-int)
 | 
			
		||||
- "error":    Error message (json-string, optional)
 | 
			
		||||
              Only present on failure.  This field contains a human-readable
 | 
			
		||||
              error message.  There are no semantics other than that streaming
 | 
			
		||||
              has failed and clients should not try to interpret the error
 | 
			
		||||
              string.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "BLOCK_JOB_COMPLETED",
 | 
			
		||||
     "data": { "type": "stream", "device": "virtio-disk0",
 | 
			
		||||
               "len": 10737418240, "offset": 10737418240,
 | 
			
		||||
               "speed": 0 },
 | 
			
		||||
     "timestamp": { "seconds": 1267061043, "microseconds": 959568 } }
 | 
			
		||||
 | 
			
		||||
BLOCK_JOB_ERROR
 | 
			
		||||
---------------
 | 
			
		||||
 | 
			
		||||
Emitted when a block job encounters an error.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "device": device name (json-string)
 | 
			
		||||
- "operation": I/O operation (json-string, "read" or "write")
 | 
			
		||||
- "action": action that has been taken, it's one of the following (json-string):
 | 
			
		||||
    "ignore": error has been ignored, the job may fail later
 | 
			
		||||
    "report": error will be reported and the job canceled
 | 
			
		||||
    "stop": error caused job to be paused
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "BLOCK_JOB_ERROR",
 | 
			
		||||
    "data": { "device": "ide0-hd1",
 | 
			
		||||
              "operation": "write",
 | 
			
		||||
              "action": "stop" },
 | 
			
		||||
    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
 | 
			
		||||
 | 
			
		||||
BLOCK_JOB_READY
 | 
			
		||||
---------------
 | 
			
		||||
 | 
			
		||||
Emitted when a block job is ready to complete.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "device": device name (json-string)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "BLOCK_JOB_READY",
 | 
			
		||||
    "data": { "device": "ide0-hd1" },
 | 
			
		||||
    "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
 | 
			
		||||
 | 
			
		||||
Note: The "ready to complete" status is always reset by a BLOCK_JOB_ERROR
 | 
			
		||||
event.
 | 
			
		||||
 | 
			
		||||
DEVICE_TRAY_MOVED
 | 
			
		||||
-----------------
 | 
			
		||||
 | 
			
		||||
It's emitted whenever the tray of a removable device is moved by the guest
 | 
			
		||||
or by HMP/QMP commands.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "device": device name (json-string)
 | 
			
		||||
- "tray-open": true if the tray has been opened or false if it has been closed
 | 
			
		||||
               (json-bool)
 | 
			
		||||
 | 
			
		||||
{ "event": "DEVICE_TRAY_MOVED",
 | 
			
		||||
  "data": { "device": "ide1-cd0",
 | 
			
		||||
            "tray-open": true
 | 
			
		||||
  },
 | 
			
		||||
  "timestamp": { "seconds": 1265044230, "microseconds": 450486 } }
 | 
			
		||||
 | 
			
		||||
RESET
 | 
			
		||||
-----
 | 
			
		||||
 | 
			
		||||
Emitted when the Virtual Machine is reseted.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "RESET",
 | 
			
		||||
    "timestamp": { "seconds": 1267041653, "microseconds": 9518 } }
 | 
			
		||||
 | 
			
		||||
RESUME
 | 
			
		||||
------
 | 
			
		||||
 | 
			
		||||
Emitted when the Virtual Machine resumes execution.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "RESUME",
 | 
			
		||||
    "timestamp": { "seconds": 1271770767, "microseconds": 582542 } }
 | 
			
		||||
 | 
			
		||||
RTC_CHANGE
 | 
			
		||||
----------
 | 
			
		||||
 | 
			
		||||
Emitted when the guest changes the RTC time.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "offset": delta against the host UTC in seconds (json-number)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "RTC_CHANGE",
 | 
			
		||||
    "data": { "offset": 78 },
 | 
			
		||||
    "timestamp": { "seconds": 1267020223, "microseconds": 435656 } }
 | 
			
		||||
 | 
			
		||||
SHUTDOWN
 | 
			
		||||
--------
 | 
			
		||||
 | 
			
		||||
Emitted when the Virtual Machine is powered down.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "SHUTDOWN",
 | 
			
		||||
    "timestamp": { "seconds": 1267040730, "microseconds": 682951 } }
 | 
			
		||||
 | 
			
		||||
Note: If the command-line option "-no-shutdown" has been specified, a STOP
 | 
			
		||||
event will eventually follow the SHUTDOWN event.
 | 
			
		||||
 | 
			
		||||
SPICE_CONNECTED, SPICE_DISCONNECTED
 | 
			
		||||
-----------------------------------
 | 
			
		||||
 | 
			
		||||
Emitted when a SPICE client connects or disconnects.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "server": Server information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "port": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
- "client": Client information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "port": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "timestamp": {"seconds": 1290688046, "microseconds": 388707},
 | 
			
		||||
  "event": "SPICE_CONNECTED",
 | 
			
		||||
  "data": {
 | 
			
		||||
    "server": { "port": "5920", "family": "ipv4", "host": "127.0.0.1"},
 | 
			
		||||
    "client": {"port": "52873", "family": "ipv4", "host": "127.0.0.1"}
 | 
			
		||||
}}
 | 
			
		||||
 | 
			
		||||
SPICE_INITIALIZED
 | 
			
		||||
-----------------
 | 
			
		||||
 | 
			
		||||
Emitted after initial handshake and authentication takes place (if any)
 | 
			
		||||
and the SPICE channel is up'n'running
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "server": Server information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "port": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "auth": authentication method (json-string, optional)
 | 
			
		||||
- "client": Client information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "port": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "connection-id": spice connection id.  All channels with the same id
 | 
			
		||||
                     belong to the same spice session (json-int)
 | 
			
		||||
  - "channel-type": channel type.  "1" is the main control channel, filter for
 | 
			
		||||
                    this one if you want track spice sessions only (json-int)
 | 
			
		||||
  - "channel-id": channel id.  Usually "0", might be different needed when
 | 
			
		||||
                  multiple channels of the same type exist, such as multiple
 | 
			
		||||
                  display channels in a multihead setup (json-int)
 | 
			
		||||
  - "tls": whevener the channel is encrypted (json-bool)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "timestamp": {"seconds": 1290688046, "microseconds": 417172},
 | 
			
		||||
  "event": "SPICE_INITIALIZED",
 | 
			
		||||
  "data": {"server": {"auth": "spice", "port": "5921",
 | 
			
		||||
                      "family": "ipv4", "host": "127.0.0.1"},
 | 
			
		||||
           "client": {"port": "49004", "family": "ipv4", "channel-type": 3,
 | 
			
		||||
                      "connection-id": 1804289383, "host": "127.0.0.1",
 | 
			
		||||
                      "channel-id": 0, "tls": true}
 | 
			
		||||
}}
 | 
			
		||||
 | 
			
		||||
STOP
 | 
			
		||||
----
 | 
			
		||||
 | 
			
		||||
Emitted when the Virtual Machine is stopped.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "STOP",
 | 
			
		||||
    "timestamp": { "seconds": 1267041730, "microseconds": 281295 } }
 | 
			
		||||
 | 
			
		||||
SUSPEND
 | 
			
		||||
-------
 | 
			
		||||
 | 
			
		||||
Emitted when guest enters S3 state.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "SUSPEND",
 | 
			
		||||
     "timestamp": { "seconds": 1344456160, "microseconds": 309119 } }
 | 
			
		||||
 | 
			
		||||
SUSPEND_DISK
 | 
			
		||||
------------
 | 
			
		||||
 | 
			
		||||
Emitted when the guest makes a request to enter S4 state.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "SUSPEND_DISK",
 | 
			
		||||
     "timestamp": { "seconds": 1344456160, "microseconds": 309119 } }
 | 
			
		||||
 | 
			
		||||
Note: QEMU shuts down when entering S4 state.
 | 
			
		||||
 | 
			
		||||
VNC_CONNECTED
 | 
			
		||||
-------------
 | 
			
		||||
 | 
			
		||||
Emitted when a VNC client establishes a connection.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "server": Server information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "service": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "auth": authentication method (json-string, optional)
 | 
			
		||||
- "client": Client information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "service": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "VNC_CONNECTED",
 | 
			
		||||
    "data": {
 | 
			
		||||
        "server": { "auth": "sasl", "family": "ipv4",
 | 
			
		||||
                    "service": "5901", "host": "0.0.0.0" },
 | 
			
		||||
        "client": { "family": "ipv4", "service": "58425",
 | 
			
		||||
                    "host": "127.0.0.1" } },
 | 
			
		||||
    "timestamp": { "seconds": 1262976601, "microseconds": 975795 } }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Note: This event is emitted before any authentication takes place, thus
 | 
			
		||||
the authentication ID is not provided.
 | 
			
		||||
 | 
			
		||||
VNC_DISCONNECTED
 | 
			
		||||
----------------
 | 
			
		||||
 | 
			
		||||
Emitted when the connection is closed.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "server": Server information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "service": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "auth": authentication method (json-string, optional)
 | 
			
		||||
- "client": Client information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "service": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "x509_dname": TLS dname (json-string, optional)
 | 
			
		||||
  - "sasl_username": SASL username (json-string, optional)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "VNC_DISCONNECTED",
 | 
			
		||||
    "data": {
 | 
			
		||||
        "server": { "auth": "sasl", "family": "ipv4",
 | 
			
		||||
                    "service": "5901", "host": "0.0.0.0" },
 | 
			
		||||
        "client": { "family": "ipv4", "service": "58425",
 | 
			
		||||
                    "host": "127.0.0.1", "sasl_username": "luiz" } },
 | 
			
		||||
    "timestamp": { "seconds": 1262976601, "microseconds": 975795 } }
 | 
			
		||||
 | 
			
		||||
VNC_INITIALIZED
 | 
			
		||||
---------------
 | 
			
		||||
 | 
			
		||||
Emitted after authentication takes place (if any) and the VNC session is
 | 
			
		||||
made active.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "server": Server information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "service": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "auth": authentication method (json-string, optional)
 | 
			
		||||
- "client": Client information (json-object)
 | 
			
		||||
  - "host": IP address (json-string)
 | 
			
		||||
  - "service": port number (json-string)
 | 
			
		||||
  - "family": address family (json-string, "ipv4" or "ipv6")
 | 
			
		||||
  - "x509_dname": TLS dname (json-string, optional)
 | 
			
		||||
  - "sasl_username": SASL username (json-string, optional)
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "VNC_INITIALIZED",
 | 
			
		||||
    "data": {
 | 
			
		||||
        "server": { "auth": "sasl", "family": "ipv4",
 | 
			
		||||
                    "service": "5901", "host": "0.0.0.0"},
 | 
			
		||||
        "client": { "family": "ipv4", "service": "46089",
 | 
			
		||||
                    "host": "127.0.0.1", "sasl_username": "luiz" } },
 | 
			
		||||
        "timestamp": { "seconds": 1263475302, "microseconds": 150772 } }
 | 
			
		||||
 | 
			
		||||
WAKEUP
 | 
			
		||||
------
 | 
			
		||||
 | 
			
		||||
Emitted when the guest has woken up from S3 and is running.
 | 
			
		||||
 | 
			
		||||
Data: None.
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "WATCHDOG",
 | 
			
		||||
     "timestamp": { "seconds": 1344522075, "microseconds": 745528 } }
 | 
			
		||||
 | 
			
		||||
WATCHDOG
 | 
			
		||||
--------
 | 
			
		||||
 | 
			
		||||
Emitted when the watchdog device's timer is expired.
 | 
			
		||||
 | 
			
		||||
Data:
 | 
			
		||||
 | 
			
		||||
- "action": Action that has been taken, it's one of the following (json-string):
 | 
			
		||||
            "reset", "shutdown", "poweroff", "pause", "debug", or "none"
 | 
			
		||||
 | 
			
		||||
Example:
 | 
			
		||||
 | 
			
		||||
{ "event": "WATCHDOG",
 | 
			
		||||
     "data": { "action": "reset" },
 | 
			
		||||
     "timestamp": { "seconds": 1267061043, "microseconds": 959568 } }
 | 
			
		||||
 | 
			
		||||
Note: If action is "reset", "shutdown", or "pause" the WATCHDOG event is
 | 
			
		||||
followed respectively by the RESET, SHUTDOWN, or STOP events.
 | 
			
		||||
							
								
								
									
										279
									
								
								QMP/qmp-shell
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										279
									
								
								QMP/qmp-shell
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,279 @@
 | 
			
		||||
#!/usr/bin/python
 | 
			
		||||
#
 | 
			
		||||
# Low-level QEMU shell on top of QMP.
 | 
			
		||||
#
 | 
			
		||||
# Copyright (C) 2009, 2010 Red Hat Inc.
 | 
			
		||||
#
 | 
			
		||||
# Authors:
 | 
			
		||||
#  Luiz Capitulino <lcapitulino@redhat.com>
 | 
			
		||||
#
 | 
			
		||||
# This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
			
		||||
# the COPYING file in the top-level directory.
 | 
			
		||||
#
 | 
			
		||||
# Usage:
 | 
			
		||||
#
 | 
			
		||||
# Start QEMU with:
 | 
			
		||||
#
 | 
			
		||||
# # qemu [...] -qmp unix:./qmp-sock,server
 | 
			
		||||
#
 | 
			
		||||
# Run the shell:
 | 
			
		||||
#
 | 
			
		||||
# $ qmp-shell ./qmp-sock
 | 
			
		||||
#
 | 
			
		||||
# Commands have the following format:
 | 
			
		||||
#
 | 
			
		||||
#    < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
 | 
			
		||||
#
 | 
			
		||||
# For example:
 | 
			
		||||
#
 | 
			
		||||
# (QEMU) device_add driver=e1000 id=net1
 | 
			
		||||
# {u'return': {}}
 | 
			
		||||
# (QEMU)
 | 
			
		||||
 | 
			
		||||
import qmp
 | 
			
		||||
import readline
 | 
			
		||||
import sys
 | 
			
		||||
import pprint
 | 
			
		||||
 | 
			
		||||
class QMPCompleter(list):
 | 
			
		||||
    def complete(self, text, state):
 | 
			
		||||
        for cmd in self:
 | 
			
		||||
            if cmd.startswith(text):
 | 
			
		||||
                if not state:
 | 
			
		||||
                    return cmd
 | 
			
		||||
                else:
 | 
			
		||||
                    state -= 1
 | 
			
		||||
 | 
			
		||||
class QMPShellError(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
class QMPShellBadPort(QMPShellError):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
 | 
			
		||||
#       _execute_cmd()). Let's design a better one.
 | 
			
		||||
class QMPShell(qmp.QEMUMonitorProtocol):
 | 
			
		||||
    def __init__(self, address, pp=None):
 | 
			
		||||
        qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
 | 
			
		||||
        self._greeting = None
 | 
			
		||||
        self._completer = None
 | 
			
		||||
        self._pp = pp
 | 
			
		||||
 | 
			
		||||
    def __get_address(self, arg):
 | 
			
		||||
        """
 | 
			
		||||
        Figure out if the argument is in the port:host form, if it's not it's
 | 
			
		||||
        probably a file path.
 | 
			
		||||
        """
 | 
			
		||||
        addr = arg.split(':')
 | 
			
		||||
        if len(addr) == 2:
 | 
			
		||||
            try:
 | 
			
		||||
                port = int(addr[1])
 | 
			
		||||
            except ValueError:
 | 
			
		||||
                raise QMPShellBadPort
 | 
			
		||||
            return ( addr[0], port )
 | 
			
		||||
        # socket path
 | 
			
		||||
        return arg
 | 
			
		||||
 | 
			
		||||
    def _fill_completion(self):
 | 
			
		||||
        for cmd in self.cmd('query-commands')['return']:
 | 
			
		||||
            self._completer.append(cmd['name'])
 | 
			
		||||
 | 
			
		||||
    def __completer_setup(self):
 | 
			
		||||
        self._completer = QMPCompleter()
 | 
			
		||||
        self._fill_completion()
 | 
			
		||||
        readline.set_completer(self._completer.complete)
 | 
			
		||||
        readline.parse_and_bind("tab: complete")
 | 
			
		||||
        # XXX: default delimiters conflict with some command names (eg. query-),
 | 
			
		||||
        # clearing everything as it doesn't seem to matter
 | 
			
		||||
        readline.set_completer_delims('')
 | 
			
		||||
 | 
			
		||||
    def __build_cmd(self, cmdline):
 | 
			
		||||
        """
 | 
			
		||||
        Build a QMP input object from a user provided command-line in the
 | 
			
		||||
        following format:
 | 
			
		||||
    
 | 
			
		||||
            < command-name > [ arg-name1=arg1 ] ... [ arg-nameN=argN ]
 | 
			
		||||
        """
 | 
			
		||||
        cmdargs = cmdline.split()
 | 
			
		||||
        qmpcmd = { 'execute': cmdargs[0], 'arguments': {} }
 | 
			
		||||
        for arg in cmdargs[1:]:
 | 
			
		||||
            opt = arg.split('=')
 | 
			
		||||
            try:
 | 
			
		||||
                value = int(opt[1])
 | 
			
		||||
            except ValueError:
 | 
			
		||||
                value = opt[1]
 | 
			
		||||
            qmpcmd['arguments'][opt[0]] = value
 | 
			
		||||
        return qmpcmd
 | 
			
		||||
 | 
			
		||||
    def _execute_cmd(self, cmdline):
 | 
			
		||||
        try:
 | 
			
		||||
            qmpcmd = self.__build_cmd(cmdline)
 | 
			
		||||
        except:
 | 
			
		||||
            print 'command format: <command-name> ',
 | 
			
		||||
            print '[arg-name1=arg1] ... [arg-nameN=argN]'
 | 
			
		||||
            return True
 | 
			
		||||
        resp = self.cmd_obj(qmpcmd)
 | 
			
		||||
        if resp is None:
 | 
			
		||||
            print 'Disconnected'
 | 
			
		||||
            return False
 | 
			
		||||
 | 
			
		||||
        if self._pp is not None:
 | 
			
		||||
            self._pp.pprint(resp)
 | 
			
		||||
        else:
 | 
			
		||||
            print resp
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    def connect(self):
 | 
			
		||||
        self._greeting = qmp.QEMUMonitorProtocol.connect(self)
 | 
			
		||||
        self.__completer_setup()
 | 
			
		||||
 | 
			
		||||
    def show_banner(self, msg='Welcome to the QMP low-level shell!'):
 | 
			
		||||
        print msg
 | 
			
		||||
        version = self._greeting['QMP']['version']['qemu']
 | 
			
		||||
        print 'Connected to QEMU %d.%d.%d\n' % (version['major'],version['minor'],version['micro'])
 | 
			
		||||
 | 
			
		||||
    def read_exec_command(self, prompt):
 | 
			
		||||
        """
 | 
			
		||||
        Read and execute a command.
 | 
			
		||||
 | 
			
		||||
        @return True if execution was ok, return False if disconnected.
 | 
			
		||||
        """
 | 
			
		||||
        try:
 | 
			
		||||
            cmdline = raw_input(prompt)
 | 
			
		||||
        except EOFError:
 | 
			
		||||
            print
 | 
			
		||||
            return False
 | 
			
		||||
        if cmdline == '':
 | 
			
		||||
            for ev in self.get_events():
 | 
			
		||||
                print ev
 | 
			
		||||
            self.clear_events()
 | 
			
		||||
            return True
 | 
			
		||||
        else:
 | 
			
		||||
            return self._execute_cmd(cmdline)
 | 
			
		||||
 | 
			
		||||
class HMPShell(QMPShell):
 | 
			
		||||
    def __init__(self, address):
 | 
			
		||||
        QMPShell.__init__(self, address)
 | 
			
		||||
        self.__cpu_index = 0
 | 
			
		||||
 | 
			
		||||
    def __cmd_completion(self):
 | 
			
		||||
        for cmd in self.__cmd_passthrough('help')['return'].split('\r\n'):
 | 
			
		||||
            if cmd and cmd[0] != '[' and cmd[0] != '\t':
 | 
			
		||||
                name = cmd.split()[0] # drop help text
 | 
			
		||||
                if name == 'info':
 | 
			
		||||
                    continue
 | 
			
		||||
                if name.find('|') != -1:
 | 
			
		||||
                    # Command in the form 'foobar|f' or 'f|foobar', take the
 | 
			
		||||
                    # full name
 | 
			
		||||
                    opt = name.split('|')
 | 
			
		||||
                    if len(opt[0]) == 1:
 | 
			
		||||
                        name = opt[1]
 | 
			
		||||
                    else:
 | 
			
		||||
                        name = opt[0]
 | 
			
		||||
                self._completer.append(name)
 | 
			
		||||
                self._completer.append('help ' + name) # help completion
 | 
			
		||||
 | 
			
		||||
    def __info_completion(self):
 | 
			
		||||
        for cmd in self.__cmd_passthrough('info')['return'].split('\r\n'):
 | 
			
		||||
            if cmd:
 | 
			
		||||
                self._completer.append('info ' + cmd.split()[1])
 | 
			
		||||
 | 
			
		||||
    def __other_completion(self):
 | 
			
		||||
        # special cases
 | 
			
		||||
        self._completer.append('help info')
 | 
			
		||||
 | 
			
		||||
    def _fill_completion(self):
 | 
			
		||||
        self.__cmd_completion()
 | 
			
		||||
        self.__info_completion()
 | 
			
		||||
        self.__other_completion()
 | 
			
		||||
 | 
			
		||||
    def __cmd_passthrough(self, cmdline, cpu_index = 0):
 | 
			
		||||
        return self.cmd_obj({ 'execute': 'human-monitor-command', 'arguments':
 | 
			
		||||
                              { 'command-line': cmdline,
 | 
			
		||||
                                'cpu-index': cpu_index } })
 | 
			
		||||
 | 
			
		||||
    def _execute_cmd(self, cmdline):
 | 
			
		||||
        if cmdline.split()[0] == "cpu":
 | 
			
		||||
            # trap the cpu command, it requires special setting
 | 
			
		||||
            try:
 | 
			
		||||
                idx = int(cmdline.split()[1])
 | 
			
		||||
                if not 'return' in self.__cmd_passthrough('info version', idx):
 | 
			
		||||
                    print 'bad CPU index'
 | 
			
		||||
                    return True
 | 
			
		||||
                self.__cpu_index = idx
 | 
			
		||||
            except ValueError:
 | 
			
		||||
                print 'cpu command takes an integer argument'
 | 
			
		||||
                return True
 | 
			
		||||
        resp = self.__cmd_passthrough(cmdline, self.__cpu_index)
 | 
			
		||||
        if resp is None:
 | 
			
		||||
            print 'Disconnected'
 | 
			
		||||
            return False
 | 
			
		||||
        assert 'return' in resp or 'error' in resp
 | 
			
		||||
        if 'return' in resp:
 | 
			
		||||
            # Success
 | 
			
		||||
            if len(resp['return']) > 0:
 | 
			
		||||
                print resp['return'],
 | 
			
		||||
        else:
 | 
			
		||||
            # Error
 | 
			
		||||
            print '%s: %s' % (resp['error']['class'], resp['error']['desc'])
 | 
			
		||||
        return True
 | 
			
		||||
 | 
			
		||||
    def show_banner(self):
 | 
			
		||||
        QMPShell.show_banner(self, msg='Welcome to the HMP shell!')
 | 
			
		||||
 | 
			
		||||
def die(msg):
 | 
			
		||||
    sys.stderr.write('ERROR: %s\n' % msg)
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
def fail_cmdline(option=None):
 | 
			
		||||
    if option:
 | 
			
		||||
        sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % option)
 | 
			
		||||
    sys.stderr.write('qemu-shell [ -p ] [ -H ] < UNIX socket path> | < TCP address:port >\n')
 | 
			
		||||
    sys.exit(1)
 | 
			
		||||
 | 
			
		||||
def main():
 | 
			
		||||
    addr = ''
 | 
			
		||||
    qemu = None
 | 
			
		||||
    hmp = False
 | 
			
		||||
    pp = None
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        for arg in sys.argv[1:]:
 | 
			
		||||
            if arg == "-H":
 | 
			
		||||
                if qemu is not None:
 | 
			
		||||
                    fail_cmdline(arg)
 | 
			
		||||
                hmp = True
 | 
			
		||||
            elif arg == "-p":
 | 
			
		||||
                if pp is not None:
 | 
			
		||||
                    fail_cmdline(arg)
 | 
			
		||||
                pp = pprint.PrettyPrinter(indent=4)
 | 
			
		||||
            else:
 | 
			
		||||
                if qemu is not None:
 | 
			
		||||
                    fail_cmdline(arg)
 | 
			
		||||
                if hmp:
 | 
			
		||||
                    qemu = HMPShell(arg)
 | 
			
		||||
                else:
 | 
			
		||||
                    qemu = QMPShell(arg, pp)
 | 
			
		||||
                addr = arg
 | 
			
		||||
 | 
			
		||||
        if qemu is None:
 | 
			
		||||
            fail_cmdline()
 | 
			
		||||
    except QMPShellBadPort:
 | 
			
		||||
        die('bad port number in command-line')
 | 
			
		||||
 | 
			
		||||
    try:
 | 
			
		||||
        qemu.connect()
 | 
			
		||||
    except qmp.QMPConnectError:
 | 
			
		||||
        die('Didn\'t get QMP greeting message')
 | 
			
		||||
    except qmp.QMPCapabilitiesError:
 | 
			
		||||
        die('Could not negotiate capabilities')
 | 
			
		||||
    except qemu.error:
 | 
			
		||||
        die('Could not connect to %s' % addr)
 | 
			
		||||
 | 
			
		||||
    qemu.show_banner()
 | 
			
		||||
    while qemu.read_exec_command('(QEMU) '):
 | 
			
		||||
        pass
 | 
			
		||||
    qemu.close()
 | 
			
		||||
 | 
			
		||||
if __name__ == '__main__':
 | 
			
		||||
    main()
 | 
			
		||||
							
								
								
									
										282
									
								
								QMP/qmp-spec.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										282
									
								
								QMP/qmp-spec.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,282 @@
 | 
			
		||||
           QEMU Monitor Protocol Specification - Version 0.1
 | 
			
		||||
 | 
			
		||||
1. Introduction
 | 
			
		||||
===============
 | 
			
		||||
 | 
			
		||||
This document specifies the QEMU Monitor Protocol (QMP), a JSON-based protocol
 | 
			
		||||
which is available for applications to control QEMU at the machine-level.
 | 
			
		||||
 | 
			
		||||
To enable QMP support, QEMU has to be run in "control mode". This is done by
 | 
			
		||||
starting QEMU with the appropriate command-line options. Please, refer to the
 | 
			
		||||
QEMU manual page for more information.
 | 
			
		||||
 | 
			
		||||
2. Protocol Specification
 | 
			
		||||
=========================
 | 
			
		||||
 | 
			
		||||
This section details the protocol format. For the purpose of this document
 | 
			
		||||
"Client" is any application which is communicating with QEMU in control mode,
 | 
			
		||||
and "Server" is QEMU itself.
 | 
			
		||||
 | 
			
		||||
JSON data structures, when mentioned in this document, are always in the
 | 
			
		||||
following format:
 | 
			
		||||
 | 
			
		||||
    json-DATA-STRUCTURE-NAME
 | 
			
		||||
 | 
			
		||||
Where DATA-STRUCTURE-NAME is any valid JSON data structure, as defined by
 | 
			
		||||
the JSON standard:
 | 
			
		||||
 | 
			
		||||
http://www.ietf.org/rfc/rfc4627.txt
 | 
			
		||||
 | 
			
		||||
For convenience, json-object members and json-array elements mentioned in
 | 
			
		||||
this document will be in a certain order. However, in real protocol usage
 | 
			
		||||
they can be in ANY order, thus no particular order should be assumed.
 | 
			
		||||
 | 
			
		||||
2.1 General Definitions
 | 
			
		||||
-----------------------
 | 
			
		||||
 | 
			
		||||
2.1.1 All interactions transmitted by the Server are json-objects, always
 | 
			
		||||
      terminating with CRLF
 | 
			
		||||
 | 
			
		||||
2.1.2 All json-objects members are mandatory when not specified otherwise
 | 
			
		||||
 | 
			
		||||
2.2 Server Greeting
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
Right when connected the Server will issue a greeting message, which signals
 | 
			
		||||
that the connection has been successfully established and that the Server is
 | 
			
		||||
ready for capabilities negotiation (for more information refer to section
 | 
			
		||||
'4. Capabilities Negotiation').
 | 
			
		||||
 | 
			
		||||
The format is:
 | 
			
		||||
 | 
			
		||||
{ "QMP": { "version": json-object, "capabilities": json-array } }
 | 
			
		||||
 | 
			
		||||
 Where,
 | 
			
		||||
 | 
			
		||||
- The "version" member contains the Server's version information (the format
 | 
			
		||||
  is the same of the 'query-version' command)
 | 
			
		||||
- The "capabilities" member specify the availability of features beyond the
 | 
			
		||||
  baseline specification
 | 
			
		||||
 | 
			
		||||
2.3 Issuing Commands
 | 
			
		||||
--------------------
 | 
			
		||||
 | 
			
		||||
The format for command execution is:
 | 
			
		||||
 | 
			
		||||
{ "execute": json-string, "arguments": json-object, "id": json-value }
 | 
			
		||||
 | 
			
		||||
 Where,
 | 
			
		||||
 | 
			
		||||
- The "execute" member identifies the command to be executed by the Server
 | 
			
		||||
- The "arguments" member is used to pass any arguments required for the
 | 
			
		||||
  execution of the command, it is optional when no arguments are required
 | 
			
		||||
- The "id" member is a transaction identification associated with the
 | 
			
		||||
  command execution, it is optional and will be part of the response if
 | 
			
		||||
  provided
 | 
			
		||||
 | 
			
		||||
2.4 Commands Responses
 | 
			
		||||
----------------------
 | 
			
		||||
 | 
			
		||||
There are two possible responses which the Server will issue as the result
 | 
			
		||||
of a command execution: success or error.
 | 
			
		||||
 | 
			
		||||
2.4.1 success
 | 
			
		||||
-------------
 | 
			
		||||
 | 
			
		||||
The success response is issued when the command execution has finished
 | 
			
		||||
without errors.
 | 
			
		||||
 | 
			
		||||
The format is:
 | 
			
		||||
 | 
			
		||||
{ "return": json-object, "id": json-value }
 | 
			
		||||
 | 
			
		||||
 Where,
 | 
			
		||||
 | 
			
		||||
- The "return" member contains the command returned data, which is defined
 | 
			
		||||
  in a per-command basis or an empty json-object if the command does not
 | 
			
		||||
  return data
 | 
			
		||||
- The "id" member contains the transaction identification associated
 | 
			
		||||
  with the command execution (if issued by the Client)
 | 
			
		||||
 | 
			
		||||
2.4.2 error
 | 
			
		||||
-----------
 | 
			
		||||
 | 
			
		||||
The error response is issued when the command execution could not be
 | 
			
		||||
completed because of an error condition.
 | 
			
		||||
 | 
			
		||||
The format is:
 | 
			
		||||
 | 
			
		||||
{ "error": { "class": json-string, "desc": json-string }, "id": json-value }
 | 
			
		||||
 | 
			
		||||
 Where,
 | 
			
		||||
 | 
			
		||||
- The "class" member contains the error class name (eg. "GenericError")
 | 
			
		||||
- The "desc" member is a human-readable error message. Clients should
 | 
			
		||||
  not attempt to parse this message.
 | 
			
		||||
- The "id" member contains the transaction identification associated with
 | 
			
		||||
  the command execution (if issued by the Client)
 | 
			
		||||
 | 
			
		||||
NOTE: Some errors can occur before the Server is able to read the "id" member,
 | 
			
		||||
in these cases the "id" member will not be part of the error response, even
 | 
			
		||||
if provided by the client.
 | 
			
		||||
 | 
			
		||||
2.5 Asynchronous events
 | 
			
		||||
-----------------------
 | 
			
		||||
 | 
			
		||||
As a result of state changes, the Server may send messages unilaterally
 | 
			
		||||
to the Client at any time. They are called 'asynchronous events'.
 | 
			
		||||
 | 
			
		||||
The format is:
 | 
			
		||||
 | 
			
		||||
{ "event": json-string, "data": json-object,
 | 
			
		||||
  "timestamp": { "seconds": json-number, "microseconds": json-number } }
 | 
			
		||||
 | 
			
		||||
 Where,
 | 
			
		||||
 | 
			
		||||
- The "event" member contains the event's name
 | 
			
		||||
- The "data" member contains event specific data, which is defined in a
 | 
			
		||||
  per-event basis, it is optional
 | 
			
		||||
- The "timestamp" member contains the exact time of when the event occurred
 | 
			
		||||
  in the Server. It is a fixed json-object with time in seconds and
 | 
			
		||||
  microseconds
 | 
			
		||||
 | 
			
		||||
For a listing of supported asynchronous events, please, refer to the
 | 
			
		||||
qmp-events.txt file.
 | 
			
		||||
 | 
			
		||||
3. QMP Examples
 | 
			
		||||
===============
 | 
			
		||||
 | 
			
		||||
This section provides some examples of real QMP usage, in all of them
 | 
			
		||||
'C' stands for 'Client' and 'S' stands for 'Server'.
 | 
			
		||||
 | 
			
		||||
3.1 Server greeting
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
S: {"QMP": {"version": {"qemu": "0.12.50", "package": ""}, "capabilities": []}}
 | 
			
		||||
 | 
			
		||||
3.2 Simple 'stop' execution
 | 
			
		||||
---------------------------
 | 
			
		||||
 | 
			
		||||
C: { "execute": "stop" }
 | 
			
		||||
S: {"return": {}}
 | 
			
		||||
 | 
			
		||||
3.3 KVM information
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
C: { "execute": "query-kvm", "id": "example" }
 | 
			
		||||
S: {"return": {"enabled": true, "present": true}, "id": "example"}
 | 
			
		||||
 | 
			
		||||
3.4 Parsing error
 | 
			
		||||
------------------
 | 
			
		||||
 | 
			
		||||
C: { "execute": }
 | 
			
		||||
S: {"error": {"class": "GenericError", "desc": "Invalid JSON syntax" } }
 | 
			
		||||
 | 
			
		||||
3.5 Powerdown event
 | 
			
		||||
-------------------
 | 
			
		||||
 | 
			
		||||
S: {"timestamp": {"seconds": 1258551470, "microseconds": 802384}, "event":
 | 
			
		||||
"POWERDOWN"}
 | 
			
		||||
 | 
			
		||||
4. Capabilities Negotiation
 | 
			
		||||
----------------------------
 | 
			
		||||
 | 
			
		||||
When a Client successfully establishes a connection, the Server is in
 | 
			
		||||
Capabilities Negotiation mode.
 | 
			
		||||
 | 
			
		||||
In this mode only the 'qmp_capabilities' command is allowed to run, all
 | 
			
		||||
other commands will return the CommandNotFound error. Asynchronous messages
 | 
			
		||||
are not delivered either.
 | 
			
		||||
 | 
			
		||||
Clients should use the 'qmp_capabilities' command to enable capabilities
 | 
			
		||||
advertised in the Server's greeting (section '2.2 Server Greeting') they
 | 
			
		||||
support.
 | 
			
		||||
 | 
			
		||||
When the 'qmp_capabilities' command is issued, and if it does not return an
 | 
			
		||||
error, the Server enters in Command mode where capabilities changes take
 | 
			
		||||
effect, all commands (except 'qmp_capabilities') are allowed and asynchronous
 | 
			
		||||
messages are delivered.
 | 
			
		||||
 | 
			
		||||
5 Compatibility Considerations
 | 
			
		||||
------------------------------
 | 
			
		||||
 | 
			
		||||
All protocol changes or new features which modify the protocol format in an
 | 
			
		||||
incompatible way are disabled by default and will be advertised by the
 | 
			
		||||
capabilities array (section '2.2 Server Greeting'). Thus, Clients can check
 | 
			
		||||
that array and enable the capabilities they support.
 | 
			
		||||
 | 
			
		||||
The QMP Server performs a type check on the arguments to a command.  It
 | 
			
		||||
generates an error if a value does not have the expected type for its
 | 
			
		||||
key, or if it does not understand a key that the Client included.  The
 | 
			
		||||
strictness of the Server catches wrong assumptions of Clients about
 | 
			
		||||
the Server's schema.  Clients can assume that, when such validation
 | 
			
		||||
errors occur, they will be reported before the command generated any
 | 
			
		||||
side effect.
 | 
			
		||||
 | 
			
		||||
However, Clients must not assume any particular:
 | 
			
		||||
 | 
			
		||||
- Length of json-arrays
 | 
			
		||||
- Size of json-objects; in particular, future versions of QEMU may add
 | 
			
		||||
  new keys and Clients should be able to ignore them.
 | 
			
		||||
- Order of json-object members or json-array elements
 | 
			
		||||
- Amount of errors generated by a command, that is, new errors can be added
 | 
			
		||||
  to any existing command in newer versions of the Server
 | 
			
		||||
 | 
			
		||||
Of course, the Server does guarantee to send valid JSON.  But apart from
 | 
			
		||||
this, a Client should be "conservative in what they send, and liberal in
 | 
			
		||||
what they accept".
 | 
			
		||||
 | 
			
		||||
6. Downstream extension of QMP
 | 
			
		||||
------------------------------
 | 
			
		||||
 | 
			
		||||
We recommend that downstream consumers of QEMU do *not* modify QMP.
 | 
			
		||||
Management tools should be able to support both upstream and downstream
 | 
			
		||||
versions of QMP without special logic, and downstream extensions are
 | 
			
		||||
inherently at odds with that.
 | 
			
		||||
 | 
			
		||||
However, we recognize that it is sometimes impossible for downstreams to
 | 
			
		||||
avoid modifying QMP.  Both upstream and downstream need to take care to
 | 
			
		||||
preserve long-term compatibility and interoperability.
 | 
			
		||||
 | 
			
		||||
To help with that, QMP reserves JSON object member names beginning with
 | 
			
		||||
'__' (double underscore) for downstream use ("downstream names").  This
 | 
			
		||||
means upstream will never use any downstream names for its commands,
 | 
			
		||||
arguments, errors, asynchronous events, and so forth.
 | 
			
		||||
 | 
			
		||||
Any new names downstream wishes to add must begin with '__'.  To
 | 
			
		||||
ensure compatibility with other downstreams, it is strongly
 | 
			
		||||
recommended that you prefix your downstram names with '__RFQDN_' where
 | 
			
		||||
RFQDN is a valid, reverse fully qualified domain name which you
 | 
			
		||||
control.  For example, a qemu-kvm specific monitor command would be:
 | 
			
		||||
 | 
			
		||||
    (qemu) __org.linux-kvm_enable_irqchip
 | 
			
		||||
 | 
			
		||||
Downstream must not change the server greeting (section 2.2) other than
 | 
			
		||||
to offer additional capabilities.  But see below for why even that is
 | 
			
		||||
discouraged.
 | 
			
		||||
 | 
			
		||||
Section '5 Compatibility Considerations' applies to downstream as well
 | 
			
		||||
as to upstream, obviously.  It follows that downstream must behave
 | 
			
		||||
exactly like upstream for any input not containing members with
 | 
			
		||||
downstream names ("downstream members"), except it may add members
 | 
			
		||||
with downstream names to its output.
 | 
			
		||||
 | 
			
		||||
Thus, a client should not be able to distinguish downstream from
 | 
			
		||||
upstream as long as it doesn't send input with downstream members, and
 | 
			
		||||
properly ignores any downstream members in the output it receives.
 | 
			
		||||
 | 
			
		||||
Advice on downstream modifications:
 | 
			
		||||
 | 
			
		||||
1. Introducing new commands is okay.  If you want to extend an existing
 | 
			
		||||
   command, consider introducing a new one with the new behaviour
 | 
			
		||||
   instead.
 | 
			
		||||
 | 
			
		||||
2. Introducing new asynchronous messages is okay.  If you want to extend
 | 
			
		||||
   an existing message, consider adding a new one instead.
 | 
			
		||||
 | 
			
		||||
3. Introducing new errors for use in new commands is okay.  Adding new
 | 
			
		||||
   errors to existing commands counts as extension, so 1. applies.
 | 
			
		||||
 | 
			
		||||
4. New capabilities are strongly discouraged.  Capabilities are for
 | 
			
		||||
   evolving the basic protocol, and multiple diverging basic protocol
 | 
			
		||||
   dialects are most undesirable.
 | 
			
		||||
							
								
								
									
										190
									
								
								QMP/qmp.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										190
									
								
								QMP/qmp.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,190 @@
 | 
			
		||||
# QEMU Monitor Protocol Python class
 | 
			
		||||
# 
 | 
			
		||||
# Copyright (C) 2009, 2010 Red Hat Inc.
 | 
			
		||||
#
 | 
			
		||||
# Authors:
 | 
			
		||||
#  Luiz Capitulino <lcapitulino@redhat.com>
 | 
			
		||||
#
 | 
			
		||||
# This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
			
		||||
# the COPYING file in the top-level directory.
 | 
			
		||||
 | 
			
		||||
import json
 | 
			
		||||
import errno
 | 
			
		||||
import socket
 | 
			
		||||
 | 
			
		||||
class QMPError(Exception):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
class QMPConnectError(QMPError):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
class QMPCapabilitiesError(QMPError):
 | 
			
		||||
    pass
 | 
			
		||||
 | 
			
		||||
class QEMUMonitorProtocol:
 | 
			
		||||
    def __init__(self, address, server=False):
 | 
			
		||||
        """
 | 
			
		||||
        Create a QEMUMonitorProtocol class.
 | 
			
		||||
 | 
			
		||||
        @param address: QEMU address, can be either a unix socket path (string)
 | 
			
		||||
                        or a tuple in the form ( address, port ) for a TCP
 | 
			
		||||
                        connection
 | 
			
		||||
        @param server: server mode listens on the socket (bool)
 | 
			
		||||
        @raise socket.error on socket connection errors
 | 
			
		||||
        @note No connection is established, this is done by the connect() or
 | 
			
		||||
              accept() methods
 | 
			
		||||
        """
 | 
			
		||||
        self.__events = []
 | 
			
		||||
        self.__address = address
 | 
			
		||||
        self.__sock = self.__get_sock()
 | 
			
		||||
        if server:
 | 
			
		||||
            self.__sock.bind(self.__address)
 | 
			
		||||
            self.__sock.listen(1)
 | 
			
		||||
 | 
			
		||||
    def __get_sock(self):
 | 
			
		||||
        if isinstance(self.__address, tuple):
 | 
			
		||||
            family = socket.AF_INET
 | 
			
		||||
        else:
 | 
			
		||||
            family = socket.AF_UNIX
 | 
			
		||||
        return socket.socket(family, socket.SOCK_STREAM)
 | 
			
		||||
 | 
			
		||||
    def __negotiate_capabilities(self):
 | 
			
		||||
        greeting = self.__json_read()
 | 
			
		||||
        if greeting is None or not greeting.has_key('QMP'):
 | 
			
		||||
            raise QMPConnectError
 | 
			
		||||
        # Greeting seems ok, negotiate capabilities
 | 
			
		||||
        resp = self.cmd('qmp_capabilities')
 | 
			
		||||
        if "return" in resp:
 | 
			
		||||
            return greeting
 | 
			
		||||
        raise QMPCapabilitiesError
 | 
			
		||||
 | 
			
		||||
    def __json_read(self, only_event=False):
 | 
			
		||||
        while True:
 | 
			
		||||
            data = self.__sockfile.readline()
 | 
			
		||||
            if not data:
 | 
			
		||||
                return
 | 
			
		||||
            resp = json.loads(data)
 | 
			
		||||
            if 'event' in resp:
 | 
			
		||||
                self.__events.append(resp)
 | 
			
		||||
                if not only_event:
 | 
			
		||||
                    continue
 | 
			
		||||
            return resp
 | 
			
		||||
 | 
			
		||||
    error = socket.error
 | 
			
		||||
 | 
			
		||||
    def connect(self, negotiate=True):
 | 
			
		||||
        """
 | 
			
		||||
        Connect to the QMP Monitor and perform capabilities negotiation.
 | 
			
		||||
 | 
			
		||||
        @return QMP greeting dict
 | 
			
		||||
        @raise socket.error on socket connection errors
 | 
			
		||||
        @raise QMPConnectError if the greeting is not received
 | 
			
		||||
        @raise QMPCapabilitiesError if fails to negotiate capabilities
 | 
			
		||||
        """
 | 
			
		||||
        self.__sock.connect(self.__address)
 | 
			
		||||
        self.__sockfile = self.__sock.makefile()
 | 
			
		||||
        if negotiate:
 | 
			
		||||
            return self.__negotiate_capabilities()
 | 
			
		||||
 | 
			
		||||
    def accept(self):
 | 
			
		||||
        """
 | 
			
		||||
        Await connection from QMP Monitor and perform capabilities negotiation.
 | 
			
		||||
 | 
			
		||||
        @return QMP greeting dict
 | 
			
		||||
        @raise socket.error on socket connection errors
 | 
			
		||||
        @raise QMPConnectError if the greeting is not received
 | 
			
		||||
        @raise QMPCapabilitiesError if fails to negotiate capabilities
 | 
			
		||||
        """
 | 
			
		||||
        self.__sock, _ = self.__sock.accept()
 | 
			
		||||
        self.__sockfile = self.__sock.makefile()
 | 
			
		||||
        return self.__negotiate_capabilities()
 | 
			
		||||
 | 
			
		||||
    def cmd_obj(self, qmp_cmd):
 | 
			
		||||
        """
 | 
			
		||||
        Send a QMP command to the QMP Monitor.
 | 
			
		||||
 | 
			
		||||
        @param qmp_cmd: QMP command to be sent as a Python dict
 | 
			
		||||
        @return QMP response as a Python dict or None if the connection has
 | 
			
		||||
                been closed
 | 
			
		||||
        """
 | 
			
		||||
        try:
 | 
			
		||||
            self.__sock.sendall(json.dumps(qmp_cmd))
 | 
			
		||||
        except socket.error, err:
 | 
			
		||||
            if err[0] == errno.EPIPE:
 | 
			
		||||
                return
 | 
			
		||||
            raise socket.error(err)
 | 
			
		||||
        return self.__json_read()
 | 
			
		||||
 | 
			
		||||
    def cmd(self, name, args=None, id=None):
 | 
			
		||||
        """
 | 
			
		||||
        Build a QMP command and send it to the QMP Monitor.
 | 
			
		||||
 | 
			
		||||
        @param name: command name (string)
 | 
			
		||||
        @param args: command arguments (dict)
 | 
			
		||||
        @param id: command id (dict, list, string or int)
 | 
			
		||||
        """
 | 
			
		||||
        qmp_cmd = { 'execute': name }
 | 
			
		||||
        if args:
 | 
			
		||||
            qmp_cmd['arguments'] = args
 | 
			
		||||
        if id:
 | 
			
		||||
            qmp_cmd['id'] = id
 | 
			
		||||
        return self.cmd_obj(qmp_cmd)
 | 
			
		||||
 | 
			
		||||
    def command(self, cmd, **kwds):
 | 
			
		||||
        ret = self.cmd(cmd, kwds)
 | 
			
		||||
        if ret.has_key('error'):
 | 
			
		||||
            raise Exception(ret['error']['desc'])
 | 
			
		||||
        return ret['return']
 | 
			
		||||
 | 
			
		||||
    def pull_event(self, wait=False):
 | 
			
		||||
        """
 | 
			
		||||
        Get and delete the first available QMP event.
 | 
			
		||||
 | 
			
		||||
        @param wait: block until an event is available (bool)
 | 
			
		||||
        """
 | 
			
		||||
        self.__sock.setblocking(0)
 | 
			
		||||
        try:
 | 
			
		||||
            self.__json_read()
 | 
			
		||||
        except socket.error, err:
 | 
			
		||||
            if err[0] == errno.EAGAIN:
 | 
			
		||||
                # No data available
 | 
			
		||||
                pass
 | 
			
		||||
        self.__sock.setblocking(1)
 | 
			
		||||
        if not self.__events and wait:
 | 
			
		||||
            self.__json_read(only_event=True)
 | 
			
		||||
        event = self.__events[0]
 | 
			
		||||
        del self.__events[0]
 | 
			
		||||
        return event
 | 
			
		||||
 | 
			
		||||
    def get_events(self, wait=False):
 | 
			
		||||
        """
 | 
			
		||||
        Get a list of available QMP events.
 | 
			
		||||
 | 
			
		||||
        @param wait: block until an event is available (bool)
 | 
			
		||||
        """
 | 
			
		||||
        self.__sock.setblocking(0)
 | 
			
		||||
        try:
 | 
			
		||||
            self.__json_read()
 | 
			
		||||
        except socket.error, err:
 | 
			
		||||
            if err[0] == errno.EAGAIN:
 | 
			
		||||
                # No data available
 | 
			
		||||
                pass
 | 
			
		||||
        self.__sock.setblocking(1)
 | 
			
		||||
        if not self.__events and wait:
 | 
			
		||||
            self.__json_read(only_event=True)
 | 
			
		||||
        return self.__events
 | 
			
		||||
 | 
			
		||||
    def clear_events(self):
 | 
			
		||||
        """
 | 
			
		||||
        Clear current list of pending events.
 | 
			
		||||
        """
 | 
			
		||||
        self.__events = []
 | 
			
		||||
 | 
			
		||||
    def close(self):
 | 
			
		||||
        self.__sock.close()
 | 
			
		||||
        self.__sockfile.close()
 | 
			
		||||
 | 
			
		||||
    timeout = socket.timeout
 | 
			
		||||
 | 
			
		||||
    def settimeout(self, timeout):
 | 
			
		||||
        self.__sock.settimeout(timeout)
 | 
			
		||||
@@ -61,4 +61,4 @@ else:
 | 
			
		||||
srv = QEMUMonitorProtocol(socket_path)
 | 
			
		||||
srv.connect()
 | 
			
		||||
 | 
			
		||||
print srv.command('qom-set', path=path, property=prop, value=value)
 | 
			
		||||
print srv.command('qom-set', path=path, property=prop, value=sys.argv[2])
 | 
			
		||||
							
								
								
									
										107
									
								
								README
									
									
									
									
									
								
							
							
						
						
									
										107
									
								
								README
									
									
									
									
									
								
							@@ -1,106 +1,3 @@
 | 
			
		||||
         QEMU README
 | 
			
		||||
         ===========
 | 
			
		||||
Read the documentation in qemu-doc.html or on http://wiki.qemu.org
 | 
			
		||||
 | 
			
		||||
QEMU is a generic and open source machine & userspace emulator and
 | 
			
		||||
virtualizer.
 | 
			
		||||
 | 
			
		||||
QEMU is capable of emulating a complete machine in software without any
 | 
			
		||||
need for hardware virtualization support. By using dynamic translation,
 | 
			
		||||
it achieves very good performance. QEMU can also integrate with the Xen
 | 
			
		||||
and KVM hypervisors to provide emulated hardware while allowing the
 | 
			
		||||
hypervisor to manage the CPU. With hypervisor support, QEMU can achieve
 | 
			
		||||
near native performance for CPUs. When QEMU emulates CPUs directly it is
 | 
			
		||||
capable of running operating systems made for one machine (e.g. an ARMv7
 | 
			
		||||
board) on a different machine (e.g. an x86_64 PC board).
 | 
			
		||||
 | 
			
		||||
QEMU is also capable of providing userspace API virtualization for Linux
 | 
			
		||||
and BSD kernel interfaces. This allows binaries compiled against one
 | 
			
		||||
architecture ABI (e.g. the Linux PPC64 ABI) to be run on a host using a
 | 
			
		||||
different architecture ABI (e.g. the Linux x86_64 ABI). This does not
 | 
			
		||||
involve any hardware emulation, simply CPU and syscall emulation.
 | 
			
		||||
 | 
			
		||||
QEMU aims to fit into a variety of use cases. It can be invoked directly
 | 
			
		||||
by users wishing to have full control over its behaviour and settings.
 | 
			
		||||
It also aims to facilitate integration into higher level management
 | 
			
		||||
layers, by providing a stable command line interface and monitor API.
 | 
			
		||||
It is commonly invoked indirectly via the libvirt library when using
 | 
			
		||||
open source applications such as oVirt, OpenStack and virt-manager.
 | 
			
		||||
 | 
			
		||||
QEMU as a whole is released under the GNU General Public License,
 | 
			
		||||
version 2. For full licensing details, consult the LICENSE file.
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Building
 | 
			
		||||
========
 | 
			
		||||
 | 
			
		||||
QEMU is multi-platform software intended to be buildable on all modern
 | 
			
		||||
Linux platforms, OS-X, Win32 (via the Mingw64 toolchain) and a variety
 | 
			
		||||
of other UNIX targets. The simple steps to build QEMU are:
 | 
			
		||||
 | 
			
		||||
  mkdir build
 | 
			
		||||
  cd build
 | 
			
		||||
  ../configure
 | 
			
		||||
  make
 | 
			
		||||
 | 
			
		||||
Additional information can also be found online via the QEMU website:
 | 
			
		||||
 | 
			
		||||
  http://qemu-project.org/Hosts/Linux
 | 
			
		||||
  http://qemu-project.org/Hosts/Mac
 | 
			
		||||
  http://qemu-project.org/Hosts/W32
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Submitting patches
 | 
			
		||||
==================
 | 
			
		||||
 | 
			
		||||
The QEMU source code is maintained under the GIT version control system.
 | 
			
		||||
 | 
			
		||||
   git clone git://git.qemu-project.org/qemu.git
 | 
			
		||||
 | 
			
		||||
When submitting patches, the preferred approach is to use 'git
 | 
			
		||||
format-patch' and/or 'git send-email' to format & send the mail to the
 | 
			
		||||
qemu-devel@nongnu.org mailing list. All patches submitted must contain
 | 
			
		||||
a 'Signed-off-by' line from the author. Patches should follow the
 | 
			
		||||
guidelines set out in the HACKING and CODING_STYLE files.
 | 
			
		||||
 | 
			
		||||
Additional information on submitting patches can be found online via
 | 
			
		||||
the QEMU website
 | 
			
		||||
 | 
			
		||||
  http://qemu-project.org/Contribute/SubmitAPatch
 | 
			
		||||
  http://qemu-project.org/Contribute/TrivialPatches
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Bug reporting
 | 
			
		||||
=============
 | 
			
		||||
 | 
			
		||||
The QEMU project uses Launchpad as its primary upstream bug tracker. Bugs
 | 
			
		||||
found when running code built from QEMU git or upstream released sources
 | 
			
		||||
should be reported via:
 | 
			
		||||
 | 
			
		||||
  https://bugs.launchpad.net/qemu/
 | 
			
		||||
 | 
			
		||||
If using QEMU via an operating system vendor pre-built binary package, it
 | 
			
		||||
is preferable to report bugs to the vendor's own bug tracker first. If
 | 
			
		||||
the bug is also known to affect latest upstream code, it can also be
 | 
			
		||||
reported via launchpad.
 | 
			
		||||
 | 
			
		||||
For additional information on bug reporting consult:
 | 
			
		||||
 | 
			
		||||
  http://qemu-project.org/Contribute/ReportABug
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Contact
 | 
			
		||||
=======
 | 
			
		||||
 | 
			
		||||
The QEMU community can be contacted in a number of ways, with the two
 | 
			
		||||
main methods being email and IRC
 | 
			
		||||
 | 
			
		||||
 - qemu-devel@nongnu.org
 | 
			
		||||
   http://lists.nongnu.org/mailman/listinfo/qemu-devel
 | 
			
		||||
 - #qemu on irc.oftc.net
 | 
			
		||||
 | 
			
		||||
Information on additional methods of contacting the community can be
 | 
			
		||||
found online via the QEMU website:
 | 
			
		||||
 | 
			
		||||
  http://qemu-project.org/Contribute/StartHere
 | 
			
		||||
 | 
			
		||||
-- End
 | 
			
		||||
- QEMU team
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										37
									
								
								TODO
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								TODO
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
General:
 | 
			
		||||
-------
 | 
			
		||||
- cycle counter for all archs
 | 
			
		||||
- cpu_interrupt() win32/SMP fix
 | 
			
		||||
- merge PIC spurious interrupt patch
 | 
			
		||||
- warning for OS/2: must not use 128 MB memory (merge bochs cmos patch ?)
 | 
			
		||||
- config file (at least for windows/Mac OS X)
 | 
			
		||||
- update doc: PCI infos.
 | 
			
		||||
- basic VGA optimizations
 | 
			
		||||
- better code fetch
 | 
			
		||||
- do not resize vga if invalid size.
 | 
			
		||||
- TLB code protection support for PPC
 | 
			
		||||
- disable SMC handling for ARM/SPARC/PPC (not finished)
 | 
			
		||||
- see undefined flags for BTx insn
 | 
			
		||||
- keyboard output buffer filling timing emulation
 | 
			
		||||
- tests for each target CPU
 | 
			
		||||
- fix all remaining thread lock issues (must put TBs in a specific invalid
 | 
			
		||||
  state, find a solution for tb_flush()).
 | 
			
		||||
 | 
			
		||||
ppc specific:
 | 
			
		||||
------------
 | 
			
		||||
- TLB invalidate not needed if msr_pr changes
 | 
			
		||||
- enable shift optimizations ?
 | 
			
		||||
 | 
			
		||||
linux-user specific:
 | 
			
		||||
-------------------
 | 
			
		||||
- remove threading support as it cannot work at this point
 | 
			
		||||
- improve IPC syscalls
 | 
			
		||||
- more syscalls (in particular all 64 bit ones, IPCs, fix 64 bit
 | 
			
		||||
  issues, fix 16 bit uid issues)
 | 
			
		||||
- use kernel traps for unaligned accesses on ARM ?
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
lower priority:
 | 
			
		||||
--------------
 | 
			
		||||
- int15 ah=86: use better timing
 | 
			
		||||
- use -msoft-float on ARM
 | 
			
		||||
@@ -1,4 +0,0 @@
 | 
			
		||||
obj-$(CONFIG_SOFTMMU) += accel.o
 | 
			
		||||
obj-y += kvm/
 | 
			
		||||
obj-y += tcg/
 | 
			
		||||
obj-y += stubs/
 | 
			
		||||
							
								
								
									
										128
									
								
								accel/accel.c
									
									
									
									
									
								
							
							
						
						
									
										128
									
								
								accel/accel.c
									
									
									
									
									
								
							@@ -1,128 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU System Emulator, accelerator interfaces
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2003-2008 Fabrice Bellard
 | 
			
		||||
 * Copyright (c) 2014 Red Hat Inc.
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 | 
			
		||||
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/accel.h"
 | 
			
		||||
#include "hw/boards.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "sysemu/arch_init.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "sysemu/kvm.h"
 | 
			
		||||
#include "sysemu/qtest.h"
 | 
			
		||||
#include "hw/xen/xen.h"
 | 
			
		||||
#include "qom/object.h"
 | 
			
		||||
 | 
			
		||||
static const TypeInfo accel_type = {
 | 
			
		||||
    .name = TYPE_ACCEL,
 | 
			
		||||
    .parent = TYPE_OBJECT,
 | 
			
		||||
    .class_size = sizeof(AccelClass),
 | 
			
		||||
    .instance_size = sizeof(AccelState),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/* Lookup AccelClass from opt_name. Returns NULL if not found */
 | 
			
		||||
static AccelClass *accel_find(const char *opt_name)
 | 
			
		||||
{
 | 
			
		||||
    char *class_name = g_strdup_printf(ACCEL_CLASS_NAME("%s"), opt_name);
 | 
			
		||||
    AccelClass *ac = ACCEL_CLASS(object_class_by_name(class_name));
 | 
			
		||||
    g_free(class_name);
 | 
			
		||||
    return ac;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int accel_init_machine(AccelClass *acc, MachineState *ms)
 | 
			
		||||
{
 | 
			
		||||
    ObjectClass *oc = OBJECT_CLASS(acc);
 | 
			
		||||
    const char *cname = object_class_get_name(oc);
 | 
			
		||||
    AccelState *accel = ACCEL(object_new(cname));
 | 
			
		||||
    int ret;
 | 
			
		||||
    ms->accelerator = accel;
 | 
			
		||||
    *(acc->allowed) = true;
 | 
			
		||||
    ret = acc->init_machine(ms);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        ms->accelerator = NULL;
 | 
			
		||||
        *(acc->allowed) = false;
 | 
			
		||||
        object_unref(OBJECT(accel));
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void configure_accelerator(MachineState *ms)
 | 
			
		||||
{
 | 
			
		||||
    const char *p;
 | 
			
		||||
    char buf[10];
 | 
			
		||||
    int ret;
 | 
			
		||||
    bool accel_initialised = false;
 | 
			
		||||
    bool init_failed = false;
 | 
			
		||||
    AccelClass *acc = NULL;
 | 
			
		||||
 | 
			
		||||
    p = qemu_opt_get(qemu_get_machine_opts(), "accel");
 | 
			
		||||
    if (p == NULL) {
 | 
			
		||||
        /* Use the default "accelerator", tcg */
 | 
			
		||||
        p = "tcg";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (!accel_initialised && *p != '\0') {
 | 
			
		||||
        if (*p == ':') {
 | 
			
		||||
            p++;
 | 
			
		||||
        }
 | 
			
		||||
        p = get_opt_name(buf, sizeof(buf), p, ':');
 | 
			
		||||
        acc = accel_find(buf);
 | 
			
		||||
        if (!acc) {
 | 
			
		||||
            fprintf(stderr, "\"%s\" accelerator not found.\n", buf);
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        if (acc->available && !acc->available()) {
 | 
			
		||||
            printf("%s not supported for this target\n",
 | 
			
		||||
                   acc->name);
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        ret = accel_init_machine(acc, ms);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            init_failed = true;
 | 
			
		||||
            fprintf(stderr, "failed to initialize %s: %s\n",
 | 
			
		||||
                    acc->name,
 | 
			
		||||
                    strerror(-ret));
 | 
			
		||||
        } else {
 | 
			
		||||
            accel_initialised = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!accel_initialised) {
 | 
			
		||||
        if (!init_failed) {
 | 
			
		||||
            fprintf(stderr, "No accelerator found!\n");
 | 
			
		||||
        }
 | 
			
		||||
        exit(1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (init_failed) {
 | 
			
		||||
        fprintf(stderr, "Back to %s accelerator.\n", acc->name);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void register_accel_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&accel_type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(register_accel_types);
 | 
			
		||||
@@ -1 +0,0 @@
 | 
			
		||||
obj-$(CONFIG_KVM) += kvm-all.o
 | 
			
		||||
							
								
								
									
										2633
									
								
								accel/kvm/kvm-all.c
									
									
									
									
									
								
							
							
						
						
									
										2633
									
								
								accel/kvm/kvm-all.c
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,15 +0,0 @@
 | 
			
		||||
# Trace events for debugging and performance instrumentation
 | 
			
		||||
 | 
			
		||||
# kvm-all.c
 | 
			
		||||
kvm_ioctl(int type, void *arg) "type 0x%x, arg %p"
 | 
			
		||||
kvm_vm_ioctl(int type, void *arg) "type 0x%x, arg %p"
 | 
			
		||||
kvm_vcpu_ioctl(int cpu_index, int type, void *arg) "cpu_index %d, type 0x%x, arg %p"
 | 
			
		||||
kvm_run_exit(int cpu_index, uint32_t reason) "cpu_index %d, reason %d"
 | 
			
		||||
kvm_device_ioctl(int fd, int type, void *arg) "dev fd %d, type 0x%x, arg %p"
 | 
			
		||||
kvm_failed_reg_get(uint64_t id, const char *msg) "Warning: Unable to retrieve ONEREG %" PRIu64 " from KVM: %s"
 | 
			
		||||
kvm_failed_reg_set(uint64_t id, const char *msg) "Warning: Unable to set ONEREG %" PRIu64 " to KVM: %s"
 | 
			
		||||
kvm_irqchip_commit_routes(void) ""
 | 
			
		||||
kvm_irqchip_add_msi_route(char *name, int vector, int virq) "dev %s vector %d virq %d"
 | 
			
		||||
kvm_irqchip_update_msi_route(int virq) "Updating MSI route virq=%d"
 | 
			
		||||
kvm_irqchip_release_virq(int virq) "virq %d"
 | 
			
		||||
 | 
			
		||||
@@ -1 +0,0 @@
 | 
			
		||||
obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
 | 
			
		||||
@@ -1,158 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU KVM stub
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright Red Hat, Inc. 2010
 | 
			
		||||
 *
 | 
			
		||||
 * Author: Paolo Bonzini     <pbonzini@redhat.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "cpu.h"
 | 
			
		||||
#include "sysemu/kvm.h"
 | 
			
		||||
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
#include "hw/pci/msi.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
KVMState *kvm_state;
 | 
			
		||||
bool kvm_kernel_irqchip;
 | 
			
		||||
bool kvm_async_interrupts_allowed;
 | 
			
		||||
bool kvm_eventfds_allowed;
 | 
			
		||||
bool kvm_irqfds_allowed;
 | 
			
		||||
bool kvm_resamplefds_allowed;
 | 
			
		||||
bool kvm_msi_via_irqfd_allowed;
 | 
			
		||||
bool kvm_gsi_routing_allowed;
 | 
			
		||||
bool kvm_gsi_direct_mapping;
 | 
			
		||||
bool kvm_allowed;
 | 
			
		||||
bool kvm_readonly_mem_allowed;
 | 
			
		||||
bool kvm_ioeventfd_any_length_allowed;
 | 
			
		||||
bool kvm_msi_use_devid;
 | 
			
		||||
 | 
			
		||||
int kvm_destroy_vcpu(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_init_vcpu(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_flush_coalesced_mmio_buffer(void)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_cpu_synchronize_state(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_cpu_synchronize_post_reset(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_cpu_synchronize_post_init(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_cpu_exec(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    abort();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_has_sync_mmu(void)
 | 
			
		||||
{
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_has_many_ioeventfds(void)
 | 
			
		||||
{
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_update_guest_debug(CPUState *cpu, unsigned long reinject_trap)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_insert_breakpoint(CPUState *cpu, target_ulong addr,
 | 
			
		||||
                          target_ulong len, int type)
 | 
			
		||||
{
 | 
			
		||||
    return -EINVAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_remove_breakpoint(CPUState *cpu, target_ulong addr,
 | 
			
		||||
                          target_ulong len, int type)
 | 
			
		||||
{
 | 
			
		||||
    return -EINVAL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_remove_all_breakpoints(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_on_sigbus_vcpu(CPUState *cpu, int code, void *addr)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_on_sigbus(int code, void *addr)
 | 
			
		||||
{
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
int kvm_irqchip_add_msi_route(KVMState *s, int vector, PCIDevice *dev)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_init_irq_routing(KVMState *s)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_irqchip_release_virq(KVMState *s, int virq)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_irqchip_update_msi_route(KVMState *s, int virq, MSIMessage msg,
 | 
			
		||||
                                 PCIDevice *dev)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_irqchip_commit_routes(KVMState *s)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_irqchip_add_adapter_route(KVMState *s, AdapterInfo *adapter)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_irqchip_add_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
 | 
			
		||||
                                       EventNotifier *rn, int virq)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int kvm_irqchip_remove_irqfd_notifier_gsi(KVMState *s, EventNotifier *n,
 | 
			
		||||
                                          int virq)
 | 
			
		||||
{
 | 
			
		||||
    return -ENOSYS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool kvm_has_free_slot(MachineState *ms)
 | 
			
		||||
{
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void kvm_init_cpu_signals(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    abort();
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
@@ -1,3 +0,0 @@
 | 
			
		||||
obj-$(CONFIG_SOFTMMU) += tcg-all.o
 | 
			
		||||
obj-$(CONFIG_SOFTMMU) += cputlb.o
 | 
			
		||||
obj-y += cpu-exec.o cpu-exec-common.o translate-all.o translate-common.o
 | 
			
		||||
@@ -1,82 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 *  emulator main execution loop
 | 
			
		||||
 *
 | 
			
		||||
 *  Copyright (c) 2003-2005 Fabrice Bellard
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "cpu.h"
 | 
			
		||||
#include "sysemu/cpus.h"
 | 
			
		||||
#include "exec/exec-all.h"
 | 
			
		||||
#include "exec/memory-internal.h"
 | 
			
		||||
 | 
			
		||||
/* exit the current TB, but without causing any exception to be raised */
 | 
			
		||||
void cpu_loop_exit_noexc(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    /* XXX: restore cpu registers saved in host registers */
 | 
			
		||||
 | 
			
		||||
    cpu->exception_index = -1;
 | 
			
		||||
    siglongjmp(cpu->jmp_env, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if defined(CONFIG_SOFTMMU)
 | 
			
		||||
void cpu_reloading_memory_map(void)
 | 
			
		||||
{
 | 
			
		||||
    if (qemu_in_vcpu_thread() && current_cpu->running) {
 | 
			
		||||
        /* The guest can in theory prolong the RCU critical section as long
 | 
			
		||||
         * as it feels like. The major problem with this is that because it
 | 
			
		||||
         * can do multiple reconfigurations of the memory map within the
 | 
			
		||||
         * critical section, we could potentially accumulate an unbounded
 | 
			
		||||
         * collection of memory data structures awaiting reclamation.
 | 
			
		||||
         *
 | 
			
		||||
         * Because the only thing we're currently protecting with RCU is the
 | 
			
		||||
         * memory data structures, it's sufficient to break the critical section
 | 
			
		||||
         * in this callback, which we know will get called every time the
 | 
			
		||||
         * memory map is rearranged.
 | 
			
		||||
         *
 | 
			
		||||
         * (If we add anything else in the system that uses RCU to protect
 | 
			
		||||
         * its data structures, we will need to implement some other mechanism
 | 
			
		||||
         * to force TCG CPUs to exit the critical section, at which point this
 | 
			
		||||
         * part of this callback might become unnecessary.)
 | 
			
		||||
         *
 | 
			
		||||
         * This pair matches cpu_exec's rcu_read_lock()/rcu_read_unlock(), which
 | 
			
		||||
         * only protects cpu->as->dispatch. Since we know our caller is about
 | 
			
		||||
         * to reload it, it's safe to split the critical section.
 | 
			
		||||
         */
 | 
			
		||||
        rcu_read_unlock();
 | 
			
		||||
        rcu_read_lock();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void cpu_loop_exit(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    siglongjmp(cpu->jmp_env, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc)
 | 
			
		||||
{
 | 
			
		||||
    if (pc) {
 | 
			
		||||
        cpu_restore_state(cpu, pc);
 | 
			
		||||
    }
 | 
			
		||||
    siglongjmp(cpu->jmp_env, 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cpu_loop_exit_atomic(CPUState *cpu, uintptr_t pc)
 | 
			
		||||
{
 | 
			
		||||
    cpu->exception_index = EXCP_ATOMIC;
 | 
			
		||||
    cpu_loop_exit_restore(cpu, pc);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,683 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 *  emulator main execution loop
 | 
			
		||||
 *
 | 
			
		||||
 *  Copyright (c) 2003-2005 Fabrice Bellard
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "cpu.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "disas/disas.h"
 | 
			
		||||
#include "exec/exec-all.h"
 | 
			
		||||
#include "tcg.h"
 | 
			
		||||
#include "qemu/atomic.h"
 | 
			
		||||
#include "sysemu/qtest.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "exec/address-spaces.h"
 | 
			
		||||
#include "qemu/rcu.h"
 | 
			
		||||
#include "exec/tb-hash.h"
 | 
			
		||||
#include "exec/log.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 | 
			
		||||
#include "hw/i386/apic.h"
 | 
			
		||||
#endif
 | 
			
		||||
#include "sysemu/cpus.h"
 | 
			
		||||
#include "sysemu/replay.h"
 | 
			
		||||
 | 
			
		||||
/* -icount align implementation. */
 | 
			
		||||
 | 
			
		||||
typedef struct SyncClocks {
 | 
			
		||||
    int64_t diff_clk;
 | 
			
		||||
    int64_t last_cpu_icount;
 | 
			
		||||
    int64_t realtime_clock;
 | 
			
		||||
} SyncClocks;
 | 
			
		||||
 | 
			
		||||
#if !defined(CONFIG_USER_ONLY)
 | 
			
		||||
/* Allow the guest to have a max 3ms advance.
 | 
			
		||||
 * The difference between the 2 clocks could therefore
 | 
			
		||||
 * oscillate around 0.
 | 
			
		||||
 */
 | 
			
		||||
#define VM_CLOCK_ADVANCE 3000000
 | 
			
		||||
#define THRESHOLD_REDUCE 1.5
 | 
			
		||||
#define MAX_DELAY_PRINT_RATE 2000000000LL
 | 
			
		||||
#define MAX_NB_PRINTS 100
 | 
			
		||||
 | 
			
		||||
static void align_clocks(SyncClocks *sc, const CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    int64_t cpu_icount;
 | 
			
		||||
 | 
			
		||||
    if (!icount_align_option) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
 | 
			
		||||
    sc->diff_clk += cpu_icount_to_ns(sc->last_cpu_icount - cpu_icount);
 | 
			
		||||
    sc->last_cpu_icount = cpu_icount;
 | 
			
		||||
 | 
			
		||||
    if (sc->diff_clk > VM_CLOCK_ADVANCE) {
 | 
			
		||||
#ifndef _WIN32
 | 
			
		||||
        struct timespec sleep_delay, rem_delay;
 | 
			
		||||
        sleep_delay.tv_sec = sc->diff_clk / 1000000000LL;
 | 
			
		||||
        sleep_delay.tv_nsec = sc->diff_clk % 1000000000LL;
 | 
			
		||||
        if (nanosleep(&sleep_delay, &rem_delay) < 0) {
 | 
			
		||||
            sc->diff_clk = rem_delay.tv_sec * 1000000000LL + rem_delay.tv_nsec;
 | 
			
		||||
        } else {
 | 
			
		||||
            sc->diff_clk = 0;
 | 
			
		||||
        }
 | 
			
		||||
#else
 | 
			
		||||
        Sleep(sc->diff_clk / SCALE_MS);
 | 
			
		||||
        sc->diff_clk = 0;
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void print_delay(const SyncClocks *sc)
 | 
			
		||||
{
 | 
			
		||||
    static float threshold_delay;
 | 
			
		||||
    static int64_t last_realtime_clock;
 | 
			
		||||
    static int nb_prints;
 | 
			
		||||
 | 
			
		||||
    if (icount_align_option &&
 | 
			
		||||
        sc->realtime_clock - last_realtime_clock >= MAX_DELAY_PRINT_RATE &&
 | 
			
		||||
        nb_prints < MAX_NB_PRINTS) {
 | 
			
		||||
        if ((-sc->diff_clk / (float)1000000000LL > threshold_delay) ||
 | 
			
		||||
            (-sc->diff_clk / (float)1000000000LL <
 | 
			
		||||
             (threshold_delay - THRESHOLD_REDUCE))) {
 | 
			
		||||
            threshold_delay = (-sc->diff_clk / 1000000000LL) + 1;
 | 
			
		||||
            printf("Warning: The guest is now late by %.1f to %.1f seconds\n",
 | 
			
		||||
                   threshold_delay - 1,
 | 
			
		||||
                   threshold_delay);
 | 
			
		||||
            nb_prints++;
 | 
			
		||||
            last_realtime_clock = sc->realtime_clock;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void init_delay_params(SyncClocks *sc,
 | 
			
		||||
                              const CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    if (!icount_align_option) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    sc->realtime_clock = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL_RT);
 | 
			
		||||
    sc->diff_clk = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) - sc->realtime_clock;
 | 
			
		||||
    sc->last_cpu_icount = cpu->icount_extra + cpu->icount_decr.u16.low;
 | 
			
		||||
    if (sc->diff_clk < max_delay) {
 | 
			
		||||
        max_delay = sc->diff_clk;
 | 
			
		||||
    }
 | 
			
		||||
    if (sc->diff_clk > max_advance) {
 | 
			
		||||
        max_advance = sc->diff_clk;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Print every 2s max if the guest is late. We limit the number
 | 
			
		||||
       of printed messages to NB_PRINT_MAX(currently 100) */
 | 
			
		||||
    print_delay(sc);
 | 
			
		||||
}
 | 
			
		||||
#else
 | 
			
		||||
static void align_clocks(SyncClocks *sc, const CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void init_delay_params(SyncClocks *sc, const CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
#endif /* CONFIG USER ONLY */
 | 
			
		||||
 | 
			
		||||
/* Execute a TB, and fix up the CPU state afterwards if necessary */
 | 
			
		||||
static inline tcg_target_ulong cpu_tb_exec(CPUState *cpu, TranslationBlock *itb)
 | 
			
		||||
{
 | 
			
		||||
    CPUArchState *env = cpu->env_ptr;
 | 
			
		||||
    uintptr_t ret;
 | 
			
		||||
    TranslationBlock *last_tb;
 | 
			
		||||
    int tb_exit;
 | 
			
		||||
    uint8_t *tb_ptr = itb->tc_ptr;
 | 
			
		||||
 | 
			
		||||
    qemu_log_mask_and_addr(CPU_LOG_EXEC, itb->pc,
 | 
			
		||||
                           "Trace %p [%d: " TARGET_FMT_lx "] %s\n",
 | 
			
		||||
                           itb->tc_ptr, cpu->cpu_index, itb->pc,
 | 
			
		||||
                           lookup_symbol(itb->pc));
 | 
			
		||||
 | 
			
		||||
#if defined(DEBUG_DISAS)
 | 
			
		||||
    if (qemu_loglevel_mask(CPU_LOG_TB_CPU)
 | 
			
		||||
        && qemu_log_in_addr_range(itb->pc)) {
 | 
			
		||||
        qemu_log_lock();
 | 
			
		||||
#if defined(TARGET_I386)
 | 
			
		||||
        log_cpu_state(cpu, CPU_DUMP_CCOP);
 | 
			
		||||
#else
 | 
			
		||||
        log_cpu_state(cpu, 0);
 | 
			
		||||
#endif
 | 
			
		||||
        qemu_log_unlock();
 | 
			
		||||
    }
 | 
			
		||||
#endif /* DEBUG_DISAS */
 | 
			
		||||
 | 
			
		||||
    cpu->can_do_io = !use_icount;
 | 
			
		||||
    ret = tcg_qemu_tb_exec(env, tb_ptr);
 | 
			
		||||
    cpu->can_do_io = 1;
 | 
			
		||||
    last_tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
 | 
			
		||||
    tb_exit = ret & TB_EXIT_MASK;
 | 
			
		||||
    trace_exec_tb_exit(last_tb, tb_exit);
 | 
			
		||||
 | 
			
		||||
    if (tb_exit > TB_EXIT_IDX1) {
 | 
			
		||||
        /* We didn't start executing this TB (eg because the instruction
 | 
			
		||||
         * counter hit zero); we must restore the guest PC to the address
 | 
			
		||||
         * of the start of the TB.
 | 
			
		||||
         */
 | 
			
		||||
        CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
        qemu_log_mask_and_addr(CPU_LOG_EXEC, last_tb->pc,
 | 
			
		||||
                               "Stopped execution of TB chain before %p ["
 | 
			
		||||
                               TARGET_FMT_lx "] %s\n",
 | 
			
		||||
                               last_tb->tc_ptr, last_tb->pc,
 | 
			
		||||
                               lookup_symbol(last_tb->pc));
 | 
			
		||||
        if (cc->synchronize_from_tb) {
 | 
			
		||||
            cc->synchronize_from_tb(cpu, last_tb);
 | 
			
		||||
        } else {
 | 
			
		||||
            assert(cc->set_pc);
 | 
			
		||||
            cc->set_pc(cpu, last_tb->pc);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
/* Execute the code without caching the generated code. An interpreter
 | 
			
		||||
   could be used if available. */
 | 
			
		||||
static void cpu_exec_nocache(CPUState *cpu, int max_cycles,
 | 
			
		||||
                             TranslationBlock *orig_tb, bool ignore_icount)
 | 
			
		||||
{
 | 
			
		||||
    TranslationBlock *tb;
 | 
			
		||||
 | 
			
		||||
    /* Should never happen.
 | 
			
		||||
       We only end up here when an existing TB is too long.  */
 | 
			
		||||
    if (max_cycles > CF_COUNT_MASK)
 | 
			
		||||
        max_cycles = CF_COUNT_MASK;
 | 
			
		||||
 | 
			
		||||
    tb_lock();
 | 
			
		||||
    tb = tb_gen_code(cpu, orig_tb->pc, orig_tb->cs_base, orig_tb->flags,
 | 
			
		||||
                     max_cycles | CF_NOCACHE
 | 
			
		||||
                         | (ignore_icount ? CF_IGNORE_ICOUNT : 0));
 | 
			
		||||
    tb->orig_tb = orig_tb;
 | 
			
		||||
    tb_unlock();
 | 
			
		||||
 | 
			
		||||
    /* execute the generated code */
 | 
			
		||||
    trace_exec_tb_nocache(tb, tb->pc);
 | 
			
		||||
    cpu_tb_exec(cpu, tb);
 | 
			
		||||
 | 
			
		||||
    tb_lock();
 | 
			
		||||
    tb_phys_invalidate(tb, -1);
 | 
			
		||||
    tb_free(tb);
 | 
			
		||||
    tb_unlock();
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static void cpu_exec_step(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
    CPUArchState *env = (CPUArchState *)cpu->env_ptr;
 | 
			
		||||
    TranslationBlock *tb;
 | 
			
		||||
    target_ulong cs_base, pc;
 | 
			
		||||
    uint32_t flags;
 | 
			
		||||
 | 
			
		||||
    cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
 | 
			
		||||
    if (sigsetjmp(cpu->jmp_env, 0) == 0) {
 | 
			
		||||
        mmap_lock();
 | 
			
		||||
        tb_lock();
 | 
			
		||||
        tb = tb_gen_code(cpu, pc, cs_base, flags,
 | 
			
		||||
                         1 | CF_NOCACHE | CF_IGNORE_ICOUNT);
 | 
			
		||||
        tb->orig_tb = NULL;
 | 
			
		||||
        tb_unlock();
 | 
			
		||||
        mmap_unlock();
 | 
			
		||||
 | 
			
		||||
        cc->cpu_exec_enter(cpu);
 | 
			
		||||
        /* execute the generated code */
 | 
			
		||||
        trace_exec_tb_nocache(tb, pc);
 | 
			
		||||
        cpu_tb_exec(cpu, tb);
 | 
			
		||||
        cc->cpu_exec_exit(cpu);
 | 
			
		||||
 | 
			
		||||
        tb_lock();
 | 
			
		||||
        tb_phys_invalidate(tb, -1);
 | 
			
		||||
        tb_free(tb);
 | 
			
		||||
        tb_unlock();
 | 
			
		||||
    } else {
 | 
			
		||||
        /* We may have exited due to another problem here, so we need
 | 
			
		||||
         * to reset any tb_locks we may have taken but didn't release.
 | 
			
		||||
         * The mmap_lock is dropped by tb_gen_code if it runs out of
 | 
			
		||||
         * memory.
 | 
			
		||||
         */
 | 
			
		||||
#ifndef CONFIG_SOFTMMU
 | 
			
		||||
        tcg_debug_assert(!have_mmap_lock());
 | 
			
		||||
#endif
 | 
			
		||||
        tb_lock_reset();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cpu_exec_step_atomic(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    start_exclusive();
 | 
			
		||||
 | 
			
		||||
    /* Since we got here, we know that parallel_cpus must be true.  */
 | 
			
		||||
    parallel_cpus = false;
 | 
			
		||||
    cpu_exec_step(cpu);
 | 
			
		||||
    parallel_cpus = true;
 | 
			
		||||
 | 
			
		||||
    end_exclusive();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct tb_desc {
 | 
			
		||||
    target_ulong pc;
 | 
			
		||||
    target_ulong cs_base;
 | 
			
		||||
    CPUArchState *env;
 | 
			
		||||
    tb_page_addr_t phys_page1;
 | 
			
		||||
    uint32_t flags;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static bool tb_cmp(const void *p, const void *d)
 | 
			
		||||
{
 | 
			
		||||
    const TranslationBlock *tb = p;
 | 
			
		||||
    const struct tb_desc *desc = d;
 | 
			
		||||
 | 
			
		||||
    if (tb->pc == desc->pc &&
 | 
			
		||||
        tb->page_addr[0] == desc->phys_page1 &&
 | 
			
		||||
        tb->cs_base == desc->cs_base &&
 | 
			
		||||
        tb->flags == desc->flags &&
 | 
			
		||||
        !atomic_read(&tb->invalid)) {
 | 
			
		||||
        /* check next page if needed */
 | 
			
		||||
        if (tb->page_addr[1] == -1) {
 | 
			
		||||
            return true;
 | 
			
		||||
        } else {
 | 
			
		||||
            tb_page_addr_t phys_page2;
 | 
			
		||||
            target_ulong virt_page2;
 | 
			
		||||
 | 
			
		||||
            virt_page2 = (desc->pc & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
 | 
			
		||||
            phys_page2 = get_page_addr_code(desc->env, virt_page2);
 | 
			
		||||
            if (tb->page_addr[1] == phys_page2) {
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TranslationBlock *tb_htable_lookup(CPUState *cpu, target_ulong pc,
 | 
			
		||||
                                   target_ulong cs_base, uint32_t flags)
 | 
			
		||||
{
 | 
			
		||||
    tb_page_addr_t phys_pc;
 | 
			
		||||
    struct tb_desc desc;
 | 
			
		||||
    uint32_t h;
 | 
			
		||||
 | 
			
		||||
    desc.env = (CPUArchState *)cpu->env_ptr;
 | 
			
		||||
    desc.cs_base = cs_base;
 | 
			
		||||
    desc.flags = flags;
 | 
			
		||||
    desc.pc = pc;
 | 
			
		||||
    phys_pc = get_page_addr_code(desc.env, pc);
 | 
			
		||||
    desc.phys_page1 = phys_pc & TARGET_PAGE_MASK;
 | 
			
		||||
    h = tb_hash_func(phys_pc, pc, flags);
 | 
			
		||||
    return qht_lookup(&tcg_ctx.tb_ctx.htable, tb_cmp, &desc, h);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline TranslationBlock *tb_find(CPUState *cpu,
 | 
			
		||||
                                        TranslationBlock *last_tb,
 | 
			
		||||
                                        int tb_exit)
 | 
			
		||||
{
 | 
			
		||||
    CPUArchState *env = (CPUArchState *)cpu->env_ptr;
 | 
			
		||||
    TranslationBlock *tb;
 | 
			
		||||
    target_ulong cs_base, pc;
 | 
			
		||||
    uint32_t flags;
 | 
			
		||||
    bool have_tb_lock = false;
 | 
			
		||||
 | 
			
		||||
    /* we record a subset of the CPU state. It will
 | 
			
		||||
       always be the same before a given translated block
 | 
			
		||||
       is executed. */
 | 
			
		||||
    cpu_get_tb_cpu_state(env, &pc, &cs_base, &flags);
 | 
			
		||||
    tb = atomic_rcu_read(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)]);
 | 
			
		||||
    if (unlikely(!tb || tb->pc != pc || tb->cs_base != cs_base ||
 | 
			
		||||
                 tb->flags != flags)) {
 | 
			
		||||
        tb = tb_htable_lookup(cpu, pc, cs_base, flags);
 | 
			
		||||
        if (!tb) {
 | 
			
		||||
 | 
			
		||||
            /* mmap_lock is needed by tb_gen_code, and mmap_lock must be
 | 
			
		||||
             * taken outside tb_lock. As system emulation is currently
 | 
			
		||||
             * single threaded the locks are NOPs.
 | 
			
		||||
             */
 | 
			
		||||
            mmap_lock();
 | 
			
		||||
            tb_lock();
 | 
			
		||||
            have_tb_lock = true;
 | 
			
		||||
 | 
			
		||||
            /* There's a chance that our desired tb has been translated while
 | 
			
		||||
             * taking the locks so we check again inside the lock.
 | 
			
		||||
             */
 | 
			
		||||
            tb = tb_htable_lookup(cpu, pc, cs_base, flags);
 | 
			
		||||
            if (!tb) {
 | 
			
		||||
                /* if no translated code available, then translate it now */
 | 
			
		||||
                tb = tb_gen_code(cpu, pc, cs_base, flags, 0);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            mmap_unlock();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* We add the TB in the virtual pc hash table for the fast lookup */
 | 
			
		||||
        atomic_set(&cpu->tb_jmp_cache[tb_jmp_cache_hash_func(pc)], tb);
 | 
			
		||||
    }
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
    /* We don't take care of direct jumps when address mapping changes in
 | 
			
		||||
     * system emulation. So it's not safe to make a direct jump to a TB
 | 
			
		||||
     * spanning two pages because the mapping for the second page can change.
 | 
			
		||||
     */
 | 
			
		||||
    if (tb->page_addr[1] != -1) {
 | 
			
		||||
        last_tb = NULL;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
    /* See if we can patch the calling TB. */
 | 
			
		||||
    if (last_tb && !qemu_loglevel_mask(CPU_LOG_TB_NOCHAIN)) {
 | 
			
		||||
        if (!have_tb_lock) {
 | 
			
		||||
            tb_lock();
 | 
			
		||||
            have_tb_lock = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (!tb->invalid) {
 | 
			
		||||
            tb_add_jump(last_tb, tb_exit, tb);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (have_tb_lock) {
 | 
			
		||||
        tb_unlock();
 | 
			
		||||
    }
 | 
			
		||||
    return tb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline bool cpu_handle_halt(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    if (cpu->halted) {
 | 
			
		||||
#if defined(TARGET_I386) && !defined(CONFIG_USER_ONLY)
 | 
			
		||||
        if ((cpu->interrupt_request & CPU_INTERRUPT_POLL)
 | 
			
		||||
            && replay_interrupt()) {
 | 
			
		||||
            X86CPU *x86_cpu = X86_CPU(cpu);
 | 
			
		||||
            qemu_mutex_lock_iothread();
 | 
			
		||||
            apic_poll_irq(x86_cpu->apic_state);
 | 
			
		||||
            cpu_reset_interrupt(cpu, CPU_INTERRUPT_POLL);
 | 
			
		||||
            qemu_mutex_unlock_iothread();
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        if (!cpu_has_work(cpu)) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        cpu->halted = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline void cpu_handle_debug_exception(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
    CPUWatchpoint *wp;
 | 
			
		||||
 | 
			
		||||
    if (!cpu->watchpoint_hit) {
 | 
			
		||||
        QTAILQ_FOREACH(wp, &cpu->watchpoints, entry) {
 | 
			
		||||
            wp->flags &= ~BP_WATCHPOINT_HIT;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cc->debug_excp_handler(cpu);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline bool cpu_handle_exception(CPUState *cpu, int *ret)
 | 
			
		||||
{
 | 
			
		||||
    if (cpu->exception_index >= 0) {
 | 
			
		||||
        if (cpu->exception_index >= EXCP_INTERRUPT) {
 | 
			
		||||
            /* exit request from the cpu execution loop */
 | 
			
		||||
            *ret = cpu->exception_index;
 | 
			
		||||
            if (*ret == EXCP_DEBUG) {
 | 
			
		||||
                cpu_handle_debug_exception(cpu);
 | 
			
		||||
            }
 | 
			
		||||
            cpu->exception_index = -1;
 | 
			
		||||
            return true;
 | 
			
		||||
        } else {
 | 
			
		||||
#if defined(CONFIG_USER_ONLY)
 | 
			
		||||
            /* if user mode only, we simulate a fake exception
 | 
			
		||||
               which will be handled outside the cpu execution
 | 
			
		||||
               loop */
 | 
			
		||||
#if defined(TARGET_I386)
 | 
			
		||||
            CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
            cc->do_interrupt(cpu);
 | 
			
		||||
#endif
 | 
			
		||||
            *ret = cpu->exception_index;
 | 
			
		||||
            cpu->exception_index = -1;
 | 
			
		||||
            return true;
 | 
			
		||||
#else
 | 
			
		||||
            if (replay_exception()) {
 | 
			
		||||
                CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
                qemu_mutex_lock_iothread();
 | 
			
		||||
                cc->do_interrupt(cpu);
 | 
			
		||||
                qemu_mutex_unlock_iothread();
 | 
			
		||||
                cpu->exception_index = -1;
 | 
			
		||||
            } else if (!replay_has_interrupt()) {
 | 
			
		||||
                /* give a chance to iothread in replay mode */
 | 
			
		||||
                *ret = EXCP_INTERRUPT;
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
#endif
 | 
			
		||||
        }
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
    } else if (replay_has_exception()
 | 
			
		||||
               && cpu->icount_decr.u16.low + cpu->icount_extra == 0) {
 | 
			
		||||
        /* try to cause an exception pending in the log */
 | 
			
		||||
        cpu_exec_nocache(cpu, 1, tb_find(cpu, NULL, 0), true);
 | 
			
		||||
        *ret = -1;
 | 
			
		||||
        return true;
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline bool cpu_handle_interrupt(CPUState *cpu,
 | 
			
		||||
                                        TranslationBlock **last_tb)
 | 
			
		||||
{
 | 
			
		||||
    CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
 | 
			
		||||
    if (unlikely(atomic_read(&cpu->interrupt_request))) {
 | 
			
		||||
        int interrupt_request;
 | 
			
		||||
        qemu_mutex_lock_iothread();
 | 
			
		||||
        interrupt_request = cpu->interrupt_request;
 | 
			
		||||
        if (unlikely(cpu->singlestep_enabled & SSTEP_NOIRQ)) {
 | 
			
		||||
            /* Mask out external interrupts for this step. */
 | 
			
		||||
            interrupt_request &= ~CPU_INTERRUPT_SSTEP_MASK;
 | 
			
		||||
        }
 | 
			
		||||
        if (interrupt_request & CPU_INTERRUPT_DEBUG) {
 | 
			
		||||
            cpu->interrupt_request &= ~CPU_INTERRUPT_DEBUG;
 | 
			
		||||
            cpu->exception_index = EXCP_DEBUG;
 | 
			
		||||
            qemu_mutex_unlock_iothread();
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        if (replay_mode == REPLAY_MODE_PLAY && !replay_has_interrupt()) {
 | 
			
		||||
            /* Do nothing */
 | 
			
		||||
        } else if (interrupt_request & CPU_INTERRUPT_HALT) {
 | 
			
		||||
            replay_interrupt();
 | 
			
		||||
            cpu->interrupt_request &= ~CPU_INTERRUPT_HALT;
 | 
			
		||||
            cpu->halted = 1;
 | 
			
		||||
            cpu->exception_index = EXCP_HLT;
 | 
			
		||||
            qemu_mutex_unlock_iothread();
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
#if defined(TARGET_I386)
 | 
			
		||||
        else if (interrupt_request & CPU_INTERRUPT_INIT) {
 | 
			
		||||
            X86CPU *x86_cpu = X86_CPU(cpu);
 | 
			
		||||
            CPUArchState *env = &x86_cpu->env;
 | 
			
		||||
            replay_interrupt();
 | 
			
		||||
            cpu_svm_check_intercept_param(env, SVM_EXIT_INIT, 0, 0);
 | 
			
		||||
            do_cpu_init(x86_cpu);
 | 
			
		||||
            cpu->exception_index = EXCP_HALTED;
 | 
			
		||||
            qemu_mutex_unlock_iothread();
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
#else
 | 
			
		||||
        else if (interrupt_request & CPU_INTERRUPT_RESET) {
 | 
			
		||||
            replay_interrupt();
 | 
			
		||||
            cpu_reset(cpu);
 | 
			
		||||
            qemu_mutex_unlock_iothread();
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        /* The target hook has 3 exit conditions:
 | 
			
		||||
           False when the interrupt isn't processed,
 | 
			
		||||
           True when it is, and we should restart on a new TB,
 | 
			
		||||
           and via longjmp via cpu_loop_exit.  */
 | 
			
		||||
        else {
 | 
			
		||||
            if (cc->cpu_exec_interrupt(cpu, interrupt_request)) {
 | 
			
		||||
                replay_interrupt();
 | 
			
		||||
                *last_tb = NULL;
 | 
			
		||||
            }
 | 
			
		||||
            /* The target hook may have updated the 'cpu->interrupt_request';
 | 
			
		||||
             * reload the 'interrupt_request' value */
 | 
			
		||||
            interrupt_request = cpu->interrupt_request;
 | 
			
		||||
        }
 | 
			
		||||
        if (interrupt_request & CPU_INTERRUPT_EXITTB) {
 | 
			
		||||
            cpu->interrupt_request &= ~CPU_INTERRUPT_EXITTB;
 | 
			
		||||
            /* ensure that no TB jump will be modified as
 | 
			
		||||
               the program flow was changed */
 | 
			
		||||
            *last_tb = NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* If we exit via cpu_loop_exit/longjmp it is reset in cpu_exec */
 | 
			
		||||
        qemu_mutex_unlock_iothread();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Finally, check if we need to exit to the main loop.  */
 | 
			
		||||
    if (unlikely(atomic_read(&cpu->exit_request)
 | 
			
		||||
        || (use_icount && cpu->icount_decr.u16.low + cpu->icount_extra == 0))) {
 | 
			
		||||
        atomic_set(&cpu->exit_request, 0);
 | 
			
		||||
        cpu->exception_index = EXCP_INTERRUPT;
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline void cpu_loop_exec_tb(CPUState *cpu, TranslationBlock *tb,
 | 
			
		||||
                                    TranslationBlock **last_tb, int *tb_exit)
 | 
			
		||||
{
 | 
			
		||||
    uintptr_t ret;
 | 
			
		||||
    int32_t insns_left;
 | 
			
		||||
 | 
			
		||||
    trace_exec_tb(tb, tb->pc);
 | 
			
		||||
    ret = cpu_tb_exec(cpu, tb);
 | 
			
		||||
    tb = (TranslationBlock *)(ret & ~TB_EXIT_MASK);
 | 
			
		||||
    *tb_exit = ret & TB_EXIT_MASK;
 | 
			
		||||
    if (*tb_exit != TB_EXIT_REQUESTED) {
 | 
			
		||||
        *last_tb = tb;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    *last_tb = NULL;
 | 
			
		||||
    insns_left = atomic_read(&cpu->icount_decr.u32);
 | 
			
		||||
    atomic_set(&cpu->icount_decr.u16.high, 0);
 | 
			
		||||
    if (insns_left < 0) {
 | 
			
		||||
        /* Something asked us to stop executing chained TBs; just
 | 
			
		||||
         * continue round the main loop. Whatever requested the exit
 | 
			
		||||
         * will also have set something else (eg exit_request or
 | 
			
		||||
         * interrupt_request) which we will handle next time around
 | 
			
		||||
         * the loop.  But we need to ensure the zeroing of icount_decr
 | 
			
		||||
         * comes before the next read of cpu->exit_request
 | 
			
		||||
         * or cpu->interrupt_request.
 | 
			
		||||
         */
 | 
			
		||||
        smp_mb();
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Instruction counter expired.  */
 | 
			
		||||
    assert(use_icount);
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
    /* Ensure global icount has gone forward */
 | 
			
		||||
    cpu_update_icount(cpu);
 | 
			
		||||
    /* Refill decrementer and continue execution.  */
 | 
			
		||||
    insns_left = MIN(0xffff, cpu->icount_budget);
 | 
			
		||||
    cpu->icount_decr.u16.low = insns_left;
 | 
			
		||||
    cpu->icount_extra = cpu->icount_budget - insns_left;
 | 
			
		||||
    if (!cpu->icount_extra) {
 | 
			
		||||
        /* Execute any remaining instructions, then let the main loop
 | 
			
		||||
         * handle the next event.
 | 
			
		||||
         */
 | 
			
		||||
        if (insns_left > 0) {
 | 
			
		||||
            cpu_exec_nocache(cpu, insns_left, tb, false);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* main execution loop */
 | 
			
		||||
 | 
			
		||||
int cpu_exec(CPUState *cpu)
 | 
			
		||||
{
 | 
			
		||||
    CPUClass *cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
    int ret;
 | 
			
		||||
    SyncClocks sc = { 0 };
 | 
			
		||||
 | 
			
		||||
    /* replay_interrupt may need current_cpu */
 | 
			
		||||
    current_cpu = cpu;
 | 
			
		||||
 | 
			
		||||
    if (cpu_handle_halt(cpu)) {
 | 
			
		||||
        return EXCP_HALTED;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    rcu_read_lock();
 | 
			
		||||
 | 
			
		||||
    cc->cpu_exec_enter(cpu);
 | 
			
		||||
 | 
			
		||||
    /* Calculate difference between guest clock and host clock.
 | 
			
		||||
     * This delay includes the delay of the last cycle, so
 | 
			
		||||
     * what we have to do is sleep until it is 0. As for the
 | 
			
		||||
     * advance/delay we gain here, we try to fix it next time.
 | 
			
		||||
     */
 | 
			
		||||
    init_delay_params(&sc, cpu);
 | 
			
		||||
 | 
			
		||||
    /* prepare setjmp context for exception handling */
 | 
			
		||||
    if (sigsetjmp(cpu->jmp_env, 0) != 0) {
 | 
			
		||||
#if defined(__clang__) || !QEMU_GNUC_PREREQ(4, 6)
 | 
			
		||||
        /* Some compilers wrongly smash all local variables after
 | 
			
		||||
         * siglongjmp. There were bug reports for gcc 4.5.0 and clang.
 | 
			
		||||
         * Reload essential local variables here for those compilers.
 | 
			
		||||
         * Newer versions of gcc would complain about this code (-Wclobbered). */
 | 
			
		||||
        cpu = current_cpu;
 | 
			
		||||
        cc = CPU_GET_CLASS(cpu);
 | 
			
		||||
#else /* buggy compiler */
 | 
			
		||||
        /* Assert that the compiler does not smash local variables. */
 | 
			
		||||
        g_assert(cpu == current_cpu);
 | 
			
		||||
        g_assert(cc == CPU_GET_CLASS(cpu));
 | 
			
		||||
#endif /* buggy compiler */
 | 
			
		||||
        cpu->can_do_io = 1;
 | 
			
		||||
        tb_lock_reset();
 | 
			
		||||
        if (qemu_mutex_iothread_locked()) {
 | 
			
		||||
            qemu_mutex_unlock_iothread();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* if an exception is pending, we execute it here */
 | 
			
		||||
    while (!cpu_handle_exception(cpu, &ret)) {
 | 
			
		||||
        TranslationBlock *last_tb = NULL;
 | 
			
		||||
        int tb_exit = 0;
 | 
			
		||||
 | 
			
		||||
        while (!cpu_handle_interrupt(cpu, &last_tb)) {
 | 
			
		||||
            TranslationBlock *tb = tb_find(cpu, last_tb, tb_exit);
 | 
			
		||||
            cpu_loop_exec_tb(cpu, tb, &last_tb, &tb_exit);
 | 
			
		||||
            /* Try to align the host and virtual clocks
 | 
			
		||||
               if the guest is in advance */
 | 
			
		||||
            align_clocks(&sc, cpu);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cc->cpu_exec_exit(cpu);
 | 
			
		||||
    rcu_read_unlock();
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1051
									
								
								accel/tcg/cputlb.c
									
									
									
									
									
								
							
							
						
						
									
										1051
									
								
								accel/tcg/cputlb.c
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,61 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU System Emulator, accelerator interfaces
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2003-2008 Fabrice Bellard
 | 
			
		||||
 * Copyright (c) 2014 Red Hat Inc.
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 | 
			
		||||
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/accel.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "qom/object.h"
 | 
			
		||||
 | 
			
		||||
int tcg_tb_size;
 | 
			
		||||
static bool tcg_allowed = true;
 | 
			
		||||
 | 
			
		||||
static int tcg_init(MachineState *ms)
 | 
			
		||||
{
 | 
			
		||||
    tcg_exec_init(tcg_tb_size * 1024 * 1024);
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void tcg_accel_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    AccelClass *ac = ACCEL_CLASS(oc);
 | 
			
		||||
    ac->name = "tcg";
 | 
			
		||||
    ac->init_machine = tcg_init;
 | 
			
		||||
    ac->allowed = &tcg_allowed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define TYPE_TCG_ACCEL ACCEL_CLASS_NAME("tcg")
 | 
			
		||||
 | 
			
		||||
static const TypeInfo tcg_accel_type = {
 | 
			
		||||
    .name = TYPE_TCG_ACCEL,
 | 
			
		||||
    .parent = TYPE_ACCEL,
 | 
			
		||||
    .class_init = tcg_accel_class_init,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void register_accel_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&tcg_accel_type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(register_accel_types);
 | 
			
		||||
@@ -1,10 +0,0 @@
 | 
			
		||||
# Trace events for debugging and performance instrumentation
 | 
			
		||||
 | 
			
		||||
# TCG related tracing (mostly disabled by default)
 | 
			
		||||
# cpu-exec.c
 | 
			
		||||
disable exec_tb(void *tb, uintptr_t pc) "tb:%p pc=0x%"PRIxPTR
 | 
			
		||||
disable exec_tb_nocache(void *tb, uintptr_t pc) "tb:%p pc=0x%"PRIxPTR
 | 
			
		||||
disable exec_tb_exit(void *last_tb, unsigned int flags) "tb:%p flags=%x"
 | 
			
		||||
 | 
			
		||||
# translate-all.c
 | 
			
		||||
translate_block(void *tb, uintptr_t pc, uint8_t *tb_code) "tb:%p, pc:0x%"PRIxPTR", tb_code:%p"
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -1,36 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 *  Translated block handling
 | 
			
		||||
 *
 | 
			
		||||
 *  Copyright (c) 2003 Fabrice Bellard
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
#ifndef TRANSLATE_ALL_H
 | 
			
		||||
#define TRANSLATE_ALL_H
 | 
			
		||||
 | 
			
		||||
#include "exec/exec-all.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/* translate-all.c */
 | 
			
		||||
void tb_invalidate_phys_page_fast(tb_page_addr_t start, int len);
 | 
			
		||||
void tb_invalidate_phys_page_range(tb_page_addr_t start, tb_page_addr_t end,
 | 
			
		||||
                                   int is_cpu_write_access);
 | 
			
		||||
void tb_invalidate_phys_range(tb_page_addr_t start, tb_page_addr_t end);
 | 
			
		||||
void tb_check_watchpoint(CPUState *cpu);
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_USER_ONLY
 | 
			
		||||
int page_unprotect(target_ulong address, uintptr_t pc);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* TRANSLATE_ALL_H */
 | 
			
		||||
@@ -1,56 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 *  Host code generation common components
 | 
			
		||||
 *
 | 
			
		||||
 *  Copyright (c) 2015 Peter Crosthwaite <crosthwaite.peter@gmail.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qom/cpu.h"
 | 
			
		||||
#include "sysemu/cpus.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
 | 
			
		||||
uintptr_t qemu_real_host_page_size;
 | 
			
		||||
intptr_t qemu_real_host_page_mask;
 | 
			
		||||
 | 
			
		||||
#ifndef CONFIG_USER_ONLY
 | 
			
		||||
/* mask must never be zero, except for A20 change call */
 | 
			
		||||
static void tcg_handle_interrupt(CPUState *cpu, int mask)
 | 
			
		||||
{
 | 
			
		||||
    int old_mask;
 | 
			
		||||
    g_assert(qemu_mutex_iothread_locked());
 | 
			
		||||
 | 
			
		||||
    old_mask = cpu->interrupt_request;
 | 
			
		||||
    cpu->interrupt_request |= mask;
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * If called from iothread context, wake the target cpu in
 | 
			
		||||
     * case its halted.
 | 
			
		||||
     */
 | 
			
		||||
    if (!qemu_cpu_is_self(cpu)) {
 | 
			
		||||
        qemu_cpu_kick(cpu);
 | 
			
		||||
    } else {
 | 
			
		||||
        cpu->icount_decr.u16.high = -1;
 | 
			
		||||
        if (use_icount &&
 | 
			
		||||
            !cpu->can_do_io
 | 
			
		||||
            && (mask & ~old_mask) != 0) {
 | 
			
		||||
            cpu_abort(cpu, "Raised interrupt while not in I/O function");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
CPUInterruptHandler cpu_interrupt_handler = tcg_handle_interrupt;
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										269
									
								
								aio-posix.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										269
									
								
								aio-posix.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,269 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU aio implementation
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright IBM, Corp. 2008
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *  Anthony Liguori   <aliguori@us.ibm.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
			
		||||
 * the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 * Contributions after 2012-01-13 are licensed under the terms of the
 | 
			
		||||
 * GNU GPL, version 2 or (at your option) any later version.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block.h"
 | 
			
		||||
#include "qemu/queue.h"
 | 
			
		||||
#include "qemu/sockets.h"
 | 
			
		||||
 | 
			
		||||
struct AioHandler
 | 
			
		||||
{
 | 
			
		||||
    GPollFD pfd;
 | 
			
		||||
    IOHandler *io_read;
 | 
			
		||||
    IOHandler *io_write;
 | 
			
		||||
    AioFlushHandler *io_flush;
 | 
			
		||||
    int deleted;
 | 
			
		||||
    void *opaque;
 | 
			
		||||
    QLIST_ENTRY(AioHandler) node;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static AioHandler *find_aio_handler(AioContext *ctx, int fd)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        if (node->pfd.fd == fd)
 | 
			
		||||
            if (!node->deleted)
 | 
			
		||||
                return node;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void aio_set_fd_handler(AioContext *ctx,
 | 
			
		||||
                        int fd,
 | 
			
		||||
                        IOHandler *io_read,
 | 
			
		||||
                        IOHandler *io_write,
 | 
			
		||||
                        AioFlushHandler *io_flush,
 | 
			
		||||
                        void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
 | 
			
		||||
    node = find_aio_handler(ctx, fd);
 | 
			
		||||
 | 
			
		||||
    /* Are we deleting the fd handler? */
 | 
			
		||||
    if (!io_read && !io_write) {
 | 
			
		||||
        if (node) {
 | 
			
		||||
            g_source_remove_poll(&ctx->source, &node->pfd);
 | 
			
		||||
 | 
			
		||||
            /* If the lock is held, just mark the node as deleted */
 | 
			
		||||
            if (ctx->walking_handlers) {
 | 
			
		||||
                node->deleted = 1;
 | 
			
		||||
                node->pfd.revents = 0;
 | 
			
		||||
            } else {
 | 
			
		||||
                /* Otherwise, delete it for real.  We can't just mark it as
 | 
			
		||||
                 * deleted because deleted nodes are only cleaned up after
 | 
			
		||||
                 * releasing the walking_handlers lock.
 | 
			
		||||
                 */
 | 
			
		||||
                QLIST_REMOVE(node, node);
 | 
			
		||||
                g_free(node);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        if (node == NULL) {
 | 
			
		||||
            /* Alloc and insert if it's not already there */
 | 
			
		||||
            node = g_malloc0(sizeof(AioHandler));
 | 
			
		||||
            node->pfd.fd = fd;
 | 
			
		||||
            QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
 | 
			
		||||
 | 
			
		||||
            g_source_add_poll(&ctx->source, &node->pfd);
 | 
			
		||||
        }
 | 
			
		||||
        /* Update handler with latest information */
 | 
			
		||||
        node->io_read = io_read;
 | 
			
		||||
        node->io_write = io_write;
 | 
			
		||||
        node->io_flush = io_flush;
 | 
			
		||||
        node->opaque = opaque;
 | 
			
		||||
 | 
			
		||||
        node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP : 0);
 | 
			
		||||
        node->pfd.events |= (io_write ? G_IO_OUT : 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_notify(ctx);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void aio_set_event_notifier(AioContext *ctx,
 | 
			
		||||
                            EventNotifier *notifier,
 | 
			
		||||
                            EventNotifierHandler *io_read,
 | 
			
		||||
                            AioFlushEventNotifierHandler *io_flush)
 | 
			
		||||
{
 | 
			
		||||
    aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
 | 
			
		||||
                       (IOHandler *)io_read, NULL,
 | 
			
		||||
                       (AioFlushHandler *)io_flush, notifier);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool aio_pending(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        int revents;
 | 
			
		||||
 | 
			
		||||
        /*
 | 
			
		||||
         * FIXME: right now we cannot get G_IO_HUP and G_IO_ERR because
 | 
			
		||||
         * main-loop.c is still select based (due to the slirp legacy).
 | 
			
		||||
         * If main-loop.c ever switches to poll, G_IO_ERR should be
 | 
			
		||||
         * tested too.  Dispatching G_IO_ERR to both handlers should be
 | 
			
		||||
         * okay, since handlers need to be ready for spurious wakeups.
 | 
			
		||||
         */
 | 
			
		||||
        revents = node->pfd.revents & node->pfd.events;
 | 
			
		||||
        if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
{
 | 
			
		||||
    static struct timeval tv0;
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
    fd_set rdfds, wrfds;
 | 
			
		||||
    int max_fd = -1;
 | 
			
		||||
    int ret;
 | 
			
		||||
    bool busy, progress;
 | 
			
		||||
 | 
			
		||||
    progress = false;
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * If there are callbacks left that have been queued, we need to call then.
 | 
			
		||||
     * Do not call select in this case, because it is possible that the caller
 | 
			
		||||
     * does not need a complete flush (as is the case for qemu_aio_wait loops).
 | 
			
		||||
     */
 | 
			
		||||
    if (aio_bh_poll(ctx)) {
 | 
			
		||||
        blocking = false;
 | 
			
		||||
        progress = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Then dispatch any pending callbacks from the GSource.
 | 
			
		||||
     *
 | 
			
		||||
     * We have to walk very carefully in case qemu_aio_set_fd_handler is
 | 
			
		||||
     * called while we're walking.
 | 
			
		||||
     */
 | 
			
		||||
    node = QLIST_FIRST(&ctx->aio_handlers);
 | 
			
		||||
    while (node) {
 | 
			
		||||
        AioHandler *tmp;
 | 
			
		||||
        int revents;
 | 
			
		||||
 | 
			
		||||
        ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
        revents = node->pfd.revents & node->pfd.events;
 | 
			
		||||
        node->pfd.revents = 0;
 | 
			
		||||
 | 
			
		||||
        /* See comment in aio_pending.  */
 | 
			
		||||
        if (revents & (G_IO_IN | G_IO_HUP | G_IO_ERR) && node->io_read) {
 | 
			
		||||
            node->io_read(node->opaque);
 | 
			
		||||
            progress = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (revents & (G_IO_OUT | G_IO_ERR) && node->io_write) {
 | 
			
		||||
            node->io_write(node->opaque);
 | 
			
		||||
            progress = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        tmp = node;
 | 
			
		||||
        node = QLIST_NEXT(node, node);
 | 
			
		||||
 | 
			
		||||
        ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
        if (!ctx->walking_handlers && tmp->deleted) {
 | 
			
		||||
            QLIST_REMOVE(tmp, node);
 | 
			
		||||
            g_free(tmp);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (progress && !blocking) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
    FD_ZERO(&rdfds);
 | 
			
		||||
    FD_ZERO(&wrfds);
 | 
			
		||||
 | 
			
		||||
    /* fill fd sets */
 | 
			
		||||
    busy = false;
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        /* If there aren't pending AIO operations, don't invoke callbacks.
 | 
			
		||||
         * Otherwise, if there are no AIO requests, qemu_aio_wait() would
 | 
			
		||||
         * wait indefinitely.
 | 
			
		||||
         */
 | 
			
		||||
        if (!node->deleted && node->io_flush) {
 | 
			
		||||
            if (node->io_flush(node->opaque) == 0) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            busy = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (!node->deleted && node->io_read) {
 | 
			
		||||
            FD_SET(node->pfd.fd, &rdfds);
 | 
			
		||||
            max_fd = MAX(max_fd, node->pfd.fd + 1);
 | 
			
		||||
        }
 | 
			
		||||
        if (!node->deleted && node->io_write) {
 | 
			
		||||
            FD_SET(node->pfd.fd, &wrfds);
 | 
			
		||||
            max_fd = MAX(max_fd, node->pfd.fd + 1);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
    /* No AIO operations?  Get us out of here */
 | 
			
		||||
    if (!busy) {
 | 
			
		||||
        return progress;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* wait until next event */
 | 
			
		||||
    ret = select(max_fd, &rdfds, &wrfds, NULL, blocking ? NULL : &tv0);
 | 
			
		||||
 | 
			
		||||
    /* if we have any readable fds, dispatch event */
 | 
			
		||||
    if (ret > 0) {
 | 
			
		||||
        /* we have to walk very carefully in case
 | 
			
		||||
         * qemu_aio_set_fd_handler is called while we're walking */
 | 
			
		||||
        node = QLIST_FIRST(&ctx->aio_handlers);
 | 
			
		||||
        while (node) {
 | 
			
		||||
            AioHandler *tmp;
 | 
			
		||||
 | 
			
		||||
            ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
            if (!node->deleted &&
 | 
			
		||||
                FD_ISSET(node->pfd.fd, &rdfds) &&
 | 
			
		||||
                node->io_read) {
 | 
			
		||||
                node->io_read(node->opaque);
 | 
			
		||||
                progress = true;
 | 
			
		||||
            }
 | 
			
		||||
            if (!node->deleted &&
 | 
			
		||||
                FD_ISSET(node->pfd.fd, &wrfds) &&
 | 
			
		||||
                node->io_write) {
 | 
			
		||||
                node->io_write(node->opaque);
 | 
			
		||||
                progress = true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            tmp = node;
 | 
			
		||||
            node = QLIST_NEXT(node, node);
 | 
			
		||||
 | 
			
		||||
            ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
            if (!ctx->walking_handlers && tmp->deleted) {
 | 
			
		||||
                QLIST_REMOVE(tmp, node);
 | 
			
		||||
                g_free(tmp);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert(progress || busy);
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										219
									
								
								aio-win32.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										219
									
								
								aio-win32.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,219 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU aio implementation
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright IBM Corp., 2008
 | 
			
		||||
 * Copyright Red Hat Inc., 2012
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *  Anthony Liguori   <aliguori@us.ibm.com>
 | 
			
		||||
 *  Paolo Bonzini     <pbonzini@redhat.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
			
		||||
 * the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 * Contributions after 2012-01-13 are licensed under the terms of the
 | 
			
		||||
 * GNU GPL, version 2 or (at your option) any later version.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block.h"
 | 
			
		||||
#include "qemu/queue.h"
 | 
			
		||||
#include "qemu/sockets.h"
 | 
			
		||||
 | 
			
		||||
struct AioHandler {
 | 
			
		||||
    EventNotifier *e;
 | 
			
		||||
    EventNotifierHandler *io_notify;
 | 
			
		||||
    AioFlushEventNotifierHandler *io_flush;
 | 
			
		||||
    GPollFD pfd;
 | 
			
		||||
    int deleted;
 | 
			
		||||
    QLIST_ENTRY(AioHandler) node;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void aio_set_event_notifier(AioContext *ctx,
 | 
			
		||||
                            EventNotifier *e,
 | 
			
		||||
                            EventNotifierHandler *io_notify,
 | 
			
		||||
                            AioFlushEventNotifierHandler *io_flush)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        if (node->e == e && !node->deleted) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Are we deleting the fd handler? */
 | 
			
		||||
    if (!io_notify) {
 | 
			
		||||
        if (node) {
 | 
			
		||||
            g_source_remove_poll(&ctx->source, &node->pfd);
 | 
			
		||||
 | 
			
		||||
            /* If the lock is held, just mark the node as deleted */
 | 
			
		||||
            if (ctx->walking_handlers) {
 | 
			
		||||
                node->deleted = 1;
 | 
			
		||||
                node->pfd.revents = 0;
 | 
			
		||||
            } else {
 | 
			
		||||
                /* Otherwise, delete it for real.  We can't just mark it as
 | 
			
		||||
                 * deleted because deleted nodes are only cleaned up after
 | 
			
		||||
                 * releasing the walking_handlers lock.
 | 
			
		||||
                 */
 | 
			
		||||
                QLIST_REMOVE(node, node);
 | 
			
		||||
                g_free(node);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        if (node == NULL) {
 | 
			
		||||
            /* Alloc and insert if it's not already there */
 | 
			
		||||
            node = g_malloc0(sizeof(AioHandler));
 | 
			
		||||
            node->e = e;
 | 
			
		||||
            node->pfd.fd = (uintptr_t)event_notifier_get_handle(e);
 | 
			
		||||
            node->pfd.events = G_IO_IN;
 | 
			
		||||
            QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
 | 
			
		||||
 | 
			
		||||
            g_source_add_poll(&ctx->source, &node->pfd);
 | 
			
		||||
        }
 | 
			
		||||
        /* Update handler with latest information */
 | 
			
		||||
        node->io_notify = io_notify;
 | 
			
		||||
        node->io_flush = io_flush;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    aio_notify(ctx);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool aio_pending(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        if (node->pfd.revents && node->io_notify) {
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool aio_poll(AioContext *ctx, bool blocking)
 | 
			
		||||
{
 | 
			
		||||
    AioHandler *node;
 | 
			
		||||
    HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
 | 
			
		||||
    bool busy, progress;
 | 
			
		||||
    int count;
 | 
			
		||||
 | 
			
		||||
    progress = false;
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * If there are callbacks left that have been queued, we need to call then.
 | 
			
		||||
     * Do not call select in this case, because it is possible that the caller
 | 
			
		||||
     * does not need a complete flush (as is the case for qemu_aio_wait loops).
 | 
			
		||||
     */
 | 
			
		||||
    if (aio_bh_poll(ctx)) {
 | 
			
		||||
        blocking = false;
 | 
			
		||||
        progress = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Then dispatch any pending callbacks from the GSource.
 | 
			
		||||
     *
 | 
			
		||||
     * We have to walk very carefully in case qemu_aio_set_fd_handler is
 | 
			
		||||
     * called while we're walking.
 | 
			
		||||
     */
 | 
			
		||||
    node = QLIST_FIRST(&ctx->aio_handlers);
 | 
			
		||||
    while (node) {
 | 
			
		||||
        AioHandler *tmp;
 | 
			
		||||
 | 
			
		||||
        ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
        if (node->pfd.revents && node->io_notify) {
 | 
			
		||||
            node->pfd.revents = 0;
 | 
			
		||||
            node->io_notify(node->e);
 | 
			
		||||
            progress = true;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        tmp = node;
 | 
			
		||||
        node = QLIST_NEXT(node, node);
 | 
			
		||||
 | 
			
		||||
        ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
        if (!ctx->walking_handlers && tmp->deleted) {
 | 
			
		||||
            QLIST_REMOVE(tmp, node);
 | 
			
		||||
            g_free(tmp);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (progress && !blocking) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
    /* fill fd sets */
 | 
			
		||||
    busy = false;
 | 
			
		||||
    count = 0;
 | 
			
		||||
    QLIST_FOREACH(node, &ctx->aio_handlers, node) {
 | 
			
		||||
        /* If there aren't pending AIO operations, don't invoke callbacks.
 | 
			
		||||
         * Otherwise, if there are no AIO requests, qemu_aio_wait() would
 | 
			
		||||
         * wait indefinitely.
 | 
			
		||||
         */
 | 
			
		||||
        if (!node->deleted && node->io_flush) {
 | 
			
		||||
            if (node->io_flush(node->e) == 0) {
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            busy = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (!node->deleted && node->io_notify) {
 | 
			
		||||
            events[count++] = event_notifier_get_handle(node->e);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
    /* No AIO operations?  Get us out of here */
 | 
			
		||||
    if (!busy) {
 | 
			
		||||
        return progress;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* wait until next event */
 | 
			
		||||
    while (count > 0) {
 | 
			
		||||
        int timeout = blocking ? INFINITE : 0;
 | 
			
		||||
        int ret = WaitForMultipleObjects(count, events, FALSE, timeout);
 | 
			
		||||
 | 
			
		||||
        /* if we have any signaled events, dispatch event */
 | 
			
		||||
        if ((DWORD) (ret - WAIT_OBJECT_0) >= count) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        blocking = false;
 | 
			
		||||
 | 
			
		||||
        /* we have to walk very carefully in case
 | 
			
		||||
         * qemu_aio_set_fd_handler is called while we're walking */
 | 
			
		||||
        node = QLIST_FIRST(&ctx->aio_handlers);
 | 
			
		||||
        while (node) {
 | 
			
		||||
            AioHandler *tmp;
 | 
			
		||||
 | 
			
		||||
            ctx->walking_handlers++;
 | 
			
		||||
 | 
			
		||||
            if (!node->deleted &&
 | 
			
		||||
                event_notifier_get_handle(node->e) == events[ret - WAIT_OBJECT_0] &&
 | 
			
		||||
                node->io_notify) {
 | 
			
		||||
                node->io_notify(node->e);
 | 
			
		||||
                progress = true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            tmp = node;
 | 
			
		||||
            node = QLIST_NEXT(node, node);
 | 
			
		||||
 | 
			
		||||
            ctx->walking_handlers--;
 | 
			
		||||
 | 
			
		||||
            if (!ctx->walking_handlers && tmp->deleted) {
 | 
			
		||||
                QLIST_REMOVE(tmp, node);
 | 
			
		||||
                g_free(tmp);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* Try again, but only call each handler once.  */
 | 
			
		||||
        events[ret - WAIT_OBJECT_0] = events[--count];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert(progress || busy);
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1079
									
								
								arch_init.c
									
									
									
									
									
								
							
							
						
						
									
										1079
									
								
								arch_init.c
									
									
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										217
									
								
								async.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										217
									
								
								async.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,217 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU System Emulator
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2003-2008 Fabrice Bellard
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 | 
			
		||||
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/aio.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
 | 
			
		||||
/***********************************************************/
 | 
			
		||||
/* bottom halves (can be seen as timers which expire ASAP) */
 | 
			
		||||
 | 
			
		||||
struct QEMUBH {
 | 
			
		||||
    AioContext *ctx;
 | 
			
		||||
    QEMUBHFunc *cb;
 | 
			
		||||
    void *opaque;
 | 
			
		||||
    QEMUBH *next;
 | 
			
		||||
    bool scheduled;
 | 
			
		||||
    bool idle;
 | 
			
		||||
    bool deleted;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
    bh = g_malloc0(sizeof(QEMUBH));
 | 
			
		||||
    bh->ctx = ctx;
 | 
			
		||||
    bh->cb = cb;
 | 
			
		||||
    bh->opaque = opaque;
 | 
			
		||||
    bh->next = ctx->first_bh;
 | 
			
		||||
    ctx->first_bh = bh;
 | 
			
		||||
    return bh;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int aio_bh_poll(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    QEMUBH *bh, **bhp, *next;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    ctx->walking_bh++;
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
    for (bh = ctx->first_bh; bh; bh = next) {
 | 
			
		||||
        next = bh->next;
 | 
			
		||||
        if (!bh->deleted && bh->scheduled) {
 | 
			
		||||
            bh->scheduled = 0;
 | 
			
		||||
            if (!bh->idle)
 | 
			
		||||
                ret = 1;
 | 
			
		||||
            bh->idle = 0;
 | 
			
		||||
            bh->cb(bh->opaque);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ctx->walking_bh--;
 | 
			
		||||
 | 
			
		||||
    /* remove deleted bhs */
 | 
			
		||||
    if (!ctx->walking_bh) {
 | 
			
		||||
        bhp = &ctx->first_bh;
 | 
			
		||||
        while (*bhp) {
 | 
			
		||||
            bh = *bhp;
 | 
			
		||||
            if (bh->deleted) {
 | 
			
		||||
                *bhp = bh->next;
 | 
			
		||||
                g_free(bh);
 | 
			
		||||
            } else {
 | 
			
		||||
                bhp = &bh->next;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qemu_bh_schedule_idle(QEMUBH *bh)
 | 
			
		||||
{
 | 
			
		||||
    if (bh->scheduled)
 | 
			
		||||
        return;
 | 
			
		||||
    bh->scheduled = 1;
 | 
			
		||||
    bh->idle = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qemu_bh_schedule(QEMUBH *bh)
 | 
			
		||||
{
 | 
			
		||||
    if (bh->scheduled)
 | 
			
		||||
        return;
 | 
			
		||||
    bh->scheduled = 1;
 | 
			
		||||
    bh->idle = 0;
 | 
			
		||||
    aio_notify(bh->ctx);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qemu_bh_cancel(QEMUBH *bh)
 | 
			
		||||
{
 | 
			
		||||
    bh->scheduled = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qemu_bh_delete(QEMUBH *bh)
 | 
			
		||||
{
 | 
			
		||||
    bh->scheduled = 0;
 | 
			
		||||
    bh->deleted = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static gboolean
 | 
			
		||||
aio_ctx_prepare(GSource *source, gint    *timeout)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *ctx = (AioContext *) source;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
 | 
			
		||||
    for (bh = ctx->first_bh; bh; bh = bh->next) {
 | 
			
		||||
        if (!bh->deleted && bh->scheduled) {
 | 
			
		||||
            if (bh->idle) {
 | 
			
		||||
                /* idle bottom halves will be polled at least
 | 
			
		||||
                 * every 10ms */
 | 
			
		||||
                *timeout = 10;
 | 
			
		||||
            } else {
 | 
			
		||||
                /* non-idle bottom halves will be executed
 | 
			
		||||
                 * immediately */
 | 
			
		||||
                *timeout = 0;
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static gboolean
 | 
			
		||||
aio_ctx_check(GSource *source)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *ctx = (AioContext *) source;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
 | 
			
		||||
    for (bh = ctx->first_bh; bh; bh = bh->next) {
 | 
			
		||||
        if (!bh->deleted && bh->scheduled) {
 | 
			
		||||
            return true;
 | 
			
		||||
	}
 | 
			
		||||
    }
 | 
			
		||||
    return aio_pending(ctx);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static gboolean
 | 
			
		||||
aio_ctx_dispatch(GSource     *source,
 | 
			
		||||
                 GSourceFunc  callback,
 | 
			
		||||
                 gpointer     user_data)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *ctx = (AioContext *) source;
 | 
			
		||||
 | 
			
		||||
    assert(callback == NULL);
 | 
			
		||||
    aio_poll(ctx, false);
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
aio_ctx_finalize(GSource     *source)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *ctx = (AioContext *) source;
 | 
			
		||||
 | 
			
		||||
    aio_set_event_notifier(ctx, &ctx->notifier, NULL, NULL);
 | 
			
		||||
    event_notifier_cleanup(&ctx->notifier);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static GSourceFuncs aio_source_funcs = {
 | 
			
		||||
    aio_ctx_prepare,
 | 
			
		||||
    aio_ctx_check,
 | 
			
		||||
    aio_ctx_dispatch,
 | 
			
		||||
    aio_ctx_finalize
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
GSource *aio_get_g_source(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    g_source_ref(&ctx->source);
 | 
			
		||||
    return &ctx->source;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void aio_notify(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    event_notifier_set(&ctx->notifier);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
AioContext *aio_context_new(void)
 | 
			
		||||
{
 | 
			
		||||
    AioContext *ctx;
 | 
			
		||||
    ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
 | 
			
		||||
    event_notifier_init(&ctx->notifier, false);
 | 
			
		||||
    aio_set_event_notifier(ctx, &ctx->notifier, 
 | 
			
		||||
                           (EventNotifierHandler *)
 | 
			
		||||
                           event_notifier_test_and_clear, NULL);
 | 
			
		||||
 | 
			
		||||
    return ctx;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void aio_context_ref(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    g_source_ref(&ctx->source);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void aio_context_unref(AioContext *ctx)
 | 
			
		||||
{
 | 
			
		||||
    g_source_unref(&ctx->source);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,215 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Atomic helper templates
 | 
			
		||||
 * Included from tcg-runtime.c and cputlb.c.
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2016 Red Hat, Inc
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#if DATA_SIZE == 16
 | 
			
		||||
# define SUFFIX     o
 | 
			
		||||
# define DATA_TYPE  Int128
 | 
			
		||||
# define BSWAP      bswap128
 | 
			
		||||
#elif DATA_SIZE == 8
 | 
			
		||||
# define SUFFIX     q
 | 
			
		||||
# define DATA_TYPE  uint64_t
 | 
			
		||||
# define BSWAP      bswap64
 | 
			
		||||
#elif DATA_SIZE == 4
 | 
			
		||||
# define SUFFIX     l
 | 
			
		||||
# define DATA_TYPE  uint32_t
 | 
			
		||||
# define BSWAP      bswap32
 | 
			
		||||
#elif DATA_SIZE == 2
 | 
			
		||||
# define SUFFIX     w
 | 
			
		||||
# define DATA_TYPE  uint16_t
 | 
			
		||||
# define BSWAP      bswap16
 | 
			
		||||
#elif DATA_SIZE == 1
 | 
			
		||||
# define SUFFIX     b
 | 
			
		||||
# define DATA_TYPE  uint8_t
 | 
			
		||||
# define BSWAP
 | 
			
		||||
#else
 | 
			
		||||
# error unsupported data size
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if DATA_SIZE >= 4
 | 
			
		||||
# define ABI_TYPE  DATA_TYPE
 | 
			
		||||
#else
 | 
			
		||||
# define ABI_TYPE  uint32_t
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
/* Define host-endian atomic operations.  Note that END is used within
 | 
			
		||||
   the ATOMIC_NAME macro, and redefined below.  */
 | 
			
		||||
#if DATA_SIZE == 1
 | 
			
		||||
# define END
 | 
			
		||||
#elif defined(HOST_WORDS_BIGENDIAN)
 | 
			
		||||
# define END  _be
 | 
			
		||||
#else
 | 
			
		||||
# define END  _le
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(cmpxchg)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                              ABI_TYPE cmpv, ABI_TYPE newv EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    return atomic_cmpxchg__nocheck(haddr, cmpv, newv);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if DATA_SIZE >= 16
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(ld)(CPUArchState *env, target_ulong addr EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE val, *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    __atomic_load(haddr, &val, __ATOMIC_RELAXED);
 | 
			
		||||
    return val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATOMIC_NAME(st)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                     ABI_TYPE val EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    __atomic_store(haddr, &val, __ATOMIC_RELAXED);
 | 
			
		||||
}
 | 
			
		||||
#else
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                           ABI_TYPE val EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    return atomic_xchg__nocheck(haddr, val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define GEN_ATOMIC_HELPER(X)                                        \
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr,       \
 | 
			
		||||
                 ABI_TYPE val EXTRA_ARGS)                           \
 | 
			
		||||
{                                                                   \
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;                           \
 | 
			
		||||
    return atomic_##X(haddr, val);                                  \
 | 
			
		||||
}                                                                   \
 | 
			
		||||
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_add)
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_and)
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_or)
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_xor)
 | 
			
		||||
GEN_ATOMIC_HELPER(add_fetch)
 | 
			
		||||
GEN_ATOMIC_HELPER(and_fetch)
 | 
			
		||||
GEN_ATOMIC_HELPER(or_fetch)
 | 
			
		||||
GEN_ATOMIC_HELPER(xor_fetch)
 | 
			
		||||
 | 
			
		||||
#undef GEN_ATOMIC_HELPER
 | 
			
		||||
#endif /* DATA SIZE >= 16 */
 | 
			
		||||
 | 
			
		||||
#undef END
 | 
			
		||||
 | 
			
		||||
#if DATA_SIZE > 1
 | 
			
		||||
 | 
			
		||||
/* Define reverse-host-endian atomic operations.  Note that END is used
 | 
			
		||||
   within the ATOMIC_NAME macro.  */
 | 
			
		||||
#ifdef HOST_WORDS_BIGENDIAN
 | 
			
		||||
# define END  _le
 | 
			
		||||
#else
 | 
			
		||||
# define END  _be
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(cmpxchg)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                              ABI_TYPE cmpv, ABI_TYPE newv EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    return BSWAP(atomic_cmpxchg__nocheck(haddr, BSWAP(cmpv), BSWAP(newv)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if DATA_SIZE >= 16
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(ld)(CPUArchState *env, target_ulong addr EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE val, *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    __atomic_load(haddr, &val, __ATOMIC_RELAXED);
 | 
			
		||||
    return BSWAP(val);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATOMIC_NAME(st)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                     ABI_TYPE val EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    val = BSWAP(val);
 | 
			
		||||
    __atomic_store(haddr, &val, __ATOMIC_RELAXED);
 | 
			
		||||
}
 | 
			
		||||
#else
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(xchg)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                           ABI_TYPE val EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    return BSWAP(atomic_xchg__nocheck(haddr, BSWAP(val)));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define GEN_ATOMIC_HELPER(X)                                        \
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(X)(CPUArchState *env, target_ulong addr,       \
 | 
			
		||||
                 ABI_TYPE val EXTRA_ARGS)                           \
 | 
			
		||||
{                                                                   \
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;                           \
 | 
			
		||||
    return BSWAP(atomic_##X(haddr, BSWAP(val)));                    \
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_and)
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_or)
 | 
			
		||||
GEN_ATOMIC_HELPER(fetch_xor)
 | 
			
		||||
GEN_ATOMIC_HELPER(and_fetch)
 | 
			
		||||
GEN_ATOMIC_HELPER(or_fetch)
 | 
			
		||||
GEN_ATOMIC_HELPER(xor_fetch)
 | 
			
		||||
 | 
			
		||||
#undef GEN_ATOMIC_HELPER
 | 
			
		||||
 | 
			
		||||
/* Note that for addition, we need to use a separate cmpxchg loop instead
 | 
			
		||||
   of bswaps for the reverse-host-endian helpers.  */
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(fetch_add)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                         ABI_TYPE val EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    DATA_TYPE ldo, ldn, ret, sto;
 | 
			
		||||
 | 
			
		||||
    ldo = atomic_read__nocheck(haddr);
 | 
			
		||||
    while (1) {
 | 
			
		||||
        ret = BSWAP(ldo);
 | 
			
		||||
        sto = BSWAP(ret + val);
 | 
			
		||||
        ldn = atomic_cmpxchg__nocheck(haddr, ldo, sto);
 | 
			
		||||
        if (ldn == ldo) {
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
        ldo = ldn;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ABI_TYPE ATOMIC_NAME(add_fetch)(CPUArchState *env, target_ulong addr,
 | 
			
		||||
                         ABI_TYPE val EXTRA_ARGS)
 | 
			
		||||
{
 | 
			
		||||
    DATA_TYPE *haddr = ATOMIC_MMU_LOOKUP;
 | 
			
		||||
    DATA_TYPE ldo, ldn, ret, sto;
 | 
			
		||||
 | 
			
		||||
    ldo = atomic_read__nocheck(haddr);
 | 
			
		||||
    while (1) {
 | 
			
		||||
        ret = BSWAP(ldo) + val;
 | 
			
		||||
        sto = BSWAP(ret);
 | 
			
		||||
        ldn = atomic_cmpxchg__nocheck(haddr, ldo, sto);
 | 
			
		||||
        if (ldn == ldo) {
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
        ldo = ldn;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
#endif /* DATA_SIZE >= 16 */
 | 
			
		||||
 | 
			
		||||
#undef END
 | 
			
		||||
#endif /* DATA_SIZE > 1 */
 | 
			
		||||
 | 
			
		||||
#undef BSWAP
 | 
			
		||||
#undef ABI_TYPE
 | 
			
		||||
#undef DATA_TYPE
 | 
			
		||||
#undef SUFFIX
 | 
			
		||||
#undef DATA_SIZE
 | 
			
		||||
@@ -5,9 +5,13 @@ common-obj-$(CONFIG_SPICE) += spiceaudio.o
 | 
			
		||||
common-obj-$(CONFIG_COREAUDIO) += coreaudio.o
 | 
			
		||||
common-obj-$(CONFIG_ALSA) += alsaaudio.o
 | 
			
		||||
common-obj-$(CONFIG_DSOUND) += dsoundaudio.o
 | 
			
		||||
common-obj-$(CONFIG_FMOD) += fmodaudio.o
 | 
			
		||||
common-obj-$(CONFIG_ESD) += esdaudio.o
 | 
			
		||||
common-obj-$(CONFIG_PA) += paaudio.o
 | 
			
		||||
common-obj-$(CONFIG_WINWAVE) += winwaveaudio.o
 | 
			
		||||
common-obj-$(CONFIG_AUDIO_PT_INT) += audio_pt_int.o
 | 
			
		||||
common-obj-$(CONFIG_AUDIO_WIN_INT) += audio_win_int.o
 | 
			
		||||
common-obj-y += wavcapture.o
 | 
			
		||||
 | 
			
		||||
sdlaudio.o-cflags := $(SDL_CFLAGS)
 | 
			
		||||
$(obj)/audio.o $(obj)/fmodaudio.o: QEMU_CFLAGS += $(FMOD_CFLAGS)
 | 
			
		||||
$(obj)/sdlaudio.o: QEMU_CFLAGS += $(SDL_CFLAGS)
 | 
			
		||||
 
 | 
			
		||||
@@ -21,12 +21,10 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include <alsa/asoundlib.h>
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
 | 
			
		||||
#if QEMU_GNUC_PREREQ(4, 3)
 | 
			
		||||
#pragma GCC diagnostic ignored "-Waddress"
 | 
			
		||||
@@ -35,28 +33,9 @@
 | 
			
		||||
#define AUDIO_CAP "alsa"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct ALSAConf {
 | 
			
		||||
    int size_in_usec_in;
 | 
			
		||||
    int size_in_usec_out;
 | 
			
		||||
    const char *pcm_name_in;
 | 
			
		||||
    const char *pcm_name_out;
 | 
			
		||||
    unsigned int buffer_size_in;
 | 
			
		||||
    unsigned int period_size_in;
 | 
			
		||||
    unsigned int buffer_size_out;
 | 
			
		||||
    unsigned int period_size_out;
 | 
			
		||||
    unsigned int threshold;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_in_overridden;
 | 
			
		||||
    int period_size_in_overridden;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_out_overridden;
 | 
			
		||||
    int period_size_out_overridden;
 | 
			
		||||
} ALSAConf;
 | 
			
		||||
 | 
			
		||||
struct pollhlp {
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    struct pollfd *pfds;
 | 
			
		||||
    ALSAConf *conf;
 | 
			
		||||
    int count;
 | 
			
		||||
    int mask;
 | 
			
		||||
};
 | 
			
		||||
@@ -77,6 +56,30 @@ typedef struct ALSAVoiceIn {
 | 
			
		||||
    struct pollhlp pollhlp;
 | 
			
		||||
} ALSAVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int size_in_usec_in;
 | 
			
		||||
    int size_in_usec_out;
 | 
			
		||||
    const char *pcm_name_in;
 | 
			
		||||
    const char *pcm_name_out;
 | 
			
		||||
    unsigned int buffer_size_in;
 | 
			
		||||
    unsigned int period_size_in;
 | 
			
		||||
    unsigned int buffer_size_out;
 | 
			
		||||
    unsigned int period_size_out;
 | 
			
		||||
    unsigned int threshold;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_in_overridden;
 | 
			
		||||
    int period_size_in_overridden;
 | 
			
		||||
 | 
			
		||||
    int buffer_size_out_overridden;
 | 
			
		||||
    int period_size_out_overridden;
 | 
			
		||||
    int verbose;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .buffer_size_out = 4096,
 | 
			
		||||
    .period_size_out = 1024,
 | 
			
		||||
    .pcm_name_out = "default",
 | 
			
		||||
    .pcm_name_in = "default",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct alsa_params_req {
 | 
			
		||||
    int freq;
 | 
			
		||||
    snd_pcm_format_t fmt;
 | 
			
		||||
@@ -202,7 +205,9 @@ static void alsa_poll_handler (void *opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!(revents & hlp->mask)) {
 | 
			
		||||
        trace_alsa_revents(revents);
 | 
			
		||||
        if (conf.verbose) {
 | 
			
		||||
            dolog ("revents = %d\n", revents);
 | 
			
		||||
        }
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -261,14 +266,31 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < count; ++i) {
 | 
			
		||||
        if (pfds[i].events & POLLIN) {
 | 
			
		||||
            qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler, NULL, hlp);
 | 
			
		||||
            err = qemu_set_fd_handler (pfds[i].fd, alsa_poll_handler,
 | 
			
		||||
                                       NULL, hlp);
 | 
			
		||||
        }
 | 
			
		||||
        if (pfds[i].events & POLLOUT) {
 | 
			
		||||
            trace_alsa_pollout(i, pfds[i].fd);
 | 
			
		||||
            qemu_set_fd_handler (pfds[i].fd, NULL, alsa_poll_handler, hlp);
 | 
			
		||||
            if (conf.verbose) {
 | 
			
		||||
                dolog ("POLLOUT %d %d\n", i, pfds[i].fd);
 | 
			
		||||
            }
 | 
			
		||||
            err = qemu_set_fd_handler (pfds[i].fd, NULL,
 | 
			
		||||
                                       alsa_poll_handler, hlp);
 | 
			
		||||
        }
 | 
			
		||||
        if (conf.verbose) {
 | 
			
		||||
            dolog ("Set handler events=%#x index=%d fd=%d err=%d\n",
 | 
			
		||||
                   pfds[i].events, i, pfds[i].fd, err);
 | 
			
		||||
        }
 | 
			
		||||
        trace_alsa_set_handler(pfds[i].events, i, pfds[i].fd, err);
 | 
			
		||||
 | 
			
		||||
        if (err) {
 | 
			
		||||
            dolog ("Failed to set handler events=%#x index=%d fd=%d err=%d\n",
 | 
			
		||||
                   pfds[i].events, i, pfds[i].fd, err);
 | 
			
		||||
 | 
			
		||||
            while (i--) {
 | 
			
		||||
                qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
 | 
			
		||||
            }
 | 
			
		||||
            g_free (pfds);
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    hlp->pfds = pfds;
 | 
			
		||||
    hlp->count = count;
 | 
			
		||||
@@ -454,15 +476,14 @@ static void alsa_set_threshold (snd_pcm_t *handle, snd_pcm_uframes_t threshold)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
                      struct alsa_params_obt *obt, snd_pcm_t **handlep,
 | 
			
		||||
                      ALSAConf *conf)
 | 
			
		||||
                      struct alsa_params_obt *obt, snd_pcm_t **handlep)
 | 
			
		||||
{
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    snd_pcm_hw_params_t *hw_params;
 | 
			
		||||
    int err;
 | 
			
		||||
    int size_in_usec;
 | 
			
		||||
    unsigned int freq, nchannels;
 | 
			
		||||
    const char *pcm_name = in ? conf->pcm_name_in : conf->pcm_name_out;
 | 
			
		||||
    const char *pcm_name = in ? conf.pcm_name_in : conf.pcm_name_out;
 | 
			
		||||
    snd_pcm_uframes_t obt_buffer_size;
 | 
			
		||||
    const char *typ = in ? "ADC" : "DAC";
 | 
			
		||||
    snd_pcm_format_t obtfmt;
 | 
			
		||||
@@ -501,7 +522,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = snd_pcm_hw_params_set_format (handle, hw_params, req->fmt);
 | 
			
		||||
    if (err < 0) {
 | 
			
		||||
    if (err < 0 && conf.verbose) {
 | 
			
		||||
        alsa_logerr2 (err, typ, "Failed to set format %d\n", req->fmt);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -633,7 +654,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
        goto err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!in && conf->threshold) {
 | 
			
		||||
    if (!in && conf.threshold) {
 | 
			
		||||
        snd_pcm_uframes_t threshold;
 | 
			
		||||
        int bytes_per_sec;
 | 
			
		||||
 | 
			
		||||
@@ -655,7 +676,7 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        threshold = (conf->threshold * bytes_per_sec) / 1000;
 | 
			
		||||
        threshold = (conf.threshold * bytes_per_sec) / 1000;
 | 
			
		||||
        alsa_set_threshold (handle, threshold);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -665,9 +686,10 @@ static int alsa_open (int in, struct alsa_params_req *req,
 | 
			
		||||
 | 
			
		||||
    *handlep = handle;
 | 
			
		||||
 | 
			
		||||
    if (obtfmt != req->fmt ||
 | 
			
		||||
    if (conf.verbose &&
 | 
			
		||||
        (obtfmt != req->fmt ||
 | 
			
		||||
         obt->nchannels != req->nchannels ||
 | 
			
		||||
         obt->freq != req->freq) {
 | 
			
		||||
         obt->freq != req->freq)) {
 | 
			
		||||
        dolog ("Audio parameters for %s\n", typ);
 | 
			
		||||
        alsa_dump_info (req, obt, obtfmt);
 | 
			
		||||
    }
 | 
			
		||||
@@ -721,7 +743,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
 | 
			
		||||
            if (written <= 0) {
 | 
			
		||||
                switch (written) {
 | 
			
		||||
                case 0:
 | 
			
		||||
                    trace_alsa_wrote_zero(len);
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Failed to write %d frames (wrote zero)\n", len);
 | 
			
		||||
                    }
 | 
			
		||||
                    return;
 | 
			
		||||
 | 
			
		||||
                case -EPIPE:
 | 
			
		||||
@@ -730,7 +754,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
 | 
			
		||||
                                     len);
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    trace_alsa_xrun_out();
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Recovering from playback xrun\n");
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                case -ESTRPIPE:
 | 
			
		||||
@@ -741,7 +767,9 @@ static void alsa_write_pending (ALSAVoiceOut *alsa)
 | 
			
		||||
                                     len);
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
                    trace_alsa_resume_out();
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Resuming suspended output stream\n");
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                case -EAGAIN:
 | 
			
		||||
@@ -787,31 +815,31 @@ static void alsa_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    ldebug ("alsa_fini\n");
 | 
			
		||||
    alsa_anal_close (&alsa->handle, &alsa->pollhlp);
 | 
			
		||||
 | 
			
		||||
    g_free(alsa->pcm_buf);
 | 
			
		||||
    alsa->pcm_buf = NULL;
 | 
			
		||||
    if (alsa->pcm_buf) {
 | 
			
		||||
        g_free (alsa->pcm_buf);
 | 
			
		||||
        alsa->pcm_buf = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                         void *drv_opaque)
 | 
			
		||||
static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ALSAVoiceOut *alsa = (ALSAVoiceOut *) hw;
 | 
			
		||||
    struct alsa_params_req req;
 | 
			
		||||
    struct alsa_params_obt obt;
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    ALSAConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.period_size = conf->period_size_out;
 | 
			
		||||
    req.buffer_size = conf->buffer_size_out;
 | 
			
		||||
    req.size_in_usec = conf->size_in_usec_out;
 | 
			
		||||
    req.period_size = conf.period_size_out;
 | 
			
		||||
    req.buffer_size = conf.buffer_size_out;
 | 
			
		||||
    req.size_in_usec = conf.size_in_usec_out;
 | 
			
		||||
    req.override_mask =
 | 
			
		||||
        (conf->period_size_out_overridden ? 1 : 0) |
 | 
			
		||||
        (conf->buffer_size_out_overridden ? 2 : 0);
 | 
			
		||||
        (conf.period_size_out_overridden ? 1 : 0) |
 | 
			
		||||
        (conf.buffer_size_out_overridden ? 2 : 0);
 | 
			
		||||
 | 
			
		||||
    if (alsa_open (0, &req, &obt, &handle, conf)) {
 | 
			
		||||
    if (alsa_open (0, &req, &obt, &handle)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -832,7 +860,6 @@ static int alsa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    alsa->handle = handle;
 | 
			
		||||
    alsa->pollhlp.conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -903,26 +930,25 @@ static int alsa_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int alsa_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ALSAVoiceIn *alsa = (ALSAVoiceIn *) hw;
 | 
			
		||||
    struct alsa_params_req req;
 | 
			
		||||
    struct alsa_params_obt obt;
 | 
			
		||||
    snd_pcm_t *handle;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    ALSAConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_alsafmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.period_size = conf->period_size_in;
 | 
			
		||||
    req.buffer_size = conf->buffer_size_in;
 | 
			
		||||
    req.size_in_usec = conf->size_in_usec_in;
 | 
			
		||||
    req.period_size = conf.period_size_in;
 | 
			
		||||
    req.buffer_size = conf.buffer_size_in;
 | 
			
		||||
    req.size_in_usec = conf.size_in_usec_in;
 | 
			
		||||
    req.override_mask =
 | 
			
		||||
        (conf->period_size_in_overridden ? 1 : 0) |
 | 
			
		||||
        (conf->buffer_size_in_overridden ? 2 : 0);
 | 
			
		||||
        (conf.period_size_in_overridden ? 1 : 0) |
 | 
			
		||||
        (conf.buffer_size_in_overridden ? 2 : 0);
 | 
			
		||||
 | 
			
		||||
    if (alsa_open (1, &req, &obt, &handle, conf)) {
 | 
			
		||||
    if (alsa_open (1, &req, &obt, &handle)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -943,7 +969,6 @@ static int alsa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    alsa->handle = handle;
 | 
			
		||||
    alsa->pollhlp.conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -953,8 +978,10 @@ static void alsa_fini_in (HWVoiceIn *hw)
 | 
			
		||||
 | 
			
		||||
    alsa_anal_close (&alsa->handle, &alsa->pollhlp);
 | 
			
		||||
 | 
			
		||||
    g_free(alsa->pcm_buf);
 | 
			
		||||
    alsa->pcm_buf = NULL;
 | 
			
		||||
    if (alsa->pcm_buf) {
 | 
			
		||||
        g_free (alsa->pcm_buf);
 | 
			
		||||
        alsa->pcm_buf = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
@@ -999,10 +1026,14 @@ static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
                dolog ("Failed to resume suspended input stream\n");
 | 
			
		||||
                return 0;
 | 
			
		||||
            }
 | 
			
		||||
            trace_alsa_resume_in();
 | 
			
		||||
            if (conf.verbose) {
 | 
			
		||||
                dolog ("Resuming suspended input stream\n");
 | 
			
		||||
            }
 | 
			
		||||
            break;
 | 
			
		||||
        default:
 | 
			
		||||
            trace_alsa_no_frames(state);
 | 
			
		||||
            if (conf.verbose) {
 | 
			
		||||
                dolog ("No frames available and ALSA state is %d\n", state);
 | 
			
		||||
            }
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -1037,7 +1068,9 @@ static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
            if (nread <= 0) {
 | 
			
		||||
                switch (nread) {
 | 
			
		||||
                case 0:
 | 
			
		||||
                    trace_alsa_read_zero(len);
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Failed to read %ld frames (read zero)\n", len);
 | 
			
		||||
                    }
 | 
			
		||||
                    goto exit;
 | 
			
		||||
 | 
			
		||||
                case -EPIPE:
 | 
			
		||||
@@ -1045,7 +1078,9 @@ static int alsa_run_in (HWVoiceIn *hw)
 | 
			
		||||
                        alsa_logerr (nread, "Failed to read %ld frames\n", len);
 | 
			
		||||
                        goto exit;
 | 
			
		||||
                    }
 | 
			
		||||
                    trace_alsa_xrun_in();
 | 
			
		||||
                    if (conf.verbose) {
 | 
			
		||||
                        dolog ("Recovering from capture xrun\n");
 | 
			
		||||
                    }
 | 
			
		||||
                    continue;
 | 
			
		||||
 | 
			
		||||
                case -EAGAIN:
 | 
			
		||||
@@ -1117,85 +1152,82 @@ static int alsa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static ALSAConf glob_conf = {
 | 
			
		||||
    .buffer_size_out = 4096,
 | 
			
		||||
    .period_size_out = 1024,
 | 
			
		||||
    .pcm_name_out = "default",
 | 
			
		||||
    .pcm_name_in = "default",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *alsa_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    ALSAConf *conf = g_malloc(sizeof(ALSAConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void alsa_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option alsa_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_SIZE_IN_USEC",
 | 
			
		||||
        .tag         = AUD_OPT_BOOL,
 | 
			
		||||
        .valp        = &glob_conf.size_in_usec_out,
 | 
			
		||||
        .valp        = &conf.size_in_usec_out,
 | 
			
		||||
        .descr       = "DAC period/buffer size in microseconds (otherwise in frames)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_PERIOD_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.period_size_out,
 | 
			
		||||
        .valp        = &conf.period_size_out,
 | 
			
		||||
        .descr       = "DAC period size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.period_size_out_overridden
 | 
			
		||||
        .overriddenp = &conf.period_size_out_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_BUFFER_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.buffer_size_out,
 | 
			
		||||
        .valp        = &conf.buffer_size_out,
 | 
			
		||||
        .descr       = "DAC buffer size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.buffer_size_out_overridden
 | 
			
		||||
        .overriddenp = &conf.buffer_size_out_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_SIZE_IN_USEC",
 | 
			
		||||
        .tag         = AUD_OPT_BOOL,
 | 
			
		||||
        .valp        = &glob_conf.size_in_usec_in,
 | 
			
		||||
        .valp        = &conf.size_in_usec_in,
 | 
			
		||||
        .descr       =
 | 
			
		||||
        "ADC period/buffer size in microseconds (otherwise in frames)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_PERIOD_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.period_size_in,
 | 
			
		||||
        .valp        = &conf.period_size_in,
 | 
			
		||||
        .descr       = "ADC period size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.period_size_in_overridden
 | 
			
		||||
        .overriddenp = &conf.period_size_in_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_BUFFER_SIZE",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.buffer_size_in,
 | 
			
		||||
        .valp        = &conf.buffer_size_in,
 | 
			
		||||
        .descr       = "ADC buffer size (0 to go with system default)",
 | 
			
		||||
        .overriddenp = &glob_conf.buffer_size_in_overridden
 | 
			
		||||
        .overriddenp = &conf.buffer_size_in_overridden
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "THRESHOLD",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &glob_conf.threshold,
 | 
			
		||||
        .valp        = &conf.threshold,
 | 
			
		||||
        .descr       = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_DEV",
 | 
			
		||||
        .tag         = AUD_OPT_STR,
 | 
			
		||||
        .valp        = &glob_conf.pcm_name_out,
 | 
			
		||||
        .valp        = &conf.pcm_name_out,
 | 
			
		||||
        .descr       = "DAC device name (for instance dmix)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_DEV",
 | 
			
		||||
        .tag         = AUD_OPT_STR,
 | 
			
		||||
        .valp        = &glob_conf.pcm_name_in,
 | 
			
		||||
        .valp        = &conf.pcm_name_in,
 | 
			
		||||
        .descr       = "ADC device name"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "VERBOSE",
 | 
			
		||||
        .tag         = AUD_OPT_BOOL,
 | 
			
		||||
        .valp        = &conf.verbose,
 | 
			
		||||
        .descr       = "Behave in a more verbose way"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										112
									
								
								audio/audio.c
									
									
									
									
									
								
							
							
						
						
									
										112
									
								
								audio/audio.c
									
									
									
									
									
								
							@@ -21,18 +21,16 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "qemu/cutils.h"
 | 
			
		||||
#include "sysemu/replay.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "audio"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
/* #define DEBUG_PLIVE */
 | 
			
		||||
/* #define DEBUG_LIVE */
 | 
			
		||||
/* #define DEBUG_OUT */
 | 
			
		||||
/* #define DEBUG_CAPTURE */
 | 
			
		||||
@@ -68,6 +66,8 @@ static struct {
 | 
			
		||||
        int hertz;
 | 
			
		||||
        int64_t ticks;
 | 
			
		||||
    } period;
 | 
			
		||||
    int plive;
 | 
			
		||||
    int log_to_monitor;
 | 
			
		||||
    int try_poll_in;
 | 
			
		||||
    int try_poll_out;
 | 
			
		||||
} conf = {
 | 
			
		||||
@@ -95,7 +95,9 @@ static struct {
 | 
			
		||||
        }
 | 
			
		||||
    },
 | 
			
		||||
 | 
			
		||||
    .period = { .hertz = 100 },
 | 
			
		||||
    .period = { .hertz = 250 },
 | 
			
		||||
    .plive = 0,
 | 
			
		||||
    .log_to_monitor = 0,
 | 
			
		||||
    .try_poll_in = 1,
 | 
			
		||||
    .try_poll_out = 1,
 | 
			
		||||
};
 | 
			
		||||
@@ -329,11 +331,20 @@ static const char *audio_get_conf_str (const char *key,
 | 
			
		||||
 | 
			
		||||
void AUD_vlog (const char *cap, const char *fmt, va_list ap)
 | 
			
		||||
{
 | 
			
		||||
    if (cap) {
 | 
			
		||||
        fprintf(stderr, "%s: ", cap);
 | 
			
		||||
    }
 | 
			
		||||
    if (conf.log_to_monitor) {
 | 
			
		||||
        if (cap) {
 | 
			
		||||
            monitor_printf(default_mon, "%s: ", cap);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    vfprintf(stderr, fmt, ap);
 | 
			
		||||
        monitor_vprintf(default_mon, fmt, ap);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        if (cap) {
 | 
			
		||||
            fprintf (stderr, "%s: ", cap);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        vfprintf (stderr, fmt, ap);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void AUD_log (const char *cap, const char *fmt, ...)
 | 
			
		||||
@@ -1113,11 +1124,10 @@ static int audio_is_timer_needed (void)
 | 
			
		||||
static void audio_reset_timer (AudioState *s)
 | 
			
		||||
{
 | 
			
		||||
    if (audio_is_timer_needed ()) {
 | 
			
		||||
        timer_mod_anticipate_ns(s->ts,
 | 
			
		||||
            qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
 | 
			
		||||
        qemu_mod_timer (s->ts, qemu_get_clock_ns (vm_clock) + 1);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        timer_del (s->ts);
 | 
			
		||||
        qemu_del_timer (s->ts);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1132,6 +1142,8 @@ static void audio_timer (void *opaque)
 | 
			
		||||
 */
 | 
			
		||||
int AUD_write (SWVoiceOut *sw, void *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    int bytes;
 | 
			
		||||
 | 
			
		||||
    if (!sw) {
 | 
			
		||||
        /* XXX: Consider options */
 | 
			
		||||
        return size;
 | 
			
		||||
@@ -1142,11 +1154,14 @@ int AUD_write (SWVoiceOut *sw, void *buf, int size)
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return sw->hw->pcm_ops->write(sw, buf, size);
 | 
			
		||||
    bytes = sw->hw->pcm_ops->write (sw, buf, size);
 | 
			
		||||
    return bytes;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int AUD_read (SWVoiceIn *sw, void *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    int bytes;
 | 
			
		||||
 | 
			
		||||
    if (!sw) {
 | 
			
		||||
        /* XXX: Consider options */
 | 
			
		||||
        return size;
 | 
			
		||||
@@ -1157,7 +1172,8 @@ int AUD_read (SWVoiceIn *sw, void *buf, int size)
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return sw->hw->pcm_ops->read(sw, buf, size);
 | 
			
		||||
    bytes = sw->hw->pcm_ops->read (sw, buf, size);
 | 
			
		||||
    return bytes;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int AUD_get_buffer_size_out (SWVoiceOut *sw)
 | 
			
		||||
@@ -1388,7 +1404,6 @@ static void audio_run_out (AudioState *s)
 | 
			
		||||
 | 
			
		||||
        prev_rpos = hw->rpos;
 | 
			
		||||
        played = hw->pcm_ops->run_out (hw, live);
 | 
			
		||||
        replay_audio_out(&played);
 | 
			
		||||
        if (audio_bug (AUDIO_FUNC, hw->rpos >= hw->samples)) {
 | 
			
		||||
            dolog ("hw->rpos=%d hw->samples=%d played=%d\n",
 | 
			
		||||
                   hw->rpos, hw->samples, played);
 | 
			
		||||
@@ -1438,6 +1453,9 @@ static void audio_run_out (AudioState *s)
 | 
			
		||||
            while (sw) {
 | 
			
		||||
                sw1 = sw->entries.le_next;
 | 
			
		||||
                if (!sw->active && !sw->callback.fn) {
 | 
			
		||||
#ifdef DEBUG_PLIVE
 | 
			
		||||
                    dolog ("Finishing with old voice\n");
 | 
			
		||||
#endif
 | 
			
		||||
                    audio_close_out (sw);
 | 
			
		||||
                }
 | 
			
		||||
                sw = sw1;
 | 
			
		||||
@@ -1452,12 +1470,9 @@ static void audio_run_in (AudioState *s)
 | 
			
		||||
 | 
			
		||||
    while ((hw = audio_pcm_hw_find_any_enabled_in (hw))) {
 | 
			
		||||
        SWVoiceIn *sw;
 | 
			
		||||
        int captured = 0, min;
 | 
			
		||||
        int captured, min;
 | 
			
		||||
 | 
			
		||||
        if (replay_mode != REPLAY_MODE_PLAY) {
 | 
			
		||||
            captured = hw->pcm_ops->run_in(hw);
 | 
			
		||||
        }
 | 
			
		||||
        replay_audio_in(&captured, hw->conv_buf, &hw->wpos, hw->samples);
 | 
			
		||||
        captured = hw->pcm_ops->run_in (hw);
 | 
			
		||||
 | 
			
		||||
        min = audio_pcm_hw_find_min_in (hw);
 | 
			
		||||
        hw->total_samples_captured += captured - min;
 | 
			
		||||
@@ -1632,6 +1647,18 @@ static struct audio_option audio_options[] = {
 | 
			
		||||
        .valp  = &conf.period.hertz,
 | 
			
		||||
        .descr = "Timer period in HZ (0 - use lowest possible)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PLIVE",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.plive,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "LOG_TO_MONITOR",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.log_to_monitor,
 | 
			
		||||
        .descr = "Print logging messages to monitor instead of stderr"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@@ -1744,21 +1771,13 @@ static void audio_vm_change_state_handler (void *opaque, int running,
 | 
			
		||||
    audio_reset_timer (s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool is_cleaning_up;
 | 
			
		||||
 | 
			
		||||
bool audio_is_cleaning_up(void)
 | 
			
		||||
{
 | 
			
		||||
    return is_cleaning_up;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void audio_cleanup(void)
 | 
			
		||||
static void audio_atexit (void)
 | 
			
		||||
{
 | 
			
		||||
    AudioState *s = &glob_audio_state;
 | 
			
		||||
    HWVoiceOut *hwo, *hwon;
 | 
			
		||||
    HWVoiceIn *hwi, *hwin;
 | 
			
		||||
    HWVoiceOut *hwo = NULL;
 | 
			
		||||
    HWVoiceIn *hwi = NULL;
 | 
			
		||||
 | 
			
		||||
    is_cleaning_up = true;
 | 
			
		||||
    QLIST_FOREACH_SAFE(hwo, &glob_audio_state.hw_head_out, entries, hwon) {
 | 
			
		||||
    while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
 | 
			
		||||
        SWVoiceCap *sc;
 | 
			
		||||
 | 
			
		||||
        if (hwo->enabled) {
 | 
			
		||||
@@ -1774,20 +1793,17 @@ void audio_cleanup(void)
 | 
			
		||||
                cb->ops.destroy (cb->opaque);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        QLIST_REMOVE(hwo, entries);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QLIST_FOREACH_SAFE(hwi, &glob_audio_state.hw_head_in, entries, hwin) {
 | 
			
		||||
    while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
 | 
			
		||||
        if (hwi->enabled) {
 | 
			
		||||
            hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
 | 
			
		||||
        }
 | 
			
		||||
        hwi->pcm_ops->fini_in (hwi);
 | 
			
		||||
        QLIST_REMOVE(hwi, entries);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (s->drv) {
 | 
			
		||||
        s->drv->fini (s->drv_opaque);
 | 
			
		||||
        s->drv = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -1795,7 +1811,8 @@ static const VMStateDescription vmstate_audio = {
 | 
			
		||||
    .name = "audio",
 | 
			
		||||
    .version_id = 1,
 | 
			
		||||
    .minimum_version_id = 1,
 | 
			
		||||
    .fields = (VMStateField[]) {
 | 
			
		||||
    .minimum_version_id_old = 1,
 | 
			
		||||
    .fields      = (VMStateField []) {
 | 
			
		||||
        VMSTATE_END_OF_LIST()
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
@@ -1815,9 +1832,12 @@ static void audio_init (void)
 | 
			
		||||
    QLIST_INIT (&s->hw_head_out);
 | 
			
		||||
    QLIST_INIT (&s->hw_head_in);
 | 
			
		||||
    QLIST_INIT (&s->cap_head);
 | 
			
		||||
    atexit(audio_cleanup);
 | 
			
		||||
    atexit (audio_atexit);
 | 
			
		||||
 | 
			
		||||
    s->ts = timer_new_ns(QEMU_CLOCK_VIRTUAL, audio_timer, s);
 | 
			
		||||
    s->ts = qemu_new_timer_ns (vm_clock, audio_timer, s);
 | 
			
		||||
    if (!s->ts) {
 | 
			
		||||
        hw_error("Could not create audio timer\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_process_options ("AUDIO", audio_options);
 | 
			
		||||
 | 
			
		||||
@@ -1868,8 +1888,12 @@ static void audio_init (void)
 | 
			
		||||
 | 
			
		||||
    if (!done) {
 | 
			
		||||
        done = !audio_driver_init (s, &no_audio_driver);
 | 
			
		||||
        assert(done);
 | 
			
		||||
        dolog("warning: Using timer based audio emulation\n");
 | 
			
		||||
        if (!done) {
 | 
			
		||||
            hw_error("Could not initialize audio subsystem\n");
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            dolog ("warning: Using timer based audio emulation\n");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (conf.period.hertz <= 0) {
 | 
			
		||||
@@ -1880,7 +1904,8 @@ static void audio_init (void)
 | 
			
		||||
        }
 | 
			
		||||
        conf.period.ticks = 1;
 | 
			
		||||
    } else {
 | 
			
		||||
        conf.period.ticks = NANOSECONDS_PER_SECOND / conf.period.hertz;
 | 
			
		||||
        conf.period.ticks =
 | 
			
		||||
            muldiv64 (1, get_ticks_per_sec (), conf.period.hertz);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    e = qemu_add_vm_change_state_handler (audio_vm_change_state_handler, s);
 | 
			
		||||
@@ -1982,7 +2007,8 @@ CaptureVoiceOut *AUD_add_capture (
 | 
			
		||||
        QLIST_INSERT_HEAD (&s->cap_head, cap, entries);
 | 
			
		||||
        QLIST_INSERT_HEAD (&cap->cb_head, cb, entries);
 | 
			
		||||
 | 
			
		||||
        QLIST_FOREACH(hw, &glob_audio_state.hw_head_out, entries) {
 | 
			
		||||
        hw = NULL;
 | 
			
		||||
        while ((hw = audio_pcm_hw_find_any_out (hw))) {
 | 
			
		||||
            audio_attach_capture (hw);
 | 
			
		||||
        }
 | 
			
		||||
        return cap;
 | 
			
		||||
@@ -2028,8 +2054,6 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
 | 
			
		||||
                    sw = sw1;
 | 
			
		||||
                }
 | 
			
		||||
                QLIST_REMOVE (cap, entries);
 | 
			
		||||
                g_free (cap->hw.mix_buf);
 | 
			
		||||
                g_free (cap->buf);
 | 
			
		||||
                g_free (cap);
 | 
			
		||||
            }
 | 
			
		||||
            return;
 | 
			
		||||
 
 | 
			
		||||
@@ -21,10 +21,10 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef QEMU_AUDIO_H
 | 
			
		||||
#define QEMU_AUDIO_H
 | 
			
		||||
 | 
			
		||||
#include "config-host.h"
 | 
			
		||||
#include "qemu/queue.h"
 | 
			
		||||
 | 
			
		||||
typedef void (*audio_callback_fn) (void *opaque, int avail);
 | 
			
		||||
@@ -163,12 +163,4 @@ static inline void *advance (void *p, int incr)
 | 
			
		||||
int wav_start_capture (CaptureState *s, const char *path, int freq,
 | 
			
		||||
                       int bits, int nchannels);
 | 
			
		||||
 | 
			
		||||
bool audio_is_cleaning_up(void);
 | 
			
		||||
void audio_cleanup(void);
 | 
			
		||||
 | 
			
		||||
void audio_sample_to_uint64(void *samples, int pos,
 | 
			
		||||
                            uint64_t *left, uint64_t *right);
 | 
			
		||||
void audio_sample_from_uint64(void *samples, int pos,
 | 
			
		||||
                            uint64_t left, uint64_t right);
 | 
			
		||||
 | 
			
		||||
#endif /* QEMU_AUDIO_H */
 | 
			
		||||
#endif  /* audio.h */
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,6 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef QEMU_AUDIO_INT_H
 | 
			
		||||
#define QEMU_AUDIO_INT_H
 | 
			
		||||
 | 
			
		||||
@@ -157,13 +156,13 @@ struct audio_driver {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_pcm_ops {
 | 
			
		||||
    int  (*init_out)(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque);
 | 
			
		||||
    int  (*init_out)(HWVoiceOut *hw, struct audsettings *as);
 | 
			
		||||
    void (*fini_out)(HWVoiceOut *hw);
 | 
			
		||||
    int  (*run_out) (HWVoiceOut *hw, int live);
 | 
			
		||||
    int  (*write)   (SWVoiceOut *sw, void *buf, int size);
 | 
			
		||||
    int  (*ctl_out) (HWVoiceOut *hw, int cmd, ...);
 | 
			
		||||
 | 
			
		||||
    int  (*init_in) (HWVoiceIn *hw, struct audsettings *as, void *drv_opaque);
 | 
			
		||||
    int  (*init_in) (HWVoiceIn *hw, struct audsettings *as);
 | 
			
		||||
    void (*fini_in) (HWVoiceIn *hw);
 | 
			
		||||
    int  (*run_in)  (HWVoiceIn *hw);
 | 
			
		||||
    int  (*read)    (SWVoiceIn *sw, void *buf, int size);
 | 
			
		||||
@@ -207,11 +206,14 @@ extern struct audio_driver no_audio_driver;
 | 
			
		||||
extern struct audio_driver oss_audio_driver;
 | 
			
		||||
extern struct audio_driver sdl_audio_driver;
 | 
			
		||||
extern struct audio_driver wav_audio_driver;
 | 
			
		||||
extern struct audio_driver fmod_audio_driver;
 | 
			
		||||
extern struct audio_driver alsa_audio_driver;
 | 
			
		||||
extern struct audio_driver coreaudio_audio_driver;
 | 
			
		||||
extern struct audio_driver dsound_audio_driver;
 | 
			
		||||
extern struct audio_driver esd_audio_driver;
 | 
			
		||||
extern struct audio_driver pa_audio_driver;
 | 
			
		||||
extern struct audio_driver spice_audio_driver;
 | 
			
		||||
extern struct audio_driver winwave_audio_driver;
 | 
			
		||||
extern const struct mixeng_volume nominal_volume;
 | 
			
		||||
 | 
			
		||||
void audio_pcm_init_info (struct audio_pcm_info *info, struct audsettings *as);
 | 
			
		||||
@@ -241,13 +243,38 @@ static inline int audio_ring_dist (int dst, int src, int len)
 | 
			
		||||
    return (dst >= src) ? (dst - src) : (len - src + dst);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define dolog(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
 | 
			
		||||
static void GCC_ATTR dolog (const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
#define ldebug(fmt, ...) AUD_log(AUDIO_CAP, fmt, ## __VA_ARGS__)
 | 
			
		||||
static void GCC_ATTR ldebug (const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
}
 | 
			
		||||
#else
 | 
			
		||||
#define ldebug(fmt, ...) (void)0
 | 
			
		||||
#if defined NDEBUG && defined __GNUC__
 | 
			
		||||
#define ldebug(...)
 | 
			
		||||
#elif defined NDEBUG && defined _MSC_VER
 | 
			
		||||
#define ldebug __noop
 | 
			
		||||
#else
 | 
			
		||||
static void GCC_ATTR ldebug (const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    (void) fmt;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#undef GCC_ATTR
 | 
			
		||||
 | 
			
		||||
#define AUDIO_STRINGIFY_(n) #n
 | 
			
		||||
#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
 | 
			
		||||
@@ -258,4 +285,4 @@ static inline int audio_ring_dist (int dst, int src, int len)
 | 
			
		||||
#define AUDIO_FUNC __FILE__ ":" AUDIO_STRINGIFY (__LINE__)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#endif /* QEMU_AUDIO_INT_H */
 | 
			
		||||
#endif /* audio_int.h */
 | 
			
		||||
 
 | 
			
		||||
@@ -1,4 +1,3 @@
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -19,4 +19,4 @@ int audio_pt_wait (struct audio_pt *, const char *);
 | 
			
		||||
int audio_pt_unlock_and_signal (struct audio_pt *, const char *);
 | 
			
		||||
int audio_pt_join (struct audio_pt *, void **, const char *);
 | 
			
		||||
 | 
			
		||||
#endif /* QEMU_AUDIO_PT_INT_H */
 | 
			
		||||
#endif /* audio_pt_int.h */
 | 
			
		||||
 
 | 
			
		||||
@@ -71,7 +71,10 @@ static void glue (audio_init_nb_voices_, TYPE) (struct audio_driver *drv)
 | 
			
		||||
 | 
			
		||||
static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
 | 
			
		||||
{
 | 
			
		||||
    g_free (HWBUF);
 | 
			
		||||
    if (HWBUF) {
 | 
			
		||||
        g_free (HWBUF);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    HWBUF = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -89,7 +92,9 @@ static int glue (audio_pcm_hw_alloc_resources_, TYPE) (HW *hw)
 | 
			
		||||
 | 
			
		||||
static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
 | 
			
		||||
{
 | 
			
		||||
    g_free (sw->buf);
 | 
			
		||||
    if (sw->buf) {
 | 
			
		||||
        g_free (sw->buf);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (sw->rate) {
 | 
			
		||||
        st_rate_stop (sw->rate);
 | 
			
		||||
@@ -167,8 +172,10 @@ static int glue (audio_pcm_sw_init_, TYPE) (
 | 
			
		||||
static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
 | 
			
		||||
{
 | 
			
		||||
    glue (audio_pcm_sw_free_resources_, TYPE) (sw);
 | 
			
		||||
    g_free (sw->name);
 | 
			
		||||
    sw->name = NULL;
 | 
			
		||||
    if (sw->name) {
 | 
			
		||||
        g_free (sw->name);
 | 
			
		||||
        sw->name = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
 | 
			
		||||
@@ -191,9 +198,9 @@ static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
 | 
			
		||||
        audio_detach_capture (hw);
 | 
			
		||||
#endif
 | 
			
		||||
        QLIST_REMOVE (hw, entries);
 | 
			
		||||
        glue (hw->pcm_ops->fini_, TYPE) (hw);
 | 
			
		||||
        glue (s->nb_hw_voices_, TYPE) += 1;
 | 
			
		||||
        glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
 | 
			
		||||
        glue (hw->pcm_ops->fini_, TYPE) (hw);
 | 
			
		||||
        g_free (hw);
 | 
			
		||||
        *hwp = NULL;
 | 
			
		||||
    }
 | 
			
		||||
@@ -262,7 +269,7 @@ static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    QLIST_INIT (&hw->cap_head);
 | 
			
		||||
#endif
 | 
			
		||||
    if (glue (hw->pcm_ops->init_, TYPE) (hw, as, s->drv_opaque)) {
 | 
			
		||||
    if (glue (hw->pcm_ops->init_, TYPE) (hw, as)) {
 | 
			
		||||
        goto err0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -398,6 +405,10 @@ SW *glue (AUD_open_, TYPE) (
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    AudioState *s = &glob_audio_state;
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    int live = 0;
 | 
			
		||||
    SW *old_sw = NULL;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
 | 
			
		||||
        dolog ("card=%p name=%p callback_fn=%p as=%p\n",
 | 
			
		||||
@@ -422,6 +433,29 @@ SW *glue (AUD_open_, TYPE) (
 | 
			
		||||
        return sw;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    if (conf.plive && sw && (!sw->active && !sw->empty)) {
 | 
			
		||||
        live = sw->total_hw_samples_mixed;
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_PLIVE
 | 
			
		||||
        dolog ("Replacing voice %s with %d live samples\n", SW_NAME (sw), live);
 | 
			
		||||
        dolog ("Old %s freq %d, bits %d, channels %d\n",
 | 
			
		||||
               SW_NAME (sw), sw->info.freq, sw->info.bits, sw->info.nchannels);
 | 
			
		||||
        dolog ("New %s freq %d, bits %d, channels %d\n",
 | 
			
		||||
               name,
 | 
			
		||||
               as->freq,
 | 
			
		||||
               (as->fmt == AUD_FMT_S16 || as->fmt == AUD_FMT_U16) ? 16 : 8,
 | 
			
		||||
               as->nchannels);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
        if (live) {
 | 
			
		||||
            old_sw = sw;
 | 
			
		||||
            old_sw->callback.fn = NULL;
 | 
			
		||||
            sw = NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (!glue (conf.fixed_, TYPE).enabled && sw) {
 | 
			
		||||
        glue (AUD_close_, TYPE) (card, sw);
 | 
			
		||||
        sw = NULL;
 | 
			
		||||
@@ -454,6 +488,20 @@ SW *glue (AUD_open_, TYPE) (
 | 
			
		||||
    sw->callback.fn = callback_fn;
 | 
			
		||||
    sw->callback.opaque = callback_opaque;
 | 
			
		||||
 | 
			
		||||
#ifdef DAC
 | 
			
		||||
    if (live) {
 | 
			
		||||
        int mixed =
 | 
			
		||||
            (live << old_sw->info.shift)
 | 
			
		||||
            * old_sw->info.bytes_per_second
 | 
			
		||||
            / sw->info.bytes_per_second;
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_PLIVE
 | 
			
		||||
        dolog ("Silence will be mixed %d\n", mixed);
 | 
			
		||||
#endif
 | 
			
		||||
        sw->total_hw_samples_mixed += mixed;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_AUDIO
 | 
			
		||||
    dolog ("%s\n", name);
 | 
			
		||||
    audio_pcm_print_info ("hw", &sw->hw->info);
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
/* public domain */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "win-int"
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
 
 | 
			
		||||
@@ -22,8 +22,8 @@
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include <CoreAudio/CoreAudio.h>
 | 
			
		||||
#include <string.h>             /* strerror */
 | 
			
		||||
#include <pthread.h>            /* pthread_X */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
@@ -32,248 +32,28 @@
 | 
			
		||||
#define AUDIO_CAP "coreaudio"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
#ifndef MAC_OS_X_VERSION_10_6
 | 
			
		||||
#define MAC_OS_X_VERSION_10_6 1060
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
struct {
 | 
			
		||||
    int buffer_frames;
 | 
			
		||||
    int nbuffers;
 | 
			
		||||
} CoreaudioConf;
 | 
			
		||||
    int isAtexit;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .buffer_frames = 512,
 | 
			
		||||
    .nbuffers = 4,
 | 
			
		||||
    .isAtexit = 0
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct coreaudioVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    pthread_mutex_t mutex;
 | 
			
		||||
    int isAtexit;
 | 
			
		||||
    AudioDeviceID outputDeviceID;
 | 
			
		||||
    UInt32 audioDevicePropertyBufferFrameSize;
 | 
			
		||||
    AudioStreamBasicDescription outputStreamBasicDescription;
 | 
			
		||||
    AudioDeviceIOProcID ioprocid;
 | 
			
		||||
    int live;
 | 
			
		||||
    int decr;
 | 
			
		||||
    int rpos;
 | 
			
		||||
} coreaudioVoiceOut;
 | 
			
		||||
 | 
			
		||||
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
 | 
			
		||||
/* The APIs used here only become available from 10.6 */
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_voice(AudioDeviceID *id)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*id);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioHardwarePropertyDefaultOutputDevice,
 | 
			
		||||
        kAudioObjectPropertyScopeGlobal,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectGetPropertyData(kAudioObjectSystemObject,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      &size,
 | 
			
		||||
                                      id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_framesizerange(AudioDeviceID id,
 | 
			
		||||
                                             AudioValueRange *framerange)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*framerange);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSizeRange,
 | 
			
		||||
        kAudioDevicePropertyScopeOutput,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectGetPropertyData(id,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      &size,
 | 
			
		||||
                                      framerange);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*framesize);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSize,
 | 
			
		||||
        kAudioDevicePropertyScopeOutput,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectGetPropertyData(id,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      &size,
 | 
			
		||||
                                      framesize);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*framesize);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSize,
 | 
			
		||||
        kAudioDevicePropertyScopeOutput,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectSetPropertyData(id,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      size,
 | 
			
		||||
                                      framesize);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_streamformat(AudioDeviceID id,
 | 
			
		||||
                                           AudioStreamBasicDescription *d)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*d);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioDevicePropertyStreamFormat,
 | 
			
		||||
        kAudioDevicePropertyScopeOutput,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectGetPropertyData(id,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      &size,
 | 
			
		||||
                                      d);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_set_streamformat(AudioDeviceID id,
 | 
			
		||||
                                           AudioStreamBasicDescription *d)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*d);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioDevicePropertyStreamFormat,
 | 
			
		||||
        kAudioDevicePropertyScopeOutput,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectSetPropertyData(id,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      size,
 | 
			
		||||
                                      d);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*result);
 | 
			
		||||
    AudioObjectPropertyAddress addr = {
 | 
			
		||||
        kAudioDevicePropertyDeviceIsRunning,
 | 
			
		||||
        kAudioDevicePropertyScopeOutput,
 | 
			
		||||
        kAudioObjectPropertyElementMaster
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    return AudioObjectGetPropertyData(id,
 | 
			
		||||
                                      &addr,
 | 
			
		||||
                                      0,
 | 
			
		||||
                                      NULL,
 | 
			
		||||
                                      &size,
 | 
			
		||||
                                      result);
 | 
			
		||||
}
 | 
			
		||||
#else
 | 
			
		||||
/* Legacy versions of functions using deprecated APIs */
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_voice(AudioDeviceID *id)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*id);
 | 
			
		||||
 | 
			
		||||
    return AudioHardwareGetProperty(
 | 
			
		||||
        kAudioHardwarePropertyDefaultOutputDevice,
 | 
			
		||||
        &size,
 | 
			
		||||
        id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_framesizerange(AudioDeviceID id,
 | 
			
		||||
                                             AudioValueRange *framerange)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*framerange);
 | 
			
		||||
 | 
			
		||||
    return AudioDeviceGetProperty(
 | 
			
		||||
        id,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSizeRange,
 | 
			
		||||
        &size,
 | 
			
		||||
        framerange);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_framesize(AudioDeviceID id, UInt32 *framesize)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*framesize);
 | 
			
		||||
 | 
			
		||||
    return AudioDeviceGetProperty(
 | 
			
		||||
        id,
 | 
			
		||||
        0,
 | 
			
		||||
        false,
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSize,
 | 
			
		||||
        &size,
 | 
			
		||||
        framesize);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_set_framesize(AudioDeviceID id, UInt32 *framesize)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*framesize);
 | 
			
		||||
 | 
			
		||||
    return AudioDeviceSetProperty(
 | 
			
		||||
        id,
 | 
			
		||||
        NULL,
 | 
			
		||||
        0,
 | 
			
		||||
        false,
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSize,
 | 
			
		||||
        size,
 | 
			
		||||
        framesize);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_streamformat(AudioDeviceID id,
 | 
			
		||||
                                           AudioStreamBasicDescription *d)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*d);
 | 
			
		||||
 | 
			
		||||
    return AudioDeviceGetProperty(
 | 
			
		||||
        id,
 | 
			
		||||
        0,
 | 
			
		||||
        false,
 | 
			
		||||
        kAudioDevicePropertyStreamFormat,
 | 
			
		||||
        &size,
 | 
			
		||||
        d);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_set_streamformat(AudioDeviceID id,
 | 
			
		||||
                                           AudioStreamBasicDescription *d)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*d);
 | 
			
		||||
 | 
			
		||||
    return AudioDeviceSetProperty(
 | 
			
		||||
        id,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        kAudioDevicePropertyStreamFormat,
 | 
			
		||||
        size,
 | 
			
		||||
        d);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSStatus coreaudio_get_isrunning(AudioDeviceID id, UInt32 *result)
 | 
			
		||||
{
 | 
			
		||||
    UInt32 size = sizeof(*result);
 | 
			
		||||
 | 
			
		||||
    return AudioDeviceGetProperty(
 | 
			
		||||
        id,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        kAudioDevicePropertyDeviceIsRunning,
 | 
			
		||||
        &size,
 | 
			
		||||
        result);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static void coreaudio_logstatus (OSStatus status)
 | 
			
		||||
{
 | 
			
		||||
    const char *str = "BUG";
 | 
			
		||||
@@ -368,7 +148,10 @@ static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
 | 
			
		||||
{
 | 
			
		||||
    OSStatus status;
 | 
			
		||||
    UInt32 result = 0;
 | 
			
		||||
    status = coreaudio_get_isrunning(outputDeviceID, &result);
 | 
			
		||||
    UInt32 propertySize = sizeof(outputDeviceID);
 | 
			
		||||
    status = AudioDeviceGetProperty(
 | 
			
		||||
        outputDeviceID, 0, 0,
 | 
			
		||||
        kAudioDevicePropertyDeviceIsRunning, &propertySize, &result);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr(status,
 | 
			
		||||
                         "Could not determine whether Device is playing\n");
 | 
			
		||||
@@ -376,6 +159,11 @@ static inline UInt32 isPlaying (AudioDeviceID outputDeviceID)
 | 
			
		||||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coreaudio_atexit (void)
 | 
			
		||||
{
 | 
			
		||||
    conf.isAtexit = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coreaudio_lock (coreaudioVoiceOut *core, const char *fn_name)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
@@ -499,15 +287,14 @@ static int coreaudio_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                              void *drv_opaque)
 | 
			
		||||
static int coreaudio_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    OSStatus status;
 | 
			
		||||
    coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
 | 
			
		||||
    UInt32 propertySize;
 | 
			
		||||
    int err;
 | 
			
		||||
    const char *typ = "playback";
 | 
			
		||||
    AudioValueRange frameRange;
 | 
			
		||||
    CoreaudioConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    /* create mutex */
 | 
			
		||||
    err = pthread_mutex_init(&core->mutex, NULL);
 | 
			
		||||
@@ -518,7 +305,12 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
 | 
			
		||||
    status = coreaudio_get_voice(&core->outputDeviceID);
 | 
			
		||||
    /* open default output device */
 | 
			
		||||
    propertySize = sizeof(core->outputDeviceID);
 | 
			
		||||
    status = AudioHardwareGetProperty(
 | 
			
		||||
        kAudioHardwarePropertyDefaultOutputDevice,
 | 
			
		||||
        &propertySize,
 | 
			
		||||
        &core->outputDeviceID);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ,
 | 
			
		||||
                           "Could not get default output Device\n");
 | 
			
		||||
@@ -530,29 +322,42 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* get minimum and maximum buffer frame sizes */
 | 
			
		||||
    status = coreaudio_get_framesizerange(core->outputDeviceID,
 | 
			
		||||
                                          &frameRange);
 | 
			
		||||
    propertySize = sizeof(frameRange);
 | 
			
		||||
    status = AudioDeviceGetProperty(
 | 
			
		||||
        core->outputDeviceID,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSizeRange,
 | 
			
		||||
        &propertySize,
 | 
			
		||||
        &frameRange);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ,
 | 
			
		||||
                           "Could not get device buffer frame range\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (frameRange.mMinimum > conf->buffer_frames) {
 | 
			
		||||
    if (frameRange.mMinimum > conf.buffer_frames) {
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMinimum;
 | 
			
		||||
        dolog ("warning: Upsizing Buffer Frames to %f\n", frameRange.mMinimum);
 | 
			
		||||
    }
 | 
			
		||||
    else if (frameRange.mMaximum < conf->buffer_frames) {
 | 
			
		||||
    else if (frameRange.mMaximum < conf.buffer_frames) {
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = (UInt32) frameRange.mMaximum;
 | 
			
		||||
        dolog ("warning: Downsizing Buffer Frames to %f\n", frameRange.mMaximum);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = conf->buffer_frames;
 | 
			
		||||
        core->audioDevicePropertyBufferFrameSize = conf.buffer_frames;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* set Buffer Frame Size */
 | 
			
		||||
    status = coreaudio_set_framesize(core->outputDeviceID,
 | 
			
		||||
                                     &core->audioDevicePropertyBufferFrameSize);
 | 
			
		||||
    propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
 | 
			
		||||
    status = AudioDeviceSetProperty(
 | 
			
		||||
        core->outputDeviceID,
 | 
			
		||||
        NULL,
 | 
			
		||||
        0,
 | 
			
		||||
        false,
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSize,
 | 
			
		||||
        propertySize,
 | 
			
		||||
        &core->audioDevicePropertyBufferFrameSize);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ,
 | 
			
		||||
                           "Could not set device buffer frame size %" PRIu32 "\n",
 | 
			
		||||
@@ -561,18 +366,30 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* get Buffer Frame Size */
 | 
			
		||||
    status = coreaudio_get_framesize(core->outputDeviceID,
 | 
			
		||||
                                     &core->audioDevicePropertyBufferFrameSize);
 | 
			
		||||
    propertySize = sizeof(core->audioDevicePropertyBufferFrameSize);
 | 
			
		||||
    status = AudioDeviceGetProperty(
 | 
			
		||||
        core->outputDeviceID,
 | 
			
		||||
        0,
 | 
			
		||||
        false,
 | 
			
		||||
        kAudioDevicePropertyBufferFrameSize,
 | 
			
		||||
        &propertySize,
 | 
			
		||||
        &core->audioDevicePropertyBufferFrameSize);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ,
 | 
			
		||||
                           "Could not get device buffer frame size\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    hw->samples = conf->nbuffers * core->audioDevicePropertyBufferFrameSize;
 | 
			
		||||
    hw->samples = conf.nbuffers * core->audioDevicePropertyBufferFrameSize;
 | 
			
		||||
 | 
			
		||||
    /* get StreamFormat */
 | 
			
		||||
    status = coreaudio_get_streamformat(core->outputDeviceID,
 | 
			
		||||
                                        &core->outputStreamBasicDescription);
 | 
			
		||||
    propertySize = sizeof(core->outputStreamBasicDescription);
 | 
			
		||||
    status = AudioDeviceGetProperty(
 | 
			
		||||
        core->outputDeviceID,
 | 
			
		||||
        0,
 | 
			
		||||
        false,
 | 
			
		||||
        kAudioDevicePropertyStreamFormat,
 | 
			
		||||
        &propertySize,
 | 
			
		||||
        &core->outputStreamBasicDescription);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ,
 | 
			
		||||
                           "Could not get Device Stream properties\n");
 | 
			
		||||
@@ -582,8 +399,15 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
 | 
			
		||||
    /* set Samplerate */
 | 
			
		||||
    core->outputStreamBasicDescription.mSampleRate = (Float64) as->freq;
 | 
			
		||||
    status = coreaudio_set_streamformat(core->outputDeviceID,
 | 
			
		||||
                                        &core->outputStreamBasicDescription);
 | 
			
		||||
    propertySize = sizeof(core->outputStreamBasicDescription);
 | 
			
		||||
    status = AudioDeviceSetProperty(
 | 
			
		||||
        core->outputDeviceID,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        0,
 | 
			
		||||
        kAudioDevicePropertyStreamFormat,
 | 
			
		||||
        propertySize,
 | 
			
		||||
        &core->outputStreamBasicDescription);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ, "Could not set samplerate %d\n",
 | 
			
		||||
                           as->freq);
 | 
			
		||||
@@ -592,12 +416,8 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* set Callback */
 | 
			
		||||
    core->ioprocid = NULL;
 | 
			
		||||
    status = AudioDeviceCreateIOProcID(core->outputDeviceID,
 | 
			
		||||
                                       audioDeviceIOProc,
 | 
			
		||||
                                       hw,
 | 
			
		||||
                                       &core->ioprocid);
 | 
			
		||||
    if (status != kAudioHardwareNoError || core->ioprocid == NULL) {
 | 
			
		||||
    status = AudioDeviceAddIOProc(core->outputDeviceID, audioDeviceIOProc, hw);
 | 
			
		||||
    if (status != kAudioHardwareNoError) {
 | 
			
		||||
        coreaudio_logerr2 (status, typ, "Could not set IOProc\n");
 | 
			
		||||
        core->outputDeviceID = kAudioDeviceUnknown;
 | 
			
		||||
        return -1;
 | 
			
		||||
@@ -605,10 +425,10 @@ static int coreaudio_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
 | 
			
		||||
    /* start Playback */
 | 
			
		||||
    if (!isPlaying(core->outputDeviceID)) {
 | 
			
		||||
        status = AudioDeviceStart(core->outputDeviceID, core->ioprocid);
 | 
			
		||||
        status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
        if (status != kAudioHardwareNoError) {
 | 
			
		||||
            coreaudio_logerr2 (status, typ, "Could not start playback\n");
 | 
			
		||||
            AudioDeviceDestroyIOProcID(core->outputDeviceID, core->ioprocid);
 | 
			
		||||
            AudioDeviceRemoveIOProc(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
            core->outputDeviceID = kAudioDeviceUnknown;
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
@@ -623,18 +443,18 @@ static void coreaudio_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    int err;
 | 
			
		||||
    coreaudioVoiceOut *core = (coreaudioVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    if (!audio_is_cleaning_up()) {
 | 
			
		||||
    if (!conf.isAtexit) {
 | 
			
		||||
        /* stop playback */
 | 
			
		||||
        if (isPlaying(core->outputDeviceID)) {
 | 
			
		||||
            status = AudioDeviceStop(core->outputDeviceID, core->ioprocid);
 | 
			
		||||
            status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
            if (status != kAudioHardwareNoError) {
 | 
			
		||||
                coreaudio_logerr (status, "Could not stop playback\n");
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* remove callback */
 | 
			
		||||
        status = AudioDeviceDestroyIOProcID(core->outputDeviceID,
 | 
			
		||||
                                            core->ioprocid);
 | 
			
		||||
        status = AudioDeviceRemoveIOProc(core->outputDeviceID,
 | 
			
		||||
                                         audioDeviceIOProc);
 | 
			
		||||
        if (status != kAudioHardwareNoError) {
 | 
			
		||||
            coreaudio_logerr (status, "Could not remove IOProc\n");
 | 
			
		||||
        }
 | 
			
		||||
@@ -657,7 +477,7 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        /* start playback */
 | 
			
		||||
        if (!isPlaying(core->outputDeviceID)) {
 | 
			
		||||
            status = AudioDeviceStart(core->outputDeviceID, core->ioprocid);
 | 
			
		||||
            status = AudioDeviceStart(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
            if (status != kAudioHardwareNoError) {
 | 
			
		||||
                coreaudio_logerr (status, "Could not resume playback\n");
 | 
			
		||||
            }
 | 
			
		||||
@@ -666,10 +486,9 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        /* stop playback */
 | 
			
		||||
        if (!audio_is_cleaning_up()) {
 | 
			
		||||
        if (!conf.isAtexit) {
 | 
			
		||||
            if (isPlaying(core->outputDeviceID)) {
 | 
			
		||||
                status = AudioDeviceStop(core->outputDeviceID,
 | 
			
		||||
                                         core->ioprocid);
 | 
			
		||||
                status = AudioDeviceStop(core->outputDeviceID, audioDeviceIOProc);
 | 
			
		||||
                if (status != kAudioHardwareNoError) {
 | 
			
		||||
                    coreaudio_logerr (status, "Could not pause playback\n");
 | 
			
		||||
                }
 | 
			
		||||
@@ -680,35 +499,28 @@ static int coreaudio_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static CoreaudioConf glob_conf = {
 | 
			
		||||
    .buffer_frames = 512,
 | 
			
		||||
    .nbuffers = 4,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *coreaudio_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    CoreaudioConf *conf = g_malloc(sizeof(CoreaudioConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
 | 
			
		||||
    return conf;
 | 
			
		||||
    atexit(coreaudio_atexit);
 | 
			
		||||
    return &coreaudio_audio_init;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coreaudio_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option coreaudio_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFFER_SIZE",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.buffer_frames,
 | 
			
		||||
        .valp  = &conf.buffer_frames,
 | 
			
		||||
        .descr = "Size of the buffer in frames"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFFER_COUNT",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.nbuffers,
 | 
			
		||||
        .valp  = &conf.nbuffers,
 | 
			
		||||
        .descr = "Number of buffers"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
@@ -67,11 +67,11 @@ static int glue (dsound_lock_, TYPE) (
 | 
			
		||||
    LPVOID *p2p,
 | 
			
		||||
    DWORD *blen1p,
 | 
			
		||||
    DWORD *blen2p,
 | 
			
		||||
    int entire,
 | 
			
		||||
    dsound *s
 | 
			
		||||
    int entire
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    int i;
 | 
			
		||||
    LPVOID p1 = NULL, p2 = NULL;
 | 
			
		||||
    DWORD blen1 = 0, blen2 = 0;
 | 
			
		||||
    DWORD flag;
 | 
			
		||||
@@ -81,18 +81,37 @@ static int glue (dsound_lock_, TYPE) (
 | 
			
		||||
#else
 | 
			
		||||
    flag = entire ? DSBLOCK_ENTIREBUFFER : 0;
 | 
			
		||||
#endif
 | 
			
		||||
    hr = glue(IFACE, _Lock)(buf, pos, len, &p1, &blen1, &p2, &blen2, flag);
 | 
			
		||||
    for (i = 0; i < conf.lock_retries; ++i) {
 | 
			
		||||
        hr = glue (IFACE, _Lock) (
 | 
			
		||||
            buf,
 | 
			
		||||
            pos,
 | 
			
		||||
            len,
 | 
			
		||||
            &p1,
 | 
			
		||||
            &blen1,
 | 
			
		||||
            &p2,
 | 
			
		||||
            &blen2,
 | 
			
		||||
            flag
 | 
			
		||||
            );
 | 
			
		||||
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
#ifndef DSBTYPE_IN
 | 
			
		||||
        if (hr == DSERR_BUFFERLOST) {
 | 
			
		||||
            if (glue (dsound_restore_, TYPE) (buf, s)) {
 | 
			
		||||
                dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
            if (hr == DSERR_BUFFERLOST) {
 | 
			
		||||
                if (glue (dsound_restore_, TYPE) (buf)) {
 | 
			
		||||
                    dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
                    goto fail;
 | 
			
		||||
                }
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
#endif
 | 
			
		||||
            dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        dsound_logerr (hr, "Could not lock " NAME "\n");
 | 
			
		||||
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (i == conf.lock_retries) {
 | 
			
		||||
        dolog ("%d attempts to lock " NAME " failed\n", i);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -155,19 +174,16 @@ static void dsound_fini_out (HWVoiceOut *hw)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef DSBTYPE_IN
 | 
			
		||||
static int dsound_init_in(HWVoiceIn *hw, struct audsettings *as,
 | 
			
		||||
                          void *drv_opaque)
 | 
			
		||||
static int dsound_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
#else
 | 
			
		||||
static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                           void *drv_opaque)
 | 
			
		||||
static int dsound_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
#endif
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    dsound *s = drv_opaque;
 | 
			
		||||
    dsound *s = &glob_dsound;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    DSoundConf *conf = &s->conf;
 | 
			
		||||
#ifdef DSBTYPE_IN
 | 
			
		||||
    const char *typ = "ADC";
 | 
			
		||||
    DSoundVoiceIn *ds = (DSoundVoiceIn *) hw;
 | 
			
		||||
@@ -194,7 +210,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    bd.dwSize = sizeof (bd);
 | 
			
		||||
    bd.lpwfxFormat = &wfx;
 | 
			
		||||
#ifdef DSBTYPE_IN
 | 
			
		||||
    bd.dwBufferBytes = conf->bufsize_in;
 | 
			
		||||
    bd.dwBufferBytes = conf.bufsize_in;
 | 
			
		||||
    hr = IDirectSoundCapture_CreateCaptureBuffer (
 | 
			
		||||
        s->dsound_capture,
 | 
			
		||||
        &bd,
 | 
			
		||||
@@ -203,7 +219,7 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
        );
 | 
			
		||||
#else
 | 
			
		||||
    bd.dwFlags = DSBCAPS_STICKYFOCUS | DSBCAPS_GETCURRENTPOSITION2;
 | 
			
		||||
    bd.dwBufferBytes = conf->bufsize_out;
 | 
			
		||||
    bd.dwBufferBytes = conf.bufsize_out;
 | 
			
		||||
    hr = IDirectSound_CreateSoundBuffer (
 | 
			
		||||
        s->dsound,
 | 
			
		||||
        &bd,
 | 
			
		||||
@@ -253,7 +269,6 @@ static int dsound_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
            );
 | 
			
		||||
    }
 | 
			
		||||
    hw->samples = bc.dwBufferBytes >> hw->info.shift;
 | 
			
		||||
    ds->s = s;
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_DSOUND
 | 
			
		||||
    dolog ("caps %ld, desc %ld\n",
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,6 @@
 | 
			
		||||
 * SEAL 1.07 by Carlos 'pel' Hasan was used as documentation
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
@@ -42,25 +41,42 @@
 | 
			
		||||
 | 
			
		||||
/* #define DEBUG_DSOUND */
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
static struct {
 | 
			
		||||
    int lock_retries;
 | 
			
		||||
    int restore_retries;
 | 
			
		||||
    int getstatus_retries;
 | 
			
		||||
    int set_primary;
 | 
			
		||||
    int bufsize_in;
 | 
			
		||||
    int bufsize_out;
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
    int latency_millis;
 | 
			
		||||
} DSoundConf;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .lock_retries       = 1,
 | 
			
		||||
    .restore_retries    = 1,
 | 
			
		||||
    .getstatus_retries  = 1,
 | 
			
		||||
    .set_primary        = 0,
 | 
			
		||||
    .bufsize_in         = 16384,
 | 
			
		||||
    .bufsize_out        = 16384,
 | 
			
		||||
    .settings.freq      = 44100,
 | 
			
		||||
    .settings.nchannels = 2,
 | 
			
		||||
    .settings.fmt       = AUD_FMT_S16,
 | 
			
		||||
    .latency_millis     = 10
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    LPDIRECTSOUND dsound;
 | 
			
		||||
    LPDIRECTSOUNDCAPTURE dsound_capture;
 | 
			
		||||
    LPDIRECTSOUNDBUFFER dsound_primary_buffer;
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
    DSoundConf conf;
 | 
			
		||||
} dsound;
 | 
			
		||||
 | 
			
		||||
static dsound glob_dsound;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    LPDIRECTSOUNDBUFFER dsound_buffer;
 | 
			
		||||
    DWORD old_pos;
 | 
			
		||||
    int first_time;
 | 
			
		||||
    dsound *s;
 | 
			
		||||
#ifdef DEBUG_DSOUND
 | 
			
		||||
    DWORD old_ppos;
 | 
			
		||||
    DWORD played;
 | 
			
		||||
@@ -72,7 +88,6 @@ typedef struct {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    int first_time;
 | 
			
		||||
    LPDIRECTSOUNDCAPTUREBUFFER dsound_capture_buffer;
 | 
			
		||||
    dsound *s;
 | 
			
		||||
} DSoundVoiceIn;
 | 
			
		||||
 | 
			
		||||
static void dsound_log_hresult (HRESULT hr)
 | 
			
		||||
@@ -266,17 +281,29 @@ static void print_wave_format (WAVEFORMATEX *wfx)
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
 | 
			
		||||
static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_Restore (dsb);
 | 
			
		||||
    for (i = 0; i < conf.restore_retries; ++i) {
 | 
			
		||||
        hr = IDirectSoundBuffer_Restore (dsb);
 | 
			
		||||
 | 
			
		||||
    if (hr != DS_OK) {
 | 
			
		||||
        dsound_logerr (hr, "Could not restore playback buffer\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
        switch (hr) {
 | 
			
		||||
        case DS_OK:
 | 
			
		||||
            return 0;
 | 
			
		||||
 | 
			
		||||
        case DSERR_BUFFERLOST:
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        default:
 | 
			
		||||
            dsound_logerr (hr, "Could not restore playback buffer\n");
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
    dolog ("%d attempts to restore playback buffer failed\n", i);
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "dsound_template.h"
 | 
			
		||||
@@ -284,20 +311,25 @@ static int dsound_restore_out (LPDIRECTSOUNDBUFFER dsb, dsound *s)
 | 
			
		||||
#include "dsound_template.h"
 | 
			
		||||
#undef DSBTYPE_IN
 | 
			
		||||
 | 
			
		||||
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp,
 | 
			
		||||
                                  dsound *s)
 | 
			
		||||
static int dsound_get_status_out (LPDIRECTSOUNDBUFFER dsb, DWORD *statusp)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not get playback buffer status\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    for (i = 0; i < conf.getstatus_retries; ++i) {
 | 
			
		||||
        hr = IDirectSoundBuffer_GetStatus (dsb, statusp);
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
            dsound_logerr (hr, "Could not get playback buffer status\n");
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    if (*statusp & DSERR_BUFFERLOST) {
 | 
			
		||||
        dsound_restore_out(dsb, s);
 | 
			
		||||
        return -1;
 | 
			
		||||
        if (*statusp & DSERR_BUFFERLOST) {
 | 
			
		||||
            if (dsound_restore_out (dsb)) {
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
@@ -344,8 +376,7 @@ static void dsound_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
 | 
			
		||||
    hw->rpos = pos % hw->samples;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
 | 
			
		||||
                                 dsound *s)
 | 
			
		||||
static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    LPVOID p1, p2;
 | 
			
		||||
@@ -358,8 +389,7 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
 | 
			
		||||
        hw->samples << hw->info.shift,
 | 
			
		||||
        &p1, &p2,
 | 
			
		||||
        &blen1, &blen2,
 | 
			
		||||
        1,
 | 
			
		||||
        s
 | 
			
		||||
        1
 | 
			
		||||
        );
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return;
 | 
			
		||||
@@ -385,9 +415,25 @@ static void dsound_clear_sample (HWVoiceOut *hw, LPDIRECTSOUNDBUFFER dsb,
 | 
			
		||||
    dsound_unlock_out (dsb, p1, p2, blen1, blen2);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dsound_open (dsound *s)
 | 
			
		||||
static void dsound_close (dsound *s)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
 | 
			
		||||
    if (s->dsound_primary_buffer) {
 | 
			
		||||
        hr = IDirectSoundBuffer_Release (s->dsound_primary_buffer);
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
            dsound_logerr (hr, "Could not release primary buffer\n");
 | 
			
		||||
        }
 | 
			
		||||
        s->dsound_primary_buffer = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dsound_open (dsound *s)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    DSBUFFERDESC dsbd;
 | 
			
		||||
    HWND hwnd;
 | 
			
		||||
 | 
			
		||||
    hwnd = GetForegroundWindow ();
 | 
			
		||||
@@ -403,7 +449,63 @@ static int dsound_open (dsound *s)
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!conf.set_primary) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = waveformat_from_audio_settings (&wfx, &conf.settings);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    memset (&dsbd, 0, sizeof (dsbd));
 | 
			
		||||
    dsbd.dwSize = sizeof (dsbd);
 | 
			
		||||
    dsbd.dwFlags = DSBCAPS_PRIMARYBUFFER;
 | 
			
		||||
    dsbd.dwBufferBytes = 0;
 | 
			
		||||
    dsbd.lpwfxFormat = NULL;
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSound_CreateSoundBuffer (
 | 
			
		||||
        s->dsound,
 | 
			
		||||
        &dsbd,
 | 
			
		||||
        &s->dsound_primary_buffer,
 | 
			
		||||
        NULL
 | 
			
		||||
        );
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not create primary playback buffer\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_SetFormat (s->dsound_primary_buffer, &wfx);
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not set primary playback buffer format\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    hr = IDirectSoundBuffer_GetFormat (
 | 
			
		||||
        s->dsound_primary_buffer,
 | 
			
		||||
        &wfx,
 | 
			
		||||
        sizeof (wfx),
 | 
			
		||||
        NULL
 | 
			
		||||
        );
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not get primary playback buffer format\n");
 | 
			
		||||
        goto fail0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_DSOUND
 | 
			
		||||
    dolog ("Primary\n");
 | 
			
		||||
    print_wave_format (&wfx);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    err = waveformat_to_audio_settings (&wfx, &s->settings);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        goto fail0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 fail0:
 | 
			
		||||
    dsound_close (s);
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
@@ -412,7 +514,6 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    DWORD status;
 | 
			
		||||
    DSoundVoiceOut *ds = (DSoundVoiceOut *) hw;
 | 
			
		||||
    LPDIRECTSOUNDBUFFER dsb = ds->dsound_buffer;
 | 
			
		||||
    dsound *s = ds->s;
 | 
			
		||||
 | 
			
		||||
    if (!dsb) {
 | 
			
		||||
        dolog ("Attempt to control voice without a buffer\n");
 | 
			
		||||
@@ -421,7 +522,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status, s)) {
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status)) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -430,7 +531,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        dsound_clear_sample (hw, dsb, s);
 | 
			
		||||
        dsound_clear_sample (hw, dsb);
 | 
			
		||||
 | 
			
		||||
        hr = IDirectSoundBuffer_Play (dsb, 0, 0, DSBPLAY_LOOPING);
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
@@ -440,7 +541,7 @@ static int dsound_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status, s)) {
 | 
			
		||||
        if (dsound_get_status_out (dsb, &status)) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -477,8 +578,6 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    DWORD wpos, ppos, old_pos;
 | 
			
		||||
    LPVOID p1, p2;
 | 
			
		||||
    int bufsize;
 | 
			
		||||
    dsound *s = ds->s;
 | 
			
		||||
    DSoundConf *conf = &s->conf;
 | 
			
		||||
 | 
			
		||||
    if (!dsb) {
 | 
			
		||||
        dolog ("Attempt to run empty with playback buffer\n");
 | 
			
		||||
@@ -501,14 +600,14 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    len = live << hwshift;
 | 
			
		||||
 | 
			
		||||
    if (ds->first_time) {
 | 
			
		||||
        if (conf->latency_millis) {
 | 
			
		||||
        if (conf.latency_millis) {
 | 
			
		||||
            DWORD cur_blat;
 | 
			
		||||
 | 
			
		||||
            cur_blat = audio_ring_dist (wpos, ppos, bufsize);
 | 
			
		||||
            ds->first_time = 0;
 | 
			
		||||
            old_pos = wpos;
 | 
			
		||||
            old_pos +=
 | 
			
		||||
                millis_to_bytes (&hw->info, conf->latency_millis) - cur_blat;
 | 
			
		||||
                millis_to_bytes (&hw->info, conf.latency_millis) - cur_blat;
 | 
			
		||||
            old_pos %= bufsize;
 | 
			
		||||
            old_pos &= ~hw->info.align;
 | 
			
		||||
        }
 | 
			
		||||
@@ -564,8 +663,7 @@ static int dsound_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
        len,
 | 
			
		||||
        &p1, &p2,
 | 
			
		||||
        &blen1, &blen2,
 | 
			
		||||
        0,
 | 
			
		||||
        s
 | 
			
		||||
        0
 | 
			
		||||
        );
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return 0;
 | 
			
		||||
@@ -668,7 +766,6 @@ static int dsound_run_in (HWVoiceIn *hw)
 | 
			
		||||
    DWORD cpos, rpos;
 | 
			
		||||
    LPVOID p1, p2;
 | 
			
		||||
    int hwshift;
 | 
			
		||||
    dsound *s = ds->s;
 | 
			
		||||
 | 
			
		||||
    if (!dscb) {
 | 
			
		||||
        dolog ("Attempt to run without capture buffer\n");
 | 
			
		||||
@@ -723,8 +820,7 @@ static int dsound_run_in (HWVoiceIn *hw)
 | 
			
		||||
        &p2,
 | 
			
		||||
        &blen1,
 | 
			
		||||
        &blen2,
 | 
			
		||||
        0,
 | 
			
		||||
        s
 | 
			
		||||
        0
 | 
			
		||||
        );
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return 0;
 | 
			
		||||
@@ -747,19 +843,12 @@ static int dsound_run_in (HWVoiceIn *hw)
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static DSoundConf glob_conf = {
 | 
			
		||||
    .bufsize_in         = 16384,
 | 
			
		||||
    .bufsize_out        = 16384,
 | 
			
		||||
    .latency_millis     = 10
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void dsound_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    dsound *s = opaque;
 | 
			
		||||
 | 
			
		||||
    if (!s->dsound) {
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -770,7 +859,6 @@ static void dsound_audio_fini (void *opaque)
 | 
			
		||||
    s->dsound = NULL;
 | 
			
		||||
 | 
			
		||||
    if (!s->dsound_capture) {
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -779,21 +867,17 @@ static void dsound_audio_fini (void *opaque)
 | 
			
		||||
        dsound_logerr (hr, "Could not release DirectSoundCapture\n");
 | 
			
		||||
    }
 | 
			
		||||
    s->dsound_capture = NULL;
 | 
			
		||||
 | 
			
		||||
    g_free(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *dsound_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    HRESULT hr;
 | 
			
		||||
    dsound *s = g_malloc0(sizeof(dsound));
 | 
			
		||||
    dsound *s = &glob_dsound;
 | 
			
		||||
 | 
			
		||||
    s->conf = glob_conf;
 | 
			
		||||
    hr = CoInitialize (NULL);
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not initialize COM\n");
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -806,7 +890,6 @@ static void *dsound_audio_init (void)
 | 
			
		||||
        );
 | 
			
		||||
    if (FAILED (hr)) {
 | 
			
		||||
        dsound_logerr (hr, "Could not create DirectSound instance\n");
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -818,7 +901,7 @@ static void *dsound_audio_init (void)
 | 
			
		||||
        if (FAILED (hr)) {
 | 
			
		||||
            dsound_logerr (hr, "Could not release DirectSound\n");
 | 
			
		||||
        }
 | 
			
		||||
        g_free(s);
 | 
			
		||||
        s->dsound = NULL;
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -855,22 +938,64 @@ static void *dsound_audio_init (void)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option dsound_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "LOCK_RETRIES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.lock_retries,
 | 
			
		||||
        .descr = "Number of times to attempt locking the buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "RESTOURE_RETRIES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.restore_retries,
 | 
			
		||||
        .descr = "Number of times to attempt restoring the buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "GETSTATUS_RETRIES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.getstatus_retries,
 | 
			
		||||
        .descr = "Number of times to attempt getting status of the buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SET_PRIMARY",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.set_primary,
 | 
			
		||||
        .descr = "Set the parameters of primary buffer"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "LATENCY_MILLIS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.latency_millis,
 | 
			
		||||
        .valp  = &conf.latency_millis,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PRIMARY_FREQ",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.settings.freq,
 | 
			
		||||
        .descr = "Primary buffer frequency"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PRIMARY_CHANNELS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.settings.nchannels,
 | 
			
		||||
        .descr = "Primary buffer number of channels (1 - mono, 2 - stereo)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PRIMARY_FMT",
 | 
			
		||||
        .tag   = AUD_OPT_FMT,
 | 
			
		||||
        .valp  = &conf.settings.fmt,
 | 
			
		||||
        .descr = "Primary buffer format"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFSIZE_OUT",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.bufsize_out,
 | 
			
		||||
        .valp  = &conf.bufsize_out,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFSIZE_IN",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.bufsize_in,
 | 
			
		||||
        .valp  = &conf.bufsize_in,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										557
									
								
								audio/esdaudio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										557
									
								
								audio/esdaudio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,557 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU ESD audio driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2006 Frederick Reeve (brushed up by malc)
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 | 
			
		||||
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include <esd.h>
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "esd"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
#include "audio_pt_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    int done;
 | 
			
		||||
    int live;
 | 
			
		||||
    int decr;
 | 
			
		||||
    int rpos;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int fd;
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
} ESDVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    int done;
 | 
			
		||||
    int dead;
 | 
			
		||||
    int incr;
 | 
			
		||||
    int wpos;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int fd;
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
} ESDVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int samples;
 | 
			
		||||
    int divisor;
 | 
			
		||||
    char *dac_host;
 | 
			
		||||
    char *adc_host;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .samples = 1024,
 | 
			
		||||
    .divisor = 2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) qesd_logerr (int err, const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Reason: %s\n", strerror (err));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* playback */
 | 
			
		||||
static void *qesd_thread_out (void *arg)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceOut *esd = arg;
 | 
			
		||||
    HWVoiceOut *hw = &esd->hw;
 | 
			
		||||
    int threshold;
 | 
			
		||||
 | 
			
		||||
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (;;) {
 | 
			
		||||
        int decr, to_mix, rpos;
 | 
			
		||||
 | 
			
		||||
        for (;;) {
 | 
			
		||||
            if (esd->done) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (esd->live > threshold) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        decr = to_mix = esd->live;
 | 
			
		||||
        rpos = hw->rpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        while (to_mix) {
 | 
			
		||||
            ssize_t written;
 | 
			
		||||
            int chunk = audio_MIN (to_mix, hw->samples - rpos);
 | 
			
		||||
            struct st_sample *src = hw->mix_buf + rpos;
 | 
			
		||||
 | 
			
		||||
            hw->clip (esd->pcm_buf, src, chunk);
 | 
			
		||||
 | 
			
		||||
        again:
 | 
			
		||||
            written = write (esd->fd, esd->pcm_buf, chunk << hw->info.shift);
 | 
			
		||||
            if (written == -1) {
 | 
			
		||||
                if (errno == EINTR || errno == EAGAIN) {
 | 
			
		||||
                    goto again;
 | 
			
		||||
                }
 | 
			
		||||
                qesd_logerr (errno, "write failed\n");
 | 
			
		||||
                return NULL;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (written != chunk << hw->info.shift) {
 | 
			
		||||
                int wsamples = written >> hw->info.shift;
 | 
			
		||||
                int wbytes = wsamples << hw->info.shift;
 | 
			
		||||
                if (wbytes != written) {
 | 
			
		||||
                    dolog ("warning: Misaligned write %d (requested %zd), "
 | 
			
		||||
                           "alignment %d\n",
 | 
			
		||||
                           wbytes, written, hw->info.align + 1);
 | 
			
		||||
                }
 | 
			
		||||
                to_mix -= wsamples;
 | 
			
		||||
                rpos = (rpos + wsamples) % hw->samples;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            rpos = (rpos + chunk) % hw->samples;
 | 
			
		||||
            to_mix -= chunk;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        esd->rpos = rpos;
 | 
			
		||||
        esd->live -= decr;
 | 
			
		||||
        esd->decr += decr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 exit:
 | 
			
		||||
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
    int decr;
 | 
			
		||||
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    decr = audio_MIN (live, esd->decr);
 | 
			
		||||
    esd->decr -= decr;
 | 
			
		||||
    esd->live = live - decr;
 | 
			
		||||
    hw->rpos = esd->rpos;
 | 
			
		||||
    if (esd->live > 0) {
 | 
			
		||||
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    int esdfmt = ESD_STREAM | ESD_PLAY;
 | 
			
		||||
 | 
			
		||||
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
 | 
			
		||||
    switch (as->fmt) {
 | 
			
		||||
    case AUD_FMT_S8:
 | 
			
		||||
    case AUD_FMT_U8:
 | 
			
		||||
        esdfmt |= ESD_BITS8;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_U8;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S32:
 | 
			
		||||
    case AUD_FMT_U32:
 | 
			
		||||
        dolog ("Will use 16 instead of 32 bit samples\n");
 | 
			
		||||
        /* fall through */
 | 
			
		||||
    case AUD_FMT_S16:
 | 
			
		||||
    case AUD_FMT_U16:
 | 
			
		||||
    deffmt:
 | 
			
		||||
        esdfmt |= ESD_BITS16;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_S16;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("Internal logic error: Bad audio format %d\n", as->fmt);
 | 
			
		||||
        goto deffmt;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    obt_as.endianness = AUDIO_HOST_ENDIANNESS;
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
 | 
			
		||||
    hw->samples = conf.samples;
 | 
			
		||||
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    if (!esd->pcm_buf) {
 | 
			
		||||
        dolog ("Could not allocate buffer (%d bytes)\n",
 | 
			
		||||
               hw->samples << hw->info.shift);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    esd->fd = esd_play_stream (esdfmt, as->freq, conf.dac_host, NULL);
 | 
			
		||||
    if (esd->fd < 0) {
 | 
			
		||||
        qesd_logerr (errno, "esd_play_stream failed\n");
 | 
			
		||||
        goto fail1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_init (&esd->pt, qesd_thread_out, esd, AUDIO_CAP, AUDIO_FUNC)) {
 | 
			
		||||
        goto fail2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 fail2:
 | 
			
		||||
    if (close (esd->fd)) {
 | 
			
		||||
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
 | 
			
		||||
                     AUDIO_FUNC, esd->fd);
 | 
			
		||||
    }
 | 
			
		||||
    esd->fd = -1;
 | 
			
		||||
 | 
			
		||||
 fail1:
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void qesd_fini_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    void *ret;
 | 
			
		||||
    ESDVoiceOut *esd = (ESDVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    esd->done = 1;
 | 
			
		||||
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    if (esd->fd >= 0) {
 | 
			
		||||
        if (close (esd->fd)) {
 | 
			
		||||
            qesd_logerr (errno, "failed to close esd socket\n");
 | 
			
		||||
        }
 | 
			
		||||
        esd->fd = -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    (void) hw;
 | 
			
		||||
    (void) cmd;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* capture */
 | 
			
		||||
static void *qesd_thread_in (void *arg)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceIn *esd = arg;
 | 
			
		||||
    HWVoiceIn *hw = &esd->hw;
 | 
			
		||||
    int threshold;
 | 
			
		||||
 | 
			
		||||
    threshold = conf.divisor ? hw->samples / conf.divisor : 0;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (;;) {
 | 
			
		||||
        int incr, to_grab, wpos;
 | 
			
		||||
 | 
			
		||||
        for (;;) {
 | 
			
		||||
            if (esd->done) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (esd->dead > threshold) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (audio_pt_wait (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
                goto exit;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        incr = to_grab = esd->dead;
 | 
			
		||||
        wpos = hw->wpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        while (to_grab) {
 | 
			
		||||
            ssize_t nread;
 | 
			
		||||
            int chunk = audio_MIN (to_grab, hw->samples - wpos);
 | 
			
		||||
            void *buf = advance (esd->pcm_buf, wpos);
 | 
			
		||||
 | 
			
		||||
        again:
 | 
			
		||||
            nread = read (esd->fd, buf, chunk << hw->info.shift);
 | 
			
		||||
            if (nread == -1) {
 | 
			
		||||
                if (errno == EINTR || errno == EAGAIN) {
 | 
			
		||||
                    goto again;
 | 
			
		||||
                }
 | 
			
		||||
                qesd_logerr (errno, "read failed\n");
 | 
			
		||||
                return NULL;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (nread != chunk << hw->info.shift) {
 | 
			
		||||
                int rsamples = nread >> hw->info.shift;
 | 
			
		||||
                int rbytes = rsamples << hw->info.shift;
 | 
			
		||||
                if (rbytes != nread) {
 | 
			
		||||
                    dolog ("warning: Misaligned write %d (requested %zd), "
 | 
			
		||||
                           "alignment %d\n",
 | 
			
		||||
                           rbytes, nread, hw->info.align + 1);
 | 
			
		||||
                }
 | 
			
		||||
                to_grab -= rsamples;
 | 
			
		||||
                wpos = (wpos + rsamples) % hw->samples;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            hw->conv (hw->conv_buf + wpos, buf, nread >> hw->info.shift);
 | 
			
		||||
            wpos = (wpos + chunk) % hw->samples;
 | 
			
		||||
            to_grab -= chunk;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        esd->wpos = wpos;
 | 
			
		||||
        esd->dead -= incr;
 | 
			
		||||
        esd->incr += incr;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 exit:
 | 
			
		||||
    audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_run_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    int live, incr, dead;
 | 
			
		||||
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_lock (&esd->pt, AUDIO_FUNC)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    live = audio_pcm_hw_get_live_in (hw);
 | 
			
		||||
    dead = hw->samples - live;
 | 
			
		||||
    incr = audio_MIN (dead, esd->incr);
 | 
			
		||||
    esd->incr -= incr;
 | 
			
		||||
    esd->dead = dead - incr;
 | 
			
		||||
    hw->wpos = esd->wpos;
 | 
			
		||||
    if (esd->dead > 0) {
 | 
			
		||||
        audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        audio_pt_unlock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    }
 | 
			
		||||
    return incr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_read (SWVoiceIn *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_read (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    int esdfmt = ESD_STREAM | ESD_RECORD;
 | 
			
		||||
 | 
			
		||||
    esdfmt |= (as->nchannels == 2) ? ESD_STEREO : ESD_MONO;
 | 
			
		||||
    switch (as->fmt) {
 | 
			
		||||
    case AUD_FMT_S8:
 | 
			
		||||
    case AUD_FMT_U8:
 | 
			
		||||
        esdfmt |= ESD_BITS8;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_U8;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S16:
 | 
			
		||||
    case AUD_FMT_U16:
 | 
			
		||||
        esdfmt |= ESD_BITS16;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_S16;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S32:
 | 
			
		||||
    case AUD_FMT_U32:
 | 
			
		||||
        dolog ("Will use 16 instead of 32 bit samples\n");
 | 
			
		||||
        esdfmt |= ESD_BITS16;
 | 
			
		||||
        obt_as.fmt = AUD_FMT_S16;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    obt_as.endianness = AUDIO_HOST_ENDIANNESS;
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
 | 
			
		||||
    hw->samples = conf.samples;
 | 
			
		||||
    esd->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    if (!esd->pcm_buf) {
 | 
			
		||||
        dolog ("Could not allocate buffer (%d bytes)\n",
 | 
			
		||||
               hw->samples << hw->info.shift);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    esd->fd = esd_record_stream (esdfmt, as->freq, conf.adc_host, NULL);
 | 
			
		||||
    if (esd->fd < 0) {
 | 
			
		||||
        qesd_logerr (errno, "esd_record_stream failed\n");
 | 
			
		||||
        goto fail1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (audio_pt_init (&esd->pt, qesd_thread_in, esd, AUDIO_CAP, AUDIO_FUNC)) {
 | 
			
		||||
        goto fail2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 fail2:
 | 
			
		||||
    if (close (esd->fd)) {
 | 
			
		||||
        qesd_logerr (errno, "%s: close on esd socket(%d) failed\n",
 | 
			
		||||
                     AUDIO_FUNC, esd->fd);
 | 
			
		||||
    }
 | 
			
		||||
    esd->fd = -1;
 | 
			
		||||
 | 
			
		||||
 fail1:
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void qesd_fini_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    void *ret;
 | 
			
		||||
    ESDVoiceIn *esd = (ESDVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    audio_pt_lock (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    esd->done = 1;
 | 
			
		||||
    audio_pt_unlock_and_signal (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
    audio_pt_join (&esd->pt, &ret, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    if (esd->fd >= 0) {
 | 
			
		||||
        if (close (esd->fd)) {
 | 
			
		||||
            qesd_logerr (errno, "failed to close esd socket\n");
 | 
			
		||||
        }
 | 
			
		||||
        esd->fd = -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pt_fini (&esd->pt, AUDIO_FUNC);
 | 
			
		||||
 | 
			
		||||
    g_free (esd->pcm_buf);
 | 
			
		||||
    esd->pcm_buf = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qesd_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    (void) hw;
 | 
			
		||||
    (void) cmd;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* common */
 | 
			
		||||
static void *qesd_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void qesd_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    ldebug ("esd_fini");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct audio_option qesd_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SAMPLES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.samples,
 | 
			
		||||
        .descr = "buffer size in samples"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DIVISOR",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.divisor,
 | 
			
		||||
        .descr = "threshold divisor"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DAC_HOST",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &conf.dac_host,
 | 
			
		||||
        .descr = "playback host"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "ADC_HOST",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &conf.adc_host,
 | 
			
		||||
        .descr = "capture host"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct audio_pcm_ops qesd_pcm_ops = {
 | 
			
		||||
    .init_out = qesd_init_out,
 | 
			
		||||
    .fini_out = qesd_fini_out,
 | 
			
		||||
    .run_out  = qesd_run_out,
 | 
			
		||||
    .write    = qesd_write,
 | 
			
		||||
    .ctl_out  = qesd_ctl_out,
 | 
			
		||||
 | 
			
		||||
    .init_in  = qesd_init_in,
 | 
			
		||||
    .fini_in  = qesd_fini_in,
 | 
			
		||||
    .run_in   = qesd_run_in,
 | 
			
		||||
    .read     = qesd_read,
 | 
			
		||||
    .ctl_in   = qesd_ctl_in,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_driver esd_audio_driver = {
 | 
			
		||||
    .name           = "esd",
 | 
			
		||||
    .descr          = "http://en.wikipedia.org/wiki/Esound",
 | 
			
		||||
    .options        = qesd_options,
 | 
			
		||||
    .init           = qesd_audio_init,
 | 
			
		||||
    .fini           = qesd_audio_fini,
 | 
			
		||||
    .pcm_ops        = &qesd_pcm_ops,
 | 
			
		||||
    .can_be_default = 0,
 | 
			
		||||
    .max_voices_out = INT_MAX,
 | 
			
		||||
    .max_voices_in  = INT_MAX,
 | 
			
		||||
    .voice_size_out = sizeof (ESDVoiceOut),
 | 
			
		||||
    .voice_size_in  = sizeof (ESDVoiceIn)
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										685
									
								
								audio/fmodaudio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										685
									
								
								audio/fmodaudio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,685 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU FMOD audio driver
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2004-2005 Vassili Karpov (malc)
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 | 
			
		||||
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include <fmod.h>
 | 
			
		||||
#include <fmod_errors.h>
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "fmod"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct FMODVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    unsigned int old_pos;
 | 
			
		||||
    FSOUND_SAMPLE *fmod_sample;
 | 
			
		||||
    int channel;
 | 
			
		||||
} FMODVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct FMODVoiceIn {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    FSOUND_SAMPLE *fmod_sample;
 | 
			
		||||
} FMODVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    const char *drvname;
 | 
			
		||||
    int nb_samples;
 | 
			
		||||
    int freq;
 | 
			
		||||
    int nb_channels;
 | 
			
		||||
    int bufsize;
 | 
			
		||||
    int broken_adc;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .nb_samples  = 2048 * 2,
 | 
			
		||||
    .freq        = 44100,
 | 
			
		||||
    .nb_channels = 2,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (1, 2) fmod_logerr (const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Reason: %s\n",
 | 
			
		||||
             FMOD_ErrorString (FSOUND_GetError ()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) fmod_logerr2 (
 | 
			
		||||
    const char *typ,
 | 
			
		||||
    const char *fmt,
 | 
			
		||||
    ...
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Could not initialize %s\n", typ);
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Reason: %s\n",
 | 
			
		||||
             FMOD_ErrorString (FSOUND_GetError ()));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_clear_sample (FMODVoiceOut *fmd)
 | 
			
		||||
{
 | 
			
		||||
    HWVoiceOut *hw = &fmd->hw;
 | 
			
		||||
    int status;
 | 
			
		||||
    void *p1 = 0, *p2 = 0;
 | 
			
		||||
    unsigned int len1 = 0, len2 = 0;
 | 
			
		||||
 | 
			
		||||
    status = FSOUND_Sample_Lock (
 | 
			
		||||
        fmd->fmod_sample,
 | 
			
		||||
        0,
 | 
			
		||||
        hw->samples << hw->info.shift,
 | 
			
		||||
        &p1,
 | 
			
		||||
        &p2,
 | 
			
		||||
        &len1,
 | 
			
		||||
        &len2
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to lock sample\n");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((len1 & hw->info.align) || (len2 & hw->info.align)) {
 | 
			
		||||
        dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
 | 
			
		||||
               len1, len2, hw->info.align + 1);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((len1 + len2) - (hw->samples << hw->info.shift)) {
 | 
			
		||||
        dolog ("Lock returned incomplete length %d, %d\n",
 | 
			
		||||
               len1 + len2, hw->samples << hw->info.shift);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_info_clear_buf (&hw->info, p1, hw->samples);
 | 
			
		||||
 | 
			
		||||
 fail:
 | 
			
		||||
    status = FSOUND_Sample_Unlock (fmd->fmod_sample, p1, p2, len1, len2);
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to unlock sample\n");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_write_sample (HWVoiceOut *hw, uint8_t *dst, int dst_len)
 | 
			
		||||
{
 | 
			
		||||
    int src_len1 = dst_len;
 | 
			
		||||
    int src_len2 = 0;
 | 
			
		||||
    int pos = hw->rpos + dst_len;
 | 
			
		||||
    struct st_sample *src1 = hw->mix_buf + hw->rpos;
 | 
			
		||||
    struct st_sample *src2 = NULL;
 | 
			
		||||
 | 
			
		||||
    if (pos > hw->samples) {
 | 
			
		||||
        src_len1 = hw->samples - hw->rpos;
 | 
			
		||||
        src2 = hw->mix_buf;
 | 
			
		||||
        src_len2 = dst_len - src_len1;
 | 
			
		||||
        pos = src_len2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (src_len1) {
 | 
			
		||||
        hw->clip (dst, src1, src_len1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (src_len2) {
 | 
			
		||||
        dst = advance (dst, src_len1 << hw->info.shift);
 | 
			
		||||
        hw->clip (dst, src2, src_len2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    hw->rpos = pos % hw->samples;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_unlock_sample (FSOUND_SAMPLE *sample, void *p1, void *p2,
 | 
			
		||||
                               unsigned int blen1, unsigned int blen2)
 | 
			
		||||
{
 | 
			
		||||
    int status = FSOUND_Sample_Unlock (sample, p1, p2, blen1, blen2);
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to unlock sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_lock_sample (
 | 
			
		||||
    FSOUND_SAMPLE *sample,
 | 
			
		||||
    struct audio_pcm_info *info,
 | 
			
		||||
    int pos,
 | 
			
		||||
    int len,
 | 
			
		||||
    void **p1,
 | 
			
		||||
    void **p2,
 | 
			
		||||
    unsigned int *blen1,
 | 
			
		||||
    unsigned int *blen2
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
 | 
			
		||||
    status = FSOUND_Sample_Lock (
 | 
			
		||||
        sample,
 | 
			
		||||
        pos << info->shift,
 | 
			
		||||
        len << info->shift,
 | 
			
		||||
        p1,
 | 
			
		||||
        p2,
 | 
			
		||||
        blen1,
 | 
			
		||||
        blen2
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("Failed to lock sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if ((*blen1 & info->align) || (*blen2 & info->align)) {
 | 
			
		||||
        dolog ("Lock returned misaligned length %d, %d, alignment %d\n",
 | 
			
		||||
               *blen1, *blen2, info->align + 1);
 | 
			
		||||
 | 
			
		||||
        fmod_unlock_sample (sample, *p1, *p2, *blen1, *blen2);
 | 
			
		||||
 | 
			
		||||
        *p1 = NULL - 1;
 | 
			
		||||
        *p2 = NULL - 1;
 | 
			
		||||
        *blen1 = ~0U;
 | 
			
		||||
        *blen2 = ~0U;
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!*p1 && *blen1) {
 | 
			
		||||
        dolog ("warning: !p1 && blen1=%d\n", *blen1);
 | 
			
		||||
        *blen1 = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!p2 && *blen2) {
 | 
			
		||||
        dolog ("warning: !p2 && blen2=%d\n", *blen2);
 | 
			
		||||
        *blen2 = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
    int decr;
 | 
			
		||||
    void *p1 = 0, *p2 = 0;
 | 
			
		||||
    unsigned int blen1 = 0, blen2 = 0;
 | 
			
		||||
    unsigned int len1 = 0, len2 = 0;
 | 
			
		||||
 | 
			
		||||
    if (!hw->pending_disable) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    decr = live;
 | 
			
		||||
 | 
			
		||||
    if (fmd->channel >= 0) {
 | 
			
		||||
        int len = decr;
 | 
			
		||||
        int old_pos = fmd->old_pos;
 | 
			
		||||
        int ppos = FSOUND_GetCurrentPosition (fmd->channel);
 | 
			
		||||
 | 
			
		||||
        if (ppos == old_pos || !ppos) {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ((old_pos < ppos) && ((old_pos + len) > ppos)) {
 | 
			
		||||
            len = ppos - old_pos;
 | 
			
		||||
        }
 | 
			
		||||
        else {
 | 
			
		||||
            if ((old_pos > ppos) && ((old_pos + len) > (ppos + hw->samples))) {
 | 
			
		||||
                len = hw->samples - old_pos + ppos;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        decr = len;
 | 
			
		||||
 | 
			
		||||
        if (audio_bug (AUDIO_FUNC, decr < 0)) {
 | 
			
		||||
            dolog ("decr=%d live=%d ppos=%d old_pos=%d len=%d\n",
 | 
			
		||||
                   decr, live, ppos, old_pos, len);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if (!decr) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
 | 
			
		||||
                          fmd->old_pos, decr,
 | 
			
		||||
                          &p1, &p2,
 | 
			
		||||
                          &blen1, &blen2)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len1 = blen1 >> hw->info.shift;
 | 
			
		||||
    len2 = blen2 >> hw->info.shift;
 | 
			
		||||
    ldebug ("%p %p %d %d %d %d\n", p1, p2, len1, len2, blen1, blen2);
 | 
			
		||||
    decr = len1 + len2;
 | 
			
		||||
 | 
			
		||||
    if (p1 && len1) {
 | 
			
		||||
        fmod_write_sample (hw, p1, len1);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (p2 && len2) {
 | 
			
		||||
        fmod_write_sample (hw, p2, len2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
 | 
			
		||||
 | 
			
		||||
    fmd->old_pos = (fmd->old_pos + decr) % hw->samples;
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int aud_to_fmodfmt (audfmt_e fmt, int stereo)
 | 
			
		||||
{
 | 
			
		||||
    int mode = FSOUND_LOOP_NORMAL;
 | 
			
		||||
 | 
			
		||||
    switch (fmt) {
 | 
			
		||||
    case AUD_FMT_S8:
 | 
			
		||||
        mode |= FSOUND_SIGNED | FSOUND_8BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_U8:
 | 
			
		||||
        mode |= FSOUND_UNSIGNED | FSOUND_8BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_S16:
 | 
			
		||||
        mode |= FSOUND_SIGNED | FSOUND_16BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case AUD_FMT_U16:
 | 
			
		||||
        mode |= FSOUND_UNSIGNED | FSOUND_16BITS;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("Internal logic error: Bad audio format %d\n", fmt);
 | 
			
		||||
#ifdef DEBUG_FMOD
 | 
			
		||||
        abort ();
 | 
			
		||||
#endif
 | 
			
		||||
        mode |= FSOUND_8BITS;
 | 
			
		||||
    }
 | 
			
		||||
    mode |= stereo ? FSOUND_STEREO : FSOUND_MONO;
 | 
			
		||||
    return mode;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_fini_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    if (fmd->fmod_sample) {
 | 
			
		||||
        FSOUND_Sample_Free (fmd->fmod_sample);
 | 
			
		||||
        fmd->fmod_sample = 0;
 | 
			
		||||
 | 
			
		||||
        if (fmd->channel >= 0) {
 | 
			
		||||
            FSOUND_StopSound (fmd->channel);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int mode, channel;
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
 | 
			
		||||
    mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
 | 
			
		||||
    fmd->fmod_sample = FSOUND_Sample_Alloc (
 | 
			
		||||
        FSOUND_FREE,            /* index */
 | 
			
		||||
        conf.nb_samples,        /* length */
 | 
			
		||||
        mode,                   /* mode */
 | 
			
		||||
        as->freq,               /* freq */
 | 
			
		||||
        255,                    /* volume */
 | 
			
		||||
        128,                    /* pan */
 | 
			
		||||
        255                     /* priority */
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!fmd->fmod_sample) {
 | 
			
		||||
        fmod_logerr2 ("DAC", "Failed to allocate FMOD sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    channel = FSOUND_PlaySoundEx (FSOUND_FREE, fmd->fmod_sample, 0, 1);
 | 
			
		||||
    if (channel < 0) {
 | 
			
		||||
        fmod_logerr2 ("DAC", "Failed to start playing sound\n");
 | 
			
		||||
        FSOUND_Sample_Free (fmd->fmod_sample);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    fmd->channel = channel;
 | 
			
		||||
 | 
			
		||||
    /* FMOD always operates on little endian frames? */
 | 
			
		||||
    obt_as.endianness = 0;
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = conf.nb_samples;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
    FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        fmod_clear_sample (fmd);
 | 
			
		||||
        status = FSOUND_SetPaused (fmd->channel, 0);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to resume channel %d\n", fmd->channel);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        status = FSOUND_SetPaused (fmd->channel, 1);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to pause channel %d\n", fmd->channel);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int mode;
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
 | 
			
		||||
    if (conf.broken_adc) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    mode = aud_to_fmodfmt (as->fmt, as->nchannels == 2 ? 1 : 0);
 | 
			
		||||
    fmd->fmod_sample = FSOUND_Sample_Alloc (
 | 
			
		||||
        FSOUND_FREE,            /* index */
 | 
			
		||||
        conf.nb_samples,        /* length */
 | 
			
		||||
        mode,                   /* mode */
 | 
			
		||||
        as->freq,               /* freq */
 | 
			
		||||
        255,                    /* volume */
 | 
			
		||||
        128,                    /* pan */
 | 
			
		||||
        255                     /* priority */
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
    if (!fmd->fmod_sample) {
 | 
			
		||||
        fmod_logerr2 ("ADC", "Failed to allocate FMOD sample\n");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* FMOD always operates on little endian frames? */
 | 
			
		||||
    obt_as.endianness = 0;
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = conf.nb_samples;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_fini_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    if (fmd->fmod_sample) {
 | 
			
		||||
        FSOUND_Record_Stop ();
 | 
			
		||||
        FSOUND_Sample_Free (fmd->fmod_sample);
 | 
			
		||||
        fmd->fmod_sample = 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_run_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
    int hwshift = hw->info.shift;
 | 
			
		||||
    int live, dead, new_pos, len;
 | 
			
		||||
    unsigned int blen1 = 0, blen2 = 0;
 | 
			
		||||
    unsigned int len1, len2;
 | 
			
		||||
    unsigned int decr;
 | 
			
		||||
    void *p1, *p2;
 | 
			
		||||
 | 
			
		||||
    live = audio_pcm_hw_get_live_in (hw);
 | 
			
		||||
    dead = hw->samples - live;
 | 
			
		||||
    if (!dead) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    new_pos = FSOUND_Record_GetPosition ();
 | 
			
		||||
    if (new_pos < 0) {
 | 
			
		||||
        fmod_logerr ("Could not get recording position\n");
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len = audio_ring_dist (new_pos,  hw->wpos, hw->samples);
 | 
			
		||||
    if (!len) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    len = audio_MIN (len, dead);
 | 
			
		||||
 | 
			
		||||
    if (fmod_lock_sample (fmd->fmod_sample, &fmd->hw.info,
 | 
			
		||||
                          hw->wpos, len,
 | 
			
		||||
                          &p1, &p2,
 | 
			
		||||
                          &blen1, &blen2)) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len1 = blen1 >> hwshift;
 | 
			
		||||
    len2 = blen2 >> hwshift;
 | 
			
		||||
    decr = len1 + len2;
 | 
			
		||||
 | 
			
		||||
    if (p1 && blen1) {
 | 
			
		||||
        hw->conv (hw->conv_buf + hw->wpos, p1, len1);
 | 
			
		||||
    }
 | 
			
		||||
    if (p2 && len2) {
 | 
			
		||||
        hw->conv (hw->conv_buf, p2, len2);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fmod_unlock_sample (fmd->fmod_sample, p1, p2, blen1, blen2);
 | 
			
		||||
    hw->wpos = (hw->wpos + decr) % hw->samples;
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    const char *name;
 | 
			
		||||
    int type;
 | 
			
		||||
} drvtab[] = {
 | 
			
		||||
    { .name = "none",   .type = FSOUND_OUTPUT_NOSOUND },
 | 
			
		||||
#ifdef _WIN32
 | 
			
		||||
    { .name = "winmm",  .type = FSOUND_OUTPUT_WINMM   },
 | 
			
		||||
    { .name = "dsound", .type = FSOUND_OUTPUT_DSOUND  },
 | 
			
		||||
    { .name = "a3d",    .type = FSOUND_OUTPUT_A3D     },
 | 
			
		||||
    { .name = "asio",   .type = FSOUND_OUTPUT_ASIO    },
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef __linux__
 | 
			
		||||
    { .name = "oss",    .type = FSOUND_OUTPUT_OSS     },
 | 
			
		||||
    { .name = "alsa",   .type = FSOUND_OUTPUT_ALSA    },
 | 
			
		||||
    { .name = "esd",    .type = FSOUND_OUTPUT_ESD     },
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef __APPLE__
 | 
			
		||||
    { .name = "mac",    .type = FSOUND_OUTPUT_MAC     },
 | 
			
		||||
#endif
 | 
			
		||||
#if 0
 | 
			
		||||
    { .name = "xbox",   .type = FSOUND_OUTPUT_XBOX    },
 | 
			
		||||
    { .name = "ps2",    .type = FSOUND_OUTPUT_PS2     },
 | 
			
		||||
    { .name = "gcube",  .type = FSOUND_OUTPUT_GC      },
 | 
			
		||||
#endif
 | 
			
		||||
    { .name = "none-realtime", .type = FSOUND_OUTPUT_NOSOUND_NONREALTIME }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *fmod_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    size_t i;
 | 
			
		||||
    double ver;
 | 
			
		||||
    int status;
 | 
			
		||||
    int output_type = -1;
 | 
			
		||||
    const char *drv = conf.drvname;
 | 
			
		||||
 | 
			
		||||
    ver = FSOUND_GetVersion ();
 | 
			
		||||
    if (ver < FMOD_VERSION) {
 | 
			
		||||
        dolog ("Wrong FMOD version %f, need at least %f\n", ver, FMOD_VERSION);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef __linux__
 | 
			
		||||
    if (ver < 3.75) {
 | 
			
		||||
        dolog ("FMOD before 3.75 has bug preventing ADC from working\n"
 | 
			
		||||
               "ADC will be disabled.\n");
 | 
			
		||||
        conf.broken_adc = 1;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (drv) {
 | 
			
		||||
        int found = 0;
 | 
			
		||||
        for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
 | 
			
		||||
            if (!strcmp (drv, drvtab[i].name)) {
 | 
			
		||||
                output_type = drvtab[i].type;
 | 
			
		||||
                found = 1;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (!found) {
 | 
			
		||||
            dolog ("Unknown FMOD driver `%s'\n", drv);
 | 
			
		||||
            dolog ("Valid drivers:\n");
 | 
			
		||||
            for (i = 0; i < ARRAY_SIZE (drvtab); i++) {
 | 
			
		||||
                dolog ("  %s\n", drvtab[i].name);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (output_type != -1) {
 | 
			
		||||
        status = FSOUND_SetOutput (output_type);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("FSOUND_SetOutput(%d) failed\n", output_type);
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (conf.bufsize) {
 | 
			
		||||
        status = FSOUND_SetBufferSize (conf.bufsize);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("FSOUND_SetBufferSize (%d) failed\n", conf.bufsize);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    status = FSOUND_Init (conf.freq, conf.nb_channels, 0);
 | 
			
		||||
    if (!status) {
 | 
			
		||||
        fmod_logerr ("FSOUND_Init failed\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_read (SWVoiceIn *sw, void *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_read (sw, buf, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int fmod_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    int status;
 | 
			
		||||
    FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        status = FSOUND_Record_StartSample (fmd->fmod_sample, 1);
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to start recording\n");
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        status = FSOUND_Record_Stop ();
 | 
			
		||||
        if (!status) {
 | 
			
		||||
            fmod_logerr ("Failed to stop recording\n");
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void fmod_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    FSOUND_Close ();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option fmod_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DRV",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &conf.drvname,
 | 
			
		||||
        .descr = "FMOD driver"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FREQ",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.freq,
 | 
			
		||||
        .descr = "Default frequency"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SAMPLES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.nb_samples,
 | 
			
		||||
        .descr = "Buffer size in samples"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "CHANNELS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.nb_channels,
 | 
			
		||||
        .descr = "Number of default channels (1 - mono, 2 - stereo)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "BUFSIZE",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &conf.bufsize,
 | 
			
		||||
        .descr = "(undocumented)"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct audio_pcm_ops fmod_pcm_ops = {
 | 
			
		||||
    .init_out = fmod_init_out,
 | 
			
		||||
    .fini_out = fmod_fini_out,
 | 
			
		||||
    .run_out  = fmod_run_out,
 | 
			
		||||
    .write    = fmod_write,
 | 
			
		||||
    .ctl_out  = fmod_ctl_out,
 | 
			
		||||
 | 
			
		||||
    .init_in  = fmod_init_in,
 | 
			
		||||
    .fini_in  = fmod_fini_in,
 | 
			
		||||
    .run_in   = fmod_run_in,
 | 
			
		||||
    .read     = fmod_read,
 | 
			
		||||
    .ctl_in   = fmod_ctl_in
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_driver fmod_audio_driver = {
 | 
			
		||||
    .name           = "fmod",
 | 
			
		||||
    .descr          = "FMOD 3.xx http://www.fmod.org",
 | 
			
		||||
    .options        = fmod_options,
 | 
			
		||||
    .init           = fmod_audio_init,
 | 
			
		||||
    .fini           = fmod_audio_fini,
 | 
			
		||||
    .pcm_ops        = &fmod_pcm_ops,
 | 
			
		||||
    .can_be_default = 1,
 | 
			
		||||
    .max_voices_out = INT_MAX,
 | 
			
		||||
    .max_voices_in  = INT_MAX,
 | 
			
		||||
    .voice_size_out = sizeof (FMODVoiceOut),
 | 
			
		||||
    .voice_size_in  = sizeof (FMODVoiceIn)
 | 
			
		||||
};
 | 
			
		||||
@@ -22,10 +22,7 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/bswap.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "mixeng"
 | 
			
		||||
@@ -268,42 +265,11 @@ f_sample *mixeng_clip[2][2][2][3] = {
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
void audio_sample_to_uint64(void *samples, int pos,
 | 
			
		||||
                            uint64_t *left, uint64_t *right)
 | 
			
		||||
{
 | 
			
		||||
    struct st_sample *sample = samples;
 | 
			
		||||
    sample += pos;
 | 
			
		||||
#ifdef FLOAT_MIXENG
 | 
			
		||||
    error_report(
 | 
			
		||||
        "Coreaudio and floating point samples are not supported by replay yet");
 | 
			
		||||
    abort();
 | 
			
		||||
#else
 | 
			
		||||
    *left = sample->l;
 | 
			
		||||
    *right = sample->r;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void audio_sample_from_uint64(void *samples, int pos,
 | 
			
		||||
                            uint64_t left, uint64_t right)
 | 
			
		||||
{
 | 
			
		||||
    struct st_sample *sample = samples;
 | 
			
		||||
    sample += pos;
 | 
			
		||||
#ifdef FLOAT_MIXENG
 | 
			
		||||
    error_report(
 | 
			
		||||
        "Coreaudio and floating point samples are not supported by replay yet");
 | 
			
		||||
    abort();
 | 
			
		||||
#else
 | 
			
		||||
    sample->l = left;
 | 
			
		||||
    sample->r = right;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * August 21, 1998
 | 
			
		||||
 * Copyright 1998 Fabrice Bellard.
 | 
			
		||||
 *
 | 
			
		||||
 * [Rewrote completely the code of Lance Norskog And Sundry
 | 
			
		||||
 * [Rewrote completly the code of Lance Norskog And Sundry
 | 
			
		||||
 * Contributors with a more efficient algorithm.]
 | 
			
		||||
 *
 | 
			
		||||
 * This source code is freely redistributable and may be used for
 | 
			
		||||
@@ -382,6 +348,7 @@ void mixeng_clear (struct st_sample *buf, int len)
 | 
			
		||||
 | 
			
		||||
void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
 | 
			
		||||
{
 | 
			
		||||
#ifdef CONFIG_MIXEMU
 | 
			
		||||
    if (vol->mute) {
 | 
			
		||||
        mixeng_clear (buf, len);
 | 
			
		||||
        return;
 | 
			
		||||
@@ -397,4 +364,9 @@ void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
 | 
			
		||||
#endif
 | 
			
		||||
        buf += 1;
 | 
			
		||||
    }
 | 
			
		||||
#else
 | 
			
		||||
    (void) buf;
 | 
			
		||||
    (void) len;
 | 
			
		||||
    (void) vol;
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,6 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#ifndef QEMU_MIXENG_H
 | 
			
		||||
#define QEMU_MIXENG_H
 | 
			
		||||
 | 
			
		||||
@@ -49,4 +48,4 @@ void st_rate_stop (void *opaque);
 | 
			
		||||
void mixeng_clear (struct st_sample *buf, int len);
 | 
			
		||||
void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol);
 | 
			
		||||
 | 
			
		||||
#endif /* QEMU_MIXENG_H */
 | 
			
		||||
#endif  /* mixeng.h */
 | 
			
		||||
 
 | 
			
		||||
@@ -35,7 +35,7 @@
 | 
			
		||||
#define IN_T glue (glue (ITYPE, BSIZE), _t)
 | 
			
		||||
 | 
			
		||||
#ifdef FLOAT_MIXENG
 | 
			
		||||
static inline mixeng_real glue (conv_, ET) (IN_T v)
 | 
			
		||||
static mixeng_real inline glue (conv_, ET) (IN_T v)
 | 
			
		||||
{
 | 
			
		||||
    IN_T nv = ENDIAN_CONVERT (v);
 | 
			
		||||
 | 
			
		||||
@@ -54,7 +54,7 @@ static inline mixeng_real glue (conv_, ET) (IN_T v)
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline IN_T glue (clip_, ET) (mixeng_real v)
 | 
			
		||||
static IN_T inline glue (clip_, ET) (mixeng_real v)
 | 
			
		||||
{
 | 
			
		||||
    if (v >= 0.5) {
 | 
			
		||||
        return IN_MAX;
 | 
			
		||||
 
 | 
			
		||||
@@ -21,9 +21,7 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/host-utils.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
 | 
			
		||||
@@ -48,10 +46,10 @@ static int no_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    int64_t ticks;
 | 
			
		||||
    int64_t bytes;
 | 
			
		||||
 | 
			
		||||
    now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
    now = qemu_get_clock_ns (vm_clock);
 | 
			
		||||
    ticks = now - no->old_ticks;
 | 
			
		||||
    bytes = muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
 | 
			
		||||
    bytes = audio_MIN(bytes, INT_MAX);
 | 
			
		||||
    bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
 | 
			
		||||
    bytes = audio_MIN (bytes, INT_MAX);
 | 
			
		||||
    samples = bytes >> hw->info.shift;
 | 
			
		||||
 | 
			
		||||
    no->old_ticks = now;
 | 
			
		||||
@@ -62,10 +60,10 @@ static int no_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
 | 
			
		||||
static int no_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write(sw, buf, len);
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int no_init_out(HWVoiceOut *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int no_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = 1024;
 | 
			
		||||
@@ -84,7 +82,7 @@ static int no_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int no_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int no_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = 1024;
 | 
			
		||||
@@ -104,10 +102,10 @@ static int no_run_in (HWVoiceIn *hw)
 | 
			
		||||
    int samples = 0;
 | 
			
		||||
 | 
			
		||||
    if (dead) {
 | 
			
		||||
        int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
        int64_t now = qemu_get_clock_ns (vm_clock);
 | 
			
		||||
        int64_t ticks = now - no->old_ticks;
 | 
			
		||||
        int64_t bytes =
 | 
			
		||||
            muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
 | 
			
		||||
            muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
 | 
			
		||||
 | 
			
		||||
        no->old_ticks = now;
 | 
			
		||||
        bytes = audio_MIN (bytes, INT_MAX);
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										157
									
								
								audio/ossaudio.c
									
									
									
									
									
								
							
							
						
						
									
										157
									
								
								audio/ossaudio.c
									
									
									
									
									
								
							@@ -21,14 +21,19 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <sys/mman.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <sys/ioctl.h>
 | 
			
		||||
#ifdef __OpenBSD__
 | 
			
		||||
#include <soundcard.h>
 | 
			
		||||
#else
 | 
			
		||||
#include <sys/soundcard.h>
 | 
			
		||||
#endif
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
#include "qemu/host-utils.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "oss"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
@@ -37,16 +42,6 @@
 | 
			
		||||
#define USE_DSP_POLICY
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
typedef struct OSSConf {
 | 
			
		||||
    int try_mmap;
 | 
			
		||||
    int nfrags;
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    const char *devpath_out;
 | 
			
		||||
    const char *devpath_in;
 | 
			
		||||
    int exclusive;
 | 
			
		||||
    int policy;
 | 
			
		||||
} OSSConf;
 | 
			
		||||
 | 
			
		||||
typedef struct OSSVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
@@ -56,7 +51,6 @@ typedef struct OSSVoiceOut {
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    int mmapped;
 | 
			
		||||
    int pending;
 | 
			
		||||
    OSSConf *conf;
 | 
			
		||||
} OSSVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct OSSVoiceIn {
 | 
			
		||||
@@ -65,9 +59,28 @@ typedef struct OSSVoiceIn {
 | 
			
		||||
    int fd;
 | 
			
		||||
    int nfrags;
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    OSSConf *conf;
 | 
			
		||||
} OSSVoiceIn;
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int try_mmap;
 | 
			
		||||
    int nfrags;
 | 
			
		||||
    int fragsize;
 | 
			
		||||
    const char *devpath_out;
 | 
			
		||||
    const char *devpath_in;
 | 
			
		||||
    int debug;
 | 
			
		||||
    int exclusive;
 | 
			
		||||
    int policy;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .try_mmap = 0,
 | 
			
		||||
    .nfrags = 4,
 | 
			
		||||
    .fragsize = 4096,
 | 
			
		||||
    .devpath_out = "/dev/dsp",
 | 
			
		||||
    .devpath_in = "/dev/dsp",
 | 
			
		||||
    .debug = 0,
 | 
			
		||||
    .exclusive = 0,
 | 
			
		||||
    .policy = 5
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct oss_params {
 | 
			
		||||
    int freq;
 | 
			
		||||
    audfmt_e fmt;
 | 
			
		||||
@@ -129,18 +142,18 @@ static void oss_helper_poll_in (void *opaque)
 | 
			
		||||
    audio_run ("oss_poll_in");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void oss_poll_out (HWVoiceOut *hw)
 | 
			
		||||
static int oss_poll_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceOut *oss = (OSSVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
 | 
			
		||||
    return qemu_set_fd_handler (oss->fd, NULL, oss_helper_poll_out, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void oss_poll_in (HWVoiceIn *hw)
 | 
			
		||||
static int oss_poll_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceIn *oss = (OSSVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
 | 
			
		||||
    return qemu_set_fd_handler (oss->fd, oss_helper_poll_in, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
@@ -263,18 +276,18 @@ static int oss_get_version (int fd, int *version, const char *typ)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static int oss_open (int in, struct oss_params *req,
 | 
			
		||||
                     struct oss_params *obt, int *pfd, OSSConf* conf)
 | 
			
		||||
                     struct oss_params *obt, int *pfd)
 | 
			
		||||
{
 | 
			
		||||
    int fd;
 | 
			
		||||
    int oflags = conf->exclusive ? O_EXCL : 0;
 | 
			
		||||
    int oflags = conf.exclusive ? O_EXCL : 0;
 | 
			
		||||
    audio_buf_info abinfo;
 | 
			
		||||
    int fmt, freq, nchannels;
 | 
			
		||||
    int setfragment = 1;
 | 
			
		||||
    const char *dspname = in ? conf->devpath_in : conf->devpath_out;
 | 
			
		||||
    const char *dspname = in ? conf.devpath_in : conf.devpath_out;
 | 
			
		||||
    const char *typ = in ? "ADC" : "DAC";
 | 
			
		||||
 | 
			
		||||
    /* Kludge needed to have working mmap on Linux */
 | 
			
		||||
    oflags |= conf->try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
 | 
			
		||||
    oflags |= conf.try_mmap ? O_RDWR : (in ? O_RDONLY : O_WRONLY);
 | 
			
		||||
 | 
			
		||||
    fd = open (dspname, oflags | O_NONBLOCK);
 | 
			
		||||
    if (-1 == fd) {
 | 
			
		||||
@@ -308,18 +321,20 @@ static int oss_open (int in, struct oss_params *req,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_DSP_POLICY
 | 
			
		||||
    if (conf->policy >= 0) {
 | 
			
		||||
    if (conf.policy >= 0) {
 | 
			
		||||
        int version;
 | 
			
		||||
 | 
			
		||||
        if (!oss_get_version (fd, &version, typ)) {
 | 
			
		||||
            trace_oss_version(version);
 | 
			
		||||
            if (conf.debug) {
 | 
			
		||||
                dolog ("OSS version = %#x\n", version);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (version >= 0x040000) {
 | 
			
		||||
                int policy = conf->policy;
 | 
			
		||||
                int policy = conf.policy;
 | 
			
		||||
                if (ioctl (fd, SNDCTL_DSP_POLICY, &policy)) {
 | 
			
		||||
                    oss_logerr2 (errno, typ,
 | 
			
		||||
                                 "Failed to set timing policy to %d\n",
 | 
			
		||||
                                 conf->policy);
 | 
			
		||||
                                 conf.policy);
 | 
			
		||||
                    goto err;
 | 
			
		||||
                }
 | 
			
		||||
                setfragment = 0;
 | 
			
		||||
@@ -447,12 +462,19 @@ static int oss_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (abinfo.bytes > bufsize) {
 | 
			
		||||
            trace_oss_invalid_available_size(abinfo.bytes, bufsize);
 | 
			
		||||
            if (conf.debug) {
 | 
			
		||||
                dolog ("warning: Invalid available size, size=%d bufsize=%d\n"
 | 
			
		||||
                       "please report your OS/audio hw to av1474@comtv.ru\n",
 | 
			
		||||
                       abinfo.bytes, bufsize);
 | 
			
		||||
            }
 | 
			
		||||
            abinfo.bytes = bufsize;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (abinfo.bytes < 0) {
 | 
			
		||||
            trace_oss_invalid_available_size(abinfo.bytes, bufsize);
 | 
			
		||||
            if (conf.debug) {
 | 
			
		||||
                dolog ("warning: Invalid available size, size=%d bufsize=%d\n",
 | 
			
		||||
                       abinfo.bytes, bufsize);
 | 
			
		||||
            }
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -492,8 +514,7 @@ static void oss_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int oss_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceOut *oss = (OSSVoiceOut *) hw;
 | 
			
		||||
    struct oss_params req, obt;
 | 
			
		||||
@@ -502,17 +523,16 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    int fd;
 | 
			
		||||
    audfmt_e effective_fmt;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    OSSConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    oss->fd = -1;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.fragsize = conf->fragsize;
 | 
			
		||||
    req.nfrags = conf->nfrags;
 | 
			
		||||
    req.fragsize = conf.fragsize;
 | 
			
		||||
    req.nfrags = conf.nfrags;
 | 
			
		||||
 | 
			
		||||
    if (oss_open (0, &req, &obt, &fd, conf)) {
 | 
			
		||||
    if (oss_open (0, &req, &obt, &fd)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -539,7 +559,7 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    hw->samples = (obt.nfrags * obt.fragsize) >> hw->info.shift;
 | 
			
		||||
 | 
			
		||||
    oss->mmapped = 0;
 | 
			
		||||
    if (conf->try_mmap) {
 | 
			
		||||
    if (conf.try_mmap) {
 | 
			
		||||
        oss->pcm_buf = mmap (
 | 
			
		||||
            NULL,
 | 
			
		||||
            hw->samples << hw->info.shift,
 | 
			
		||||
@@ -599,7 +619,6 @@ static int oss_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    oss->fd = fd;
 | 
			
		||||
    oss->conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -619,8 +638,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            ldebug ("enabling voice\n");
 | 
			
		||||
            if (poll_mode) {
 | 
			
		||||
                oss_poll_out (hw);
 | 
			
		||||
            if (poll_mode && oss_poll_out (hw)) {
 | 
			
		||||
                poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            hw->poll_mode = poll_mode;
 | 
			
		||||
@@ -662,7 +680,7 @@ static int oss_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int oss_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    OSSVoiceIn *oss = (OSSVoiceIn *) hw;
 | 
			
		||||
    struct oss_params req, obt;
 | 
			
		||||
@@ -671,16 +689,15 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    int fd;
 | 
			
		||||
    audfmt_e effective_fmt;
 | 
			
		||||
    struct audsettings obt_as;
 | 
			
		||||
    OSSConf *conf = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    oss->fd = -1;
 | 
			
		||||
 | 
			
		||||
    req.fmt = aud_to_ossfmt (as->fmt, as->endianness);
 | 
			
		||||
    req.freq = as->freq;
 | 
			
		||||
    req.nchannels = as->nchannels;
 | 
			
		||||
    req.fragsize = conf->fragsize;
 | 
			
		||||
    req.nfrags = conf->nfrags;
 | 
			
		||||
    if (oss_open (1, &req, &obt, &fd, conf)) {
 | 
			
		||||
    req.fragsize = conf.fragsize;
 | 
			
		||||
    req.nfrags = conf.nfrags;
 | 
			
		||||
    if (oss_open (1, &req, &obt, &fd)) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -714,7 +731,6 @@ static int oss_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    oss->fd = fd;
 | 
			
		||||
    oss->conf = conf;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -724,8 +740,10 @@ static void oss_fini_in (HWVoiceIn *hw)
 | 
			
		||||
 | 
			
		||||
    oss_anal_close (&oss->fd);
 | 
			
		||||
 | 
			
		||||
    g_free(oss->pcm_buf);
 | 
			
		||||
    oss->pcm_buf = NULL;
 | 
			
		||||
    if (oss->pcm_buf) {
 | 
			
		||||
        g_free (oss->pcm_buf);
 | 
			
		||||
        oss->pcm_buf = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int oss_run_in (HWVoiceIn *hw)
 | 
			
		||||
@@ -816,8 +834,7 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
            poll_mode = va_arg (ap, int);
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            if (poll_mode) {
 | 
			
		||||
                oss_poll_in (hw);
 | 
			
		||||
            if (poll_mode && oss_poll_in (hw)) {
 | 
			
		||||
                poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            hw->poll_mode = poll_mode;
 | 
			
		||||
@@ -834,79 +851,67 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static OSSConf glob_conf = {
 | 
			
		||||
    .try_mmap = 0,
 | 
			
		||||
    .nfrags = 4,
 | 
			
		||||
    .fragsize = 4096,
 | 
			
		||||
    .devpath_out = "/dev/dsp",
 | 
			
		||||
    .devpath_in = "/dev/dsp",
 | 
			
		||||
    .exclusive = 0,
 | 
			
		||||
    .policy = 5
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *oss_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    OSSConf *conf = g_malloc(sizeof(OSSConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
 | 
			
		||||
    if (access(conf->devpath_in, R_OK | W_OK) < 0 ||
 | 
			
		||||
        access(conf->devpath_out, R_OK | W_OK) < 0) {
 | 
			
		||||
        g_free(conf);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void oss_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option oss_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FRAGSIZE",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.fragsize,
 | 
			
		||||
        .valp  = &conf.fragsize,
 | 
			
		||||
        .descr = "Fragment size in bytes"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "NFRAGS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.nfrags,
 | 
			
		||||
        .valp  = &conf.nfrags,
 | 
			
		||||
        .descr = "Number of fragments"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "MMAP",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &glob_conf.try_mmap,
 | 
			
		||||
        .valp  = &conf.try_mmap,
 | 
			
		||||
        .descr = "Try using memory mapped access"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DAC_DEV",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.devpath_out,
 | 
			
		||||
        .valp  = &conf.devpath_out,
 | 
			
		||||
        .descr = "Path to DAC device"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "ADC_DEV",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.devpath_in,
 | 
			
		||||
        .valp  = &conf.devpath_in,
 | 
			
		||||
        .descr = "Path to ADC device"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "EXCLUSIVE",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &glob_conf.exclusive,
 | 
			
		||||
        .descr = "Open device in exclusive mode (vmix won't work)"
 | 
			
		||||
        .valp  = &conf.exclusive,
 | 
			
		||||
        .descr = "Open device in exclusive mode (vmix wont work)"
 | 
			
		||||
    },
 | 
			
		||||
#ifdef USE_DSP_POLICY
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "POLICY",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.policy,
 | 
			
		||||
        .valp  = &conf.policy,
 | 
			
		||||
        .descr = "Set the timing policy of the device, -1 to use fragment mode",
 | 
			
		||||
    },
 | 
			
		||||
#endif
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DEBUG",
 | 
			
		||||
        .tag   = AUD_OPT_BOOL,
 | 
			
		||||
        .valp  = &conf.debug,
 | 
			
		||||
        .descr = "Turn on some debugging messages"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										125
									
								
								audio/paaudio.c
									
									
									
									
									
								
							
							
						
						
									
										125
									
								
								audio/paaudio.c
									
									
									
									
									
								
							@@ -1,5 +1,4 @@
 | 
			
		||||
/* public domain */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
@@ -9,19 +8,6 @@
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
#include "audio_pt_int.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int samples;
 | 
			
		||||
    char *server;
 | 
			
		||||
    char *sink;
 | 
			
		||||
    char *source;
 | 
			
		||||
} PAConf;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    PAConf conf;
 | 
			
		||||
    pa_threaded_mainloop *mainloop;
 | 
			
		||||
    pa_context *context;
 | 
			
		||||
} paaudio;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    int done;
 | 
			
		||||
@@ -31,7 +17,6 @@ typedef struct {
 | 
			
		||||
    pa_stream *stream;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
    paaudio *g;
 | 
			
		||||
} PAVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
@@ -45,10 +30,20 @@ typedef struct {
 | 
			
		||||
    struct audio_pt pt;
 | 
			
		||||
    const void *read_data;
 | 
			
		||||
    size_t read_index, read_length;
 | 
			
		||||
    paaudio *g;
 | 
			
		||||
} PAVoiceIn;
 | 
			
		||||
 | 
			
		||||
static void qpa_audio_fini(void *opaque);
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int samples;
 | 
			
		||||
    char *server;
 | 
			
		||||
    char *sink;
 | 
			
		||||
    char *source;
 | 
			
		||||
    pa_threaded_mainloop *mainloop;
 | 
			
		||||
    pa_context *context;
 | 
			
		||||
} paaudio;
 | 
			
		||||
 | 
			
		||||
static paaudio glob_paaudio = {
 | 
			
		||||
    .samples = 4096,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
@@ -111,7 +106,7 @@ static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x)
 | 
			
		||||
 | 
			
		||||
static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = p->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_lock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
@@ -165,7 +160,7 @@ unlock_and_fail:
 | 
			
		||||
 | 
			
		||||
static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = p->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_lock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
@@ -227,7 +222,7 @@ static void *qpa_thread_out (void *arg)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        decr = to_mix = audio_MIN (pa->live, pa->g->conf.samples >> 2);
 | 
			
		||||
        decr = to_mix = audio_MIN (pa->live, glob_paaudio.samples >> 2);
 | 
			
		||||
        rpos = pa->rpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
 | 
			
		||||
@@ -319,7 +314,7 @@ static void *qpa_thread_in (void *arg)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        incr = to_grab = audio_MIN (pa->dead, pa->g->conf.samples >> 2);
 | 
			
		||||
        incr = to_grab = audio_MIN (pa->dead, glob_paaudio.samples >> 2);
 | 
			
		||||
        wpos = pa->wpos;
 | 
			
		||||
 | 
			
		||||
        if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
 | 
			
		||||
@@ -435,7 +430,7 @@ static audfmt_e pa_to_audfmt (pa_sample_format_t fmt, int *endianness)
 | 
			
		||||
 | 
			
		||||
static void context_state_cb (pa_context *c, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = userdata;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    switch (pa_context_get_state(c)) {
 | 
			
		||||
    case PA_CONTEXT_READY:
 | 
			
		||||
@@ -454,7 +449,7 @@ static void context_state_cb (pa_context *c, void *userdata)
 | 
			
		||||
 | 
			
		||||
static void stream_state_cb (pa_stream *s, void * userdata)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = userdata;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    switch (pa_stream_get_state (s)) {
 | 
			
		||||
 | 
			
		||||
@@ -472,21 +467,23 @@ static void stream_state_cb (pa_stream *s, void * userdata)
 | 
			
		||||
 | 
			
		||||
static void stream_request_cb (pa_stream *s, size_t length, void *userdata)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = userdata;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_signal (g->mainloop, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static pa_stream *qpa_simple_new (
 | 
			
		||||
        paaudio *g,
 | 
			
		||||
        const char *server,
 | 
			
		||||
        const char *name,
 | 
			
		||||
        pa_stream_direction_t dir,
 | 
			
		||||
        const char *dev,
 | 
			
		||||
        const char *stream_name,
 | 
			
		||||
        const pa_sample_spec *ss,
 | 
			
		||||
        const pa_channel_map *map,
 | 
			
		||||
        const pa_buffer_attr *attr,
 | 
			
		||||
        int *rerror)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
    int r;
 | 
			
		||||
    pa_stream *stream;
 | 
			
		||||
 | 
			
		||||
@@ -537,36 +534,35 @@ fail:
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int error;
 | 
			
		||||
    pa_sample_spec ss;
 | 
			
		||||
    pa_buffer_attr ba;
 | 
			
		||||
    static pa_sample_spec ss;
 | 
			
		||||
    static pa_buffer_attr ba;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    PAVoiceOut *pa = (PAVoiceOut *) hw;
 | 
			
		||||
    paaudio *g = pa->g = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    ss.format = audfmt_to_pa (as->fmt, as->endianness);
 | 
			
		||||
    ss.channels = as->nchannels;
 | 
			
		||||
    ss.rate = as->freq;
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * qemu audio tick runs at 100 Hz (by default), so processing
 | 
			
		||||
     * data chunks worth 10 ms of sound should be a good fit.
 | 
			
		||||
     * qemu audio tick runs at 250 Hz (by default), so processing
 | 
			
		||||
     * data chunks worth 4 ms of sound should be a good fit.
 | 
			
		||||
     */
 | 
			
		||||
    ba.tlength = pa_usec_to_bytes (10 * 1000, &ss);
 | 
			
		||||
    ba.minreq = pa_usec_to_bytes (5 * 1000, &ss);
 | 
			
		||||
    ba.tlength = pa_usec_to_bytes (4 * 1000, &ss);
 | 
			
		||||
    ba.minreq = pa_usec_to_bytes (2 * 1000, &ss);
 | 
			
		||||
    ba.maxlength = -1;
 | 
			
		||||
    ba.prebuf = -1;
 | 
			
		||||
 | 
			
		||||
    obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
 | 
			
		||||
 | 
			
		||||
    pa->stream = qpa_simple_new (
 | 
			
		||||
        g,
 | 
			
		||||
        glob_paaudio.server,
 | 
			
		||||
        "qemu",
 | 
			
		||||
        PA_STREAM_PLAYBACK,
 | 
			
		||||
        g->conf.sink,
 | 
			
		||||
        glob_paaudio.sink,
 | 
			
		||||
        "pcm.playback",
 | 
			
		||||
        &ss,
 | 
			
		||||
        NULL,                   /* channel map */
 | 
			
		||||
        &ba,                    /* buffering attributes */
 | 
			
		||||
@@ -578,7 +574,7 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = g->conf.samples;
 | 
			
		||||
    hw->samples = glob_paaudio.samples;
 | 
			
		||||
    pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    pa->rpos = hw->rpos;
 | 
			
		||||
    if (!pa->pcm_buf) {
 | 
			
		||||
@@ -605,13 +601,12 @@ static int qpa_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int error;
 | 
			
		||||
    pa_sample_spec ss;
 | 
			
		||||
    static pa_sample_spec ss;
 | 
			
		||||
    struct audsettings obt_as = *as;
 | 
			
		||||
    PAVoiceIn *pa = (PAVoiceIn *) hw;
 | 
			
		||||
    paaudio *g = pa->g = drv_opaque;
 | 
			
		||||
 | 
			
		||||
    ss.format = audfmt_to_pa (as->fmt, as->endianness);
 | 
			
		||||
    ss.channels = as->nchannels;
 | 
			
		||||
@@ -620,10 +615,11 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
 | 
			
		||||
 | 
			
		||||
    pa->stream = qpa_simple_new (
 | 
			
		||||
        g,
 | 
			
		||||
        glob_paaudio.server,
 | 
			
		||||
        "qemu",
 | 
			
		||||
        PA_STREAM_RECORD,
 | 
			
		||||
        g->conf.source,
 | 
			
		||||
        glob_paaudio.source,
 | 
			
		||||
        "pcm.capture",
 | 
			
		||||
        &ss,
 | 
			
		||||
        NULL,                   /* channel map */
 | 
			
		||||
        NULL,                   /* buffering attributes */
 | 
			
		||||
@@ -635,7 +631,7 @@ static int qpa_init_in(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, &obt_as);
 | 
			
		||||
    hw->samples = g->conf.samples;
 | 
			
		||||
    hw->samples = glob_paaudio.samples;
 | 
			
		||||
    pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
 | 
			
		||||
    pa->wpos = hw->wpos;
 | 
			
		||||
    if (!pa->pcm_buf) {
 | 
			
		||||
@@ -707,7 +703,7 @@ static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    PAVoiceOut *pa = (PAVoiceOut *) hw;
 | 
			
		||||
    pa_operation *op;
 | 
			
		||||
    pa_cvolume v;
 | 
			
		||||
    paaudio *g = pa->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
#ifdef PA_CHECK_VERSION    /* macro is present in 0.9.16+ */
 | 
			
		||||
    pa_cvolume_init (&v);  /* function is present in 0.9.13+ */
 | 
			
		||||
@@ -759,7 +755,7 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
    PAVoiceIn *pa = (PAVoiceIn *) hw;
 | 
			
		||||
    pa_operation *op;
 | 
			
		||||
    pa_cvolume v;
 | 
			
		||||
    paaudio *g = pa->g;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
#ifdef PA_CHECK_VERSION
 | 
			
		||||
    pa_cvolume_init (&v);
 | 
			
		||||
@@ -781,22 +777,23 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
            pa_threaded_mainloop_lock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
            op = pa_context_set_source_output_volume (g->context,
 | 
			
		||||
                pa_stream_get_index (pa->stream),
 | 
			
		||||
            /* FIXME: use the upcoming "set_source_output_{volume,mute}" */
 | 
			
		||||
            op = pa_context_set_source_volume_by_index (g->context,
 | 
			
		||||
                pa_stream_get_device_index (pa->stream),
 | 
			
		||||
                &v, NULL, NULL);
 | 
			
		||||
            if (!op) {
 | 
			
		||||
                qpa_logerr (pa_context_errno (g->context),
 | 
			
		||||
                            "set_source_output_volume() failed\n");
 | 
			
		||||
                            "set_source_volume() failed\n");
 | 
			
		||||
            } else {
 | 
			
		||||
                pa_operation_unref(op);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            op = pa_context_set_source_output_mute (g->context,
 | 
			
		||||
            op = pa_context_set_source_mute_by_index (g->context,
 | 
			
		||||
                pa_stream_get_index (pa->stream),
 | 
			
		||||
                sw->vol.mute, NULL, NULL);
 | 
			
		||||
            if (!op) {
 | 
			
		||||
                qpa_logerr (pa_context_errno (g->context),
 | 
			
		||||
                            "set_source_output_mute() failed\n");
 | 
			
		||||
                            "set_source_mute() failed\n");
 | 
			
		||||
            } else {
 | 
			
		||||
                pa_operation_unref (op);
 | 
			
		||||
            }
 | 
			
		||||
@@ -808,31 +805,23 @@ static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* common */
 | 
			
		||||
static PAConf glob_conf = {
 | 
			
		||||
    .samples = 4096,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *qpa_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    paaudio *g = g_malloc(sizeof(paaudio));
 | 
			
		||||
    g->conf = glob_conf;
 | 
			
		||||
    g->mainloop = NULL;
 | 
			
		||||
    g->context = NULL;
 | 
			
		||||
    paaudio *g = &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
    g->mainloop = pa_threaded_mainloop_new ();
 | 
			
		||||
    if (!g->mainloop) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop),
 | 
			
		||||
                                 g->conf.server);
 | 
			
		||||
    g->context = pa_context_new (pa_threaded_mainloop_get_api (g->mainloop), glob_paaudio.server);
 | 
			
		||||
    if (!g->context) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pa_context_set_state_callback (g->context, context_state_cb, g);
 | 
			
		||||
 | 
			
		||||
    if (pa_context_connect (g->context, g->conf.server, 0, NULL) < 0) {
 | 
			
		||||
    if (pa_context_connect (g->context, glob_paaudio.server, 0, NULL) < 0) {
 | 
			
		||||
        qpa_logerr (pa_context_errno (g->context),
 | 
			
		||||
                    "pa_context_connect() failed\n");
 | 
			
		||||
        goto fail;
 | 
			
		||||
@@ -865,13 +854,12 @@ static void *qpa_audio_init (void)
 | 
			
		||||
 | 
			
		||||
    pa_threaded_mainloop_unlock (g->mainloop);
 | 
			
		||||
 | 
			
		||||
    return g;
 | 
			
		||||
    return &glob_paaudio;
 | 
			
		||||
 | 
			
		||||
unlock_and_fail:
 | 
			
		||||
    pa_threaded_mainloop_unlock (g->mainloop);
 | 
			
		||||
fail:
 | 
			
		||||
    AUD_log (AUDIO_CAP, "Failed to initialize PA context");
 | 
			
		||||
    qpa_audio_fini(g);
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -886,38 +874,39 @@ static void qpa_audio_fini (void *opaque)
 | 
			
		||||
    if (g->context) {
 | 
			
		||||
        pa_context_disconnect (g->context);
 | 
			
		||||
        pa_context_unref (g->context);
 | 
			
		||||
        g->context = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (g->mainloop) {
 | 
			
		||||
        pa_threaded_mainloop_free (g->mainloop);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free(g);
 | 
			
		||||
    g->mainloop = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct audio_option qpa_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SAMPLES",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.samples,
 | 
			
		||||
        .valp  = &glob_paaudio.samples,
 | 
			
		||||
        .descr = "buffer size in samples"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SERVER",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.server,
 | 
			
		||||
        .valp  = &glob_paaudio.server,
 | 
			
		||||
        .descr = "server address"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SINK",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.sink,
 | 
			
		||||
        .valp  = &glob_paaudio.sink,
 | 
			
		||||
        .descr = "sink device name"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "SOURCE",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.source,
 | 
			
		||||
        .valp  = &glob_paaudio.source,
 | 
			
		||||
        .descr = "source device name"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
@@ -21,7 +21,6 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include <SDL.h>
 | 
			
		||||
#include <SDL_thread.h>
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
@@ -38,14 +37,10 @@
 | 
			
		||||
#define AUDIO_CAP "sdl"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
#define USE_SEMAPHORE (SDL_MAJOR_VERSION < 2)
 | 
			
		||||
 | 
			
		||||
typedef struct SDLVoiceOut {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    int live;
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    int rpos;
 | 
			
		||||
#endif
 | 
			
		||||
    int decr;
 | 
			
		||||
} SDLVoiceOut;
 | 
			
		||||
 | 
			
		||||
@@ -57,12 +52,9 @@ static struct {
 | 
			
		||||
 | 
			
		||||
static struct SDLAudioState {
 | 
			
		||||
    int exit;
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    SDL_mutex *mutex;
 | 
			
		||||
    SDL_sem *sem;
 | 
			
		||||
#endif
 | 
			
		||||
    int initialized;
 | 
			
		||||
    bool driver_created;
 | 
			
		||||
} glob_sdl;
 | 
			
		||||
typedef struct SDLAudioState SDLAudioState;
 | 
			
		||||
 | 
			
		||||
@@ -79,45 +71,31 @@ static void GCC_FMT_ATTR (1, 2) sdl_logerr (const char *fmt, ...)
 | 
			
		||||
 | 
			
		||||
static int sdl_lock (SDLAudioState *s, const char *forfn)
 | 
			
		||||
{
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    if (SDL_LockMutex (s->mutex)) {
 | 
			
		||||
        sdl_logerr ("SDL_LockMutex for %s failed\n", forfn);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
#else
 | 
			
		||||
    SDL_LockAudio();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int sdl_unlock (SDLAudioState *s, const char *forfn)
 | 
			
		||||
{
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    if (SDL_UnlockMutex (s->mutex)) {
 | 
			
		||||
        sdl_logerr ("SDL_UnlockMutex for %s failed\n", forfn);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
#else
 | 
			
		||||
    SDL_UnlockAudio();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int sdl_post (SDLAudioState *s, const char *forfn)
 | 
			
		||||
{
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    if (SDL_SemPost (s->sem)) {
 | 
			
		||||
        sdl_logerr ("SDL_SemPost for %s failed\n", forfn);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
static int sdl_wait (SDLAudioState *s, const char *forfn)
 | 
			
		||||
{
 | 
			
		||||
    if (SDL_SemWait (s->sem)) {
 | 
			
		||||
@@ -126,7 +104,6 @@ static int sdl_wait (SDLAudioState *s, const char *forfn)
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static int sdl_unlock_and_post (SDLAudioState *s, const char *forfn)
 | 
			
		||||
{
 | 
			
		||||
@@ -267,7 +244,6 @@ static void sdl_callback (void *opaque, Uint8 *buf, int len)
 | 
			
		||||
        int to_mix, decr;
 | 
			
		||||
 | 
			
		||||
        /* dolog ("in callback samples=%d\n", samples); */
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
        sdl_wait (s, "sdl_callback");
 | 
			
		||||
        if (s->exit) {
 | 
			
		||||
            return;
 | 
			
		||||
@@ -286,11 +262,6 @@ static void sdl_callback (void *opaque, Uint8 *buf, int len)
 | 
			
		||||
        if (!sdl->live) {
 | 
			
		||||
            goto again;
 | 
			
		||||
        }
 | 
			
		||||
#else
 | 
			
		||||
        if (s->exit || !sdl->live) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
        /* dolog ("in callback live=%d\n", live); */
 | 
			
		||||
        to_mix = audio_MIN (samples, sdl->live);
 | 
			
		||||
@@ -301,11 +272,7 @@ static void sdl_callback (void *opaque, Uint8 *buf, int len)
 | 
			
		||||
 | 
			
		||||
            /* dolog ("in callback to_mix %d, chunk %d\n", to_mix, chunk); */
 | 
			
		||||
            hw->clip (buf, src, chunk);
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
            sdl->rpos = (sdl->rpos + chunk) % hw->samples;
 | 
			
		||||
#else
 | 
			
		||||
            hw->rpos = (hw->rpos + chunk) % hw->samples;
 | 
			
		||||
#endif
 | 
			
		||||
            to_mix -= chunk;
 | 
			
		||||
            buf += chunk << hw->info.shift;
 | 
			
		||||
        }
 | 
			
		||||
@@ -313,21 +280,12 @@ static void sdl_callback (void *opaque, Uint8 *buf, int len)
 | 
			
		||||
        sdl->live -= decr;
 | 
			
		||||
        sdl->decr += decr;
 | 
			
		||||
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    again:
 | 
			
		||||
        if (sdl_unlock (s, "sdl_callback")) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    /* dolog ("done len=%d\n", len); */
 | 
			
		||||
 | 
			
		||||
#if (SDL_MAJOR_VERSION >= 2)
 | 
			
		||||
    /* SDL2 does not clear the remaining buffer for us, so do it on our own */
 | 
			
		||||
    if (samples) {
 | 
			
		||||
        memset(buf, 0, samples << hw->info.shift);
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int sdl_write_out (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
@@ -355,12 +313,8 @@ static int sdl_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    decr = audio_MIN (sdl->decr, live);
 | 
			
		||||
    sdl->decr -= decr;
 | 
			
		||||
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    sdl->live = live - decr;
 | 
			
		||||
    hw->rpos = sdl->rpos;
 | 
			
		||||
#else
 | 
			
		||||
    sdl->live = live;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    if (sdl->live > 0) {
 | 
			
		||||
        sdl_unlock_and_post (s, "sdl_run_out");
 | 
			
		||||
@@ -378,8 +332,7 @@ static void sdl_fini_out (HWVoiceOut *hw)
 | 
			
		||||
    sdl_close (&glob_sdl);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int sdl_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int sdl_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    SDLVoiceOut *sdl = (SDLVoiceOut *) hw;
 | 
			
		||||
    SDLAudioState *s = &glob_sdl;
 | 
			
		||||
@@ -439,17 +392,12 @@ static int sdl_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
static void *sdl_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    SDLAudioState *s = &glob_sdl;
 | 
			
		||||
    if (s->driver_created) {
 | 
			
		||||
        sdl_logerr("Can't create multiple sdl backends\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (SDL_InitSubSystem (SDL_INIT_AUDIO)) {
 | 
			
		||||
        sdl_logerr ("SDL failed to initialize audio subsystem\n");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    s->mutex = SDL_CreateMutex ();
 | 
			
		||||
    if (!s->mutex) {
 | 
			
		||||
        sdl_logerr ("Failed to create SDL mutex\n");
 | 
			
		||||
@@ -464,9 +412,7 @@ static void *sdl_audio_init (void)
 | 
			
		||||
        SDL_QuitSubSystem (SDL_INIT_AUDIO);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    s->driver_created = true;
 | 
			
		||||
    return s;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -474,12 +420,9 @@ static void sdl_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    SDLAudioState *s = opaque;
 | 
			
		||||
    sdl_close (s);
 | 
			
		||||
#if USE_SEMAPHORE
 | 
			
		||||
    SDL_DestroySemaphore (s->sem);
 | 
			
		||||
    SDL_DestroyMutex (s->mutex);
 | 
			
		||||
#endif
 | 
			
		||||
    SDL_QuitSubSystem (SDL_INIT_AUDIO);
 | 
			
		||||
    s->driver_created = false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option sdl_options[] = {
 | 
			
		||||
 
 | 
			
		||||
@@ -17,10 +17,7 @@
 | 
			
		||||
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "qemu/host-utils.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "ui/qemu-spice.h"
 | 
			
		||||
 | 
			
		||||
@@ -28,17 +25,8 @@
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
 | 
			
		||||
#define LINE_OUT_SAMPLES (480 * 4)
 | 
			
		||||
#else
 | 
			
		||||
#define LINE_OUT_SAMPLES (256 * 4)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
 | 
			
		||||
#define LINE_IN_SAMPLES (480 * 4)
 | 
			
		||||
#else
 | 
			
		||||
#define LINE_IN_SAMPLES (256 * 4)
 | 
			
		||||
#endif
 | 
			
		||||
#define LINE_IN_SAMPLES 1024
 | 
			
		||||
#define LINE_OUT_SAMPLES 1024
 | 
			
		||||
 | 
			
		||||
typedef struct SpiceRateCtl {
 | 
			
		||||
    int64_t               start_ticks;
 | 
			
		||||
@@ -93,7 +81,7 @@ static void spice_audio_fini (void *opaque)
 | 
			
		||||
static void rate_start (SpiceRateCtl *rate)
 | 
			
		||||
{
 | 
			
		||||
    memset (rate, 0, sizeof (*rate));
 | 
			
		||||
    rate->start_ticks = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
    rate->start_ticks = qemu_get_clock_ns (vm_clock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
 | 
			
		||||
@@ -103,13 +91,13 @@ static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
 | 
			
		||||
    int64_t bytes;
 | 
			
		||||
    int64_t samples;
 | 
			
		||||
 | 
			
		||||
    now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
    now = qemu_get_clock_ns (vm_clock);
 | 
			
		||||
    ticks = now - rate->start_ticks;
 | 
			
		||||
    bytes = muldiv64(ticks, info->bytes_per_second, NANOSECONDS_PER_SECOND);
 | 
			
		||||
    bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
 | 
			
		||||
    samples = (bytes - rate->bytes_sent) >> info->shift;
 | 
			
		||||
    if (samples < 0 || samples > 65536) {
 | 
			
		||||
        error_report("Resetting rate control (%" PRId64 " samples)", samples);
 | 
			
		||||
        rate_start(rate);
 | 
			
		||||
        fprintf (stderr, "Resetting rate control (%" PRId64 " samples)\n", samples);
 | 
			
		||||
        rate_start (rate);
 | 
			
		||||
        samples = 0;
 | 
			
		||||
    }
 | 
			
		||||
    rate->bytes_sent += samples << info->shift;
 | 
			
		||||
@@ -118,17 +106,12 @@ static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
 | 
			
		||||
 | 
			
		||||
/* playback */
 | 
			
		||||
 | 
			
		||||
static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                         void *drv_opaque)
 | 
			
		||||
static int line_out_init (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
 | 
			
		||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
 | 
			
		||||
    settings.freq       = spice_server_get_best_playback_rate(NULL);
 | 
			
		||||
#else
 | 
			
		||||
    settings.freq       = SPICE_INTERFACE_PLAYBACK_FREQ;
 | 
			
		||||
#endif
 | 
			
		||||
    settings.nchannels  = SPICE_INTERFACE_PLAYBACK_CHAN;
 | 
			
		||||
    settings.fmt        = AUD_FMT_S16;
 | 
			
		||||
    settings.endianness = AUDIO_HOST_ENDIANNESS;
 | 
			
		||||
@@ -139,9 +122,6 @@ static int line_out_init(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
 | 
			
		||||
    out->sin.base.sif = &playback_sif.base;
 | 
			
		||||
    qemu_spice_add_interface (&out->sin.base);
 | 
			
		||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
 | 
			
		||||
    spice_server_set_playback_rate(&out->sin, settings.freq);
 | 
			
		||||
#endif
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -247,16 +227,12 @@ static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
 | 
			
		||||
/* record */
 | 
			
		||||
 | 
			
		||||
static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
static int line_in_init (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
 | 
			
		||||
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
 | 
			
		||||
    settings.freq       = spice_server_get_best_record_rate(NULL);
 | 
			
		||||
#else
 | 
			
		||||
    settings.freq       = SPICE_INTERFACE_RECORD_FREQ;
 | 
			
		||||
#endif
 | 
			
		||||
    settings.nchannels  = SPICE_INTERFACE_RECORD_CHAN;
 | 
			
		||||
    settings.fmt        = AUD_FMT_S16;
 | 
			
		||||
    settings.endianness = AUDIO_HOST_ENDIANNESS;
 | 
			
		||||
@@ -267,9 +243,6 @@ static int line_in_init(HWVoiceIn *hw, struct audsettings *as, void *drv_opaque)
 | 
			
		||||
 | 
			
		||||
    in->sin.base.sif = &record_sif.base;
 | 
			
		||||
    qemu_spice_add_interface (&in->sin.base);
 | 
			
		||||
#if SPICE_INTERFACE_RECORD_MAJOR > 2 || SPICE_INTERFACE_RECORD_MINOR >= 3
 | 
			
		||||
    spice_server_set_record_rate(&in->sin, settings.freq);
 | 
			
		||||
#endif
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,17 +0,0 @@
 | 
			
		||||
# See docs/tracing.txt for syntax documentation.
 | 
			
		||||
 | 
			
		||||
# audio/alsaaudio.c
 | 
			
		||||
alsa_revents(int revents) "revents = %d"
 | 
			
		||||
alsa_pollout(int i, int fd) "i = %d fd = %d"
 | 
			
		||||
alsa_set_handler(int events, int index, int fd, int err) "events=%#x index=%d fd=%d err=%d"
 | 
			
		||||
alsa_wrote_zero(int len) "Failed to write %d frames (wrote zero)"
 | 
			
		||||
alsa_read_zero(long len) "Failed to read %ld frames (read zero)"
 | 
			
		||||
alsa_xrun_out(void) "Recovering from playback xrun"
 | 
			
		||||
alsa_xrun_in(void) "Recovering from capture xrun"
 | 
			
		||||
alsa_resume_out(void) "Resuming suspended output stream"
 | 
			
		||||
alsa_resume_in(void) "Resuming suspended input stream"
 | 
			
		||||
alsa_no_frames(int state) "No frames available and ALSA state is %d"
 | 
			
		||||
 | 
			
		||||
# audio/ossaudio.c
 | 
			
		||||
oss_version(int version) "OSS version = %#x"
 | 
			
		||||
oss_invalid_available_size(int size, int bufsize) "Invalid available size, size=%d bufsize=%d"
 | 
			
		||||
@@ -21,8 +21,7 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu/host-utils.h"
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
@@ -37,10 +36,15 @@ typedef struct WAVVoiceOut {
 | 
			
		||||
    int total_samples;
 | 
			
		||||
} WAVVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
static struct {
 | 
			
		||||
    struct audsettings settings;
 | 
			
		||||
    const char *wav_path;
 | 
			
		||||
} WAVConf;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .settings.freq      = 44100,
 | 
			
		||||
    .settings.nchannels = 2,
 | 
			
		||||
    .settings.fmt       = AUD_FMT_S16,
 | 
			
		||||
    .wav_path           = "qemu.wav"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int wav_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
@@ -48,10 +52,10 @@ static int wav_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
    int rpos, decr, samples;
 | 
			
		||||
    uint8_t *dst;
 | 
			
		||||
    struct st_sample *src;
 | 
			
		||||
    int64_t now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
 | 
			
		||||
    int64_t now = qemu_get_clock_ns (vm_clock);
 | 
			
		||||
    int64_t ticks = now - wav->old_ticks;
 | 
			
		||||
    int64_t bytes =
 | 
			
		||||
        muldiv64(ticks, hw->info.bytes_per_second, NANOSECONDS_PER_SECOND);
 | 
			
		||||
        muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
 | 
			
		||||
 | 
			
		||||
    if (bytes > INT_MAX) {
 | 
			
		||||
        samples = INT_MAX >> hw->info.shift;
 | 
			
		||||
@@ -101,8 +105,7 @@ static void le_store (uint8_t *buf, uint32_t val, int len)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
                        void *drv_opaque)
 | 
			
		||||
static int wav_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    WAVVoiceOut *wav = (WAVVoiceOut *) hw;
 | 
			
		||||
    int bits16 = 0, stereo = 0;
 | 
			
		||||
@@ -112,8 +115,9 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
        0x02, 0x00, 0x44, 0xac, 0x00, 0x00, 0x10, 0xb1, 0x02, 0x00, 0x04,
 | 
			
		||||
        0x00, 0x10, 0x00, 0x64, 0x61, 0x74, 0x61, 0x00, 0x00, 0x00, 0x00
 | 
			
		||||
    };
 | 
			
		||||
    WAVConf *conf = drv_opaque;
 | 
			
		||||
    struct audsettings wav_as = conf->settings;
 | 
			
		||||
    struct audsettings wav_as = conf.settings;
 | 
			
		||||
 | 
			
		||||
    (void) as;
 | 
			
		||||
 | 
			
		||||
    stereo = wav_as.nchannels == 2;
 | 
			
		||||
    switch (wav_as.fmt) {
 | 
			
		||||
@@ -151,10 +155,10 @@ static int wav_init_out(HWVoiceOut *hw, struct audsettings *as,
 | 
			
		||||
    le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4);
 | 
			
		||||
    le_store (hdr + 32, 1 << (bits16 + stereo), 2);
 | 
			
		||||
 | 
			
		||||
    wav->f = fopen (conf->wav_path, "wb");
 | 
			
		||||
    wav->f = fopen (conf.wav_path, "wb");
 | 
			
		||||
    if (!wav->f) {
 | 
			
		||||
        dolog ("Failed to open wave file `%s'\nReason: %s\n",
 | 
			
		||||
               conf->wav_path, strerror (errno));
 | 
			
		||||
               conf.wav_path, strerror (errno));
 | 
			
		||||
        g_free (wav->pcm_buf);
 | 
			
		||||
        wav->pcm_buf = NULL;
 | 
			
		||||
        return -1;
 | 
			
		||||
@@ -222,49 +226,40 @@ static int wav_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static WAVConf glob_conf = {
 | 
			
		||||
    .settings.freq      = 44100,
 | 
			
		||||
    .settings.nchannels = 2,
 | 
			
		||||
    .settings.fmt       = AUD_FMT_S16,
 | 
			
		||||
    .wav_path           = "qemu.wav"
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void *wav_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    WAVConf *conf = g_malloc(sizeof(WAVConf));
 | 
			
		||||
    *conf = glob_conf;
 | 
			
		||||
    return conf;
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void wav_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    ldebug ("wav_fini");
 | 
			
		||||
    g_free(opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option wav_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FREQUENCY",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.settings.freq,
 | 
			
		||||
        .valp  = &conf.settings.freq,
 | 
			
		||||
        .descr = "Frequency"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "FORMAT",
 | 
			
		||||
        .tag   = AUD_OPT_FMT,
 | 
			
		||||
        .valp  = &glob_conf.settings.fmt,
 | 
			
		||||
        .valp  = &conf.settings.fmt,
 | 
			
		||||
        .descr = "Format"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "DAC_FIXED_CHANNELS",
 | 
			
		||||
        .tag   = AUD_OPT_INT,
 | 
			
		||||
        .valp  = &glob_conf.settings.nchannels,
 | 
			
		||||
        .valp  = &conf.settings.nchannels,
 | 
			
		||||
        .descr = "Number of channels (1 - mono, 2 - stereo)"
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name  = "PATH",
 | 
			
		||||
        .tag   = AUD_OPT_STR,
 | 
			
		||||
        .valp  = &glob_conf.wav_path,
 | 
			
		||||
        .valp  = &conf.wav_path,
 | 
			
		||||
        .descr = "Path to wave file"
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,5 @@
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
@@ -65,7 +63,8 @@ static void wav_destroy (void *opaque)
 | 
			
		||||
        }
 | 
			
		||||
    doclose:
 | 
			
		||||
        if (fclose (wav->f)) {
 | 
			
		||||
            error_report("wav_destroy: fclose failed: %s", strerror(errno));
 | 
			
		||||
            fprintf (stderr, "wav_destroy: fclose failed: %s",
 | 
			
		||||
                     strerror (errno));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -88,7 +87,6 @@ static void wav_capture_destroy (void *opaque)
 | 
			
		||||
    WAVState *wav = opaque;
 | 
			
		||||
 | 
			
		||||
    AUD_del_capture (wav->cap, wav);
 | 
			
		||||
    g_free (wav);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void wav_capture_info (void *opaque)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										717
									
								
								audio/winwaveaudio.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										717
									
								
								audio/winwaveaudio.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,717 @@
 | 
			
		||||
/* public domain */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "audio.h"
 | 
			
		||||
 | 
			
		||||
#define AUDIO_CAP "winwave"
 | 
			
		||||
#include "audio_int.h"
 | 
			
		||||
 | 
			
		||||
#include <windows.h>
 | 
			
		||||
#include <mmsystem.h>
 | 
			
		||||
 | 
			
		||||
#include "audio_win_int.h"
 | 
			
		||||
 | 
			
		||||
static struct {
 | 
			
		||||
    int dac_headers;
 | 
			
		||||
    int dac_samples;
 | 
			
		||||
    int adc_headers;
 | 
			
		||||
    int adc_samples;
 | 
			
		||||
} conf = {
 | 
			
		||||
    .dac_headers = 4,
 | 
			
		||||
    .dac_samples = 1024,
 | 
			
		||||
    .adc_headers = 4,
 | 
			
		||||
    .adc_samples = 1024
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceOut hw;
 | 
			
		||||
    HWAVEOUT hwo;
 | 
			
		||||
    WAVEHDR *hdrs;
 | 
			
		||||
    HANDLE event;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int avail;
 | 
			
		||||
    int pending;
 | 
			
		||||
    int curhdr;
 | 
			
		||||
    int paused;
 | 
			
		||||
    CRITICAL_SECTION crit_sect;
 | 
			
		||||
} WaveVoiceOut;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    HWVoiceIn hw;
 | 
			
		||||
    HWAVEIN hwi;
 | 
			
		||||
    WAVEHDR *hdrs;
 | 
			
		||||
    HANDLE event;
 | 
			
		||||
    void *pcm_buf;
 | 
			
		||||
    int curhdr;
 | 
			
		||||
    int paused;
 | 
			
		||||
    int rpos;
 | 
			
		||||
    int avail;
 | 
			
		||||
    CRITICAL_SECTION crit_sect;
 | 
			
		||||
} WaveVoiceIn;
 | 
			
		||||
 | 
			
		||||
static void winwave_log_mmresult (MMRESULT mr)
 | 
			
		||||
{
 | 
			
		||||
    const char *str = "BUG";
 | 
			
		||||
 | 
			
		||||
    switch (mr) {
 | 
			
		||||
    case MMSYSERR_NOERROR:
 | 
			
		||||
        str = "Success";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_INVALHANDLE:
 | 
			
		||||
        str = "Specified device handle is invalid";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_BADDEVICEID:
 | 
			
		||||
        str = "Specified device id is out of range";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_NODRIVER:
 | 
			
		||||
        str = "No device driver is present";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case MMSYSERR_NOMEM:
 | 
			
		||||
        str = "Unable to allocate or lock memory";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WAVERR_SYNC:
 | 
			
		||||
        str = "Device is synchronous but waveOutOpen was called "
 | 
			
		||||
            "without using the WINWAVE_ALLOWSYNC flag";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WAVERR_UNPREPARED:
 | 
			
		||||
        str = "The data block pointed to by the pwh parameter "
 | 
			
		||||
            "hasn't been prepared";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WAVERR_STILLPLAYING:
 | 
			
		||||
        str = "There are still buffers in the queue";
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("Reason: Unknown (MMRESULT %#x)\n", mr);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    dolog ("Reason: %s\n", str);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR (2, 3) winwave_logerr (
 | 
			
		||||
    MMRESULT mr,
 | 
			
		||||
    const char *fmt,
 | 
			
		||||
    ...
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start (ap, fmt);
 | 
			
		||||
    AUD_vlog (AUDIO_CAP, fmt, ap);
 | 
			
		||||
    va_end (ap);
 | 
			
		||||
 | 
			
		||||
    AUD_log (NULL, " failed\n");
 | 
			
		||||
    winwave_log_mmresult (mr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_anal_close_out (WaveVoiceOut *wave)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
 | 
			
		||||
    mr = waveOutClose (wave->hwo);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveOutClose");
 | 
			
		||||
    }
 | 
			
		||||
    wave->hwo = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void CALLBACK winwave_callback_out (
 | 
			
		||||
    HWAVEOUT hwo,
 | 
			
		||||
    UINT msg,
 | 
			
		||||
    DWORD_PTR dwInstance,
 | 
			
		||||
    DWORD_PTR dwParam1,
 | 
			
		||||
    DWORD_PTR dwParam2
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) dwInstance;
 | 
			
		||||
 | 
			
		||||
    switch (msg) {
 | 
			
		||||
    case WOM_DONE:
 | 
			
		||||
        {
 | 
			
		||||
            WAVEHDR *h = (WAVEHDR *) dwParam1;
 | 
			
		||||
            if (!h->dwUser) {
 | 
			
		||||
                h->dwUser = 1;
 | 
			
		||||
                EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
                {
 | 
			
		||||
                    wave->avail += conf.dac_samples;
 | 
			
		||||
                }
 | 
			
		||||
                LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
                if (wave->hw.poll_mode) {
 | 
			
		||||
                    if (!SetEvent (wave->event)) {
 | 
			
		||||
                        dolog ("DAC SetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WOM_CLOSE:
 | 
			
		||||
    case WOM_OPEN:
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("unknown wave out callback msg %x\n", msg);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_init_out (HWVoiceOut *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    int err;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    WaveVoiceOut *wave;
 | 
			
		||||
 | 
			
		||||
    wave = (WaveVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    InitializeCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    err = waveformat_from_audio_settings (&wfx, as);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        goto err0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    mr = waveOutOpen (&wave->hwo, WAVE_MAPPER, &wfx,
 | 
			
		||||
                      (DWORD_PTR) winwave_callback_out,
 | 
			
		||||
                      (DWORD_PTR) wave, CALLBACK_FUNCTION);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveOutOpen");
 | 
			
		||||
        goto err1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wave->hdrs = audio_calloc (AUDIO_FUNC, conf.dac_headers,
 | 
			
		||||
                               sizeof (*wave->hdrs));
 | 
			
		||||
    if (!wave->hdrs) {
 | 
			
		||||
        goto err2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = conf.dac_samples * conf.dac_headers;
 | 
			
		||||
    wave->avail = hw->samples;
 | 
			
		||||
 | 
			
		||||
    wave->pcm_buf = audio_calloc (AUDIO_FUNC, conf.dac_samples,
 | 
			
		||||
                                  conf.dac_headers << hw->info.shift);
 | 
			
		||||
    if (!wave->pcm_buf) {
 | 
			
		||||
        goto err3;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.dac_headers; ++i) {
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[i];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        h->dwBufferLength = conf.dac_samples << hw->info.shift;
 | 
			
		||||
        h->lpData = advance (wave->pcm_buf, i * h->dwBufferLength);
 | 
			
		||||
        h->dwFlags = 0;
 | 
			
		||||
 | 
			
		||||
        mr = waveOutPrepareHeader (wave->hwo, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveOutPrepareHeader(%d)", i);
 | 
			
		||||
            goto err4;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 err4:
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
 err3:
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
 err2:
 | 
			
		||||
    winwave_anal_close_out (wave);
 | 
			
		||||
 err1:
 | 
			
		||||
 err0:
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_write (SWVoiceOut *sw, void *buf, int len)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_write (sw, buf, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_run_out (HWVoiceOut *hw, int live)
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
 | 
			
		||||
    int decr;
 | 
			
		||||
    int doreset;
 | 
			
		||||
 | 
			
		||||
    EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
    {
 | 
			
		||||
        decr = audio_MIN (live, wave->avail);
 | 
			
		||||
        decr = audio_pcm_hw_clip_out (hw, wave->pcm_buf, decr, wave->pending);
 | 
			
		||||
        wave->pending += decr;
 | 
			
		||||
        wave->avail -= decr;
 | 
			
		||||
    }
 | 
			
		||||
    LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    doreset = hw->poll_mode && (wave->pending >= conf.dac_samples);
 | 
			
		||||
    if (doreset && !ResetEvent (wave->event)) {
 | 
			
		||||
        dolog ("DAC ResetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (wave->pending >= conf.dac_samples) {
 | 
			
		||||
        MMRESULT mr;
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[wave->curhdr];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        mr = waveOutWrite (wave->hwo, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveOutWrite(%d)", wave->curhdr);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        wave->pending -= conf.dac_samples;
 | 
			
		||||
        wave->curhdr = (wave->curhdr + 1) % conf.dac_headers;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return decr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_poll (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
    audio_run ("winwave_poll");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_fini_out (HWVoiceOut *hw)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    mr = waveOutReset (wave->hwo);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveOutReset");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.dac_headers; ++i) {
 | 
			
		||||
        mr = waveOutUnprepareHeader (wave->hwo, &wave->hdrs[i],
 | 
			
		||||
                                     sizeof (wave->hdrs[i]));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveOutUnprepareHeader(%d)", i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    winwave_anal_close_out (wave);
 | 
			
		||||
 | 
			
		||||
    if (wave->event) {
 | 
			
		||||
        qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        if (!CloseHandle (wave->event)) {
 | 
			
		||||
            dolog ("DAC CloseHandle failed %lx\n", GetLastError ());
 | 
			
		||||
        }
 | 
			
		||||
        wave->event = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
    wave->pcm_buf = NULL;
 | 
			
		||||
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
    wave->hdrs = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_ctl_out (HWVoiceOut *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceOut *wave = (WaveVoiceOut *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        {
 | 
			
		||||
            va_list ap;
 | 
			
		||||
            int poll_mode;
 | 
			
		||||
 | 
			
		||||
            va_start (ap, cmd);
 | 
			
		||||
            poll_mode = va_arg (ap, int);
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            if (poll_mode && !wave->event) {
 | 
			
		||||
                wave->event = CreateEvent (NULL, TRUE, TRUE, NULL);
 | 
			
		||||
                if (!wave->event) {
 | 
			
		||||
                    dolog ("DAC CreateEvent: %lx, poll mode will be disabled\n",
 | 
			
		||||
                           GetLastError ());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (wave->event) {
 | 
			
		||||
                int ret;
 | 
			
		||||
 | 
			
		||||
                ret = qemu_add_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
                hw->poll_mode = (ret == 0);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                hw->poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            wave->paused = 0;
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        if (!wave->paused) {
 | 
			
		||||
            mr = waveOutReset (wave->hwo);
 | 
			
		||||
            if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
                winwave_logerr (mr, "waveOutReset");
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                wave->paused = 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (wave->event) {
 | 
			
		||||
            qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_anal_close_in (WaveVoiceIn *wave)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
 | 
			
		||||
    mr = waveInClose (wave->hwi);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveInClose");
 | 
			
		||||
    }
 | 
			
		||||
    wave->hwi = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void CALLBACK winwave_callback_in (
 | 
			
		||||
    HWAVEIN *hwi,
 | 
			
		||||
    UINT msg,
 | 
			
		||||
    DWORD_PTR dwInstance,
 | 
			
		||||
    DWORD_PTR dwParam1,
 | 
			
		||||
    DWORD_PTR dwParam2
 | 
			
		||||
    )
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) dwInstance;
 | 
			
		||||
 | 
			
		||||
    switch (msg) {
 | 
			
		||||
    case WIM_DATA:
 | 
			
		||||
        {
 | 
			
		||||
            WAVEHDR *h = (WAVEHDR *) dwParam1;
 | 
			
		||||
            if (!h->dwUser) {
 | 
			
		||||
                h->dwUser = 1;
 | 
			
		||||
                EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
                {
 | 
			
		||||
                    wave->avail += conf.adc_samples;
 | 
			
		||||
                }
 | 
			
		||||
                LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
                if (wave->hw.poll_mode) {
 | 
			
		||||
                    if (!SetEvent (wave->event)) {
 | 
			
		||||
                        dolog ("ADC SetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case WIM_CLOSE:
 | 
			
		||||
    case WIM_OPEN:
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    default:
 | 
			
		||||
        dolog ("unknown wave in callback msg %x\n", msg);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_add_buffers (WaveVoiceIn *wave, int samples)
 | 
			
		||||
{
 | 
			
		||||
    int doreset;
 | 
			
		||||
 | 
			
		||||
    doreset = wave->hw.poll_mode && (samples >= conf.adc_samples);
 | 
			
		||||
    if (doreset && !ResetEvent (wave->event)) {
 | 
			
		||||
        dolog ("ADC ResetEvent failed %lx\n", GetLastError ());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while (samples >= conf.adc_samples) {
 | 
			
		||||
        MMRESULT mr;
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[wave->curhdr];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        mr = waveInAddBuffer (wave->hwi, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveInAddBuffer(%d)", wave->curhdr);
 | 
			
		||||
        }
 | 
			
		||||
        wave->curhdr = (wave->curhdr + 1) % conf.adc_headers;
 | 
			
		||||
        samples -= conf.adc_samples;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_init_in (HWVoiceIn *hw, struct audsettings *as)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    int err;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WAVEFORMATEX wfx;
 | 
			
		||||
    WaveVoiceIn *wave;
 | 
			
		||||
 | 
			
		||||
    wave = (WaveVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    InitializeCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    err = waveformat_from_audio_settings (&wfx, as);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        goto err0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    mr = waveInOpen (&wave->hwi, WAVE_MAPPER, &wfx,
 | 
			
		||||
                     (DWORD_PTR) winwave_callback_in,
 | 
			
		||||
                     (DWORD_PTR) wave, CALLBACK_FUNCTION);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveInOpen");
 | 
			
		||||
        goto err1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wave->hdrs = audio_calloc (AUDIO_FUNC, conf.dac_headers,
 | 
			
		||||
                               sizeof (*wave->hdrs));
 | 
			
		||||
    if (!wave->hdrs) {
 | 
			
		||||
        goto err2;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    audio_pcm_init_info (&hw->info, as);
 | 
			
		||||
    hw->samples = conf.adc_samples * conf.adc_headers;
 | 
			
		||||
    wave->avail = 0;
 | 
			
		||||
 | 
			
		||||
    wave->pcm_buf = audio_calloc (AUDIO_FUNC, conf.adc_samples,
 | 
			
		||||
                                  conf.adc_headers << hw->info.shift);
 | 
			
		||||
    if (!wave->pcm_buf) {
 | 
			
		||||
        goto err3;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.adc_headers; ++i) {
 | 
			
		||||
        WAVEHDR *h = &wave->hdrs[i];
 | 
			
		||||
 | 
			
		||||
        h->dwUser = 0;
 | 
			
		||||
        h->dwBufferLength = conf.adc_samples << hw->info.shift;
 | 
			
		||||
        h->lpData = advance (wave->pcm_buf, i * h->dwBufferLength);
 | 
			
		||||
        h->dwFlags = 0;
 | 
			
		||||
 | 
			
		||||
        mr = waveInPrepareHeader (wave->hwi, h, sizeof (*h));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveInPrepareHeader(%d)", i);
 | 
			
		||||
            goto err4;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    wave->paused = 1;
 | 
			
		||||
    winwave_add_buffers (wave, hw->samples);
 | 
			
		||||
    return 0;
 | 
			
		||||
 | 
			
		||||
 err4:
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
 err3:
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
 err2:
 | 
			
		||||
    winwave_anal_close_in (wave);
 | 
			
		||||
 err1:
 | 
			
		||||
 err0:
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_fini_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    mr = waveInReset (wave->hwi);
 | 
			
		||||
    if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
        winwave_logerr (mr, "waveInReset");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < conf.adc_headers; ++i) {
 | 
			
		||||
        mr = waveInUnprepareHeader (wave->hwi, &wave->hdrs[i],
 | 
			
		||||
                                     sizeof (wave->hdrs[i]));
 | 
			
		||||
        if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
            winwave_logerr (mr, "waveInUnprepareHeader(%d)", i);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    winwave_anal_close_in (wave);
 | 
			
		||||
 | 
			
		||||
    if (wave->event) {
 | 
			
		||||
        qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        if (!CloseHandle (wave->event)) {
 | 
			
		||||
            dolog ("ADC CloseHandle failed %lx\n", GetLastError ());
 | 
			
		||||
        }
 | 
			
		||||
        wave->event = NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free (wave->pcm_buf);
 | 
			
		||||
    wave->pcm_buf = NULL;
 | 
			
		||||
 | 
			
		||||
    g_free (wave->hdrs);
 | 
			
		||||
    wave->hdrs = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_run_in (HWVoiceIn *hw)
 | 
			
		||||
{
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
 | 
			
		||||
    int live = audio_pcm_hw_get_live_in (hw);
 | 
			
		||||
    int dead = hw->samples - live;
 | 
			
		||||
    int decr, ret;
 | 
			
		||||
 | 
			
		||||
    if (!dead) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    EnterCriticalSection (&wave->crit_sect);
 | 
			
		||||
    {
 | 
			
		||||
        decr = audio_MIN (dead, wave->avail);
 | 
			
		||||
        wave->avail -= decr;
 | 
			
		||||
    }
 | 
			
		||||
    LeaveCriticalSection (&wave->crit_sect);
 | 
			
		||||
 | 
			
		||||
    ret = decr;
 | 
			
		||||
    while (decr) {
 | 
			
		||||
        int left = hw->samples - hw->wpos;
 | 
			
		||||
        int conv = audio_MIN (left, decr);
 | 
			
		||||
        hw->conv (hw->conv_buf + hw->wpos,
 | 
			
		||||
                  advance (wave->pcm_buf, wave->rpos << hw->info.shift),
 | 
			
		||||
                  conv);
 | 
			
		||||
 | 
			
		||||
        wave->rpos = (wave->rpos + conv) % hw->samples;
 | 
			
		||||
        hw->wpos = (hw->wpos + conv) % hw->samples;
 | 
			
		||||
        decr -= conv;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    winwave_add_buffers (wave, ret);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_read (SWVoiceIn *sw, void *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    return audio_pcm_sw_read (sw, buf, size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int winwave_ctl_in (HWVoiceIn *hw, int cmd, ...)
 | 
			
		||||
{
 | 
			
		||||
    MMRESULT mr;
 | 
			
		||||
    WaveVoiceIn *wave = (WaveVoiceIn *) hw;
 | 
			
		||||
 | 
			
		||||
    switch (cmd) {
 | 
			
		||||
    case VOICE_ENABLE:
 | 
			
		||||
        {
 | 
			
		||||
            va_list ap;
 | 
			
		||||
            int poll_mode;
 | 
			
		||||
 | 
			
		||||
            va_start (ap, cmd);
 | 
			
		||||
            poll_mode = va_arg (ap, int);
 | 
			
		||||
            va_end (ap);
 | 
			
		||||
 | 
			
		||||
            if (poll_mode && !wave->event) {
 | 
			
		||||
                wave->event = CreateEvent (NULL, TRUE, TRUE, NULL);
 | 
			
		||||
                if (!wave->event) {
 | 
			
		||||
                    dolog ("ADC CreateEvent: %lx, poll mode will be disabled\n",
 | 
			
		||||
                           GetLastError ());
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (wave->event) {
 | 
			
		||||
                int ret;
 | 
			
		||||
 | 
			
		||||
                ret = qemu_add_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
                hw->poll_mode = (ret == 0);
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                hw->poll_mode = 0;
 | 
			
		||||
            }
 | 
			
		||||
            if (wave->paused) {
 | 
			
		||||
                mr = waveInStart (wave->hwi);
 | 
			
		||||
                if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
                    winwave_logerr (mr, "waveInStart");
 | 
			
		||||
                }
 | 
			
		||||
                wave->paused = 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
 | 
			
		||||
    case VOICE_DISABLE:
 | 
			
		||||
        if (!wave->paused) {
 | 
			
		||||
            mr = waveInStop (wave->hwi);
 | 
			
		||||
            if (mr != MMSYSERR_NOERROR) {
 | 
			
		||||
                winwave_logerr (mr, "waveInStop");
 | 
			
		||||
            }
 | 
			
		||||
            else {
 | 
			
		||||
                wave->paused = 1;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        if (wave->event) {
 | 
			
		||||
            qemu_del_wait_object (wave->event, winwave_poll, wave);
 | 
			
		||||
        }
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void *winwave_audio_init (void)
 | 
			
		||||
{
 | 
			
		||||
    return &conf;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void winwave_audio_fini (void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    (void) opaque;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static struct audio_option winwave_options[] = {
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_HEADERS",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.dac_headers,
 | 
			
		||||
        .descr       = "DAC number of headers",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "DAC_SAMPLES",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.dac_samples,
 | 
			
		||||
        .descr       = "DAC number of samples per header",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_HEADERS",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.adc_headers,
 | 
			
		||||
        .descr       = "ADC number of headers",
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
        .name        = "ADC_SAMPLES",
 | 
			
		||||
        .tag         = AUD_OPT_INT,
 | 
			
		||||
        .valp        = &conf.adc_samples,
 | 
			
		||||
        .descr       = "ADC number of samples per header",
 | 
			
		||||
    },
 | 
			
		||||
    { /* End of list */ }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static struct audio_pcm_ops winwave_pcm_ops = {
 | 
			
		||||
    .init_out = winwave_init_out,
 | 
			
		||||
    .fini_out = winwave_fini_out,
 | 
			
		||||
    .run_out  = winwave_run_out,
 | 
			
		||||
    .write    = winwave_write,
 | 
			
		||||
    .ctl_out  = winwave_ctl_out,
 | 
			
		||||
    .init_in  = winwave_init_in,
 | 
			
		||||
    .fini_in  = winwave_fini_in,
 | 
			
		||||
    .run_in   = winwave_run_in,
 | 
			
		||||
    .read     = winwave_read,
 | 
			
		||||
    .ctl_in   = winwave_ctl_in
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct audio_driver winwave_audio_driver = {
 | 
			
		||||
    .name           = "winwave",
 | 
			
		||||
    .descr          = "Windows Waveform Audio http://msdn.microsoft.com",
 | 
			
		||||
    .options        = winwave_options,
 | 
			
		||||
    .init           = winwave_audio_init,
 | 
			
		||||
    .fini           = winwave_audio_fini,
 | 
			
		||||
    .pcm_ops        = &winwave_pcm_ops,
 | 
			
		||||
    .can_be_default = 1,
 | 
			
		||||
    .max_voices_out = INT_MAX,
 | 
			
		||||
    .max_voices_in  = INT_MAX,
 | 
			
		||||
    .voice_size_out = sizeof (WaveVoiceOut),
 | 
			
		||||
    .voice_size_in  = sizeof (WaveVoiceIn)
 | 
			
		||||
};
 | 
			
		||||
@@ -1,10 +1,2 @@
 | 
			
		||||
common-obj-y += rng.o rng-egd.o
 | 
			
		||||
common-obj-$(CONFIG_POSIX) += rng-random.o
 | 
			
		||||
 | 
			
		||||
common-obj-$(CONFIG_TPM) += tpm.o
 | 
			
		||||
 | 
			
		||||
common-obj-y += hostmem.o hostmem-ram.o
 | 
			
		||||
common-obj-$(CONFIG_LINUX) += hostmem-file.o
 | 
			
		||||
 | 
			
		||||
common-obj-y += cryptodev.o
 | 
			
		||||
common-obj-y += cryptodev-builtin.o
 | 
			
		||||
 
 | 
			
		||||
@@ -1,400 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU Cryptodev backend for QEMU cipher APIs
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *    Gonglei <arei.gonglei@huawei.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/cryptodev.h"
 | 
			
		||||
#include "hw/boards.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "standard-headers/linux/virtio_crypto.h"
 | 
			
		||||
#include "crypto/cipher.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @TYPE_CRYPTODEV_BACKEND_BUILTIN:
 | 
			
		||||
 * name of backend that uses QEMU cipher API
 | 
			
		||||
 */
 | 
			
		||||
#define TYPE_CRYPTODEV_BACKEND_BUILTIN "cryptodev-backend-builtin"
 | 
			
		||||
 | 
			
		||||
#define CRYPTODEV_BACKEND_BUILTIN(obj) \
 | 
			
		||||
    OBJECT_CHECK(CryptoDevBackendBuiltin, \
 | 
			
		||||
                 (obj), TYPE_CRYPTODEV_BACKEND_BUILTIN)
 | 
			
		||||
 | 
			
		||||
typedef struct CryptoDevBackendBuiltin
 | 
			
		||||
                         CryptoDevBackendBuiltin;
 | 
			
		||||
 | 
			
		||||
typedef struct CryptoDevBackendBuiltinSession {
 | 
			
		||||
    QCryptoCipher *cipher;
 | 
			
		||||
    uint8_t direction; /* encryption or decryption */
 | 
			
		||||
    uint8_t type; /* cipher? hash? aead? */
 | 
			
		||||
    QTAILQ_ENTRY(CryptoDevBackendBuiltinSession) next;
 | 
			
		||||
} CryptoDevBackendBuiltinSession;
 | 
			
		||||
 | 
			
		||||
/* Max number of symmetric sessions */
 | 
			
		||||
#define MAX_NUM_SESSIONS 256
 | 
			
		||||
 | 
			
		||||
#define CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN    512
 | 
			
		||||
#define CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN  64
 | 
			
		||||
 | 
			
		||||
struct CryptoDevBackendBuiltin {
 | 
			
		||||
    CryptoDevBackend parent_obj;
 | 
			
		||||
 | 
			
		||||
    CryptoDevBackendBuiltinSession *sessions[MAX_NUM_SESSIONS];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void cryptodev_builtin_init(
 | 
			
		||||
             CryptoDevBackend *backend, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    /* Only support one queue */
 | 
			
		||||
    int queues = backend->conf.peers.queues;
 | 
			
		||||
    CryptoDevBackendClient *cc;
 | 
			
		||||
 | 
			
		||||
    if (queues != 1) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                  "Only support one queue in cryptdov-builtin backend");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cc = cryptodev_backend_new_client(
 | 
			
		||||
              "cryptodev-builtin", NULL);
 | 
			
		||||
    cc->info_str = g_strdup_printf("cryptodev-builtin0");
 | 
			
		||||
    cc->queue_index = 0;
 | 
			
		||||
    backend->conf.peers.ccs[0] = cc;
 | 
			
		||||
 | 
			
		||||
    backend->conf.crypto_services =
 | 
			
		||||
                         1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
 | 
			
		||||
                         1u << VIRTIO_CRYPTO_SERVICE_HASH |
 | 
			
		||||
                         1u << VIRTIO_CRYPTO_SERVICE_MAC;
 | 
			
		||||
    backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
 | 
			
		||||
    backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
 | 
			
		||||
    /*
 | 
			
		||||
     * Set the Maximum length of crypto request.
 | 
			
		||||
     * Why this value? Just avoid to overflow when
 | 
			
		||||
     * memory allocation for each crypto request.
 | 
			
		||||
     */
 | 
			
		||||
    backend->conf.max_size = LONG_MAX - sizeof(CryptoDevBackendSymOpInfo);
 | 
			
		||||
    backend->conf.max_cipher_key_len = CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN;
 | 
			
		||||
    backend->conf.max_auth_key_len = CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN;
 | 
			
		||||
 | 
			
		||||
    cryptodev_backend_set_ready(backend, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
cryptodev_builtin_get_unused_session_index(
 | 
			
		||||
                 CryptoDevBackendBuiltin *builtin)
 | 
			
		||||
{
 | 
			
		||||
    size_t i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < MAX_NUM_SESSIONS; i++) {
 | 
			
		||||
        if (builtin->sessions[i] == NULL) {
 | 
			
		||||
            return i;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#define AES_KEYSIZE_128 16
 | 
			
		||||
#define AES_KEYSIZE_192 24
 | 
			
		||||
#define AES_KEYSIZE_256 32
 | 
			
		||||
#define AES_KEYSIZE_128_XTS AES_KEYSIZE_256
 | 
			
		||||
#define AES_KEYSIZE_256_XTS 64
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
cryptodev_builtin_get_aes_algo(uint32_t key_len, int mode, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int algo;
 | 
			
		||||
 | 
			
		||||
    if (key_len == AES_KEYSIZE_128) {
 | 
			
		||||
        algo = QCRYPTO_CIPHER_ALG_AES_128;
 | 
			
		||||
    } else if (key_len == AES_KEYSIZE_192) {
 | 
			
		||||
        algo = QCRYPTO_CIPHER_ALG_AES_192;
 | 
			
		||||
    } else if (key_len == AES_KEYSIZE_256) { /* equals AES_KEYSIZE_128_XTS */
 | 
			
		||||
        if (mode == QCRYPTO_CIPHER_MODE_XTS) {
 | 
			
		||||
            algo = QCRYPTO_CIPHER_ALG_AES_128;
 | 
			
		||||
        } else {
 | 
			
		||||
            algo = QCRYPTO_CIPHER_ALG_AES_256;
 | 
			
		||||
        }
 | 
			
		||||
    } else if (key_len == AES_KEYSIZE_256_XTS) {
 | 
			
		||||
        if (mode == QCRYPTO_CIPHER_MODE_XTS) {
 | 
			
		||||
            algo = QCRYPTO_CIPHER_ALG_AES_256;
 | 
			
		||||
        } else {
 | 
			
		||||
            goto err;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        goto err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return algo;
 | 
			
		||||
 | 
			
		||||
err:
 | 
			
		||||
   error_setg(errp, "Unsupported key length :%u", key_len);
 | 
			
		||||
   return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int cryptodev_builtin_create_cipher_session(
 | 
			
		||||
                    CryptoDevBackendBuiltin *builtin,
 | 
			
		||||
                    CryptoDevBackendSymSessionInfo *sess_info,
 | 
			
		||||
                    Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int algo;
 | 
			
		||||
    int mode;
 | 
			
		||||
    QCryptoCipher *cipher;
 | 
			
		||||
    int index;
 | 
			
		||||
    CryptoDevBackendBuiltinSession *sess;
 | 
			
		||||
 | 
			
		||||
    if (sess_info->op_type != VIRTIO_CRYPTO_SYM_OP_CIPHER) {
 | 
			
		||||
        error_setg(errp, "Unsupported optype :%u", sess_info->op_type);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    index = cryptodev_builtin_get_unused_session_index(builtin);
 | 
			
		||||
    if (index < 0) {
 | 
			
		||||
        error_setg(errp, "Total number of sessions created exceeds %u",
 | 
			
		||||
                  MAX_NUM_SESSIONS);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    switch (sess_info->cipher_alg) {
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_AES_ECB:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_ECB;
 | 
			
		||||
        algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
 | 
			
		||||
                                                    mode, errp);
 | 
			
		||||
        if (algo < 0)  {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_AES_CBC:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_CBC;
 | 
			
		||||
        algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
 | 
			
		||||
                                                    mode, errp);
 | 
			
		||||
        if (algo < 0)  {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_AES_CTR:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_CTR;
 | 
			
		||||
        algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
 | 
			
		||||
                                                    mode, errp);
 | 
			
		||||
        if (algo < 0)  {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_AES_XTS:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_XTS;
 | 
			
		||||
        algo = cryptodev_builtin_get_aes_algo(sess_info->key_len,
 | 
			
		||||
                                                    mode, errp);
 | 
			
		||||
        if (algo < 0)  {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_ECB;
 | 
			
		||||
        algo = QCRYPTO_CIPHER_ALG_3DES;
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_CBC;
 | 
			
		||||
        algo = QCRYPTO_CIPHER_ALG_3DES;
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
 | 
			
		||||
        mode = QCRYPTO_CIPHER_MODE_CTR;
 | 
			
		||||
        algo = QCRYPTO_CIPHER_ALG_3DES;
 | 
			
		||||
        break;
 | 
			
		||||
    default:
 | 
			
		||||
        error_setg(errp, "Unsupported cipher alg :%u",
 | 
			
		||||
                   sess_info->cipher_alg);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cipher = qcrypto_cipher_new(algo, mode,
 | 
			
		||||
                               sess_info->cipher_key,
 | 
			
		||||
                               sess_info->key_len,
 | 
			
		||||
                               errp);
 | 
			
		||||
    if (!cipher) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sess = g_new0(CryptoDevBackendBuiltinSession, 1);
 | 
			
		||||
    sess->cipher = cipher;
 | 
			
		||||
    sess->direction = sess_info->direction;
 | 
			
		||||
    sess->type = sess_info->op_type;
 | 
			
		||||
 | 
			
		||||
    builtin->sessions[index] = sess;
 | 
			
		||||
 | 
			
		||||
    return index;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t cryptodev_builtin_sym_create_session(
 | 
			
		||||
           CryptoDevBackend *backend,
 | 
			
		||||
           CryptoDevBackendSymSessionInfo *sess_info,
 | 
			
		||||
           uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendBuiltin *builtin =
 | 
			
		||||
                      CRYPTODEV_BACKEND_BUILTIN(backend);
 | 
			
		||||
    int64_t session_id = -1;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    switch (sess_info->op_code) {
 | 
			
		||||
    case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
 | 
			
		||||
        ret = cryptodev_builtin_create_cipher_session(
 | 
			
		||||
                           builtin, sess_info, errp);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            return ret;
 | 
			
		||||
        } else {
 | 
			
		||||
            session_id = ret;
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case VIRTIO_CRYPTO_HASH_CREATE_SESSION:
 | 
			
		||||
    case VIRTIO_CRYPTO_MAC_CREATE_SESSION:
 | 
			
		||||
    default:
 | 
			
		||||
        error_setg(errp, "Unsupported opcode :%" PRIu32 "",
 | 
			
		||||
                   sess_info->op_code);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return session_id;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int cryptodev_builtin_sym_close_session(
 | 
			
		||||
           CryptoDevBackend *backend,
 | 
			
		||||
           uint64_t session_id,
 | 
			
		||||
           uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendBuiltin *builtin =
 | 
			
		||||
                      CRYPTODEV_BACKEND_BUILTIN(backend);
 | 
			
		||||
 | 
			
		||||
    if (session_id >= MAX_NUM_SESSIONS ||
 | 
			
		||||
              builtin->sessions[session_id] == NULL) {
 | 
			
		||||
        error_setg(errp, "Cannot find a valid session id: %" PRIu64 "",
 | 
			
		||||
                      session_id);
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qcrypto_cipher_free(builtin->sessions[session_id]->cipher);
 | 
			
		||||
    g_free(builtin->sessions[session_id]);
 | 
			
		||||
    builtin->sessions[session_id] = NULL;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int cryptodev_builtin_sym_operation(
 | 
			
		||||
                 CryptoDevBackend *backend,
 | 
			
		||||
                 CryptoDevBackendSymOpInfo *op_info,
 | 
			
		||||
                 uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendBuiltin *builtin =
 | 
			
		||||
                      CRYPTODEV_BACKEND_BUILTIN(backend);
 | 
			
		||||
    CryptoDevBackendBuiltinSession *sess;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    if (op_info->session_id >= MAX_NUM_SESSIONS ||
 | 
			
		||||
              builtin->sessions[op_info->session_id] == NULL) {
 | 
			
		||||
        error_setg(errp, "Cannot find a valid session id: %" PRIu64 "",
 | 
			
		||||
                   op_info->session_id);
 | 
			
		||||
        return -VIRTIO_CRYPTO_INVSESS;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (op_info->op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
               "Algorithm chain is unsupported for cryptdoev-builtin");
 | 
			
		||||
        return -VIRTIO_CRYPTO_NOTSUPP;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sess = builtin->sessions[op_info->session_id];
 | 
			
		||||
 | 
			
		||||
    if (op_info->iv_len > 0) {
 | 
			
		||||
        ret = qcrypto_cipher_setiv(sess->cipher, op_info->iv,
 | 
			
		||||
                                   op_info->iv_len, errp);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            return -VIRTIO_CRYPTO_ERR;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (sess->direction == VIRTIO_CRYPTO_OP_ENCRYPT) {
 | 
			
		||||
        ret = qcrypto_cipher_encrypt(sess->cipher, op_info->src,
 | 
			
		||||
                                     op_info->dst, op_info->src_len, errp);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            return -VIRTIO_CRYPTO_ERR;
 | 
			
		||||
        }
 | 
			
		||||
    } else {
 | 
			
		||||
        ret = qcrypto_cipher_decrypt(sess->cipher, op_info->src,
 | 
			
		||||
                                     op_info->dst, op_info->src_len, errp);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            return -VIRTIO_CRYPTO_ERR;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return VIRTIO_CRYPTO_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void cryptodev_builtin_cleanup(
 | 
			
		||||
             CryptoDevBackend *backend,
 | 
			
		||||
             Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendBuiltin *builtin =
 | 
			
		||||
                      CRYPTODEV_BACKEND_BUILTIN(backend);
 | 
			
		||||
    size_t i;
 | 
			
		||||
    int queues = backend->conf.peers.queues;
 | 
			
		||||
    CryptoDevBackendClient *cc;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < MAX_NUM_SESSIONS; i++) {
 | 
			
		||||
        if (builtin->sessions[i] != NULL) {
 | 
			
		||||
            cryptodev_builtin_sym_close_session(
 | 
			
		||||
                    backend, i, 0, errp);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < queues; i++) {
 | 
			
		||||
        cc = backend->conf.peers.ccs[i];
 | 
			
		||||
        if (cc) {
 | 
			
		||||
            cryptodev_backend_free_client(cc);
 | 
			
		||||
            backend->conf.peers.ccs[i] = NULL;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cryptodev_backend_set_ready(backend, false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_builtin_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_CLASS(oc);
 | 
			
		||||
 | 
			
		||||
    bc->init = cryptodev_builtin_init;
 | 
			
		||||
    bc->cleanup = cryptodev_builtin_cleanup;
 | 
			
		||||
    bc->create_session = cryptodev_builtin_sym_create_session;
 | 
			
		||||
    bc->close_session = cryptodev_builtin_sym_close_session;
 | 
			
		||||
    bc->do_sym_op = cryptodev_builtin_sym_operation;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo cryptodev_builtin_info = {
 | 
			
		||||
    .name = TYPE_CRYPTODEV_BACKEND_BUILTIN,
 | 
			
		||||
    .parent = TYPE_CRYPTODEV_BACKEND,
 | 
			
		||||
    .class_init = cryptodev_builtin_class_init,
 | 
			
		||||
    .instance_size = sizeof(CryptoDevBackendBuiltin),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_builtin_register_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&cryptodev_builtin_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(cryptodev_builtin_register_types);
 | 
			
		||||
@@ -1,271 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU Crypto Device Implementation
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *    Gonglei <arei.gonglei@huawei.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This library is free software; you can redistribute it and/or
 | 
			
		||||
 * modify it under the terms of the GNU Lesser General Public
 | 
			
		||||
 * License as published by the Free Software Foundation; either
 | 
			
		||||
 * version 2 of the License, or (at your option) any later version.
 | 
			
		||||
 *
 | 
			
		||||
 * This library is distributed in the hope that it will be useful,
 | 
			
		||||
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
			
		||||
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 | 
			
		||||
 * Lesser General Public License for more details.
 | 
			
		||||
 *
 | 
			
		||||
 * You should have received a copy of the GNU Lesser General Public
 | 
			
		||||
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/cryptodev.h"
 | 
			
		||||
#include "hw/boards.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qapi/visitor.h"
 | 
			
		||||
#include "qapi-types.h"
 | 
			
		||||
#include "qapi-visit.h"
 | 
			
		||||
#include "qemu/config-file.h"
 | 
			
		||||
#include "qom/object_interfaces.h"
 | 
			
		||||
#include "hw/virtio/virtio-crypto.h"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CryptoDevBackendClient *
 | 
			
		||||
cryptodev_backend_new_client(const char *model,
 | 
			
		||||
                                    const char *name)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendClient *cc;
 | 
			
		||||
 | 
			
		||||
    cc = g_malloc0(sizeof(CryptoDevBackendClient));
 | 
			
		||||
    cc->model = g_strdup(model);
 | 
			
		||||
    if (name) {
 | 
			
		||||
        cc->name = g_strdup(name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
 | 
			
		||||
 | 
			
		||||
    return cc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cryptodev_backend_free_client(
 | 
			
		||||
                  CryptoDevBackendClient *cc)
 | 
			
		||||
{
 | 
			
		||||
    QTAILQ_REMOVE(&crypto_clients, cc, next);
 | 
			
		||||
    g_free(cc->name);
 | 
			
		||||
    g_free(cc->model);
 | 
			
		||||
    g_free(cc->info_str);
 | 
			
		||||
    g_free(cc);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cryptodev_backend_cleanup(
 | 
			
		||||
             CryptoDevBackend *backend,
 | 
			
		||||
             Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendClass *bc =
 | 
			
		||||
                  CRYPTODEV_BACKEND_GET_CLASS(backend);
 | 
			
		||||
 | 
			
		||||
    if (bc->cleanup) {
 | 
			
		||||
        bc->cleanup(backend, errp);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int64_t cryptodev_backend_sym_create_session(
 | 
			
		||||
           CryptoDevBackend *backend,
 | 
			
		||||
           CryptoDevBackendSymSessionInfo *sess_info,
 | 
			
		||||
           uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendClass *bc =
 | 
			
		||||
                      CRYPTODEV_BACKEND_GET_CLASS(backend);
 | 
			
		||||
 | 
			
		||||
    if (bc->create_session) {
 | 
			
		||||
        return bc->create_session(backend, sess_info, queue_index, errp);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int cryptodev_backend_sym_close_session(
 | 
			
		||||
           CryptoDevBackend *backend,
 | 
			
		||||
           uint64_t session_id,
 | 
			
		||||
           uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendClass *bc =
 | 
			
		||||
                      CRYPTODEV_BACKEND_GET_CLASS(backend);
 | 
			
		||||
 | 
			
		||||
    if (bc->close_session) {
 | 
			
		||||
        return bc->close_session(backend, session_id, queue_index, errp);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int cryptodev_backend_sym_operation(
 | 
			
		||||
                 CryptoDevBackend *backend,
 | 
			
		||||
                 CryptoDevBackendSymOpInfo *op_info,
 | 
			
		||||
                 uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackendClass *bc =
 | 
			
		||||
                      CRYPTODEV_BACKEND_GET_CLASS(backend);
 | 
			
		||||
 | 
			
		||||
    if (bc->do_sym_op) {
 | 
			
		||||
        return bc->do_sym_op(backend, op_info, queue_index, errp);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -VIRTIO_CRYPTO_ERR;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int cryptodev_backend_crypto_operation(
 | 
			
		||||
                 CryptoDevBackend *backend,
 | 
			
		||||
                 void *opaque,
 | 
			
		||||
                 uint32_t queue_index, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    VirtIOCryptoReq *req = opaque;
 | 
			
		||||
 | 
			
		||||
    if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
 | 
			
		||||
        CryptoDevBackendSymOpInfo *op_info;
 | 
			
		||||
        op_info = req->u.sym_op_info;
 | 
			
		||||
 | 
			
		||||
        return cryptodev_backend_sym_operation(backend,
 | 
			
		||||
                         op_info, queue_index, errp);
 | 
			
		||||
    } else {
 | 
			
		||||
        error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "",
 | 
			
		||||
                   req->flags);
 | 
			
		||||
       return -VIRTIO_CRYPTO_NOTSUPP;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -VIRTIO_CRYPTO_ERR;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_backend_get_queues(Object *obj, Visitor *v, const char *name,
 | 
			
		||||
                             void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
 | 
			
		||||
    uint32_t value = backend->conf.peers.queues;
 | 
			
		||||
 | 
			
		||||
    visit_type_uint32(v, name, &value, errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_backend_set_queues(Object *obj, Visitor *v, const char *name,
 | 
			
		||||
                             void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    uint32_t value;
 | 
			
		||||
 | 
			
		||||
    visit_type_uint32(v, name, &value, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    if (!value) {
 | 
			
		||||
        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
 | 
			
		||||
                   PRIu32 "'", object_get_typename(obj), name, value);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    backend->conf.peers.queues = value;
 | 
			
		||||
out:
 | 
			
		||||
    error_propagate(errp, local_err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_backend_complete(UserCreatable *uc, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
 | 
			
		||||
    CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
 | 
			
		||||
    if (bc->init) {
 | 
			
		||||
        bc->init(backend, &local_err);
 | 
			
		||||
        if (local_err) {
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
out:
 | 
			
		||||
    error_propagate(errp, local_err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
 | 
			
		||||
{
 | 
			
		||||
    backend->is_used = used;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool cryptodev_backend_is_used(CryptoDevBackend *backend)
 | 
			
		||||
{
 | 
			
		||||
    return backend->is_used;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void cryptodev_backend_set_ready(CryptoDevBackend *backend, bool ready)
 | 
			
		||||
{
 | 
			
		||||
    backend->ready = ready;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool cryptodev_backend_is_ready(CryptoDevBackend *backend)
 | 
			
		||||
{
 | 
			
		||||
    return backend->ready;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
cryptodev_backend_can_be_deleted(UserCreatable *uc, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    return !cryptodev_backend_is_used(CRYPTODEV_BACKEND(uc));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void cryptodev_backend_instance_init(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    object_property_add(obj, "queues", "int",
 | 
			
		||||
                          cryptodev_backend_get_queues,
 | 
			
		||||
                          cryptodev_backend_set_queues,
 | 
			
		||||
                          NULL, NULL, NULL);
 | 
			
		||||
    /* Initialize devices' queues property to 1 */
 | 
			
		||||
    object_property_set_int(obj, 1, "queues", NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void cryptodev_backend_finalize(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    CryptoDevBackend *backend = CRYPTODEV_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    cryptodev_backend_cleanup(backend, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_backend_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
 | 
			
		||||
 | 
			
		||||
    ucc->complete = cryptodev_backend_complete;
 | 
			
		||||
    ucc->can_be_deleted = cryptodev_backend_can_be_deleted;
 | 
			
		||||
 | 
			
		||||
    QTAILQ_INIT(&crypto_clients);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo cryptodev_backend_info = {
 | 
			
		||||
    .name = TYPE_CRYPTODEV_BACKEND,
 | 
			
		||||
    .parent = TYPE_OBJECT,
 | 
			
		||||
    .instance_size = sizeof(CryptoDevBackend),
 | 
			
		||||
    .instance_init = cryptodev_backend_instance_init,
 | 
			
		||||
    .instance_finalize = cryptodev_backend_finalize,
 | 
			
		||||
    .class_size = sizeof(CryptoDevBackendClass),
 | 
			
		||||
    .class_init = cryptodev_backend_class_init,
 | 
			
		||||
    .interfaces = (InterfaceInfo[]) {
 | 
			
		||||
        { TYPE_USER_CREATABLE },
 | 
			
		||||
        { }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
cryptodev_backend_register_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&cryptodev_backend_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(cryptodev_backend_register_types);
 | 
			
		||||
@@ -1,141 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU Host Memory Backend for hugetlbfs
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2013-2014 Red Hat Inc
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *   Paolo Bonzini <pbonzini@redhat.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "sysemu/hostmem.h"
 | 
			
		||||
#include "sysemu/sysemu.h"
 | 
			
		||||
#include "qom/object_interfaces.h"
 | 
			
		||||
 | 
			
		||||
/* hostmem-file.c */
 | 
			
		||||
/**
 | 
			
		||||
 * @TYPE_MEMORY_BACKEND_FILE:
 | 
			
		||||
 * name of backend that uses mmap on a file descriptor
 | 
			
		||||
 */
 | 
			
		||||
#define TYPE_MEMORY_BACKEND_FILE "memory-backend-file"
 | 
			
		||||
 | 
			
		||||
#define MEMORY_BACKEND_FILE(obj) \
 | 
			
		||||
    OBJECT_CHECK(HostMemoryBackendFile, (obj), TYPE_MEMORY_BACKEND_FILE)
 | 
			
		||||
 | 
			
		||||
typedef struct HostMemoryBackendFile HostMemoryBackendFile;
 | 
			
		||||
 | 
			
		||||
struct HostMemoryBackendFile {
 | 
			
		||||
    HostMemoryBackend parent_obj;
 | 
			
		||||
 | 
			
		||||
    bool share;
 | 
			
		||||
    char *mem_path;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
file_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(backend);
 | 
			
		||||
 | 
			
		||||
    if (!backend->size) {
 | 
			
		||||
        error_setg(errp, "can't create backend with size 0");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (!fb->mem_path) {
 | 
			
		||||
        error_setg(errp, "mem-path property not set");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
#ifndef CONFIG_LINUX
 | 
			
		||||
    error_setg(errp, "-mem-path not supported on this host");
 | 
			
		||||
#else
 | 
			
		||||
    if (!host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        gchar *path;
 | 
			
		||||
        backend->force_prealloc = mem_prealloc;
 | 
			
		||||
        path = object_get_canonical_path(OBJECT(backend));
 | 
			
		||||
        memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
 | 
			
		||||
                                 path,
 | 
			
		||||
                                 backend->size, fb->share,
 | 
			
		||||
                                 fb->mem_path, errp);
 | 
			
		||||
        g_free(path);
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *get_mem_path(Object *o, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
 | 
			
		||||
 | 
			
		||||
    return g_strdup(fb->mem_path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void set_mem_path(Object *o, const char *str, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(o);
 | 
			
		||||
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
 | 
			
		||||
 | 
			
		||||
    if (host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        error_setg(errp, "cannot change property value");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    g_free(fb->mem_path);
 | 
			
		||||
    fb->mem_path = g_strdup(str);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool file_memory_backend_get_share(Object *o, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
 | 
			
		||||
 | 
			
		||||
    return fb->share;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void file_memory_backend_set_share(Object *o, bool value, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(o);
 | 
			
		||||
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
 | 
			
		||||
 | 
			
		||||
    if (host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        error_setg(errp, "cannot change property value");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    fb->share = value;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
file_backend_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
 | 
			
		||||
 | 
			
		||||
    bc->alloc = file_backend_memory_alloc;
 | 
			
		||||
 | 
			
		||||
    object_class_property_add_bool(oc, "share",
 | 
			
		||||
        file_memory_backend_get_share, file_memory_backend_set_share,
 | 
			
		||||
        &error_abort);
 | 
			
		||||
    object_class_property_add_str(oc, "mem-path",
 | 
			
		||||
        get_mem_path, set_mem_path,
 | 
			
		||||
        &error_abort);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void file_backend_instance_finalize(Object *o)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackendFile *fb = MEMORY_BACKEND_FILE(o);
 | 
			
		||||
 | 
			
		||||
    g_free(fb->mem_path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo file_backend_info = {
 | 
			
		||||
    .name = TYPE_MEMORY_BACKEND_FILE,
 | 
			
		||||
    .parent = TYPE_MEMORY_BACKEND,
 | 
			
		||||
    .class_init = file_backend_class_init,
 | 
			
		||||
    .instance_finalize = file_backend_instance_finalize,
 | 
			
		||||
    .instance_size = sizeof(HostMemoryBackendFile),
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void register_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&file_backend_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(register_types);
 | 
			
		||||
@@ -1,55 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU Host Memory Backend
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2013-2014 Red Hat Inc
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *   Igor Mammedov <imammedo@redhat.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/hostmem.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qom/object_interfaces.h"
 | 
			
		||||
 | 
			
		||||
#define TYPE_MEMORY_BACKEND_RAM "memory-backend-ram"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
ram_backend_memory_alloc(HostMemoryBackend *backend, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    char *path;
 | 
			
		||||
 | 
			
		||||
    if (!backend->size) {
 | 
			
		||||
        error_setg(errp, "can't create backend with size 0");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    path = object_get_canonical_path_component(OBJECT(backend));
 | 
			
		||||
    memory_region_init_ram(&backend->mr, OBJECT(backend), path,
 | 
			
		||||
                           backend->size, errp);
 | 
			
		||||
    g_free(path);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
ram_backend_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
 | 
			
		||||
 | 
			
		||||
    bc->alloc = ram_backend_memory_alloc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo ram_backend_info = {
 | 
			
		||||
    .name = TYPE_MEMORY_BACKEND_RAM,
 | 
			
		||||
    .parent = TYPE_MEMORY_BACKEND,
 | 
			
		||||
    .class_init = ram_backend_class_init,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void register_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&ram_backend_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(register_types);
 | 
			
		||||
@@ -1,430 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU Host Memory Backend
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2013-2014 Red Hat Inc
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *   Igor Mammedov <imammedo@redhat.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/hostmem.h"
 | 
			
		||||
#include "hw/boards.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qapi/visitor.h"
 | 
			
		||||
#include "qapi-types.h"
 | 
			
		||||
#include "qapi-visit.h"
 | 
			
		||||
#include "qemu/config-file.h"
 | 
			
		||||
#include "qom/object_interfaces.h"
 | 
			
		||||
 | 
			
		||||
#ifdef CONFIG_NUMA
 | 
			
		||||
#include <numaif.h>
 | 
			
		||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_DEFAULT != MPOL_DEFAULT);
 | 
			
		||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_PREFERRED != MPOL_PREFERRED);
 | 
			
		||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_BIND != MPOL_BIND);
 | 
			
		||||
QEMU_BUILD_BUG_ON(HOST_MEM_POLICY_INTERLEAVE != MPOL_INTERLEAVE);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_get_size(Object *obj, Visitor *v, const char *name,
 | 
			
		||||
                             void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    uint64_t value = backend->size;
 | 
			
		||||
 | 
			
		||||
    visit_type_size(v, name, &value, errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_set_size(Object *obj, Visitor *v, const char *name,
 | 
			
		||||
                             void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    uint64_t value;
 | 
			
		||||
 | 
			
		||||
    if (host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        error_setg(&local_err, "cannot change property value");
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    visit_type_size(v, name, &value, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    if (!value) {
 | 
			
		||||
        error_setg(&local_err, "Property '%s.%s' doesn't take value '%"
 | 
			
		||||
                   PRIu64 "'", object_get_typename(obj), name, value);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    backend->size = value;
 | 
			
		||||
out:
 | 
			
		||||
    error_propagate(errp, local_err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_get_host_nodes(Object *obj, Visitor *v, const char *name,
 | 
			
		||||
                                   void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    uint16List *host_nodes = NULL;
 | 
			
		||||
    uint16List **node = &host_nodes;
 | 
			
		||||
    unsigned long value;
 | 
			
		||||
 | 
			
		||||
    value = find_first_bit(backend->host_nodes, MAX_NODES);
 | 
			
		||||
    if (value == MAX_NODES) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    *node = g_malloc0(sizeof(**node));
 | 
			
		||||
    (*node)->value = value;
 | 
			
		||||
    node = &(*node)->next;
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
        value = find_next_bit(backend->host_nodes, MAX_NODES, value + 1);
 | 
			
		||||
        if (value == MAX_NODES) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        *node = g_malloc0(sizeof(**node));
 | 
			
		||||
        (*node)->value = value;
 | 
			
		||||
        node = &(*node)->next;
 | 
			
		||||
    } while (true);
 | 
			
		||||
 | 
			
		||||
    visit_type_uint16List(v, name, &host_nodes, errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_set_host_nodes(Object *obj, Visitor *v, const char *name,
 | 
			
		||||
                                   void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
#ifdef CONFIG_NUMA
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    uint16List *l = NULL;
 | 
			
		||||
 | 
			
		||||
    visit_type_uint16List(v, name, &l, errp);
 | 
			
		||||
 | 
			
		||||
    while (l) {
 | 
			
		||||
        bitmap_set(backend->host_nodes, l->value, 1);
 | 
			
		||||
        l = l->next;
 | 
			
		||||
    }
 | 
			
		||||
#else
 | 
			
		||||
    error_setg(errp, "NUMA node binding are not supported by this QEMU");
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int
 | 
			
		||||
host_memory_backend_get_policy(Object *obj, Error **errp G_GNUC_UNUSED)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    return backend->policy;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_set_policy(Object *obj, int policy, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    backend->policy = policy;
 | 
			
		||||
 | 
			
		||||
#ifndef CONFIG_NUMA
 | 
			
		||||
    if (policy != HOST_MEM_POLICY_DEFAULT) {
 | 
			
		||||
        error_setg(errp, "NUMA policies are not supported by this QEMU");
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool host_memory_backend_get_merge(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    return backend->merge;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void host_memory_backend_set_merge(Object *obj, bool value, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    if (!host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        backend->merge = value;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (value != backend->merge) {
 | 
			
		||||
        void *ptr = memory_region_get_ram_ptr(&backend->mr);
 | 
			
		||||
        uint64_t sz = memory_region_size(&backend->mr);
 | 
			
		||||
 | 
			
		||||
        qemu_madvise(ptr, sz,
 | 
			
		||||
                     value ? QEMU_MADV_MERGEABLE : QEMU_MADV_UNMERGEABLE);
 | 
			
		||||
        backend->merge = value;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool host_memory_backend_get_dump(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    return backend->dump;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void host_memory_backend_set_dump(Object *obj, bool value, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    if (!host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        backend->dump = value;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (value != backend->dump) {
 | 
			
		||||
        void *ptr = memory_region_get_ram_ptr(&backend->mr);
 | 
			
		||||
        uint64_t sz = memory_region_size(&backend->mr);
 | 
			
		||||
 | 
			
		||||
        qemu_madvise(ptr, sz,
 | 
			
		||||
                     value ? QEMU_MADV_DODUMP : QEMU_MADV_DONTDUMP);
 | 
			
		||||
        backend->dump = value;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool host_memory_backend_get_prealloc(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    return backend->prealloc || backend->force_prealloc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void host_memory_backend_set_prealloc(Object *obj, bool value,
 | 
			
		||||
                                             Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    if (backend->force_prealloc) {
 | 
			
		||||
        if (value) {
 | 
			
		||||
            error_setg(errp,
 | 
			
		||||
                       "remove -mem-prealloc to use the prealloc property");
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!host_memory_backend_mr_inited(backend)) {
 | 
			
		||||
        backend->prealloc = value;
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (value && !backend->prealloc) {
 | 
			
		||||
        int fd = memory_region_get_fd(&backend->mr);
 | 
			
		||||
        void *ptr = memory_region_get_ram_ptr(&backend->mr);
 | 
			
		||||
        uint64_t sz = memory_region_size(&backend->mr);
 | 
			
		||||
 | 
			
		||||
        os_mem_prealloc(fd, ptr, sz, smp_cpus, &local_err);
 | 
			
		||||
        if (local_err) {
 | 
			
		||||
            error_propagate(errp, local_err);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        backend->prealloc = true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void host_memory_backend_init(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(obj);
 | 
			
		||||
    MachineState *machine = MACHINE(qdev_get_machine());
 | 
			
		||||
 | 
			
		||||
    backend->merge = machine_mem_merge(machine);
 | 
			
		||||
    backend->dump = machine_dump_guest_core(machine);
 | 
			
		||||
    backend->prealloc = mem_prealloc;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool host_memory_backend_mr_inited(HostMemoryBackend *backend)
 | 
			
		||||
{
 | 
			
		||||
    /*
 | 
			
		||||
     * NOTE: We forbid zero-length memory backend, so here zero means
 | 
			
		||||
     * "we haven't inited the backend memory region yet".
 | 
			
		||||
     */
 | 
			
		||||
    return memory_region_size(&backend->mr) != 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MemoryRegion *
 | 
			
		||||
host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    return host_memory_backend_mr_inited(backend) ? &backend->mr : NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void host_memory_backend_set_mapped(HostMemoryBackend *backend, bool mapped)
 | 
			
		||||
{
 | 
			
		||||
    backend->is_mapped = mapped;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool host_memory_backend_is_mapped(HostMemoryBackend *backend)
 | 
			
		||||
{
 | 
			
		||||
    return backend->is_mapped;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_memory_complete(UserCreatable *uc, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(uc);
 | 
			
		||||
    HostMemoryBackendClass *bc = MEMORY_BACKEND_GET_CLASS(uc);
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    void *ptr;
 | 
			
		||||
    uint64_t sz;
 | 
			
		||||
 | 
			
		||||
    if (bc->alloc) {
 | 
			
		||||
        bc->alloc(backend, &local_err);
 | 
			
		||||
        if (local_err) {
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ptr = memory_region_get_ram_ptr(&backend->mr);
 | 
			
		||||
        sz = memory_region_size(&backend->mr);
 | 
			
		||||
 | 
			
		||||
        if (backend->merge) {
 | 
			
		||||
            qemu_madvise(ptr, sz, QEMU_MADV_MERGEABLE);
 | 
			
		||||
        }
 | 
			
		||||
        if (!backend->dump) {
 | 
			
		||||
            qemu_madvise(ptr, sz, QEMU_MADV_DONTDUMP);
 | 
			
		||||
        }
 | 
			
		||||
#ifdef CONFIG_NUMA
 | 
			
		||||
        unsigned long lastbit = find_last_bit(backend->host_nodes, MAX_NODES);
 | 
			
		||||
        /* lastbit == MAX_NODES means maxnode = 0 */
 | 
			
		||||
        unsigned long maxnode = (lastbit + 1) % (MAX_NODES + 1);
 | 
			
		||||
        /* ensure policy won't be ignored in case memory is preallocated
 | 
			
		||||
         * before mbind(). note: MPOL_MF_STRICT is ignored on hugepages so
 | 
			
		||||
         * this doesn't catch hugepage case. */
 | 
			
		||||
        unsigned flags = MPOL_MF_STRICT | MPOL_MF_MOVE;
 | 
			
		||||
 | 
			
		||||
        /* check for invalid host-nodes and policies and give more verbose
 | 
			
		||||
         * error messages than mbind(). */
 | 
			
		||||
        if (maxnode && backend->policy == MPOL_DEFAULT) {
 | 
			
		||||
            error_setg(errp, "host-nodes must be empty for policy default,"
 | 
			
		||||
                       " or you should explicitly specify a policy other"
 | 
			
		||||
                       " than default");
 | 
			
		||||
            return;
 | 
			
		||||
        } else if (maxnode == 0 && backend->policy != MPOL_DEFAULT) {
 | 
			
		||||
            error_setg(errp, "host-nodes must be set for policy %s",
 | 
			
		||||
                       HostMemPolicy_lookup[backend->policy]);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* We can have up to MAX_NODES nodes, but we need to pass maxnode+1
 | 
			
		||||
         * as argument to mbind() due to an old Linux bug (feature?) which
 | 
			
		||||
         * cuts off the last specified node. This means backend->host_nodes
 | 
			
		||||
         * must have MAX_NODES+1 bits available.
 | 
			
		||||
         */
 | 
			
		||||
        assert(sizeof(backend->host_nodes) >=
 | 
			
		||||
               BITS_TO_LONGS(MAX_NODES + 1) * sizeof(unsigned long));
 | 
			
		||||
        assert(maxnode <= MAX_NODES);
 | 
			
		||||
        if (mbind(ptr, sz, backend->policy,
 | 
			
		||||
                  maxnode ? backend->host_nodes : NULL, maxnode + 1, flags)) {
 | 
			
		||||
            if (backend->policy != MPOL_DEFAULT || errno != ENOSYS) {
 | 
			
		||||
                error_setg_errno(errp, errno,
 | 
			
		||||
                                 "cannot bind memory to host NUMA nodes");
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        /* Preallocate memory after the NUMA policy has been instantiated.
 | 
			
		||||
         * This is necessary to guarantee memory is allocated with
 | 
			
		||||
         * specified NUMA policy in place.
 | 
			
		||||
         */
 | 
			
		||||
        if (backend->prealloc) {
 | 
			
		||||
            os_mem_prealloc(memory_region_get_fd(&backend->mr), ptr, sz,
 | 
			
		||||
                            smp_cpus, &local_err);
 | 
			
		||||
            if (local_err) {
 | 
			
		||||
                goto out;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
out:
 | 
			
		||||
    error_propagate(errp, local_err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool
 | 
			
		||||
host_memory_backend_can_be_deleted(UserCreatable *uc, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    if (host_memory_backend_is_mapped(MEMORY_BACKEND(uc))) {
 | 
			
		||||
        return false;
 | 
			
		||||
    } else {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *get_id(Object *o, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(o);
 | 
			
		||||
 | 
			
		||||
    return g_strdup(backend->id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void set_id(Object *o, const char *str, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(o);
 | 
			
		||||
 | 
			
		||||
    if (backend->id) {
 | 
			
		||||
        error_setg(errp, "cannot change property value");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    backend->id = g_strdup(str);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void
 | 
			
		||||
host_memory_backend_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
 | 
			
		||||
 | 
			
		||||
    ucc->complete = host_memory_backend_memory_complete;
 | 
			
		||||
    ucc->can_be_deleted = host_memory_backend_can_be_deleted;
 | 
			
		||||
 | 
			
		||||
    object_class_property_add_bool(oc, "merge",
 | 
			
		||||
        host_memory_backend_get_merge,
 | 
			
		||||
        host_memory_backend_set_merge, &error_abort);
 | 
			
		||||
    object_class_property_add_bool(oc, "dump",
 | 
			
		||||
        host_memory_backend_get_dump,
 | 
			
		||||
        host_memory_backend_set_dump, &error_abort);
 | 
			
		||||
    object_class_property_add_bool(oc, "prealloc",
 | 
			
		||||
        host_memory_backend_get_prealloc,
 | 
			
		||||
        host_memory_backend_set_prealloc, &error_abort);
 | 
			
		||||
    object_class_property_add(oc, "size", "int",
 | 
			
		||||
        host_memory_backend_get_size,
 | 
			
		||||
        host_memory_backend_set_size,
 | 
			
		||||
        NULL, NULL, &error_abort);
 | 
			
		||||
    object_class_property_add(oc, "host-nodes", "int",
 | 
			
		||||
        host_memory_backend_get_host_nodes,
 | 
			
		||||
        host_memory_backend_set_host_nodes,
 | 
			
		||||
        NULL, NULL, &error_abort);
 | 
			
		||||
    object_class_property_add_enum(oc, "policy", "HostMemPolicy",
 | 
			
		||||
        HostMemPolicy_lookup,
 | 
			
		||||
        host_memory_backend_get_policy,
 | 
			
		||||
        host_memory_backend_set_policy, &error_abort);
 | 
			
		||||
    object_class_property_add_str(oc, "id", get_id, set_id, &error_abort);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void host_memory_backend_finalize(Object *o)
 | 
			
		||||
{
 | 
			
		||||
    HostMemoryBackend *backend = MEMORY_BACKEND(o);
 | 
			
		||||
    g_free(backend->id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo host_memory_backend_info = {
 | 
			
		||||
    .name = TYPE_MEMORY_BACKEND,
 | 
			
		||||
    .parent = TYPE_OBJECT,
 | 
			
		||||
    .abstract = true,
 | 
			
		||||
    .class_size = sizeof(HostMemoryBackendClass),
 | 
			
		||||
    .class_init = host_memory_backend_class_init,
 | 
			
		||||
    .instance_size = sizeof(HostMemoryBackend),
 | 
			
		||||
    .instance_init = host_memory_backend_init,
 | 
			
		||||
    .instance_finalize = host_memory_backend_finalize,
 | 
			
		||||
    .interfaces = (InterfaceInfo[]) {
 | 
			
		||||
        { TYPE_USER_CREATABLE },
 | 
			
		||||
        { }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void register_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&host_memory_backend_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(register_types);
 | 
			
		||||
@@ -10,11 +10,10 @@
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/rng.h"
 | 
			
		||||
#include "chardev/char-fe.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu/rng.h"
 | 
			
		||||
#include "char/char.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "hw/qdev.h" /* just for DEFINE_PROP_CHR */
 | 
			
		||||
 | 
			
		||||
#define TYPE_RNG_EGD "rng-egd"
 | 
			
		||||
#define RNG_EGD(obj) OBJECT_CHECK(RngEgd, (obj), TYPE_RNG_EGD)
 | 
			
		||||
@@ -23,14 +22,35 @@ typedef struct RngEgd
 | 
			
		||||
{
 | 
			
		||||
    RngBackend parent;
 | 
			
		||||
 | 
			
		||||
    CharBackend chr;
 | 
			
		||||
    CharDriverState *chr;
 | 
			
		||||
    char *chr_name;
 | 
			
		||||
 | 
			
		||||
    GSList *requests;
 | 
			
		||||
} RngEgd;
 | 
			
		||||
 | 
			
		||||
static void rng_egd_request_entropy(RngBackend *b, RngRequest *req)
 | 
			
		||||
typedef struct RngRequest
 | 
			
		||||
{
 | 
			
		||||
    EntropyReceiveFunc *receive_entropy;
 | 
			
		||||
    uint8_t *data;
 | 
			
		||||
    void *opaque;
 | 
			
		||||
    size_t offset;
 | 
			
		||||
    size_t size;
 | 
			
		||||
} RngRequest;
 | 
			
		||||
 | 
			
		||||
static void rng_egd_request_entropy(RngBackend *b, size_t size,
 | 
			
		||||
                                    EntropyReceiveFunc *receive_entropy,
 | 
			
		||||
                                    void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(b);
 | 
			
		||||
    size_t size = req->size;
 | 
			
		||||
    RngRequest *req;
 | 
			
		||||
 | 
			
		||||
    req = g_malloc(sizeof(*req));
 | 
			
		||||
 | 
			
		||||
    req->offset = 0;
 | 
			
		||||
    req->size = size;
 | 
			
		||||
    req->receive_entropy = receive_entropy;
 | 
			
		||||
    req->opaque = opaque;
 | 
			
		||||
    req->data = g_malloc(req->size);
 | 
			
		||||
 | 
			
		||||
    while (size > 0) {
 | 
			
		||||
        uint8_t header[2];
 | 
			
		||||
@@ -40,21 +60,28 @@ static void rng_egd_request_entropy(RngBackend *b, RngRequest *req)
 | 
			
		||||
        header[0] = 0x02;
 | 
			
		||||
        header[1] = len;
 | 
			
		||||
 | 
			
		||||
        /* XXX this blocks entire thread. Rewrite to use
 | 
			
		||||
         * qemu_chr_fe_write and background I/O callbacks */
 | 
			
		||||
        qemu_chr_fe_write_all(&s->chr, header, sizeof(header));
 | 
			
		||||
        qemu_chr_fe_write(s->chr, header, sizeof(header));
 | 
			
		||||
 | 
			
		||||
        size -= len;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->requests = g_slist_append(s->requests, req);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_egd_free_request(RngRequest *req)
 | 
			
		||||
{
 | 
			
		||||
    g_free(req->data);
 | 
			
		||||
    g_free(req);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int rng_egd_chr_can_read(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(opaque);
 | 
			
		||||
    RngRequest *req;
 | 
			
		||||
    GSList *i;
 | 
			
		||||
    int size = 0;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(req, &s->parent.requests, next) {
 | 
			
		||||
    for (i = s->requests; i; i = i->next) {
 | 
			
		||||
        RngRequest *req = i->data;
 | 
			
		||||
        size += req->size - req->offset;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -64,49 +91,67 @@ static int rng_egd_chr_can_read(void *opaque)
 | 
			
		||||
static void rng_egd_chr_read(void *opaque, const uint8_t *buf, int size)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(opaque);
 | 
			
		||||
    size_t buf_offset = 0;
 | 
			
		||||
 | 
			
		||||
    while (size > 0 && !QSIMPLEQ_EMPTY(&s->parent.requests)) {
 | 
			
		||||
        RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
 | 
			
		||||
    while (size > 0 && s->requests) {
 | 
			
		||||
        RngRequest *req = s->requests->data;
 | 
			
		||||
        int len = MIN(size, req->size - req->offset);
 | 
			
		||||
 | 
			
		||||
        memcpy(req->data + req->offset, buf + buf_offset, len);
 | 
			
		||||
        buf_offset += len;
 | 
			
		||||
        memcpy(req->data + req->offset, buf, len);
 | 
			
		||||
        req->offset += len;
 | 
			
		||||
        size -= len;
 | 
			
		||||
 | 
			
		||||
        if (req->offset == req->size) {
 | 
			
		||||
            s->requests = g_slist_remove_link(s->requests, s->requests);
 | 
			
		||||
 | 
			
		||||
            req->receive_entropy(req->opaque, req->data, req->size);
 | 
			
		||||
 | 
			
		||||
            rng_backend_finalize_request(&s->parent, req);
 | 
			
		||||
            rng_egd_free_request(req);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_egd_free_requests(RngEgd *s)
 | 
			
		||||
{
 | 
			
		||||
    GSList *i;
 | 
			
		||||
 | 
			
		||||
    for (i = s->requests; i; i = i->next) {
 | 
			
		||||
        rng_egd_free_request(i->data);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_slist_free(s->requests);
 | 
			
		||||
    s->requests = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_egd_cancel_requests(RngBackend *b)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(b);
 | 
			
		||||
 | 
			
		||||
    /* We simply delete the list of pending requests.  If there is data in the 
 | 
			
		||||
     * queue waiting to be read, this is okay, because there will always be
 | 
			
		||||
     * more data than we requested originally
 | 
			
		||||
     */
 | 
			
		||||
    rng_egd_free_requests(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_egd_opened(RngBackend *b, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(b);
 | 
			
		||||
    Chardev *chr;
 | 
			
		||||
 | 
			
		||||
    if (s->chr_name == NULL) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                   "chardev", "a valid character device");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                  "chardev", "a valid character device");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    chr = qemu_chr_find(s->chr_name);
 | 
			
		||||
    if (chr == NULL) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
 | 
			
		||||
                  "Device '%s' not found", s->chr_name);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    if (!qemu_chr_fe_init(&s->chr, chr, errp)) {
 | 
			
		||||
    s->chr = qemu_chr_find(s->chr_name);
 | 
			
		||||
    if (s->chr == NULL) {
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_FOUND, s->chr_name);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* FIXME we should resubmit pending requests when the CDS reconnects. */
 | 
			
		||||
    qemu_chr_fe_set_handlers(&s->chr, rng_egd_chr_can_read,
 | 
			
		||||
                             rng_egd_chr_read, NULL, s, NULL, true);
 | 
			
		||||
    qemu_chr_add_handlers(s->chr, rng_egd_chr_can_read, rng_egd_chr_read,
 | 
			
		||||
                          NULL, s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
 | 
			
		||||
@@ -115,7 +160,7 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
 | 
			
		||||
    RngEgd *s = RNG_EGD(b);
 | 
			
		||||
 | 
			
		||||
    if (b->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
    } else {
 | 
			
		||||
        g_free(s->chr_name);
 | 
			
		||||
        s->chr_name = g_strdup(value);
 | 
			
		||||
@@ -125,10 +170,9 @@ static void rng_egd_set_chardev(Object *obj, const char *value, Error **errp)
 | 
			
		||||
static char *rng_egd_get_chardev(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(obj);
 | 
			
		||||
    Chardev *chr = qemu_chr_fe_get_driver(&s->chr);
 | 
			
		||||
 | 
			
		||||
    if (chr && chr->label) {
 | 
			
		||||
        return g_strdup(chr->label);
 | 
			
		||||
    if (s->chr && s->chr->label) {
 | 
			
		||||
        return g_strdup(s->chr->label);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return NULL;
 | 
			
		||||
@@ -145,8 +189,13 @@ static void rng_egd_finalize(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    RngEgd *s = RNG_EGD(obj);
 | 
			
		||||
 | 
			
		||||
    qemu_chr_fe_deinit(&s->chr, false);
 | 
			
		||||
    if (s->chr) {
 | 
			
		||||
        qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free(s->chr_name);
 | 
			
		||||
 | 
			
		||||
    rng_egd_free_requests(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_egd_class_init(ObjectClass *klass, void *data)
 | 
			
		||||
@@ -154,6 +203,7 @@ static void rng_egd_class_init(ObjectClass *klass, void *data)
 | 
			
		||||
    RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
 | 
			
		||||
 | 
			
		||||
    rbc->request_entropy = rng_egd_request_entropy;
 | 
			
		||||
    rbc->cancel_requests = rng_egd_cancel_requests;
 | 
			
		||||
    rbc->opened = rng_egd_opened;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,19 +10,21 @@
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/rng-random.h"
 | 
			
		||||
#include "sysemu/rng.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu/rng-random.h"
 | 
			
		||||
#include "qemu/rng.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/main-loop.h"
 | 
			
		||||
 | 
			
		||||
struct RngRandom
 | 
			
		||||
struct RndRandom
 | 
			
		||||
{
 | 
			
		||||
    RngBackend parent;
 | 
			
		||||
 | 
			
		||||
    int fd;
 | 
			
		||||
    char *filename;
 | 
			
		||||
 | 
			
		||||
    EntropyReceiveFunc *receive_func;
 | 
			
		||||
    void *opaque;
 | 
			
		||||
    size_t size;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -34,78 +36,87 @@ struct RngRandom
 | 
			
		||||
 | 
			
		||||
static void entropy_available(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(opaque);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(opaque);
 | 
			
		||||
    uint8_t buffer[s->size];
 | 
			
		||||
    ssize_t len;
 | 
			
		||||
 | 
			
		||||
    while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
 | 
			
		||||
        RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
 | 
			
		||||
        ssize_t len;
 | 
			
		||||
 | 
			
		||||
        len = read(s->fd, req->data, req->size);
 | 
			
		||||
        if (len < 0 && errno == EAGAIN) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        g_assert(len != -1);
 | 
			
		||||
 | 
			
		||||
        req->receive_entropy(req->opaque, req->data, len);
 | 
			
		||||
 | 
			
		||||
        rng_backend_finalize_request(&s->parent, req);
 | 
			
		||||
    len = read(s->fd, buffer, s->size);
 | 
			
		||||
    if (len < 0 && errno == EAGAIN) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    g_assert(len != -1);
 | 
			
		||||
 | 
			
		||||
    s->receive_func(s->opaque, buffer, len);
 | 
			
		||||
    s->receive_func = NULL;
 | 
			
		||||
 | 
			
		||||
    /* We've drained all requests, the fd handler can be reset. */
 | 
			
		||||
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
 | 
			
		||||
static void rng_random_request_entropy(RngBackend *b, size_t size,
 | 
			
		||||
                                        EntropyReceiveFunc *receive_entropy,
 | 
			
		||||
                                        void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(b);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(b);
 | 
			
		||||
 | 
			
		||||
    if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
 | 
			
		||||
        /* If there are no pending requests yet, we need to
 | 
			
		||||
         * install our fd handler. */
 | 
			
		||||
        qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
 | 
			
		||||
    if (s->receive_func) {
 | 
			
		||||
        s->receive_func(s->opaque, NULL, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->receive_func = receive_entropy;
 | 
			
		||||
    s->opaque = opaque;
 | 
			
		||||
    s->size = size;
 | 
			
		||||
 | 
			
		||||
    qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_random_opened(RngBackend *b, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(b);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(b);
 | 
			
		||||
 | 
			
		||||
    if (s->filename == NULL) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                   "filename", "a valid filename");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
			
		||||
                  "filename", "a valid filename");
 | 
			
		||||
    } else {
 | 
			
		||||
        s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
 | 
			
		||||
 | 
			
		||||
        if (s->fd == -1) {
 | 
			
		||||
            error_setg_file_open(errp, errno, s->filename);
 | 
			
		||||
            error_set(errp, QERR_OPEN_FILE_FAILED, s->filename);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static char *rng_random_get_filename(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
 | 
			
		||||
    return g_strdup(s->filename);
 | 
			
		||||
    if (s->filename) {
 | 
			
		||||
        return g_strdup(s->filename);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_random_set_filename(Object *obj, const char *filename,
 | 
			
		||||
                                 Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    RngBackend *b = RNG_BACKEND(obj);
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
 | 
			
		||||
    if (b->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free(s->filename);
 | 
			
		||||
    if (s->filename) {
 | 
			
		||||
        g_free(s->filename);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->filename = g_strdup(filename);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_random_init(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
 | 
			
		||||
    object_property_add_str(obj, "filename",
 | 
			
		||||
                            rng_random_get_filename,
 | 
			
		||||
@@ -113,15 +124,15 @@ static void rng_random_init(Object *obj)
 | 
			
		||||
                            NULL);
 | 
			
		||||
 | 
			
		||||
    s->filename = g_strdup("/dev/random");
 | 
			
		||||
    s->fd = -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_random_finalize(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    RngRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
    RndRandom *s = RNG_RANDOM(obj);
 | 
			
		||||
 | 
			
		||||
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 | 
			
		||||
 | 
			
		||||
    if (s->fd != -1) {
 | 
			
		||||
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
 | 
			
		||||
        qemu_close(s->fd);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -139,7 +150,7 @@ static void rng_random_class_init(ObjectClass *klass, void *data)
 | 
			
		||||
static const TypeInfo rng_random_info = {
 | 
			
		||||
    .name = TYPE_RNG_RANDOM,
 | 
			
		||||
    .parent = TYPE_RNG_BACKEND,
 | 
			
		||||
    .instance_size = sizeof(RngRandom),
 | 
			
		||||
    .instance_size = sizeof(RndRandom),
 | 
			
		||||
    .class_init = rng_random_class_init,
 | 
			
		||||
    .instance_init = rng_random_init,
 | 
			
		||||
    .instance_finalize = rng_random_finalize,
 | 
			
		||||
 
 | 
			
		||||
@@ -10,31 +10,26 @@
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/rng.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu/rng.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qom/object_interfaces.h"
 | 
			
		||||
 | 
			
		||||
void rng_backend_request_entropy(RngBackend *s, size_t size,
 | 
			
		||||
                                 EntropyReceiveFunc *receive_entropy,
 | 
			
		||||
                                 void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
 | 
			
		||||
    RngRequest *req;
 | 
			
		||||
 | 
			
		||||
    if (k->request_entropy) {
 | 
			
		||||
        req = g_malloc(sizeof(*req));
 | 
			
		||||
        k->request_entropy(s, size, receive_entropy, opaque);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
        req->offset = 0;
 | 
			
		||||
        req->size = size;
 | 
			
		||||
        req->receive_entropy = receive_entropy;
 | 
			
		||||
        req->opaque = opaque;
 | 
			
		||||
        req->data = g_malloc(req->size);
 | 
			
		||||
void rng_backend_cancel_requests(RngBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
        k->request_entropy(s, req);
 | 
			
		||||
 | 
			
		||||
        QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
 | 
			
		||||
    if (k->cancel_requests) {
 | 
			
		||||
        k->cancel_requests(s);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -45,99 +40,49 @@ static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
 | 
			
		||||
    return s->opened;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_complete(UserCreatable *uc, Error **errp)
 | 
			
		||||
void rng_backend_open(RngBackend *s, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    object_property_set_bool(OBJECT(uc), true, "opened", errp);
 | 
			
		||||
    object_property_set_bool(OBJECT(s), true, "opened", errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_prop_set_opened(Object *obj, bool value, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    RngBackend *s = RNG_BACKEND(obj);
 | 
			
		||||
    RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
 | 
			
		||||
    if (value == s->opened) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!value && s->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        error_set(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (k->opened) {
 | 
			
		||||
        k->opened(s, &local_err);
 | 
			
		||||
        if (local_err) {
 | 
			
		||||
            error_propagate(errp, local_err);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        k->opened(s, errp);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->opened = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_free_request(RngRequest *req)
 | 
			
		||||
{
 | 
			
		||||
    g_free(req->data);
 | 
			
		||||
    g_free(req);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_free_requests(RngBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    RngRequest *req, *next;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
 | 
			
		||||
        rng_backend_free_request(req);
 | 
			
		||||
    if (!error_is_set(errp)) {
 | 
			
		||||
        s->opened = value;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_INIT(&s->requests);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
 | 
			
		||||
{
 | 
			
		||||
    QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
 | 
			
		||||
    rng_backend_free_request(req);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_init(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    RngBackend *s = RNG_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_INIT(&s->requests);
 | 
			
		||||
 | 
			
		||||
    object_property_add_bool(obj, "opened",
 | 
			
		||||
                             rng_backend_prop_get_opened,
 | 
			
		||||
                             rng_backend_prop_set_opened,
 | 
			
		||||
                             NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_finalize(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    RngBackend *s = RNG_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    rng_backend_free_requests(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void rng_backend_class_init(ObjectClass *oc, void *data)
 | 
			
		||||
{
 | 
			
		||||
    UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
 | 
			
		||||
 | 
			
		||||
    ucc->complete = rng_backend_complete;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo rng_backend_info = {
 | 
			
		||||
    .name = TYPE_RNG_BACKEND,
 | 
			
		||||
    .parent = TYPE_OBJECT,
 | 
			
		||||
    .instance_size = sizeof(RngBackend),
 | 
			
		||||
    .instance_init = rng_backend_init,
 | 
			
		||||
    .instance_finalize = rng_backend_finalize,
 | 
			
		||||
    .class_size = sizeof(RngBackendClass),
 | 
			
		||||
    .class_init = rng_backend_class_init,
 | 
			
		||||
    .abstract = true,
 | 
			
		||||
    .interfaces = (InterfaceInfo[]) {
 | 
			
		||||
        { TYPE_USER_CREATABLE },
 | 
			
		||||
        { }
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void register_types(void)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										198
									
								
								backends/tpm.c
									
									
									
									
									
								
							
							
						
						
									
										198
									
								
								backends/tpm.c
									
									
									
									
									
								
							@@ -1,198 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU TPM Backend
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright IBM, Corp. 2013
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *  Stefan Berger   <stefanb@us.ibm.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 * Based on backends/rng.c by Anthony Liguori
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "sysemu/tpm_backend.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "sysemu/tpm.h"
 | 
			
		||||
#include "qemu/thread.h"
 | 
			
		||||
#include "sysemu/tpm_backend_int.h"
 | 
			
		||||
 | 
			
		||||
enum TpmType tpm_backend_get_type(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->type;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *tpm_backend_get_desc(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->desc();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_destroy(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    k->ops->destroy(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int tpm_backend_init(TPMBackend *s, TPMState *state,
 | 
			
		||||
                     TPMRecvDataCB *datacb)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->init(s, state, datacb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int tpm_backend_startup_tpm(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->startup_tpm(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool tpm_backend_had_startup_error(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->had_startup_error(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
size_t tpm_backend_realloc_buffer(TPMBackend *s, TPMSizedBuffer *sb)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->realloc_buffer(sb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_deliver_request(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    k->ops->deliver_request(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_reset(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    k->ops->reset(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_cancel_cmd(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    k->ops->cancel_cmd(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool tpm_backend_get_tpm_established_flag(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->get_tpm_established_flag(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int tpm_backend_reset_tpm_established_flag(TPMBackend *s, uint8_t locty)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->reset_tpm_established_flag(s, locty);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TPMVersion tpm_backend_get_tpm_version(TPMBackend *s)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
 | 
			
		||||
    return k->ops->get_tpm_version(s);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool tpm_backend_prop_get_opened(Object *obj, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackend *s = TPM_BACKEND(obj);
 | 
			
		||||
 | 
			
		||||
    return s->opened;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_open(TPMBackend *s, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    object_property_set_bool(OBJECT(s), true, "opened", errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void tpm_backend_prop_set_opened(Object *obj, bool value, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    TPMBackend *s = TPM_BACKEND(obj);
 | 
			
		||||
    TPMBackendClass *k = TPM_BACKEND_GET_CLASS(s);
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
 | 
			
		||||
    if (value == s->opened) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!value && s->opened) {
 | 
			
		||||
        error_setg(errp, QERR_PERMISSION_DENIED);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (k->opened) {
 | 
			
		||||
        k->opened(s, &local_err);
 | 
			
		||||
        if (local_err) {
 | 
			
		||||
            error_propagate(errp, local_err);
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->opened = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void tpm_backend_instance_init(Object *obj)
 | 
			
		||||
{
 | 
			
		||||
    object_property_add_bool(obj, "opened",
 | 
			
		||||
                             tpm_backend_prop_get_opened,
 | 
			
		||||
                             tpm_backend_prop_set_opened,
 | 
			
		||||
                             NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_thread_deliver_request(TPMBackendThread *tbt)
 | 
			
		||||
{
 | 
			
		||||
   g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_PROCESS_CMD, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_thread_create(TPMBackendThread *tbt,
 | 
			
		||||
                               GFunc func, gpointer user_data)
 | 
			
		||||
{
 | 
			
		||||
    if (!tbt->pool) {
 | 
			
		||||
        tbt->pool = g_thread_pool_new(func, user_data, 1, TRUE, NULL);
 | 
			
		||||
        g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_INIT, NULL);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void tpm_backend_thread_end(TPMBackendThread *tbt)
 | 
			
		||||
{
 | 
			
		||||
    if (tbt->pool) {
 | 
			
		||||
        g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_END, NULL);
 | 
			
		||||
        g_thread_pool_free(tbt->pool, FALSE, TRUE);
 | 
			
		||||
        tbt->pool = NULL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const TypeInfo tpm_backend_info = {
 | 
			
		||||
    .name = TYPE_TPM_BACKEND,
 | 
			
		||||
    .parent = TYPE_OBJECT,
 | 
			
		||||
    .instance_size = sizeof(TPMBackend),
 | 
			
		||||
    .instance_init = tpm_backend_instance_init,
 | 
			
		||||
    .class_size = sizeof(TPMBackendClass),
 | 
			
		||||
    .abstract = true,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void register_types(void)
 | 
			
		||||
{
 | 
			
		||||
    type_register_static(&tpm_backend_info);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type_init(register_types);
 | 
			
		||||
							
								
								
									
										92
									
								
								balloon.c
									
									
									
									
									
								
							
							
						
						
									
										92
									
								
								balloon.c
									
									
									
									
									
								
							@@ -24,45 +24,17 @@
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "monitor/monitor.h"
 | 
			
		||||
#include "exec/cpu-common.h"
 | 
			
		||||
#include "sysemu/kvm.h"
 | 
			
		||||
#include "sysemu/balloon.h"
 | 
			
		||||
#include "trace-root.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "qmp-commands.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qapi/qmp/qjson.h"
 | 
			
		||||
 | 
			
		||||
static QEMUBalloonEvent *balloon_event_fn;
 | 
			
		||||
static QEMUBalloonStatus *balloon_stat_fn;
 | 
			
		||||
static void *balloon_opaque;
 | 
			
		||||
static bool balloon_inhibited;
 | 
			
		||||
 | 
			
		||||
bool qemu_balloon_is_inhibited(void)
 | 
			
		||||
{
 | 
			
		||||
    return balloon_inhibited;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qemu_balloon_inhibit(bool state)
 | 
			
		||||
{
 | 
			
		||||
    balloon_inhibited = state;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool have_balloon(Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    if (kvm_enabled() && !kvm_has_sync_mmu()) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_KVM_MISSING_CAP,
 | 
			
		||||
                  "Using KVM without synchronous MMU, balloon unavailable");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (!balloon_event_fn) {
 | 
			
		||||
        error_set(errp, ERROR_CLASS_DEVICE_NOT_ACTIVE,
 | 
			
		||||
                  "No balloon device has been activated");
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
 | 
			
		||||
                             QEMUBalloonStatus *stat_func, void *opaque)
 | 
			
		||||
@@ -71,6 +43,7 @@ int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
 | 
			
		||||
        /* We're already registered one balloon handler.  How many can
 | 
			
		||||
         * a guest really have?
 | 
			
		||||
         */
 | 
			
		||||
        error_report("Another balloon device already registered");
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
    balloon_event_fn = event_func;
 | 
			
		||||
@@ -89,30 +62,71 @@ void qemu_remove_balloon_handler(void *opaque)
 | 
			
		||||
    balloon_opaque = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qemu_balloon(ram_addr_t target)
 | 
			
		||||
{
 | 
			
		||||
    if (!balloon_event_fn) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    trace_balloon_event(balloon_opaque, target);
 | 
			
		||||
    balloon_event_fn(balloon_opaque, target);
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int qemu_balloon_status(BalloonInfo *info)
 | 
			
		||||
{
 | 
			
		||||
    if (!balloon_stat_fn) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
    balloon_stat_fn(balloon_opaque, info);
 | 
			
		||||
    return 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qemu_balloon_changed(int64_t actual)
 | 
			
		||||
{
 | 
			
		||||
    QObject *data;
 | 
			
		||||
 | 
			
		||||
    data = qobject_from_jsonf("{ 'actual': %" PRId64 " }",
 | 
			
		||||
                              actual);
 | 
			
		||||
 | 
			
		||||
    monitor_protocol_event(QEVENT_BALLOON_CHANGE, data);
 | 
			
		||||
 | 
			
		||||
    qobject_decref(data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
BalloonInfo *qmp_query_balloon(Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BalloonInfo *info;
 | 
			
		||||
 | 
			
		||||
    if (!have_balloon(errp)) {
 | 
			
		||||
    if (kvm_enabled() && !kvm_has_sync_mmu()) {
 | 
			
		||||
        error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    info = g_malloc0(sizeof(*info));
 | 
			
		||||
    balloon_stat_fn(balloon_opaque, info);
 | 
			
		||||
 | 
			
		||||
    if (qemu_balloon_status(info) == 0) {
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
 | 
			
		||||
        qapi_free_BalloonInfo(info);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return info;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void qmp_balloon(int64_t target, Error **errp)
 | 
			
		||||
void qmp_balloon(int64_t value, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    if (!have_balloon(errp)) {
 | 
			
		||||
    if (kvm_enabled() && !kvm_has_sync_mmu()) {
 | 
			
		||||
        error_set(errp, QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (target <= 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
 | 
			
		||||
    if (value <= 0) {
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    trace_balloon_event(balloon_opaque, target);
 | 
			
		||||
    balloon_event_fn(balloon_opaque, target);
 | 
			
		||||
    
 | 
			
		||||
    if (qemu_balloon(value) == 0) {
 | 
			
		||||
        error_set(errp, QERR_DEVICE_NOT_ACTIVE, "balloon");
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										751
									
								
								block-migration.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										751
									
								
								block-migration.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,751 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU live block migration
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright IBM, Corp. 2009
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *  Liran Schour   <lirans@il.ibm.com>
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2.  See
 | 
			
		||||
 * the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 * Contributions after 2012-01-13 are licensed under the terms of the
 | 
			
		||||
 * GNU GPL, version 2 or (at your option) any later version.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "hw/hw.h"
 | 
			
		||||
#include "qemu/queue.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "migration/block.h"
 | 
			
		||||
#include "migration/migration.h"
 | 
			
		||||
#include "sysemu/blockdev.h"
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
 | 
			
		||||
#define BLOCK_SIZE                       (1 << 20)
 | 
			
		||||
#define BDRV_SECTORS_PER_DIRTY_CHUNK     (BLOCK_SIZE >> BDRV_SECTOR_BITS)
 | 
			
		||||
 | 
			
		||||
#define BLK_MIG_FLAG_DEVICE_BLOCK       0x01
 | 
			
		||||
#define BLK_MIG_FLAG_EOS                0x02
 | 
			
		||||
#define BLK_MIG_FLAG_PROGRESS           0x04
 | 
			
		||||
 | 
			
		||||
#define MAX_IS_ALLOCATED_SEARCH 65536
 | 
			
		||||
 | 
			
		||||
//#define DEBUG_BLK_MIGRATION
 | 
			
		||||
 | 
			
		||||
#ifdef DEBUG_BLK_MIGRATION
 | 
			
		||||
#define DPRINTF(fmt, ...) \
 | 
			
		||||
    do { printf("blk_migration: " fmt, ## __VA_ARGS__); } while (0)
 | 
			
		||||
#else
 | 
			
		||||
#define DPRINTF(fmt, ...) \
 | 
			
		||||
    do { } while (0)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
typedef struct BlkMigDevState {
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
    int bulk_completed;
 | 
			
		||||
    int shared_base;
 | 
			
		||||
    int64_t cur_sector;
 | 
			
		||||
    int64_t cur_dirty;
 | 
			
		||||
    int64_t completed_sectors;
 | 
			
		||||
    int64_t total_sectors;
 | 
			
		||||
    int64_t dirty;
 | 
			
		||||
    QSIMPLEQ_ENTRY(BlkMigDevState) entry;
 | 
			
		||||
    unsigned long *aio_bitmap;
 | 
			
		||||
} BlkMigDevState;
 | 
			
		||||
 | 
			
		||||
typedef struct BlkMigBlock {
 | 
			
		||||
    uint8_t *buf;
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    int64_t sector;
 | 
			
		||||
    int nr_sectors;
 | 
			
		||||
    struct iovec iov;
 | 
			
		||||
    QEMUIOVector qiov;
 | 
			
		||||
    BlockDriverAIOCB *aiocb;
 | 
			
		||||
    int ret;
 | 
			
		||||
    QSIMPLEQ_ENTRY(BlkMigBlock) entry;
 | 
			
		||||
} BlkMigBlock;
 | 
			
		||||
 | 
			
		||||
typedef struct BlkMigState {
 | 
			
		||||
    int blk_enable;
 | 
			
		||||
    int shared_base;
 | 
			
		||||
    QSIMPLEQ_HEAD(bmds_list, BlkMigDevState) bmds_list;
 | 
			
		||||
    QSIMPLEQ_HEAD(blk_list, BlkMigBlock) blk_list;
 | 
			
		||||
    int submitted;
 | 
			
		||||
    int read_done;
 | 
			
		||||
    int transferred;
 | 
			
		||||
    int64_t total_sector_sum;
 | 
			
		||||
    int prev_progress;
 | 
			
		||||
    int bulk_completed;
 | 
			
		||||
    long double prev_time_offset;
 | 
			
		||||
} BlkMigState;
 | 
			
		||||
 | 
			
		||||
static BlkMigState block_mig_state;
 | 
			
		||||
 | 
			
		||||
static void blk_send(QEMUFile *f, BlkMigBlock * blk)
 | 
			
		||||
{
 | 
			
		||||
    int len;
 | 
			
		||||
 | 
			
		||||
    /* sector number and flags */
 | 
			
		||||
    qemu_put_be64(f, (blk->sector << BDRV_SECTOR_BITS)
 | 
			
		||||
                     | BLK_MIG_FLAG_DEVICE_BLOCK);
 | 
			
		||||
 | 
			
		||||
    /* device name */
 | 
			
		||||
    len = strlen(blk->bmds->bs->device_name);
 | 
			
		||||
    qemu_put_byte(f, len);
 | 
			
		||||
    qemu_put_buffer(f, (uint8_t *)blk->bmds->bs->device_name, len);
 | 
			
		||||
 | 
			
		||||
    qemu_put_buffer(f, blk->buf, BLOCK_SIZE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int blk_mig_active(void)
 | 
			
		||||
{
 | 
			
		||||
    return !QSIMPLEQ_EMPTY(&block_mig_state.bmds_list);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t blk_mig_bytes_transferred(void)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    uint64_t sum = 0;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        sum += bmds->completed_sectors;
 | 
			
		||||
    }
 | 
			
		||||
    return sum << BDRV_SECTOR_BITS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t blk_mig_bytes_remaining(void)
 | 
			
		||||
{
 | 
			
		||||
    return blk_mig_bytes_total() - blk_mig_bytes_transferred();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint64_t blk_mig_bytes_total(void)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    uint64_t sum = 0;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        sum += bmds->total_sectors;
 | 
			
		||||
    }
 | 
			
		||||
    return sum << BDRV_SECTOR_BITS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int bmds_aio_inflight(BlkMigDevState *bmds, int64_t sector)
 | 
			
		||||
{
 | 
			
		||||
    int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
 | 
			
		||||
    if ((sector << BDRV_SECTOR_BITS) < bdrv_getlength(bmds->bs)) {
 | 
			
		||||
        return !!(bmds->aio_bitmap[chunk / (sizeof(unsigned long) * 8)] &
 | 
			
		||||
            (1UL << (chunk % (sizeof(unsigned long) * 8))));
 | 
			
		||||
    } else {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void bmds_set_aio_inflight(BlkMigDevState *bmds, int64_t sector_num,
 | 
			
		||||
                             int nb_sectors, int set)
 | 
			
		||||
{
 | 
			
		||||
    int64_t start, end;
 | 
			
		||||
    unsigned long val, idx, bit;
 | 
			
		||||
 | 
			
		||||
    start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
    end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
 | 
			
		||||
    for (; start <= end; start++) {
 | 
			
		||||
        idx = start / (sizeof(unsigned long) * 8);
 | 
			
		||||
        bit = start % (sizeof(unsigned long) * 8);
 | 
			
		||||
        val = bmds->aio_bitmap[idx];
 | 
			
		||||
        if (set) {
 | 
			
		||||
            val |= 1UL << bit;
 | 
			
		||||
        } else {
 | 
			
		||||
            val &= ~(1UL << bit);
 | 
			
		||||
        }
 | 
			
		||||
        bmds->aio_bitmap[idx] = val;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void alloc_aio_bitmap(BlkMigDevState *bmds)
 | 
			
		||||
{
 | 
			
		||||
    BlockDriverState *bs = bmds->bs;
 | 
			
		||||
    int64_t bitmap_size;
 | 
			
		||||
 | 
			
		||||
    bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
 | 
			
		||||
            BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
 | 
			
		||||
    bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
 | 
			
		||||
 | 
			
		||||
    bmds->aio_bitmap = g_malloc0(bitmap_size);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blk_mig_read_cb(void *opaque, int ret)
 | 
			
		||||
{
 | 
			
		||||
    long double curr_time = qemu_get_clock_ns(rt_clock);
 | 
			
		||||
    BlkMigBlock *blk = opaque;
 | 
			
		||||
 | 
			
		||||
    blk->ret = ret;
 | 
			
		||||
 | 
			
		||||
    block_mig_state.prev_time_offset = curr_time;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_INSERT_TAIL(&block_mig_state.blk_list, blk, entry);
 | 
			
		||||
    bmds_set_aio_inflight(blk->bmds, blk->sector, blk->nr_sectors, 0);
 | 
			
		||||
 | 
			
		||||
    block_mig_state.submitted--;
 | 
			
		||||
    block_mig_state.read_done++;
 | 
			
		||||
    assert(block_mig_state.submitted >= 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int mig_save_device_bulk(QEMUFile *f, BlkMigDevState *bmds)
 | 
			
		||||
{
 | 
			
		||||
    int64_t total_sectors = bmds->total_sectors;
 | 
			
		||||
    int64_t cur_sector = bmds->cur_sector;
 | 
			
		||||
    BlockDriverState *bs = bmds->bs;
 | 
			
		||||
    BlkMigBlock *blk;
 | 
			
		||||
    int nr_sectors;
 | 
			
		||||
 | 
			
		||||
    if (bmds->shared_base) {
 | 
			
		||||
        while (cur_sector < total_sectors &&
 | 
			
		||||
               !bdrv_is_allocated(bs, cur_sector, MAX_IS_ALLOCATED_SEARCH,
 | 
			
		||||
                                  &nr_sectors)) {
 | 
			
		||||
            cur_sector += nr_sectors;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (cur_sector >= total_sectors) {
 | 
			
		||||
        bmds->cur_sector = bmds->completed_sectors = total_sectors;
 | 
			
		||||
        return 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bmds->completed_sectors = cur_sector;
 | 
			
		||||
 | 
			
		||||
    cur_sector &= ~((int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK - 1);
 | 
			
		||||
 | 
			
		||||
    /* we are going to transfer a full block even if it is not allocated */
 | 
			
		||||
    nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
 | 
			
		||||
    if (total_sectors - cur_sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
 | 
			
		||||
        nr_sectors = total_sectors - cur_sector;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    blk = g_malloc(sizeof(BlkMigBlock));
 | 
			
		||||
    blk->buf = g_malloc(BLOCK_SIZE);
 | 
			
		||||
    blk->bmds = bmds;
 | 
			
		||||
    blk->sector = cur_sector;
 | 
			
		||||
    blk->nr_sectors = nr_sectors;
 | 
			
		||||
 | 
			
		||||
    blk->iov.iov_base = blk->buf;
 | 
			
		||||
    blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
 | 
			
		||||
    qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
 | 
			
		||||
 | 
			
		||||
    if (block_mig_state.submitted == 0) {
 | 
			
		||||
        block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    blk->aiocb = bdrv_aio_readv(bs, cur_sector, &blk->qiov,
 | 
			
		||||
                                nr_sectors, blk_mig_read_cb, blk);
 | 
			
		||||
    block_mig_state.submitted++;
 | 
			
		||||
 | 
			
		||||
    bdrv_reset_dirty(bs, cur_sector, nr_sectors);
 | 
			
		||||
    bmds->cur_sector = cur_sector + nr_sectors;
 | 
			
		||||
 | 
			
		||||
    return (bmds->cur_sector >= total_sectors);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void set_dirty_tracking(int enable)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        bdrv_set_dirty_tracking(bmds->bs, enable ? BLOCK_SIZE : 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    int64_t sectors;
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_read_only(bs)) {
 | 
			
		||||
        sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
 | 
			
		||||
        if (sectors <= 0) {
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        bmds = g_malloc0(sizeof(BlkMigDevState));
 | 
			
		||||
        bmds->bs = bs;
 | 
			
		||||
        bmds->bulk_completed = 0;
 | 
			
		||||
        bmds->total_sectors = sectors;
 | 
			
		||||
        bmds->completed_sectors = 0;
 | 
			
		||||
        bmds->shared_base = block_mig_state.shared_base;
 | 
			
		||||
        alloc_aio_bitmap(bmds);
 | 
			
		||||
        drive_get_ref(drive_get_by_blockdev(bs));
 | 
			
		||||
        bdrv_set_in_use(bs, 1);
 | 
			
		||||
 | 
			
		||||
        block_mig_state.total_sector_sum += sectors;
 | 
			
		||||
 | 
			
		||||
        if (bmds->shared_base) {
 | 
			
		||||
            DPRINTF("Start migration for %s with shared base image\n",
 | 
			
		||||
                    bs->device_name);
 | 
			
		||||
        } else {
 | 
			
		||||
            DPRINTF("Start full migration for %s\n", bs->device_name);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void init_blk_migration(QEMUFile *f)
 | 
			
		||||
{
 | 
			
		||||
    block_mig_state.submitted = 0;
 | 
			
		||||
    block_mig_state.read_done = 0;
 | 
			
		||||
    block_mig_state.transferred = 0;
 | 
			
		||||
    block_mig_state.total_sector_sum = 0;
 | 
			
		||||
    block_mig_state.prev_progress = -1;
 | 
			
		||||
    block_mig_state.bulk_completed = 0;
 | 
			
		||||
 | 
			
		||||
    bdrv_iterate(init_blk_migration_it, NULL);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blk_mig_save_bulked_block(QEMUFile *f)
 | 
			
		||||
{
 | 
			
		||||
    int64_t completed_sector_sum = 0;
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    int progress;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        if (bmds->bulk_completed == 0) {
 | 
			
		||||
            if (mig_save_device_bulk(f, bmds) == 1) {
 | 
			
		||||
                /* completed bulk section for this device */
 | 
			
		||||
                bmds->bulk_completed = 1;
 | 
			
		||||
            }
 | 
			
		||||
            completed_sector_sum += bmds->completed_sectors;
 | 
			
		||||
            ret = 1;
 | 
			
		||||
            break;
 | 
			
		||||
        } else {
 | 
			
		||||
            completed_sector_sum += bmds->completed_sectors;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (block_mig_state.total_sector_sum != 0) {
 | 
			
		||||
        progress = completed_sector_sum * 100 /
 | 
			
		||||
                   block_mig_state.total_sector_sum;
 | 
			
		||||
    } else {
 | 
			
		||||
        progress = 100;
 | 
			
		||||
    }
 | 
			
		||||
    if (progress != block_mig_state.prev_progress) {
 | 
			
		||||
        block_mig_state.prev_progress = progress;
 | 
			
		||||
        qemu_put_be64(f, (progress << BDRV_SECTOR_BITS)
 | 
			
		||||
                         | BLK_MIG_FLAG_PROGRESS);
 | 
			
		||||
        DPRINTF("Completed %d %%\r", progress);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blk_mig_reset_dirty_cursor(void)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        bmds->cur_dirty = 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int mig_save_device_dirty(QEMUFile *f, BlkMigDevState *bmds,
 | 
			
		||||
                                 int is_async)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigBlock *blk;
 | 
			
		||||
    int64_t total_sectors = bmds->total_sectors;
 | 
			
		||||
    int64_t sector;
 | 
			
		||||
    int nr_sectors;
 | 
			
		||||
    int ret = -EIO;
 | 
			
		||||
 | 
			
		||||
    for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
 | 
			
		||||
        if (bmds_aio_inflight(bmds, sector)) {
 | 
			
		||||
            bdrv_drain_all();
 | 
			
		||||
        }
 | 
			
		||||
        if (bdrv_get_dirty(bmds->bs, sector)) {
 | 
			
		||||
 | 
			
		||||
            if (total_sectors - sector < BDRV_SECTORS_PER_DIRTY_CHUNK) {
 | 
			
		||||
                nr_sectors = total_sectors - sector;
 | 
			
		||||
            } else {
 | 
			
		||||
                nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
            }
 | 
			
		||||
            blk = g_malloc(sizeof(BlkMigBlock));
 | 
			
		||||
            blk->buf = g_malloc(BLOCK_SIZE);
 | 
			
		||||
            blk->bmds = bmds;
 | 
			
		||||
            blk->sector = sector;
 | 
			
		||||
            blk->nr_sectors = nr_sectors;
 | 
			
		||||
 | 
			
		||||
            if (is_async) {
 | 
			
		||||
                blk->iov.iov_base = blk->buf;
 | 
			
		||||
                blk->iov.iov_len = nr_sectors * BDRV_SECTOR_SIZE;
 | 
			
		||||
                qemu_iovec_init_external(&blk->qiov, &blk->iov, 1);
 | 
			
		||||
 | 
			
		||||
                if (block_mig_state.submitted == 0) {
 | 
			
		||||
                    block_mig_state.prev_time_offset = qemu_get_clock_ns(rt_clock);
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                blk->aiocb = bdrv_aio_readv(bmds->bs, sector, &blk->qiov,
 | 
			
		||||
                                            nr_sectors, blk_mig_read_cb, blk);
 | 
			
		||||
                block_mig_state.submitted++;
 | 
			
		||||
                bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
 | 
			
		||||
            } else {
 | 
			
		||||
                ret = bdrv_read(bmds->bs, sector, blk->buf, nr_sectors);
 | 
			
		||||
                if (ret < 0) {
 | 
			
		||||
                    goto error;
 | 
			
		||||
                }
 | 
			
		||||
                blk_send(f, blk);
 | 
			
		||||
 | 
			
		||||
                g_free(blk->buf);
 | 
			
		||||
                g_free(blk);
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            bdrv_reset_dirty(bmds->bs, sector, nr_sectors);
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        sector += BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
        bmds->cur_dirty = sector;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return (bmds->cur_dirty >= bmds->total_sectors);
 | 
			
		||||
 | 
			
		||||
error:
 | 
			
		||||
    DPRINTF("Error reading sector %" PRId64 "\n", sector);
 | 
			
		||||
    g_free(blk->buf);
 | 
			
		||||
    g_free(blk);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* return value:
 | 
			
		||||
 * 0: too much data for max_downtime
 | 
			
		||||
 * 1: few enough data for max_downtime
 | 
			
		||||
*/
 | 
			
		||||
static int blk_mig_save_dirty_block(QEMUFile *f, int is_async)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    int ret = 1;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        ret = mig_save_device_dirty(f, bmds, is_async);
 | 
			
		||||
        if (ret <= 0) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int flush_blks(QEMUFile *f)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigBlock *blk;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    DPRINTF("%s Enter submitted %d read_done %d transferred %d\n",
 | 
			
		||||
            __FUNCTION__, block_mig_state.submitted, block_mig_state.read_done,
 | 
			
		||||
            block_mig_state.transferred);
 | 
			
		||||
 | 
			
		||||
    while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
 | 
			
		||||
        if (qemu_file_rate_limit(f)) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        if (blk->ret < 0) {
 | 
			
		||||
            ret = blk->ret;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        blk_send(f, blk);
 | 
			
		||||
 | 
			
		||||
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
 | 
			
		||||
        g_free(blk->buf);
 | 
			
		||||
        g_free(blk);
 | 
			
		||||
 | 
			
		||||
        block_mig_state.read_done--;
 | 
			
		||||
        block_mig_state.transferred++;
 | 
			
		||||
        assert(block_mig_state.read_done >= 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    DPRINTF("%s Exit submitted %d read_done %d transferred %d\n", __FUNCTION__,
 | 
			
		||||
            block_mig_state.submitted, block_mig_state.read_done,
 | 
			
		||||
            block_mig_state.transferred);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t get_remaining_dirty(void)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    int64_t dirty = 0;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
 | 
			
		||||
        dirty += bdrv_get_dirty_count(bmds->bs);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return dirty << BDRV_SECTOR_BITS;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blk_mig_cleanup(void)
 | 
			
		||||
{
 | 
			
		||||
    BlkMigDevState *bmds;
 | 
			
		||||
    BlkMigBlock *blk;
 | 
			
		||||
 | 
			
		||||
    bdrv_drain_all();
 | 
			
		||||
 | 
			
		||||
    set_dirty_tracking(0);
 | 
			
		||||
 | 
			
		||||
    while ((bmds = QSIMPLEQ_FIRST(&block_mig_state.bmds_list)) != NULL) {
 | 
			
		||||
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.bmds_list, entry);
 | 
			
		||||
        bdrv_set_in_use(bmds->bs, 0);
 | 
			
		||||
        drive_put_ref(drive_get_by_blockdev(bmds->bs));
 | 
			
		||||
        g_free(bmds->aio_bitmap);
 | 
			
		||||
        g_free(bmds);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
 | 
			
		||||
        QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
 | 
			
		||||
        g_free(blk->buf);
 | 
			
		||||
        g_free(blk);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void block_migration_cancel(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    blk_mig_cleanup();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int block_save_setup(QEMUFile *f, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    DPRINTF("Enter save live setup submitted %d transferred %d\n",
 | 
			
		||||
            block_mig_state.submitted, block_mig_state.transferred);
 | 
			
		||||
 | 
			
		||||
    init_blk_migration(f);
 | 
			
		||||
 | 
			
		||||
    /* start track dirty blocks */
 | 
			
		||||
    set_dirty_tracking(1);
 | 
			
		||||
 | 
			
		||||
    ret = flush_blks(f);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        blk_mig_cleanup();
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    blk_mig_reset_dirty_cursor();
 | 
			
		||||
 | 
			
		||||
    qemu_put_be64(f, BLK_MIG_FLAG_EOS);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int block_save_iterate(QEMUFile *f, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    int64_t last_ftell = qemu_ftell(f);
 | 
			
		||||
 | 
			
		||||
    DPRINTF("Enter save live iterate submitted %d transferred %d\n",
 | 
			
		||||
            block_mig_state.submitted, block_mig_state.transferred);
 | 
			
		||||
 | 
			
		||||
    ret = flush_blks(f);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        blk_mig_cleanup();
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    blk_mig_reset_dirty_cursor();
 | 
			
		||||
 | 
			
		||||
    /* control the rate of transfer */
 | 
			
		||||
    while ((block_mig_state.submitted +
 | 
			
		||||
            block_mig_state.read_done) * BLOCK_SIZE <
 | 
			
		||||
           qemu_file_get_rate_limit(f)) {
 | 
			
		||||
        if (block_mig_state.bulk_completed == 0) {
 | 
			
		||||
            /* first finish the bulk phase */
 | 
			
		||||
            if (blk_mig_save_bulked_block(f) == 0) {
 | 
			
		||||
                /* finished saving bulk on all devices */
 | 
			
		||||
                block_mig_state.bulk_completed = 1;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            ret = blk_mig_save_dirty_block(f, 1);
 | 
			
		||||
            if (ret != 0) {
 | 
			
		||||
                /* no more dirty blocks */
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        blk_mig_cleanup();
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = flush_blks(f);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        blk_mig_cleanup();
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_put_be64(f, BLK_MIG_FLAG_EOS);
 | 
			
		||||
 | 
			
		||||
    return qemu_ftell(f) - last_ftell;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int block_save_complete(QEMUFile *f, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    DPRINTF("Enter save live complete submitted %d transferred %d\n",
 | 
			
		||||
            block_mig_state.submitted, block_mig_state.transferred);
 | 
			
		||||
 | 
			
		||||
    ret = flush_blks(f);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        blk_mig_cleanup();
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    blk_mig_reset_dirty_cursor();
 | 
			
		||||
 | 
			
		||||
    /* we know for sure that save bulk is completed and
 | 
			
		||||
       all async read completed */
 | 
			
		||||
    assert(block_mig_state.submitted == 0);
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
        ret = blk_mig_save_dirty_block(f, 0);
 | 
			
		||||
    } while (ret == 0);
 | 
			
		||||
 | 
			
		||||
    blk_mig_cleanup();
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    /* report completion */
 | 
			
		||||
    qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
 | 
			
		||||
 | 
			
		||||
    DPRINTF("Block migration completed\n");
 | 
			
		||||
 | 
			
		||||
    qemu_put_be64(f, BLK_MIG_FLAG_EOS);
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static uint64_t block_save_pending(QEMUFile *f, void *opaque, uint64_t max_size)
 | 
			
		||||
{
 | 
			
		||||
    /* Estimate pending number of bytes to send */
 | 
			
		||||
    uint64_t pending = get_remaining_dirty() +
 | 
			
		||||
                       block_mig_state.submitted * BLOCK_SIZE +
 | 
			
		||||
                       block_mig_state.read_done * BLOCK_SIZE;
 | 
			
		||||
 | 
			
		||||
    /* Report at least one block pending during bulk phase */
 | 
			
		||||
    if (pending == 0 && !block_mig_state.bulk_completed) {
 | 
			
		||||
        pending = BLOCK_SIZE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    DPRINTF("Enter save live pending  %" PRIu64 "\n", pending);
 | 
			
		||||
    return pending;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int block_load(QEMUFile *f, void *opaque, int version_id)
 | 
			
		||||
{
 | 
			
		||||
    static int banner_printed;
 | 
			
		||||
    int len, flags;
 | 
			
		||||
    char device_name[256];
 | 
			
		||||
    int64_t addr;
 | 
			
		||||
    BlockDriverState *bs, *bs_prev = NULL;
 | 
			
		||||
    uint8_t *buf;
 | 
			
		||||
    int64_t total_sectors = 0;
 | 
			
		||||
    int nr_sectors;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
        addr = qemu_get_be64(f);
 | 
			
		||||
 | 
			
		||||
        flags = addr & ~BDRV_SECTOR_MASK;
 | 
			
		||||
        addr >>= BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
        if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
 | 
			
		||||
            /* get device name */
 | 
			
		||||
            len = qemu_get_byte(f);
 | 
			
		||||
            qemu_get_buffer(f, (uint8_t *)device_name, len);
 | 
			
		||||
            device_name[len] = '\0';
 | 
			
		||||
 | 
			
		||||
            bs = bdrv_find(device_name);
 | 
			
		||||
            if (!bs) {
 | 
			
		||||
                fprintf(stderr, "Error unknown block device %s\n",
 | 
			
		||||
                        device_name);
 | 
			
		||||
                return -EINVAL;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (bs != bs_prev) {
 | 
			
		||||
                bs_prev = bs;
 | 
			
		||||
                total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
 | 
			
		||||
                if (total_sectors <= 0) {
 | 
			
		||||
                    error_report("Error getting length of block device %s",
 | 
			
		||||
                                 device_name);
 | 
			
		||||
                    return -EINVAL;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (total_sectors - addr < BDRV_SECTORS_PER_DIRTY_CHUNK) {
 | 
			
		||||
                nr_sectors = total_sectors - addr;
 | 
			
		||||
            } else {
 | 
			
		||||
                nr_sectors = BDRV_SECTORS_PER_DIRTY_CHUNK;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            buf = g_malloc(BLOCK_SIZE);
 | 
			
		||||
 | 
			
		||||
            qemu_get_buffer(f, buf, BLOCK_SIZE);
 | 
			
		||||
            ret = bdrv_write(bs, addr, buf, nr_sectors);
 | 
			
		||||
 | 
			
		||||
            g_free(buf);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                return ret;
 | 
			
		||||
            }
 | 
			
		||||
        } else if (flags & BLK_MIG_FLAG_PROGRESS) {
 | 
			
		||||
            if (!banner_printed) {
 | 
			
		||||
                printf("Receiving block device images\n");
 | 
			
		||||
                banner_printed = 1;
 | 
			
		||||
            }
 | 
			
		||||
            printf("Completed %d %%%c", (int)addr,
 | 
			
		||||
                   (addr == 100) ? '\n' : '\r');
 | 
			
		||||
            fflush(stdout);
 | 
			
		||||
        } else if (!(flags & BLK_MIG_FLAG_EOS)) {
 | 
			
		||||
            fprintf(stderr, "Unknown block migration flags: %#x\n", flags);
 | 
			
		||||
            return -EINVAL;
 | 
			
		||||
        }
 | 
			
		||||
        ret = qemu_file_get_error(f);
 | 
			
		||||
        if (ret != 0) {
 | 
			
		||||
            return ret;
 | 
			
		||||
        }
 | 
			
		||||
    } while (!(flags & BLK_MIG_FLAG_EOS));
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void block_set_params(const MigrationParams *params, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    block_mig_state.blk_enable = params->blk;
 | 
			
		||||
    block_mig_state.shared_base = params->shared;
 | 
			
		||||
 | 
			
		||||
    /* shared base means that blk_enable = 1 */
 | 
			
		||||
    block_mig_state.blk_enable |= params->shared;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool block_is_active(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    return block_mig_state.blk_enable == 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SaveVMHandlers savevm_block_handlers = {
 | 
			
		||||
    .set_params = block_set_params,
 | 
			
		||||
    .save_live_setup = block_save_setup,
 | 
			
		||||
    .save_live_iterate = block_save_iterate,
 | 
			
		||||
    .save_live_complete = block_save_complete,
 | 
			
		||||
    .save_live_pending = block_save_pending,
 | 
			
		||||
    .load_state = block_load,
 | 
			
		||||
    .cancel = block_migration_cancel,
 | 
			
		||||
    .is_active = block_is_active,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void blk_mig_init(void)
 | 
			
		||||
{
 | 
			
		||||
    QSIMPLEQ_INIT(&block_mig_state.bmds_list);
 | 
			
		||||
    QSIMPLEQ_INIT(&block_mig_state.blk_list);
 | 
			
		||||
 | 
			
		||||
    register_savevm_live(NULL, "block", 0, 1, &savevm_block_handlers,
 | 
			
		||||
                         &block_mig_state);
 | 
			
		||||
}
 | 
			
		||||
@@ -1,48 +1,24 @@
 | 
			
		||||
block-obj-y += raw-format.o qcow.o vdi.o vmdk.o cloop.o bochs.o vpc.o vvfat.o dmg.o
 | 
			
		||||
block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
 | 
			
		||||
block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
 | 
			
		||||
block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 | 
			
		||||
block-obj-y += qed-check.o
 | 
			
		||||
block-obj-y += vhdx.o vhdx-endian.o vhdx-log.o
 | 
			
		||||
block-obj-y += quorum.o
 | 
			
		||||
block-obj-y += parallels.o blkdebug.o blkverify.o blkreplay.o
 | 
			
		||||
block-obj-y += block-backend.o snapshot.o qapi.o
 | 
			
		||||
block-obj-$(CONFIG_WIN32) += file-win32.o win32-aio.o
 | 
			
		||||
block-obj-$(CONFIG_POSIX) += file-posix.o
 | 
			
		||||
block-obj-y += parallels.o blkdebug.o blkverify.o
 | 
			
		||||
block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 | 
			
		||||
block-obj-$(CONFIG_POSIX) += raw-posix.o
 | 
			
		||||
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 | 
			
		||||
block-obj-y += null.o mirror.o commit.o io.o
 | 
			
		||||
block-obj-y += throttle-groups.o
 | 
			
		||||
 | 
			
		||||
block-obj-y += nbd.o nbd-client.o sheepdog.o
 | 
			
		||||
ifeq ($(CONFIG_POSIX),y)
 | 
			
		||||
block-obj-y += nbd.o sheepdog.o
 | 
			
		||||
block-obj-$(CONFIG_LIBISCSI) += iscsi.o
 | 
			
		||||
block-obj-$(if $(CONFIG_LIBISCSI),y,n) += iscsi-opts.o
 | 
			
		||||
block-obj-$(CONFIG_LIBNFS) += nfs.o
 | 
			
		||||
block-obj-$(CONFIG_CURL) += curl.o
 | 
			
		||||
block-obj-$(CONFIG_RBD) += rbd.o
 | 
			
		||||
block-obj-$(CONFIG_GLUSTERFS) += gluster.o
 | 
			
		||||
block-obj-$(CONFIG_VXHS) += vxhs.o
 | 
			
		||||
block-obj-$(CONFIG_LIBSSH2) += ssh.o
 | 
			
		||||
block-obj-y += accounting.o dirty-bitmap.o
 | 
			
		||||
block-obj-y += write-threshold.o
 | 
			
		||||
block-obj-y += backup.o
 | 
			
		||||
block-obj-$(CONFIG_REPLICATION) += replication.o
 | 
			
		||||
 | 
			
		||||
block-obj-y += crypto.o
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
common-obj-y += stream.o
 | 
			
		||||
common-obj-y += commit.o
 | 
			
		||||
common-obj-y += mirror.o
 | 
			
		||||
block-obj-y += dictzip.o
 | 
			
		||||
block-obj-y += tar.o
 | 
			
		||||
 | 
			
		||||
nfs.o-libs         := $(LIBNFS_LIBS)
 | 
			
		||||
iscsi.o-cflags     := $(LIBISCSI_CFLAGS)
 | 
			
		||||
iscsi.o-libs       := $(LIBISCSI_LIBS)
 | 
			
		||||
curl.o-cflags      := $(CURL_CFLAGS)
 | 
			
		||||
curl.o-libs        := $(CURL_LIBS)
 | 
			
		||||
rbd.o-cflags       := $(RBD_CFLAGS)
 | 
			
		||||
rbd.o-libs         := $(RBD_LIBS)
 | 
			
		||||
gluster.o-cflags   := $(GLUSTERFS_CFLAGS)
 | 
			
		||||
gluster.o-libs     := $(GLUSTERFS_LIBS)
 | 
			
		||||
vxhs.o-libs        := $(VXHS_LIBS)
 | 
			
		||||
ssh.o-cflags       := $(LIBSSH2_CFLAGS)
 | 
			
		||||
ssh.o-libs         := $(LIBSSH2_LIBS)
 | 
			
		||||
block-obj-$(if $(CONFIG_BZIP2),m,n) += dmg-bz2.o
 | 
			
		||||
dmg-bz2.o-libs     := $(BZIP2_LIBS)
 | 
			
		||||
qcow.o-libs        := -lz
 | 
			
		||||
linux-aio.o-libs   := -laio
 | 
			
		||||
$(obj)/curl.o: QEMU_CFLAGS+=$(CURL_CFLAGS)
 | 
			
		||||
 
 | 
			
		||||
@@ -1,185 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU System Emulator block accounting
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2011 Christoph Hellwig
 | 
			
		||||
 * Copyright (c) 2015 Igalia, S.L.
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
 * of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
 * in the Software without restriction, including without limitation the rights
 | 
			
		||||
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
 * copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
 * furnished to do so, subject to the following conditions:
 | 
			
		||||
 *
 | 
			
		||||
 * The above copyright notice and this permission notice shall be included in
 | 
			
		||||
 * all copies or substantial portions of the Software.
 | 
			
		||||
 *
 | 
			
		||||
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 | 
			
		||||
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "block/accounting.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/timer.h"
 | 
			
		||||
#include "sysemu/qtest.h"
 | 
			
		||||
 | 
			
		||||
static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
 | 
			
		||||
static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
 | 
			
		||||
 | 
			
		||||
void block_acct_init(BlockAcctStats *stats)
 | 
			
		||||
{
 | 
			
		||||
    qemu_mutex_init(&stats->lock);
 | 
			
		||||
    if (qtest_enabled()) {
 | 
			
		||||
        clock_type = QEMU_CLOCK_VIRTUAL;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_setup(BlockAcctStats *stats, bool account_invalid,
 | 
			
		||||
                      bool account_failed)
 | 
			
		||||
{
 | 
			
		||||
    stats->account_invalid = account_invalid;
 | 
			
		||||
    stats->account_failed = account_failed;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_cleanup(BlockAcctStats *stats)
 | 
			
		||||
{
 | 
			
		||||
    BlockAcctTimedStats *s, *next;
 | 
			
		||||
    QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
 | 
			
		||||
        g_free(s);
 | 
			
		||||
    }
 | 
			
		||||
    qemu_mutex_destroy(&stats->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
 | 
			
		||||
{
 | 
			
		||||
    BlockAcctTimedStats *s;
 | 
			
		||||
    unsigned i;
 | 
			
		||||
 | 
			
		||||
    s = g_new0(BlockAcctTimedStats, 1);
 | 
			
		||||
    s->interval_length = interval_length;
 | 
			
		||||
    s->stats = stats;
 | 
			
		||||
    qemu_mutex_lock(&stats->lock);
 | 
			
		||||
    QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
 | 
			
		||||
        timed_average_init(&s->latency[i], clock_type,
 | 
			
		||||
                           (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
 | 
			
		||||
    }
 | 
			
		||||
    qemu_mutex_unlock(&stats->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
 | 
			
		||||
                                              BlockAcctTimedStats *s)
 | 
			
		||||
{
 | 
			
		||||
    if (s == NULL) {
 | 
			
		||||
        return QSLIST_FIRST(&stats->intervals);
 | 
			
		||||
    } else {
 | 
			
		||||
        return QSLIST_NEXT(s, entries);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
 | 
			
		||||
                      int64_t bytes, enum BlockAcctType type)
 | 
			
		||||
{
 | 
			
		||||
    assert(type < BLOCK_MAX_IOTYPE);
 | 
			
		||||
 | 
			
		||||
    cookie->bytes = bytes;
 | 
			
		||||
    cookie->start_time_ns = qemu_clock_get_ns(clock_type);
 | 
			
		||||
    cookie->type = type;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
 | 
			
		||||
                                 bool failed)
 | 
			
		||||
{
 | 
			
		||||
    BlockAcctTimedStats *s;
 | 
			
		||||
    int64_t time_ns = qemu_clock_get_ns(clock_type);
 | 
			
		||||
    int64_t latency_ns = time_ns - cookie->start_time_ns;
 | 
			
		||||
 | 
			
		||||
    if (qtest_enabled()) {
 | 
			
		||||
        latency_ns = qtest_latency_ns;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    assert(cookie->type < BLOCK_MAX_IOTYPE);
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_lock(&stats->lock);
 | 
			
		||||
 | 
			
		||||
    if (failed) {
 | 
			
		||||
        stats->failed_ops[cookie->type]++;
 | 
			
		||||
    } else {
 | 
			
		||||
        stats->nr_bytes[cookie->type] += cookie->bytes;
 | 
			
		||||
        stats->nr_ops[cookie->type]++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!failed || stats->account_failed) {
 | 
			
		||||
        stats->total_time_ns[cookie->type] += latency_ns;
 | 
			
		||||
        stats->last_access_time_ns = time_ns;
 | 
			
		||||
 | 
			
		||||
        QSLIST_FOREACH(s, &stats->intervals, entries) {
 | 
			
		||||
            timed_average_account(&s->latency[cookie->type], latency_ns);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_unlock(&stats->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
 | 
			
		||||
{
 | 
			
		||||
    block_account_one_io(stats, cookie, false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
 | 
			
		||||
{
 | 
			
		||||
    block_account_one_io(stats, cookie, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
 | 
			
		||||
{
 | 
			
		||||
    assert(type < BLOCK_MAX_IOTYPE);
 | 
			
		||||
 | 
			
		||||
    /* block_account_one_io() updates total_time_ns[], but this one does
 | 
			
		||||
     * not.  The reason is that invalid requests are accounted during their
 | 
			
		||||
     * submission, therefore there's no actual I/O involved.
 | 
			
		||||
     */
 | 
			
		||||
    qemu_mutex_lock(&stats->lock);
 | 
			
		||||
    stats->invalid_ops[type]++;
 | 
			
		||||
 | 
			
		||||
    if (stats->account_invalid) {
 | 
			
		||||
        stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
 | 
			
		||||
    }
 | 
			
		||||
    qemu_mutex_unlock(&stats->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
 | 
			
		||||
                      int num_requests)
 | 
			
		||||
{
 | 
			
		||||
    assert(type < BLOCK_MAX_IOTYPE);
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_lock(&stats->lock);
 | 
			
		||||
    stats->merged[type] += num_requests;
 | 
			
		||||
    qemu_mutex_unlock(&stats->lock);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
 | 
			
		||||
{
 | 
			
		||||
    return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
double block_acct_queue_depth(BlockAcctTimedStats *stats,
 | 
			
		||||
                              enum BlockAcctType type)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t sum, elapsed;
 | 
			
		||||
 | 
			
		||||
    assert(type < BLOCK_MAX_IOTYPE);
 | 
			
		||||
 | 
			
		||||
    qemu_mutex_lock(&stats->stats->lock);
 | 
			
		||||
    sum = timed_average_sum(&stats->latency[type], &elapsed);
 | 
			
		||||
    qemu_mutex_unlock(&stats->stats->lock);
 | 
			
		||||
 | 
			
		||||
    return (double) sum / elapsed;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										699
									
								
								block/backup.c
									
									
									
									
									
								
							
							
						
						
									
										699
									
								
								block/backup.c
									
									
									
									
									
								
							@@ -1,699 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * QEMU backup
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2013 Proxmox Server Solutions
 | 
			
		||||
 *
 | 
			
		||||
 * Authors:
 | 
			
		||||
 *  Dietmar Maurer (dietmar@proxmox.com)
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "block/block.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/blockjob_int.h"
 | 
			
		||||
#include "block/block_backup.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "qemu/ratelimit.h"
 | 
			
		||||
#include "qemu/cutils.h"
 | 
			
		||||
#include "sysemu/block-backend.h"
 | 
			
		||||
#include "qemu/bitmap.h"
 | 
			
		||||
#include "qemu/error-report.h"
 | 
			
		||||
 | 
			
		||||
#define BACKUP_CLUSTER_SIZE_DEFAULT (1 << 16)
 | 
			
		||||
#define SLICE_TIME 100000000ULL /* ns */
 | 
			
		||||
 | 
			
		||||
typedef struct BackupBlockJob {
 | 
			
		||||
    BlockJob common;
 | 
			
		||||
    BlockBackend *target;
 | 
			
		||||
    /* bitmap for sync=incremental */
 | 
			
		||||
    BdrvDirtyBitmap *sync_bitmap;
 | 
			
		||||
    MirrorSyncMode sync_mode;
 | 
			
		||||
    RateLimit limit;
 | 
			
		||||
    BlockdevOnError on_source_error;
 | 
			
		||||
    BlockdevOnError on_target_error;
 | 
			
		||||
    CoRwlock flush_rwlock;
 | 
			
		||||
    uint64_t sectors_read;
 | 
			
		||||
    unsigned long *done_bitmap;
 | 
			
		||||
    int64_t cluster_size;
 | 
			
		||||
    bool compress;
 | 
			
		||||
    NotifierWithReturn before_write;
 | 
			
		||||
    QLIST_HEAD(, CowRequest) inflight_reqs;
 | 
			
		||||
} BackupBlockJob;
 | 
			
		||||
 | 
			
		||||
/* Size of a cluster in sectors, instead of bytes. */
 | 
			
		||||
static inline int64_t cluster_size_sectors(BackupBlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
  return job->cluster_size / BDRV_SECTOR_SIZE;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* See if in-flight requests overlap and wait for them to complete */
 | 
			
		||||
static void coroutine_fn wait_for_overlapping_requests(BackupBlockJob *job,
 | 
			
		||||
                                                       int64_t start,
 | 
			
		||||
                                                       int64_t end)
 | 
			
		||||
{
 | 
			
		||||
    CowRequest *req;
 | 
			
		||||
    bool retry;
 | 
			
		||||
 | 
			
		||||
    do {
 | 
			
		||||
        retry = false;
 | 
			
		||||
        QLIST_FOREACH(req, &job->inflight_reqs, list) {
 | 
			
		||||
            if (end > req->start && start < req->end) {
 | 
			
		||||
                qemu_co_queue_wait(&req->wait_queue, NULL);
 | 
			
		||||
                retry = true;
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    } while (retry);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Keep track of an in-flight request */
 | 
			
		||||
static void cow_request_begin(CowRequest *req, BackupBlockJob *job,
 | 
			
		||||
                                     int64_t start, int64_t end)
 | 
			
		||||
{
 | 
			
		||||
    req->start = start;
 | 
			
		||||
    req->end = end;
 | 
			
		||||
    qemu_co_queue_init(&req->wait_queue);
 | 
			
		||||
    QLIST_INSERT_HEAD(&job->inflight_reqs, req, list);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Forget about a completed request */
 | 
			
		||||
static void cow_request_end(CowRequest *req)
 | 
			
		||||
{
 | 
			
		||||
    QLIST_REMOVE(req, list);
 | 
			
		||||
    qemu_co_queue_restart_all(&req->wait_queue);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn backup_do_cow(BackupBlockJob *job,
 | 
			
		||||
                                      int64_t sector_num, int nb_sectors,
 | 
			
		||||
                                      bool *error_is_read,
 | 
			
		||||
                                      bool is_write_notifier)
 | 
			
		||||
{
 | 
			
		||||
    BlockBackend *blk = job->common.blk;
 | 
			
		||||
    CowRequest cow_request;
 | 
			
		||||
    struct iovec iov;
 | 
			
		||||
    QEMUIOVector bounce_qiov;
 | 
			
		||||
    void *bounce_buffer = NULL;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    int64_t sectors_per_cluster = cluster_size_sectors(job);
 | 
			
		||||
    int64_t start, end;
 | 
			
		||||
    int n;
 | 
			
		||||
 | 
			
		||||
    qemu_co_rwlock_rdlock(&job->flush_rwlock);
 | 
			
		||||
 | 
			
		||||
    start = sector_num / sectors_per_cluster;
 | 
			
		||||
    end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
 | 
			
		||||
 | 
			
		||||
    trace_backup_do_cow_enter(job, start, sector_num, nb_sectors);
 | 
			
		||||
 | 
			
		||||
    wait_for_overlapping_requests(job, start, end);
 | 
			
		||||
    cow_request_begin(&cow_request, job, start, end);
 | 
			
		||||
 | 
			
		||||
    for (; start < end; start++) {
 | 
			
		||||
        if (test_bit(start, job->done_bitmap)) {
 | 
			
		||||
            trace_backup_do_cow_skip(job, start);
 | 
			
		||||
            continue; /* already copied */
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        trace_backup_do_cow_process(job, start);
 | 
			
		||||
 | 
			
		||||
        n = MIN(sectors_per_cluster,
 | 
			
		||||
                job->common.len / BDRV_SECTOR_SIZE -
 | 
			
		||||
                start * sectors_per_cluster);
 | 
			
		||||
 | 
			
		||||
        if (!bounce_buffer) {
 | 
			
		||||
            bounce_buffer = blk_blockalign(blk, job->cluster_size);
 | 
			
		||||
        }
 | 
			
		||||
        iov.iov_base = bounce_buffer;
 | 
			
		||||
        iov.iov_len = n * BDRV_SECTOR_SIZE;
 | 
			
		||||
        qemu_iovec_init_external(&bounce_qiov, &iov, 1);
 | 
			
		||||
 | 
			
		||||
        ret = blk_co_preadv(blk, start * job->cluster_size,
 | 
			
		||||
                            bounce_qiov.size, &bounce_qiov,
 | 
			
		||||
                            is_write_notifier ? BDRV_REQ_NO_SERIALISING : 0);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            trace_backup_do_cow_read_fail(job, start, ret);
 | 
			
		||||
            if (error_is_read) {
 | 
			
		||||
                *error_is_read = true;
 | 
			
		||||
            }
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (buffer_is_zero(iov.iov_base, iov.iov_len)) {
 | 
			
		||||
            ret = blk_co_pwrite_zeroes(job->target, start * job->cluster_size,
 | 
			
		||||
                                       bounce_qiov.size, BDRV_REQ_MAY_UNMAP);
 | 
			
		||||
        } else {
 | 
			
		||||
            ret = blk_co_pwritev(job->target, start * job->cluster_size,
 | 
			
		||||
                                 bounce_qiov.size, &bounce_qiov,
 | 
			
		||||
                                 job->compress ? BDRV_REQ_WRITE_COMPRESSED : 0);
 | 
			
		||||
        }
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            trace_backup_do_cow_write_fail(job, start, ret);
 | 
			
		||||
            if (error_is_read) {
 | 
			
		||||
                *error_is_read = false;
 | 
			
		||||
            }
 | 
			
		||||
            goto out;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        set_bit(start, job->done_bitmap);
 | 
			
		||||
 | 
			
		||||
        /* Publish progress, guest I/O counts as progress too.  Note that the
 | 
			
		||||
         * offset field is an opaque progress value, it is not a disk offset.
 | 
			
		||||
         */
 | 
			
		||||
        job->sectors_read += n;
 | 
			
		||||
        job->common.offset += n * BDRV_SECTOR_SIZE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
out:
 | 
			
		||||
    if (bounce_buffer) {
 | 
			
		||||
        qemu_vfree(bounce_buffer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    cow_request_end(&cow_request);
 | 
			
		||||
 | 
			
		||||
    trace_backup_do_cow_return(job, sector_num, nb_sectors, ret);
 | 
			
		||||
 | 
			
		||||
    qemu_co_rwlock_unlock(&job->flush_rwlock);
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn backup_before_write_notify(
 | 
			
		||||
        NotifierWithReturn *notifier,
 | 
			
		||||
        void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *job = container_of(notifier, BackupBlockJob, before_write);
 | 
			
		||||
    BdrvTrackedRequest *req = opaque;
 | 
			
		||||
    int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
 | 
			
		||||
    int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
    assert(req->bs == blk_bs(job->common.blk));
 | 
			
		||||
    assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
 | 
			
		||||
    assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
 | 
			
		||||
 | 
			
		||||
    return backup_do_cow(job, sector_num, nb_sectors, NULL, true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    if (speed < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_cleanup_sync_bitmap(BackupBlockJob *job, int ret)
 | 
			
		||||
{
 | 
			
		||||
    BdrvDirtyBitmap *bm;
 | 
			
		||||
    BlockDriverState *bs = blk_bs(job->common.blk);
 | 
			
		||||
 | 
			
		||||
    if (ret < 0 || block_job_is_cancelled(&job->common)) {
 | 
			
		||||
        /* Merge the successor back into the parent, delete nothing. */
 | 
			
		||||
        bm = bdrv_reclaim_dirty_bitmap(bs, job->sync_bitmap, NULL);
 | 
			
		||||
        assert(bm);
 | 
			
		||||
    } else {
 | 
			
		||||
        /* Everything is fine, delete this bitmap and install the backup. */
 | 
			
		||||
        bm = bdrv_dirty_bitmap_abdicate(bs, job->sync_bitmap, NULL);
 | 
			
		||||
        assert(bm);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_commit(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
    if (s->sync_bitmap) {
 | 
			
		||||
        backup_cleanup_sync_bitmap(s, 0);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_abort(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
    if (s->sync_bitmap) {
 | 
			
		||||
        backup_cleanup_sync_bitmap(s, -1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_clean(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
    assert(s->target);
 | 
			
		||||
    blk_unref(s->target);
 | 
			
		||||
    s->target = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_attached_aio_context(BlockJob *job, AioContext *aio_context)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    blk_set_aio_context(s->target, aio_context);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void backup_do_checkpoint(BlockJob *job, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
 | 
			
		||||
    int64_t len;
 | 
			
		||||
 | 
			
		||||
    assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
 | 
			
		||||
 | 
			
		||||
    if (backup_job->sync_mode != MIRROR_SYNC_MODE_NONE) {
 | 
			
		||||
        error_setg(errp, "The backup job only supports block checkpoint in"
 | 
			
		||||
                   " sync=none mode");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len = DIV_ROUND_UP(backup_job->common.len, backup_job->cluster_size);
 | 
			
		||||
    bitmap_zero(backup_job->done_bitmap, len);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void backup_wait_for_overlapping_requests(BlockJob *job, int64_t sector_num,
 | 
			
		||||
                                          int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
 | 
			
		||||
    int64_t sectors_per_cluster = cluster_size_sectors(backup_job);
 | 
			
		||||
    int64_t start, end;
 | 
			
		||||
 | 
			
		||||
    assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
 | 
			
		||||
 | 
			
		||||
    start = sector_num / sectors_per_cluster;
 | 
			
		||||
    end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
 | 
			
		||||
    wait_for_overlapping_requests(backup_job, start, end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void backup_cow_request_begin(CowRequest *req, BlockJob *job,
 | 
			
		||||
                              int64_t sector_num,
 | 
			
		||||
                              int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *backup_job = container_of(job, BackupBlockJob, common);
 | 
			
		||||
    int64_t sectors_per_cluster = cluster_size_sectors(backup_job);
 | 
			
		||||
    int64_t start, end;
 | 
			
		||||
 | 
			
		||||
    assert(job->driver->job_type == BLOCK_JOB_TYPE_BACKUP);
 | 
			
		||||
 | 
			
		||||
    start = sector_num / sectors_per_cluster;
 | 
			
		||||
    end = DIV_ROUND_UP(sector_num + nb_sectors, sectors_per_cluster);
 | 
			
		||||
    cow_request_begin(req, backup_job, start, end);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void backup_cow_request_end(CowRequest *req)
 | 
			
		||||
{
 | 
			
		||||
    cow_request_end(req);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void backup_drain(BlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *s = container_of(job, BackupBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    /* Need to keep a reference in case blk_drain triggers execution
 | 
			
		||||
     * of backup_complete...
 | 
			
		||||
     */
 | 
			
		||||
    if (s->target) {
 | 
			
		||||
        BlockBackend *target = s->target;
 | 
			
		||||
        blk_ref(target);
 | 
			
		||||
        blk_drain(target);
 | 
			
		||||
        blk_unref(target);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockErrorAction backup_error_action(BackupBlockJob *job,
 | 
			
		||||
                                            bool read, int error)
 | 
			
		||||
{
 | 
			
		||||
    if (read) {
 | 
			
		||||
        return block_job_error_action(&job->common, job->on_source_error,
 | 
			
		||||
                                      true, error);
 | 
			
		||||
    } else {
 | 
			
		||||
        return block_job_error_action(&job->common, job->on_target_error,
 | 
			
		||||
                                      false, error);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int ret;
 | 
			
		||||
} BackupCompleteData;
 | 
			
		||||
 | 
			
		||||
static void backup_complete(BlockJob *job, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BackupCompleteData *data = opaque;
 | 
			
		||||
 | 
			
		||||
    block_job_completed(job, data->ret);
 | 
			
		||||
    g_free(data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool coroutine_fn yield_and_check(BackupBlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    if (block_job_is_cancelled(&job->common)) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* we need to yield so that bdrv_drain_all() returns.
 | 
			
		||||
     * (without, VM does not reboot)
 | 
			
		||||
     */
 | 
			
		||||
    if (job->common.speed) {
 | 
			
		||||
        uint64_t delay_ns = ratelimit_calculate_delay(&job->limit,
 | 
			
		||||
                                                      job->sectors_read);
 | 
			
		||||
        job->sectors_read = 0;
 | 
			
		||||
        block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, delay_ns);
 | 
			
		||||
    } else {
 | 
			
		||||
        block_job_sleep_ns(&job->common, QEMU_CLOCK_REALTIME, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (block_job_is_cancelled(&job->common)) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn backup_run_incremental(BackupBlockJob *job)
 | 
			
		||||
{
 | 
			
		||||
    bool error_is_read;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    int clusters_per_iter;
 | 
			
		||||
    uint32_t granularity;
 | 
			
		||||
    int64_t sector;
 | 
			
		||||
    int64_t cluster;
 | 
			
		||||
    int64_t end;
 | 
			
		||||
    int64_t last_cluster = -1;
 | 
			
		||||
    int64_t sectors_per_cluster = cluster_size_sectors(job);
 | 
			
		||||
    BdrvDirtyBitmapIter *dbi;
 | 
			
		||||
 | 
			
		||||
    granularity = bdrv_dirty_bitmap_granularity(job->sync_bitmap);
 | 
			
		||||
    clusters_per_iter = MAX((granularity / job->cluster_size), 1);
 | 
			
		||||
    dbi = bdrv_dirty_iter_new(job->sync_bitmap, 0);
 | 
			
		||||
 | 
			
		||||
    /* Find the next dirty sector(s) */
 | 
			
		||||
    while ((sector = bdrv_dirty_iter_next(dbi)) != -1) {
 | 
			
		||||
        cluster = sector / sectors_per_cluster;
 | 
			
		||||
 | 
			
		||||
        /* Fake progress updates for any clusters we skipped */
 | 
			
		||||
        if (cluster != last_cluster + 1) {
 | 
			
		||||
            job->common.offset += ((cluster - last_cluster - 1) *
 | 
			
		||||
                                   job->cluster_size);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (end = cluster + clusters_per_iter; cluster < end; cluster++) {
 | 
			
		||||
            do {
 | 
			
		||||
                if (yield_and_check(job)) {
 | 
			
		||||
                    goto out;
 | 
			
		||||
                }
 | 
			
		||||
                ret = backup_do_cow(job, cluster * sectors_per_cluster,
 | 
			
		||||
                                    sectors_per_cluster, &error_is_read,
 | 
			
		||||
                                    false);
 | 
			
		||||
                if ((ret < 0) &&
 | 
			
		||||
                    backup_error_action(job, error_is_read, -ret) ==
 | 
			
		||||
                    BLOCK_ERROR_ACTION_REPORT) {
 | 
			
		||||
                    goto out;
 | 
			
		||||
                }
 | 
			
		||||
            } while (ret < 0);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* If the bitmap granularity is smaller than the backup granularity,
 | 
			
		||||
         * we need to advance the iterator pointer to the next cluster. */
 | 
			
		||||
        if (granularity < job->cluster_size) {
 | 
			
		||||
            bdrv_set_dirty_iter(dbi, cluster * sectors_per_cluster);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        last_cluster = cluster - 1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Play some final catchup with the progress meter */
 | 
			
		||||
    end = DIV_ROUND_UP(job->common.len, job->cluster_size);
 | 
			
		||||
    if (last_cluster + 1 < end) {
 | 
			
		||||
        job->common.offset += ((end - last_cluster - 1) * job->cluster_size);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
out:
 | 
			
		||||
    bdrv_dirty_iter_free(dbi);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coroutine_fn backup_run(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BackupBlockJob *job = opaque;
 | 
			
		||||
    BackupCompleteData *data;
 | 
			
		||||
    BlockDriverState *bs = blk_bs(job->common.blk);
 | 
			
		||||
    int64_t start, end;
 | 
			
		||||
    int64_t sectors_per_cluster = cluster_size_sectors(job);
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
 | 
			
		||||
    QLIST_INIT(&job->inflight_reqs);
 | 
			
		||||
    qemu_co_rwlock_init(&job->flush_rwlock);
 | 
			
		||||
 | 
			
		||||
    start = 0;
 | 
			
		||||
    end = DIV_ROUND_UP(job->common.len, job->cluster_size);
 | 
			
		||||
 | 
			
		||||
    job->done_bitmap = bitmap_new(end);
 | 
			
		||||
 | 
			
		||||
    job->before_write.notify = backup_before_write_notify;
 | 
			
		||||
    bdrv_add_before_write_notifier(bs, &job->before_write);
 | 
			
		||||
 | 
			
		||||
    if (job->sync_mode == MIRROR_SYNC_MODE_NONE) {
 | 
			
		||||
        while (!block_job_is_cancelled(&job->common)) {
 | 
			
		||||
            /* Yield until the job is cancelled.  We just let our before_write
 | 
			
		||||
             * notify callback service CoW requests. */
 | 
			
		||||
            block_job_yield(&job->common);
 | 
			
		||||
        }
 | 
			
		||||
    } else if (job->sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
 | 
			
		||||
        ret = backup_run_incremental(job);
 | 
			
		||||
    } else {
 | 
			
		||||
        /* Both FULL and TOP SYNC_MODE's require copying.. */
 | 
			
		||||
        for (; start < end; start++) {
 | 
			
		||||
            bool error_is_read;
 | 
			
		||||
            int alloced = 0;
 | 
			
		||||
 | 
			
		||||
            if (yield_and_check(job)) {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
 | 
			
		||||
                int i, n;
 | 
			
		||||
 | 
			
		||||
                /* Check to see if these blocks are already in the
 | 
			
		||||
                 * backing file. */
 | 
			
		||||
 | 
			
		||||
                for (i = 0; i < sectors_per_cluster;) {
 | 
			
		||||
                    /* bdrv_is_allocated() only returns true/false based
 | 
			
		||||
                     * on the first set of sectors it comes across that
 | 
			
		||||
                     * are are all in the same state.
 | 
			
		||||
                     * For that reason we must verify each sector in the
 | 
			
		||||
                     * backup cluster length.  We end up copying more than
 | 
			
		||||
                     * needed but at some point that is always the case. */
 | 
			
		||||
                    alloced =
 | 
			
		||||
                        bdrv_is_allocated(bs,
 | 
			
		||||
                                start * sectors_per_cluster + i,
 | 
			
		||||
                                sectors_per_cluster - i, &n);
 | 
			
		||||
                    i += n;
 | 
			
		||||
 | 
			
		||||
                    if (alloced || n == 0) {
 | 
			
		||||
                        break;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                /* If the above loop never found any sectors that are in
 | 
			
		||||
                 * the topmost image, skip this backup. */
 | 
			
		||||
                if (alloced == 0) {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            /* FULL sync mode we copy the whole drive. */
 | 
			
		||||
            if (alloced < 0) {
 | 
			
		||||
                ret = alloced;
 | 
			
		||||
            } else {
 | 
			
		||||
                ret = backup_do_cow(job, start * sectors_per_cluster,
 | 
			
		||||
                                    sectors_per_cluster, &error_is_read,
 | 
			
		||||
                                    false);
 | 
			
		||||
            }
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                /* Depending on error action, fail now or retry cluster */
 | 
			
		||||
                BlockErrorAction action =
 | 
			
		||||
                    backup_error_action(job, error_is_read, -ret);
 | 
			
		||||
                if (action == BLOCK_ERROR_ACTION_REPORT) {
 | 
			
		||||
                    break;
 | 
			
		||||
                } else {
 | 
			
		||||
                    start--;
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    notifier_with_return_remove(&job->before_write);
 | 
			
		||||
 | 
			
		||||
    /* wait until pending backup_do_cow() calls have completed */
 | 
			
		||||
    qemu_co_rwlock_wrlock(&job->flush_rwlock);
 | 
			
		||||
    qemu_co_rwlock_unlock(&job->flush_rwlock);
 | 
			
		||||
    g_free(job->done_bitmap);
 | 
			
		||||
 | 
			
		||||
    data = g_malloc(sizeof(*data));
 | 
			
		||||
    data->ret = ret;
 | 
			
		||||
    block_job_defer_to_main_loop(&job->common, backup_complete, data);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const BlockJobDriver backup_job_driver = {
 | 
			
		||||
    .instance_size          = sizeof(BackupBlockJob),
 | 
			
		||||
    .job_type               = BLOCK_JOB_TYPE_BACKUP,
 | 
			
		||||
    .start                  = backup_run,
 | 
			
		||||
    .set_speed              = backup_set_speed,
 | 
			
		||||
    .commit                 = backup_commit,
 | 
			
		||||
    .abort                  = backup_abort,
 | 
			
		||||
    .clean                  = backup_clean,
 | 
			
		||||
    .attached_aio_context   = backup_attached_aio_context,
 | 
			
		||||
    .drain                  = backup_drain,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs,
 | 
			
		||||
                  BlockDriverState *target, int64_t speed,
 | 
			
		||||
                  MirrorSyncMode sync_mode, BdrvDirtyBitmap *sync_bitmap,
 | 
			
		||||
                  bool compress,
 | 
			
		||||
                  BlockdevOnError on_source_error,
 | 
			
		||||
                  BlockdevOnError on_target_error,
 | 
			
		||||
                  int creation_flags,
 | 
			
		||||
                  BlockCompletionFunc *cb, void *opaque,
 | 
			
		||||
                  BlockJobTxn *txn, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    int64_t len;
 | 
			
		||||
    BlockDriverInfo bdi;
 | 
			
		||||
    BackupBlockJob *job = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    assert(bs);
 | 
			
		||||
    assert(target);
 | 
			
		||||
 | 
			
		||||
    if (bs == target) {
 | 
			
		||||
        error_setg(errp, "Source and target cannot be the same");
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_inserted(bs)) {
 | 
			
		||||
        error_setg(errp, "Device is not inserted: %s",
 | 
			
		||||
                   bdrv_get_device_name(bs));
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!bdrv_is_inserted(target)) {
 | 
			
		||||
        error_setg(errp, "Device is not inserted: %s",
 | 
			
		||||
                   bdrv_get_device_name(target));
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (compress && target->drv->bdrv_co_pwritev_compressed == NULL) {
 | 
			
		||||
        error_setg(errp, "Compression is not supported for this drive %s",
 | 
			
		||||
                   bdrv_get_device_name(target));
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (sync_mode == MIRROR_SYNC_MODE_INCREMENTAL) {
 | 
			
		||||
        if (!sync_bitmap) {
 | 
			
		||||
            error_setg(errp, "must provide a valid bitmap name for "
 | 
			
		||||
                             "\"incremental\" sync mode");
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /* Create a new bitmap, and freeze/disable this one. */
 | 
			
		||||
        if (bdrv_dirty_bitmap_create_successor(bs, sync_bitmap, errp) < 0) {
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
    } else if (sync_bitmap) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                   "a sync_bitmap was provided to backup_run, "
 | 
			
		||||
                   "but received an incompatible sync_mode (%s)",
 | 
			
		||||
                   MirrorSyncMode_lookup[sync_mode]);
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    len = bdrv_getlength(bs);
 | 
			
		||||
    if (len < 0) {
 | 
			
		||||
        error_setg_errno(errp, -len, "unable to get length for '%s'",
 | 
			
		||||
                         bdrv_get_device_name(bs));
 | 
			
		||||
        goto error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* job->common.len is fixed, so we can't allow resize */
 | 
			
		||||
    job = block_job_create(job_id, &backup_job_driver, bs,
 | 
			
		||||
                           BLK_PERM_CONSISTENT_READ,
 | 
			
		||||
                           BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
 | 
			
		||||
                           BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD,
 | 
			
		||||
                           speed, creation_flags, cb, opaque, errp);
 | 
			
		||||
    if (!job) {
 | 
			
		||||
        goto error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* The target must match the source in size, so no resize here either */
 | 
			
		||||
    job->target = blk_new(BLK_PERM_WRITE,
 | 
			
		||||
                          BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE |
 | 
			
		||||
                          BLK_PERM_WRITE_UNCHANGED | BLK_PERM_GRAPH_MOD);
 | 
			
		||||
    ret = blk_insert_bs(job->target, target, errp);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    job->on_source_error = on_source_error;
 | 
			
		||||
    job->on_target_error = on_target_error;
 | 
			
		||||
    job->sync_mode = sync_mode;
 | 
			
		||||
    job->sync_bitmap = sync_mode == MIRROR_SYNC_MODE_INCREMENTAL ?
 | 
			
		||||
                       sync_bitmap : NULL;
 | 
			
		||||
    job->compress = compress;
 | 
			
		||||
 | 
			
		||||
    /* If there is no backing file on the target, we cannot rely on COW if our
 | 
			
		||||
     * backup cluster size is smaller than the target cluster size. Even for
 | 
			
		||||
     * targets with a backing file, try to avoid COW if possible. */
 | 
			
		||||
    ret = bdrv_get_info(target, &bdi);
 | 
			
		||||
    if (ret == -ENOTSUP && !target->backing) {
 | 
			
		||||
        /* Cluster size is not defined */
 | 
			
		||||
        error_report("WARNING: The target block device doesn't provide "
 | 
			
		||||
                     "information about the block size and it doesn't have a "
 | 
			
		||||
                     "backing file. The default block size of %u bytes is "
 | 
			
		||||
                     "used. If the actual block size of the target exceeds "
 | 
			
		||||
                     "this default, the backup may be unusable",
 | 
			
		||||
                     BACKUP_CLUSTER_SIZE_DEFAULT);
 | 
			
		||||
        job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
 | 
			
		||||
    } else if (ret < 0 && !target->backing) {
 | 
			
		||||
        error_setg_errno(errp, -ret,
 | 
			
		||||
            "Couldn't determine the cluster size of the target image, "
 | 
			
		||||
            "which has no backing file");
 | 
			
		||||
        error_append_hint(errp,
 | 
			
		||||
            "Aborting, since this may create an unusable destination image\n");
 | 
			
		||||
        goto error;
 | 
			
		||||
    } else if (ret < 0 && target->backing) {
 | 
			
		||||
        /* Not fatal; just trudge on ahead. */
 | 
			
		||||
        job->cluster_size = BACKUP_CLUSTER_SIZE_DEFAULT;
 | 
			
		||||
    } else {
 | 
			
		||||
        job->cluster_size = MAX(BACKUP_CLUSTER_SIZE_DEFAULT, bdi.cluster_size);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Required permissions are already taken with target's blk_new() */
 | 
			
		||||
    block_job_add_bdrv(&job->common, "target", target, 0, BLK_PERM_ALL,
 | 
			
		||||
                       &error_abort);
 | 
			
		||||
    job->common.len = len;
 | 
			
		||||
    block_job_txn_add_job(txn, &job->common);
 | 
			
		||||
 | 
			
		||||
    return &job->common;
 | 
			
		||||
 | 
			
		||||
 error:
 | 
			
		||||
    if (sync_bitmap) {
 | 
			
		||||
        bdrv_reclaim_dirty_bitmap(bs, sync_bitmap, NULL);
 | 
			
		||||
    }
 | 
			
		||||
    if (job) {
 | 
			
		||||
        backup_clean(&job->common);
 | 
			
		||||
        block_job_early_fail(&job->common);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return NULL;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										701
									
								
								block/blkdebug.c
									
									
									
									
									
								
							
							
						
						
									
										701
									
								
								block/blkdebug.c
									
									
									
									
									
								
							@@ -1,7 +1,6 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Block protocol for I/O error injection
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (C) 2016-2017 Red Hat, Inc.
 | 
			
		||||
 * Copyright (c) 2010 Kevin Wolf <kwolf@redhat.com>
 | 
			
		||||
 *
 | 
			
		||||
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
@@ -23,38 +22,23 @@
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu/cutils.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "qemu/config-file.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include "qapi/qmp/qbool.h"
 | 
			
		||||
#include "qapi/qmp/qdict.h"
 | 
			
		||||
#include "qapi/qmp/qint.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
#include "sysemu/qtest.h"
 | 
			
		||||
 | 
			
		||||
typedef struct BDRVBlkdebugState {
 | 
			
		||||
    int state;
 | 
			
		||||
    int new_state;
 | 
			
		||||
    uint64_t align;
 | 
			
		||||
    uint64_t max_transfer;
 | 
			
		||||
    uint64_t opt_write_zero;
 | 
			
		||||
    uint64_t max_write_zero;
 | 
			
		||||
    uint64_t opt_discard;
 | 
			
		||||
    uint64_t max_discard;
 | 
			
		||||
 | 
			
		||||
    /* For blkdebug_refresh_filename() */
 | 
			
		||||
    char *config_file;
 | 
			
		||||
 | 
			
		||||
    QLIST_HEAD(, BlkdebugRule) rules[BLKDBG__MAX];
 | 
			
		||||
    QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
 | 
			
		||||
    QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
 | 
			
		||||
    QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
 | 
			
		||||
} BDRVBlkdebugState;
 | 
			
		||||
 | 
			
		||||
typedef struct BlkdebugAIOCB {
 | 
			
		||||
    BlockAIOCB common;
 | 
			
		||||
    BlockDriverAIOCB common;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
    int ret;
 | 
			
		||||
} BlkdebugAIOCB;
 | 
			
		||||
 | 
			
		||||
@@ -64,6 +48,13 @@ typedef struct BlkdebugSuspendedReq {
 | 
			
		||||
    QLIST_ENTRY(BlkdebugSuspendedReq) next;
 | 
			
		||||
} BlkdebugSuspendedReq;
 | 
			
		||||
 | 
			
		||||
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);
 | 
			
		||||
 | 
			
		||||
static const AIOCBInfo blkdebug_aiocb_info = {
 | 
			
		||||
    .aiocb_size = sizeof(BlkdebugAIOCB),
 | 
			
		||||
    .cancel     = blkdebug_aio_cancel,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum {
 | 
			
		||||
    ACTION_INJECT_ERROR,
 | 
			
		||||
    ACTION_SET_STATE,
 | 
			
		||||
@@ -71,7 +62,7 @@ enum {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
typedef struct BlkdebugRule {
 | 
			
		||||
    BlkdebugEvent event;
 | 
			
		||||
    BlkDebugEvent event;
 | 
			
		||||
    int action;
 | 
			
		||||
    int state;
 | 
			
		||||
    union {
 | 
			
		||||
@@ -79,7 +70,7 @@ typedef struct BlkdebugRule {
 | 
			
		||||
            int error;
 | 
			
		||||
            int immediately;
 | 
			
		||||
            int once;
 | 
			
		||||
            int64_t offset;
 | 
			
		||||
            int64_t sector;
 | 
			
		||||
        } inject;
 | 
			
		||||
        struct {
 | 
			
		||||
            int new_state;
 | 
			
		||||
@@ -150,12 +141,55 @@ static QemuOptsList *config_groups[] = {
 | 
			
		||||
    NULL
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int get_event_by_name(const char *name, BlkdebugEvent *event)
 | 
			
		||||
static const char *event_names[BLKDBG_EVENT_MAX] = {
 | 
			
		||||
    [BLKDBG_L1_UPDATE]                      = "l1_update",
 | 
			
		||||
    [BLKDBG_L1_GROW_ALLOC_TABLE]            = "l1_grow.alloc_table",
 | 
			
		||||
    [BLKDBG_L1_GROW_WRITE_TABLE]            = "l1_grow.write_table",
 | 
			
		||||
    [BLKDBG_L1_GROW_ACTIVATE_TABLE]         = "l1_grow.activate_table",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_L2_LOAD]                        = "l2_load",
 | 
			
		||||
    [BLKDBG_L2_UPDATE]                      = "l2_update",
 | 
			
		||||
    [BLKDBG_L2_UPDATE_COMPRESSED]           = "l2_update_compressed",
 | 
			
		||||
    [BLKDBG_L2_ALLOC_COW_READ]              = "l2_alloc.cow_read",
 | 
			
		||||
    [BLKDBG_L2_ALLOC_WRITE]                 = "l2_alloc.write",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_READ_AIO]                       = "read_aio",
 | 
			
		||||
    [BLKDBG_READ_BACKING_AIO]               = "read_backing_aio",
 | 
			
		||||
    [BLKDBG_READ_COMPRESSED]                = "read_compressed",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_WRITE_AIO]                      = "write_aio",
 | 
			
		||||
    [BLKDBG_WRITE_COMPRESSED]               = "write_compressed",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_VMSTATE_LOAD]                   = "vmstate_load",
 | 
			
		||||
    [BLKDBG_VMSTATE_SAVE]                   = "vmstate_save",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_COW_READ]                       = "cow_read",
 | 
			
		||||
    [BLKDBG_COW_WRITE]                      = "cow_write",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_REFTABLE_LOAD]                  = "reftable_load",
 | 
			
		||||
    [BLKDBG_REFTABLE_GROW]                  = "reftable_grow",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_REFBLOCK_LOAD]                  = "refblock_load",
 | 
			
		||||
    [BLKDBG_REFBLOCK_UPDATE]                = "refblock_update",
 | 
			
		||||
    [BLKDBG_REFBLOCK_UPDATE_PART]           = "refblock_update_part",
 | 
			
		||||
    [BLKDBG_REFBLOCK_ALLOC]                 = "refblock_alloc",
 | 
			
		||||
    [BLKDBG_REFBLOCK_ALLOC_HOOKUP]          = "refblock_alloc.hookup",
 | 
			
		||||
    [BLKDBG_REFBLOCK_ALLOC_WRITE]           = "refblock_alloc.write",
 | 
			
		||||
    [BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS]    = "refblock_alloc.write_blocks",
 | 
			
		||||
    [BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE]     = "refblock_alloc.write_table",
 | 
			
		||||
    [BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE]    = "refblock_alloc.switch_table",
 | 
			
		||||
 | 
			
		||||
    [BLKDBG_CLUSTER_ALLOC]                  = "cluster_alloc",
 | 
			
		||||
    [BLKDBG_CLUSTER_ALLOC_BYTES]            = "cluster_alloc_bytes",
 | 
			
		||||
    [BLKDBG_CLUSTER_FREE]                   = "cluster_free",
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int get_event_by_name(const char *name, BlkDebugEvent *event)
 | 
			
		||||
{
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < BLKDBG__MAX; i++) {
 | 
			
		||||
        if (!strcmp(BlkdebugEvent_lookup[i], name)) {
 | 
			
		||||
    for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
 | 
			
		||||
        if (!strcmp(event_names[i], name)) {
 | 
			
		||||
            *event = i;
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
@@ -169,22 +203,17 @@ struct add_rule_data {
 | 
			
		||||
    int action;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
 | 
			
		||||
static int add_rule(QemuOpts *opts, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    struct add_rule_data *d = opaque;
 | 
			
		||||
    BDRVBlkdebugState *s = d->s;
 | 
			
		||||
    const char* event_name;
 | 
			
		||||
    BlkdebugEvent event;
 | 
			
		||||
    BlkDebugEvent event;
 | 
			
		||||
    struct BlkdebugRule *rule;
 | 
			
		||||
    int64_t sector;
 | 
			
		||||
 | 
			
		||||
    /* Find the right event for the rule */
 | 
			
		||||
    event_name = qemu_opt_get(opts, "event");
 | 
			
		||||
    if (!event_name) {
 | 
			
		||||
        error_setg(errp, "Missing event name for rule");
 | 
			
		||||
        return -1;
 | 
			
		||||
    } else if (get_event_by_name(event_name, &event) < 0) {
 | 
			
		||||
        error_setg(errp, "Invalid event name \"%s\"", event_name);
 | 
			
		||||
    if (!event_name || get_event_by_name(event_name, &event) < 0) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -203,9 +232,7 @@ static int add_rule(void *opaque, QemuOpts *opts, Error **errp)
 | 
			
		||||
        rule->options.inject.once  = qemu_opt_get_bool(opts, "once", 0);
 | 
			
		||||
        rule->options.inject.immediately =
 | 
			
		||||
            qemu_opt_get_bool(opts, "immediately", 0);
 | 
			
		||||
        sector = qemu_opt_get_number(opts, "sector", -1);
 | 
			
		||||
        rule->options.inject.offset =
 | 
			
		||||
            sector == -1 ? -1 : sector * BDRV_SECTOR_SIZE;
 | 
			
		||||
        rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case ACTION_SET_STATE:
 | 
			
		||||
@@ -240,407 +267,166 @@ static void remove_rule(BlkdebugRule *rule)
 | 
			
		||||
    g_free(rule);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int read_config(BDRVBlkdebugState *s, const char *filename,
 | 
			
		||||
                       QDict *options, Error **errp)
 | 
			
		||||
static int read_config(BDRVBlkdebugState *s, const char *filename)
 | 
			
		||||
{
 | 
			
		||||
    FILE *f = NULL;
 | 
			
		||||
    FILE *f;
 | 
			
		||||
    int ret;
 | 
			
		||||
    struct add_rule_data d;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
 | 
			
		||||
    if (filename) {
 | 
			
		||||
        f = fopen(filename, "r");
 | 
			
		||||
        if (f == NULL) {
 | 
			
		||||
            error_setg_errno(errp, errno, "Could not read blkdebug config file");
 | 
			
		||||
            return -errno;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ret = qemu_config_parse(f, config_groups, filename);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            error_setg(errp, "Could not parse blkdebug config file");
 | 
			
		||||
            ret = -EINVAL;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    /* Allow usage without config file */
 | 
			
		||||
    if (!*filename) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_config_parse_qdict(options, config_groups, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
    f = fopen(filename, "r");
 | 
			
		||||
    if (f == NULL) {
 | 
			
		||||
        return -errno;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = qemu_config_parse(f, config_groups, filename);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    d.s = s;
 | 
			
		||||
    d.action = ACTION_INJECT_ERROR;
 | 
			
		||||
    qemu_opts_foreach(&inject_error_opts, add_rule, &d, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
 | 
			
		||||
 | 
			
		||||
    d.action = ACTION_SET_STATE;
 | 
			
		||||
    qemu_opts_foreach(&set_state_opts, add_rule, &d, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
fail:
 | 
			
		||||
    qemu_opts_reset(&inject_error_opts);
 | 
			
		||||
    qemu_opts_reset(&set_state_opts);
 | 
			
		||||
    if (f) {
 | 
			
		||||
        fclose(f);
 | 
			
		||||
    }
 | 
			
		||||
    fclose(f);
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
 | 
			
		||||
static void blkdebug_parse_filename(const char *filename, QDict *options,
 | 
			
		||||
                                    Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    const char *c;
 | 
			
		||||
 | 
			
		||||
    /* Parse the blkdebug: prefix */
 | 
			
		||||
    if (!strstart(filename, "blkdebug:", &filename)) {
 | 
			
		||||
        /* There was no prefix; therefore, all options have to be already
 | 
			
		||||
           present in the QDict (except for the filename) */
 | 
			
		||||
        qdict_put_str(options, "x-image", filename);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Parse config file path */
 | 
			
		||||
    c = strchr(filename, ':');
 | 
			
		||||
    if (c == NULL) {
 | 
			
		||||
        error_setg(errp, "blkdebug requires both config file and image path");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (c != filename) {
 | 
			
		||||
        QString *config_path;
 | 
			
		||||
        config_path = qstring_from_substr(filename, 0, c - filename - 1);
 | 
			
		||||
        qdict_put(options, "config", config_path);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* TODO Allow multi-level nesting and set file.filename here */
 | 
			
		||||
    filename = c + 1;
 | 
			
		||||
    qdict_put_str(options, "x-image", filename);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static QemuOptsList runtime_opts = {
 | 
			
		||||
    .name = "blkdebug",
 | 
			
		||||
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
 | 
			
		||||
    .desc = {
 | 
			
		||||
        {
 | 
			
		||||
            .name = "config",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "Path to the configuration file",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "x-image",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "[internal use only, will be removed]",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "align",
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Required alignment in bytes",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "max-transfer",
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Maximum transfer size in bytes",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "opt-write-zero",
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Optimum write zero alignment in bytes",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "max-write-zero",
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Maximum write zero size in bytes",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "opt-discard",
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Optimum discard alignment in bytes",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "max-discard",
 | 
			
		||||
            .type = QEMU_OPT_SIZE,
 | 
			
		||||
            .help = "Maximum discard size in bytes",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                         Error **errp)
 | 
			
		||||
static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
    uint64_t align;
 | 
			
		||||
    char *config, *c;
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto out;
 | 
			
		||||
    /* Parse the blkdebug: prefix */
 | 
			
		||||
    if (strncmp(filename, "blkdebug:", strlen("blkdebug:"))) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
    filename += strlen("blkdebug:");
 | 
			
		||||
 | 
			
		||||
    /* Read rules from config file */
 | 
			
		||||
    c = strchr(filename, ':');
 | 
			
		||||
    if (c == NULL) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Read rules from config file or command line options */
 | 
			
		||||
    s->config_file = g_strdup(qemu_opt_get(opts, "config"));
 | 
			
		||||
    ret = read_config(s, s->config_file, options, errp);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        goto out;
 | 
			
		||||
    config = g_strdup(filename);
 | 
			
		||||
    config[c - filename] = '\0';
 | 
			
		||||
    ret = read_config(s, config);
 | 
			
		||||
    g_free(config);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    filename = c + 1;
 | 
			
		||||
 | 
			
		||||
    /* Set initial state */
 | 
			
		||||
    s->state = 1;
 | 
			
		||||
 | 
			
		||||
    /* Open the image file */
 | 
			
		||||
    bs->file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options, "image",
 | 
			
		||||
                               bs, &child_file, false, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bs->supported_write_flags = BDRV_REQ_FUA &
 | 
			
		||||
        bs->file->bs->supported_write_flags;
 | 
			
		||||
    bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
 | 
			
		||||
        bs->file->bs->supported_zero_flags;
 | 
			
		||||
    ret = -EINVAL;
 | 
			
		||||
 | 
			
		||||
    /* Set alignment overrides */
 | 
			
		||||
    s->align = qemu_opt_get_size(opts, "align", 0);
 | 
			
		||||
    if (s->align && (s->align >= INT_MAX || !is_power_of_2(s->align))) {
 | 
			
		||||
        error_setg(errp, "Cannot meet constraints with align %" PRIu64,
 | 
			
		||||
                   s->align);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
    align = MAX(s->align, bs->file->bs->bl.request_alignment);
 | 
			
		||||
 | 
			
		||||
    s->max_transfer = qemu_opt_get_size(opts, "max-transfer", 0);
 | 
			
		||||
    if (s->max_transfer &&
 | 
			
		||||
        (s->max_transfer >= INT_MAX ||
 | 
			
		||||
         !QEMU_IS_ALIGNED(s->max_transfer, align))) {
 | 
			
		||||
        error_setg(errp, "Cannot meet constraints with max-transfer %" PRIu64,
 | 
			
		||||
                   s->max_transfer);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->opt_write_zero = qemu_opt_get_size(opts, "opt-write-zero", 0);
 | 
			
		||||
    if (s->opt_write_zero &&
 | 
			
		||||
        (s->opt_write_zero >= INT_MAX ||
 | 
			
		||||
         !QEMU_IS_ALIGNED(s->opt_write_zero, align))) {
 | 
			
		||||
        error_setg(errp, "Cannot meet constraints with opt-write-zero %" PRIu64,
 | 
			
		||||
                   s->opt_write_zero);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->max_write_zero = qemu_opt_get_size(opts, "max-write-zero", 0);
 | 
			
		||||
    if (s->max_write_zero &&
 | 
			
		||||
        (s->max_write_zero >= INT_MAX ||
 | 
			
		||||
         !QEMU_IS_ALIGNED(s->max_write_zero,
 | 
			
		||||
                          MAX(s->opt_write_zero, align)))) {
 | 
			
		||||
        error_setg(errp, "Cannot meet constraints with max-write-zero %" PRIu64,
 | 
			
		||||
                   s->max_write_zero);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->opt_discard = qemu_opt_get_size(opts, "opt-discard", 0);
 | 
			
		||||
    if (s->opt_discard &&
 | 
			
		||||
        (s->opt_discard >= INT_MAX ||
 | 
			
		||||
         !QEMU_IS_ALIGNED(s->opt_discard, align))) {
 | 
			
		||||
        error_setg(errp, "Cannot meet constraints with opt-discard %" PRIu64,
 | 
			
		||||
                   s->opt_discard);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->max_discard = qemu_opt_get_size(opts, "max-discard", 0);
 | 
			
		||||
    if (s->max_discard &&
 | 
			
		||||
        (s->max_discard >= INT_MAX ||
 | 
			
		||||
         !QEMU_IS_ALIGNED(s->max_discard,
 | 
			
		||||
                          MAX(s->opt_discard, align)))) {
 | 
			
		||||
        error_setg(errp, "Cannot meet constraints with max-discard %" PRIu64,
 | 
			
		||||
                   s->max_discard);
 | 
			
		||||
        goto out;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
out:
 | 
			
		||||
    /* Open the backing file */
 | 
			
		||||
    ret = bdrv_file_open(&bs->file, filename, flags);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        g_free(s->config_file);
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
    return ret;
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int rule_check(BlockDriverState *bs, uint64_t offset, uint64_t bytes)
 | 
			
		||||
static void error_callback_bh(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    struct BlkdebugAIOCB *acb = opaque;
 | 
			
		||||
    qemu_bh_delete(acb->bh);
 | 
			
		||||
    acb->common.cb(acb->common.opaque, acb->ret);
 | 
			
		||||
    qemu_aio_release(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
 | 
			
		||||
{
 | 
			
		||||
    BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
 | 
			
		||||
    qemu_aio_release(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
 | 
			
		||||
    BlockDriverCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    int error = rule->options.inject.error;
 | 
			
		||||
    struct BlkdebugAIOCB *acb;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
 | 
			
		||||
    if (rule->options.inject.once) {
 | 
			
		||||
        QSIMPLEQ_INIT(&s->active_rules);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (rule->options.inject.immediately) {
 | 
			
		||||
        return NULL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
 | 
			
		||||
    acb->ret = -error;
 | 
			
		||||
 | 
			
		||||
    bh = qemu_bh_new(error_callback_bh, acb);
 | 
			
		||||
    acb->bh = bh;
 | 
			
		||||
    qemu_bh_schedule(bh);
 | 
			
		||||
 | 
			
		||||
    return &acb->common;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
 | 
			
		||||
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
 | 
			
		||||
    BlockDriverCompletionFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    BlkdebugRule *rule = NULL;
 | 
			
		||||
    int error;
 | 
			
		||||
    bool immediately;
 | 
			
		||||
 | 
			
		||||
    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
 | 
			
		||||
        uint64_t inject_offset = rule->options.inject.offset;
 | 
			
		||||
 | 
			
		||||
        if (inject_offset == -1 ||
 | 
			
		||||
            (bytes && inject_offset >= offset &&
 | 
			
		||||
             inject_offset < offset + bytes))
 | 
			
		||||
        {
 | 
			
		||||
        if (rule->options.inject.sector == -1 ||
 | 
			
		||||
            (rule->options.inject.sector >= sector_num &&
 | 
			
		||||
             rule->options.inject.sector < sector_num + nb_sectors)) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!rule || !rule->options.inject.error) {
 | 
			
		||||
        return 0;
 | 
			
		||||
    if (rule && rule->options.inject.error) {
 | 
			
		||||
        return inject_error(bs, cb, opaque, rule);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    immediately = rule->options.inject.immediately;
 | 
			
		||||
    error = rule->options.inject.error;
 | 
			
		||||
 | 
			
		||||
    if (rule->options.inject.once) {
 | 
			
		||||
        QSIMPLEQ_REMOVE(&s->active_rules, rule, BlkdebugRule, active_next);
 | 
			
		||||
        remove_rule(rule);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!immediately) {
 | 
			
		||||
        aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
 | 
			
		||||
        qemu_coroutine_yield();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return -error;
 | 
			
		||||
    return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
blkdebug_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 | 
			
		||||
                   QEMUIOVector *qiov, int flags)
 | 
			
		||||
static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
 | 
			
		||||
    int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
 | 
			
		||||
    BlockDriverCompletionFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    BlkdebugRule *rule = NULL;
 | 
			
		||||
 | 
			
		||||
    /* Sanity check block layer guarantees */
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
 | 
			
		||||
    if (bs->bl.max_transfer) {
 | 
			
		||||
        assert(bytes <= bs->bl.max_transfer);
 | 
			
		||||
    QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
 | 
			
		||||
        if (rule->options.inject.sector == -1 ||
 | 
			
		||||
            (rule->options.inject.sector >= sector_num &&
 | 
			
		||||
             rule->options.inject.sector < sector_num + nb_sectors)) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = rule_check(bs, offset, bytes);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return err;
 | 
			
		||||
    if (rule && rule->options.inject.error) {
 | 
			
		||||
        return inject_error(bs, cb, opaque, rule);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
 | 
			
		||||
    return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
blkdebug_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 | 
			
		||||
                    QEMUIOVector *qiov, int flags)
 | 
			
		||||
{
 | 
			
		||||
    int err;
 | 
			
		||||
 | 
			
		||||
    /* Sanity check block layer guarantees */
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
 | 
			
		||||
    if (bs->bl.max_transfer) {
 | 
			
		||||
        assert(bytes <= bs->bl.max_transfer);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = rule_check(bs, offset, bytes);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blkdebug_co_flush(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    int err = rule_check(bs, 0, 0);
 | 
			
		||||
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_co_flush(bs->file->bs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkdebug_co_pwrite_zeroes(BlockDriverState *bs,
 | 
			
		||||
                                                  int64_t offset, int count,
 | 
			
		||||
                                                  BdrvRequestFlags flags)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t align = MAX(bs->bl.request_alignment,
 | 
			
		||||
                         bs->bl.pwrite_zeroes_alignment);
 | 
			
		||||
    int err;
 | 
			
		||||
 | 
			
		||||
    /* Only pass through requests that are larger than requested
 | 
			
		||||
     * preferred alignment (so that we test the fallback to writes on
 | 
			
		||||
     * unaligned portions), and check that the block layer never hands
 | 
			
		||||
     * us anything unaligned that crosses an alignment boundary.  */
 | 
			
		||||
    if (count < align) {
 | 
			
		||||
        assert(QEMU_IS_ALIGNED(offset, align) ||
 | 
			
		||||
               QEMU_IS_ALIGNED(offset + count, align) ||
 | 
			
		||||
               DIV_ROUND_UP(offset, align) ==
 | 
			
		||||
               DIV_ROUND_UP(offset + count, align));
 | 
			
		||||
        return -ENOTSUP;
 | 
			
		||||
    }
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(offset, align));
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(count, align));
 | 
			
		||||
    if (bs->bl.max_pwrite_zeroes) {
 | 
			
		||||
        assert(count <= bs->bl.max_pwrite_zeroes);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = rule_check(bs, offset, count);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,
 | 
			
		||||
                                             int64_t offset, int count)
 | 
			
		||||
{
 | 
			
		||||
    uint32_t align = bs->bl.pdiscard_alignment;
 | 
			
		||||
    int err;
 | 
			
		||||
 | 
			
		||||
    /* Only pass through requests that are larger than requested
 | 
			
		||||
     * minimum alignment, and ensure that unaligned requests do not
 | 
			
		||||
     * cross optimum discard boundaries. */
 | 
			
		||||
    if (count < bs->bl.request_alignment) {
 | 
			
		||||
        assert(QEMU_IS_ALIGNED(offset, align) ||
 | 
			
		||||
               QEMU_IS_ALIGNED(offset + count, align) ||
 | 
			
		||||
               DIV_ROUND_UP(offset, align) ==
 | 
			
		||||
               DIV_ROUND_UP(offset + count, align));
 | 
			
		||||
        return -ENOTSUP;
 | 
			
		||||
    }
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
 | 
			
		||||
    assert(QEMU_IS_ALIGNED(count, bs->bl.request_alignment));
 | 
			
		||||
    if (align && count >= align) {
 | 
			
		||||
        assert(QEMU_IS_ALIGNED(offset, align));
 | 
			
		||||
        assert(QEMU_IS_ALIGNED(count, align));
 | 
			
		||||
    }
 | 
			
		||||
    if (bs->bl.max_pdiscard) {
 | 
			
		||||
        assert(count <= bs->bl.max_pdiscard);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    err = rule_check(bs, offset, count);
 | 
			
		||||
    if (err) {
 | 
			
		||||
        return err;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_co_pdiscard(bs->file->bs, offset, count);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkdebug_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
@@ -648,13 +434,11 @@ static void blkdebug_close(BlockDriverState *bs)
 | 
			
		||||
    BlkdebugRule *rule, *next;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < BLKDBG__MAX; i++) {
 | 
			
		||||
    for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
 | 
			
		||||
        QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
 | 
			
		||||
            remove_rule(rule);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    g_free(s->config_file);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
 | 
			
		||||
@@ -670,13 +454,9 @@ static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
 | 
			
		||||
    remove_rule(rule);
 | 
			
		||||
    QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
 | 
			
		||||
 | 
			
		||||
    if (!qtest_enabled()) {
 | 
			
		||||
        printf("blkdebug: Suspended request '%s'\n", r.tag);
 | 
			
		||||
    }
 | 
			
		||||
    printf("blkdebug: Suspended request '%s'\n", r.tag);
 | 
			
		||||
    qemu_coroutine_yield();
 | 
			
		||||
    if (!qtest_enabled()) {
 | 
			
		||||
        printf("blkdebug: Resuming request '%s'\n", r.tag);
 | 
			
		||||
    }
 | 
			
		||||
    printf("blkdebug: Resuming request '%s'\n", r.tag);
 | 
			
		||||
 | 
			
		||||
    QLIST_REMOVE(&r, next);
 | 
			
		||||
    g_free(r.tag);
 | 
			
		||||
@@ -713,13 +493,13 @@ static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
 | 
			
		||||
    return injected;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkdebug_debug_event(BlockDriverState *bs, BlkdebugEvent event)
 | 
			
		||||
static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    struct BlkdebugRule *rule, *next;
 | 
			
		||||
    bool injected;
 | 
			
		||||
 | 
			
		||||
    assert((int)event >= 0 && event < BLKDBG__MAX);
 | 
			
		||||
    assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
 | 
			
		||||
 | 
			
		||||
    injected = false;
 | 
			
		||||
    s->new_state = s->state;
 | 
			
		||||
@@ -734,7 +514,7 @@ static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    struct BlkdebugRule *rule;
 | 
			
		||||
    BlkdebugEvent blkdebug_event;
 | 
			
		||||
    BlkDebugEvent blkdebug_event;
 | 
			
		||||
 | 
			
		||||
    if (get_event_by_name(event, &blkdebug_event) < 0) {
 | 
			
		||||
        return -ENOENT;
 | 
			
		||||
@@ -757,42 +537,17 @@ static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
 | 
			
		||||
static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    BlkdebugSuspendedReq *r, *next;
 | 
			
		||||
    BlkdebugSuspendedReq *r;
 | 
			
		||||
 | 
			
		||||
    QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
 | 
			
		||||
    QLIST_FOREACH(r, &s->suspended_reqs, next) {
 | 
			
		||||
        if (!strcmp(r->tag, tag)) {
 | 
			
		||||
            qemu_coroutine_enter(r->co);
 | 
			
		||||
            qemu_coroutine_enter(r->co, NULL);
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -ENOENT;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blkdebug_debug_remove_breakpoint(BlockDriverState *bs,
 | 
			
		||||
                                            const char *tag)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    BlkdebugSuspendedReq *r, *r_next;
 | 
			
		||||
    BlkdebugRule *rule, *next;
 | 
			
		||||
    int i, ret = -ENOENT;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < BLKDBG__MAX; i++) {
 | 
			
		||||
        QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
 | 
			
		||||
            if (rule->action == ACTION_SUSPEND &&
 | 
			
		||||
                !strcmp(rule->options.suspend.tag, tag)) {
 | 
			
		||||
                remove_rule(rule);
 | 
			
		||||
                ret = 0;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, r_next) {
 | 
			
		||||
        if (!strcmp(r->tag, tag)) {
 | 
			
		||||
            qemu_coroutine_enter(r->co);
 | 
			
		||||
            ret = 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
 | 
			
		||||
{
 | 
			
		||||
@@ -809,114 +564,24 @@ static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
 | 
			
		||||
 | 
			
		||||
static int64_t blkdebug_getlength(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    return bdrv_getlength(bs->file->bs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blkdebug_truncate(BlockDriverState *bs, int64_t offset, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    return bdrv_truncate(bs->file, offset, errp);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkdebug_refresh_filename(BlockDriverState *bs, QDict *options)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
    QDict *opts;
 | 
			
		||||
    const QDictEntry *e;
 | 
			
		||||
    bool force_json = false;
 | 
			
		||||
 | 
			
		||||
    for (e = qdict_first(options); e; e = qdict_next(options, e)) {
 | 
			
		||||
        if (strcmp(qdict_entry_key(e), "config") &&
 | 
			
		||||
            strcmp(qdict_entry_key(e), "x-image"))
 | 
			
		||||
        {
 | 
			
		||||
            force_json = true;
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (force_json && !bs->file->bs->full_open_options) {
 | 
			
		||||
        /* The config file cannot be recreated, so creating a plain filename
 | 
			
		||||
         * is impossible */
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!force_json && bs->file->bs->exact_filename[0]) {
 | 
			
		||||
        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
 | 
			
		||||
                 "blkdebug:%s:%s", s->config_file ?: "",
 | 
			
		||||
                 bs->file->bs->exact_filename);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    opts = qdict_new();
 | 
			
		||||
    qdict_put_str(opts, "driver", "blkdebug");
 | 
			
		||||
 | 
			
		||||
    QINCREF(bs->file->bs->full_open_options);
 | 
			
		||||
    qdict_put(opts, "image", bs->file->bs->full_open_options);
 | 
			
		||||
 | 
			
		||||
    for (e = qdict_first(options); e; e = qdict_next(options, e)) {
 | 
			
		||||
        if (strcmp(qdict_entry_key(e), "x-image")) {
 | 
			
		||||
            qobject_incref(qdict_entry_value(e));
 | 
			
		||||
            qdict_put_obj(opts, qdict_entry_key(e), qdict_entry_value(e));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bs->full_open_options = opts;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkdebug_refresh_limits(BlockDriverState *bs, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkdebugState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    if (s->align) {
 | 
			
		||||
        bs->bl.request_alignment = s->align;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->max_transfer) {
 | 
			
		||||
        bs->bl.max_transfer = s->max_transfer;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->opt_write_zero) {
 | 
			
		||||
        bs->bl.pwrite_zeroes_alignment = s->opt_write_zero;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->max_write_zero) {
 | 
			
		||||
        bs->bl.max_pwrite_zeroes = s->max_write_zero;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->opt_discard) {
 | 
			
		||||
        bs->bl.pdiscard_alignment = s->opt_discard;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->max_discard) {
 | 
			
		||||
        bs->bl.max_pdiscard = s->max_discard;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blkdebug_reopen_prepare(BDRVReopenState *reopen_state,
 | 
			
		||||
                                   BlockReopenQueue *queue, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    return 0;
 | 
			
		||||
    return bdrv_getlength(bs->file);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_blkdebug = {
 | 
			
		||||
    .format_name            = "blkdebug",
 | 
			
		||||
    .protocol_name          = "blkdebug",
 | 
			
		||||
    .instance_size          = sizeof(BDRVBlkdebugState),
 | 
			
		||||
    .format_name        = "blkdebug",
 | 
			
		||||
    .protocol_name      = "blkdebug",
 | 
			
		||||
 | 
			
		||||
    .bdrv_parse_filename    = blkdebug_parse_filename,
 | 
			
		||||
    .bdrv_file_open         = blkdebug_open,
 | 
			
		||||
    .bdrv_close             = blkdebug_close,
 | 
			
		||||
    .bdrv_reopen_prepare    = blkdebug_reopen_prepare,
 | 
			
		||||
    .bdrv_child_perm        = bdrv_filter_default_perms,
 | 
			
		||||
    .instance_size      = sizeof(BDRVBlkdebugState),
 | 
			
		||||
 | 
			
		||||
    .bdrv_getlength         = blkdebug_getlength,
 | 
			
		||||
    .bdrv_truncate          = blkdebug_truncate,
 | 
			
		||||
    .bdrv_refresh_filename  = blkdebug_refresh_filename,
 | 
			
		||||
    .bdrv_refresh_limits    = blkdebug_refresh_limits,
 | 
			
		||||
    .bdrv_file_open     = blkdebug_open,
 | 
			
		||||
    .bdrv_close         = blkdebug_close,
 | 
			
		||||
    .bdrv_getlength     = blkdebug_getlength,
 | 
			
		||||
 | 
			
		||||
    .bdrv_co_preadv         = blkdebug_co_preadv,
 | 
			
		||||
    .bdrv_co_pwritev        = blkdebug_co_pwritev,
 | 
			
		||||
    .bdrv_co_flush_to_disk  = blkdebug_co_flush,
 | 
			
		||||
    .bdrv_co_pwrite_zeroes  = blkdebug_co_pwrite_zeroes,
 | 
			
		||||
    .bdrv_co_pdiscard       = blkdebug_co_pdiscard,
 | 
			
		||||
    .bdrv_aio_readv     = blkdebug_aio_readv,
 | 
			
		||||
    .bdrv_aio_writev    = blkdebug_aio_writev,
 | 
			
		||||
 | 
			
		||||
    .bdrv_debug_event           = blkdebug_debug_event,
 | 
			
		||||
    .bdrv_debug_breakpoint      = blkdebug_debug_breakpoint,
 | 
			
		||||
    .bdrv_debug_remove_breakpoint
 | 
			
		||||
                                = blkdebug_debug_remove_breakpoint,
 | 
			
		||||
    .bdrv_debug_resume          = blkdebug_debug_resume,
 | 
			
		||||
    .bdrv_debug_is_suspended    = blkdebug_debug_is_suspended,
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -1,153 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Block protocol for record/replay
 | 
			
		||||
 *
 | 
			
		||||
 * Copyright (c) 2010-2016 Institute for System Programming
 | 
			
		||||
 *                         of the Russian Academy of Sciences.
 | 
			
		||||
 *
 | 
			
		||||
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "sysemu/replay.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
 | 
			
		||||
typedef struct Request {
 | 
			
		||||
    Coroutine *co;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
} Request;
 | 
			
		||||
 | 
			
		||||
static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                          Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    /* Open the image file */
 | 
			
		||||
    bs->file = bdrv_open_child(NULL, options, "image",
 | 
			
		||||
                               bs, &child_file, false, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
fail:
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkreplay_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t blkreplay_getlength(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    return bdrv_getlength(bs->file->bs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* This bh is used for synchronization of return from coroutines.
 | 
			
		||||
   It continues yielded coroutine which then finishes its execution.
 | 
			
		||||
   BH is called adjusted to some replay checkpoint, therefore
 | 
			
		||||
   record and replay will always finish coroutines deterministically.
 | 
			
		||||
*/
 | 
			
		||||
static void blkreplay_bh_cb(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    Request *req = opaque;
 | 
			
		||||
    aio_co_wake(req->co);
 | 
			
		||||
    qemu_bh_delete(req->bh);
 | 
			
		||||
    g_free(req);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void block_request_create(uint64_t reqid, BlockDriverState *bs,
 | 
			
		||||
                                 Coroutine *co)
 | 
			
		||||
{
 | 
			
		||||
    Request *req = g_new(Request, 1);
 | 
			
		||||
    *req = (Request) {
 | 
			
		||||
        .co = co,
 | 
			
		||||
        .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req),
 | 
			
		||||
    };
 | 
			
		||||
    replay_block_event(req->bh, reqid);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs,
 | 
			
		||||
    uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t reqid = blkreplay_next_id();
 | 
			
		||||
    int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
 | 
			
		||||
    block_request_create(reqid, bs, qemu_coroutine_self());
 | 
			
		||||
    qemu_coroutine_yield();
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs,
 | 
			
		||||
    uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t reqid = blkreplay_next_id();
 | 
			
		||||
    int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
 | 
			
		||||
    block_request_create(reqid, bs, qemu_coroutine_self());
 | 
			
		||||
    qemu_coroutine_yield();
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs,
 | 
			
		||||
    int64_t offset, int count, BdrvRequestFlags flags)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t reqid = blkreplay_next_id();
 | 
			
		||||
    int ret = bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
 | 
			
		||||
    block_request_create(reqid, bs, qemu_coroutine_self());
 | 
			
		||||
    qemu_coroutine_yield();
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs,
 | 
			
		||||
                                              int64_t offset, int count)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t reqid = blkreplay_next_id();
 | 
			
		||||
    int ret = bdrv_co_pdiscard(bs->file->bs, offset, count);
 | 
			
		||||
    block_request_create(reqid, bs, qemu_coroutine_self());
 | 
			
		||||
    qemu_coroutine_yield();
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    uint64_t reqid = blkreplay_next_id();
 | 
			
		||||
    int ret = bdrv_co_flush(bs->file->bs);
 | 
			
		||||
    block_request_create(reqid, bs, qemu_coroutine_self());
 | 
			
		||||
    qemu_coroutine_yield();
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_blkreplay = {
 | 
			
		||||
    .format_name            = "blkreplay",
 | 
			
		||||
    .protocol_name          = "blkreplay",
 | 
			
		||||
    .instance_size          = 0,
 | 
			
		||||
 | 
			
		||||
    .bdrv_file_open         = blkreplay_open,
 | 
			
		||||
    .bdrv_close             = blkreplay_close,
 | 
			
		||||
    .bdrv_child_perm        = bdrv_filter_default_perms,
 | 
			
		||||
    .bdrv_getlength         = blkreplay_getlength,
 | 
			
		||||
 | 
			
		||||
    .bdrv_co_preadv         = blkreplay_co_preadv,
 | 
			
		||||
    .bdrv_co_pwritev        = blkreplay_co_pwritev,
 | 
			
		||||
 | 
			
		||||
    .bdrv_co_pwrite_zeroes  = blkreplay_co_pwrite_zeroes,
 | 
			
		||||
    .bdrv_co_pdiscard       = blkreplay_co_pdiscard,
 | 
			
		||||
    .bdrv_co_flush          = blkreplay_co_flush,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void bdrv_blkreplay_init(void)
 | 
			
		||||
{
 | 
			
		||||
    bdrv_register(&bdrv_blkreplay);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
block_init(bdrv_blkreplay_init);
 | 
			
		||||
@@ -7,48 +7,61 @@
 | 
			
		||||
 * See the COPYING file in the top-level directory.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include <stdarg.h>
 | 
			
		||||
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qapi/qmp/qdict.h"
 | 
			
		||||
#include "qapi/qmp/qstring.h"
 | 
			
		||||
#include "qemu/cutils.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    BdrvChild *test_file;
 | 
			
		||||
    BlockDriverState *test_file;
 | 
			
		||||
} BDRVBlkverifyState;
 | 
			
		||||
 | 
			
		||||
typedef struct BlkverifyRequest {
 | 
			
		||||
    Coroutine *co;
 | 
			
		||||
    BlockDriverState *bs;
 | 
			
		||||
typedef struct BlkverifyAIOCB BlkverifyAIOCB;
 | 
			
		||||
struct BlkverifyAIOCB {
 | 
			
		||||
    BlockDriverAIOCB common;
 | 
			
		||||
    QEMUBH *bh;
 | 
			
		||||
 | 
			
		||||
    /* Request metadata */
 | 
			
		||||
    bool is_write;
 | 
			
		||||
    uint64_t offset;
 | 
			
		||||
    uint64_t bytes;
 | 
			
		||||
    int flags;
 | 
			
		||||
 | 
			
		||||
    int (*request_fn)(BdrvChild *, int64_t, unsigned int, QEMUIOVector *,
 | 
			
		||||
                      BdrvRequestFlags);
 | 
			
		||||
 | 
			
		||||
    int ret;                    /* test image result */
 | 
			
		||||
    int raw_ret;                /* raw image result */
 | 
			
		||||
    int64_t sector_num;
 | 
			
		||||
    int nb_sectors;
 | 
			
		||||
 | 
			
		||||
    int ret;                    /* first completed request's result */
 | 
			
		||||
    unsigned int done;          /* completion counter */
 | 
			
		||||
    bool *finished;             /* completion signal for cancel */
 | 
			
		||||
 | 
			
		||||
    QEMUIOVector *qiov;         /* user I/O vector */
 | 
			
		||||
    QEMUIOVector *raw_qiov;     /* cloned I/O vector for raw file */
 | 
			
		||||
} BlkverifyRequest;
 | 
			
		||||
    QEMUIOVector raw_qiov;      /* cloned I/O vector for raw file */
 | 
			
		||||
    void *buf;                  /* buffer for raw file I/O */
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
 | 
			
		||||
    void (*verify)(BlkverifyAIOCB *acb);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void blkverify_aio_cancel(BlockDriverAIOCB *blockacb)
 | 
			
		||||
{
 | 
			
		||||
    BlkverifyAIOCB *acb = (BlkverifyAIOCB *)blockacb;
 | 
			
		||||
    bool finished = false;
 | 
			
		||||
 | 
			
		||||
    /* Wait until request completes, invokes its callback, and frees itself */
 | 
			
		||||
    acb->finished = &finished;
 | 
			
		||||
    while (!finished) {
 | 
			
		||||
        qemu_aio_wait();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const AIOCBInfo blkverify_aiocb_info = {
 | 
			
		||||
    .aiocb_size         = sizeof(BlkverifyAIOCB),
 | 
			
		||||
    .cancel             = blkverify_aio_cancel,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
 | 
			
		||||
                                             const char *fmt, ...)
 | 
			
		||||
{
 | 
			
		||||
    va_list ap;
 | 
			
		||||
 | 
			
		||||
    va_start(ap, fmt);
 | 
			
		||||
    fprintf(stderr, "blkverify: %s offset=%" PRId64 " bytes=%" PRId64 " ",
 | 
			
		||||
            r->is_write ? "write" : "read", r->offset, r->bytes);
 | 
			
		||||
    fprintf(stderr, "blkverify: %s sector_num=%" PRId64 " nb_sectors=%d ",
 | 
			
		||||
            acb->is_write ? "write" : "read", acb->sector_num,
 | 
			
		||||
            acb->nb_sectors);
 | 
			
		||||
    vfprintf(stderr, fmt, ap);
 | 
			
		||||
    fprintf(stderr, "\n");
 | 
			
		||||
    va_end(ap);
 | 
			
		||||
@@ -56,101 +69,50 @@ static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyRequest *r,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
 | 
			
		||||
static void blkverify_parse_filename(const char *filename, QDict *options,
 | 
			
		||||
                                     Error **errp)
 | 
			
		||||
static int blkverify_open(BlockDriverState *bs, const char *filename, int flags)
 | 
			
		||||
{
 | 
			
		||||
    const char *c;
 | 
			
		||||
    QString *raw_path;
 | 
			
		||||
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
    int ret;
 | 
			
		||||
    char *raw, *c;
 | 
			
		||||
 | 
			
		||||
    /* Parse the blkverify: prefix */
 | 
			
		||||
    if (!strstart(filename, "blkverify:", &filename)) {
 | 
			
		||||
        /* There was no prefix; therefore, all options have to be already
 | 
			
		||||
           present in the QDict (except for the filename) */
 | 
			
		||||
        qdict_put_str(options, "x-image", filename);
 | 
			
		||||
        return;
 | 
			
		||||
    if (strncmp(filename, "blkverify:", strlen("blkverify:"))) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
    filename += strlen("blkverify:");
 | 
			
		||||
 | 
			
		||||
    /* Parse the raw image filename */
 | 
			
		||||
    c = strchr(filename, ':');
 | 
			
		||||
    if (c == NULL) {
 | 
			
		||||
        error_setg(errp, "blkverify requires raw copy and original image path");
 | 
			
		||||
        return;
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* TODO Implement option pass-through and set raw.filename here */
 | 
			
		||||
    raw_path = qstring_from_substr(filename, 0, c - filename - 1);
 | 
			
		||||
    qdict_put(options, "x-raw", raw_path);
 | 
			
		||||
 | 
			
		||||
    /* TODO Allow multi-level nesting and set file.filename here */
 | 
			
		||||
    raw = g_strdup(filename);
 | 
			
		||||
    raw[c - filename] = '\0';
 | 
			
		||||
    ret = bdrv_file_open(&bs->file, raw, flags);
 | 
			
		||||
    g_free(raw);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    filename = c + 1;
 | 
			
		||||
    qdict_put_str(options, "x-image", filename);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static QemuOptsList runtime_opts = {
 | 
			
		||||
    .name = "blkverify",
 | 
			
		||||
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
 | 
			
		||||
    .desc = {
 | 
			
		||||
        {
 | 
			
		||||
            .name = "x-raw",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "[internal use only, will be removed]",
 | 
			
		||||
        },
 | 
			
		||||
        {
 | 
			
		||||
            .name = "x-image",
 | 
			
		||||
            .type = QEMU_OPT_STRING,
 | 
			
		||||
            .help = "[internal use only, will be removed]",
 | 
			
		||||
        },
 | 
			
		||||
        { /* end of list */ }
 | 
			
		||||
    },
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int blkverify_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                          Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
    QemuOpts *opts;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
 | 
			
		||||
    qemu_opts_absorb_qdict(opts, options, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Open the raw file */
 | 
			
		||||
    bs->file = bdrv_open_child(qemu_opt_get(opts, "x-raw"), options, "raw",
 | 
			
		||||
                               bs, &child_file, false, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Open the test file */
 | 
			
		||||
    s->test_file = bdrv_open_child(qemu_opt_get(opts, "x-image"), options,
 | 
			
		||||
                                   "test", bs, &child_format, false,
 | 
			
		||||
                                   &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    s->test_file = bdrv_new("");
 | 
			
		||||
    ret = bdrv_open(s->test_file, filename, flags, NULL);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        bdrv_delete(s->test_file);
 | 
			
		||||
        s->test_file = NULL;
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
fail:
 | 
			
		||||
    qemu_opts_del(opts);
 | 
			
		||||
    return ret;
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkverify_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    bdrv_unref_child(bs, s->test_file);
 | 
			
		||||
    bdrv_delete(s->test_file);
 | 
			
		||||
    s->test_file = NULL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -158,174 +120,242 @@ static int64_t blkverify_getlength(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    return bdrv_getlength(s->test_file->bs);
 | 
			
		||||
    return bdrv_getlength(s->test_file);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coroutine_fn blkverify_do_test_req(void *opaque)
 | 
			
		||||
/**
 | 
			
		||||
 * Check that I/O vector contents are identical
 | 
			
		||||
 *
 | 
			
		||||
 * @a:          I/O vector
 | 
			
		||||
 * @b:          I/O vector
 | 
			
		||||
 * @ret:        Offset to first mismatching byte or -1 if match
 | 
			
		||||
 */
 | 
			
		||||
static ssize_t blkverify_iovec_compare(QEMUIOVector *a, QEMUIOVector *b)
 | 
			
		||||
{
 | 
			
		||||
    BlkverifyRequest *r = opaque;
 | 
			
		||||
    BDRVBlkverifyState *s = r->bs->opaque;
 | 
			
		||||
    int i;
 | 
			
		||||
    ssize_t offset = 0;
 | 
			
		||||
 | 
			
		||||
    r->ret = r->request_fn(s->test_file, r->offset, r->bytes, r->qiov,
 | 
			
		||||
                           r->flags);
 | 
			
		||||
    r->done++;
 | 
			
		||||
    qemu_coroutine_enter_if_inactive(r->co);
 | 
			
		||||
    assert(a->niov == b->niov);
 | 
			
		||||
    for (i = 0; i < a->niov; i++) {
 | 
			
		||||
        size_t len = 0;
 | 
			
		||||
        uint8_t *p = (uint8_t *)a->iov[i].iov_base;
 | 
			
		||||
        uint8_t *q = (uint8_t *)b->iov[i].iov_base;
 | 
			
		||||
 | 
			
		||||
        assert(a->iov[i].iov_len == b->iov[i].iov_len);
 | 
			
		||||
        while (len < a->iov[i].iov_len && *p++ == *q++) {
 | 
			
		||||
            len++;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        offset += len;
 | 
			
		||||
 | 
			
		||||
        if (len != a->iov[i].iov_len) {
 | 
			
		||||
            return offset;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coroutine_fn blkverify_do_raw_req(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BlkverifyRequest *r = opaque;
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int src_index;
 | 
			
		||||
    struct iovec *src_iov;
 | 
			
		||||
    void *dest_base;
 | 
			
		||||
} IOVectorSortElem;
 | 
			
		||||
 | 
			
		||||
    r->raw_ret = r->request_fn(r->bs->file, r->offset, r->bytes, r->raw_qiov,
 | 
			
		||||
                               r->flags);
 | 
			
		||||
    r->done++;
 | 
			
		||||
    qemu_coroutine_enter_if_inactive(r->co);
 | 
			
		||||
static int sortelem_cmp_src_base(const void *a, const void *b)
 | 
			
		||||
{
 | 
			
		||||
    const IOVectorSortElem *elem_a = a;
 | 
			
		||||
    const IOVectorSortElem *elem_b = b;
 | 
			
		||||
 | 
			
		||||
    /* Don't overflow */
 | 
			
		||||
    if (elem_a->src_iov->iov_base < elem_b->src_iov->iov_base) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    } else if (elem_a->src_iov->iov_base > elem_b->src_iov->iov_base) {
 | 
			
		||||
        return 1;
 | 
			
		||||
    } else {
 | 
			
		||||
        return 0;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
blkverify_co_prwv(BlockDriverState *bs, BlkverifyRequest *r, uint64_t offset,
 | 
			
		||||
                  uint64_t bytes, QEMUIOVector *qiov, QEMUIOVector *raw_qiov,
 | 
			
		||||
                  int flags, bool is_write)
 | 
			
		||||
static int sortelem_cmp_src_index(const void *a, const void *b)
 | 
			
		||||
{
 | 
			
		||||
    Coroutine *co_a, *co_b;
 | 
			
		||||
    const IOVectorSortElem *elem_a = a;
 | 
			
		||||
    const IOVectorSortElem *elem_b = b;
 | 
			
		||||
 | 
			
		||||
    *r = (BlkverifyRequest) {
 | 
			
		||||
        .co         = qemu_coroutine_self(),
 | 
			
		||||
        .bs         = bs,
 | 
			
		||||
        .offset     = offset,
 | 
			
		||||
        .bytes      = bytes,
 | 
			
		||||
        .qiov       = qiov,
 | 
			
		||||
        .raw_qiov   = raw_qiov,
 | 
			
		||||
        .flags      = flags,
 | 
			
		||||
        .is_write   = is_write,
 | 
			
		||||
        .request_fn = is_write ? bdrv_co_pwritev : bdrv_co_preadv,
 | 
			
		||||
    };
 | 
			
		||||
    return elem_a->src_index - elem_b->src_index;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    co_a = qemu_coroutine_create(blkverify_do_test_req, r);
 | 
			
		||||
    co_b = qemu_coroutine_create(blkverify_do_raw_req, r);
 | 
			
		||||
/**
 | 
			
		||||
 * Copy contents of I/O vector
 | 
			
		||||
 *
 | 
			
		||||
 * The relative relationships of overlapping iovecs are preserved.  This is
 | 
			
		||||
 * necessary to ensure identical semantics in the cloned I/O vector.
 | 
			
		||||
 */
 | 
			
		||||
static void blkverify_iovec_clone(QEMUIOVector *dest, const QEMUIOVector *src,
 | 
			
		||||
                                  void *buf)
 | 
			
		||||
{
 | 
			
		||||
    IOVectorSortElem sortelems[src->niov];
 | 
			
		||||
    void *last_end;
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    qemu_coroutine_enter(co_a);
 | 
			
		||||
    qemu_coroutine_enter(co_b);
 | 
			
		||||
    /* Sort by source iovecs by base address */
 | 
			
		||||
    for (i = 0; i < src->niov; i++) {
 | 
			
		||||
        sortelems[i].src_index = i;
 | 
			
		||||
        sortelems[i].src_iov = &src->iov[i];
 | 
			
		||||
    }
 | 
			
		||||
    qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_base);
 | 
			
		||||
 | 
			
		||||
    while (r->done < 2) {
 | 
			
		||||
        qemu_coroutine_yield();
 | 
			
		||||
    /* Allocate buffer space taking into account overlapping iovecs */
 | 
			
		||||
    last_end = NULL;
 | 
			
		||||
    for (i = 0; i < src->niov; i++) {
 | 
			
		||||
        struct iovec *cur = sortelems[i].src_iov;
 | 
			
		||||
        ptrdiff_t rewind = 0;
 | 
			
		||||
 | 
			
		||||
        /* Detect overlap */
 | 
			
		||||
        if (last_end && last_end > cur->iov_base) {
 | 
			
		||||
            rewind = last_end - cur->iov_base;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        sortelems[i].dest_base = buf - rewind;
 | 
			
		||||
        buf += cur->iov_len - MIN(rewind, cur->iov_len);
 | 
			
		||||
        last_end = MAX(cur->iov_base + cur->iov_len, last_end);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (r->ret != r->raw_ret) {
 | 
			
		||||
        blkverify_err(r, "return value mismatch %d != %d", r->ret, r->raw_ret);
 | 
			
		||||
    /* Sort by source iovec index and build destination iovec */
 | 
			
		||||
    qsort(sortelems, src->niov, sizeof(sortelems[0]), sortelem_cmp_src_index);
 | 
			
		||||
    for (i = 0; i < src->niov; i++) {
 | 
			
		||||
        qemu_iovec_add(dest, sortelems[i].dest_base, src->iov[i].iov_len);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return r->ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
blkverify_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 | 
			
		||||
                    QEMUIOVector *qiov, int flags)
 | 
			
		||||
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
 | 
			
		||||
                                         int64_t sector_num, QEMUIOVector *qiov,
 | 
			
		||||
                                         int nb_sectors,
 | 
			
		||||
                                         BlockDriverCompletionFunc *cb,
 | 
			
		||||
                                         void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BlkverifyRequest r;
 | 
			
		||||
    QEMUIOVector raw_qiov;
 | 
			
		||||
    void *buf;
 | 
			
		||||
    ssize_t cmp_offset;
 | 
			
		||||
    int ret;
 | 
			
		||||
    BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
 | 
			
		||||
 | 
			
		||||
    buf = qemu_blockalign(bs->file->bs, qiov->size);
 | 
			
		||||
    qemu_iovec_init(&raw_qiov, qiov->niov);
 | 
			
		||||
    qemu_iovec_clone(&raw_qiov, qiov, buf);
 | 
			
		||||
    acb->bh = NULL;
 | 
			
		||||
    acb->is_write = is_write;
 | 
			
		||||
    acb->sector_num = sector_num;
 | 
			
		||||
    acb->nb_sectors = nb_sectors;
 | 
			
		||||
    acb->ret = -EINPROGRESS;
 | 
			
		||||
    acb->done = 0;
 | 
			
		||||
    acb->qiov = qiov;
 | 
			
		||||
    acb->buf = NULL;
 | 
			
		||||
    acb->verify = NULL;
 | 
			
		||||
    acb->finished = NULL;
 | 
			
		||||
    return acb;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    ret = blkverify_co_prwv(bs, &r, offset, bytes, qiov, &raw_qiov, flags,
 | 
			
		||||
                            false);
 | 
			
		||||
static void blkverify_aio_bh(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BlkverifyAIOCB *acb = opaque;
 | 
			
		||||
 | 
			
		||||
    cmp_offset = qemu_iovec_compare(qiov, &raw_qiov);
 | 
			
		||||
    if (cmp_offset != -1) {
 | 
			
		||||
        blkverify_err(&r, "contents mismatch at offset %" PRId64,
 | 
			
		||||
                      offset + cmp_offset);
 | 
			
		||||
    qemu_bh_delete(acb->bh);
 | 
			
		||||
    if (acb->buf) {
 | 
			
		||||
        qemu_iovec_destroy(&acb->raw_qiov);
 | 
			
		||||
        qemu_vfree(acb->buf);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_destroy(&raw_qiov);
 | 
			
		||||
    qemu_vfree(buf);
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
    acb->common.cb(acb->common.opaque, acb->ret);
 | 
			
		||||
    if (acb->finished) {
 | 
			
		||||
        *acb->finished = true;
 | 
			
		||||
    }
 | 
			
		||||
    qemu_aio_release(acb);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
blkverify_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 | 
			
		||||
                     QEMUIOVector *qiov, int flags)
 | 
			
		||||
static void blkverify_aio_cb(void *opaque, int ret)
 | 
			
		||||
{
 | 
			
		||||
    BlkverifyRequest r;
 | 
			
		||||
    return blkverify_co_prwv(bs, &r, offset, bytes, qiov, qiov, flags, true);
 | 
			
		||||
    BlkverifyAIOCB *acb = opaque;
 | 
			
		||||
 | 
			
		||||
    switch (++acb->done) {
 | 
			
		||||
    case 1:
 | 
			
		||||
        acb->ret = ret;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
    case 2:
 | 
			
		||||
        if (acb->ret != ret) {
 | 
			
		||||
            blkverify_err(acb, "return value mismatch %d != %d", acb->ret, ret);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (acb->verify) {
 | 
			
		||||
            acb->verify(acb);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        acb->bh = qemu_bh_new(blkverify_aio_bh, acb);
 | 
			
		||||
        qemu_bh_schedule(acb->bh);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int blkverify_co_flush(BlockDriverState *bs)
 | 
			
		||||
static void blkverify_verify_readv(BlkverifyAIOCB *acb)
 | 
			
		||||
{
 | 
			
		||||
    ssize_t offset = blkverify_iovec_compare(acb->qiov, &acb->raw_qiov);
 | 
			
		||||
    if (offset != -1) {
 | 
			
		||||
        blkverify_err(acb, "contents mismatch in sector %" PRId64,
 | 
			
		||||
                      acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
 | 
			
		||||
        BlockDriverCompletionFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
    BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
 | 
			
		||||
                                            nb_sectors, cb, opaque);
 | 
			
		||||
 | 
			
		||||
    acb->verify = blkverify_verify_readv;
 | 
			
		||||
    acb->buf = qemu_blockalign(bs->file, qiov->size);
 | 
			
		||||
    qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
 | 
			
		||||
    blkverify_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
 | 
			
		||||
 | 
			
		||||
    bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
 | 
			
		||||
                   blkverify_aio_cb, acb);
 | 
			
		||||
    bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
 | 
			
		||||
                   blkverify_aio_cb, acb);
 | 
			
		||||
    return &acb->common;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs,
 | 
			
		||||
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
 | 
			
		||||
        BlockDriverCompletionFunc *cb, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
    BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
 | 
			
		||||
                                            nb_sectors, cb, opaque);
 | 
			
		||||
 | 
			
		||||
    bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
 | 
			
		||||
                    blkverify_aio_cb, acb);
 | 
			
		||||
    bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
 | 
			
		||||
                    blkverify_aio_cb, acb);
 | 
			
		||||
    return &acb->common;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs,
 | 
			
		||||
                                             BlockDriverCompletionFunc *cb,
 | 
			
		||||
                                             void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    /* Only flush test file, the raw file is not important */
 | 
			
		||||
    return bdrv_co_flush(s->test_file->bs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static bool blkverify_recurse_is_first_non_filter(BlockDriverState *bs,
 | 
			
		||||
                                                  BlockDriverState *candidate)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    bool perm = bdrv_recurse_is_first_non_filter(bs->file->bs, candidate);
 | 
			
		||||
 | 
			
		||||
    if (perm) {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bdrv_recurse_is_first_non_filter(s->test_file->bs, candidate);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void blkverify_refresh_filename(BlockDriverState *bs, QDict *options)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBlkverifyState *s = bs->opaque;
 | 
			
		||||
 | 
			
		||||
    /* bs->file->bs has already been refreshed */
 | 
			
		||||
    bdrv_refresh_filename(s->test_file->bs);
 | 
			
		||||
 | 
			
		||||
    if (bs->file->bs->full_open_options
 | 
			
		||||
        && s->test_file->bs->full_open_options)
 | 
			
		||||
    {
 | 
			
		||||
        QDict *opts = qdict_new();
 | 
			
		||||
        qdict_put_str(opts, "driver", "blkverify");
 | 
			
		||||
 | 
			
		||||
        QINCREF(bs->file->bs->full_open_options);
 | 
			
		||||
        qdict_put(opts, "raw", bs->file->bs->full_open_options);
 | 
			
		||||
        QINCREF(s->test_file->bs->full_open_options);
 | 
			
		||||
        qdict_put(opts, "test", s->test_file->bs->full_open_options);
 | 
			
		||||
 | 
			
		||||
        bs->full_open_options = opts;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bs->file->bs->exact_filename[0]
 | 
			
		||||
        && s->test_file->bs->exact_filename[0])
 | 
			
		||||
    {
 | 
			
		||||
        snprintf(bs->exact_filename, sizeof(bs->exact_filename),
 | 
			
		||||
                 "blkverify:%s:%s",
 | 
			
		||||
                 bs->file->bs->exact_filename,
 | 
			
		||||
                 s->test_file->bs->exact_filename);
 | 
			
		||||
    }
 | 
			
		||||
    return bdrv_aio_flush(s->test_file, cb, opaque);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static BlockDriver bdrv_blkverify = {
 | 
			
		||||
    .format_name                      = "blkverify",
 | 
			
		||||
    .protocol_name                    = "blkverify",
 | 
			
		||||
    .instance_size                    = sizeof(BDRVBlkverifyState),
 | 
			
		||||
    .format_name        = "blkverify",
 | 
			
		||||
    .protocol_name      = "blkverify",
 | 
			
		||||
 | 
			
		||||
    .bdrv_parse_filename              = blkverify_parse_filename,
 | 
			
		||||
    .bdrv_file_open                   = blkverify_open,
 | 
			
		||||
    .bdrv_close                       = blkverify_close,
 | 
			
		||||
    .bdrv_child_perm                  = bdrv_filter_default_perms,
 | 
			
		||||
    .bdrv_getlength                   = blkverify_getlength,
 | 
			
		||||
    .bdrv_refresh_filename            = blkverify_refresh_filename,
 | 
			
		||||
    .instance_size      = sizeof(BDRVBlkverifyState),
 | 
			
		||||
 | 
			
		||||
    .bdrv_co_preadv                   = blkverify_co_preadv,
 | 
			
		||||
    .bdrv_co_pwritev                  = blkverify_co_pwritev,
 | 
			
		||||
    .bdrv_co_flush                    = blkverify_co_flush,
 | 
			
		||||
    .bdrv_getlength     = blkverify_getlength,
 | 
			
		||||
 | 
			
		||||
    .is_filter                        = true,
 | 
			
		||||
    .bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
 | 
			
		||||
    .bdrv_file_open     = blkverify_open,
 | 
			
		||||
    .bdrv_close         = blkverify_close,
 | 
			
		||||
 | 
			
		||||
    .bdrv_aio_readv     = blkverify_aio_readv,
 | 
			
		||||
    .bdrv_aio_writev    = blkverify_aio_writev,
 | 
			
		||||
    .bdrv_aio_flush     = blkverify_aio_flush,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static void bdrv_blkverify_init(void)
 | 
			
		||||
 
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										132
									
								
								block/bochs.c
									
									
									
									
									
								
							
							
						
						
									
										132
									
								
								block/bochs.c
									
									
									
									
									
								
							@@ -22,12 +22,9 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include "qemu/bswap.h"
 | 
			
		||||
 | 
			
		||||
/**************************************************************/
 | 
			
		||||
 | 
			
		||||
@@ -96,24 +93,14 @@ static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                      Error **errp)
 | 
			
		||||
static int bochs_open(BlockDriverState *bs, int flags)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBochsState *s = bs->opaque;
 | 
			
		||||
    uint32_t i;
 | 
			
		||||
    struct bochs_header bochs;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
 | 
			
		||||
                               false, errp);
 | 
			
		||||
    if (!bs->file) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_set_read_only(bs, true, errp); /* no write support yet */
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    bs->read_only = 1; // no write support yet
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pread(bs->file, 0, &bochs, sizeof(bochs));
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
@@ -125,8 +112,7 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        strcmp(bochs.subtype, GROWING_TYPE) ||
 | 
			
		||||
	((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
 | 
			
		||||
	(le32_to_cpu(bochs.version) != HEADER_V1))) {
 | 
			
		||||
        error_setg(errp, "Image not in Bochs format");
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
        return -EMEDIUMTYPE;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (le32_to_cpu(bochs.version) == HEADER_V1) {
 | 
			
		||||
@@ -139,15 +125,12 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
     * needed for the largest image that bximage can create (~8 TB). */
 | 
			
		||||
    s->catalog_size = le32_to_cpu(bochs.catalog);
 | 
			
		||||
    if (s->catalog_size > 0x100000) {
 | 
			
		||||
        error_setg(errp, "Catalog size is too large");
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "Catalog size is too large");
 | 
			
		||||
        return -EFBIG;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->catalog_bitmap = g_try_new(uint32_t, s->catalog_size);
 | 
			
		||||
    if (s->catalog_size && s->catalog_bitmap == NULL) {
 | 
			
		||||
        error_setg(errp, "Could not allocate memory for catalog");
 | 
			
		||||
        return -ENOMEM;
 | 
			
		||||
    }
 | 
			
		||||
    s->catalog_bitmap = g_malloc(s->catalog_size * 4);
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
 | 
			
		||||
                     s->catalog_size * 4);
 | 
			
		||||
@@ -164,27 +147,20 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
 | 
			
		||||
 | 
			
		||||
    s->extent_size = le32_to_cpu(bochs.extent);
 | 
			
		||||
    if (s->extent_size < BDRV_SECTOR_SIZE) {
 | 
			
		||||
        /* bximage actually never creates extents smaller than 4k */
 | 
			
		||||
        error_setg(errp, "Extent size must be at least 512");
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    } else if (!is_power_of_2(s->extent_size)) {
 | 
			
		||||
        error_setg(errp, "Extent size %" PRIu32 " is not a power of two",
 | 
			
		||||
                   s->extent_size);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    if (s->extent_size == 0) {
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "Extent size may not be zero");
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    } else if (s->extent_size > 0x800000) {
 | 
			
		||||
        error_setg(errp, "Extent size %" PRIu32 " is too large",
 | 
			
		||||
                   s->extent_size);
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "Extent size %" PRIu32 " is too large",
 | 
			
		||||
                      s->extent_size);
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (s->catalog_size < DIV_ROUND_UP(bs->total_sectors,
 | 
			
		||||
                                       s->extent_size / BDRV_SECTOR_SIZE))
 | 
			
		||||
    {
 | 
			
		||||
        error_setg(errp, "Catalog size is too small for this disk size");
 | 
			
		||||
    if (s->catalog_size < bs->total_sectors / s->extent_size) {
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "Catalog size is too small for this disk size");
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
@@ -197,25 +173,19 @@ fail:
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void bochs_refresh_limits(BlockDriverState *bs, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBochsState *s = bs->opaque;
 | 
			
		||||
    uint64_t offset = sector_num * 512;
 | 
			
		||||
    uint64_t extent_index, extent_offset, bitmap_offset;
 | 
			
		||||
    char bitmap_entry;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    // seek to sector
 | 
			
		||||
    extent_index = offset / s->extent_size;
 | 
			
		||||
    extent_offset = (offset % s->extent_size) / 512;
 | 
			
		||||
 | 
			
		||||
    if (s->catalog_bitmap[extent_index] == 0xffffffff) {
 | 
			
		||||
	return 0; /* not allocated */
 | 
			
		||||
	return -1; /* not allocated */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bitmap_offset = s->data_offset +
 | 
			
		||||
@@ -223,65 +193,47 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 | 
			
		||||
        (s->extent_blocks + s->bitmap_blocks));
 | 
			
		||||
 | 
			
		||||
    /* read in bitmap for current extent */
 | 
			
		||||
    ret = bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
 | 
			
		||||
                     &bitmap_entry, 1);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
 | 
			
		||||
                   &bitmap_entry, 1) != 1) {
 | 
			
		||||
        return -1;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!((bitmap_entry >> (extent_offset % 8)) & 1)) {
 | 
			
		||||
	return 0; /* not allocated */
 | 
			
		||||
	return -1; /* not allocated */
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
bochs_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 | 
			
		||||
                QEMUIOVector *qiov, int flags)
 | 
			
		||||
static int bochs_read(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                    uint8_t *buf, int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    BDRVBochsState *s = bs->opaque;
 | 
			
		||||
    uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
 | 
			
		||||
    int nb_sectors = bytes >> BDRV_SECTOR_BITS;
 | 
			
		||||
    uint64_t bytes_done = 0;
 | 
			
		||||
    QEMUIOVector local_qiov;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
 | 
			
		||||
    assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_init(&local_qiov, qiov->niov);
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
 | 
			
		||||
    while (nb_sectors > 0) {
 | 
			
		||||
        int64_t block_offset = seek_to_sector(bs, sector_num);
 | 
			
		||||
        if (block_offset < 0) {
 | 
			
		||||
            ret = block_offset;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        qemu_iovec_reset(&local_qiov);
 | 
			
		||||
        qemu_iovec_concat(&local_qiov, qiov, bytes_done, 512);
 | 
			
		||||
 | 
			
		||||
        if (block_offset > 0) {
 | 
			
		||||
            ret = bdrv_co_preadv(bs->file, block_offset, 512,
 | 
			
		||||
                                 &local_qiov, 0);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                goto fail;
 | 
			
		||||
        if (block_offset >= 0) {
 | 
			
		||||
            ret = bdrv_pread(bs->file, block_offset, buf, 512);
 | 
			
		||||
            if (ret != 512) {
 | 
			
		||||
                return -1;
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            qemu_iovec_memset(&local_qiov, 0, 0, 512);
 | 
			
		||||
        }
 | 
			
		||||
        } else
 | 
			
		||||
            memset(buf, 0, 512);
 | 
			
		||||
        nb_sectors--;
 | 
			
		||||
        sector_num++;
 | 
			
		||||
        bytes_done += 512;
 | 
			
		||||
        buf += 512;
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
fail:
 | 
			
		||||
static coroutine_fn int bochs_co_read(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                                      uint8_t *buf, int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    BDRVBochsState *s = bs->opaque;
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
    ret = bochs_read(bs, sector_num, buf, nb_sectors);
 | 
			
		||||
    qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
    qemu_iovec_destroy(&local_qiov);
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -296,9 +248,7 @@ static BlockDriver bdrv_bochs = {
 | 
			
		||||
    .instance_size	= sizeof(BDRVBochsState),
 | 
			
		||||
    .bdrv_probe		= bochs_probe,
 | 
			
		||||
    .bdrv_open		= bochs_open,
 | 
			
		||||
    .bdrv_child_perm     = bdrv_format_default_perms,
 | 
			
		||||
    .bdrv_refresh_limits = bochs_refresh_limits,
 | 
			
		||||
    .bdrv_co_preadv = bochs_co_preadv,
 | 
			
		||||
    .bdrv_read          = bochs_co_read,
 | 
			
		||||
    .bdrv_close		= bochs_close,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										125
									
								
								block/cloop.c
									
									
									
									
									
								
							
							
						
						
									
										125
									
								
								block/cloop.c
									
									
									
									
									
								
							@@ -21,12 +21,9 @@
 | 
			
		||||
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
 * THE SOFTWARE.
 | 
			
		||||
 */
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qemu-common.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "qemu/module.h"
 | 
			
		||||
#include "qemu/bswap.h"
 | 
			
		||||
#include <zlib.h>
 | 
			
		||||
 | 
			
		||||
/* Maximum compressed block size */
 | 
			
		||||
@@ -59,23 +56,13 @@ static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
                      Error **errp)
 | 
			
		||||
static int cloop_open(BlockDriverState *bs, int flags)
 | 
			
		||||
{
 | 
			
		||||
    BDRVCloopState *s = bs->opaque;
 | 
			
		||||
    uint32_t offsets_size, max_compressed_block_size = 1, i;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file,
 | 
			
		||||
                               false, errp);
 | 
			
		||||
    if (!bs->file) {
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_set_read_only(bs, true, errp);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
    bs->read_only = 1;
 | 
			
		||||
 | 
			
		||||
    /* read header */
 | 
			
		||||
    ret = bdrv_pread(bs->file, 128, &s->block_size, 4);
 | 
			
		||||
@@ -84,12 +71,14 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    }
 | 
			
		||||
    s->block_size = be32_to_cpu(s->block_size);
 | 
			
		||||
    if (s->block_size % 512) {
 | 
			
		||||
        error_setg(errp, "block_size %" PRIu32 " must be a multiple of 512",
 | 
			
		||||
                   s->block_size);
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "block_size %u must be a multiple of 512",
 | 
			
		||||
                      s->block_size);
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
    if (s->block_size == 0) {
 | 
			
		||||
        error_setg(errp, "block_size cannot be zero");
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "block_size cannot be zero");
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -98,9 +87,10 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
     * need a buffer this big.
 | 
			
		||||
     */
 | 
			
		||||
    if (s->block_size > MAX_BLOCK_SIZE) {
 | 
			
		||||
        error_setg(errp, "block_size %" PRIu32 " must be %u MB or less",
 | 
			
		||||
                   s->block_size,
 | 
			
		||||
                   MAX_BLOCK_SIZE / (1024 * 1024));
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "block_size %u must be %u MB or less",
 | 
			
		||||
                      s->block_size,
 | 
			
		||||
                      MAX_BLOCK_SIZE / (1024 * 1024));
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -113,9 +103,10 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    /* read offsets */
 | 
			
		||||
    if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
 | 
			
		||||
        /* Prevent integer overflow */
 | 
			
		||||
        error_setg(errp, "n_blocks %" PRIu32 " must be %zu or less",
 | 
			
		||||
                   s->n_blocks,
 | 
			
		||||
                   (UINT32_MAX - 1) / sizeof(uint64_t));
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "n_blocks %u must be %zu or less",
 | 
			
		||||
                      s->n_blocks,
 | 
			
		||||
                      (UINT32_MAX - 1) / sizeof(uint64_t));
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
    offsets_size = (s->n_blocks + 1) * sizeof(uint64_t);
 | 
			
		||||
@@ -124,16 +115,12 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
         * fail or overflows bdrv_pread() size.  In practice the 512 MB
 | 
			
		||||
         * offsets[] limit supports 16 TB images at 256 KB block size.
 | 
			
		||||
         */
 | 
			
		||||
        error_setg(errp, "image requires too many offsets, "
 | 
			
		||||
                   "try increasing block size");
 | 
			
		||||
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                      "image requires too many offsets, "
 | 
			
		||||
                      "try increasing block size");
 | 
			
		||||
        return -EINVAL;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->offsets = g_try_malloc(offsets_size);
 | 
			
		||||
    if (s->offsets == NULL) {
 | 
			
		||||
        error_setg(errp, "Could not allocate offsets table");
 | 
			
		||||
        return -ENOMEM;
 | 
			
		||||
    }
 | 
			
		||||
    s->offsets = g_malloc(offsets_size);
 | 
			
		||||
 | 
			
		||||
    ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
@@ -149,8 +136,9 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (s->offsets[i] < s->offsets[i - 1]) {
 | 
			
		||||
            error_setg(errp, "offsets not monotonically increasing at "
 | 
			
		||||
                       "index %" PRIu32 ", image file is corrupt", i);
 | 
			
		||||
            qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                          "offsets not monotonically increasing at "
 | 
			
		||||
                          "index %u, image file is corrupt", i);
 | 
			
		||||
            ret = -EINVAL;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
@@ -163,8 +151,9 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
         * ridiculous s->compressed_block allocation.
 | 
			
		||||
         */
 | 
			
		||||
        if (size > 2 * MAX_BLOCK_SIZE) {
 | 
			
		||||
            error_setg(errp, "invalid compressed block size at index %" PRIu32
 | 
			
		||||
                       ", image file is corrupt", i);
 | 
			
		||||
            qerror_report(ERROR_CLASS_GENERIC_ERROR,
 | 
			
		||||
                          "invalid compressed block size at index %u, "
 | 
			
		||||
                          "image file is corrupt", i);
 | 
			
		||||
            ret = -EINVAL;
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
@@ -175,20 +164,8 @@ static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* initialize zlib engine */
 | 
			
		||||
    s->compressed_block = g_try_malloc(max_compressed_block_size + 1);
 | 
			
		||||
    if (s->compressed_block == NULL) {
 | 
			
		||||
        error_setg(errp, "Could not allocate compressed_block");
 | 
			
		||||
        ret = -ENOMEM;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->uncompressed_block = g_try_malloc(s->block_size);
 | 
			
		||||
    if (s->uncompressed_block == NULL) {
 | 
			
		||||
        error_setg(errp, "Could not allocate uncompressed_block");
 | 
			
		||||
        ret = -ENOMEM;
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->compressed_block = g_malloc(max_compressed_block_size + 1);
 | 
			
		||||
    s->uncompressed_block = g_malloc(s->block_size);
 | 
			
		||||
    if (inflateInit(&s->zstream) != Z_OK) {
 | 
			
		||||
        ret = -EINVAL;
 | 
			
		||||
        goto fail;
 | 
			
		||||
@@ -207,11 +184,6 @@ fail:
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void cloop_refresh_limits(BlockDriverState *bs, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static inline int cloop_read_block(BlockDriverState *bs, int block_num)
 | 
			
		||||
{
 | 
			
		||||
    BDRVCloopState *s = bs->opaque;
 | 
			
		||||
@@ -220,8 +192,8 @@ static inline int cloop_read_block(BlockDriverState *bs, int block_num)
 | 
			
		||||
        int ret;
 | 
			
		||||
        uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
 | 
			
		||||
 | 
			
		||||
        ret = bdrv_pread(bs->file, s->offsets[block_num],
 | 
			
		||||
                         s->compressed_block, bytes);
 | 
			
		||||
        ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
 | 
			
		||||
                         bytes);
 | 
			
		||||
        if (ret != bytes) {
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
@@ -244,38 +216,33 @@ static inline int cloop_read_block(BlockDriverState *bs, int block_num)
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn
 | 
			
		||||
cloop_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
 | 
			
		||||
                QEMUIOVector *qiov, int flags)
 | 
			
		||||
static int cloop_read(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                    uint8_t *buf, int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    BDRVCloopState *s = bs->opaque;
 | 
			
		||||
    uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
 | 
			
		||||
    int nb_sectors = bytes >> BDRV_SECTOR_BITS;
 | 
			
		||||
    int ret, i;
 | 
			
		||||
 | 
			
		||||
    assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0);
 | 
			
		||||
    assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
 | 
			
		||||
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
    int i;
 | 
			
		||||
 | 
			
		||||
    for (i = 0; i < nb_sectors; i++) {
 | 
			
		||||
        void *data;
 | 
			
		||||
        uint32_t sector_offset_in_block =
 | 
			
		||||
            ((sector_num + i) % s->sectors_per_block),
 | 
			
		||||
            block_num = (sector_num + i) / s->sectors_per_block;
 | 
			
		||||
        if (cloop_read_block(bs, block_num) != 0) {
 | 
			
		||||
            ret = -EIO;
 | 
			
		||||
            goto fail;
 | 
			
		||||
            return -1;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        data = s->uncompressed_block + sector_offset_in_block * 512;
 | 
			
		||||
        qemu_iovec_from_buf(qiov, i * 512, data, 512);
 | 
			
		||||
        memcpy(buf + i * 512,
 | 
			
		||||
            s->uncompressed_block + sector_offset_in_block * 512, 512);
 | 
			
		||||
    }
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
fail:
 | 
			
		||||
static coroutine_fn int cloop_co_read(BlockDriverState *bs, int64_t sector_num,
 | 
			
		||||
                                      uint8_t *buf, int nb_sectors)
 | 
			
		||||
{
 | 
			
		||||
    int ret;
 | 
			
		||||
    BDRVCloopState *s = bs->opaque;
 | 
			
		||||
    qemu_co_mutex_lock(&s->lock);
 | 
			
		||||
    ret = cloop_read(bs, sector_num, buf, nb_sectors);
 | 
			
		||||
    qemu_co_mutex_unlock(&s->lock);
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -293,9 +260,7 @@ static BlockDriver bdrv_cloop = {
 | 
			
		||||
    .instance_size  = sizeof(BDRVCloopState),
 | 
			
		||||
    .bdrv_probe     = cloop_probe,
 | 
			
		||||
    .bdrv_open      = cloop_open,
 | 
			
		||||
    .bdrv_child_perm     = bdrv_format_default_perms,
 | 
			
		||||
    .bdrv_refresh_limits = cloop_refresh_limits,
 | 
			
		||||
    .bdrv_co_preadv = cloop_co_preadv,
 | 
			
		||||
    .bdrv_read      = cloop_co_read,
 | 
			
		||||
    .bdrv_close     = cloop_close,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										511
									
								
								block/commit.c
									
									
									
									
									
								
							
							
						
						
									
										511
									
								
								block/commit.c
									
									
									
									
									
								
							@@ -12,15 +12,10 @@
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "qemu/osdep.h"
 | 
			
		||||
#include "qemu/cutils.h"
 | 
			
		||||
#include "trace.h"
 | 
			
		||||
#include "block/block_int.h"
 | 
			
		||||
#include "block/blockjob_int.h"
 | 
			
		||||
#include "qapi/error.h"
 | 
			
		||||
#include "qapi/qmp/qerror.h"
 | 
			
		||||
#include "block/blockjob.h"
 | 
			
		||||
#include "qemu/ratelimit.h"
 | 
			
		||||
#include "sysemu/block-backend.h"
 | 
			
		||||
 | 
			
		||||
enum {
 | 
			
		||||
    /*
 | 
			
		||||
@@ -37,162 +32,102 @@ typedef struct CommitBlockJob {
 | 
			
		||||
    BlockJob common;
 | 
			
		||||
    RateLimit limit;
 | 
			
		||||
    BlockDriverState *active;
 | 
			
		||||
    BlockDriverState *commit_top_bs;
 | 
			
		||||
    BlockBackend *top;
 | 
			
		||||
    BlockBackend *base;
 | 
			
		||||
    BlockDriverState *top;
 | 
			
		||||
    BlockDriverState *base;
 | 
			
		||||
    BlockdevOnError on_error;
 | 
			
		||||
    int base_flags;
 | 
			
		||||
    int orig_overlay_flags;
 | 
			
		||||
    char *backing_file_str;
 | 
			
		||||
} CommitBlockJob;
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn commit_populate(BlockBackend *bs, BlockBackend *base,
 | 
			
		||||
static int coroutine_fn commit_populate(BlockDriverState *bs,
 | 
			
		||||
                                        BlockDriverState *base,
 | 
			
		||||
                                        int64_t sector_num, int nb_sectors,
 | 
			
		||||
                                        void *buf)
 | 
			
		||||
{
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    QEMUIOVector qiov;
 | 
			
		||||
    struct iovec iov = {
 | 
			
		||||
        .iov_base = buf,
 | 
			
		||||
        .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    qemu_iovec_init_external(&qiov, &iov, 1);
 | 
			
		||||
 | 
			
		||||
    ret = blk_co_preadv(bs, sector_num * BDRV_SECTOR_SIZE,
 | 
			
		||||
                        qiov.size, &qiov, 0);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = blk_co_pwritev(base, sector_num * BDRV_SECTOR_SIZE,
 | 
			
		||||
                         qiov.size, &qiov, 0);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
    ret = bdrv_write(base, sector_num, buf, nb_sectors);
 | 
			
		||||
    if (ret) {
 | 
			
		||||
        return ret;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    int ret;
 | 
			
		||||
} CommitCompleteData;
 | 
			
		||||
 | 
			
		||||
static void commit_complete(BlockJob *job, void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    CommitBlockJob *s = container_of(job, CommitBlockJob, common);
 | 
			
		||||
    CommitCompleteData *data = opaque;
 | 
			
		||||
    BlockDriverState *active = s->active;
 | 
			
		||||
    BlockDriverState *top = blk_bs(s->top);
 | 
			
		||||
    BlockDriverState *base = blk_bs(s->base);
 | 
			
		||||
    BlockDriverState *overlay_bs = bdrv_find_overlay(active, s->commit_top_bs);
 | 
			
		||||
    int ret = data->ret;
 | 
			
		||||
    bool remove_commit_top_bs = false;
 | 
			
		||||
 | 
			
		||||
    /* Make sure overlay_bs and top stay around until bdrv_set_backing_hd() */
 | 
			
		||||
    bdrv_ref(top);
 | 
			
		||||
    bdrv_ref(overlay_bs);
 | 
			
		||||
 | 
			
		||||
    /* Remove base node parent that still uses BLK_PERM_WRITE/RESIZE before
 | 
			
		||||
     * the normal backing chain can be restored. */
 | 
			
		||||
    blk_unref(s->base);
 | 
			
		||||
 | 
			
		||||
    if (!block_job_is_cancelled(&s->common) && ret == 0) {
 | 
			
		||||
        /* success */
 | 
			
		||||
        ret = bdrv_drop_intermediate(active, s->commit_top_bs, base,
 | 
			
		||||
                                     s->backing_file_str);
 | 
			
		||||
    } else if (overlay_bs) {
 | 
			
		||||
        /* XXX Can (or should) we somehow keep 'consistent read' blocked even
 | 
			
		||||
         * after the failed/cancelled commit job is gone? If we already wrote
 | 
			
		||||
         * something to base, the intermediate images aren't valid any more. */
 | 
			
		||||
        remove_commit_top_bs = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* restore base open flags here if appropriate (e.g., change the base back
 | 
			
		||||
     * to r/o). These reopens do not need to be atomic, since we won't abort
 | 
			
		||||
     * even on failure here */
 | 
			
		||||
    if (s->base_flags != bdrv_get_flags(base)) {
 | 
			
		||||
        bdrv_reopen(base, s->base_flags, NULL);
 | 
			
		||||
    }
 | 
			
		||||
    if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
 | 
			
		||||
        bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
 | 
			
		||||
    }
 | 
			
		||||
    g_free(s->backing_file_str);
 | 
			
		||||
    blk_unref(s->top);
 | 
			
		||||
    block_job_completed(&s->common, ret);
 | 
			
		||||
    g_free(data);
 | 
			
		||||
 | 
			
		||||
    /* If bdrv_drop_intermediate() didn't already do that, remove the commit
 | 
			
		||||
     * filter driver from the backing chain. Do this as the final step so that
 | 
			
		||||
     * the 'consistent read' permission can be granted.  */
 | 
			
		||||
    if (remove_commit_top_bs) {
 | 
			
		||||
        bdrv_set_backing_hd(overlay_bs, top, &error_abort);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    bdrv_unref(overlay_bs);
 | 
			
		||||
    bdrv_unref(top);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void coroutine_fn commit_run(void *opaque)
 | 
			
		||||
{
 | 
			
		||||
    CommitBlockJob *s = opaque;
 | 
			
		||||
    CommitCompleteData *data;
 | 
			
		||||
    BlockDriverState *active = s->active;
 | 
			
		||||
    BlockDriverState *top = s->top;
 | 
			
		||||
    BlockDriverState *base = s->base;
 | 
			
		||||
    BlockDriverState *overlay_bs;
 | 
			
		||||
    int64_t sector_num, end;
 | 
			
		||||
    uint64_t delay_ns = 0;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    int n = 0;
 | 
			
		||||
    void *buf = NULL;
 | 
			
		||||
    void *buf;
 | 
			
		||||
    int bytes_written = 0;
 | 
			
		||||
    int64_t base_len;
 | 
			
		||||
 | 
			
		||||
    ret = s->common.len = blk_getlength(s->top);
 | 
			
		||||
    ret = s->common.len = bdrv_getlength(top);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    if (s->common.len < 0) {
 | 
			
		||||
        goto out;
 | 
			
		||||
        goto exit_restore_reopen;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = base_len = blk_getlength(s->base);
 | 
			
		||||
    ret = base_len = bdrv_getlength(base);
 | 
			
		||||
    if (base_len < 0) {
 | 
			
		||||
        goto out;
 | 
			
		||||
        goto exit_restore_reopen;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (base_len < s->common.len) {
 | 
			
		||||
        ret = blk_truncate(s->base, s->common.len, NULL);
 | 
			
		||||
        ret = bdrv_truncate(base, s->common.len);
 | 
			
		||||
        if (ret) {
 | 
			
		||||
            goto out;
 | 
			
		||||
            goto exit_restore_reopen;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    end = s->common.len >> BDRV_SECTOR_BITS;
 | 
			
		||||
    buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
 | 
			
		||||
    buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
 | 
			
		||||
 | 
			
		||||
    for (sector_num = 0; sector_num < end; sector_num += n) {
 | 
			
		||||
        uint64_t delay_ns = 0;
 | 
			
		||||
        bool copy;
 | 
			
		||||
 | 
			
		||||
wait:
 | 
			
		||||
        /* Note that even when no rate limit is applied we need to yield
 | 
			
		||||
         * with no pending I/O here so that bdrv_drain_all() returns.
 | 
			
		||||
         */
 | 
			
		||||
        block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
 | 
			
		||||
        block_job_sleep_ns(&s->common, rt_clock, delay_ns);
 | 
			
		||||
        if (block_job_is_cancelled(&s->common)) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
        /* Copy if allocated above the base */
 | 
			
		||||
        ret = bdrv_is_allocated_above(blk_bs(s->top), blk_bs(s->base),
 | 
			
		||||
                                      sector_num,
 | 
			
		||||
                                      COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
 | 
			
		||||
                                      &n);
 | 
			
		||||
        ret = bdrv_co_is_allocated_above(top, base, sector_num,
 | 
			
		||||
                                         COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
 | 
			
		||||
                                         &n);
 | 
			
		||||
        copy = (ret == 1);
 | 
			
		||||
        trace_commit_one_iteration(s, sector_num, n, ret);
 | 
			
		||||
        if (copy) {
 | 
			
		||||
            ret = commit_populate(s->top, s->base, sector_num, n, buf);
 | 
			
		||||
            if (s->common.speed) {
 | 
			
		||||
                delay_ns = ratelimit_calculate_delay(&s->limit, n);
 | 
			
		||||
                if (delay_ns > 0) {
 | 
			
		||||
                    goto wait;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            ret = commit_populate(top, base, sector_num, n, buf);
 | 
			
		||||
            bytes_written += n * BDRV_SECTOR_SIZE;
 | 
			
		||||
        }
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            BlockErrorAction action =
 | 
			
		||||
                block_job_error_action(&s->common, false, s->on_error, -ret);
 | 
			
		||||
            if (action == BLOCK_ERROR_ACTION_REPORT) {
 | 
			
		||||
                goto out;
 | 
			
		||||
            if (s->on_error == BLOCKDEV_ON_ERROR_STOP ||
 | 
			
		||||
                s->on_error == BLOCKDEV_ON_ERROR_REPORT||
 | 
			
		||||
                (s->on_error == BLOCKDEV_ON_ERROR_ENOSPC && ret == -ENOSPC)) {
 | 
			
		||||
                goto exit_free_buf;
 | 
			
		||||
            } else {
 | 
			
		||||
                n = 0;
 | 
			
		||||
                continue;
 | 
			
		||||
@@ -200,20 +135,31 @@ static void coroutine_fn commit_run(void *opaque)
 | 
			
		||||
        }
 | 
			
		||||
        /* Publish progress */
 | 
			
		||||
        s->common.offset += n * BDRV_SECTOR_SIZE;
 | 
			
		||||
 | 
			
		||||
        if (copy && s->common.speed) {
 | 
			
		||||
            delay_ns = ratelimit_calculate_delay(&s->limit, n);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
 | 
			
		||||
out:
 | 
			
		||||
    if (!block_job_is_cancelled(&s->common) && sector_num == end) {
 | 
			
		||||
        /* success */
 | 
			
		||||
        ret = bdrv_drop_intermediate(active, top, base);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
exit_free_buf:
 | 
			
		||||
    qemu_vfree(buf);
 | 
			
		||||
 | 
			
		||||
    data = g_malloc(sizeof(*data));
 | 
			
		||||
    data->ret = ret;
 | 
			
		||||
    block_job_defer_to_main_loop(&s->common, commit_complete, data);
 | 
			
		||||
exit_restore_reopen:
 | 
			
		||||
    /* restore base open flags here if appropriate (e.g., change the base back
 | 
			
		||||
     * to r/o). These reopens do not need to be atomic, since we won't abort
 | 
			
		||||
     * even on failure here */
 | 
			
		||||
    if (s->base_flags != bdrv_get_flags(base)) {
 | 
			
		||||
        bdrv_reopen(base, s->base_flags, NULL);
 | 
			
		||||
    }
 | 
			
		||||
    overlay_bs = bdrv_find_overlay(active, top);
 | 
			
		||||
    if (overlay_bs && s->orig_overlay_flags != bdrv_get_flags(overlay_bs)) {
 | 
			
		||||
        bdrv_reopen(overlay_bs, s->orig_overlay_flags, NULL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    block_job_completed(&s->common, ret);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
@@ -221,82 +167,44 @@ static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
 | 
			
		||||
    CommitBlockJob *s = container_of(job, CommitBlockJob, common);
 | 
			
		||||
 | 
			
		||||
    if (speed < 0) {
 | 
			
		||||
        error_setg(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER, "speed");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
    ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const BlockJobDriver commit_job_driver = {
 | 
			
		||||
static BlockJobType commit_job_type = {
 | 
			
		||||
    .instance_size = sizeof(CommitBlockJob),
 | 
			
		||||
    .job_type      = BLOCK_JOB_TYPE_COMMIT,
 | 
			
		||||
    .job_type      = "commit",
 | 
			
		||||
    .set_speed     = commit_set_speed,
 | 
			
		||||
    .start         = commit_run,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,
 | 
			
		||||
    uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
 | 
			
		||||
{
 | 
			
		||||
    return bdrv_co_preadv(bs->backing, offset, bytes, qiov, flags);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int64_t coroutine_fn bdrv_commit_top_get_block_status(
 | 
			
		||||
    BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum,
 | 
			
		||||
    BlockDriverState **file)
 | 
			
		||||
{
 | 
			
		||||
    *pnum = nb_sectors;
 | 
			
		||||
    *file = bs->backing->bs;
 | 
			
		||||
    return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
 | 
			
		||||
           (sector_num << BDRV_SECTOR_BITS);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void bdrv_commit_top_refresh_filename(BlockDriverState *bs, QDict *opts)
 | 
			
		||||
{
 | 
			
		||||
    bdrv_refresh_filename(bs->backing->bs);
 | 
			
		||||
    pstrcpy(bs->exact_filename, sizeof(bs->exact_filename),
 | 
			
		||||
            bs->backing->bs->filename);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void bdrv_commit_top_close(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,
 | 
			
		||||
                                       const BdrvChildRole *role,
 | 
			
		||||
                                       uint64_t perm, uint64_t shared,
 | 
			
		||||
                                       uint64_t *nperm, uint64_t *nshared)
 | 
			
		||||
{
 | 
			
		||||
    *nperm = 0;
 | 
			
		||||
    *nshared = BLK_PERM_ALL;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* Dummy node that provides consistent read to its users without requiring it
 | 
			
		||||
 * from its backing file and that allows writes on the backing file chain. */
 | 
			
		||||
static BlockDriver bdrv_commit_top = {
 | 
			
		||||
    .format_name                = "commit_top",
 | 
			
		||||
    .bdrv_co_preadv             = bdrv_commit_top_preadv,
 | 
			
		||||
    .bdrv_co_get_block_status   = bdrv_commit_top_get_block_status,
 | 
			
		||||
    .bdrv_refresh_filename      = bdrv_commit_top_refresh_filename,
 | 
			
		||||
    .bdrv_close                 = bdrv_commit_top_close,
 | 
			
		||||
    .bdrv_child_perm            = bdrv_commit_top_child_perm,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
void commit_start(const char *job_id, BlockDriverState *bs,
 | 
			
		||||
                  BlockDriverState *base, BlockDriverState *top, int64_t speed,
 | 
			
		||||
                  BlockdevOnError on_error, const char *backing_file_str,
 | 
			
		||||
                  const char *filter_node_name, Error **errp)
 | 
			
		||||
void commit_start(BlockDriverState *bs, BlockDriverState *base,
 | 
			
		||||
                  BlockDriverState *top, int64_t speed,
 | 
			
		||||
                  BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
 | 
			
		||||
                  void *opaque, Error **errp)
 | 
			
		||||
{
 | 
			
		||||
    CommitBlockJob *s;
 | 
			
		||||
    BlockReopenQueue *reopen_queue = NULL;
 | 
			
		||||
    int orig_overlay_flags;
 | 
			
		||||
    int orig_base_flags;
 | 
			
		||||
    BlockDriverState *iter;
 | 
			
		||||
    BlockDriverState *overlay_bs;
 | 
			
		||||
    BlockDriverState *commit_top_bs = NULL;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
    int ret;
 | 
			
		||||
 | 
			
		||||
    assert(top != bs);
 | 
			
		||||
    if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
 | 
			
		||||
         on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
 | 
			
		||||
        !bdrv_iostatus_is_enabled(bs)) {
 | 
			
		||||
        error_set(errp, QERR_INVALID_PARAMETER_COMBINATION);
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Once we support top == active layer, remove this check */
 | 
			
		||||
    if (top == bs) {
 | 
			
		||||
        error_setg(errp,
 | 
			
		||||
                   "Top image as the active layer is currently unsupported");
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (top == base) {
 | 
			
		||||
        error_setg(errp, "Invalid files for merge: top and base are the same");
 | 
			
		||||
        return;
 | 
			
		||||
@@ -309,281 +217,42 @@ void commit_start(const char *job_id, BlockDriverState *bs,
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s = block_job_create(job_id, &commit_job_driver, bs, 0, BLK_PERM_ALL,
 | 
			
		||||
                         speed, BLOCK_JOB_DEFAULT, NULL, NULL, errp);
 | 
			
		||||
    if (!s) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    orig_base_flags    = bdrv_get_flags(base);
 | 
			
		||||
    orig_overlay_flags = bdrv_get_flags(overlay_bs);
 | 
			
		||||
 | 
			
		||||
    /* convert base & overlay_bs to r/w, if necessary */
 | 
			
		||||
    if (!(orig_base_flags & BDRV_O_RDWR)) {
 | 
			
		||||
        reopen_queue = bdrv_reopen_queue(reopen_queue, base, NULL,
 | 
			
		||||
        reopen_queue = bdrv_reopen_queue(reopen_queue, base,
 | 
			
		||||
                                         orig_base_flags | BDRV_O_RDWR);
 | 
			
		||||
    }
 | 
			
		||||
    if (!(orig_overlay_flags & BDRV_O_RDWR)) {
 | 
			
		||||
        reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs, NULL,
 | 
			
		||||
        reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs,
 | 
			
		||||
                                         orig_overlay_flags | BDRV_O_RDWR);
 | 
			
		||||
    }
 | 
			
		||||
    if (reopen_queue) {
 | 
			
		||||
        bdrv_reopen_multiple(bdrv_get_aio_context(bs), reopen_queue, &local_err);
 | 
			
		||||
        bdrv_reopen_multiple(reopen_queue, &local_err);
 | 
			
		||||
        if (local_err != NULL) {
 | 
			
		||||
            error_propagate(errp, local_err);
 | 
			
		||||
            goto fail;
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Insert commit_top block node above top, so we can block consistent read
 | 
			
		||||
     * on the backing chain below it */
 | 
			
		||||
    commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, filter_node_name, 0,
 | 
			
		||||
                                         errp);
 | 
			
		||||
    if (commit_top_bs == NULL) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    commit_top_bs->total_sectors = top->total_sectors;
 | 
			
		||||
    bdrv_set_aio_context(commit_top_bs, bdrv_get_aio_context(top));
 | 
			
		||||
 | 
			
		||||
    bdrv_set_backing_hd(commit_top_bs, top, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        bdrv_unref(commit_top_bs);
 | 
			
		||||
        commit_top_bs = NULL;
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
    bdrv_set_backing_hd(overlay_bs, commit_top_bs, &local_err);
 | 
			
		||||
    if (local_err) {
 | 
			
		||||
        bdrv_unref(commit_top_bs);
 | 
			
		||||
        commit_top_bs = NULL;
 | 
			
		||||
        error_propagate(errp, local_err);
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->commit_top_bs = commit_top_bs;
 | 
			
		||||
    bdrv_unref(commit_top_bs);
 | 
			
		||||
 | 
			
		||||
    /* Block all nodes between top and base, because they will
 | 
			
		||||
     * disappear from the chain after this operation. */
 | 
			
		||||
    assert(bdrv_chain_contains(top, base));
 | 
			
		||||
    for (iter = top; iter != base; iter = backing_bs(iter)) {
 | 
			
		||||
        /* XXX BLK_PERM_WRITE needs to be allowed so we don't block ourselves
 | 
			
		||||
         * at s->base (if writes are blocked for a node, they are also blocked
 | 
			
		||||
         * for its backing file). The other options would be a second filter
 | 
			
		||||
         * driver above s->base. */
 | 
			
		||||
        ret = block_job_add_bdrv(&s->common, "intermediate node", iter, 0,
 | 
			
		||||
                                 BLK_PERM_WRITE_UNCHANGED | BLK_PERM_WRITE,
 | 
			
		||||
                                 errp);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto fail;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ret = block_job_add_bdrv(&s->common, "base", base, 0, BLK_PERM_ALL, errp);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* overlay_bs must be blocked because it needs to be modified to
 | 
			
		||||
     * update the backing image string. */
 | 
			
		||||
    ret = block_job_add_bdrv(&s->common, "overlay of top", overlay_bs,
 | 
			
		||||
                             BLK_PERM_GRAPH_MOD, BLK_PERM_ALL, errp);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->base = blk_new(BLK_PERM_CONSISTENT_READ
 | 
			
		||||
                      | BLK_PERM_WRITE
 | 
			
		||||
                      | BLK_PERM_RESIZE,
 | 
			
		||||
                      BLK_PERM_CONSISTENT_READ
 | 
			
		||||
                      | BLK_PERM_GRAPH_MOD
 | 
			
		||||
                      | BLK_PERM_WRITE_UNCHANGED);
 | 
			
		||||
    ret = blk_insert_bs(s->base, base, errp);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Required permissions are already taken with block_job_add_bdrv() */
 | 
			
		||||
    s->top = blk_new(0, BLK_PERM_ALL);
 | 
			
		||||
    ret = blk_insert_bs(s->top, top, errp);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        goto fail;
 | 
			
		||||
    s = block_job_create(&commit_job_type, bs, speed, cb, opaque, errp);
 | 
			
		||||
    if (!s) {
 | 
			
		||||
        return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    s->base   = base;
 | 
			
		||||
    s->top    = top;
 | 
			
		||||
    s->active = bs;
 | 
			
		||||
 | 
			
		||||
    s->base_flags          = orig_base_flags;
 | 
			
		||||
    s->orig_overlay_flags  = orig_overlay_flags;
 | 
			
		||||
 | 
			
		||||
    s->backing_file_str = g_strdup(backing_file_str);
 | 
			
		||||
 | 
			
		||||
    s->on_error = on_error;
 | 
			
		||||
    s->common.co = qemu_coroutine_create(commit_run);
 | 
			
		||||
 | 
			
		||||
    trace_commit_start(bs, base, top, s);
 | 
			
		||||
    block_job_start(&s->common);
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
fail:
 | 
			
		||||
    if (s->base) {
 | 
			
		||||
        blk_unref(s->base);
 | 
			
		||||
    }
 | 
			
		||||
    if (s->top) {
 | 
			
		||||
        blk_unref(s->top);
 | 
			
		||||
    }
 | 
			
		||||
    if (commit_top_bs) {
 | 
			
		||||
        bdrv_set_backing_hd(overlay_bs, top, &error_abort);
 | 
			
		||||
    }
 | 
			
		||||
    block_job_early_fail(&s->common);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
#define COMMIT_BUF_SECTORS 2048
 | 
			
		||||
 | 
			
		||||
/* commit COW file into the raw image */
 | 
			
		||||
int bdrv_commit(BlockDriverState *bs)
 | 
			
		||||
{
 | 
			
		||||
    BlockBackend *src, *backing;
 | 
			
		||||
    BlockDriverState *backing_file_bs = NULL;
 | 
			
		||||
    BlockDriverState *commit_top_bs = NULL;
 | 
			
		||||
    BlockDriver *drv = bs->drv;
 | 
			
		||||
    int64_t sector, total_sectors, length, backing_length;
 | 
			
		||||
    int n, ro, open_flags;
 | 
			
		||||
    int ret = 0;
 | 
			
		||||
    uint8_t *buf = NULL;
 | 
			
		||||
    Error *local_err = NULL;
 | 
			
		||||
 | 
			
		||||
    if (!drv)
 | 
			
		||||
        return -ENOMEDIUM;
 | 
			
		||||
 | 
			
		||||
    if (!bs->backing) {
 | 
			
		||||
        return -ENOTSUP;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT_SOURCE, NULL) ||
 | 
			
		||||
        bdrv_op_is_blocked(bs->backing->bs, BLOCK_OP_TYPE_COMMIT_TARGET, NULL)) {
 | 
			
		||||
        return -EBUSY;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ro = bs->backing->bs->read_only;
 | 
			
		||||
    open_flags =  bs->backing->bs->open_flags;
 | 
			
		||||
 | 
			
		||||
    if (ro) {
 | 
			
		||||
        if (bdrv_reopen(bs->backing->bs, open_flags | BDRV_O_RDWR, NULL)) {
 | 
			
		||||
            return -EACCES;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    src = blk_new(BLK_PERM_CONSISTENT_READ, BLK_PERM_ALL);
 | 
			
		||||
    backing = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL);
 | 
			
		||||
 | 
			
		||||
    ret = blk_insert_bs(src, bs, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_report_err(local_err);
 | 
			
		||||
        goto ro_cleanup;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* Insert commit_top block node above backing, so we can write to it */
 | 
			
		||||
    backing_file_bs = backing_bs(bs);
 | 
			
		||||
 | 
			
		||||
    commit_top_bs = bdrv_new_open_driver(&bdrv_commit_top, NULL, BDRV_O_RDWR,
 | 
			
		||||
                                         &local_err);
 | 
			
		||||
    if (commit_top_bs == NULL) {
 | 
			
		||||
        error_report_err(local_err);
 | 
			
		||||
        goto ro_cleanup;
 | 
			
		||||
    }
 | 
			
		||||
    bdrv_set_aio_context(commit_top_bs, bdrv_get_aio_context(backing_file_bs));
 | 
			
		||||
 | 
			
		||||
    bdrv_set_backing_hd(commit_top_bs, backing_file_bs, &error_abort);
 | 
			
		||||
    bdrv_set_backing_hd(bs, commit_top_bs, &error_abort);
 | 
			
		||||
 | 
			
		||||
    ret = blk_insert_bs(backing, backing_file_bs, &local_err);
 | 
			
		||||
    if (ret < 0) {
 | 
			
		||||
        error_report_err(local_err);
 | 
			
		||||
        goto ro_cleanup;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    length = blk_getlength(src);
 | 
			
		||||
    if (length < 0) {
 | 
			
		||||
        ret = length;
 | 
			
		||||
        goto ro_cleanup;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    backing_length = blk_getlength(backing);
 | 
			
		||||
    if (backing_length < 0) {
 | 
			
		||||
        ret = backing_length;
 | 
			
		||||
        goto ro_cleanup;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* If our top snapshot is larger than the backing file image,
 | 
			
		||||
     * grow the backing file image if possible.  If not possible,
 | 
			
		||||
     * we must return an error */
 | 
			
		||||
    if (length > backing_length) {
 | 
			
		||||
        ret = blk_truncate(backing, length, &local_err);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            error_report_err(local_err);
 | 
			
		||||
            goto ro_cleanup;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    total_sectors = length >> BDRV_SECTOR_BITS;
 | 
			
		||||
 | 
			
		||||
    /* blk_try_blockalign() for src will choose an alignment that works for
 | 
			
		||||
     * backing as well, so no need to compare the alignment manually. */
 | 
			
		||||
    buf = blk_try_blockalign(src, COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
 | 
			
		||||
    if (buf == NULL) {
 | 
			
		||||
        ret = -ENOMEM;
 | 
			
		||||
        goto ro_cleanup;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (sector = 0; sector < total_sectors; sector += n) {
 | 
			
		||||
        ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto ro_cleanup;
 | 
			
		||||
        }
 | 
			
		||||
        if (ret) {
 | 
			
		||||
            ret = blk_pread(src, sector * BDRV_SECTOR_SIZE, buf,
 | 
			
		||||
                            n * BDRV_SECTOR_SIZE);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                goto ro_cleanup;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            ret = blk_pwrite(backing, sector * BDRV_SECTOR_SIZE, buf,
 | 
			
		||||
                             n * BDRV_SECTOR_SIZE, 0);
 | 
			
		||||
            if (ret < 0) {
 | 
			
		||||
                goto ro_cleanup;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (drv->bdrv_make_empty) {
 | 
			
		||||
        ret = drv->bdrv_make_empty(bs);
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            goto ro_cleanup;
 | 
			
		||||
        }
 | 
			
		||||
        blk_flush(src);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Make sure all data we wrote to the backing device is actually
 | 
			
		||||
     * stable on disk.
 | 
			
		||||
     */
 | 
			
		||||
    blk_flush(backing);
 | 
			
		||||
 | 
			
		||||
    ret = 0;
 | 
			
		||||
ro_cleanup:
 | 
			
		||||
    qemu_vfree(buf);
 | 
			
		||||
 | 
			
		||||
    blk_unref(backing);
 | 
			
		||||
    if (backing_file_bs) {
 | 
			
		||||
        bdrv_set_backing_hd(bs, backing_file_bs, &error_abort);
 | 
			
		||||
    }
 | 
			
		||||
    bdrv_unref(commit_top_bs);
 | 
			
		||||
    blk_unref(src);
 | 
			
		||||
 | 
			
		||||
    if (ro) {
 | 
			
		||||
        /* ignoring error return here */
 | 
			
		||||
        bdrv_reopen(bs->backing->bs, open_flags & ~BDRV_O_RDWR, NULL);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
    trace_commit_start(bs, base, top, s, s->common.co, opaque);
 | 
			
		||||
    qemu_coroutine_enter(s->common.co, s);
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user