Compare commits
46 Commits
for_upstre
...
v0.15.1-qe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
36f5bc4fdb | ||
|
|
82b2b32a32 | ||
|
|
4a81ab81e4 | ||
|
|
68e3508eaf | ||
|
|
fb524042db | ||
|
|
336398391a | ||
|
|
672aefeb5e | ||
|
|
6a10ccca80 | ||
|
|
7095e71576 | ||
|
|
91b31d6158 | ||
|
|
b89f4a7d2a | ||
|
|
fbdd7c8bd5 | ||
|
|
e19a4e89ae | ||
|
|
ff5acedd8f | ||
|
|
2af86a2ff7 | ||
|
|
e62ad8314a | ||
|
|
76e4e1d237 | ||
|
|
4fbe5233fd | ||
|
|
4bea41dbaa | ||
|
|
e2f775205a | ||
|
|
51dd7a94c7 | ||
|
|
9096de69ff | ||
|
|
09afeef1ab | ||
|
|
01825a8ddf | ||
|
|
ae2dd33693 | ||
|
|
a80f53aee3 | ||
|
|
88ca9f047b | ||
|
|
898517b0bc | ||
|
|
9dc9f2b820 | ||
|
|
ef942b795a | ||
|
|
868aa386b8 | ||
|
|
7e10be8c74 | ||
|
|
9843621e3b | ||
|
|
ab640dbfc0 | ||
|
|
eaa8b2778c | ||
|
|
7ec7f28019 | ||
|
|
31b1308046 | ||
|
|
4ec648dd6e | ||
|
|
e961d129e1 | ||
|
|
8959459386 | ||
|
|
e2b40e003a | ||
|
|
1a39b0fcff | ||
|
|
4a97e18b87 | ||
|
|
f1ee0a0ebd | ||
|
|
3583bc031e | ||
|
|
2798b5e174 |
7
.exrc
7
.exrc
@@ -1,7 +0,0 @@
|
|||||||
"VIM settings to match QEMU coding style. They are activated by adding the
|
|
||||||
"following settings (without the " symbol) as last two lines in $HOME/.vimrc:
|
|
||||||
"set secure
|
|
||||||
"set exrc
|
|
||||||
set expandtab
|
|
||||||
set shiftwidth=4
|
|
||||||
set smarttab
|
|
||||||
136
.gitignore
vendored
136
.gitignore
vendored
@@ -1,71 +1,47 @@
|
|||||||
/config-devices.*
|
config-devices.*
|
||||||
/config-all-devices.*
|
config-all-devices.*
|
||||||
/config-all-disas.*
|
config-host.*
|
||||||
/config-host.*
|
config-target.*
|
||||||
/config-target.*
|
trace.h
|
||||||
/config.status
|
trace.c
|
||||||
/config-temp
|
trace-dtrace.h
|
||||||
/trace/generated-tracers.h
|
trace-dtrace.dtrace
|
||||||
/trace/generated-tracers.c
|
|
||||||
/trace/generated-tracers-dtrace.h
|
|
||||||
/trace/generated-tracers.dtrace
|
|
||||||
/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
|
|
||||||
/trace/generated-ust-provider.h
|
|
||||||
/trace/generated-ust.c
|
|
||||||
/libcacard/trace/generated-tracers.c
|
|
||||||
*-timestamp
|
*-timestamp
|
||||||
/*-softmmu
|
*-softmmu
|
||||||
/*-darwin-user
|
*-darwin-user
|
||||||
/*-linux-user
|
*-linux-user
|
||||||
/*-bsd-user
|
*-bsd-user
|
||||||
/libdis*
|
libdis*
|
||||||
/libuser
|
libhw32
|
||||||
/linux-headers/asm
|
libhw64
|
||||||
/qga/qapi-generated
|
libuser
|
||||||
/qapi-generated
|
qapi-generated
|
||||||
/qapi-types.[ch]
|
qemu-doc.html
|
||||||
/qapi-visit.[ch]
|
qemu-tech.html
|
||||||
/qapi-event.[ch]
|
qemu-doc.info
|
||||||
/qmp-commands.h
|
qemu-tech.info
|
||||||
/qmp-marshal.c
|
qemu.1
|
||||||
/qemu-doc.html
|
qemu.pod
|
||||||
/qemu-tech.html
|
qemu-img.1
|
||||||
/qemu-doc.info
|
qemu-img.pod
|
||||||
/qemu-tech.info
|
qemu-img
|
||||||
/qemu.1
|
qemu-nbd
|
||||||
/qemu.pod
|
qemu-nbd.8
|
||||||
/qemu-img.1
|
qemu-nbd.pod
|
||||||
/qemu-img.pod
|
qemu-options.def
|
||||||
/qemu-img
|
qemu-options.texi
|
||||||
/qemu-nbd
|
qemu-img-cmds.texi
|
||||||
/qemu-nbd.8
|
qemu-img-cmds.h
|
||||||
/qemu-nbd.pod
|
qemu-io
|
||||||
/qemu-options.def
|
qemu-ga
|
||||||
/qemu-options.texi
|
qemu-monitor.texi
|
||||||
/qemu-img-cmds.texi
|
QMP/qmp-commands.txt
|
||||||
/qemu-img-cmds.h
|
.gdbinit
|
||||||
/qemu-io
|
|
||||||
/qemu-ga
|
|
||||||
/qemu-bridge-helper
|
|
||||||
/qemu-monitor.texi
|
|
||||||
/qmp-commands.txt
|
|
||||||
/vscclient
|
|
||||||
/fsdev/virtfs-proxy-helper
|
|
||||||
/fsdev/virtfs-proxy-helper.1
|
|
||||||
/fsdev/virtfs-proxy-helper.pod
|
|
||||||
*.a
|
*.a
|
||||||
*.aux
|
*.aux
|
||||||
*.cp
|
*.cp
|
||||||
*.dvi
|
*.dvi
|
||||||
*.exe
|
*.exe
|
||||||
*.dll
|
|
||||||
*.so
|
|
||||||
*.mo
|
|
||||||
*.fn
|
*.fn
|
||||||
*.ky
|
*.ky
|
||||||
*.log
|
*.log
|
||||||
@@ -79,34 +55,20 @@
|
|||||||
*.tp
|
*.tp
|
||||||
*.vr
|
*.vr
|
||||||
*.d
|
*.d
|
||||||
!/scripts/qemu-guest-agent/fsfreeze-hook.d
|
|
||||||
*.o
|
*.o
|
||||||
*.lo
|
*.swp
|
||||||
*.la
|
*.orig
|
||||||
*.pc
|
.pc
|
||||||
.libs
|
patches
|
||||||
.sdk
|
pc-bios/bios-pq/status
|
||||||
*.gcda
|
pc-bios/vgabios-pq/status
|
||||||
*.gcno
|
pc-bios/optionrom/linuxboot.bin
|
||||||
/pc-bios/bios-pq/status
|
pc-bios/optionrom/multiboot.bin
|
||||||
/pc-bios/vgabios-pq/status
|
pc-bios/optionrom/multiboot.raw
|
||||||
/pc-bios/optionrom/linuxboot.asm
|
pc-bios/optionrom/extboot.bin
|
||||||
/pc-bios/optionrom/linuxboot.bin
|
pc-bios/optionrom/vapic.bin
|
||||||
/pc-bios/optionrom/linuxboot.raw
|
|
||||||
/pc-bios/optionrom/linuxboot.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
|
|
||||||
.stgit-*
|
.stgit-*
|
||||||
cscope.*
|
cscope.*
|
||||||
tags
|
tags
|
||||||
TAGS
|
TAGS
|
||||||
*~
|
*~
|
||||||
/tests/qemu-iotests/common.env
|
|
||||||
|
|||||||
29
.gitmodules
vendored
29
.gitmodules
vendored
@@ -1,33 +1,12 @@
|
|||||||
[submodule "roms/vgabios"]
|
[submodule "roms/vgabios"]
|
||||||
path = roms/vgabios
|
path = roms/vgabios
|
||||||
url = git://git.qemu-project.org/vgabios.git/
|
url = git://git.kernel.org/pub/scm/virt/kvm/vgabios.git/
|
||||||
[submodule "roms/seabios"]
|
[submodule "roms/seabios"]
|
||||||
path = roms/seabios
|
path = roms/seabios
|
||||||
url = git://git.qemu-project.org/seabios.git/
|
url = git://git.qemu.org/seabios.git/
|
||||||
[submodule "roms/SLOF"]
|
[submodule "roms/SLOF"]
|
||||||
path = roms/SLOF
|
path = roms/SLOF
|
||||||
url = git://git.qemu-project.org/SLOF.git
|
url = git://git.qemu.org/SLOF.git
|
||||||
[submodule "roms/ipxe"]
|
[submodule "roms/ipxe"]
|
||||||
path = 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
|
|
||||||
[submodule "roms/qemu-palcode"]
|
|
||||||
path = roms/qemu-palcode
|
|
||||||
url = git://github.com/rth7680/qemu-palcode.git
|
|
||||||
[submodule "roms/sgabios"]
|
|
||||||
path = roms/sgabios
|
|
||||||
url = git://git.qemu-project.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
|
|
||||||
|
|||||||
17
.mailmap
17
.mailmap
@@ -1,17 +0,0 @@
|
|||||||
# This mailmap just translates the weird addresses from the original import into git
|
|
||||||
# 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>
|
|
||||||
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>
|
|
||||||
Fabrice Bellard <fabrice@bellard.org> bellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>
|
|
||||||
Jocelyn Mayer <l_indien@magic.fr> j_mayer <j_mayer@c046a42c-6fe2-441c-8c8c-71466251a162>
|
|
||||||
Paul Brook <paul@codesourcery.com> pbrook <pbrook@c046a42c-6fe2-441c-8c8c-71466251a162>
|
|
||||||
Thiemo Seufer <ths@networkno.de> ths <ths@c046a42c-6fe2-441c-8c8c-71466251a162>
|
|
||||||
malc <av1474@comtv.ru> malc <malc@c046a42c-6fe2-441c-8c8c-71466251a162>
|
|
||||||
# There is also a:
|
|
||||||
# (no author) <(no author)@c046a42c-6fe2-441c-8c8c-71466251a162>
|
|
||||||
# for the cvs2svn initialization commit e63c3dc74bf.
|
|
||||||
103
.travis.yml
103
.travis.yml
@@ -1,103 +0,0 @@
|
|||||||
language: c
|
|
||||||
python:
|
|
||||||
- "2.4"
|
|
||||||
compiler:
|
|
||||||
- gcc
|
|
||||||
- clang
|
|
||||||
notifications:
|
|
||||||
irc:
|
|
||||||
channels:
|
|
||||||
- "irc.oftc.net#qemu"
|
|
||||||
on_success: change
|
|
||||||
on_failure: always
|
|
||||||
env:
|
|
||||||
global:
|
|
||||||
- TEST_CMD=""
|
|
||||||
- EXTRA_CONFIG=""
|
|
||||||
# Development packages, EXTRA_PKGS saved for additional builds
|
|
||||||
- CORE_PKGS="libusb-1.0-0-dev libiscsi-dev librados-dev libncurses5-dev"
|
|
||||||
- NET_PKGS="libseccomp-dev libgnutls-dev libssh2-1-dev libspice-server-dev libspice-protocol-dev libnss3-dev"
|
|
||||||
- GUI_PKGS="libgtk-3-dev libvte-2.90-dev libsdl1.2-dev libpng12-dev libpixman-1-dev"
|
|
||||||
- EXTRA_PKGS=""
|
|
||||||
matrix:
|
|
||||||
# Group major targets together with their linux-user counterparts
|
|
||||||
- TARGETS=alpha-softmmu,alpha-linux-user
|
|
||||||
- TARGETS=arm-softmmu,arm-linux-user,armeb-linux-user,aarch64-softmmu,aarch64-linux-user
|
|
||||||
- TARGETS=cris-softmmu,cris-linux-user
|
|
||||||
- TARGETS=i386-softmmu,i386-linux-user,x86_64-softmmu,x86_64-linux-user
|
|
||||||
- TARGETS=m68k-softmmu,m68k-linux-user
|
|
||||||
- TARGETS=microblaze-softmmu,microblazeel-softmmu,microblaze-linux-user,microblazeel-linux-user
|
|
||||||
- TARGETS=mips-softmmu,mips64-softmmu,mips64el-softmmu,mipsel-softmmu
|
|
||||||
- TARGETS=mips-linux-user,mips64-linux-user,mips64el-linux-user,mipsel-linux-user,mipsn32-linux-user,mipsn32el-linux-user
|
|
||||||
- TARGETS=or32-softmmu,or32-linux-user
|
|
||||||
- TARGETS=ppc-softmmu,ppc64-softmmu,ppcemb-softmmu,ppc-linux-user,ppc64-linux-user,ppc64abi32-linux-user,ppc64le-linux-user
|
|
||||||
- TARGETS=s390x-softmmu,s390x-linux-user
|
|
||||||
- TARGETS=sh4-softmmu,sh4eb-softmmu,sh4-linux-user sh4eb-linux-user
|
|
||||||
- TARGETS=sparc-softmmu,sparc64-softmmu,sparc-linux-user,sparc32plus-linux-user,sparc64-linux-user
|
|
||||||
- TARGETS=unicore32-softmmu,unicore32-linux-user
|
|
||||||
# Group remaining softmmu only targets into one build
|
|
||||||
- TARGETS=lm32-softmmu,moxie-softmmu,tricore-softmmu,xtensa-softmmu,xtensaeb-softmmu
|
|
||||||
git:
|
|
||||||
# we want to do this ourselves
|
|
||||||
submodules: false
|
|
||||||
before_install:
|
|
||||||
- wget -O - http://people.linaro.org/~alex.bennee/qemu-submodule-git-seed.tar.xz | tar -xvJ
|
|
||||||
- git submodule update --init --recursive
|
|
||||||
- sudo apt-get update -qq
|
|
||||||
- sudo apt-get install -qq ${CORE_PKGS} ${NET_PKGS} ${GUI_PKGS} ${EXTRA_PKGS}
|
|
||||||
before_script:
|
|
||||||
- ./configure --target-list=${TARGETS} --enable-debug-tcg ${EXTRA_CONFIG}
|
|
||||||
script:
|
|
||||||
- make -j2 && ${TEST_CMD}
|
|
||||||
matrix:
|
|
||||||
# We manually include a number of additional build for non-standard bits
|
|
||||||
include:
|
|
||||||
# Make check target (we only do this once)
|
|
||||||
- env:
|
|
||||||
- TARGETS=alpha-softmmu,arm-softmmu,aarch64-softmmu,cris-softmmu,
|
|
||||||
i386-softmmu,x86_64-softmmu,m68k-softmmu,microblaze-softmmu,
|
|
||||||
microblazeel-softmmu,mips-softmmu,mips64-softmmu,
|
|
||||||
mips64el-softmmu,mipsel-softmmu,or32-softmmu,ppc-softmmu,
|
|
||||||
ppc64-softmmu,ppcemb-softmmu,s390x-softmmu,sh4-softmmu,
|
|
||||||
sh4eb-softmmu,sparc-softmmu,sparc64-softmmu,
|
|
||||||
unicore32-softmmu,unicore32-linux-user,
|
|
||||||
lm32-softmmu,moxie-softmmu,tricore-softmmu,xtensa-softmmu,
|
|
||||||
xtensaeb-softmmu
|
|
||||||
TEST_CMD="make check"
|
|
||||||
compiler: gcc
|
|
||||||
# Debug related options
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-debug"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-debug --enable-tcg-interpreter"
|
|
||||||
compiler: gcc
|
|
||||||
# All the extra -dev packages
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_PKGS="libaio-dev libcap-ng-dev libattr1-dev libbrlapi-dev uuid-dev libusb-1.0.0-dev"
|
|
||||||
compiler: gcc
|
|
||||||
# Currently configure doesn't force --disable-pie
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-gprof --enable-gcov --disable-pie"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_PKGS="sparse"
|
|
||||||
EXTRA_CONFIG="--enable-sparse"
|
|
||||||
compiler: gcc
|
|
||||||
# All the trace backends (apart from dtrace)
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=stderr"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=simple"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=ftrace"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_PKGS="liblttng-ust-dev liburcu-dev"
|
|
||||||
EXTRA_CONFIG="--enable-trace-backends=ust"
|
|
||||||
compiler: gcc
|
|
||||||
- env: TARGETS=i386-softmmu,x86_64-softmmu
|
|
||||||
EXTRA_CONFIG="--enable-modules"
|
|
||||||
compiler: gcc
|
|
||||||
30
CODING_STYLE
30
CODING_STYLE
@@ -1,4 +1,4 @@
|
|||||||
QEMU Coding Style
|
Qemu Coding Style
|
||||||
=================
|
=================
|
||||||
|
|
||||||
Please use the script checkpatch.pl in the scripts directory to check
|
Please use the script checkpatch.pl in the scripts directory to check
|
||||||
@@ -44,8 +44,7 @@ Rationale:
|
|||||||
3. Naming
|
3. Naming
|
||||||
|
|
||||||
Variables are lower_case_with_underscores; easy to type and read. Structured
|
Variables are lower_case_with_underscores; easy to type and read. Structured
|
||||||
type names are in CamelCase; harder to type but standing out. Enum type
|
type names are in CamelCase; harder to type but standing out. Scalar type
|
||||||
names and function type names should also be in CamelCase. Scalar type
|
|
||||||
names are lower_case_with_underscores_ending_with_a_t, like the POSIX
|
names are lower_case_with_underscores_ending_with_a_t, like the POSIX
|
||||||
uint64_t and family. Note that this last convention contradicts POSIX
|
uint64_t and family. Note that this last convention contradicts POSIX
|
||||||
and is therefore likely to be changed.
|
and is therefore likely to be changed.
|
||||||
@@ -69,10 +68,6 @@ keyword. Example:
|
|||||||
printf("a was something else entirely.\n");
|
printf("a was something else entirely.\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Note that 'else if' is considered a single statement; otherwise a long if/
|
|
||||||
else if/else if/.../else sequence would need an indent for every else
|
|
||||||
statement.
|
|
||||||
|
|
||||||
An exception is the opening brace for a function; for reasons of tradition
|
An exception is the opening brace for a function; for reasons of tradition
|
||||||
and clarity it comes on a line by itself:
|
and clarity it comes on a line by itself:
|
||||||
|
|
||||||
@@ -84,24 +79,3 @@ and clarity it comes on a line by itself:
|
|||||||
Rationale: a consistent (except for functions...) bracing style reduces
|
Rationale: a consistent (except for functions...) bracing style reduces
|
||||||
ambiguity and avoids needless churn when lines are added or removed.
|
ambiguity and avoids needless churn when lines are added or removed.
|
||||||
Furthermore, it is the QEMU coding style.
|
Furthermore, it is the QEMU coding style.
|
||||||
|
|
||||||
5. Declarations
|
|
||||||
|
|
||||||
Mixed declarations (interleaving statements and declarations within blocks)
|
|
||||||
are not allowed; declarations should be at the beginning of blocks. In other
|
|
||||||
words, the code should not generate warnings if using GCC's
|
|
||||||
-Wdeclaration-after-statement option.
|
|
||||||
|
|
||||||
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.
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
This file documents changes for QEMU releases 0.12 and earlier.
|
This file documents changes for QEMU releases 0.12 and earlier.
|
||||||
For changelog information for later releases, see
|
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.
|
more detailed information.
|
||||||
|
|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ version 0.10.2:
|
|||||||
|
|
||||||
- fix savevm/loadvm (Anthony Liguori)
|
- fix savevm/loadvm (Anthony Liguori)
|
||||||
- live migration: fix dirty tracking windows (Glauber Costa)
|
- live migration: fix dirty tracking windows (Glauber Costa)
|
||||||
- live migration: improve error propagation (Glauber Costa)
|
- live migration: improve error propogation (Glauber Costa)
|
||||||
- qcow2: fix image creation for > ~2TB images (Chris Wright)
|
- qcow2: fix image creation for > ~2TB images (Chris Wright)
|
||||||
- hotplug: fix error handling for if= parameter (Eduardo Habkost)
|
- hotplug: fix error handling for if= parameter (Eduardo Habkost)
|
||||||
- qcow2: fix data corruption (Nolan Leake)
|
- qcow2: fix data corruption (Nolan Leake)
|
||||||
@@ -386,7 +386,7 @@ version 0.5.3:
|
|||||||
- support of CD-ROM change
|
- support of CD-ROM change
|
||||||
- multiple network interface support
|
- multiple network interface support
|
||||||
- initial x86-64 host support (Gwenole Beauchesne)
|
- initial x86-64 host support (Gwenole Beauchesne)
|
||||||
- lret to outer privilege fix (OS/2 install fix)
|
- lret to outer priviledge fix (OS/2 install fix)
|
||||||
- task switch fixes (SkyOS boot)
|
- task switch fixes (SkyOS boot)
|
||||||
- VM save/restore commands
|
- VM save/restore commands
|
||||||
- new timer API
|
- new timer API
|
||||||
@@ -447,7 +447,7 @@ version 0.5.0:
|
|||||||
- multi-target build
|
- multi-target build
|
||||||
- fixed: no error code in hardware interrupts
|
- fixed: no error code in hardware interrupts
|
||||||
- fixed: pop ss, mov ss, x and sti disable hardware irqs for the next insn
|
- fixed: pop ss, mov ss, x and sti disable hardware irqs for the next insn
|
||||||
- correct single stepping through string operations
|
- correct single stepping thru string operations
|
||||||
- preliminary SPARC target support (Thomas M. Ogrisegg)
|
- preliminary SPARC target support (Thomas M. Ogrisegg)
|
||||||
- tun-fd option (Rusty Russell)
|
- tun-fd option (Rusty Russell)
|
||||||
- automatic IDE geometry detection
|
- automatic IDE geometry detection
|
||||||
|
|||||||
67
HACKING
67
HACKING
@@ -32,7 +32,7 @@ mandatory for VMState fields.
|
|||||||
|
|
||||||
Don't use Linux kernel internal types like u32, __u32 or __le32.
|
Don't use Linux kernel internal types like u32, __u32 or __le32.
|
||||||
|
|
||||||
Use hwaddr for guest physical addresses except pcibus_t
|
Use target_phys_addr_t for guest physical addresses except pcibus_t
|
||||||
for PCI addresses. In addition, ram_addr_t is a QEMU internal address
|
for PCI addresses. In addition, ram_addr_t is a QEMU internal address
|
||||||
space that maps guest RAM physical addresses into an intermediate
|
space that maps guest RAM physical addresses into an intermediate
|
||||||
address space that can map to host virtual address spaces. Generally
|
address space that can map to host virtual address spaces. Generally
|
||||||
@@ -40,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
|
it would not be correct to store an actual guest physical address in a
|
||||||
ram_addr_t.
|
ram_addr_t.
|
||||||
|
|
||||||
For CPU virtual addresses there are several possible types.
|
Use target_ulong (or abi_ulong) for CPU virtual addresses, however
|
||||||
vaddr is the best type to use to hold a CPU virtual address in
|
devices should not need to use target_ulong.
|
||||||
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.
|
|
||||||
|
|
||||||
Of course, take all of the above with a grain of salt. If you're about
|
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
|
to use some system interface that requires a type like size_t, pid_t or
|
||||||
@@ -92,24 +77,22 @@ avoided.
|
|||||||
|
|
||||||
Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
|
Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
|
||||||
APIs is not allowed in the QEMU codebase. Instead of these routines,
|
APIs is not allowed in the QEMU codebase. Instead of these routines,
|
||||||
use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
|
use the replacement qemu_malloc/qemu_mallocz/qemu_realloc/qemu_free or
|
||||||
g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
|
qemu_vmalloc/qemu_memalign/qemu_vfree APIs.
|
||||||
APIs.
|
|
||||||
|
|
||||||
Please note that g_malloc will exit on allocation failure, so there
|
Please note that NULL check for the qemu_malloc result is redundant and
|
||||||
is no need to test for failure (as you would have to with malloc).
|
that qemu_malloc() call with zero size is not allowed.
|
||||||
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
|
Memory allocated by qemu_vmalloc or qemu_memalign must be freed with
|
||||||
qemu_vfree, since breaking this will cause problems on Win32.
|
qemu_vfree, since breaking this will cause problems on Win32 and user
|
||||||
|
emulators.
|
||||||
|
|
||||||
4. String manipulation
|
4. String manipulation
|
||||||
|
|
||||||
Do not use the strncpy function. As mentioned in the man page, it does *not*
|
Do not use the strncpy function. According to the man page, it does
|
||||||
guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
|
*not* guarantee a NULL-terminated buffer, which makes it extremely dangerous
|
||||||
It also zeros trailing destination bytes out to the specified length. Instead,
|
to use. Instead, use functionally equivalent function:
|
||||||
use this similar function when possible, but note its different signature:
|
void pstrcpy(char *buf, int buf_size, const char *str)
|
||||||
void pstrcpy(char *dest, int dest_buf_size, const char *src)
|
|
||||||
|
|
||||||
Don't use strcat because it can't check for buffer overflows, but:
|
Don't use strcat because it can't check for buffer overflows, but:
|
||||||
char *pstrcat(char *buf, int buf_size, const char *s)
|
char *pstrcat(char *buf, int buf_size, const char *s)
|
||||||
@@ -125,7 +108,7 @@ int qemu_strnlen(const char *s, int max_len)
|
|||||||
There are also replacement character processing macros for isxyz and toxyz,
|
There are also replacement character processing macros for isxyz and toxyz,
|
||||||
so instead of e.g. isalnum you should use qemu_isalnum.
|
so instead of e.g. isalnum you should use qemu_isalnum.
|
||||||
|
|
||||||
Because of the memory management rules, you must use g_strdup/g_strndup
|
Because of the memory management rules, you must use qemu_strdup/qemu_strndup
|
||||||
instead of plain strdup/strndup.
|
instead of plain strdup/strndup.
|
||||||
|
|
||||||
5. Printf-style functions
|
5. Printf-style functions
|
||||||
@@ -137,23 +120,3 @@ gcc's printf attribute directive in the prototype.
|
|||||||
This makes it so gcc's -Wformat and -Wformat-security options can do
|
This makes it so gcc's -Wformat and -Wformat-security options can do
|
||||||
their jobs and cross-check format strings with the number and types
|
their jobs and cross-check format strings with the number and types
|
||||||
of arguments.
|
of arguments.
|
||||||
|
|
||||||
6. C standard, implementation defined and undefined behaviors
|
|
||||||
|
|
||||||
C code in QEMU should be written to the C99 language specification. A copy
|
|
||||||
of the final version of the C99 standard with corrigenda TC1, TC2, and TC3
|
|
||||||
included, formatted as a draft, can be downloaded from:
|
|
||||||
http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
|
|
||||||
|
|
||||||
The C language specification defines regions of undefined behavior and
|
|
||||||
implementation defined behavior (to give compiler authors enough leeway to
|
|
||||||
produce better code). In general, code in QEMU should follow the language
|
|
||||||
specification and avoid both undefined and implementation defined
|
|
||||||
constructs. ("It works fine on the gcc I tested it with" is not a valid
|
|
||||||
argument...) However there are a few areas where we allow ourselves to
|
|
||||||
assume certain behaviors because in practice all the platforms we care about
|
|
||||||
behave in the same way and writing strictly conformant code would be
|
|
||||||
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)
|
|
||||||
|
|||||||
17
LICENSE
17
LICENSE
@@ -1,21 +1,18 @@
|
|||||||
The following points clarify the QEMU license:
|
The following points clarify the QEMU license:
|
||||||
|
|
||||||
1) QEMU as a whole is released under the GNU General Public License,
|
1) QEMU as a whole is released under the GNU General Public License
|
||||||
version 2.
|
|
||||||
|
|
||||||
2) Parts of QEMU have specific licenses which are compatible with the
|
2) Parts of QEMU have specific licenses which are compatible with the
|
||||||
GNU General Public License, version 2. Hence each source file contains
|
GNU General Public License. Hence each source file contains its own
|
||||||
its own licensing information. Source files with no licensing information
|
licensing information.
|
||||||
are released under the GNU General Public License, version 2 or (at your
|
|
||||||
option) any later version.
|
|
||||||
|
|
||||||
As of July 2013, contributions under version 2 of the GNU General Public
|
In particular, the QEMU virtual CPU core library (libqemu.a) is
|
||||||
License (and no later version) are only accepted for the following files
|
released under the GNU Lesser General Public License. Many hardware
|
||||||
or directories: bsd-user/, linux-user/, hw/vfio/, hw/xen/xen_pt*.
|
device emulation sources are released under the BSD license.
|
||||||
|
|
||||||
3) The Tiny Code Generator (TCG) is released under the BSD license
|
3) The Tiny Code Generator (TCG) is released under the BSD license
|
||||||
(see license headers in files).
|
(see license headers in files).
|
||||||
|
|
||||||
4) QEMU is a trademark of Fabrice Bellard.
|
4) QEMU is a trademark of Fabrice Bellard.
|
||||||
|
|
||||||
Fabrice Bellard and the QEMU team
|
Fabrice Bellard.
|
||||||
|
|||||||
841
MAINTAINERS
841
MAINTAINERS
File diff suppressed because it is too large
Load Diff
526
Makefile
526
Makefile
@@ -1,72 +1,23 @@
|
|||||||
# Makefile for QEMU.
|
# Makefile for QEMU.
|
||||||
|
|
||||||
# Always point to the root of the build tree (needs GNU make).
|
GENERATED_HEADERS = config-host.h trace.h qemu-options.def
|
||||||
BUILD_DIR=$(CURDIR)
|
ifeq ($(TRACE_BACKEND),dtrace)
|
||||||
|
GENERATED_HEADERS += trace-dtrace.h
|
||||||
|
endif
|
||||||
|
|
||||||
# All following code might depend on configuration variables
|
|
||||||
ifneq ($(wildcard config-host.mak),)
|
ifneq ($(wildcard config-host.mak),)
|
||||||
# Put the all: rule here so that config-host.mak can contain dependencies.
|
# Put the all: rule here so that config-host.mak can contain dependencies.
|
||||||
all:
|
all: build-all
|
||||||
include config-host.mak
|
include config-host.mak
|
||||||
|
|
||||||
# Check that we're not trying to do an out-of-tree build from
|
|
||||||
# a tree that's been used for an in-tree build.
|
|
||||||
ifneq ($(realpath $(SRC_PATH)),$(realpath .))
|
|
||||||
ifneq ($(wildcard $(SRC_PATH)/config-host.mak),)
|
|
||||||
$(error This is an out of tree build but your source tree ($(SRC_PATH)) \
|
|
||||||
seems to have been used for an in-tree build. You can fix this by running \
|
|
||||||
"make distclean && rm -rf *-linux-user *-softmmu" in your source tree)
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
CONFIG_SOFTMMU := $(if $(filter %-softmmu,$(TARGET_DIRS)),y)
|
|
||||||
CONFIG_USER_ONLY := $(if $(filter %-user,$(TARGET_DIRS)),y)
|
|
||||||
CONFIG_ALL=y
|
|
||||||
-include config-all-devices.mak
|
|
||||||
-include config-all-disas.mak
|
|
||||||
|
|
||||||
include $(SRC_PATH)/rules.mak
|
include $(SRC_PATH)/rules.mak
|
||||||
config-host.mak: $(SRC_PATH)/configure
|
config-host.mak: $(SRC_PATH)/configure
|
||||||
@echo $@ is out-of-date, running configure
|
@echo $@ is out-of-date, running configure
|
||||||
@# TODO: The next lines include code which supports a smooth
|
@sed -n "/.*Configured with/s/[^:]*: //p" $@ | sh
|
||||||
@# 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
|
|
||||||
else
|
else
|
||||||
config-host.mak:
|
config-host.mak:
|
||||||
ifneq ($(filter-out %clean,$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
|
|
||||||
@echo "Please call configure before running make!"
|
@echo "Please call configure before running make!"
|
||||||
@exit 1
|
@exit 1
|
||||||
endif
|
endif
|
||||||
endif
|
|
||||||
|
|
||||||
GENERATED_HEADERS = config-host.h qemu-options.def
|
|
||||||
GENERATED_HEADERS += qmp-commands.h qapi-types.h qapi-visit.h qapi-event.h
|
|
||||||
GENERATED_SOURCES += qmp-marshal.c qapi-types.c qapi-visit.c qapi-event.c
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-events.h
|
|
||||||
GENERATED_SOURCES += trace/generated-events.c
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-tracers.h
|
|
||||||
ifeq ($(findstring dtrace,$(TRACE_BACKENDS)),dtrace)
|
|
||||||
GENERATED_HEADERS += trace/generated-tracers-dtrace.h
|
|
||||||
endif
|
|
||||||
GENERATED_SOURCES += trace/generated-tracers.c
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-tcg-tracers.h
|
|
||||||
|
|
||||||
GENERATED_HEADERS += trace/generated-helpers-wrappers.h
|
|
||||||
GENERATED_HEADERS += trace/generated-helpers.h
|
|
||||||
GENERATED_SOURCES += trace/generated-helpers.c
|
|
||||||
|
|
||||||
ifeq ($(findstring ust,$(TRACE_BACKENDS)),ust)
|
|
||||||
GENERATED_HEADERS += trace/generated-ust-provider.h
|
|
||||||
GENERATED_SOURCES += trace/generated-ust.c
|
|
||||||
endif
|
|
||||||
|
|
||||||
# Don't try to regenerate Makefile or configure
|
# Don't try to regenerate Makefile or configure
|
||||||
# We don't generate any of them
|
# We don't generate any of them
|
||||||
@@ -74,37 +25,24 @@ Makefile: ;
|
|||||||
configure: ;
|
configure: ;
|
||||||
|
|
||||||
.PHONY: all clean cscope distclean dvi html info install install-doc \
|
.PHONY: all clean cscope distclean dvi html info install install-doc \
|
||||||
pdf recurse-all speed test dist
|
pdf recurse-all speed tar tarbin test build-all
|
||||||
|
|
||||||
$(call set-vpath, $(SRC_PATH))
|
$(call set-vpath, $(SRC_PATH):$(SRC_PATH)/hw)
|
||||||
|
|
||||||
LIBS+=-lz $(LIBS_TOOLS)
|
LIBS+=-lz $(LIBS_TOOLS)
|
||||||
|
|
||||||
HELPERS-$(CONFIG_LINUX) = qemu-bridge-helper$(EXESUF)
|
|
||||||
|
|
||||||
ifdef BUILD_DOCS
|
ifdef BUILD_DOCS
|
||||||
DOCS=qemu-doc.html qemu-tech.html qemu.1 qemu-img.1 qemu-nbd.8 qmp-commands.txt
|
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
|
|
||||||
else
|
else
|
||||||
DOCS=
|
DOCS=
|
||||||
endif
|
endif
|
||||||
|
|
||||||
SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory) BUILD_DIR=$(BUILD_DIR)
|
SUBDIR_MAKEFLAGS=$(if $(V),,--no-print-directory)
|
||||||
SUBDIR_DEVICES_MAK=$(patsubst %, %/config-devices.mak, $(TARGET_DIRS))
|
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 $@")
|
|
||||||
else
|
|
||||||
config-all-devices.mak: $(SUBDIR_DEVICES_MAK)
|
config-all-devices.mak: $(SUBDIR_DEVICES_MAK)
|
||||||
$(call quiet-command, sed -n \
|
$(call quiet-command,cat $(SUBDIR_DEVICES_MAK) | grep =y | sort -u > $@," GEN $@")
|
||||||
's|^\([^=]*\)=\(.*\)$$|\1:=$$(findstring y,$$(\1)\2)|p' \
|
|
||||||
$(SUBDIR_DEVICES_MAK) | sort -u > $@, \
|
|
||||||
" GEN $@")
|
|
||||||
endif
|
|
||||||
|
|
||||||
-include $(SUBDIR_DEVICES_MAK_DEP)
|
-include $(SUBDIR_DEVICES_MAK_DEP)
|
||||||
|
|
||||||
@@ -131,28 +69,9 @@ endif
|
|||||||
defconfig:
|
defconfig:
|
||||||
rm -f config-all-devices.mak $(SUBDIR_DEVICES_MAK)
|
rm -f config-all-devices.mak $(SUBDIR_DEVICES_MAK)
|
||||||
|
|
||||||
ifneq ($(wildcard config-host.mak),)
|
-include config-all-devices.mak
|
||||||
include $(SRC_PATH)/Makefile.objs
|
|
||||||
endif
|
|
||||||
|
|
||||||
dummy := $(call unnest-vars,, \
|
build-all: $(DOCS) $(TOOLS) recurse-all
|
||||||
stub-obj-y \
|
|
||||||
util-obj-y \
|
|
||||||
qga-obj-y \
|
|
||||||
qga-vss-dll-obj-y \
|
|
||||||
block-obj-y \
|
|
||||||
block-obj-m \
|
|
||||||
common-obj-y \
|
|
||||||
common-obj-m)
|
|
||||||
|
|
||||||
ifneq ($(wildcard config-host.mak),)
|
|
||||||
include $(SRC_PATH)/tests/Makefile
|
|
||||||
endif
|
|
||||||
ifeq ($(CONFIG_SMARTCARD_NSS),y)
|
|
||||||
include $(SRC_PATH)/libcacard/Makefile
|
|
||||||
endif
|
|
||||||
|
|
||||||
all: $(DOCS) $(TOOLS) $(HELPERS-y) recurse-all modules
|
|
||||||
|
|
||||||
config-host.h: config-host.h-timestamp
|
config-host.h: config-host.h-timestamp
|
||||||
config-host.h-timestamp: config-host.mak
|
config-host.h-timestamp: config-host.mak
|
||||||
@@ -160,34 +79,20 @@ qemu-options.def: $(SRC_PATH)/qemu-options.hx
|
|||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@")
|
||||||
|
|
||||||
SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
|
SUBDIR_RULES=$(patsubst %,subdir-%, $(TARGET_DIRS))
|
||||||
SOFTMMU_SUBDIR_RULES=$(filter %-softmmu,$(SUBDIR_RULES))
|
|
||||||
|
|
||||||
$(SOFTMMU_SUBDIR_RULES): $(block-obj-y)
|
subdir-%: $(GENERATED_HEADERS)
|
||||||
$(SOFTMMU_SUBDIR_RULES): config-all-devices.mak
|
|
||||||
|
|
||||||
subdir-%:
|
|
||||||
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)
|
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C $* V="$(V)" TARGET_DIR="$*/" all,)
|
||||||
|
|
||||||
subdir-pixman: pixman/Makefile
|
ifneq ($(wildcard config-host.mak),)
|
||||||
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C pixman V="$(V)" all,)
|
include $(SRC_PATH)/Makefile.objs
|
||||||
|
endif
|
||||||
|
|
||||||
pixman/Makefile: $(SRC_PATH)/pixman/configure
|
$(common-obj-y): $(GENERATED_HEADERS)
|
||||||
(cd pixman; CFLAGS="$(CFLAGS) -fPIC $(extra_cflags) $(extra_ldflags)" $(SRC_PATH)/pixman/configure $(AUTOCONF_HOST) --disable-gtk --disable-shared --enable-static)
|
subdir-libcacard: $(oslib-obj-y) $(trace-obj-y) qemu-malloc.o qemu-timer-common.o
|
||||||
|
|
||||||
$(SRC_PATH)/pixman/configure:
|
$(filter %-softmmu,$(SUBDIR_RULES)): $(trace-obj-y) $(common-obj-y) subdir-libdis
|
||||||
(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
|
$(filter %-user,$(SUBDIR_RULES)): $(GENERATED_HEADERS) $(trace-obj-y) subdir-libdis-user subdir-libuser
|
||||||
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)
|
|
||||||
|
|
||||||
ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
|
ROMSUBDIR_RULES=$(patsubst %,romsubdir-%, $(ROMS))
|
||||||
romsubdir-%:
|
romsubdir-%:
|
||||||
@@ -197,244 +102,205 @@ ALL_SUBDIRS=$(TARGET_DIRS) $(patsubst %,pc-bios/%, $(ROMS))
|
|||||||
|
|
||||||
recurse-all: $(SUBDIR_RULES) $(ROMSUBDIR_RULES)
|
recurse-all: $(SUBDIR_RULES) $(ROMSUBDIR_RULES)
|
||||||
|
|
||||||
$(BUILD_DIR)/version.o: $(SRC_PATH)/version.rc $(BUILD_DIR)/config-host.h | $(BUILD_DIR)/version.lo
|
audio/audio.o audio/fmodaudio.o: QEMU_CFLAGS += $(FMOD_CFLAGS)
|
||||||
$(call quiet-command,$(WINDRES) -I$(BUILD_DIR) -o $@ $<," RC version.o")
|
|
||||||
$(BUILD_DIR)/version.lo: $(SRC_PATH)/version.rc $(BUILD_DIR)/config-host.h
|
|
||||||
$(call quiet-command,$(WINDRES) -I$(BUILD_DIR) -o $@ $<," RC version.lo")
|
|
||||||
|
|
||||||
Makefile: $(version-obj-y) $(version-lobj-y)
|
QEMU_CFLAGS+=$(CURL_CFLAGS)
|
||||||
|
|
||||||
|
QEMU_CFLAGS+=$(GLIB_CFLAGS)
|
||||||
|
|
||||||
|
ui/cocoa.o: ui/cocoa.m
|
||||||
|
|
||||||
|
ui/sdl.o audio/sdlaudio.o ui/sdl_zoom.o baum.o: QEMU_CFLAGS += $(SDL_CFLAGS)
|
||||||
|
|
||||||
|
ui/vnc.o: QEMU_CFLAGS += $(VNC_TLS_CFLAGS)
|
||||||
|
|
||||||
|
bt-host.o: QEMU_CFLAGS += $(BLUEZ_CFLAGS)
|
||||||
|
|
||||||
|
version.o: $(SRC_PATH)/version.rc config-host.mak
|
||||||
|
$(call quiet-command,$(WINDRES) -I. -o $@ $<," RC $(TARGET_DIR)$@")
|
||||||
|
|
||||||
|
version-obj-$(CONFIG_WIN32) += version.o
|
||||||
######################################################################
|
######################################################################
|
||||||
# Build libraries
|
# Support building shared library libcacard
|
||||||
|
|
||||||
libqemustub.a: $(stub-obj-y)
|
.PHONY: libcacard.la install-libcacard
|
||||||
libqemuutil.a: $(util-obj-y)
|
ifeq ($(LIBTOOL),)
|
||||||
|
libcacard.la:
|
||||||
|
@echo "libtool is missing, please install and rerun configure"; exit 1
|
||||||
|
|
||||||
block-modules = $(foreach o,$(block-obj-m),"$(basename $(subst /,-,$o))",) NULL
|
install-libcacard:
|
||||||
util/module.o-cflags = -D'CONFIG_BLOCK_MODULES=$(block-modules)'
|
@echo "libtool is missing, please install and rerun configure"; exit 1
|
||||||
|
else
|
||||||
|
libcacard.la: $(GENERATED_HEADERS) $(oslib-obj-y) qemu-malloc.o qemu-timer-common.o $(addsuffix .lo, $(basename $(trace-obj-y)))
|
||||||
|
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libcacard V="$(V)" TARGET_DIR="$*/" libcacard.la,)
|
||||||
|
|
||||||
|
install-libcacard: libcacard.la
|
||||||
|
$(call quiet-command,$(MAKE) $(SUBDIR_MAKEFLAGS) -C libcacard V="$(V)" TARGET_DIR="$*/" install-libcacard,)
|
||||||
|
endif
|
||||||
######################################################################
|
######################################################################
|
||||||
|
|
||||||
qemu-img.o: qemu-img-cmds.h
|
qemu-img.o: qemu-img-cmds.h
|
||||||
|
qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o cmd.o qemu-ga.o: $(GENERATED_HEADERS)
|
||||||
|
|
||||||
qemu-img$(EXESUF): qemu-img.o $(block-obj-y) libqemuutil.a libqemustub.a
|
qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
|
||||||
qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) libqemuutil.a libqemustub.a
|
|
||||||
qemu-io$(EXESUF): qemu-io.o $(block-obj-y) libqemuutil.a libqemustub.a
|
|
||||||
|
|
||||||
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
|
qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
|
||||||
|
|
||||||
fsdev/virtfs-proxy-helper$(EXESUF): fsdev/virtfs-proxy-helper.o fsdev/virtio-9p-marshal.o libqemuutil.a libqemustub.a
|
qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
|
||||||
fsdev/virtfs-proxy-helper$(EXESUF): LIBS += -lcap
|
|
||||||
|
|
||||||
qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
|
qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $@")
|
||||||
|
|
||||||
qemu-ga$(EXESUF): LIBS = $(LIBS_QGA)
|
check-qint.o check-qstring.o check-qdict.o check-qlist.o check-qfloat.o check-qjson.o: $(GENERATED_HEADERS)
|
||||||
qemu-ga$(EXESUF): QEMU_CFLAGS += -I qga/qapi-generated
|
|
||||||
|
|
||||||
gen-out-type = $(subst .,-,$(suffix $@))
|
CHECK_PROG_DEPS = qemu-malloc.o $(oslib-obj-y) $(trace-obj-y) qemu-tool.o
|
||||||
|
|
||||||
qapi-py = $(SRC_PATH)/scripts/qapi.py $(SRC_PATH)/scripts/ordereddict.py
|
check-qint: check-qint.o qint.o $(CHECK_PROG_DEPS)
|
||||||
|
check-qstring: check-qstring.o qstring.o $(CHECK_PROG_DEPS)
|
||||||
|
check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qlist.o $(CHECK_PROG_DEPS)
|
||||||
|
check-qlist: check-qlist.o qlist.o qint.o $(CHECK_PROG_DEPS)
|
||||||
|
check-qfloat: check-qfloat.o qfloat.o $(CHECK_PROG_DEPS)
|
||||||
|
check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o error.o qerror.o qemu-error.o $(CHECK_PROG_DEPS)
|
||||||
|
|
||||||
qga/qapi-generated/qga-qapi-types.c qga/qapi-generated/qga-qapi-types.h :\
|
$(qapi-obj-y): $(GENERATED_HEADERS)
|
||||||
$(SRC_PATH)/qga/qapi-schema.json $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
|
qapi-dir := qapi-generated
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
|
test-visitor.o test-qmp-commands.o qemu-ga$(EXESUF): QEMU_CFLAGS += -I $(qapi-dir)
|
||||||
$(gen-out-type) -o qga/qapi-generated -p "qga-" -i $<, \
|
|
||||||
" 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-" -i $<, \
|
|
||||||
" 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-" -i $<, \
|
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
qapi-modules = $(SRC_PATH)/qapi-schema.json $(SRC_PATH)/qapi/common.json \
|
$(qapi-dir)/test-qapi-types.c: $(qapi-dir)/test-qapi-types.h
|
||||||
$(SRC_PATH)/qapi/block.json $(SRC_PATH)/qapi/block-core.json \
|
$(qapi-dir)/test-qapi-types.h: $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
|
||||||
$(SRC_PATH)/qapi/event.json
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py -o "$(qapi-dir)" -p "test-" < $<, " GEN $@")
|
||||||
|
$(qapi-dir)/test-qapi-visit.c: $(qapi-dir)/test-qapi-visit.h
|
||||||
|
$(qapi-dir)/test-qapi-visit.h: $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-visit.py
|
||||||
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py -o "$(qapi-dir)" -p "test-" < $<, " GEN $@")
|
||||||
|
$(qapi-dir)/test-qmp-commands.h: $(qapi-dir)/test-qmp-marshal.c
|
||||||
|
$(qapi-dir)/test-qmp-marshal.c: $(SRC_PATH)/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-commands.py
|
||||||
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py -o "$(qapi-dir)" -p "test-" < $<, " GEN $@")
|
||||||
|
|
||||||
qapi-types.c qapi-types.h :\
|
$(qapi-dir)/qga-qapi-types.c: $(qapi-dir)/qga-qapi-types.h
|
||||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-types.py $(qapi-py)
|
$(qapi-dir)/qga-qapi-types.h: $(SRC_PATH)/qapi-schema-guest.json $(SRC_PATH)/scripts/qapi-types.py
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-types.py -o "$(qapi-dir)" -p "qga-" < $<, " GEN $@")
|
||||||
$(gen-out-type) -o "." -b -i $<, \
|
$(qapi-dir)/qga-qapi-visit.c: $(qapi-dir)/qga-qapi-visit.h
|
||||||
" GEN $@")
|
$(qapi-dir)/qga-qapi-visit.h: $(SRC_PATH)/qapi-schema-guest.json $(SRC_PATH)/scripts/qapi-visit.py
|
||||||
qapi-visit.c qapi-visit.h :\
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py -o "$(qapi-dir)" -p "qga-" < $<, " GEN $@")
|
||||||
$(qapi-modules) $(SRC_PATH)/scripts/qapi-visit.py $(qapi-py)
|
$(qapi-dir)/qga-qmp-marshal.c: $(SRC_PATH)/qapi-schema-guest.json $(SRC_PATH)/scripts/qapi-commands.py
|
||||||
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-visit.py \
|
$(call quiet-command,$(PYTHON) $(SRC_PATH)/scripts/qapi-commands.py -o "$(qapi-dir)" -p "qga-" < $<, " GEN $@")
|
||||||
$(gen-out-type) -o "." -b -i $<, \
|
|
||||||
" 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 "." -b -i $<, \
|
|
||||||
" 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 "." -m -i $<, \
|
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
QGALIB_GEN=$(addprefix qga/qapi-generated/, qga-qapi-types.h qga-qapi-visit.h qga-qmp-commands.h)
|
test-visitor.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h) $(qapi-obj-y)
|
||||||
$(qga-obj-y) qemu-ga.o: $(QGALIB_GEN)
|
test-visitor: test-visitor.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o qemu-malloc.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o qemu-error.o qemu-tool.o $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o
|
||||||
|
|
||||||
qemu-ga$(EXESUF): $(qga-obj-y) libqemuutil.a libqemustub.a
|
test-qmp-commands.o: $(addprefix $(qapi-dir)/, test-qapi-types.c test-qapi-types.h test-qapi-visit.c test-qapi-visit.h test-qmp-marshal.c test-qmp-commands.h) $(qapi-obj-y)
|
||||||
$(call LINK, $^)
|
test-qmp-commands: test-qmp-commands.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o $(qapi-obj-y) error.o osdep.o qemu-malloc.o $(oslib-obj-y) qjson.o json-streamer.o json-lexer.o json-parser.o qerror.o qemu-error.o qemu-tool.o $(qapi-dir)/test-qapi-visit.o $(qapi-dir)/test-qapi-types.o $(qapi-dir)/test-qmp-marshal.o module.o
|
||||||
|
|
||||||
|
QGALIB=qga/guest-agent-command-state.o qga/guest-agent-commands.o
|
||||||
|
QGALIB_GEN=$(addprefix $(qapi-dir)/, qga-qapi-types.c qga-qapi-types.h qga-qapi-visit.c qga-qmp-marshal.c)
|
||||||
|
|
||||||
|
$(QGALIB_GEN): $(GENERATED_HEADERS)
|
||||||
|
$(QGALIB) qemu-ga.o: $(QGALIB_GEN) $(qapi-obj-y)
|
||||||
|
qemu-ga$(EXESUF): qemu-ga.o $(QGALIB) qemu-tool.o qemu-error.o error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) $(qapi-obj-y) qemu-timer-common.o qemu-sockets.o module.o qapi/qmp-dispatch.o qapi/qmp-registry.o $(qapi-dir)/qga-qapi-visit.o $(qapi-dir)/qga-qapi-types.o $(qapi-dir)/qga-qmp-marshal.o
|
||||||
|
|
||||||
|
QEMULIBS=libhw32 libhw64 libuser libdis libdis-user
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
# avoid old build problems by removing potentially incorrect old files
|
# 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 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 qemu-options.def
|
||||||
find . \( -name '*.l[oa]' -o -name '*.so' -o -name '*.dll' -o -name '*.mo' -o -name '*.[oda]' \) -type f -exec rm {} +
|
rm -f *.o *.d *.a *.lo $(TOOLS) qemu-ga TAGS cscope.* *.pod *~ */*~
|
||||||
rm -f $(filter-out %.tlb,$(TOOLS)) $(HELPERS-y) qemu-ga TAGS cscope.* *.pod *~ */*~
|
rm -Rf .libs
|
||||||
rm -f fsdev/*.pod
|
rm -f slirp/*.o slirp/*.d audio/*.o audio/*.d block/*.o block/*.d net/*.o net/*.d fsdev/*.o fsdev/*.d ui/*.o ui/*.d qapi/*.o qapi/*.d qga/*.o qga/*.d
|
||||||
rm -rf .libs */.libs
|
|
||||||
rm -f qemu-img-cmds.h
|
rm -f qemu-img-cmds.h
|
||||||
@# May not be present in GENERATED_HEADERS
|
rm -f trace.c trace.h trace.c-timestamp trace.h-timestamp
|
||||||
rm -f trace/generated-tracers-dtrace.dtrace*
|
rm -f trace-dtrace.dtrace trace-dtrace.dtrace-timestamp
|
||||||
rm -f trace/generated-tracers-dtrace.h*
|
rm -f trace-dtrace.h trace-dtrace.h-timestamp
|
||||||
rm -f $(foreach f,$(GENERATED_HEADERS),$(f) $(f)-timestamp)
|
rm -rf $(qapi-dir)
|
||||||
rm -f $(foreach f,$(GENERATED_SOURCES),$(f) $(f)-timestamp)
|
$(MAKE) -C tests clean
|
||||||
rm -rf qapi-generated
|
for d in $(ALL_SUBDIRS) $(QEMULIBS) libcacard; do \
|
||||||
rm -rf qga/qapi-generated
|
|
||||||
for d in $(ALL_SUBDIRS); do \
|
|
||||||
if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
|
if test -d $$d; then $(MAKE) -C $$d $@ || exit 1; fi; \
|
||||||
rm -f $$d/qemu-options.def; \
|
rm -f $$d/qemu-options.def; \
|
||||||
done
|
done
|
||||||
|
|
||||||
VERSION ?= $(shell cat VERSION)
|
|
||||||
|
|
||||||
dist: qemu-$(VERSION).tar.bz2
|
|
||||||
|
|
||||||
qemu-%.tar.bz2:
|
|
||||||
$(SRC_PATH)/scripts/make-release "$(SRC_PATH)" "$(patsubst qemu-%.tar.bz2,%,$@)"
|
|
||||||
|
|
||||||
distclean: clean
|
distclean: clean
|
||||||
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-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 config.status
|
rm -f config-all-devices.mak
|
||||||
rm -f po/*.mo tests/qemu-iotests/common.env
|
|
||||||
rm -f roms/seabios/config.mak roms/vgabios/config.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 qemu-doc.dvi
|
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.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.log qemu-doc.pdf qemu-doc.pg qemu-doc.toc qemu-doc.tp
|
||||||
rm -f qemu-doc.vr
|
rm -f qemu-doc.vr
|
||||||
rm -f config.log
|
|
||||||
rm -f linux-headers/asm
|
|
||||||
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
|
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 \
|
for d in $(TARGET_DIRS) $(QEMULIBS); do \
|
||||||
rm -rf $$d || exit 1 ; \
|
rm -rf $$d || exit 1 ; \
|
||||||
done
|
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
|
|
||||||
|
|
||||||
KEYMAPS=da en-gb et fr fr-ch is lt modifiers no pt-br sv \
|
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 \
|
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 \
|
common de-ch es fo fr-ca hu ja mk nl-be pt sl tr
|
||||||
bepo cz
|
|
||||||
|
|
||||||
ifdef INSTALL_BLOBS
|
ifdef INSTALL_BLOBS
|
||||||
BLOBS=bios.bin bios-256k.bin sgabios.bin vgabios.bin vgabios-cirrus.bin \
|
BLOBS=bios.bin vgabios.bin vgabios-cirrus.bin \
|
||||||
vgabios-stdvga.bin vgabios-vmware.bin vgabios-qxl.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 \
|
||||||
ppc_rom.bin openbios-sparc32 openbios-sparc64 openbios-ppc QEMU,tcx.bin QEMU,cgthree.bin \
|
|
||||||
pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom \
|
pxe-e1000.rom pxe-eepro100.rom pxe-ne2k_pci.rom \
|
||||||
pxe-pcnet.rom pxe-rtl8139.rom pxe-virtio.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 \
|
|
||||||
qemu-icon.bmp qemu_logo_no_text.svg \
|
|
||||||
bamboo.dtb petalogix-s3adsp1800.dtb petalogix-ml605.dtb \
|
bamboo.dtb petalogix-s3adsp1800.dtb petalogix-ml605.dtb \
|
||||||
multiboot.bin linuxboot.bin kvmvapic.bin \
|
mpc8544ds.dtb \
|
||||||
|
multiboot.bin linuxboot.bin \
|
||||||
s390-zipl.rom \
|
s390-zipl.rom \
|
||||||
s390-ccw.img \
|
spapr-rtas.bin slof.bin
|
||||||
spapr-rtas.bin slof.bin \
|
BLOBS += extboot.bin
|
||||||
palcode-clipper \
|
BLOBS += vapic.bin
|
||||||
u-boot.e500
|
|
||||||
else
|
else
|
||||||
BLOBS=
|
BLOBS=
|
||||||
endif
|
endif
|
||||||
|
|
||||||
install-doc: $(DOCS)
|
install-doc: $(DOCS)
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_docdir)"
|
$(INSTALL_DIR) "$(DESTDIR)$(docdir)"
|
||||||
$(INSTALL_DATA) qemu-doc.html qemu-tech.html "$(DESTDIR)$(qemu_docdir)"
|
$(INSTALL_DATA) qemu-doc.html qemu-tech.html "$(DESTDIR)$(docdir)"
|
||||||
$(INSTALL_DATA) qmp-commands.txt "$(DESTDIR)$(qemu_docdir)"
|
|
||||||
ifdef CONFIG_POSIX
|
ifdef CONFIG_POSIX
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
||||||
$(INSTALL_DATA) qemu.1 "$(DESTDIR)$(mandir)/man1"
|
$(INSTALL_DATA) qemu.1 qemu-img.1 "$(DESTDIR)$(mandir)/man1"
|
||||||
ifneq ($(TOOLS),)
|
|
||||||
$(INSTALL_DATA) qemu-img.1 "$(DESTDIR)$(mandir)/man1"
|
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man8"
|
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man8"
|
||||||
$(INSTALL_DATA) qemu-nbd.8 "$(DESTDIR)$(mandir)/man8"
|
$(INSTALL_DATA) qemu-nbd.8 "$(DESTDIR)$(mandir)/man8"
|
||||||
endif
|
endif
|
||||||
endif
|
|
||||||
ifdef CONFIG_VIRTFS
|
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(mandir)/man1"
|
|
||||||
$(INSTALL_DATA) fsdev/virtfs-proxy-helper.1 "$(DESTDIR)$(mandir)/man1"
|
|
||||||
endif
|
|
||||||
|
|
||||||
install-datadir:
|
install-sysconfig:
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)"
|
$(INSTALL_DIR) "$(DESTDIR)$(sysconfdir)/qemu"
|
||||||
|
$(INSTALL_DATA) $(SRC_PATH)/sysconfigs/target/target-x86_64.conf "$(DESTDIR)$(sysconfdir)/qemu"
|
||||||
|
|
||||||
install-localstatedir:
|
install: all $(if $(BUILD_DOCS),install-doc) install-sysconfig
|
||||||
ifdef CONFIG_POSIX
|
$(INSTALL_DIR) "$(DESTDIR)$(bindir)"
|
||||||
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-sysconfig \
|
|
||||||
install-datadir install-localstatedir
|
|
||||||
ifneq ($(TOOLS),)
|
ifneq ($(TOOLS),)
|
||||||
$(call install-prog,$(TOOLS),$(DESTDIR)$(bindir))
|
$(INSTALL_PROG) $(STRIP_OPT) $(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
|
|
||||||
endif
|
|
||||||
ifneq ($(HELPERS-y),)
|
|
||||||
$(call install-prog,$(HELPERS-y),$(DESTDIR)$(libexecdir))
|
|
||||||
endif
|
endif
|
||||||
ifneq ($(BLOBS),)
|
ifneq ($(BLOBS),)
|
||||||
|
$(INSTALL_DIR) "$(DESTDIR)$(datadir)"
|
||||||
set -e; for x in $(BLOBS); do \
|
set -e; for x in $(BLOBS); do \
|
||||||
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/$$x "$(DESTDIR)$(qemu_datadir)"; \
|
if [ -f $(SRC_PATH)/pc-bios/$$x ];then \
|
||||||
|
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/$$x "$(DESTDIR)$(datadir)"; \
|
||||||
|
fi \
|
||||||
|
; if [ -f pc-bios/optionrom/$$x ];then \
|
||||||
|
$(INSTALL_DATA) pc-bios/optionrom/$$x "$(DESTDIR)$(datadir)"; \
|
||||||
|
fi \
|
||||||
done
|
done
|
||||||
endif
|
endif
|
||||||
ifeq ($(CONFIG_GTK),y)
|
$(INSTALL_DIR) "$(DESTDIR)$(datadir)/keymaps"
|
||||||
$(MAKE) -C po $@
|
|
||||||
endif
|
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/keymaps"
|
|
||||||
set -e; for x in $(KEYMAPS); do \
|
set -e; for x in $(KEYMAPS); do \
|
||||||
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/keymaps/$$x "$(DESTDIR)$(qemu_datadir)/keymaps"; \
|
$(INSTALL_DATA) $(SRC_PATH)/pc-bios/keymaps/$$x "$(DESTDIR)$(datadir)/keymaps"; \
|
||||||
done
|
done
|
||||||
$(INSTALL_DATA) $(SRC_PATH)/trace-events "$(DESTDIR)$(qemu_datadir)/trace-events"
|
|
||||||
for d in $(TARGET_DIRS); do \
|
for d in $(TARGET_DIRS); do \
|
||||||
$(MAKE) $(SUBDIR_MAKEFLAGS) TARGET_DIR=$$d/ -C $$d $@ || exit 1 ; \
|
$(MAKE) -C $$d $@ || exit 1 ; \
|
||||||
done
|
done
|
||||||
|
|
||||||
# various test targets
|
# various test targets
|
||||||
test speed: all
|
test speed: all
|
||||||
$(MAKE) -C tests/tcg $@
|
$(MAKE) -C tests $@
|
||||||
|
|
||||||
.PHONY: TAGS
|
.PHONY: TAGS
|
||||||
TAGS:
|
TAGS:
|
||||||
rm -f $@
|
find "$(SRC_PATH)" -name '*.[hc]' -print0 | xargs -0 etags
|
||||||
find "$(SRC_PATH)" -name '*.[hc]' -exec etags --append {} +
|
|
||||||
|
|
||||||
cscope:
|
cscope:
|
||||||
rm -f ./cscope.*
|
rm -f ./cscope.*
|
||||||
find "$(SRC_PATH)" -name "*.[chsS]" -print | sed 's,^\./,,' > ./cscope.files
|
find . -name "*.[ch]" -print | sed 's,^\./,,' > ./cscope.files
|
||||||
cscope -b
|
cscope -b
|
||||||
|
|
||||||
# documentation
|
# documentation
|
||||||
@@ -445,7 +311,7 @@ TEXIFLAG=$(if $(V),,--quiet)
|
|||||||
$(call quiet-command,texi2dvi $(TEXIFLAG) -I . $<," GEN $@")
|
$(call quiet-command,texi2dvi $(TEXIFLAG) -I . $<," GEN $@")
|
||||||
|
|
||||||
%.html: %.texi
|
%.html: %.texi
|
||||||
$(call quiet-command,LC_ALL=C $(MAKEINFO) $(MAKEINFOFLAGS) --html $< -o $@, \
|
$(call quiet-command,$(MAKEINFO) $(MAKEINFOFLAGS) --html $< -o $@, \
|
||||||
" GEN $@")
|
" GEN $@")
|
||||||
|
|
||||||
%.info: %.texi
|
%.info: %.texi
|
||||||
@@ -460,7 +326,7 @@ qemu-options.texi: $(SRC_PATH)/qemu-options.hx
|
|||||||
qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx
|
qemu-monitor.texi: $(SRC_PATH)/hmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -t < $< > $@," GEN $@")
|
||||||
|
|
||||||
qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx
|
QMP/qmp-commands.txt: $(SRC_PATH)/qmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@," GEN $@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -q < $< > $@," GEN $@")
|
||||||
|
|
||||||
qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
|
qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
|
||||||
@@ -469,25 +335,19 @@ qemu-img-cmds.texi: $(SRC_PATH)/qemu-img-cmds.hx
|
|||||||
qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi
|
qemu.1: qemu-doc.texi qemu-options.texi qemu-monitor.texi
|
||||||
$(call quiet-command, \
|
$(call quiet-command, \
|
||||||
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu.pod && \
|
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu.pod && \
|
||||||
$(POD2MAN) --section=1 --center=" " --release=" " qemu.pod > $@, \
|
pod2man --section=1 --center=" " --release=" " qemu.pod > $@, \
|
||||||
" GEN $@")
|
" GEN $@")
|
||||||
|
|
||||||
qemu-img.1: qemu-img.texi qemu-img-cmds.texi
|
qemu-img.1: qemu-img.texi qemu-img-cmds.texi
|
||||||
$(call quiet-command, \
|
$(call quiet-command, \
|
||||||
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-img.pod && \
|
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-img.pod && \
|
||||||
$(POD2MAN) --section=1 --center=" " --release=" " qemu-img.pod > $@, \
|
pod2man --section=1 --center=" " --release=" " qemu-img.pod > $@, \
|
||||||
" GEN $@")
|
|
||||||
|
|
||||||
fsdev/virtfs-proxy-helper.1: fsdev/virtfs-proxy-helper.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 $@")
|
" GEN $@")
|
||||||
|
|
||||||
qemu-nbd.8: qemu-nbd.texi
|
qemu-nbd.8: qemu-nbd.texi
|
||||||
$(call quiet-command, \
|
$(call quiet-command, \
|
||||||
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-nbd.pod && \
|
perl -Ww -- $(SRC_PATH)/scripts/texi2pod.pl $< qemu-nbd.pod && \
|
||||||
$(POD2MAN) --section=8 --center=" " --release=" " qemu-nbd.pod > $@, \
|
pod2man --section=8 --center=" " --release=" " qemu-nbd.pod > $@, \
|
||||||
" GEN $@")
|
" GEN $@")
|
||||||
|
|
||||||
dvi: qemu-doc.dvi qemu-tech.dvi
|
dvi: qemu-doc.dvi qemu-tech.dvi
|
||||||
@@ -499,67 +359,53 @@ qemu-doc.dvi qemu-doc.html qemu-doc.info qemu-doc.pdf: \
|
|||||||
qemu-img.texi qemu-nbd.texi qemu-options.texi \
|
qemu-img.texi qemu-nbd.texi qemu-options.texi \
|
||||||
qemu-monitor.texi qemu-img-cmds.texi
|
qemu-monitor.texi qemu-img-cmds.texi
|
||||||
|
|
||||||
ifdef CONFIG_WIN32
|
VERSION ?= $(shell cat VERSION)
|
||||||
|
FILE = qemu-$(VERSION)
|
||||||
|
|
||||||
INSTALLER = qemu-setup-$(VERSION)$(EXESUF)
|
# tar release (use 'make -k tar' on a checkouted tree)
|
||||||
|
tar:
|
||||||
|
rm -rf /tmp/$(FILE)
|
||||||
|
cp -r . /tmp/$(FILE)
|
||||||
|
cd /tmp && tar zcvf ~/$(FILE).tar.gz $(FILE) --exclude CVS --exclude .git --exclude .svn
|
||||||
|
rm -rf /tmp/$(FILE)
|
||||||
|
|
||||||
nsisflags = -V2 -NOCD
|
SYSTEM_TARGETS=$(filter %-softmmu,$(TARGET_DIRS))
|
||||||
|
SYSTEM_PROGS=$(patsubst qemu-system-i386,qemu, \
|
||||||
|
$(patsubst %-softmmu,qemu-system-%, \
|
||||||
|
$(SYSTEM_TARGETS)))
|
||||||
|
|
||||||
ifneq ($(wildcard $(SRC_PATH)/dll),)
|
USER_TARGETS=$(filter %-user,$(TARGET_DIRS))
|
||||||
ifeq ($(ARCH),x86_64)
|
USER_PROGS=$(patsubst %-bsd-user,qemu-%, \
|
||||||
# 64 bit executables
|
$(patsubst %-darwin-user,qemu-%, \
|
||||||
DLL_PATH = $(SRC_PATH)/dll/w64
|
$(patsubst %-linux-user,qemu-%, \
|
||||||
nsisflags += -DW64
|
$(USER_TARGETS))))
|
||||||
else
|
|
||||||
# 32 bit executables
|
|
||||||
DLL_PATH = $(SRC_PATH)/dll/w32
|
|
||||||
endif
|
|
||||||
endif
|
|
||||||
|
|
||||||
.PHONY: installer
|
# generate a binary distribution
|
||||||
installer: $(INSTALLER)
|
tarbin:
|
||||||
|
cd / && tar zcvf ~/qemu-$(VERSION)-$(ARCH).tar.gz \
|
||||||
INSTDIR=/tmp/qemu-nsis
|
$(patsubst %,$(bindir)/%, $(SYSTEM_PROGS)) \
|
||||||
|
$(patsubst %,$(bindir)/%, $(USER_PROGS)) \
|
||||||
$(INSTALLER): $(SRC_PATH)/qemu.nsi
|
$(bindir)/qemu-img \
|
||||||
make install prefix=${INSTDIR}
|
$(bindir)/qemu-nbd \
|
||||||
ifdef SIGNCODE
|
$(datadir)/bios.bin \
|
||||||
(cd ${INSTDIR}; \
|
$(datadir)/vgabios.bin \
|
||||||
for i in *.exe; do \
|
$(datadir)/vgabios-cirrus.bin \
|
||||||
$(SIGNCODE) $${i}; \
|
$(datadir)/ppc_rom.bin \
|
||||||
done \
|
$(datadir)/openbios-sparc32 \
|
||||||
)
|
$(datadir)/openbios-sparc64 \
|
||||||
endif # SIGNCODE
|
$(datadir)/openbios-ppc \
|
||||||
(cd ${INSTDIR}; \
|
$(datadir)/pxe-e1000.rom \
|
||||||
for i in qemu-system-*.exe; do \
|
$(datadir)/pxe-eepro100.rom \
|
||||||
arch=$${i%.exe}; \
|
$(datadir)/pxe-ne2k_pci.rom \
|
||||||
arch=$${arch#qemu-system-}; \
|
$(datadir)/pxe-pcnet.rom \
|
||||||
echo Section \"$$arch\" Section_$$arch; \
|
$(datadir)/pxe-rtl8139.rom \
|
||||||
echo SetOutPath \"\$$INSTDIR\"; \
|
$(datadir)/pxe-virtio.rom \
|
||||||
echo File \"\$${BINDIR}\\$$i\"; \
|
$(datadir)/extboot.bin \
|
||||||
echo SectionEnd; \
|
$(docdir)/qemu-doc.html \
|
||||||
done \
|
$(docdir)/qemu-tech.html \
|
||||||
) >${INSTDIR}/system-emulations.nsh
|
$(mandir)/man1/qemu.1 \
|
||||||
makensis $(nsisflags) \
|
$(mandir)/man1/qemu-img.1 \
|
||||||
$(if $(BUILD_DOCS),-DCONFIG_DOCUMENTATION="y") \
|
$(mandir)/man8/qemu-nbd.8
|
||||||
$(if $(CONFIG_GTK),-DCONFIG_GTK="y") \
|
|
||||||
-DBINDIR="${INSTDIR}" \
|
|
||||||
$(if $(DLL_PATH),-DDLLDIR="$(DLL_PATH)") \
|
|
||||||
-DSRCDIR="$(SRC_PATH)" \
|
|
||||||
-DOUTFILE="$(INSTALLER)" \
|
|
||||||
$(SRC_PATH)/qemu.nsi
|
|
||||||
rm -r ${INSTDIR}
|
|
||||||
ifdef SIGNCODE
|
|
||||||
$(SIGNCODE) $(INSTALLER)
|
|
||||||
endif # SIGNCODE
|
|
||||||
endif # CONFIG_WIN
|
|
||||||
|
|
||||||
# Add a dependency on the generated files, so that they are always
|
|
||||||
# rebuilt before other object files
|
|
||||||
ifneq ($(filter-out %clean,$(MAKECMDGOALS)),$(if $(MAKECMDGOALS),,fail))
|
|
||||||
Makefile: $(GENERATED_HEADERS)
|
|
||||||
endif
|
|
||||||
|
|
||||||
# Include automatically generated dependency files
|
# Include automatically generated dependency files
|
||||||
# Dependencies in Makefile.objs files come from our recursive subdir rules
|
-include $(wildcard *.d audio/*.d slirp/*.d block/*.d net/*.d ui/*.d qapi/*.d qga/*.d)
|
||||||
-include $(wildcard *.d tests/*.d)
|
|
||||||
|
|||||||
23
Makefile.dis
Normal file
23
Makefile.dis
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Makefile for disassemblers.
|
||||||
|
|
||||||
|
include ../config-host.mak
|
||||||
|
include config.mak
|
||||||
|
include $(SRC_PATH)/rules.mak
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
|
||||||
|
$(call set-vpath, $(SRC_PATH))
|
||||||
|
|
||||||
|
QEMU_CFLAGS+=-I..
|
||||||
|
|
||||||
|
include $(SRC_PATH)/Makefile.objs
|
||||||
|
|
||||||
|
all: $(libdis-y)
|
||||||
|
# Dummy command so that make thinks it has done something
|
||||||
|
@true
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o *.d *.a *~
|
||||||
|
|
||||||
|
# Include automatically generated dependency files
|
||||||
|
-include $(wildcard *.d */*.d)
|
||||||
24
Makefile.hw
Normal file
24
Makefile.hw
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# Makefile for qemu target independent devices.
|
||||||
|
|
||||||
|
include ../config-host.mak
|
||||||
|
include ../config-all-devices.mak
|
||||||
|
include config.mak
|
||||||
|
include $(SRC_PATH)/rules.mak
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
|
||||||
|
$(call set-vpath, $(SRC_PATH):$(SRC_PATH)/hw)
|
||||||
|
|
||||||
|
QEMU_CFLAGS+=-I.. -I$(SRC_PATH)/fpu
|
||||||
|
|
||||||
|
include $(SRC_PATH)/Makefile.objs
|
||||||
|
|
||||||
|
all: $(hw-obj-y)
|
||||||
|
# Dummy command so that make thinks it has done something
|
||||||
|
@true
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o */*.o *.d */*.d *.a */*.a *~ */*~
|
||||||
|
|
||||||
|
# Include automatically generated dependency files
|
||||||
|
-include $(wildcard *.d */*.d)
|
||||||
462
Makefile.objs
462
Makefile.objs
@@ -1,110 +1,390 @@
|
|||||||
#######################################################################
|
#######################################################################
|
||||||
# Common libraries for tools and emulators
|
# QObject
|
||||||
stub-obj-y = stubs/
|
qobject-obj-y = qint.o qstring.o qdict.o qlist.o qfloat.o qbool.o
|
||||||
util-obj-y = util/ qobject/ qapi/ qapi-types.o qapi-visit.o qapi-event.o
|
qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o
|
||||||
|
qobject-obj-y += qerror.o error.o
|
||||||
|
|
||||||
|
#######################################################################
|
||||||
|
# oslib-obj-y is code depending on the OS (win32 vs posix)
|
||||||
|
oslib-obj-y = osdep.o
|
||||||
|
oslib-obj-$(CONFIG_WIN32) += oslib-win32.o qemu-thread-win32.o
|
||||||
|
oslib-obj-$(CONFIG_POSIX) += oslib-posix.o qemu-thread-posix.o
|
||||||
|
|
||||||
#######################################################################
|
#######################################################################
|
||||||
# block-obj-y is code used by both qemu system emulation and qemu-img
|
# block-obj-y is code used by both qemu system emulation and qemu-img
|
||||||
|
|
||||||
block-obj-y = async.o thread-pool.o
|
block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o async.o
|
||||||
block-obj-y += nbd.o block.o blockjob.o
|
block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o qemu-progress.o qemu-sockets.o
|
||||||
block-obj-y += main-loop.o iohandler.o qemu-timer.o
|
block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
|
||||||
block-obj-$(CONFIG_POSIX) += aio-posix.o
|
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
|
||||||
block-obj-$(CONFIG_WIN32) += aio-win32.o
|
block-obj-$(CONFIG_POSIX) += compatfd.o
|
||||||
block-obj-y += block/
|
|
||||||
block-obj-y += qemu-io-cmds.o
|
|
||||||
|
|
||||||
block-obj-y += qemu-coroutine.o qemu-coroutine-lock.o qemu-coroutine-io.o
|
block-nested-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
|
||||||
block-obj-y += qemu-coroutine-sleep.o
|
block-nested-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
|
||||||
block-obj-y += coroutine-$(CONFIG_COROUTINE_BACKEND).o
|
block-nested-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
|
||||||
|
block-nested-y += qed-check.o
|
||||||
|
block-nested-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o
|
||||||
|
block-nested-$(CONFIG_WIN32) += raw-win32.o
|
||||||
|
block-nested-$(CONFIG_POSIX) += raw-posix.o
|
||||||
|
block-nested-$(CONFIG_CURL) += curl.o
|
||||||
|
block-nested-$(CONFIG_RBD) += rbd.o
|
||||||
|
|
||||||
block-obj-m = block/
|
block-obj-y += $(addprefix block/, $(block-nested-y))
|
||||||
|
|
||||||
|
net-obj-y = net.o
|
||||||
|
net-nested-y = queue.o checksum.o util.o
|
||||||
|
net-nested-y += socket.o
|
||||||
|
net-nested-y += dump.o
|
||||||
|
net-nested-$(CONFIG_POSIX) += tap.o
|
||||||
|
net-nested-$(CONFIG_LINUX) += tap-linux.o
|
||||||
|
net-nested-$(CONFIG_WIN32) += tap-win32.o
|
||||||
|
net-nested-$(CONFIG_BSD) += tap-bsd.o
|
||||||
|
net-nested-$(CONFIG_SOLARIS) += tap-solaris.o
|
||||||
|
net-nested-$(CONFIG_AIX) += tap-aix.o
|
||||||
|
net-nested-$(CONFIG_HAIKU) += tap-haiku.o
|
||||||
|
net-nested-$(CONFIG_SLIRP) += slirp.o
|
||||||
|
net-nested-$(CONFIG_VDE) += vde.o
|
||||||
|
net-obj-y += $(addprefix net/, $(net-nested-y))
|
||||||
|
|
||||||
|
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
|
||||||
|
fsdev-nested-y = qemu-fsdev.o
|
||||||
|
else
|
||||||
|
fsdev-nested-y = qemu-fsdev-dummy.o
|
||||||
|
endif
|
||||||
|
fsdev-obj-$(CONFIG_VIRTFS) += $(addprefix fsdev/, $(fsdev-nested-y))
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
# libqemu_common.a: Target independent part of system emulation. The
|
||||||
|
# long term path is to suppress *all* target specific code in case of
|
||||||
|
# system emulation, i.e. a single QEMU executable should support all
|
||||||
|
# CPUs and machines.
|
||||||
|
|
||||||
|
common-obj-y = $(block-obj-y) blockdev.o
|
||||||
|
common-obj-y += $(net-obj-y)
|
||||||
|
common-obj-y += $(qobject-obj-y)
|
||||||
|
common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
|
||||||
|
common-obj-y += readline.o console.o cursor.o qemu-error.o
|
||||||
|
common-obj-y += $(oslib-obj-y)
|
||||||
|
common-obj-$(CONFIG_WIN32) += os-win32.o
|
||||||
|
common-obj-$(CONFIG_POSIX) += os-posix.o
|
||||||
|
|
||||||
|
common-obj-y += tcg-runtime.o host-utils.o
|
||||||
|
common-obj-y += irq.o ioport.o input.o
|
||||||
|
common-obj-$(CONFIG_PTIMER) += ptimer.o
|
||||||
|
common-obj-$(CONFIG_MAX7310) += max7310.o
|
||||||
|
common-obj-$(CONFIG_WM8750) += wm8750.o
|
||||||
|
common-obj-$(CONFIG_TWL92230) += twl92230.o
|
||||||
|
common-obj-$(CONFIG_TSC2005) += tsc2005.o
|
||||||
|
common-obj-$(CONFIG_LM832X) += lm832x.o
|
||||||
|
common-obj-$(CONFIG_TMP105) += tmp105.o
|
||||||
|
common-obj-$(CONFIG_STELLARIS_INPUT) += stellaris_input.o
|
||||||
|
common-obj-$(CONFIG_SSD0303) += ssd0303.o
|
||||||
|
common-obj-$(CONFIG_SSD0323) += ssd0323.o
|
||||||
|
common-obj-$(CONFIG_ADS7846) += ads7846.o
|
||||||
|
common-obj-$(CONFIG_MAX111X) += max111x.o
|
||||||
|
common-obj-$(CONFIG_DS1338) += ds1338.o
|
||||||
|
common-obj-y += i2c.o smbus.o smbus_eeprom.o
|
||||||
|
common-obj-y += eeprom93xx.o
|
||||||
|
common-obj-y += scsi-disk.o cdrom.o
|
||||||
|
common-obj-y += scsi-generic.o scsi-bus.o
|
||||||
|
common-obj-y += usb.o usb-hub.o usb-$(HOST_USB).o usb-hid.o usb-msd.o usb-wacom.o
|
||||||
|
common-obj-y += usb-serial.o usb-net.o usb-bus.o usb-desc.o
|
||||||
|
common-obj-$(CONFIG_SSI) += ssi.o
|
||||||
|
common-obj-$(CONFIG_SSI_SD) += ssi-sd.o
|
||||||
|
common-obj-$(CONFIG_SD) += sd.o
|
||||||
|
common-obj-y += bt.o bt-host.o bt-vhci.o bt-l2cap.o bt-sdp.o bt-hci.o bt-hid.o usb-bt.o
|
||||||
|
common-obj-y += bt-hci-csr.o
|
||||||
|
common-obj-y += buffered_file.o migration.o migration-tcp.o
|
||||||
|
common-obj-y += qemu-char.o savevm.o #aio.o
|
||||||
|
common-obj-y += msmouse.o ps2.o
|
||||||
|
common-obj-y += qdev.o qdev-properties.o
|
||||||
|
common-obj-y += block-migration.o iohandler.o
|
||||||
|
common-obj-y += pflib.o
|
||||||
|
common-obj-y += bitmap.o bitops.o
|
||||||
|
|
||||||
|
common-obj-$(CONFIG_BRLAPI) += baum.o
|
||||||
|
common-obj-$(CONFIG_POSIX) += migration-exec.o migration-unix.o migration-fd.o
|
||||||
|
common-obj-$(CONFIG_WIN32) += version.o
|
||||||
|
|
||||||
|
common-obj-$(CONFIG_SPICE) += ui/spice-core.o ui/spice-input.o ui/spice-display.o spice-qemu-char.o
|
||||||
|
|
||||||
|
audio-obj-y = audio.o noaudio.o wavaudio.o mixeng.o
|
||||||
|
audio-obj-$(CONFIG_SDL) += sdlaudio.o
|
||||||
|
audio-obj-$(CONFIG_OSS) += ossaudio.o
|
||||||
|
audio-obj-$(CONFIG_SPICE) += spiceaudio.o
|
||||||
|
audio-obj-$(CONFIG_COREAUDIO) += coreaudio.o
|
||||||
|
audio-obj-$(CONFIG_ALSA) += alsaaudio.o
|
||||||
|
audio-obj-$(CONFIG_DSOUND) += dsoundaudio.o
|
||||||
|
audio-obj-$(CONFIG_FMOD) += fmodaudio.o
|
||||||
|
audio-obj-$(CONFIG_ESD) += esdaudio.o
|
||||||
|
audio-obj-$(CONFIG_PA) += paaudio.o
|
||||||
|
audio-obj-$(CONFIG_WINWAVE) += winwaveaudio.o
|
||||||
|
audio-obj-$(CONFIG_AUDIO_PT_INT) += audio_pt_int.o
|
||||||
|
audio-obj-$(CONFIG_AUDIO_WIN_INT) += audio_win_int.o
|
||||||
|
audio-obj-y += wavcapture.o
|
||||||
|
common-obj-y += $(addprefix audio/, $(audio-obj-y))
|
||||||
|
|
||||||
|
ui-obj-y += keymaps.o
|
||||||
|
ui-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o
|
||||||
|
ui-obj-$(CONFIG_COCOA) += cocoa.o
|
||||||
|
ui-obj-$(CONFIG_CURSES) += curses.o
|
||||||
|
vnc-obj-y += vnc.o d3des.o
|
||||||
|
vnc-obj-y += vnc-enc-zlib.o vnc-enc-hextile.o
|
||||||
|
vnc-obj-y += vnc-enc-tight.o vnc-palette.o
|
||||||
|
vnc-obj-y += vnc-enc-zrle.o
|
||||||
|
vnc-obj-$(CONFIG_VNC_TLS) += vnc-tls.o vnc-auth-vencrypt.o
|
||||||
|
vnc-obj-$(CONFIG_VNC_SASL) += vnc-auth-sasl.o
|
||||||
|
ifdef CONFIG_VNC_THREAD
|
||||||
|
vnc-obj-y += vnc-jobs-async.o
|
||||||
|
else
|
||||||
|
vnc-obj-y += vnc-jobs-sync.o
|
||||||
|
endif
|
||||||
|
common-obj-y += $(addprefix ui/, $(ui-obj-y))
|
||||||
|
common-obj-$(CONFIG_VNC) += $(addprefix ui/, $(vnc-obj-y))
|
||||||
|
|
||||||
|
common-obj-y += iov.o acl.o
|
||||||
|
#common-obj-$(CONFIG_POSIX) += compatfd.o
|
||||||
|
common-obj-y += notify.o event_notifier.o
|
||||||
|
common-obj-y += qemu-timer.o qemu-timer-common.o
|
||||||
|
|
||||||
|
slirp-obj-y = cksum.o if.o ip_icmp.o ip_input.o ip_output.o
|
||||||
|
slirp-obj-y += slirp.o mbuf.o misc.o sbuf.o socket.o tcp_input.o tcp_output.o
|
||||||
|
slirp-obj-y += tcp_subr.o tcp_timer.o udp.o bootp.o tftp.o
|
||||||
|
common-obj-$(CONFIG_SLIRP) += $(addprefix slirp/, $(slirp-obj-y))
|
||||||
|
|
||||||
|
# xen backend driver support
|
||||||
|
common-obj-$(CONFIG_XEN_BACKEND) += xen_backend.o xen_devconfig.o
|
||||||
|
common-obj-$(CONFIG_XEN_BACKEND) += xen_console.o xenfb.o xen_disk.o xen_nic.o
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
# libuser
|
||||||
|
|
||||||
|
user-obj-y =
|
||||||
|
user-obj-y += envlist.o path.o
|
||||||
|
user-obj-y += tcg-runtime.o host-utils.o
|
||||||
|
user-obj-y += cutils.o cache-utils.o
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
# libhw
|
||||||
|
|
||||||
|
hw-obj-y =
|
||||||
|
hw-obj-y += loader.o
|
||||||
|
hw-obj-$(CONFIG_VIRTIO) += virtio-console.o
|
||||||
|
hw-obj-$(CONFIG_VIRTIO_PCI) += virtio-pci.o
|
||||||
|
hw-obj-y += fw_cfg.o
|
||||||
|
hw-obj-$(CONFIG_PCI) += pci_bridge.o
|
||||||
|
hw-obj-$(CONFIG_PCI) += msix.o msi.o
|
||||||
|
hw-obj-$(CONFIG_PCI) += pci_host.o pcie_host.o
|
||||||
|
hw-obj-$(CONFIG_PCI) += ioh3420.o xio3130_upstream.o xio3130_downstream.o
|
||||||
|
hw-obj-y += watchdog.o
|
||||||
|
hw-obj-$(CONFIG_ISA_MMIO) += isa_mmio.o
|
||||||
|
hw-obj-$(CONFIG_ECC) += ecc.o
|
||||||
|
hw-obj-$(CONFIG_NAND) += nand.o
|
||||||
|
hw-obj-$(CONFIG_PFLASH_CFI01) += pflash_cfi01.o
|
||||||
|
hw-obj-$(CONFIG_PFLASH_CFI02) += pflash_cfi02.o
|
||||||
|
|
||||||
|
hw-obj-$(CONFIG_M48T59) += m48t59.o
|
||||||
|
hw-obj-$(CONFIG_ESCC) += escc.o
|
||||||
|
hw-obj-$(CONFIG_EMPTY_SLOT) += empty_slot.o
|
||||||
|
|
||||||
|
hw-obj-$(CONFIG_SERIAL) += serial.o
|
||||||
|
hw-obj-$(CONFIG_PARALLEL) += parallel.o
|
||||||
|
# Moved back to Makefile.target due to #include qemu-kvm.h:
|
||||||
|
#hw-obj-$(CONFIG_I8254) += i8254.o
|
||||||
|
#hw-obj-$(CONFIG_PCSPK) += pcspk.o
|
||||||
|
hw-obj-$(CONFIG_PCKBD) += pckbd.o
|
||||||
|
hw-obj-$(CONFIG_USB_UHCI) += usb-uhci.o
|
||||||
|
hw-obj-$(CONFIG_USB_OHCI) += usb-ohci.o
|
||||||
|
hw-obj-$(CONFIG_USB_EHCI) += usb-ehci.o
|
||||||
|
hw-obj-$(CONFIG_FDC) += fdc.o
|
||||||
|
# needs fixes for cpu hotplug, so moved to Makefile.target:
|
||||||
|
# hw-obj-$(CONFIG_ACPI) += acpi.o acpi_piix4.o
|
||||||
|
hw-obj-$(CONFIG_APM) += pm_smbus.o apm.o
|
||||||
|
hw-obj-$(CONFIG_DMA) += dma.o
|
||||||
|
hw-obj-$(CONFIG_HPET) += hpet.o
|
||||||
|
hw-obj-$(CONFIG_APPLESMC) += applesmc.o
|
||||||
|
hw-obj-$(CONFIG_SMARTCARD) += usb-ccid.o ccid-card-passthru.o
|
||||||
|
hw-obj-$(CONFIG_SMARTCARD_NSS) += ccid-card-emulated.o
|
||||||
|
hw-obj-$(CONFIG_USB_REDIR) += usb-redir.o
|
||||||
|
|
||||||
|
# PPC devices
|
||||||
|
hw-obj-$(CONFIG_OPENPIC) += openpic.o
|
||||||
|
hw-obj-$(CONFIG_PREP_PCI) += prep_pci.o
|
||||||
|
# Mac shared devices
|
||||||
|
hw-obj-$(CONFIG_MACIO) += macio.o
|
||||||
|
hw-obj-$(CONFIG_CUDA) += cuda.o
|
||||||
|
hw-obj-$(CONFIG_ADB) += adb.o
|
||||||
|
hw-obj-$(CONFIG_MAC_NVRAM) += mac_nvram.o
|
||||||
|
hw-obj-$(CONFIG_MAC_DBDMA) += mac_dbdma.o
|
||||||
|
# OldWorld PowerMac
|
||||||
|
hw-obj-$(CONFIG_HEATHROW_PIC) += heathrow_pic.o
|
||||||
|
hw-obj-$(CONFIG_GRACKLE_PCI) += grackle_pci.o
|
||||||
|
# NewWorld PowerMac
|
||||||
|
hw-obj-$(CONFIG_UNIN_PCI) += unin_pci.o
|
||||||
|
hw-obj-$(CONFIG_DEC_PCI) += dec_pci.o
|
||||||
|
# PowerPC E500 boards
|
||||||
|
hw-obj-$(CONFIG_PPCE500_PCI) += ppce500_pci.o
|
||||||
|
|
||||||
|
# MIPS devices
|
||||||
|
hw-obj-$(CONFIG_PIIX4) += piix4.o
|
||||||
|
|
||||||
|
# PCI watchdog devices
|
||||||
|
hw-obj-$(CONFIG_PCI) += wdt_i6300esb.o
|
||||||
|
|
||||||
|
hw-obj-$(CONFIG_PCI) += pcie.o pcie_aer.o pcie_port.o
|
||||||
|
|
||||||
|
# PCI network cards
|
||||||
|
hw-obj-$(CONFIG_NE2000_PCI) += ne2000.o
|
||||||
|
hw-obj-$(CONFIG_EEPRO100_PCI) += eepro100.o
|
||||||
|
hw-obj-$(CONFIG_PCNET_PCI) += pcnet-pci.o
|
||||||
|
hw-obj-$(CONFIG_PCNET_COMMON) += pcnet.o
|
||||||
|
hw-obj-$(CONFIG_E1000_PCI) += e1000.o
|
||||||
|
hw-obj-$(CONFIG_RTL8139_PCI) += rtl8139.o
|
||||||
|
|
||||||
|
hw-obj-$(CONFIG_SMC91C111) += smc91c111.o
|
||||||
|
hw-obj-$(CONFIG_LAN9118) += lan9118.o
|
||||||
|
hw-obj-$(CONFIG_NE2000_ISA) += ne2000-isa.o
|
||||||
|
|
||||||
|
# IDE
|
||||||
|
hw-obj-$(CONFIG_IDE_CORE) += ide/core.o ide/atapi.o
|
||||||
|
hw-obj-$(CONFIG_IDE_QDEV) += ide/qdev.o
|
||||||
|
hw-obj-$(CONFIG_IDE_PCI) += ide/pci.o
|
||||||
|
hw-obj-$(CONFIG_IDE_ISA) += ide/isa.o
|
||||||
|
hw-obj-$(CONFIG_IDE_PIIX) += ide/piix.o
|
||||||
|
hw-obj-$(CONFIG_IDE_CMD646) += ide/cmd646.o
|
||||||
|
hw-obj-$(CONFIG_IDE_MACIO) += ide/macio.o
|
||||||
|
hw-obj-$(CONFIG_IDE_VIA) += ide/via.o
|
||||||
|
hw-obj-$(CONFIG_AHCI) += ide/ahci.o
|
||||||
|
hw-obj-$(CONFIG_AHCI) += ide/ich.o
|
||||||
|
|
||||||
|
# SCSI layer
|
||||||
|
hw-obj-$(CONFIG_LSI_SCSI_PCI) += lsi53c895a.o
|
||||||
|
hw-obj-$(CONFIG_ESP) += esp.o
|
||||||
|
|
||||||
|
hw-obj-y += dma-helpers.o sysbus.o isa-bus.o
|
||||||
|
hw-obj-y += qdev-addr.o
|
||||||
|
|
||||||
|
# VGA
|
||||||
|
hw-obj-$(CONFIG_VGA_PCI) += vga-pci.o
|
||||||
|
hw-obj-$(CONFIG_VGA_ISA) += vga-isa.o
|
||||||
|
hw-obj-$(CONFIG_VGA_ISA_MM) += vga-isa-mm.o
|
||||||
|
hw-obj-$(CONFIG_VMWARE_VGA) += vmware_vga.o
|
||||||
|
hw-obj-$(CONFIG_VMMOUSE) += vmmouse.o
|
||||||
|
|
||||||
|
hw-obj-$(CONFIG_RC4030) += rc4030.o
|
||||||
|
hw-obj-$(CONFIG_DP8393X) += dp8393x.o
|
||||||
|
hw-obj-$(CONFIG_DS1225Y) += ds1225y.o
|
||||||
|
hw-obj-$(CONFIG_MIPSNET) += mipsnet.o
|
||||||
|
|
||||||
|
# Sound
|
||||||
|
sound-obj-y =
|
||||||
|
sound-obj-$(CONFIG_SB16) += sb16.o
|
||||||
|
sound-obj-$(CONFIG_ES1370) += es1370.o
|
||||||
|
sound-obj-$(CONFIG_AC97) += ac97.o
|
||||||
|
sound-obj-$(CONFIG_ADLIB) += fmopl.o adlib.o
|
||||||
|
sound-obj-$(CONFIG_GUS) += gus.o gusemu_hal.o gusemu_mixer.o
|
||||||
|
sound-obj-$(CONFIG_CS4231A) += cs4231a.o
|
||||||
|
sound-obj-$(CONFIG_HDA) += intel-hda.o hda-audio.o
|
||||||
|
|
||||||
|
adlib.o fmopl.o: QEMU_CFLAGS += -DBUILD_Y8950=0
|
||||||
|
hw-obj-$(CONFIG_SOUND) += $(sound-obj-y)
|
||||||
|
|
||||||
|
9pfs-nested-$(CONFIG_VIRTFS) = virtio-9p.o virtio-9p-debug.o
|
||||||
|
9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-local.o virtio-9p-xattr.o
|
||||||
|
9pfs-nested-$(CONFIG_VIRTFS) += virtio-9p-xattr-user.o virtio-9p-posix-acl.o
|
||||||
|
|
||||||
|
hw-obj-$(CONFIG_REALLY_VIRTFS) += $(addprefix 9pfs/, $(9pfs-nested-y))
|
||||||
|
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
# libdis
|
||||||
|
# NOTE: the disassembler code is only needed for debugging
|
||||||
|
|
||||||
|
libdis-y =
|
||||||
|
libdis-$(CONFIG_ALPHA_DIS) += alpha-dis.o
|
||||||
|
libdis-$(CONFIG_ARM_DIS) += arm-dis.o
|
||||||
|
libdis-$(CONFIG_CRIS_DIS) += cris-dis.o
|
||||||
|
libdis-$(CONFIG_HPPA_DIS) += hppa-dis.o
|
||||||
|
libdis-$(CONFIG_I386_DIS) += i386-dis.o
|
||||||
|
libdis-$(CONFIG_IA64_DIS) += ia64-dis.o
|
||||||
|
libdis-$(CONFIG_M68K_DIS) += m68k-dis.o
|
||||||
|
libdis-$(CONFIG_MICROBLAZE_DIS) += microblaze-dis.o
|
||||||
|
libdis-$(CONFIG_MIPS_DIS) += mips-dis.o
|
||||||
|
libdis-$(CONFIG_PPC_DIS) += ppc-dis.o
|
||||||
|
libdis-$(CONFIG_S390_DIS) += s390-dis.o
|
||||||
|
libdis-$(CONFIG_SH4_DIS) += sh4-dis.o
|
||||||
|
libdis-$(CONFIG_SPARC_DIS) += sparc-dis.o
|
||||||
|
|
||||||
|
######################################################################
|
||||||
|
# trace
|
||||||
|
|
||||||
|
ifeq ($(TRACE_BACKEND),dtrace)
|
||||||
|
trace.h: trace.h-timestamp trace-dtrace.h
|
||||||
|
else
|
||||||
|
trace.h: trace.h-timestamp
|
||||||
|
endif
|
||||||
|
trace.h-timestamp: $(SRC_PATH)/trace-events config-host.mak
|
||||||
|
$(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -h < $< > $@," GEN trace.h")
|
||||||
|
@cmp -s $@ trace.h || cp $@ trace.h
|
||||||
|
|
||||||
|
trace.c: trace.c-timestamp
|
||||||
|
trace.c-timestamp: $(SRC_PATH)/trace-events config-host.mak
|
||||||
|
$(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -c < $< > $@," GEN trace.c")
|
||||||
|
@cmp -s $@ trace.c || cp $@ trace.c
|
||||||
|
|
||||||
|
trace.o: trace.c $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
trace-dtrace.h: trace-dtrace.dtrace
|
||||||
|
$(call quiet-command,dtrace -o $@ -h -s $<, " GEN trace-dtrace.h")
|
||||||
|
|
||||||
|
# Normal practice is to name DTrace probe file with a '.d' extension
|
||||||
|
# but that gets picked up by QEMU's Makefile as an external dependency
|
||||||
|
# rule file. So we use '.dtrace' instead
|
||||||
|
trace-dtrace.dtrace: trace-dtrace.dtrace-timestamp
|
||||||
|
trace-dtrace.dtrace-timestamp: $(SRC_PATH)/trace-events config-host.mak
|
||||||
|
$(call quiet-command,sh $(SRC_PATH)/scripts/tracetool --$(TRACE_BACKEND) -d < $< > $@," GEN trace-dtrace.dtrace")
|
||||||
|
@cmp -s $@ trace-dtrace.dtrace || cp $@ trace-dtrace.dtrace
|
||||||
|
|
||||||
|
trace-dtrace.o: trace-dtrace.dtrace $(GENERATED_HEADERS)
|
||||||
|
$(call quiet-command,dtrace -o $@ -G -s $<, " GEN trace-dtrace.o")
|
||||||
|
|
||||||
|
ifeq ($(LIBTOOL),)
|
||||||
|
trace-dtrace.lo: trace-dtrace.dtrace
|
||||||
|
@echo "missing libtool. please install and rerun configure."; exit 1
|
||||||
|
else
|
||||||
|
trace-dtrace.lo: trace-dtrace.dtrace
|
||||||
|
$(call quiet-command,libtool --mode=compile --tag=CC dtrace -o $@ -G -s $<, " lt GEN trace-dtrace.o")
|
||||||
|
endif
|
||||||
|
|
||||||
|
simpletrace.o: simpletrace.c $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
ifeq ($(TRACE_BACKEND),dtrace)
|
||||||
|
trace-obj-y = trace-dtrace.o
|
||||||
|
else
|
||||||
|
trace-obj-y = trace.o
|
||||||
|
ifeq ($(TRACE_BACKEND),simple)
|
||||||
|
trace-obj-y += simpletrace.o
|
||||||
|
user-obj-y += qemu-timer-common.o
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# smartcard
|
# smartcard
|
||||||
|
|
||||||
libcacard-y += libcacard/cac.o libcacard/event.o
|
libcacard-y = cac.o event.o vcard.o vreader.o vcard_emul_nss.o vcard_emul_type.o card_7816.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
|
|
||||||
libcacard-y += libcacard/vcardt.o
|
|
||||||
libcacard/vcard_emul_nss.o-cflags := $(NSS_CFLAGS)
|
|
||||||
libcacard/vcard_emul_nss.o-libs := $(NSS_LIBS)
|
|
||||||
|
|
||||||
######################################################################
|
|
||||||
# Target independent part of system emulation. The long term path is to
|
|
||||||
# suppress *all* target specific code in case of system emulation, i.e. a
|
|
||||||
# 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 += net/
|
|
||||||
common-obj-y += qdev-monitor.o device-hotplug.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 += qemu-char.o #aio.o
|
|
||||||
common-obj-y += page_cache.o
|
|
||||||
common-obj-y += qjson.o
|
|
||||||
|
|
||||||
common-obj-$(CONFIG_SPICE) += spice-qemu-char.o
|
|
||||||
|
|
||||||
common-obj-y += audio/
|
|
||||||
common-obj-y += hw/
|
|
||||||
common-obj-y += accel.o
|
|
||||||
|
|
||||||
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 += 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-$(CONFIG_SECCOMP) += qemu-seccomp.o
|
|
||||||
|
|
||||||
common-obj-$(CONFIG_SMARTCARD_NSS) += $(libcacard-y)
|
|
||||||
|
|
||||||
######################################################################
|
######################################################################
|
||||||
# qapi
|
# qapi
|
||||||
|
|
||||||
common-obj-y += qmp-marshal.o
|
qapi-nested-y = qapi-visit-core.o qmp-input-visitor.o qmp-output-visitor.o qapi-dealloc-visitor.o
|
||||||
common-obj-y += qmp.o hmp.o
|
qapi-nested-y += qmp-registry.o qmp-dispatch.o
|
||||||
endif
|
qapi-obj-y = $(addprefix qapi/, $(qapi-nested-y))
|
||||||
|
|
||||||
#######################################################################
|
vl.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
|
||||||
# Target-independent parts used in system and user emulation
|
|
||||||
common-obj-y += qemu-log.o
|
|
||||||
common-obj-y += tcg-runtime.o
|
|
||||||
common-obj-y += hw/
|
|
||||||
common-obj-y += qom/
|
|
||||||
common-obj-y += disas/
|
|
||||||
|
|
||||||
######################################################################
|
vl.o: QEMU_CFLAGS+=$(SDL_CFLAGS)
|
||||||
# Resource file for Windows executables
|
|
||||||
version-obj-$(CONFIG_WIN32) += $(BUILD_DIR)/version.o
|
|
||||||
version-lobj-$(CONFIG_WIN32) += $(BUILD_DIR)/version.lo
|
|
||||||
|
|
||||||
######################################################################
|
vl.o: QEMU_CFLAGS+=$(GLIB_CFLAGS)
|
||||||
# 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/
|
|
||||||
|
|||||||
482
Makefile.target
482
Makefile.target
@@ -1,44 +1,51 @@
|
|||||||
# -*- Mode: makefile -*-
|
# -*- Mode: makefile -*-
|
||||||
|
|
||||||
include ../config-host.mak
|
GENERATED_HEADERS = config-target.h
|
||||||
include config-target.mak
|
CONFIG_NO_PCI = $(if $(subst n,,$(CONFIG_PCI)),n,y)
|
||||||
include config-devices.mak
|
CONFIG_NO_KVM = $(if $(subst n,,$(CONFIG_KVM)),n,y)
|
||||||
include $(SRC_PATH)/rules.mak
|
CONFIG_NO_XEN = $(if $(subst n,,$(CONFIG_XEN)),n,y)
|
||||||
|
|
||||||
$(call set-vpath, $(SRC_PATH))
|
include ../config-host.mak
|
||||||
|
include config-devices.mak
|
||||||
|
include config-target.mak
|
||||||
|
include $(SRC_PATH)/rules.mak
|
||||||
|
ifneq ($(HWDIR),)
|
||||||
|
include $(HWDIR)/config.mak
|
||||||
|
endif
|
||||||
|
|
||||||
|
TARGET_PATH=$(SRC_PATH)/target-$(TARGET_BASE_ARCH)
|
||||||
|
$(call set-vpath, $(SRC_PATH):$(TARGET_PATH):$(SRC_PATH)/hw)
|
||||||
ifdef CONFIG_LINUX
|
ifdef CONFIG_LINUX
|
||||||
QEMU_CFLAGS += -I../linux-headers
|
QEMU_CFLAGS += -I../linux-headers
|
||||||
endif
|
endif
|
||||||
QEMU_CFLAGS += -I.. -I$(SRC_PATH)/target-$(TARGET_BASE_ARCH) -DNEED_CPU_H
|
QEMU_CFLAGS += -I.. -I$(TARGET_PATH) -DNEED_CPU_H
|
||||||
|
|
||||||
QEMU_CFLAGS+=-I$(SRC_PATH)/include
|
include $(SRC_PATH)/Makefile.objs
|
||||||
|
|
||||||
ifdef CONFIG_USER_ONLY
|
ifdef CONFIG_USER_ONLY
|
||||||
# user emulator name
|
# user emulator name
|
||||||
QEMU_PROG=qemu-$(TARGET_NAME)
|
QEMU_PROG=qemu-$(TARGET_ARCH2)
|
||||||
QEMU_PROG_BUILD = $(QEMU_PROG)
|
|
||||||
else
|
else
|
||||||
# system emulator name
|
# system emulator name
|
||||||
QEMU_PROG=qemu-system-$(TARGET_NAME)$(EXESUF)
|
ifeq ($(TARGET_ARCH), i386)
|
||||||
ifneq (,$(findstring -mwindows,$(libs_softmmu)))
|
QEMU_PROG=qemu$(EXESUF)
|
||||||
# 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
|
else
|
||||||
QEMU_PROG_BUILD = $(QEMU_PROG)
|
QEMU_PROG=qemu-system-$(TARGET_ARCH2)$(EXESUF)
|
||||||
endif
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
PROGS=$(QEMU_PROG) $(QEMU_PROGW)
|
PROGS=$(QEMU_PROG)
|
||||||
STPFILES=
|
STPFILES=
|
||||||
|
|
||||||
|
ifndef CONFIG_HAIKU
|
||||||
|
LIBS+=-lm
|
||||||
|
endif
|
||||||
|
|
||||||
config-target.h: config-target.h-timestamp
|
config-target.h: config-target.h-timestamp
|
||||||
config-target.h-timestamp: config-target.mak
|
config-target.h-timestamp: config-target.mak
|
||||||
|
|
||||||
ifdef CONFIG_TRACE_SYSTEMTAP
|
ifdef CONFIG_SYSTEMTAP_TRACE
|
||||||
stap: $(QEMU_PROG).stp-installed $(QEMU_PROG).stp $(QEMU_PROG)-simpletrace.stp
|
stap: $(QEMU_PROG).stp
|
||||||
|
|
||||||
ifdef CONFIG_USER_ONLY
|
ifdef CONFIG_USER_ONLY
|
||||||
TARGET_TYPE=user
|
TARGET_TYPE=user
|
||||||
@@ -46,31 +53,13 @@ else
|
|||||||
TARGET_TYPE=system
|
TARGET_TYPE=system
|
||||||
endif
|
endif
|
||||||
|
|
||||||
$(QEMU_PROG).stp-installed: $(SRC_PATH)/trace-events
|
$(QEMU_PROG).stp:
|
||||||
$(call quiet-command,$(TRACETOOL) \
|
$(call quiet-command,sh $(SRC_PATH)/scripts/tracetool \
|
||||||
--format=stap \
|
--$(TRACE_BACKEND) \
|
||||||
--backends=$(TRACE_BACKENDS) \
|
--binary $(bindir)/$(QEMU_PROG) \
|
||||||
--binary=$(bindir)/$(QEMU_PROG) \
|
--target-arch $(TARGET_ARCH) \
|
||||||
--target-name=$(TARGET_NAME) \
|
--target-type $(TARGET_TYPE) \
|
||||||
--target-type=$(TARGET_TYPE) \
|
--stap < $(SRC_PATH)/trace-events > $(QEMU_PROG).stp," GEN $(QEMU_PROG).stp")
|
||||||
< $< > $@," GEN $(TARGET_DIR)$(QEMU_PROG).stp-installed")
|
|
||||||
|
|
||||||
$(QEMU_PROG).stp: $(SRC_PATH)/trace-events
|
|
||||||
$(call quiet-command,$(TRACETOOL) \
|
|
||||||
--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: $(SRC_PATH)/trace-events
|
|
||||||
$(call quiet-command,$(TRACETOOL) \
|
|
||||||
--format=simpletrace-stap \
|
|
||||||
--backends=$(TRACE_BACKENDS) \
|
|
||||||
--probe-prefix=qemu.$(TARGET_TYPE).$(TARGET_NAME) \
|
|
||||||
< $< > $@," GEN $(TARGET_DIR)$(QEMU_PROG)-simpletrace.stp")
|
|
||||||
|
|
||||||
else
|
else
|
||||||
stap:
|
stap:
|
||||||
endif
|
endif
|
||||||
@@ -82,102 +71,359 @@ all: $(PROGS) stap
|
|||||||
|
|
||||||
#########################################################
|
#########################################################
|
||||||
# cpu emulator library
|
# cpu emulator library
|
||||||
obj-y = exec.o translate-all.o cpu-exec.o
|
libobj-y = exec.o cpu-exec.o
|
||||||
obj-y += tcg/tcg.o tcg/tcg-op.o tcg/optimize.o
|
libobj-$(CONFIG_NO_CPU_EMULATION) += fake-exec.o
|
||||||
obj-$(CONFIG_TCG_INTERPRETER) += tci.o
|
libobj-$(CONFIG_CPU_EMULATION) += translate-all.o translate.o
|
||||||
obj-$(CONFIG_TCG_INTERPRETER) += disas/tci.o
|
libobj-$(CONFIG_CPU_EMULATION) += tcg/tcg.o
|
||||||
obj-y += fpu/softfloat.o
|
libobj-y += fpu/softfloat.o
|
||||||
obj-y += target-$(TARGET_BASE_ARCH)/
|
libobj-y += op_helper.o helper.o
|
||||||
obj-y += disas.o
|
ifeq ($(TARGET_BASE_ARCH), i386)
|
||||||
obj-$(call notempty,$(TARGET_XML_FILES)) += gdbstub-xml.o
|
libobj-y += cpuid.o
|
||||||
obj-$(call lnot,$(CONFIG_KVM)) += kvm-stub.o
|
endif
|
||||||
|
libobj-$(CONFIG_NEED_MMU) += mmu.o
|
||||||
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decContext.o
|
libobj-$(CONFIG_KVM) += kvm-tpr-opt.o
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/decNumber.o
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal32.o
|
libobj-$(TARGET_ARM) += neon_helper.o iwmmxt_helper.o
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal64.o
|
|
||||||
obj-$(CONFIG_LIBDECNUMBER) += libdecnumber/dpd/decimal128.o
|
libobj-y += disas.o
|
||||||
|
|
||||||
|
$(libobj-y): $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
# libqemu
|
||||||
|
|
||||||
|
translate.o: translate.c cpu.h
|
||||||
|
|
||||||
|
translate-all.o: translate-all.c cpu.h
|
||||||
|
|
||||||
|
tcg/tcg.o: cpu.h
|
||||||
|
|
||||||
|
# HELPER_CFLAGS is used for all the code compiled with static register
|
||||||
|
# variables
|
||||||
|
op_helper.o user-exec.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
|
||||||
|
|
||||||
|
# Note: this is a workaround. The real fix is to avoid compiling
|
||||||
|
# cpu_signal_handler() in user-exec.c.
|
||||||
|
signal.o: QEMU_CFLAGS += $(HELPER_CFLAGS)
|
||||||
|
|
||||||
#########################################################
|
#########################################################
|
||||||
# Linux user emulator target
|
# Linux user emulator target
|
||||||
|
|
||||||
ifdef CONFIG_LINUX_USER
|
ifdef CONFIG_LINUX_USER
|
||||||
|
|
||||||
QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) -I$(SRC_PATH)/linux-user
|
$(call set-vpath, $(SRC_PATH)/linux-user:$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR))
|
||||||
|
|
||||||
obj-y += linux-user/
|
QEMU_CFLAGS+=-I$(SRC_PATH)/linux-user/$(TARGET_ABI_DIR) -I$(SRC_PATH)/linux-user
|
||||||
obj-y += gdbstub.o thunk.o user-exec.o
|
obj-y = main.o syscall.o strace.o mmap.o signal.o thunk.o \
|
||||||
|
elfload.o linuxload.o uaccess.o gdbstub.o cpu-uname.o \
|
||||||
|
qemu-malloc.o user-exec.o $(oslib-obj-y)
|
||||||
|
|
||||||
|
obj-$(TARGET_HAS_BFLT) += flatload.o
|
||||||
|
|
||||||
|
obj-$(TARGET_I386) += vm86.o
|
||||||
|
|
||||||
|
obj-i386-y += ioport-user.o
|
||||||
|
|
||||||
|
nwfpe-obj-y = fpa11.o fpa11_cpdo.o fpa11_cpdt.o fpa11_cprt.o fpopcode.o
|
||||||
|
nwfpe-obj-y += single_cpdo.o double_cpdo.o extended_cpdo.o
|
||||||
|
obj-arm-y += $(addprefix nwfpe/, $(nwfpe-obj-y))
|
||||||
|
obj-arm-y += arm-semi.o
|
||||||
|
|
||||||
|
obj-m68k-y += m68k-sim.o m68k-semi.o
|
||||||
|
|
||||||
|
$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y): $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
obj-y += $(addprefix ../libuser/, $(user-obj-y))
|
||||||
|
obj-y += $(addprefix ../libdis-user/, $(libdis-y))
|
||||||
|
obj-y += $(libobj-y)
|
||||||
|
|
||||||
endif #CONFIG_LINUX_USER
|
endif #CONFIG_LINUX_USER
|
||||||
|
|
||||||
|
#########################################################
|
||||||
|
# Darwin user emulator target
|
||||||
|
|
||||||
|
ifdef CONFIG_DARWIN_USER
|
||||||
|
|
||||||
|
$(call set-vpath, $(SRC_PATH)/darwin-user)
|
||||||
|
|
||||||
|
QEMU_CFLAGS+=-I$(SRC_PATH)/darwin-user -I$(SRC_PATH)/darwin-user/$(TARGET_ARCH)
|
||||||
|
|
||||||
|
# Leave some space for the regular program loading zone
|
||||||
|
LDFLAGS+=-Wl,-segaddr,__STD_PROG_ZONE,0x1000 -image_base 0x0e000000
|
||||||
|
|
||||||
|
LIBS+=-lmx
|
||||||
|
|
||||||
|
obj-y = main.o commpage.o machload.o mmap.o signal.o syscall.o thunk.o \
|
||||||
|
gdbstub.o user-exec.o
|
||||||
|
|
||||||
|
obj-i386-y += ioport-user.o
|
||||||
|
|
||||||
|
$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y): $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
obj-y += $(addprefix ../libuser/, $(user-obj-y))
|
||||||
|
obj-y += $(addprefix ../libdis-user/, $(libdis-y))
|
||||||
|
obj-y += $(libobj-y)
|
||||||
|
|
||||||
|
endif #CONFIG_DARWIN_USER
|
||||||
|
|
||||||
#########################################################
|
#########################################################
|
||||||
# BSD user emulator target
|
# BSD user emulator target
|
||||||
|
|
||||||
ifdef CONFIG_BSD_USER
|
ifdef CONFIG_BSD_USER
|
||||||
|
|
||||||
QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ABI_DIR) \
|
$(call set-vpath, $(SRC_PATH)/bsd-user)
|
||||||
-I$(SRC_PATH)/bsd-user/$(HOST_VARIANT_DIR)
|
|
||||||
|
|
||||||
obj-y += bsd-user/
|
QEMU_CFLAGS+=-I$(SRC_PATH)/bsd-user -I$(SRC_PATH)/bsd-user/$(TARGET_ARCH)
|
||||||
obj-y += gdbstub.o user-exec.o
|
|
||||||
|
obj-y = main.o bsdload.o elfload.o mmap.o signal.o strace.o syscall.o \
|
||||||
|
gdbstub.o uaccess.o user-exec.o
|
||||||
|
|
||||||
|
obj-i386-y += ioport-user.o
|
||||||
|
|
||||||
|
$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y): $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
obj-y += $(addprefix ../libuser/, $(user-obj-y))
|
||||||
|
obj-y += $(addprefix ../libdis-user/, $(libdis-y))
|
||||||
|
obj-y += $(libobj-y)
|
||||||
|
|
||||||
endif #CONFIG_BSD_USER
|
endif #CONFIG_BSD_USER
|
||||||
|
|
||||||
#########################################################
|
#########################################################
|
||||||
# System emulator target
|
# System emulator target
|
||||||
ifdef CONFIG_SOFTMMU
|
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
|
obj-y = arch_init.o cpus.o monitor.o machine.o gdbstub.o vl.o balloon.o
|
||||||
obj-y += hw/
|
# virtio has to be here due to weird dependency between PCI and virtio-net.
|
||||||
obj-$(CONFIG_FDT) += device_tree.o
|
# need to fix this properly
|
||||||
obj-$(CONFIG_KVM) += kvm-all.o
|
obj-$(CONFIG_NO_PCI) += pci-stub.o
|
||||||
obj-y += memory.o savevm.o cputlb.o
|
obj-$(CONFIG_PCI) += pci.o
|
||||||
obj-y += memory_mapping.o
|
obj-$(CONFIG_VIRTIO) += virtio.o virtio-blk.o virtio-balloon.o virtio-net.o virtio-serial-bus.o
|
||||||
obj-y += dump.o
|
obj-y += vhost_net.o
|
||||||
LIBS+=$(libs_softmmu)
|
obj-$(CONFIG_VHOST_NET) += vhost.o
|
||||||
|
obj-$(CONFIG_REALLY_VIRTFS) += 9pfs/virtio-9p-device.o
|
||||||
|
obj-y += rwhandler.o
|
||||||
|
obj-$(CONFIG_KVM) += kvm.o kvm-all.o
|
||||||
|
obj-$(CONFIG_NO_KVM) += kvm-stub.o
|
||||||
|
LIBS+=-lz
|
||||||
|
|
||||||
|
QEMU_CFLAGS += $(VNC_TLS_CFLAGS)
|
||||||
|
QEMU_CFLAGS += $(VNC_SASL_CFLAGS)
|
||||||
|
QEMU_CFLAGS += $(VNC_JPEG_CFLAGS)
|
||||||
|
QEMU_CFLAGS += $(VNC_PNG_CFLAGS)
|
||||||
|
QEMU_CFLAGS += $(GLIB_CFLAGS)
|
||||||
|
|
||||||
# xen support
|
# xen support
|
||||||
obj-$(CONFIG_XEN) += xen-common.o
|
obj-$(CONFIG_XEN) += xen-all.o xen_machine_pv.o xen_domainbuild.o xen-mapcache.o
|
||||||
obj-$(CONFIG_XEN_I386) += xen-hvm.o xen-mapcache.o
|
obj-$(CONFIG_NO_XEN) += xen-stub.o
|
||||||
obj-$(call lnot,$(CONFIG_XEN)) += xen-common-stub.o
|
|
||||||
obj-$(call lnot,$(CONFIG_XEN_I386)) += xen-hvm-stub.o
|
obj-i386-$(CONFIG_XEN) += xen_platform.o
|
||||||
|
|
||||||
|
# Inter-VM PCI shared memory
|
||||||
|
CONFIG_IVSHMEM =
|
||||||
|
ifeq ($(CONFIG_KVM), y)
|
||||||
|
ifeq ($(CONFIG_PCI), y)
|
||||||
|
CONFIG_IVSHMEM = y
|
||||||
|
endif
|
||||||
|
endif
|
||||||
|
obj-$(CONFIG_IVSHMEM) += ivshmem.o
|
||||||
|
|
||||||
# Hardware support
|
# Hardware support
|
||||||
ifeq ($(TARGET_NAME), sparc64)
|
obj-i386-y += vga.o
|
||||||
obj-y += hw/sparc64/
|
obj-i386-y += mc146818rtc.o i8259.o pc.o
|
||||||
|
obj-i386-y += cirrus_vga.o sga.o apic.o ioapic.o piix_pci.o
|
||||||
|
obj-i386-y += vmport.o
|
||||||
|
obj-i386-y += device-hotplug.o pci-hotplug.o smbios.o wdt_ib700.o
|
||||||
|
obj-i386-y += extboot.o
|
||||||
|
obj-i386-y += debugcon.o multiboot.o
|
||||||
|
obj-i386-y += pc_piix.o
|
||||||
|
obj-i386-$(CONFIG_KVM) += kvmclock.o
|
||||||
|
obj-i386-$(CONFIG_SPICE) += qxl.o qxl-logger.o qxl-render.o
|
||||||
|
obj-i386-y += testdev.o
|
||||||
|
obj-i386-y += acpi.o acpi_piix4.o
|
||||||
|
|
||||||
|
obj-i386-y += pcspk.o i8254.o
|
||||||
|
obj-i386-$(CONFIG_KVM_PIT) += i8254-kvm.o
|
||||||
|
obj-i386-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += device-assignment.o
|
||||||
|
|
||||||
|
# Hardware support
|
||||||
|
obj-ia64-y += ide.o pckbd.o vga.o $(SOUND_HW) dma.o $(AUDIODRV)
|
||||||
|
obj-ia64-y += fdc.o mc146818rtc.o serial.o i8259.o ipf.o
|
||||||
|
obj-ia64-y += cirrus_vga.o parallel.o acpi.o piix_pci.o
|
||||||
|
obj-ia64-y += usb-uhci.o
|
||||||
|
obj-ia64-$(CONFIG_KVM_DEVICE_ASSIGNMENT) += device-assignment.o
|
||||||
|
|
||||||
|
# shared objects
|
||||||
|
obj-ppc-y = ppc.o
|
||||||
|
obj-ppc-y += vga.o
|
||||||
|
# PREP target
|
||||||
|
obj-ppc-y += i8259.o mc146818rtc.o
|
||||||
|
obj-ppc-y += ppc_prep.o
|
||||||
|
# OldWorld PowerMac
|
||||||
|
obj-ppc-y += ppc_oldworld.o
|
||||||
|
# NewWorld PowerMac
|
||||||
|
obj-ppc-y += ppc_newworld.o
|
||||||
|
# IBM pSeries (sPAPR)
|
||||||
|
ifeq ($(CONFIG_FDT)$(TARGET_PPC64),yy)
|
||||||
|
obj-ppc-y += spapr.o spapr_hcall.o spapr_rtas.o spapr_vio.o
|
||||||
|
obj-ppc-y += xics.o spapr_vty.o spapr_llan.o spapr_vscsi.o
|
||||||
|
endif
|
||||||
|
# PowerPC 4xx boards
|
||||||
|
obj-ppc-y += ppc4xx_devs.o ppc4xx_pci.o ppc405_uc.o ppc405_boards.o
|
||||||
|
obj-ppc-y += ppc440.o ppc440_bamboo.o
|
||||||
|
# PowerPC E500 boards
|
||||||
|
obj-ppc-y += ppce500_mpc8544ds.o mpc8544_guts.o
|
||||||
|
# PowerPC 440 Xilinx ML507 reference board.
|
||||||
|
obj-ppc-y += virtex_ml507.o
|
||||||
|
obj-ppc-$(CONFIG_KVM) += kvm_ppc.o
|
||||||
|
obj-ppc-$(CONFIG_FDT) += device_tree.o
|
||||||
|
|
||||||
|
# Xilinx PPC peripherals
|
||||||
|
obj-ppc-y += xilinx_intc.o
|
||||||
|
obj-ppc-y += xilinx_timer.o
|
||||||
|
obj-ppc-y += xilinx_uartlite.o
|
||||||
|
obj-ppc-y += xilinx_ethlite.o
|
||||||
|
|
||||||
|
# LM32 boards
|
||||||
|
obj-lm32-y += lm32_boards.o
|
||||||
|
obj-lm32-y += milkymist.o
|
||||||
|
|
||||||
|
# LM32 peripherals
|
||||||
|
obj-lm32-y += lm32_pic.o
|
||||||
|
obj-lm32-y += lm32_juart.o
|
||||||
|
obj-lm32-y += lm32_timer.o
|
||||||
|
obj-lm32-y += lm32_uart.o
|
||||||
|
obj-lm32-y += lm32_sys.o
|
||||||
|
obj-lm32-y += milkymist-ac97.o
|
||||||
|
obj-lm32-y += milkymist-hpdmc.o
|
||||||
|
obj-lm32-y += milkymist-memcard.o
|
||||||
|
obj-lm32-y += milkymist-minimac2.o
|
||||||
|
obj-lm32-y += milkymist-pfpu.o
|
||||||
|
obj-lm32-y += milkymist-softusb.o
|
||||||
|
obj-lm32-y += milkymist-sysctl.o
|
||||||
|
obj-lm32-$(CONFIG_OPENGL) += milkymist-tmu2.o
|
||||||
|
obj-lm32-y += milkymist-uart.o
|
||||||
|
obj-lm32-y += milkymist-vgafb.o
|
||||||
|
obj-lm32-y += framebuffer.o
|
||||||
|
|
||||||
|
obj-mips-y = mips_r4k.o mips_jazz.o mips_malta.o mips_mipssim.o
|
||||||
|
obj-mips-y += pcspk.o i8254.o
|
||||||
|
obj-mips-y += acpi.o acpi_piix4.o
|
||||||
|
obj-mips-y += mips_addr.o mips_timer.o mips_int.o
|
||||||
|
obj-mips-y += vga.o i8259.o
|
||||||
|
obj-mips-y += g364fb.o jazz_led.o
|
||||||
|
obj-mips-y += gt64xxx.o mc146818rtc.o
|
||||||
|
obj-mips-y += cirrus_vga.o
|
||||||
|
obj-mips-$(CONFIG_FULONG) += bonito.o vt82c686.o mips_fulong2e.o
|
||||||
|
|
||||||
|
obj-microblaze-y = petalogix_s3adsp1800_mmu.o
|
||||||
|
obj-microblaze-y += petalogix_ml605_mmu.o
|
||||||
|
|
||||||
|
obj-microblaze-y += microblaze_pic_cpu.o
|
||||||
|
obj-microblaze-y += xilinx_intc.o
|
||||||
|
obj-microblaze-y += xilinx_timer.o
|
||||||
|
obj-microblaze-y += xilinx_uartlite.o
|
||||||
|
obj-microblaze-y += xilinx_ethlite.o
|
||||||
|
obj-microblaze-y += xilinx_axidma.o
|
||||||
|
obj-microblaze-y += xilinx_axienet.o
|
||||||
|
|
||||||
|
obj-microblaze-$(CONFIG_FDT) += device_tree.o
|
||||||
|
|
||||||
|
# Boards
|
||||||
|
obj-cris-y = cris_pic_cpu.o
|
||||||
|
obj-cris-y += cris-boot.o
|
||||||
|
obj-cris-y += etraxfs.o axis_dev88.o
|
||||||
|
obj-cris-y += axis_dev88.o
|
||||||
|
|
||||||
|
# IO blocks
|
||||||
|
obj-cris-y += etraxfs_dma.o
|
||||||
|
obj-cris-y += etraxfs_pic.o
|
||||||
|
obj-cris-y += etraxfs_eth.o
|
||||||
|
obj-cris-y += etraxfs_timer.o
|
||||||
|
obj-cris-y += etraxfs_ser.o
|
||||||
|
|
||||||
|
ifeq ($(TARGET_ARCH), sparc64)
|
||||||
|
obj-sparc-y = sun4u.o apb_pci.o
|
||||||
|
obj-sparc-y += vga.o
|
||||||
|
obj-sparc-y += mc146818rtc.o
|
||||||
|
obj-sparc-y += cirrus_vga.o
|
||||||
else
|
else
|
||||||
obj-y += hw/$(TARGET_BASE_ARCH)/
|
obj-sparc-y = sun4m.o lance.o tcx.o sun4m_iommu.o slavio_intctl.o
|
||||||
|
obj-sparc-y += slavio_timer.o slavio_misc.o sparc32_dma.o
|
||||||
|
obj-sparc-y += cs4231.o eccmemctl.o sbi.o sun4c_intctl.o leon3.o
|
||||||
|
|
||||||
|
# GRLIB
|
||||||
|
obj-sparc-y += grlib_gptimer.o grlib_irqmp.o grlib_apbuart.o
|
||||||
endif
|
endif
|
||||||
|
|
||||||
GENERATED_HEADERS += hmp-commands.h qmp-commands-old.h
|
obj-arm-y = integratorcp.o versatilepb.o arm_pic.o arm_timer.o
|
||||||
|
obj-arm-y += arm_boot.o pl011.o pl031.o pl050.o pl080.o pl110.o pl181.o pl190.o
|
||||||
|
obj-arm-y += versatile_pci.o
|
||||||
|
obj-arm-y += realview_gic.o realview.o arm_sysctl.o arm11mpcore.o a9mpcore.o
|
||||||
|
obj-arm-y += armv7m.o armv7m_nvic.o stellaris.o pl022.o stellaris_enet.o
|
||||||
|
obj-arm-y += pl061.o
|
||||||
|
obj-arm-y += arm-semi.o
|
||||||
|
obj-arm-y += pxa2xx.o pxa2xx_pic.o pxa2xx_gpio.o pxa2xx_timer.o pxa2xx_dma.o
|
||||||
|
obj-arm-y += pxa2xx_lcd.o pxa2xx_mmci.o pxa2xx_pcmcia.o pxa2xx_keypad.o
|
||||||
|
obj-arm-y += gumstix.o
|
||||||
|
obj-arm-y += zaurus.o ide/microdrive.o spitz.o tosa.o tc6393xb.o
|
||||||
|
obj-arm-y += omap1.o omap_lcdc.o omap_dma.o omap_clk.o omap_mmc.o omap_i2c.o \
|
||||||
|
omap_gpio.o omap_intc.o omap_uart.o
|
||||||
|
obj-arm-y += omap2.o omap_dss.o soc_dma.o omap_gptimer.o omap_synctimer.o \
|
||||||
|
omap_gpmc.o omap_sdrc.o omap_spi.o omap_tap.o omap_l4.o
|
||||||
|
obj-arm-y += omap_sx1.o palm.o tsc210x.o
|
||||||
|
obj-arm-y += nseries.o blizzard.o onenand.o vga.o cbus.o tusb6010.o usb-musb.o
|
||||||
|
obj-arm-y += mst_fpga.o mainstone.o
|
||||||
|
obj-arm-y += musicpal.o bitbang_i2c.o marvell_88w8618_audio.o
|
||||||
|
obj-arm-y += framebuffer.o
|
||||||
|
obj-arm-y += syborg.o syborg_fb.o syborg_interrupt.o syborg_keyboard.o
|
||||||
|
obj-arm-y += syborg_serial.o syborg_timer.o syborg_pointer.o syborg_rtc.o
|
||||||
|
obj-arm-y += syborg_virtio.o
|
||||||
|
obj-arm-y += vexpress.o
|
||||||
|
obj-arm-y += strongarm.o
|
||||||
|
obj-arm-y += collie.o
|
||||||
|
|
||||||
|
obj-sh4-y = shix.o r2d.o sh7750.o sh7750_regnames.o tc58128.o
|
||||||
|
obj-sh4-y += sh_timer.o sh_serial.o sh_intc.o sh_pci.o sm501.o
|
||||||
|
obj-sh4-y += ide/mmio.o
|
||||||
|
|
||||||
|
obj-m68k-y = an5206.o mcf5206.o mcf_uart.o mcf_intc.o mcf5208.o mcf_fec.o
|
||||||
|
obj-m68k-y += m68k-semi.o dummy_m68k.o
|
||||||
|
|
||||||
|
obj-s390x-y = s390-virtio-bus.o s390-virtio.o
|
||||||
|
|
||||||
|
obj-alpha-y = i8259.o mc146818rtc.o
|
||||||
|
obj-alpha-y += vga.o cirrus_vga.o
|
||||||
|
|
||||||
|
ifeq ($(TARGET_ARCH), ia64)
|
||||||
|
firmware.o: firmware.c
|
||||||
|
$(CC) $(HELPER_CFLAGS) $(CPPFLAGS) $(BASE_CFLAGS) -c -o $@ $<
|
||||||
|
endif
|
||||||
|
|
||||||
|
main.o: QEMU_CFLAGS+=$(GPROF_CFLAGS)
|
||||||
|
|
||||||
|
monitor.o: hmp-commands.h qmp-commands.h
|
||||||
|
|
||||||
|
$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y): $(GENERATED_HEADERS)
|
||||||
|
|
||||||
|
obj-y += $(addprefix ../, $(common-obj-y))
|
||||||
|
obj-y += $(addprefix ../libdis/, $(libdis-y))
|
||||||
|
obj-y += $(libobj-y)
|
||||||
|
obj-y += $(addprefix $(HWDIR)/, $(hw-obj-y))
|
||||||
|
|
||||||
endif # CONFIG_SOFTMMU
|
endif # CONFIG_SOFTMMU
|
||||||
|
|
||||||
# Workaround for http://gcc.gnu.org/PR55489, see configure.
|
ifndef CONFIG_LINUX_USER
|
||||||
%/translate.o: QEMU_CFLAGS += $(TRANSLATE_OPT_CFLAGS)
|
# libcacard needs qemu-thread support, and besides is only needed by devices
|
||||||
|
# so not requires with linux-user targets
|
||||||
|
obj-$(CONFIG_SMARTCARD_NSS) += $(addprefix ../libcacard/, $(libcacard-y))
|
||||||
|
endif # CONFIG_LINUX_USER
|
||||||
|
|
||||||
dummy := $(call unnest-vars,,obj-y)
|
obj-y += $(addprefix ../, $(trace-obj-y))
|
||||||
all-obj-y := $(obj-y)
|
obj-$(CONFIG_GDBSTUB_XML) += gdbstub-xml.o
|
||||||
|
|
||||||
target-obj-y :=
|
$(QEMU_PROG): $(obj-y) $(obj-$(TARGET_BASE_ARCH)-y)
|
||||||
block-obj-y :=
|
$(call LINK,$(obj-y) $(obj-$(TARGET_BASE_ARCH)-y))
|
||||||
common-obj-y :=
|
|
||||||
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 \
|
|
||||||
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-$(CONFIG_SOFTMMU) += $(block-obj-y)
|
|
||||||
|
|
||||||
# build either PROG or PROGW
|
|
||||||
$(QEMU_PROG_BUILD): $(all-obj-y) ../libqemuutil.a ../libqemustub.a
|
|
||||||
$(call LINK,$^)
|
|
||||||
|
|
||||||
gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
|
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)$@")
|
||||||
@@ -185,26 +431,28 @@ gdbstub-xml.c: $(TARGET_XML_FILES) $(SRC_PATH)/scripts/feature_to_c.sh
|
|||||||
hmp-commands.h: $(SRC_PATH)/hmp-commands.hx
|
hmp-commands.h: $(SRC_PATH)/hmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
||||||
|
|
||||||
qmp-commands-old.h: $(SRC_PATH)/qmp-commands.hx
|
qmp-commands.h: $(SRC_PATH)/qmp-commands.hx
|
||||||
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
$(call quiet-command,sh $(SRC_PATH)/scripts/hxtool -h < $< > $@," GEN $(TARGET_DIR)$@")
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.a *~ $(PROGS)
|
rm -f *.o *.a *~ $(PROGS) nwfpe/*.o fpu/*.o
|
||||||
rm -f $(shell find . -name '*.[od]')
|
rm -f *.d */*.d tcg/*.o ide/*.o 9pfs/*.o
|
||||||
rm -f hmp-commands.h qmp-commands-old.h gdbstub-xml.c
|
rm -f hmp-commands.h qmp-commands.h gdbstub-xml.c
|
||||||
ifdef CONFIG_TRACE_SYSTEMTAP
|
ifdef CONFIG_SYSTEMTAP_TRACE
|
||||||
rm -f *.stp
|
rm -f *.stp
|
||||||
endif
|
endif
|
||||||
|
|
||||||
install: all
|
install: all
|
||||||
ifneq ($(PROGS),)
|
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
|
endif
|
||||||
$(INSTALL_DIR) "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset"
|
ifdef CONFIG_SYSTEMTAP_TRACE
|
||||||
$(INSTALL_DATA) $(QEMU_PROG).stp-installed "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG).stp"
|
$(INSTALL_DIR) "$(DESTDIR)$(datadir)/../systemtap/tapset"
|
||||||
$(INSTALL_DATA) $(QEMU_PROG)-simpletrace.stp "$(DESTDIR)$(qemu_datadir)/../systemtap/tapset/$(QEMU_PROG)-simpletrace.stp"
|
$(INSTALL_DATA) $(QEMU_PROG).stp "$(DESTDIR)$(datadir)/../systemtap/tapset"
|
||||||
endif
|
endif
|
||||||
|
|
||||||
GENERATED_HEADERS += config-target.h
|
# Include automatically generated dependency files
|
||||||
Makefile: $(GENERATED_HEADERS)
|
-include $(wildcard *.d */*.d)
|
||||||
|
|||||||
23
Makefile.user
Normal file
23
Makefile.user
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Makefile for qemu target independent user files.
|
||||||
|
|
||||||
|
include ../config-host.mak
|
||||||
|
include $(SRC_PATH)/rules.mak
|
||||||
|
-include config.mak
|
||||||
|
|
||||||
|
.PHONY: all
|
||||||
|
|
||||||
|
$(call set-vpath, $(SRC_PATH))
|
||||||
|
|
||||||
|
QEMU_CFLAGS+=-I..
|
||||||
|
|
||||||
|
include $(SRC_PATH)/Makefile.objs
|
||||||
|
|
||||||
|
all: $(user-obj-y)
|
||||||
|
# Dummy command so that make thinks it has done something
|
||||||
|
@true
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o *.d *.a *~
|
||||||
|
|
||||||
|
# Include automatically generated dependency files
|
||||||
|
-include $(wildcard *.d */*.d)
|
||||||
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
|
||||||
266
QMP/qmp-events.txt
Normal file
266
QMP/qmp-events.txt
Normal file
@@ -0,0 +1,266 @@
|
|||||||
|
QEMU Monitor Protocol Events
|
||||||
|
============================
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
STOP
|
||||||
|
----
|
||||||
|
|
||||||
|
Emitted when the Virtual Machine is stopped.
|
||||||
|
|
||||||
|
Data: None.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
{ "event": "STOP",
|
||||||
|
"timestamp": { "seconds": 1267041730, "microseconds": 281295 } }
|
||||||
|
|
||||||
|
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 conection 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 } }
|
||||||
|
|
||||||
|
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}
|
||||||
|
}}
|
||||||
|
|
||||||
|
|
||||||
|
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.
|
||||||
@@ -31,10 +31,8 @@
|
|||||||
# (QEMU)
|
# (QEMU)
|
||||||
|
|
||||||
import qmp
|
import qmp
|
||||||
import json
|
|
||||||
import readline
|
import readline
|
||||||
import sys
|
import sys
|
||||||
import pprint
|
|
||||||
|
|
||||||
class QMPCompleter(list):
|
class QMPCompleter(list):
|
||||||
def complete(self, text, state):
|
def complete(self, text, state):
|
||||||
@@ -54,11 +52,10 @@ class QMPShellBadPort(QMPShellError):
|
|||||||
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
|
# TODO: QMPShell's interface is a bit ugly (eg. _fill_completion() and
|
||||||
# _execute_cmd()). Let's design a better one.
|
# _execute_cmd()). Let's design a better one.
|
||||||
class QMPShell(qmp.QEMUMonitorProtocol):
|
class QMPShell(qmp.QEMUMonitorProtocol):
|
||||||
def __init__(self, address, pp=None):
|
def __init__(self, address):
|
||||||
qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
|
qmp.QEMUMonitorProtocol.__init__(self, self.__get_address(address))
|
||||||
self._greeting = None
|
self._greeting = None
|
||||||
self._completer = None
|
self._completer = None
|
||||||
self._pp = pp
|
|
||||||
|
|
||||||
def __get_address(self, arg):
|
def __get_address(self, arg):
|
||||||
"""
|
"""
|
||||||
@@ -100,41 +97,16 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
for arg in cmdargs[1:]:
|
for arg in cmdargs[1:]:
|
||||||
opt = arg.split('=')
|
opt = arg.split('=')
|
||||||
try:
|
try:
|
||||||
if(len(opt) > 2):
|
|
||||||
opt[1] = '='.join(opt[1:])
|
|
||||||
value = int(opt[1])
|
value = int(opt[1])
|
||||||
except ValueError:
|
except ValueError:
|
||||||
if opt[1] == 'true':
|
|
||||||
value = True
|
|
||||||
elif opt[1] == 'false':
|
|
||||||
value = False
|
|
||||||
elif opt[1].startswith('{'):
|
|
||||||
value = json.loads(opt[1])
|
|
||||||
else:
|
|
||||||
value = opt[1]
|
value = opt[1]
|
||||||
optpath = opt[0].split('.')
|
qmpcmd['arguments'][opt[0]] = value
|
||||||
parent = qmpcmd['arguments']
|
|
||||||
curpath = []
|
|
||||||
for p in optpath[:-1]:
|
|
||||||
curpath.append(p)
|
|
||||||
d = parent.get(p, {})
|
|
||||||
if type(d) is not dict:
|
|
||||||
raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath))
|
|
||||||
parent[p] = d
|
|
||||||
parent = d
|
|
||||||
if optpath[-1] in parent:
|
|
||||||
if type(parent[optpath[-1]]) is dict:
|
|
||||||
raise QMPShellError('Cannot use "%s" as both leaf and non-leaf key' % '.'.join(curpath))
|
|
||||||
else:
|
|
||||||
raise QMPShellError('Cannot set "%s" multiple times' % opt[0])
|
|
||||||
parent[optpath[-1]] = value
|
|
||||||
return qmpcmd
|
return qmpcmd
|
||||||
|
|
||||||
def _execute_cmd(self, cmdline):
|
def _execute_cmd(self, cmdline):
|
||||||
try:
|
try:
|
||||||
qmpcmd = self.__build_cmd(cmdline)
|
qmpcmd = self.__build_cmd(cmdline)
|
||||||
except Exception, e:
|
except:
|
||||||
print 'Error while parsing command line: %s' % e
|
|
||||||
print 'command format: <command-name> ',
|
print 'command format: <command-name> ',
|
||||||
print '[arg-name1=arg1] ... [arg-nameN=argN]'
|
print '[arg-name1=arg1] ... [arg-nameN=argN]'
|
||||||
return True
|
return True
|
||||||
@@ -142,10 +114,6 @@ class QMPShell(qmp.QEMUMonitorProtocol):
|
|||||||
if resp is None:
|
if resp is None:
|
||||||
print 'Disconnected'
|
print 'Disconnected'
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if self._pp is not None:
|
|
||||||
self._pp.pprint(resp)
|
|
||||||
else:
|
|
||||||
print resp
|
print resp
|
||||||
return True
|
return True
|
||||||
|
|
||||||
@@ -254,35 +222,21 @@ def die(msg):
|
|||||||
def fail_cmdline(option=None):
|
def fail_cmdline(option=None):
|
||||||
if option:
|
if option:
|
||||||
sys.stderr.write('ERROR: bad command-line option \'%s\'\n' % 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.stderr.write('qemu-shell [ -H ] < UNIX socket path> | < TCP address:port >\n')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
addr = ''
|
addr = ''
|
||||||
qemu = None
|
|
||||||
hmp = False
|
|
||||||
pp = None
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for arg in sys.argv[1:]:
|
if len(sys.argv) == 2:
|
||||||
if arg == "-H":
|
qemu = QMPShell(sys.argv[1])
|
||||||
if qemu is not None:
|
addr = sys.argv[1]
|
||||||
fail_cmdline(arg)
|
elif len(sys.argv) == 3:
|
||||||
hmp = True
|
if sys.argv[1] != '-H':
|
||||||
elif arg == "-p":
|
fail_cmdline(sys.argv[1])
|
||||||
if pp is not None:
|
qemu = HMPShell(sys.argv[2])
|
||||||
fail_cmdline(arg)
|
addr = sys.argv[2]
|
||||||
pp = pprint.PrettyPrinter(indent=4)
|
|
||||||
else:
|
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()
|
fail_cmdline()
|
||||||
except QMPShellBadPort:
|
except QMPShellBadPort:
|
||||||
die('bad port number in command-line')
|
die('bad port number in command-line')
|
||||||
@@ -1,17 +1,21 @@
|
|||||||
QEMU Machine Protocol Specification
|
QEMU Monitor Protocol Specification - Version 0.1
|
||||||
|
|
||||||
1. Introduction
|
1. Introduction
|
||||||
===============
|
===============
|
||||||
|
|
||||||
This document specifies the QEMU Machine Protocol (QMP), a JSON-based protocol
|
This document specifies the QEMU Monitor Protocol (QMP), a JSON-based protocol
|
||||||
which is available for applications to operate QEMU at the machine-level.
|
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
|
2. Protocol Specification
|
||||||
=========================
|
=========================
|
||||||
|
|
||||||
This section details the protocol format. For the purpose of this document
|
This section details the protocol format. For the purpose of this document
|
||||||
"Client" is any application which is using QMP to communicate with QEMU and
|
"Client" is any application which is communicating with QEMU in control mode,
|
||||||
"Server" is QEMU itself.
|
and "Server" is QEMU itself.
|
||||||
|
|
||||||
JSON data structures, when mentioned in this document, are always in the
|
JSON data structures, when mentioned in this document, are always in the
|
||||||
following format:
|
following format:
|
||||||
@@ -43,14 +47,14 @@ that the connection has been successfully established and that the Server is
|
|||||||
ready for capabilities negotiation (for more information refer to section
|
ready for capabilities negotiation (for more information refer to section
|
||||||
'4. Capabilities Negotiation').
|
'4. Capabilities Negotiation').
|
||||||
|
|
||||||
The greeting message format is:
|
The format is:
|
||||||
|
|
||||||
{ "QMP": { "version": json-object, "capabilities": json-array } }
|
{ "QMP": { "version": json-object, "capabilities": json-array } }
|
||||||
|
|
||||||
Where,
|
Where,
|
||||||
|
|
||||||
- The "version" member contains the Server's version information (the format
|
- The "version" member contains the Server's version information (the format
|
||||||
is the same of the query-version command)
|
is the same of the 'query-version' command)
|
||||||
- The "capabilities" member specify the availability of features beyond the
|
- The "capabilities" member specify the availability of features beyond the
|
||||||
baseline specification
|
baseline specification
|
||||||
|
|
||||||
@@ -79,7 +83,10 @@ of a command execution: success or error.
|
|||||||
2.4.1 success
|
2.4.1 success
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
The format of a success response is:
|
The success response is issued when the command execution has finished
|
||||||
|
without errors.
|
||||||
|
|
||||||
|
The format is:
|
||||||
|
|
||||||
{ "return": json-object, "id": json-value }
|
{ "return": json-object, "id": json-value }
|
||||||
|
|
||||||
@@ -89,22 +96,28 @@ The format of a success response is:
|
|||||||
in a per-command basis or an empty json-object if the command does not
|
in a per-command basis or an empty json-object if the command does not
|
||||||
return data
|
return data
|
||||||
- The "id" member contains the transaction identification associated
|
- The "id" member contains the transaction identification associated
|
||||||
with the command execution if issued by the Client
|
with the command execution (if issued by the Client)
|
||||||
|
|
||||||
2.4.2 error
|
2.4.2 error
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||
The format of an error response is:
|
The error response is issued when the command execution could not be
|
||||||
|
completed because of an error condition.
|
||||||
|
|
||||||
{ "error": { "class": json-string, "desc": json-string }, "id": json-value }
|
The format is:
|
||||||
|
|
||||||
|
{ "error": { "class": json-string, "data": json-object, "desc": json-string },
|
||||||
|
"id": json-value }
|
||||||
|
|
||||||
Where,
|
Where,
|
||||||
|
|
||||||
- The "class" member contains the error class name (eg. "GenericError")
|
- The "class" member contains the error class name (eg. "ServiceUnavailable")
|
||||||
|
- The "data" member contains specific error data and is defined in a
|
||||||
|
per-command basis, it will be an empty json-object if the error has no data
|
||||||
- The "desc" member is a human-readable error message. Clients should
|
- The "desc" member is a human-readable error message. Clients should
|
||||||
not attempt to parse this message.
|
not attempt to parse this message.
|
||||||
- The "id" member contains the transaction identification associated with
|
- The "id" member contains the transaction identification associated with
|
||||||
the command execution if issued by the Client
|
the command execution (if issued by the Client)
|
||||||
|
|
||||||
NOTE: Some errors can occur before the Server is able to read the "id" member,
|
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
|
in these cases the "id" member will not be part of the error response, even
|
||||||
@@ -114,9 +127,9 @@ if provided by the client.
|
|||||||
-----------------------
|
-----------------------
|
||||||
|
|
||||||
As a result of state changes, the Server may send messages unilaterally
|
As a result of state changes, the Server may send messages unilaterally
|
||||||
to the Client at any time. They are called "asynchronous events".
|
to the Client at any time. They are called 'asynchronous events'.
|
||||||
|
|
||||||
The format of asynchronous events is:
|
The format is:
|
||||||
|
|
||||||
{ "event": json-string, "data": json-object,
|
{ "event": json-string, "data": json-object,
|
||||||
"timestamp": { "seconds": json-number, "microseconds": json-number } }
|
"timestamp": { "seconds": json-number, "microseconds": json-number } }
|
||||||
@@ -137,37 +150,37 @@ qmp-events.txt file.
|
|||||||
===============
|
===============
|
||||||
|
|
||||||
This section provides some examples of real QMP usage, in all of them
|
This section provides some examples of real QMP usage, in all of them
|
||||||
"C" stands for "Client" and "S" stands for "Server".
|
'C' stands for 'Client' and 'S' stands for 'Server'.
|
||||||
|
|
||||||
3.1 Server greeting
|
3.1 Server greeting
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
S: { "QMP": { "version": { "qemu": { "micro": 50, "minor": 6, "major": 1 },
|
S: {"QMP": {"version": {"qemu": "0.12.50", "package": ""}, "capabilities": []}}
|
||||||
"package": ""}, "capabilities": []}}
|
|
||||||
|
|
||||||
3.2 Simple 'stop' execution
|
3.2 Simple 'stop' execution
|
||||||
---------------------------
|
---------------------------
|
||||||
|
|
||||||
C: { "execute": "stop" }
|
C: { "execute": "stop" }
|
||||||
S: { "return": {} }
|
S: {"return": {}}
|
||||||
|
|
||||||
3.3 KVM information
|
3.3 KVM information
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
C: { "execute": "query-kvm", "id": "example" }
|
C: { "execute": "query-kvm", "id": "example" }
|
||||||
S: { "return": { "enabled": true, "present": true }, "id": "example"}
|
S: {"return": {"enabled": true, "present": true}, "id": "example"}
|
||||||
|
|
||||||
3.4 Parsing error
|
3.4 Parsing error
|
||||||
------------------
|
------------------
|
||||||
|
|
||||||
C: { "execute": }
|
C: { "execute": }
|
||||||
S: { "error": { "class": "GenericError", "desc": "Invalid JSON syntax" } }
|
S: {"error": {"class": "JSONParsing", "desc": "Invalid JSON syntax", "data":
|
||||||
|
{}}}
|
||||||
|
|
||||||
3.5 Powerdown event
|
3.5 Powerdown event
|
||||||
-------------------
|
-------------------
|
||||||
|
|
||||||
S: { "timestamp": { "seconds": 1258551470, "microseconds": 802384 },
|
S: {"timestamp": {"seconds": 1258551470, "microseconds": 802384}, "event":
|
||||||
"event": "POWERDOWN" }
|
"POWERDOWN"}
|
||||||
|
|
||||||
4. Capabilities Negotiation
|
4. Capabilities Negotiation
|
||||||
----------------------------
|
----------------------------
|
||||||
@@ -175,17 +188,17 @@ S: { "timestamp": { "seconds": 1258551470, "microseconds": 802384 },
|
|||||||
When a Client successfully establishes a connection, the Server is in
|
When a Client successfully establishes a connection, the Server is in
|
||||||
Capabilities Negotiation mode.
|
Capabilities Negotiation mode.
|
||||||
|
|
||||||
In this mode only the qmp_capabilities command is allowed to run, all
|
In this mode only the 'qmp_capabilities' command is allowed to run, all
|
||||||
other commands will return the CommandNotFound error. Asynchronous
|
other commands will return the CommandNotFound error. Asynchronous messages
|
||||||
messages are not delivered either.
|
are not delivered either.
|
||||||
|
|
||||||
Clients should use the qmp_capabilities command to enable capabilities
|
Clients should use the 'qmp_capabilities' command to enable capabilities
|
||||||
advertised in the Server's greeting (section '2.2 Server Greeting') they
|
advertised in the Server's greeting (section '2.2 Server Greeting') they
|
||||||
support.
|
support.
|
||||||
|
|
||||||
When the qmp_capabilities command is issued, and if it does not return an
|
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
|
error, the Server enters in Command mode where capabilities changes take
|
||||||
effect, all commands (except qmp_capabilities) are allowed and asynchronous
|
effect, all commands (except 'qmp_capabilities') are allowed and asynchronous
|
||||||
messages are delivered.
|
messages are delivered.
|
||||||
|
|
||||||
5 Compatibility Considerations
|
5 Compatibility Considerations
|
||||||
@@ -196,27 +209,13 @@ incompatible way are disabled by default and will be advertised by the
|
|||||||
capabilities array (section '2.2 Server Greeting'). Thus, Clients can check
|
capabilities array (section '2.2 Server Greeting'). Thus, Clients can check
|
||||||
that array and enable the capabilities they support.
|
that array and enable the capabilities they support.
|
||||||
|
|
||||||
The QMP Server performs a type check on the arguments to a command. It
|
Additionally, Clients must not assume any particular:
|
||||||
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:
|
- Size of json-objects or length of json-arrays
|
||||||
|
|
||||||
- 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
|
- Order of json-object members or json-array elements
|
||||||
- Amount of errors generated by a command, that is, new errors can be added
|
- Amount of errors generated by a command, that is, new errors can be added
|
||||||
to any existing command in newer versions of the Server
|
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
|
6. Downstream extension of QMP
|
||||||
------------------------------
|
------------------------------
|
||||||
|
|
||||||
@@ -236,7 +235,7 @@ arguments, errors, asynchronous events, and so forth.
|
|||||||
|
|
||||||
Any new names downstream wishes to add must begin with '__'. To
|
Any new names downstream wishes to add must begin with '__'. To
|
||||||
ensure compatibility with other downstreams, it is strongly
|
ensure compatibility with other downstreams, it is strongly
|
||||||
recommended that you prefix your downstream names with '__RFQDN_' where
|
recommended that you prefix your downstram names with '__RFQDN_' where
|
||||||
RFQDN is a valid, reverse fully qualified domain name which you
|
RFQDN is a valid, reverse fully qualified domain name which you
|
||||||
control. For example, a qemu-kvm specific monitor command would be:
|
control. For example, a qemu-kvm specific monitor command would be:
|
||||||
|
|
||||||
@@ -49,6 +49,7 @@ class QEMUMonitorProtocol:
|
|||||||
return socket.socket(family, socket.SOCK_STREAM)
|
return socket.socket(family, socket.SOCK_STREAM)
|
||||||
|
|
||||||
def __negotiate_capabilities(self):
|
def __negotiate_capabilities(self):
|
||||||
|
self.__sockfile = self.__sock.makefile()
|
||||||
greeting = self.__json_read()
|
greeting = self.__json_read()
|
||||||
if greeting is None or not greeting.has_key('QMP'):
|
if greeting is None or not greeting.has_key('QMP'):
|
||||||
raise QMPConnectError
|
raise QMPConnectError
|
||||||
@@ -72,7 +73,7 @@ class QEMUMonitorProtocol:
|
|||||||
|
|
||||||
error = socket.error
|
error = socket.error
|
||||||
|
|
||||||
def connect(self, negotiate=True):
|
def connect(self):
|
||||||
"""
|
"""
|
||||||
Connect to the QMP Monitor and perform capabilities negotiation.
|
Connect to the QMP Monitor and perform capabilities negotiation.
|
||||||
|
|
||||||
@@ -82,8 +83,6 @@ class QEMUMonitorProtocol:
|
|||||||
@raise QMPCapabilitiesError if fails to negotiate capabilities
|
@raise QMPCapabilitiesError if fails to negotiate capabilities
|
||||||
"""
|
"""
|
||||||
self.__sock.connect(self.__address)
|
self.__sock.connect(self.__address)
|
||||||
self.__sockfile = self.__sock.makefile()
|
|
||||||
if negotiate:
|
|
||||||
return self.__negotiate_capabilities()
|
return self.__negotiate_capabilities()
|
||||||
|
|
||||||
def accept(self):
|
def accept(self):
|
||||||
@@ -96,7 +95,6 @@ class QEMUMonitorProtocol:
|
|||||||
@raise QMPCapabilitiesError if fails to negotiate capabilities
|
@raise QMPCapabilitiesError if fails to negotiate capabilities
|
||||||
"""
|
"""
|
||||||
self.__sock, _ = self.__sock.accept()
|
self.__sock, _ = self.__sock.accept()
|
||||||
self.__sockfile = self.__sock.makefile()
|
|
||||||
return self.__negotiate_capabilities()
|
return self.__negotiate_capabilities()
|
||||||
|
|
||||||
def cmd_obj(self, qmp_cmd):
|
def cmd_obj(self, qmp_cmd):
|
||||||
@@ -130,32 +128,6 @@ class QEMUMonitorProtocol:
|
|||||||
qmp_cmd['id'] = id
|
qmp_cmd['id'] = id
|
||||||
return self.cmd_obj(qmp_cmd)
|
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):
|
def get_events(self, wait=False):
|
||||||
"""
|
"""
|
||||||
Get a list of available QMP events.
|
Get a list of available QMP events.
|
||||||
@@ -171,12 +143,7 @@ class QEMUMonitorProtocol:
|
|||||||
pass
|
pass
|
||||||
self.__sock.setblocking(1)
|
self.__sock.setblocking(1)
|
||||||
if not self.__events and wait:
|
if not self.__events and wait:
|
||||||
ret = self.__json_read(only_event=True)
|
self.__json_read(only_event=True)
|
||||||
if ret == None:
|
|
||||||
# We are in blocking mode, if don't get anything, something
|
|
||||||
# went wrong
|
|
||||||
raise QMPConnectError("Error while reading from socket")
|
|
||||||
|
|
||||||
return self.__events
|
return self.__events
|
||||||
|
|
||||||
def clear_events(self):
|
def clear_events(self):
|
||||||
@@ -188,14 +155,3 @@ class QEMUMonitorProtocol:
|
|||||||
def close(self):
|
def close(self):
|
||||||
self.__sock.close()
|
self.__sock.close()
|
||||||
self.__sockfile.close()
|
self.__sockfile.close()
|
||||||
|
|
||||||
timeout = socket.timeout
|
|
||||||
|
|
||||||
def settimeout(self, timeout):
|
|
||||||
self.__sock.settimeout(timeout)
|
|
||||||
|
|
||||||
def get_sock_fd(self):
|
|
||||||
return self.__sock.fileno()
|
|
||||||
|
|
||||||
def is_scm_available(self):
|
|
||||||
return self.__sock.family == socket.AF_UNIX
|
|
||||||
4
README
4
README
@@ -1,3 +1,3 @@
|
|||||||
Read the documentation in qemu-doc.html or on http://wiki.qemu-project.org
|
Read the documentation in qemu-doc.html.
|
||||||
|
|
||||||
- QEMU team
|
Fabrice Bellard.
|
||||||
|
|||||||
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
|
||||||
430
a.out.h
Normal file
430
a.out.h
Normal file
@@ -0,0 +1,430 @@
|
|||||||
|
/* a.out.h
|
||||||
|
|
||||||
|
Copyright 1997, 1998, 1999, 2001 Red Hat, Inc.
|
||||||
|
|
||||||
|
This file is part of Cygwin.
|
||||||
|
|
||||||
|
This software is a copyrighted work licensed under the terms of the
|
||||||
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
||||||
|
details. */
|
||||||
|
|
||||||
|
#ifndef _A_OUT_H_
|
||||||
|
#define _A_OUT_H_
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
#define COFF_IMAGE_WITH_PE
|
||||||
|
#define COFF_LONG_SECTION_NAMES
|
||||||
|
|
||||||
|
/*** coff information for Intel 386/486. */
|
||||||
|
|
||||||
|
|
||||||
|
/********************** FILE HEADER **********************/
|
||||||
|
|
||||||
|
struct external_filehdr {
|
||||||
|
short f_magic; /* magic number */
|
||||||
|
short f_nscns; /* number of sections */
|
||||||
|
host_ulong f_timdat; /* time & date stamp */
|
||||||
|
host_ulong f_symptr; /* file pointer to symtab */
|
||||||
|
host_ulong f_nsyms; /* number of symtab entries */
|
||||||
|
short f_opthdr; /* sizeof(optional hdr) */
|
||||||
|
short f_flags; /* flags */
|
||||||
|
};
|
||||||
|
|
||||||
|
/* Bits for f_flags:
|
||||||
|
* F_RELFLG relocation info stripped from file
|
||||||
|
* F_EXEC file is executable (no unresolved external references)
|
||||||
|
* F_LNNO line numbers stripped from file
|
||||||
|
* F_LSYMS local symbols stripped from file
|
||||||
|
* F_AR32WR file has byte ordering of an AR32WR machine (e.g. vax)
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define F_RELFLG (0x0001)
|
||||||
|
#define F_EXEC (0x0002)
|
||||||
|
#define F_LNNO (0x0004)
|
||||||
|
#define F_LSYMS (0x0008)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#define I386MAGIC 0x14c
|
||||||
|
#define I386PTXMAGIC 0x154
|
||||||
|
#define I386AIXMAGIC 0x175
|
||||||
|
|
||||||
|
/* This is Lynx's all-platform magic number for executables. */
|
||||||
|
|
||||||
|
#define LYNXCOFFMAGIC 0415
|
||||||
|
|
||||||
|
#define I386BADMAG(x) (((x).f_magic != I386MAGIC) \
|
||||||
|
&& (x).f_magic != I386AIXMAGIC \
|
||||||
|
&& (x).f_magic != I386PTXMAGIC \
|
||||||
|
&& (x).f_magic != LYNXCOFFMAGIC)
|
||||||
|
|
||||||
|
#define FILHDR struct external_filehdr
|
||||||
|
#define FILHSZ 20
|
||||||
|
|
||||||
|
|
||||||
|
/********************** AOUT "OPTIONAL HEADER"=
|
||||||
|
**********************/
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
unsigned short magic; /* type of file */
|
||||||
|
unsigned short vstamp; /* version stamp */
|
||||||
|
host_ulong tsize; /* text size in bytes, padded to FW bdry*/
|
||||||
|
host_ulong dsize; /* initialized data " " */
|
||||||
|
host_ulong bsize; /* uninitialized data " " */
|
||||||
|
host_ulong entry; /* entry pt. */
|
||||||
|
host_ulong text_start; /* base of text used for this file */
|
||||||
|
host_ulong data_start; /* base of data used for this file=
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
AOUTHDR;
|
||||||
|
|
||||||
|
#define AOUTSZ 28
|
||||||
|
#define AOUTHDRSZ 28
|
||||||
|
|
||||||
|
#define OMAGIC 0404 /* object files, eg as output */
|
||||||
|
#define ZMAGIC 0413 /* demand load format, eg normal ld output */
|
||||||
|
#define STMAGIC 0401 /* target shlib */
|
||||||
|
#define SHMAGIC 0443 /* host shlib */
|
||||||
|
|
||||||
|
|
||||||
|
/* define some NT default values */
|
||||||
|
/* #define NT_IMAGE_BASE 0x400000 moved to internal.h */
|
||||||
|
#define NT_SECTION_ALIGNMENT 0x1000
|
||||||
|
#define NT_FILE_ALIGNMENT 0x200
|
||||||
|
#define NT_DEF_RESERVE 0x100000
|
||||||
|
#define NT_DEF_COMMIT 0x1000
|
||||||
|
|
||||||
|
/********************** SECTION HEADER **********************/
|
||||||
|
|
||||||
|
|
||||||
|
struct external_scnhdr {
|
||||||
|
char s_name[8]; /* section name */
|
||||||
|
host_ulong s_paddr; /* physical address, offset
|
||||||
|
of last addr in scn */
|
||||||
|
host_ulong s_vaddr; /* virtual address */
|
||||||
|
host_ulong s_size; /* section size */
|
||||||
|
host_ulong s_scnptr; /* file ptr to raw data for section */
|
||||||
|
host_ulong s_relptr; /* file ptr to relocation */
|
||||||
|
host_ulong s_lnnoptr; /* file ptr to line numbers */
|
||||||
|
unsigned short s_nreloc; /* number of relocation entries */
|
||||||
|
unsigned short s_nlnno; /* number of line number entries*/
|
||||||
|
host_ulong s_flags; /* flags */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define SCNHDR struct external_scnhdr
|
||||||
|
#define SCNHSZ 40
|
||||||
|
|
||||||
|
/*
|
||||||
|
* names of "special" sections
|
||||||
|
*/
|
||||||
|
#define _TEXT ".text"
|
||||||
|
#define _DATA ".data"
|
||||||
|
#define _BSS ".bss"
|
||||||
|
#define _COMMENT ".comment"
|
||||||
|
#define _LIB ".lib"
|
||||||
|
|
||||||
|
/********************** LINE NUMBERS **********************/
|
||||||
|
|
||||||
|
/* 1 line number entry for every "breakpointable" source line in a section.
|
||||||
|
* Line numbers are grouped on a per function basis; first entry in a function
|
||||||
|
* grouping will have l_lnno = 0 and in place of physical address will be the
|
||||||
|
* symbol table index of the function name.
|
||||||
|
*/
|
||||||
|
struct external_lineno {
|
||||||
|
union {
|
||||||
|
host_ulong l_symndx; /* function name symbol index, iff l_lnno 0 */
|
||||||
|
host_ulong l_paddr; /* (physical) address of line number */
|
||||||
|
} l_addr;
|
||||||
|
unsigned short l_lnno; /* line number */
|
||||||
|
};
|
||||||
|
|
||||||
|
#define LINENO struct external_lineno
|
||||||
|
#define LINESZ 6
|
||||||
|
|
||||||
|
/********************** SYMBOLS **********************/
|
||||||
|
|
||||||
|
#define E_SYMNMLEN 8 /* # characters in a symbol name */
|
||||||
|
#define E_FILNMLEN 14 /* # characters in a file name */
|
||||||
|
#define E_DIMNUM 4 /* # array dimensions in auxiliary entry */
|
||||||
|
|
||||||
|
struct __attribute__((packed)) external_syment
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
char e_name[E_SYMNMLEN];
|
||||||
|
struct {
|
||||||
|
host_ulong e_zeroes;
|
||||||
|
host_ulong e_offset;
|
||||||
|
} e;
|
||||||
|
} e;
|
||||||
|
host_ulong e_value;
|
||||||
|
unsigned short e_scnum;
|
||||||
|
unsigned short e_type;
|
||||||
|
char e_sclass[1];
|
||||||
|
char e_numaux[1];
|
||||||
|
};
|
||||||
|
|
||||||
|
#define N_BTMASK (0xf)
|
||||||
|
#define N_TMASK (0x30)
|
||||||
|
#define N_BTSHFT (4)
|
||||||
|
#define N_TSHIFT (2)
|
||||||
|
|
||||||
|
union external_auxent {
|
||||||
|
struct {
|
||||||
|
host_ulong x_tagndx; /* str, un, or enum tag indx */
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
unsigned short x_lnno; /* declaration line number */
|
||||||
|
unsigned short x_size; /* str/union/array size */
|
||||||
|
} x_lnsz;
|
||||||
|
host_ulong x_fsize; /* size of function */
|
||||||
|
} x_misc;
|
||||||
|
union {
|
||||||
|
struct { /* if ISFCN, tag, or .bb */
|
||||||
|
host_ulong x_lnnoptr;/* ptr to fcn line # */
|
||||||
|
host_ulong x_endndx; /* entry ndx past block end */
|
||||||
|
} x_fcn;
|
||||||
|
struct { /* if ISARY, up to 4 dimen. */
|
||||||
|
char x_dimen[E_DIMNUM][2];
|
||||||
|
} x_ary;
|
||||||
|
} x_fcnary;
|
||||||
|
unsigned short x_tvndx; /* tv index */
|
||||||
|
} x_sym;
|
||||||
|
|
||||||
|
union {
|
||||||
|
char x_fname[E_FILNMLEN];
|
||||||
|
struct {
|
||||||
|
host_ulong x_zeroes;
|
||||||
|
host_ulong x_offset;
|
||||||
|
} x_n;
|
||||||
|
} x_file;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
host_ulong x_scnlen; /* section length */
|
||||||
|
unsigned short x_nreloc; /* # relocation entries */
|
||||||
|
unsigned short x_nlinno; /* # line numbers */
|
||||||
|
host_ulong x_checksum; /* section COMDAT checksum */
|
||||||
|
unsigned short x_associated;/* COMDAT associated section index */
|
||||||
|
char x_comdat[1]; /* COMDAT selection number */
|
||||||
|
} x_scn;
|
||||||
|
|
||||||
|
struct {
|
||||||
|
host_ulong x_tvfill; /* tv fill value */
|
||||||
|
unsigned short x_tvlen; /* length of .tv */
|
||||||
|
char x_tvran[2][2]; /* tv range */
|
||||||
|
} x_tv; /* info about .tv section (in auxent of symbol .tv)) */
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#define SYMENT struct external_syment
|
||||||
|
#define SYMESZ 18
|
||||||
|
#define AUXENT union external_auxent
|
||||||
|
#define AUXESZ 18
|
||||||
|
|
||||||
|
#define _ETEXT "etext"
|
||||||
|
|
||||||
|
/********************** RELOCATION DIRECTIVES **********************/
|
||||||
|
|
||||||
|
struct external_reloc {
|
||||||
|
char r_vaddr[4];
|
||||||
|
char r_symndx[4];
|
||||||
|
char r_type[2];
|
||||||
|
};
|
||||||
|
|
||||||
|
#define RELOC struct external_reloc
|
||||||
|
#define RELSZ 10
|
||||||
|
|
||||||
|
/* end of coff/i386.h */
|
||||||
|
|
||||||
|
/* PE COFF header information */
|
||||||
|
|
||||||
|
#ifndef _PE_H
|
||||||
|
#define _PE_H
|
||||||
|
|
||||||
|
/* NT specific file attributes */
|
||||||
|
#define IMAGE_FILE_RELOCS_STRIPPED 0x0001
|
||||||
|
#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
|
||||||
|
#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004
|
||||||
|
#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008
|
||||||
|
#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
|
||||||
|
#define IMAGE_FILE_32BIT_MACHINE 0x0100
|
||||||
|
#define IMAGE_FILE_DEBUG_STRIPPED 0x0200
|
||||||
|
#define IMAGE_FILE_SYSTEM 0x1000
|
||||||
|
#define IMAGE_FILE_DLL 0x2000
|
||||||
|
#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
|
||||||
|
|
||||||
|
/* additional flags to be set for section headers to allow the NT loader to
|
||||||
|
read and write to the section data (to replace the addresses of data in
|
||||||
|
dlls for one thing); also to execute the section in .text's case=
|
||||||
|
*/
|
||||||
|
#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
|
||||||
|
#define IMAGE_SCN_MEM_EXECUTE 0x20000000
|
||||||
|
#define IMAGE_SCN_MEM_READ 0x40000000
|
||||||
|
#define IMAGE_SCN_MEM_WRITE 0x80000000
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Section characteristics added for ppc-nt
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define IMAGE_SCN_TYPE_NO_PAD 0x00000008 /* Reserved. */
|
||||||
|
|
||||||
|
#define IMAGE_SCN_CNT_CODE 0x00000020 /* Section contains code. */
|
||||||
|
#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040 /* Section contains initialized data. */
|
||||||
|
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080 /* Section contains uninitialized data. */
|
||||||
|
|
||||||
|
#define IMAGE_SCN_LNK_OTHER 0x00000100 /* Reserved. */
|
||||||
|
#define IMAGE_SCN_LNK_INFO 0x00000200 /* Section contains comments or some other type of information. */
|
||||||
|
#define IMAGE_SCN_LNK_REMOVE 0x00000800 /* Section contents will not become part of image. */
|
||||||
|
#define IMAGE_SCN_LNK_COMDAT 0x00001000 /* Section contents comdat. */
|
||||||
|
|
||||||
|
#define IMAGE_SCN_MEM_FARDATA 0x00008000
|
||||||
|
|
||||||
|
#define IMAGE_SCN_MEM_PURGEABLE 0x00020000
|
||||||
|
#define IMAGE_SCN_MEM_16BIT 0x00020000
|
||||||
|
#define IMAGE_SCN_MEM_LOCKED 0x00040000
|
||||||
|
#define IMAGE_SCN_MEM_PRELOAD 0x00080000
|
||||||
|
|
||||||
|
#define IMAGE_SCN_ALIGN_1BYTES 0x00100000
|
||||||
|
#define IMAGE_SCN_ALIGN_2BYTES 0x00200000
|
||||||
|
#define IMAGE_SCN_ALIGN_4BYTES 0x00300000
|
||||||
|
#define IMAGE_SCN_ALIGN_8BYTES 0x00400000
|
||||||
|
#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 /* Default alignment if no others are specified. */
|
||||||
|
#define IMAGE_SCN_ALIGN_32BYTES 0x00600000
|
||||||
|
#define IMAGE_SCN_ALIGN_64BYTES 0x00700000
|
||||||
|
|
||||||
|
|
||||||
|
#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 /* Section contains extended relocations. */
|
||||||
|
#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 /* Section is not cachable. */
|
||||||
|
#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 /* Section is not pageable. */
|
||||||
|
#define IMAGE_SCN_MEM_SHARED 0x10000000 /* Section is shareable. */
|
||||||
|
|
||||||
|
/* COMDAT selection codes. */
|
||||||
|
|
||||||
|
#define IMAGE_COMDAT_SELECT_NODUPLICATES (1) /* Warn if duplicates. */
|
||||||
|
#define IMAGE_COMDAT_SELECT_ANY (2) /* No warning. */
|
||||||
|
#define IMAGE_COMDAT_SELECT_SAME_SIZE (3) /* Warn if different size. */
|
||||||
|
#define IMAGE_COMDAT_SELECT_EXACT_MATCH (4) /* Warn if different. */
|
||||||
|
#define IMAGE_COMDAT_SELECT_ASSOCIATIVE (5) /* Base on other section. */
|
||||||
|
|
||||||
|
/* Magic values that are true for all dos/nt implementations */
|
||||||
|
#define DOSMAGIC 0x5a4d
|
||||||
|
#define NT_SIGNATURE 0x00004550
|
||||||
|
|
||||||
|
/* NT allows long filenames, we want to accommodate this. This may break
|
||||||
|
some of the bfd functions */
|
||||||
|
#undef FILNMLEN
|
||||||
|
#define FILNMLEN 18 /* # characters in a file name */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef COFF_IMAGE_WITH_PE
|
||||||
|
/* The filehdr is only weired in images */
|
||||||
|
|
||||||
|
#undef FILHDR
|
||||||
|
struct external_PE_filehdr
|
||||||
|
{
|
||||||
|
/* DOS header fields */
|
||||||
|
unsigned short e_magic; /* Magic number, 0x5a4d */
|
||||||
|
unsigned short e_cblp; /* Bytes on last page of file, 0x90 */
|
||||||
|
unsigned short e_cp; /* Pages in file, 0x3 */
|
||||||
|
unsigned short e_crlc; /* Relocations, 0x0 */
|
||||||
|
unsigned short e_cparhdr; /* Size of header in paragraphs, 0x4 */
|
||||||
|
unsigned short e_minalloc; /* Minimum extra paragraphs needed, 0x0 */
|
||||||
|
unsigned short e_maxalloc; /* Maximum extra paragraphs needed, 0xFFFF */
|
||||||
|
unsigned short e_ss; /* Initial (relative) SS value, 0x0 */
|
||||||
|
unsigned short e_sp; /* Initial SP value, 0xb8 */
|
||||||
|
unsigned short e_csum; /* Checksum, 0x0 */
|
||||||
|
unsigned short e_ip; /* Initial IP value, 0x0 */
|
||||||
|
unsigned short e_cs; /* Initial (relative) CS value, 0x0 */
|
||||||
|
unsigned short e_lfarlc; /* File address of relocation table, 0x40 */
|
||||||
|
unsigned short e_ovno; /* Overlay number, 0x0 */
|
||||||
|
char e_res[4][2]; /* Reserved words, all 0x0 */
|
||||||
|
unsigned short e_oemid; /* OEM identifier (for e_oeminfo), 0x0 */
|
||||||
|
unsigned short e_oeminfo; /* OEM information; e_oemid specific, 0x0 */
|
||||||
|
char e_res2[10][2]; /* Reserved words, all 0x0 */
|
||||||
|
host_ulong e_lfanew; /* File address of new exe header, 0x80 */
|
||||||
|
char dos_message[16][4]; /* other stuff, always follow DOS header */
|
||||||
|
unsigned int nt_signature; /* required NT signature, 0x4550 */
|
||||||
|
|
||||||
|
/* From standard header */
|
||||||
|
|
||||||
|
unsigned short f_magic; /* magic number */
|
||||||
|
unsigned short f_nscns; /* number of sections */
|
||||||
|
host_ulong f_timdat; /* time & date stamp */
|
||||||
|
host_ulong f_symptr; /* file pointer to symtab */
|
||||||
|
host_ulong f_nsyms; /* number of symtab entries */
|
||||||
|
unsigned short f_opthdr; /* sizeof(optional hdr) */
|
||||||
|
unsigned short f_flags; /* flags */
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#define FILHDR struct external_PE_filehdr
|
||||||
|
#undef FILHSZ
|
||||||
|
#define FILHSZ 152
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
unsigned short magic; /* type of file */
|
||||||
|
unsigned short vstamp; /* version stamp */
|
||||||
|
host_ulong tsize; /* text size in bytes, padded to FW bdry*/
|
||||||
|
host_ulong dsize; /* initialized data " " */
|
||||||
|
host_ulong bsize; /* uninitialized data " " */
|
||||||
|
host_ulong entry; /* entry pt. */
|
||||||
|
host_ulong text_start; /* base of text used for this file */
|
||||||
|
host_ulong data_start; /* base of all data used for this file */
|
||||||
|
|
||||||
|
/* NT extra fields; see internal.h for descriptions */
|
||||||
|
host_ulong ImageBase;
|
||||||
|
host_ulong SectionAlignment;
|
||||||
|
host_ulong FileAlignment;
|
||||||
|
unsigned short MajorOperatingSystemVersion;
|
||||||
|
unsigned short MinorOperatingSystemVersion;
|
||||||
|
unsigned short MajorImageVersion;
|
||||||
|
unsigned short MinorImageVersion;
|
||||||
|
unsigned short MajorSubsystemVersion;
|
||||||
|
unsigned short MinorSubsystemVersion;
|
||||||
|
char Reserved1[4];
|
||||||
|
host_ulong SizeOfImage;
|
||||||
|
host_ulong SizeOfHeaders;
|
||||||
|
host_ulong CheckSum;
|
||||||
|
unsigned short Subsystem;
|
||||||
|
unsigned short DllCharacteristics;
|
||||||
|
host_ulong SizeOfStackReserve;
|
||||||
|
host_ulong SizeOfStackCommit;
|
||||||
|
host_ulong SizeOfHeapReserve;
|
||||||
|
host_ulong SizeOfHeapCommit;
|
||||||
|
host_ulong LoaderFlags;
|
||||||
|
host_ulong NumberOfRvaAndSizes;
|
||||||
|
/* IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; */
|
||||||
|
char DataDirectory[16][2][4]; /* 16 entries, 2 elements/entry, 4 chars */
|
||||||
|
|
||||||
|
} PEAOUTHDR;
|
||||||
|
|
||||||
|
|
||||||
|
#undef AOUTSZ
|
||||||
|
#define AOUTSZ (AOUTHDRSZ + 196)
|
||||||
|
|
||||||
|
#undef E_FILNMLEN
|
||||||
|
#define E_FILNMLEN 18 /* # characters in a file name */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* end of coff/pe.h */
|
||||||
|
|
||||||
|
#define DT_NON (0) /* no derived type */
|
||||||
|
#define DT_PTR (1) /* pointer */
|
||||||
|
#define DT_FCN (2) /* function */
|
||||||
|
#define DT_ARY (3) /* array */
|
||||||
|
|
||||||
|
#define ISPTR(x) (((x) & N_TMASK) == (DT_PTR << N_BTSHFT))
|
||||||
|
#define ISFCN(x) (((x) & N_TMASK) == (DT_FCN << N_BTSHFT))
|
||||||
|
#define ISARY(x) (((x) & N_TMASK) == (DT_ARY << N_BTSHFT))
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* _A_OUT_H_ */
|
||||||
157
accel.c
157
accel.c
@@ -1,157 +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 "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"
|
|
||||||
#include "hw/boards.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 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
int 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return !accel_initialised;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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(&accel_type);
|
|
||||||
type_register_static(&tcg_accel_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_accel_types);
|
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/acl.h"
|
#include "acl.h"
|
||||||
|
|
||||||
#ifdef CONFIG_FNMATCH
|
#ifdef CONFIG_FNMATCH
|
||||||
#include <fnmatch.h>
|
#include <fnmatch.h>
|
||||||
@@ -55,8 +55,8 @@ qemu_acl *qemu_acl_init(const char *aclname)
|
|||||||
if (acl)
|
if (acl)
|
||||||
return acl;
|
return acl;
|
||||||
|
|
||||||
acl = g_malloc(sizeof(*acl));
|
acl = qemu_malloc(sizeof(*acl));
|
||||||
acl->aclname = g_strdup(aclname);
|
acl->aclname = qemu_strdup(aclname);
|
||||||
/* Deny by default, so there is no window of "open
|
/* Deny by default, so there is no window of "open
|
||||||
* access" between QEMU starting, and the user setting
|
* access" between QEMU starting, and the user setting
|
||||||
* up ACLs in the monitor */
|
* up ACLs in the monitor */
|
||||||
@@ -65,7 +65,7 @@ qemu_acl *qemu_acl_init(const char *aclname)
|
|||||||
acl->nentries = 0;
|
acl->nentries = 0;
|
||||||
QTAILQ_INIT(&acl->entries);
|
QTAILQ_INIT(&acl->entries);
|
||||||
|
|
||||||
acls = g_realloc(acls, sizeof(*acls) * (nacls +1));
|
acls = qemu_realloc(acls, sizeof(*acls) * (nacls +1));
|
||||||
acls[nacls] = acl;
|
acls[nacls] = acl;
|
||||||
nacls++;
|
nacls++;
|
||||||
|
|
||||||
@@ -95,16 +95,16 @@ int qemu_acl_party_is_allowed(qemu_acl *acl,
|
|||||||
|
|
||||||
void qemu_acl_reset(qemu_acl *acl)
|
void qemu_acl_reset(qemu_acl *acl)
|
||||||
{
|
{
|
||||||
qemu_acl_entry *entry, *next_entry;
|
qemu_acl_entry *entry;
|
||||||
|
|
||||||
/* Put back to deny by default, so there is no window
|
/* Put back to deny by default, so there is no window
|
||||||
* of "open access" while the user re-initializes the
|
* of "open access" while the user re-initializes the
|
||||||
* access control list */
|
* access control list */
|
||||||
acl->defaultDeny = 1;
|
acl->defaultDeny = 1;
|
||||||
QTAILQ_FOREACH_SAFE(entry, &acl->entries, next, next_entry) {
|
QTAILQ_FOREACH(entry, &acl->entries, next) {
|
||||||
QTAILQ_REMOVE(&acl->entries, entry, next);
|
QTAILQ_REMOVE(&acl->entries, entry, next);
|
||||||
g_free(entry->match);
|
free(entry->match);
|
||||||
g_free(entry);
|
free(entry);
|
||||||
}
|
}
|
||||||
acl->nentries = 0;
|
acl->nentries = 0;
|
||||||
}
|
}
|
||||||
@@ -116,8 +116,8 @@ int qemu_acl_append(qemu_acl *acl,
|
|||||||
{
|
{
|
||||||
qemu_acl_entry *entry;
|
qemu_acl_entry *entry;
|
||||||
|
|
||||||
entry = g_malloc(sizeof(*entry));
|
entry = qemu_malloc(sizeof(*entry));
|
||||||
entry->match = g_strdup(match);
|
entry->match = qemu_strdup(match);
|
||||||
entry->deny = deny;
|
entry->deny = deny;
|
||||||
|
|
||||||
QTAILQ_INSERT_TAIL(&acl->entries, entry, next);
|
QTAILQ_INSERT_TAIL(&acl->entries, entry, next);
|
||||||
@@ -132,23 +132,23 @@ int qemu_acl_insert(qemu_acl *acl,
|
|||||||
const char *match,
|
const char *match,
|
||||||
int index)
|
int index)
|
||||||
{
|
{
|
||||||
|
qemu_acl_entry *entry;
|
||||||
qemu_acl_entry *tmp;
|
qemu_acl_entry *tmp;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
if (index <= 0)
|
if (index <= 0)
|
||||||
return -1;
|
return -1;
|
||||||
if (index > acl->nentries) {
|
if (index >= acl->nentries)
|
||||||
return qemu_acl_append(acl, deny, match);
|
return qemu_acl_append(acl, deny, match);
|
||||||
}
|
|
||||||
|
|
||||||
|
entry = qemu_malloc(sizeof(*entry));
|
||||||
|
entry->match = qemu_strdup(match);
|
||||||
|
entry->deny = deny;
|
||||||
|
|
||||||
QTAILQ_FOREACH(tmp, &acl->entries, next) {
|
QTAILQ_FOREACH(tmp, &acl->entries, next) {
|
||||||
i++;
|
i++;
|
||||||
if (i == index) {
|
if (i == index) {
|
||||||
qemu_acl_entry *entry;
|
|
||||||
entry = g_malloc(sizeof(*entry));
|
|
||||||
entry->match = g_strdup(match);
|
|
||||||
entry->deny = deny;
|
|
||||||
|
|
||||||
QTAILQ_INSERT_BEFORE(tmp, entry, next);
|
QTAILQ_INSERT_BEFORE(tmp, entry, next);
|
||||||
acl->nentries++;
|
acl->nentries++;
|
||||||
break;
|
break;
|
||||||
@@ -168,9 +168,6 @@ int qemu_acl_remove(qemu_acl *acl,
|
|||||||
i++;
|
i++;
|
||||||
if (strcmp(entry->match, match) == 0) {
|
if (strcmp(entry->match, match) == 0) {
|
||||||
QTAILQ_REMOVE(&acl->entries, entry, next);
|
QTAILQ_REMOVE(&acl->entries, entry, next);
|
||||||
acl->nentries--;
|
|
||||||
g_free(entry->match);
|
|
||||||
g_free(entry);
|
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
#ifndef __QEMU_ACL_H__
|
#ifndef __QEMU_ACL_H__
|
||||||
#define __QEMU_ACL_H__
|
#define __QEMU_ACL_H__
|
||||||
|
|
||||||
#include "qemu/queue.h"
|
#include "qemu-queue.h"
|
||||||
|
|
||||||
typedef struct qemu_acl_entry qemu_acl_entry;
|
typedef struct qemu_acl_entry qemu_acl_entry;
|
||||||
typedef struct qemu_acl qemu_acl;
|
typedef struct qemu_acl qemu_acl;
|
||||||
26
aes.h
Normal file
26
aes.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef QEMU_AES_H
|
||||||
|
#define QEMU_AES_H
|
||||||
|
|
||||||
|
#define AES_MAXNR 14
|
||||||
|
#define AES_BLOCK_SIZE 16
|
||||||
|
|
||||||
|
struct aes_key_st {
|
||||||
|
uint32_t rd_key[4 *(AES_MAXNR + 1)];
|
||||||
|
int rounds;
|
||||||
|
};
|
||||||
|
typedef struct aes_key_st AES_KEY;
|
||||||
|
|
||||||
|
int AES_set_encrypt_key(const unsigned char *userKey, const int bits,
|
||||||
|
AES_KEY *key);
|
||||||
|
int AES_set_decrypt_key(const unsigned char *userKey, const int bits,
|
||||||
|
AES_KEY *key);
|
||||||
|
|
||||||
|
void AES_encrypt(const unsigned char *in, unsigned char *out,
|
||||||
|
const AES_KEY *key);
|
||||||
|
void AES_decrypt(const unsigned char *in, unsigned char *out,
|
||||||
|
const AES_KEY *key);
|
||||||
|
void AES_cbc_encrypt(const unsigned char *in, unsigned char *out,
|
||||||
|
const unsigned long length, const AES_KEY *key,
|
||||||
|
unsigned char *ivec, const int enc);
|
||||||
|
|
||||||
|
#endif
|
||||||
254
aio-posix.c
254
aio-posix.c
@@ -1,254 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
int deleted;
|
|
||||||
int pollfds_idx;
|
|
||||||
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,
|
|
||||||
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_new0(AioHandler, 1);
|
|
||||||
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->opaque = opaque;
|
|
||||||
node->pollfds_idx = -1;
|
|
||||||
|
|
||||||
node->pfd.events = (io_read ? G_IO_IN | G_IO_HUP | G_IO_ERR : 0);
|
|
||||||
node->pfd.events |= (io_write ? G_IO_OUT | G_IO_ERR : 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_notify(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_set_event_notifier(AioContext *ctx,
|
|
||||||
EventNotifier *notifier,
|
|
||||||
EventNotifierHandler *io_read)
|
|
||||||
{
|
|
||||||
aio_set_fd_handler(ctx, event_notifier_get_fd(notifier),
|
|
||||||
(IOHandler *)io_read, NULL, notifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_prepare(AioContext *ctx)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_pending(AioContext *ctx)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
int revents;
|
|
||||||
|
|
||||||
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_dispatch(AioContext *ctx)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
bool progress = false;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* If there are callbacks left that have been queued, we need to call them.
|
|
||||||
* 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 aio_poll loops).
|
|
||||||
*/
|
|
||||||
if (aio_bh_poll(ctx)) {
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We have to walk very carefully in case 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;
|
|
||||||
|
|
||||||
if (!node->deleted &&
|
|
||||||
(revents & (G_IO_IN | G_IO_HUP | G_IO_ERR)) &&
|
|
||||||
node->io_read) {
|
|
||||||
node->io_read(node->opaque);
|
|
||||||
|
|
||||||
/* aio_notify() does not count as progress */
|
|
||||||
if (node->opaque != &ctx->notifier) {
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!node->deleted &&
|
|
||||||
(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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Run our timers */
|
|
||||||
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
|
||||||
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_poll(AioContext *ctx, bool blocking)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
bool was_dispatching;
|
|
||||||
int ret;
|
|
||||||
bool progress;
|
|
||||||
|
|
||||||
was_dispatching = ctx->dispatching;
|
|
||||||
progress = false;
|
|
||||||
|
|
||||||
/* aio_notify can avoid the expensive event_notifier_set if
|
|
||||||
* everything (file descriptors, bottom halves, timers) will
|
|
||||||
* be re-evaluated before the next blocking poll(). This is
|
|
||||||
* already true when aio_poll is called with blocking == false;
|
|
||||||
* if blocking == true, it is only true after poll() returns.
|
|
||||||
*
|
|
||||||
* If we're in a nested event loop, ctx->dispatching might be true.
|
|
||||||
* In that case we can restore it just before returning, but we
|
|
||||||
* have to clear it now.
|
|
||||||
*/
|
|
||||||
aio_set_dispatching(ctx, !blocking);
|
|
||||||
|
|
||||||
ctx->walking_handlers++;
|
|
||||||
|
|
||||||
g_array_set_size(ctx->pollfds, 0);
|
|
||||||
|
|
||||||
/* fill pollfds */
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
node->pollfds_idx = -1;
|
|
||||||
if (!node->deleted && node->pfd.events) {
|
|
||||||
GPollFD pfd = {
|
|
||||||
.fd = node->pfd.fd,
|
|
||||||
.events = node->pfd.events,
|
|
||||||
};
|
|
||||||
node->pollfds_idx = ctx->pollfds->len;
|
|
||||||
g_array_append_val(ctx->pollfds, pfd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->walking_handlers--;
|
|
||||||
|
|
||||||
/* wait until next event */
|
|
||||||
ret = qemu_poll_ns((GPollFD *)ctx->pollfds->data,
|
|
||||||
ctx->pollfds->len,
|
|
||||||
blocking ? aio_compute_timeout(ctx) : 0);
|
|
||||||
|
|
||||||
/* if we have any readable fds, dispatch event */
|
|
||||||
if (ret > 0) {
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (node->pollfds_idx != -1) {
|
|
||||||
GPollFD *pfd = &g_array_index(ctx->pollfds, GPollFD,
|
|
||||||
node->pollfds_idx);
|
|
||||||
node->pfd.revents = pfd->revents;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Run dispatch even if there were no readable fds to run timers */
|
|
||||||
aio_set_dispatching(ctx, true);
|
|
||||||
if (aio_dispatch(ctx)) {
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_set_dispatching(ctx, was_dispatching);
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
353
aio-win32.c
353
aio-win32.c
@@ -1,353 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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;
|
|
||||||
IOHandler *io_read;
|
|
||||||
IOHandler *io_write;
|
|
||||||
EventNotifierHandler *io_notify;
|
|
||||||
GPollFD pfd;
|
|
||||||
int deleted;
|
|
||||||
void *opaque;
|
|
||||||
QLIST_ENTRY(AioHandler) node;
|
|
||||||
};
|
|
||||||
|
|
||||||
void aio_set_fd_handler(AioContext *ctx,
|
|
||||||
int fd,
|
|
||||||
IOHandler *io_read,
|
|
||||||
IOHandler *io_write,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
/* fd is a SOCKET in our case */
|
|
||||||
AioHandler *node;
|
|
||||||
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (node->pfd.fd == fd && !node->deleted) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Are we deleting the fd handler? */
|
|
||||||
if (!io_read && !io_write) {
|
|
||||||
if (node) {
|
|
||||||
/* 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 {
|
|
||||||
HANDLE event;
|
|
||||||
|
|
||||||
if (node == NULL) {
|
|
||||||
/* Alloc and insert if it's not already there */
|
|
||||||
node = g_new0(AioHandler, 1);
|
|
||||||
node->pfd.fd = fd;
|
|
||||||
QLIST_INSERT_HEAD(&ctx->aio_handlers, node, node);
|
|
||||||
}
|
|
||||||
|
|
||||||
node->pfd.events = 0;
|
|
||||||
if (node->io_read) {
|
|
||||||
node->pfd.events |= G_IO_IN;
|
|
||||||
}
|
|
||||||
if (node->io_write) {
|
|
||||||
node->pfd.events |= G_IO_OUT;
|
|
||||||
}
|
|
||||||
|
|
||||||
node->e = &ctx->notifier;
|
|
||||||
|
|
||||||
/* Update handler with latest information */
|
|
||||||
node->opaque = opaque;
|
|
||||||
node->io_read = io_read;
|
|
||||||
node->io_write = io_write;
|
|
||||||
|
|
||||||
event = event_notifier_get_handle(&ctx->notifier);
|
|
||||||
WSAEventSelect(node->pfd.fd, event,
|
|
||||||
FD_READ | FD_ACCEPT | FD_CLOSE |
|
|
||||||
FD_CONNECT | FD_WRITE | FD_OOB);
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_notify(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_set_event_notifier(AioContext *ctx,
|
|
||||||
EventNotifier *e,
|
|
||||||
EventNotifierHandler *io_notify)
|
|
||||||
{
|
|
||||||
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_new0(AioHandler, 1);
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
aio_notify(ctx);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_prepare(AioContext *ctx)
|
|
||||||
{
|
|
||||||
static struct timeval tv0;
|
|
||||||
AioHandler *node;
|
|
||||||
bool have_select_revents = false;
|
|
||||||
fd_set rfds, wfds;
|
|
||||||
|
|
||||||
/* fill fd sets */
|
|
||||||
FD_ZERO(&rfds);
|
|
||||||
FD_ZERO(&wfds);
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (node->io_read) {
|
|
||||||
FD_SET ((SOCKET)node->pfd.fd, &rfds);
|
|
||||||
}
|
|
||||||
if (node->io_write) {
|
|
||||||
FD_SET ((SOCKET)node->pfd.fd, &wfds);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (select(0, &rfds, &wfds, NULL, &tv0) > 0) {
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
node->pfd.revents = 0;
|
|
||||||
if (FD_ISSET(node->pfd.fd, &rfds)) {
|
|
||||||
node->pfd.revents |= G_IO_IN;
|
|
||||||
have_select_revents = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (FD_ISSET(node->pfd.fd, &wfds)) {
|
|
||||||
node->pfd.revents |= G_IO_OUT;
|
|
||||||
have_select_revents = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return have_select_revents;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_pending(AioContext *ctx)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (node->pfd.revents && node->io_notify) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((node->pfd.revents & G_IO_IN) && node->io_read) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if ((node->pfd.revents & G_IO_OUT) && node->io_write) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool aio_dispatch_handlers(AioContext *ctx, HANDLE event)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
bool progress = false;
|
|
||||||
|
|
||||||
/*
|
|
||||||
* We have to walk very carefully in case aio_set_fd_handler is
|
|
||||||
* called while we're walking.
|
|
||||||
*/
|
|
||||||
node = QLIST_FIRST(&ctx->aio_handlers);
|
|
||||||
while (node) {
|
|
||||||
AioHandler *tmp;
|
|
||||||
int revents = node->pfd.revents;
|
|
||||||
|
|
||||||
ctx->walking_handlers++;
|
|
||||||
|
|
||||||
if (!node->deleted &&
|
|
||||||
(revents || event_notifier_get_handle(node->e) == event) &&
|
|
||||||
node->io_notify) {
|
|
||||||
node->pfd.revents = 0;
|
|
||||||
node->io_notify(node->e);
|
|
||||||
|
|
||||||
/* aio_notify() does not count as progress */
|
|
||||||
if (node->e != &ctx->notifier) {
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!node->deleted &&
|
|
||||||
(node->io_read || node->io_write)) {
|
|
||||||
node->pfd.revents = 0;
|
|
||||||
if ((revents & G_IO_IN) && node->io_read) {
|
|
||||||
node->io_read(node->opaque);
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
if ((revents & G_IO_OUT) && node->io_write) {
|
|
||||||
node->io_write(node->opaque);
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* if the next select() will return an event, we have progressed */
|
|
||||||
if (event == event_notifier_get_handle(&ctx->notifier)) {
|
|
||||||
WSANETWORKEVENTS ev;
|
|
||||||
WSAEnumNetworkEvents(node->pfd.fd, event, &ev);
|
|
||||||
if (ev.lNetworkEvents) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_dispatch(AioContext *ctx)
|
|
||||||
{
|
|
||||||
bool progress;
|
|
||||||
|
|
||||||
progress = aio_bh_poll(ctx);
|
|
||||||
progress |= aio_dispatch_handlers(ctx, INVALID_HANDLE_VALUE);
|
|
||||||
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool aio_poll(AioContext *ctx, bool blocking)
|
|
||||||
{
|
|
||||||
AioHandler *node;
|
|
||||||
HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
|
|
||||||
bool was_dispatching, progress, have_select_revents, first;
|
|
||||||
int count;
|
|
||||||
int timeout;
|
|
||||||
|
|
||||||
have_select_revents = aio_prepare(ctx);
|
|
||||||
if (have_select_revents) {
|
|
||||||
blocking = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
was_dispatching = ctx->dispatching;
|
|
||||||
progress = false;
|
|
||||||
|
|
||||||
/* aio_notify can avoid the expensive event_notifier_set if
|
|
||||||
* everything (file descriptors, bottom halves, timers) will
|
|
||||||
* be re-evaluated before the next blocking poll(). This is
|
|
||||||
* already true when aio_poll is called with blocking == false;
|
|
||||||
* if blocking == true, it is only true after poll() returns.
|
|
||||||
*
|
|
||||||
* If we're in a nested event loop, ctx->dispatching might be true.
|
|
||||||
* In that case we can restore it just before returning, but we
|
|
||||||
* have to clear it now.
|
|
||||||
*/
|
|
||||||
aio_set_dispatching(ctx, !blocking);
|
|
||||||
|
|
||||||
ctx->walking_handlers++;
|
|
||||||
|
|
||||||
/* fill fd sets */
|
|
||||||
count = 0;
|
|
||||||
QLIST_FOREACH(node, &ctx->aio_handlers, node) {
|
|
||||||
if (!node->deleted && node->io_notify) {
|
|
||||||
events[count++] = event_notifier_get_handle(node->e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ctx->walking_handlers--;
|
|
||||||
first = true;
|
|
||||||
|
|
||||||
/* wait until next event */
|
|
||||||
while (count > 0) {
|
|
||||||
HANDLE event;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
timeout = blocking
|
|
||||||
? qemu_timeout_ns_to_ms(aio_compute_timeout(ctx)) : 0;
|
|
||||||
ret = WaitForMultipleObjects(count, events, FALSE, timeout);
|
|
||||||
aio_set_dispatching(ctx, true);
|
|
||||||
|
|
||||||
if (first && aio_bh_poll(ctx)) {
|
|
||||||
progress = true;
|
|
||||||
}
|
|
||||||
first = false;
|
|
||||||
|
|
||||||
/* if we have any signaled events, dispatch event */
|
|
||||||
event = NULL;
|
|
||||||
if ((DWORD) (ret - WAIT_OBJECT_0) < count) {
|
|
||||||
event = events[ret - WAIT_OBJECT_0];
|
|
||||||
events[ret - WAIT_OBJECT_0] = events[--count];
|
|
||||||
} else if (!have_select_revents) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
have_select_revents = false;
|
|
||||||
blocking = false;
|
|
||||||
|
|
||||||
progress |= aio_dispatch_handlers(ctx, event);
|
|
||||||
}
|
|
||||||
|
|
||||||
progress |= timerlistgroup_run_timers(&ctx->tlg);
|
|
||||||
|
|
||||||
aio_set_dispatching(ctx, was_dispatching);
|
|
||||||
return progress;
|
|
||||||
}
|
|
||||||
230
aio.c
Normal file
230
aio.c
Normal file
@@ -0,0 +1,230 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "qemu-common.h"
|
||||||
|
#include "block.h"
|
||||||
|
#include "qemu-queue.h"
|
||||||
|
#include "qemu_socket.h"
|
||||||
|
|
||||||
|
typedef struct AioHandler AioHandler;
|
||||||
|
|
||||||
|
/* The list of registered AIO handlers */
|
||||||
|
static QLIST_HEAD(, AioHandler) aio_handlers;
|
||||||
|
|
||||||
|
/* This is a simple lock used to protect the aio_handlers list. Specifically,
|
||||||
|
* it's used to ensure that no callbacks are removed while we're walking and
|
||||||
|
* dispatching callbacks.
|
||||||
|
*/
|
||||||
|
static int walking_handlers;
|
||||||
|
|
||||||
|
struct AioHandler
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
IOHandler *io_read;
|
||||||
|
IOHandler *io_write;
|
||||||
|
AioFlushHandler *io_flush;
|
||||||
|
AioProcessQueue *io_process_queue;
|
||||||
|
int deleted;
|
||||||
|
void *opaque;
|
||||||
|
QLIST_ENTRY(AioHandler) node;
|
||||||
|
};
|
||||||
|
|
||||||
|
static AioHandler *find_aio_handler(int fd)
|
||||||
|
{
|
||||||
|
AioHandler *node;
|
||||||
|
|
||||||
|
QLIST_FOREACH(node, &aio_handlers, node) {
|
||||||
|
if (node->fd == fd)
|
||||||
|
if (!node->deleted)
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
int qemu_aio_set_fd_handler(int fd,
|
||||||
|
IOHandler *io_read,
|
||||||
|
IOHandler *io_write,
|
||||||
|
AioFlushHandler *io_flush,
|
||||||
|
AioProcessQueue *io_process_queue,
|
||||||
|
void *opaque)
|
||||||
|
{
|
||||||
|
AioHandler *node;
|
||||||
|
|
||||||
|
node = find_aio_handler(fd);
|
||||||
|
|
||||||
|
/* Are we deleting the fd handler? */
|
||||||
|
if (!io_read && !io_write) {
|
||||||
|
if (node) {
|
||||||
|
/* If the lock is held, just mark the node as deleted */
|
||||||
|
if (walking_handlers)
|
||||||
|
node->deleted = 1;
|
||||||
|
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);
|
||||||
|
qemu_free(node);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (node == NULL) {
|
||||||
|
/* Alloc and insert if it's not already there */
|
||||||
|
node = qemu_mallocz(sizeof(AioHandler));
|
||||||
|
node->fd = fd;
|
||||||
|
QLIST_INSERT_HEAD(&aio_handlers, node, node);
|
||||||
|
}
|
||||||
|
/* Update handler with latest information */
|
||||||
|
node->io_read = io_read;
|
||||||
|
node->io_write = io_write;
|
||||||
|
node->io_flush = io_flush;
|
||||||
|
node->io_process_queue = io_process_queue;
|
||||||
|
node->opaque = opaque;
|
||||||
|
}
|
||||||
|
|
||||||
|
qemu_set_fd_handler2(fd, NULL, io_read, io_write, opaque);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void qemu_aio_flush(void)
|
||||||
|
{
|
||||||
|
AioHandler *node;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
do {
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are pending emulated aio start them now so flush
|
||||||
|
* will be able to return 1.
|
||||||
|
*/
|
||||||
|
qemu_aio_wait();
|
||||||
|
|
||||||
|
QLIST_FOREACH(node, &aio_handlers, node) {
|
||||||
|
if (node->io_flush) {
|
||||||
|
ret |= node->io_flush(node->opaque);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (qemu_bh_poll() || ret > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int qemu_aio_process_queue(void)
|
||||||
|
{
|
||||||
|
AioHandler *node;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
walking_handlers = 1;
|
||||||
|
|
||||||
|
QLIST_FOREACH(node, &aio_handlers, node) {
|
||||||
|
if (node->io_process_queue) {
|
||||||
|
if (node->io_process_queue(node->opaque)) {
|
||||||
|
ret = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
walking_handlers = 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
void qemu_aio_wait(void)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (qemu_bh_poll())
|
||||||
|
return;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If there are callbacks left that have been queued, we need to call then.
|
||||||
|
* Return afterwards to avoid waiting needlessly in select().
|
||||||
|
*/
|
||||||
|
if (qemu_aio_process_queue())
|
||||||
|
return;
|
||||||
|
|
||||||
|
do {
|
||||||
|
AioHandler *node;
|
||||||
|
fd_set rdfds, wrfds;
|
||||||
|
int max_fd = -1;
|
||||||
|
|
||||||
|
walking_handlers = 1;
|
||||||
|
|
||||||
|
FD_ZERO(&rdfds);
|
||||||
|
FD_ZERO(&wrfds);
|
||||||
|
|
||||||
|
/* fill fd sets */
|
||||||
|
QLIST_FOREACH(node, &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->io_flush && node->io_flush(node->opaque) == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!node->deleted && node->io_read) {
|
||||||
|
FD_SET(node->fd, &rdfds);
|
||||||
|
max_fd = MAX(max_fd, node->fd + 1);
|
||||||
|
}
|
||||||
|
if (!node->deleted && node->io_write) {
|
||||||
|
FD_SET(node->fd, &wrfds);
|
||||||
|
max_fd = MAX(max_fd, node->fd + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
walking_handlers = 0;
|
||||||
|
|
||||||
|
/* No AIO operations? Get us out of here */
|
||||||
|
if (max_fd == -1)
|
||||||
|
break;
|
||||||
|
|
||||||
|
/* wait until next event */
|
||||||
|
ret = select(max_fd, &rdfds, &wrfds, NULL, NULL);
|
||||||
|
if (ret == -1 && errno == EINTR)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
/* if we have any readable fds, dispatch event */
|
||||||
|
if (ret > 0) {
|
||||||
|
walking_handlers = 1;
|
||||||
|
|
||||||
|
/* we have to walk very carefully in case
|
||||||
|
* qemu_aio_set_fd_handler is called while we're walking */
|
||||||
|
node = QLIST_FIRST(&aio_handlers);
|
||||||
|
while (node) {
|
||||||
|
AioHandler *tmp;
|
||||||
|
|
||||||
|
if (!node->deleted &&
|
||||||
|
FD_ISSET(node->fd, &rdfds) &&
|
||||||
|
node->io_read) {
|
||||||
|
node->io_read(node->opaque);
|
||||||
|
}
|
||||||
|
if (!node->deleted &&
|
||||||
|
FD_ISSET(node->fd, &wrfds) &&
|
||||||
|
node->io_write) {
|
||||||
|
node->io_write(node->opaque);
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = node;
|
||||||
|
node = QLIST_NEXT(node, node);
|
||||||
|
|
||||||
|
if (tmp->deleted) {
|
||||||
|
QLIST_REMOVE(tmp, node);
|
||||||
|
qemu_free(tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
walking_handlers = 0;
|
||||||
|
}
|
||||||
|
} while (ret == 0);
|
||||||
|
}
|
||||||
1916
alpha-dis.c
Normal file
1916
alpha-dis.c
Normal file
File diff suppressed because it is too large
Load Diff
127
alpha.ld
Normal file
127
alpha.ld
Normal file
@@ -0,0 +1,127 @@
|
|||||||
|
OUTPUT_FORMAT("elf64-alpha", "elf64-alpha",
|
||||||
|
"elf64-alpha")
|
||||||
|
OUTPUT_ARCH(alpha)
|
||||||
|
ENTRY(__start)
|
||||||
|
SECTIONS
|
||||||
|
{
|
||||||
|
/* Read-only sections, merged into text segment: */
|
||||||
|
. = 0x60000000 + SIZEOF_HEADERS;
|
||||||
|
.interp : { *(.interp) }
|
||||||
|
.hash : { *(.hash) }
|
||||||
|
.dynsym : { *(.dynsym) }
|
||||||
|
.dynstr : { *(.dynstr) }
|
||||||
|
.gnu.version : { *(.gnu.version) }
|
||||||
|
.gnu.version_d : { *(.gnu.version_d) }
|
||||||
|
.gnu.version_r : { *(.gnu.version_r) }
|
||||||
|
.rel.text :
|
||||||
|
{ *(.rel.text) *(.rel.gnu.linkonce.t*) }
|
||||||
|
.rela.text :
|
||||||
|
{ *(.rela.text) *(.rela.gnu.linkonce.t*) }
|
||||||
|
.rel.data :
|
||||||
|
{ *(.rel.data) *(.rel.gnu.linkonce.d*) }
|
||||||
|
.rela.data :
|
||||||
|
{ *(.rela.data) *(.rela.gnu.linkonce.d*) }
|
||||||
|
.rel.rodata :
|
||||||
|
{ *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
|
||||||
|
.rela.rodata :
|
||||||
|
{ *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
|
||||||
|
.rel.got : { *(.rel.got) }
|
||||||
|
.rela.got : { *(.rela.got) }
|
||||||
|
.rel.ctors : { *(.rel.ctors) }
|
||||||
|
.rela.ctors : { *(.rela.ctors) }
|
||||||
|
.rel.dtors : { *(.rel.dtors) }
|
||||||
|
.rela.dtors : { *(.rela.dtors) }
|
||||||
|
.rel.init : { *(.rel.init) }
|
||||||
|
.rela.init : { *(.rela.init) }
|
||||||
|
.rel.fini : { *(.rel.fini) }
|
||||||
|
.rela.fini : { *(.rela.fini) }
|
||||||
|
.rel.bss : { *(.rel.bss) }
|
||||||
|
.rela.bss : { *(.rela.bss) }
|
||||||
|
.rel.plt : { *(.rel.plt) }
|
||||||
|
.rela.plt : { *(.rela.plt) }
|
||||||
|
.init : { *(.init) } =0x47ff041f
|
||||||
|
.text :
|
||||||
|
{
|
||||||
|
*(.text)
|
||||||
|
/* .gnu.warning sections are handled specially by elf32.em. */
|
||||||
|
*(.gnu.warning)
|
||||||
|
*(.gnu.linkonce.t*)
|
||||||
|
} =0x47ff041f
|
||||||
|
_etext = .;
|
||||||
|
PROVIDE (etext = .);
|
||||||
|
.fini : { *(.fini) } =0x47ff041f
|
||||||
|
.rodata : { *(.rodata) *(.gnu.linkonce.r*) }
|
||||||
|
.rodata1 : { *(.rodata1) }
|
||||||
|
.reginfo : { *(.reginfo) }
|
||||||
|
/* Adjust the address for the data segment. We want to adjust up to
|
||||||
|
the same address within the page on the next page up. */
|
||||||
|
. = ALIGN(0x100000) + (. & (0x100000 - 1));
|
||||||
|
.data :
|
||||||
|
{
|
||||||
|
*(.data)
|
||||||
|
*(.gnu.linkonce.d*)
|
||||||
|
CONSTRUCTORS
|
||||||
|
}
|
||||||
|
.data1 : { *(.data1) }
|
||||||
|
.ctors :
|
||||||
|
{
|
||||||
|
*(.ctors)
|
||||||
|
}
|
||||||
|
.dtors :
|
||||||
|
{
|
||||||
|
*(.dtors)
|
||||||
|
}
|
||||||
|
.plt : { *(.plt) }
|
||||||
|
.got : { *(.got.plt) *(.got) }
|
||||||
|
.dynamic : { *(.dynamic) }
|
||||||
|
/* We want the small data sections together, so single-instruction offsets
|
||||||
|
can access them all, and initialized data all before uninitialized, so
|
||||||
|
we can shorten the on-disk segment size. */
|
||||||
|
.sdata : { *(.sdata) }
|
||||||
|
_edata = .;
|
||||||
|
PROVIDE (edata = .);
|
||||||
|
__bss_start = .;
|
||||||
|
.sbss : { *(.sbss) *(.scommon) }
|
||||||
|
.bss :
|
||||||
|
{
|
||||||
|
*(.dynbss)
|
||||||
|
*(.bss)
|
||||||
|
*(COMMON)
|
||||||
|
}
|
||||||
|
_end = . ;
|
||||||
|
PROVIDE (end = .);
|
||||||
|
/* Stabs debugging sections. */
|
||||||
|
.stab 0 : { *(.stab) }
|
||||||
|
.stabstr 0 : { *(.stabstr) }
|
||||||
|
.stab.excl 0 : { *(.stab.excl) }
|
||||||
|
.stab.exclstr 0 : { *(.stab.exclstr) }
|
||||||
|
.stab.index 0 : { *(.stab.index) }
|
||||||
|
.stab.indexstr 0 : { *(.stab.indexstr) }
|
||||||
|
.comment 0 : { *(.comment) }
|
||||||
|
/* DWARF debug sections.
|
||||||
|
Symbols in the DWARF debugging sections are relative to the beginning
|
||||||
|
of the section so we begin them at 0. */
|
||||||
|
/* DWARF 1 */
|
||||||
|
.debug 0 : { *(.debug) }
|
||||||
|
.line 0 : { *(.line) }
|
||||||
|
/* GNU DWARF 1 extensions */
|
||||||
|
.debug_srcinfo 0 : { *(.debug_srcinfo) }
|
||||||
|
.debug_sfnames 0 : { *(.debug_sfnames) }
|
||||||
|
/* DWARF 1.1 and DWARF 2 */
|
||||||
|
.debug_aranges 0 : { *(.debug_aranges) }
|
||||||
|
.debug_pubnames 0 : { *(.debug_pubnames) }
|
||||||
|
/* DWARF 2 */
|
||||||
|
.debug_info 0 : { *(.debug_info) }
|
||||||
|
.debug_abbrev 0 : { *(.debug_abbrev) }
|
||||||
|
.debug_line 0 : { *(.debug_line) }
|
||||||
|
.debug_frame 0 : { *(.debug_frame) }
|
||||||
|
.debug_str 0 : { *(.debug_str) }
|
||||||
|
.debug_loc 0 : { *(.debug_loc) }
|
||||||
|
.debug_macinfo 0 : { *(.debug_macinfo) }
|
||||||
|
/* SGI/MIPS DWARF 2 extensions */
|
||||||
|
.debug_weaknames 0 : { *(.debug_weaknames) }
|
||||||
|
.debug_funcnames 0 : { *(.debug_funcnames) }
|
||||||
|
.debug_typenames 0 : { *(.debug_typenames) }
|
||||||
|
.debug_varnames 0 : { *(.debug_varnames) }
|
||||||
|
/* These must appear regardless of . */
|
||||||
|
}
|
||||||
1438
arch_init.c
1438
arch_init.c
File diff suppressed because it is too large
Load Diff
34
arch_init.h
Normal file
34
arch_init.h
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
#ifndef QEMU_ARCH_INIT_H
|
||||||
|
#define QEMU_ARCH_INIT_H
|
||||||
|
|
||||||
|
extern const char arch_config_name[];
|
||||||
|
|
||||||
|
enum {
|
||||||
|
QEMU_ARCH_ALL = -1,
|
||||||
|
QEMU_ARCH_ALPHA = 1,
|
||||||
|
QEMU_ARCH_ARM = 2,
|
||||||
|
QEMU_ARCH_CRIS = 4,
|
||||||
|
QEMU_ARCH_I386 = 8,
|
||||||
|
QEMU_ARCH_M68K = 16,
|
||||||
|
QEMU_ARCH_LM32 = 32,
|
||||||
|
QEMU_ARCH_MICROBLAZE = 64,
|
||||||
|
QEMU_ARCH_MIPS = 128,
|
||||||
|
QEMU_ARCH_PPC = 256,
|
||||||
|
QEMU_ARCH_S390X = 512,
|
||||||
|
QEMU_ARCH_SH4 = 1024,
|
||||||
|
QEMU_ARCH_SPARC = 2048,
|
||||||
|
};
|
||||||
|
|
||||||
|
extern const uint32_t arch_type;
|
||||||
|
|
||||||
|
void select_soundhw(const char *optarg);
|
||||||
|
void do_acpitable_option(const char *optarg);
|
||||||
|
void do_smbios_option(const char *optarg);
|
||||||
|
void cpudef_init(void);
|
||||||
|
int audio_available(void);
|
||||||
|
void audio_init(qemu_irq *isa_pic, PCIBus *pci_bus);
|
||||||
|
int tcg_available(void);
|
||||||
|
int kvm_available(void);
|
||||||
|
int xen_available(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
509
arm-semi.c
Normal file
509
arm-semi.c
Normal file
@@ -0,0 +1,509 @@
|
|||||||
|
/*
|
||||||
|
* Arm "Angel" semihosting syscalls
|
||||||
|
*
|
||||||
|
* Copyright (c) 2005, 2007 CodeSourcery.
|
||||||
|
* Written by Paul Brook.
|
||||||
|
*
|
||||||
|
* This program is free software; you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation; either version 2 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program 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 General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include "cpu.h"
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
#include "qemu.h"
|
||||||
|
|
||||||
|
#define ARM_ANGEL_HEAP_SIZE (128 * 1024 * 1024)
|
||||||
|
#else
|
||||||
|
#include "qemu-common.h"
|
||||||
|
#include "gdbstub.h"
|
||||||
|
#include "hw/arm-misc.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define SYS_OPEN 0x01
|
||||||
|
#define SYS_CLOSE 0x02
|
||||||
|
#define SYS_WRITEC 0x03
|
||||||
|
#define SYS_WRITE0 0x04
|
||||||
|
#define SYS_WRITE 0x05
|
||||||
|
#define SYS_READ 0x06
|
||||||
|
#define SYS_READC 0x07
|
||||||
|
#define SYS_ISTTY 0x09
|
||||||
|
#define SYS_SEEK 0x0a
|
||||||
|
#define SYS_FLEN 0x0c
|
||||||
|
#define SYS_TMPNAM 0x0d
|
||||||
|
#define SYS_REMOVE 0x0e
|
||||||
|
#define SYS_RENAME 0x0f
|
||||||
|
#define SYS_CLOCK 0x10
|
||||||
|
#define SYS_TIME 0x11
|
||||||
|
#define SYS_SYSTEM 0x12
|
||||||
|
#define SYS_ERRNO 0x13
|
||||||
|
#define SYS_GET_CMDLINE 0x15
|
||||||
|
#define SYS_HEAPINFO 0x16
|
||||||
|
#define SYS_EXIT 0x18
|
||||||
|
|
||||||
|
#ifndef O_BINARY
|
||||||
|
#define O_BINARY 0
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define GDB_O_RDONLY 0x000
|
||||||
|
#define GDB_O_WRONLY 0x001
|
||||||
|
#define GDB_O_RDWR 0x002
|
||||||
|
#define GDB_O_APPEND 0x008
|
||||||
|
#define GDB_O_CREAT 0x200
|
||||||
|
#define GDB_O_TRUNC 0x400
|
||||||
|
#define GDB_O_BINARY 0
|
||||||
|
|
||||||
|
static int gdb_open_modeflags[12] = {
|
||||||
|
GDB_O_RDONLY,
|
||||||
|
GDB_O_RDONLY | GDB_O_BINARY,
|
||||||
|
GDB_O_RDWR,
|
||||||
|
GDB_O_RDWR | GDB_O_BINARY,
|
||||||
|
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC,
|
||||||
|
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
|
||||||
|
GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC,
|
||||||
|
GDB_O_RDWR | GDB_O_CREAT | GDB_O_TRUNC | GDB_O_BINARY,
|
||||||
|
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND,
|
||||||
|
GDB_O_WRONLY | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY,
|
||||||
|
GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND,
|
||||||
|
GDB_O_RDWR | GDB_O_CREAT | GDB_O_APPEND | GDB_O_BINARY
|
||||||
|
};
|
||||||
|
|
||||||
|
static int open_modeflags[12] = {
|
||||||
|
O_RDONLY,
|
||||||
|
O_RDONLY | O_BINARY,
|
||||||
|
O_RDWR,
|
||||||
|
O_RDWR | O_BINARY,
|
||||||
|
O_WRONLY | O_CREAT | O_TRUNC,
|
||||||
|
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
||||||
|
O_RDWR | O_CREAT | O_TRUNC,
|
||||||
|
O_RDWR | O_CREAT | O_TRUNC | O_BINARY,
|
||||||
|
O_WRONLY | O_CREAT | O_APPEND,
|
||||||
|
O_WRONLY | O_CREAT | O_APPEND | O_BINARY,
|
||||||
|
O_RDWR | O_CREAT | O_APPEND,
|
||||||
|
O_RDWR | O_CREAT | O_APPEND | O_BINARY
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
static inline uint32_t set_swi_errno(TaskState *ts, uint32_t code)
|
||||||
|
{
|
||||||
|
if (code == (uint32_t)-1)
|
||||||
|
ts->swi_errno = errno;
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
static inline uint32_t set_swi_errno(CPUState *env, uint32_t code)
|
||||||
|
{
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "softmmu-semi.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static target_ulong arm_semi_syscall_len;
|
||||||
|
|
||||||
|
#if !defined(CONFIG_USER_ONLY)
|
||||||
|
static target_ulong syscall_err;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static void arm_semi_cb(CPUState *env, target_ulong ret, target_ulong err)
|
||||||
|
{
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
TaskState *ts = env->opaque;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (ret == (target_ulong)-1) {
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
ts->swi_errno = err;
|
||||||
|
#else
|
||||||
|
syscall_err = err;
|
||||||
|
#endif
|
||||||
|
env->regs[0] = ret;
|
||||||
|
} else {
|
||||||
|
/* Fixup syscalls that use nonstardard return conventions. */
|
||||||
|
switch (env->regs[0]) {
|
||||||
|
case SYS_WRITE:
|
||||||
|
case SYS_READ:
|
||||||
|
env->regs[0] = arm_semi_syscall_len - ret;
|
||||||
|
break;
|
||||||
|
case SYS_SEEK:
|
||||||
|
env->regs[0] = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
env->regs[0] = ret;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void arm_semi_flen_cb(CPUState *env, target_ulong ret, target_ulong err)
|
||||||
|
{
|
||||||
|
/* The size is always stored in big-endian order, extract
|
||||||
|
the value. We assume the size always fit in 32 bits. */
|
||||||
|
uint32_t size;
|
||||||
|
cpu_memory_rw_debug(env, env->regs[13]-64+32, (uint8_t *)&size, 4, 0);
|
||||||
|
env->regs[0] = be32_to_cpu(size);
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
((TaskState *)env->opaque)->swi_errno = err;
|
||||||
|
#else
|
||||||
|
syscall_err = err;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#define ARG(n) \
|
||||||
|
({ \
|
||||||
|
target_ulong __arg; \
|
||||||
|
/* FIXME - handle get_user() failure */ \
|
||||||
|
get_user_ual(__arg, args + (n) * 4); \
|
||||||
|
__arg; \
|
||||||
|
})
|
||||||
|
#define SET_ARG(n, val) put_user_ual(val, args + (n) * 4)
|
||||||
|
uint32_t do_arm_semihosting(CPUState *env)
|
||||||
|
{
|
||||||
|
target_ulong args;
|
||||||
|
char * s;
|
||||||
|
int nr;
|
||||||
|
uint32_t ret;
|
||||||
|
uint32_t len;
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
TaskState *ts = env->opaque;
|
||||||
|
#else
|
||||||
|
CPUState *ts = env;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
nr = env->regs[0];
|
||||||
|
args = env->regs[1];
|
||||||
|
switch (nr) {
|
||||||
|
case SYS_OPEN:
|
||||||
|
if (!(s = lock_user_string(ARG(0))))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
if (ARG(1) >= 12)
|
||||||
|
return (uint32_t)-1;
|
||||||
|
if (strcmp(s, ":tt") == 0) {
|
||||||
|
if (ARG(1) < 4)
|
||||||
|
return STDIN_FILENO;
|
||||||
|
else
|
||||||
|
return STDOUT_FILENO;
|
||||||
|
}
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "open,%s,%x,1a4", ARG(0),
|
||||||
|
(int)ARG(2)+1, gdb_open_modeflags[ARG(1)]);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
ret = set_swi_errno(ts, open(s, open_modeflags[ARG(1)], 0644));
|
||||||
|
}
|
||||||
|
unlock_user(s, ARG(0), 0);
|
||||||
|
return ret;
|
||||||
|
case SYS_CLOSE:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "close,%x", ARG(0));
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
return set_swi_errno(ts, close(ARG(0)));
|
||||||
|
}
|
||||||
|
case SYS_WRITEC:
|
||||||
|
{
|
||||||
|
char c;
|
||||||
|
|
||||||
|
if (get_user_u8(c, args))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
/* Write to debug console. stderr is near enough. */
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "write,2,%x,1", args);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
return write(STDERR_FILENO, &c, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case SYS_WRITE0:
|
||||||
|
if (!(s = lock_user_string(args)))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
len = strlen(s);
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "write,2,%x,%x\n", args, len);
|
||||||
|
ret = env->regs[0];
|
||||||
|
} else {
|
||||||
|
ret = write(STDERR_FILENO, s, len);
|
||||||
|
}
|
||||||
|
unlock_user(s, args, 0);
|
||||||
|
return ret;
|
||||||
|
case SYS_WRITE:
|
||||||
|
len = ARG(2);
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
arm_semi_syscall_len = len;
|
||||||
|
gdb_do_syscall(arm_semi_cb, "write,%x,%x,%x", ARG(0), ARG(1), len);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
if (!(s = lock_user(VERIFY_READ, ARG(1), len, 1)))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
ret = set_swi_errno(ts, write(ARG(0), s, len));
|
||||||
|
unlock_user(s, ARG(1), 0);
|
||||||
|
if (ret == (uint32_t)-1)
|
||||||
|
return -1;
|
||||||
|
return len - ret;
|
||||||
|
}
|
||||||
|
case SYS_READ:
|
||||||
|
len = ARG(2);
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
arm_semi_syscall_len = len;
|
||||||
|
gdb_do_syscall(arm_semi_cb, "read,%x,%x,%x", ARG(0), ARG(1), len);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
if (!(s = lock_user(VERIFY_WRITE, ARG(1), len, 0)))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
do
|
||||||
|
ret = set_swi_errno(ts, read(ARG(0), s, len));
|
||||||
|
while (ret == -1 && errno == EINTR);
|
||||||
|
unlock_user(s, ARG(1), len);
|
||||||
|
if (ret == (uint32_t)-1)
|
||||||
|
return -1;
|
||||||
|
return len - ret;
|
||||||
|
}
|
||||||
|
case SYS_READC:
|
||||||
|
/* XXX: Read from debug cosole. Not implemented. */
|
||||||
|
return 0;
|
||||||
|
case SYS_ISTTY:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "isatty,%x", ARG(0));
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
return isatty(ARG(0));
|
||||||
|
}
|
||||||
|
case SYS_SEEK:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "lseek,%x,%x,0", ARG(0), ARG(1));
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
ret = set_swi_errno(ts, lseek(ARG(0), ARG(1), SEEK_SET));
|
||||||
|
if (ret == (uint32_t)-1)
|
||||||
|
return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case SYS_FLEN:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_flen_cb, "fstat,%x,%x",
|
||||||
|
ARG(0), env->regs[13]-64);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
struct stat buf;
|
||||||
|
ret = set_swi_errno(ts, fstat(ARG(0), &buf));
|
||||||
|
if (ret == (uint32_t)-1)
|
||||||
|
return -1;
|
||||||
|
return buf.st_size;
|
||||||
|
}
|
||||||
|
case SYS_TMPNAM:
|
||||||
|
/* XXX: Not implemented. */
|
||||||
|
return -1;
|
||||||
|
case SYS_REMOVE:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "unlink,%s", ARG(0), (int)ARG(1)+1);
|
||||||
|
ret = env->regs[0];
|
||||||
|
} else {
|
||||||
|
if (!(s = lock_user_string(ARG(0))))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
ret = set_swi_errno(ts, remove(s));
|
||||||
|
unlock_user(s, ARG(0), 0);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
case SYS_RENAME:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "rename,%s,%s",
|
||||||
|
ARG(0), (int)ARG(1)+1, ARG(2), (int)ARG(3)+1);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
char *s2;
|
||||||
|
s = lock_user_string(ARG(0));
|
||||||
|
s2 = lock_user_string(ARG(2));
|
||||||
|
if (!s || !s2)
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
ret = (uint32_t)-1;
|
||||||
|
else
|
||||||
|
ret = set_swi_errno(ts, rename(s, s2));
|
||||||
|
if (s2)
|
||||||
|
unlock_user(s2, ARG(2), 0);
|
||||||
|
if (s)
|
||||||
|
unlock_user(s, ARG(0), 0);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
case SYS_CLOCK:
|
||||||
|
return clock() / (CLOCKS_PER_SEC / 100);
|
||||||
|
case SYS_TIME:
|
||||||
|
return set_swi_errno(ts, time(NULL));
|
||||||
|
case SYS_SYSTEM:
|
||||||
|
if (use_gdb_syscalls()) {
|
||||||
|
gdb_do_syscall(arm_semi_cb, "system,%s", ARG(0), (int)ARG(1)+1);
|
||||||
|
return env->regs[0];
|
||||||
|
} else {
|
||||||
|
if (!(s = lock_user_string(ARG(0))))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
ret = set_swi_errno(ts, system(s));
|
||||||
|
unlock_user(s, ARG(0), 0);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
case SYS_ERRNO:
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
return ts->swi_errno;
|
||||||
|
#else
|
||||||
|
return syscall_err;
|
||||||
|
#endif
|
||||||
|
case SYS_GET_CMDLINE:
|
||||||
|
{
|
||||||
|
/* Build a command-line from the original argv.
|
||||||
|
*
|
||||||
|
* The inputs are:
|
||||||
|
* * ARG(0), pointer to a buffer of at least the size
|
||||||
|
* specified in ARG(1).
|
||||||
|
* * ARG(1), size of the buffer pointed to by ARG(0) in
|
||||||
|
* bytes.
|
||||||
|
*
|
||||||
|
* The outputs are:
|
||||||
|
* * ARG(0), pointer to null-terminated string of the
|
||||||
|
* command line.
|
||||||
|
* * ARG(1), length of the string pointed to by ARG(0).
|
||||||
|
*/
|
||||||
|
|
||||||
|
char *output_buffer;
|
||||||
|
size_t input_size = ARG(1);
|
||||||
|
size_t output_size;
|
||||||
|
int status = 0;
|
||||||
|
|
||||||
|
/* Compute the size of the output string. */
|
||||||
|
#if !defined(CONFIG_USER_ONLY)
|
||||||
|
output_size = strlen(ts->boot_info->kernel_filename)
|
||||||
|
+ 1 /* Separating space. */
|
||||||
|
+ strlen(ts->boot_info->kernel_cmdline)
|
||||||
|
+ 1; /* Terminating null byte. */
|
||||||
|
#else
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
output_size = ts->info->arg_end - ts->info->arg_start;
|
||||||
|
if (!output_size) {
|
||||||
|
/* We special-case the "empty command line" case (argc==0).
|
||||||
|
Just provide the terminating 0. */
|
||||||
|
output_size = 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (output_size > input_size) {
|
||||||
|
/* Not enough space to store command-line arguments. */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Adjust the command-line length. */
|
||||||
|
SET_ARG(1, output_size - 1);
|
||||||
|
|
||||||
|
/* Lock the buffer on the ARM side. */
|
||||||
|
output_buffer = lock_user(VERIFY_WRITE, ARG(0), output_size, 0);
|
||||||
|
if (!output_buffer) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Copy the command-line arguments. */
|
||||||
|
#if !defined(CONFIG_USER_ONLY)
|
||||||
|
pstrcpy(output_buffer, output_size, ts->boot_info->kernel_filename);
|
||||||
|
pstrcat(output_buffer, output_size, " ");
|
||||||
|
pstrcat(output_buffer, output_size, ts->boot_info->kernel_cmdline);
|
||||||
|
#else
|
||||||
|
if (output_size == 1) {
|
||||||
|
/* Empty command-line. */
|
||||||
|
output_buffer[0] = '\0';
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (copy_from_user(output_buffer, ts->info->arg_start,
|
||||||
|
output_size)) {
|
||||||
|
status = -1;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Separate arguments by white spaces. */
|
||||||
|
for (i = 0; i < output_size - 1; i++) {
|
||||||
|
if (output_buffer[i] == 0) {
|
||||||
|
output_buffer[i] = ' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out:
|
||||||
|
#endif
|
||||||
|
/* Unlock the buffer on the ARM side. */
|
||||||
|
unlock_user(output_buffer, ARG(0), output_size);
|
||||||
|
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
case SYS_HEAPINFO:
|
||||||
|
{
|
||||||
|
uint32_t *ptr;
|
||||||
|
uint32_t limit;
|
||||||
|
|
||||||
|
#ifdef CONFIG_USER_ONLY
|
||||||
|
/* Some C libraries assume the heap immediately follows .bss, so
|
||||||
|
allocate it using sbrk. */
|
||||||
|
if (!ts->heap_limit) {
|
||||||
|
abi_ulong ret;
|
||||||
|
|
||||||
|
ts->heap_base = do_brk(0);
|
||||||
|
limit = ts->heap_base + ARM_ANGEL_HEAP_SIZE;
|
||||||
|
/* Try a big heap, and reduce the size if that fails. */
|
||||||
|
for (;;) {
|
||||||
|
ret = do_brk(limit);
|
||||||
|
if (ret >= limit) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
limit = (ts->heap_base >> 1) + (limit >> 1);
|
||||||
|
}
|
||||||
|
ts->heap_limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
ptr[0] = tswap32(ts->heap_base);
|
||||||
|
ptr[1] = tswap32(ts->heap_limit);
|
||||||
|
ptr[2] = tswap32(ts->stack_base);
|
||||||
|
ptr[3] = tswap32(0); /* Stack limit. */
|
||||||
|
unlock_user(ptr, ARG(0), 16);
|
||||||
|
#else
|
||||||
|
limit = ram_size;
|
||||||
|
if (!(ptr = lock_user(VERIFY_WRITE, ARG(0), 16, 0)))
|
||||||
|
/* FIXME - should this error code be -TARGET_EFAULT ? */
|
||||||
|
return (uint32_t)-1;
|
||||||
|
/* TODO: Make this use the limit of the loaded application. */
|
||||||
|
ptr[0] = tswap32(limit / 2);
|
||||||
|
ptr[1] = tswap32(limit);
|
||||||
|
ptr[2] = tswap32(limit); /* Stack base */
|
||||||
|
ptr[3] = tswap32(0); /* Stack limit. */
|
||||||
|
unlock_user(ptr, ARG(0), 16);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
case SYS_EXIT:
|
||||||
|
gdb_exit(env, 0);
|
||||||
|
exit(0);
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "qemu: Unsupported SemiHosting SWI 0x%02x\n", nr);
|
||||||
|
cpu_dump_state(env, stderr, fprintf, 0);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
153
arm.ld
Normal file
153
arm.ld
Normal file
@@ -0,0 +1,153 @@
|
|||||||
|
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm",
|
||||||
|
"elf32-littlearm")
|
||||||
|
OUTPUT_ARCH(arm)
|
||||||
|
ENTRY(_start)
|
||||||
|
SECTIONS
|
||||||
|
{
|
||||||
|
/* Read-only sections, merged into text segment: */
|
||||||
|
. = 0x60000000 + SIZEOF_HEADERS;
|
||||||
|
.interp : { *(.interp) }
|
||||||
|
.hash : { *(.hash) }
|
||||||
|
.dynsym : { *(.dynsym) }
|
||||||
|
.dynstr : { *(.dynstr) }
|
||||||
|
.gnu.version : { *(.gnu.version) }
|
||||||
|
.gnu.version_d : { *(.gnu.version_d) }
|
||||||
|
.gnu.version_r : { *(.gnu.version_r) }
|
||||||
|
.rel.text :
|
||||||
|
{ *(.rel.text) *(.rel.gnu.linkonce.t*) }
|
||||||
|
.rela.text :
|
||||||
|
{ *(.rela.text) *(.rela.gnu.linkonce.t*) }
|
||||||
|
.rel.data :
|
||||||
|
{ *(.rel.data) *(.rel.gnu.linkonce.d*) }
|
||||||
|
.rela.data :
|
||||||
|
{ *(.rela.data) *(.rela.gnu.linkonce.d*) }
|
||||||
|
.rel.rodata :
|
||||||
|
{ *(.rel.rodata) *(.rel.gnu.linkonce.r*) }
|
||||||
|
.rela.rodata :
|
||||||
|
{ *(.rela.rodata) *(.rela.gnu.linkonce.r*) }
|
||||||
|
.rel.got : { *(.rel.got) }
|
||||||
|
.rela.got : { *(.rela.got) }
|
||||||
|
.rel.ctors : { *(.rel.ctors) }
|
||||||
|
.rela.ctors : { *(.rela.ctors) }
|
||||||
|
.rel.dtors : { *(.rel.dtors) }
|
||||||
|
.rela.dtors : { *(.rela.dtors) }
|
||||||
|
.rel.init : { *(.rel.init) }
|
||||||
|
.rela.init : { *(.rela.init) }
|
||||||
|
.rel.fini : { *(.rel.fini) }
|
||||||
|
.rela.fini : { *(.rela.fini) }
|
||||||
|
.rel.bss : { *(.rel.bss) }
|
||||||
|
.rela.bss : { *(.rela.bss) }
|
||||||
|
.rel.plt : { *(.rel.plt) }
|
||||||
|
.rela.plt : { *(.rela.plt) }
|
||||||
|
.init : { *(.init) } =0x47ff041f
|
||||||
|
.text :
|
||||||
|
{
|
||||||
|
*(.text)
|
||||||
|
/* .gnu.warning sections are handled specially by elf32.em. */
|
||||||
|
*(.gnu.warning)
|
||||||
|
*(.gnu.linkonce.t*)
|
||||||
|
} =0x47ff041f
|
||||||
|
_etext = .;
|
||||||
|
PROVIDE (etext = .);
|
||||||
|
.fini : { *(.fini) } =0x47ff041f
|
||||||
|
.rodata : { *(.rodata) *(.gnu.linkonce.r*) }
|
||||||
|
.rodata1 : { *(.rodata1) }
|
||||||
|
.ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) }
|
||||||
|
__exidx_start = .;
|
||||||
|
.ARM.exidx : { *(.ARM.exidx* .gnu.linkonce.armexidx.*) }
|
||||||
|
__exidx_end = .;
|
||||||
|
.reginfo : { *(.reginfo) }
|
||||||
|
/* Adjust the address for the data segment. We want to adjust up to
|
||||||
|
the same address within the page on the next page up. */
|
||||||
|
. = ALIGN(0x100000) + (. & (0x100000 - 1));
|
||||||
|
.data :
|
||||||
|
{
|
||||||
|
*(.gen_code)
|
||||||
|
*(.data)
|
||||||
|
*(.gnu.linkonce.d*)
|
||||||
|
CONSTRUCTORS
|
||||||
|
}
|
||||||
|
.tbss : { *(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon) }
|
||||||
|
.data1 : { *(.data1) }
|
||||||
|
.preinit_array :
|
||||||
|
{
|
||||||
|
PROVIDE (__preinit_array_start = .);
|
||||||
|
KEEP (*(.preinit_array))
|
||||||
|
PROVIDE (__preinit_array_end = .);
|
||||||
|
}
|
||||||
|
.init_array :
|
||||||
|
{
|
||||||
|
PROVIDE (__init_array_start = .);
|
||||||
|
KEEP (*(SORT(.init_array.*)))
|
||||||
|
KEEP (*(.init_array))
|
||||||
|
PROVIDE (__init_array_end = .);
|
||||||
|
}
|
||||||
|
.fini_array :
|
||||||
|
{
|
||||||
|
PROVIDE (__fini_array_start = .);
|
||||||
|
KEEP (*(.fini_array))
|
||||||
|
KEEP (*(SORT(.fini_array.*)))
|
||||||
|
PROVIDE (__fini_array_end = .);
|
||||||
|
}
|
||||||
|
.ctors :
|
||||||
|
{
|
||||||
|
*(.ctors)
|
||||||
|
}
|
||||||
|
.dtors :
|
||||||
|
{
|
||||||
|
*(.dtors)
|
||||||
|
}
|
||||||
|
.plt : { *(.plt) }
|
||||||
|
.got : { *(.got.plt) *(.got) }
|
||||||
|
.dynamic : { *(.dynamic) }
|
||||||
|
/* We want the small data sections together, so single-instruction offsets
|
||||||
|
can access them all, and initialized data all before uninitialized, so
|
||||||
|
we can shorten the on-disk segment size. */
|
||||||
|
.sdata : { *(.sdata) }
|
||||||
|
_edata = .;
|
||||||
|
PROVIDE (edata = .);
|
||||||
|
__bss_start = .;
|
||||||
|
.sbss : { *(.sbss) *(.scommon) }
|
||||||
|
.bss :
|
||||||
|
{
|
||||||
|
*(.dynbss)
|
||||||
|
*(.bss)
|
||||||
|
*(COMMON)
|
||||||
|
}
|
||||||
|
_end = . ;
|
||||||
|
PROVIDE (end = .);
|
||||||
|
/* Stabs debugging sections. */
|
||||||
|
.stab 0 : { *(.stab) }
|
||||||
|
.stabstr 0 : { *(.stabstr) }
|
||||||
|
.stab.excl 0 : { *(.stab.excl) }
|
||||||
|
.stab.exclstr 0 : { *(.stab.exclstr) }
|
||||||
|
.stab.index 0 : { *(.stab.index) }
|
||||||
|
.stab.indexstr 0 : { *(.stab.indexstr) }
|
||||||
|
.comment 0 : { *(.comment) }
|
||||||
|
/* DWARF debug sections.
|
||||||
|
Symbols in the DWARF debugging sections are relative to the beginning
|
||||||
|
of the section so we begin them at 0. */
|
||||||
|
/* DWARF 1 */
|
||||||
|
.debug 0 : { *(.debug) }
|
||||||
|
.line 0 : { *(.line) }
|
||||||
|
/* GNU DWARF 1 extensions */
|
||||||
|
.debug_srcinfo 0 : { *(.debug_srcinfo) }
|
||||||
|
.debug_sfnames 0 : { *(.debug_sfnames) }
|
||||||
|
/* DWARF 1.1 and DWARF 2 */
|
||||||
|
.debug_aranges 0 : { *(.debug_aranges) }
|
||||||
|
.debug_pubnames 0 : { *(.debug_pubnames) }
|
||||||
|
/* DWARF 2 */
|
||||||
|
.debug_info 0 : { *(.debug_info) }
|
||||||
|
.debug_abbrev 0 : { *(.debug_abbrev) }
|
||||||
|
.debug_line 0 : { *(.debug_line) }
|
||||||
|
.debug_frame 0 : { *(.debug_frame) }
|
||||||
|
.debug_str 0 : { *(.debug_str) }
|
||||||
|
.debug_loc 0 : { *(.debug_loc) }
|
||||||
|
.debug_macinfo 0 : { *(.debug_macinfo) }
|
||||||
|
/* SGI/MIPS DWARF 2 extensions */
|
||||||
|
.debug_weaknames 0 : { *(.debug_weaknames) }
|
||||||
|
.debug_funcnames 0 : { *(.debug_funcnames) }
|
||||||
|
.debug_typenames 0 : { *(.debug_typenames) }
|
||||||
|
.debug_varnames 0 : { *(.debug_varnames) }
|
||||||
|
/* These must appear regardless of . */
|
||||||
|
}
|
||||||
339
async.c
339
async.c
@@ -23,61 +23,128 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/aio.h"
|
#include "qemu-aio.h"
|
||||||
#include "block/thread-pool.h"
|
|
||||||
#include "qemu/main-loop.h"
|
/*
|
||||||
#include "qemu/atomic.h"
|
* An AsyncContext protects the callbacks of AIO requests and Bottom Halves
|
||||||
|
* against interfering with each other. A typical example is qcow2 that accepts
|
||||||
|
* asynchronous requests, but relies for manipulation of its metadata on
|
||||||
|
* synchronous bdrv_read/write that doesn't trigger any callbacks.
|
||||||
|
*
|
||||||
|
* However, these functions are often emulated using AIO which means that AIO
|
||||||
|
* callbacks must be run - but at the same time we must not run callbacks of
|
||||||
|
* other requests as they might start to modify metadata and corrupt the
|
||||||
|
* internal state of the caller of bdrv_read/write.
|
||||||
|
*
|
||||||
|
* To achieve the desired semantics we switch into a new AsyncContext.
|
||||||
|
* Callbacks must only be run if they belong to the current AsyncContext.
|
||||||
|
* Otherwise they need to be queued until their own context is active again.
|
||||||
|
* This is how you can make qemu_aio_wait() wait only for your own callbacks.
|
||||||
|
*
|
||||||
|
* The AsyncContexts form a stack. When you leave a AsyncContexts, you always
|
||||||
|
* return to the old ("parent") context.
|
||||||
|
*/
|
||||||
|
struct AsyncContext {
|
||||||
|
/* Consecutive number of the AsyncContext (position in the stack) */
|
||||||
|
int id;
|
||||||
|
|
||||||
|
/* Anchor of the list of Bottom Halves belonging to the context */
|
||||||
|
struct QEMUBH *first_bh;
|
||||||
|
|
||||||
|
/* Link to parent context */
|
||||||
|
struct AsyncContext *parent;
|
||||||
|
};
|
||||||
|
|
||||||
|
/* The currently active AsyncContext */
|
||||||
|
static struct AsyncContext *async_context = &(struct AsyncContext) { 0 };
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enter a new AsyncContext. Already scheduled Bottom Halves and AIO callbacks
|
||||||
|
* won't be called until this context is left again.
|
||||||
|
*/
|
||||||
|
void async_context_push(void)
|
||||||
|
{
|
||||||
|
struct AsyncContext *new = qemu_mallocz(sizeof(*new));
|
||||||
|
new->parent = async_context;
|
||||||
|
new->id = async_context->id + 1;
|
||||||
|
async_context = new;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Run queued AIO completions and destroy Bottom Half */
|
||||||
|
static void bh_run_aio_completions(void *opaque)
|
||||||
|
{
|
||||||
|
QEMUBH **bh = opaque;
|
||||||
|
qemu_bh_delete(*bh);
|
||||||
|
qemu_free(bh);
|
||||||
|
qemu_aio_process_queue();
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
* Leave the currently active AsyncContext. All Bottom Halves belonging to the
|
||||||
|
* old context are executed before changing the context.
|
||||||
|
*/
|
||||||
|
void async_context_pop(void)
|
||||||
|
{
|
||||||
|
struct AsyncContext *old = async_context;
|
||||||
|
QEMUBH **bh;
|
||||||
|
|
||||||
|
/* Flush the bottom halves, we don't want to lose them */
|
||||||
|
while (qemu_bh_poll());
|
||||||
|
|
||||||
|
/* Switch back to the parent context */
|
||||||
|
async_context = async_context->parent;
|
||||||
|
qemu_free(old);
|
||||||
|
|
||||||
|
if (async_context == NULL) {
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Schedule BH to run any queued AIO completions as soon as possible */
|
||||||
|
bh = qemu_malloc(sizeof(*bh));
|
||||||
|
*bh = qemu_bh_new(bh_run_aio_completions, bh);
|
||||||
|
qemu_bh_schedule(*bh);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Returns the ID of the currently active AsyncContext
|
||||||
|
*/
|
||||||
|
int get_async_context_id(void)
|
||||||
|
{
|
||||||
|
return async_context->id;
|
||||||
|
}
|
||||||
|
|
||||||
/***********************************************************/
|
/***********************************************************/
|
||||||
/* bottom halves (can be seen as timers which expire ASAP) */
|
/* bottom halves (can be seen as timers which expire ASAP) */
|
||||||
|
|
||||||
struct QEMUBH {
|
struct QEMUBH {
|
||||||
AioContext *ctx;
|
|
||||||
QEMUBHFunc *cb;
|
QEMUBHFunc *cb;
|
||||||
void *opaque;
|
void *opaque;
|
||||||
|
int scheduled;
|
||||||
|
int idle;
|
||||||
|
int deleted;
|
||||||
QEMUBH *next;
|
QEMUBH *next;
|
||||||
bool scheduled;
|
|
||||||
bool idle;
|
|
||||||
bool deleted;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
QEMUBH *aio_bh_new(AioContext *ctx, QEMUBHFunc *cb, void *opaque)
|
QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
bh = g_new(QEMUBH, 1);
|
bh = qemu_mallocz(sizeof(QEMUBH));
|
||||||
*bh = (QEMUBH){
|
bh->cb = cb;
|
||||||
.ctx = ctx,
|
bh->opaque = opaque;
|
||||||
.cb = cb,
|
bh->next = async_context->first_bh;
|
||||||
.opaque = opaque,
|
async_context->first_bh = bh;
|
||||||
};
|
|
||||||
qemu_mutex_lock(&ctx->bh_lock);
|
|
||||||
bh->next = ctx->first_bh;
|
|
||||||
/* Make sure that the members are ready before putting bh into list */
|
|
||||||
smp_wmb();
|
|
||||||
ctx->first_bh = bh;
|
|
||||||
qemu_mutex_unlock(&ctx->bh_lock);
|
|
||||||
return bh;
|
return bh;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Multiple occurrences of aio_bh_poll cannot be called concurrently */
|
int qemu_bh_poll(void)
|
||||||
int aio_bh_poll(AioContext *ctx)
|
|
||||||
{
|
{
|
||||||
QEMUBH *bh, **bhp, *next;
|
QEMUBH *bh, **bhp, *next;
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ctx->walking_bh++;
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
for (bh = ctx->first_bh; bh; bh = next) {
|
for (bh = async_context->first_bh; bh; bh = next) {
|
||||||
/* Make sure that fetching bh happens before accessing its members */
|
|
||||||
smp_read_barrier_depends();
|
|
||||||
next = bh->next;
|
next = bh->next;
|
||||||
if (!bh->deleted && bh->scheduled) {
|
if (!bh->deleted && bh->scheduled) {
|
||||||
bh->scheduled = 0;
|
bh->scheduled = 0;
|
||||||
/* Paired with write barrier in bh schedule to ensure reading for
|
|
||||||
* idle & callbacks coming after bh's scheduling.
|
|
||||||
*/
|
|
||||||
smp_rmb();
|
|
||||||
if (!bh->idle)
|
if (!bh->idle)
|
||||||
ret = 1;
|
ret = 1;
|
||||||
bh->idle = 0;
|
bh->idle = 0;
|
||||||
@@ -85,23 +152,16 @@ int aio_bh_poll(AioContext *ctx)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->walking_bh--;
|
|
||||||
|
|
||||||
/* remove deleted bhs */
|
/* remove deleted bhs */
|
||||||
if (!ctx->walking_bh) {
|
bhp = &async_context->first_bh;
|
||||||
qemu_mutex_lock(&ctx->bh_lock);
|
|
||||||
bhp = &ctx->first_bh;
|
|
||||||
while (*bhp) {
|
while (*bhp) {
|
||||||
bh = *bhp;
|
bh = *bhp;
|
||||||
if (bh->deleted) {
|
if (bh->deleted) {
|
||||||
*bhp = bh->next;
|
*bhp = bh->next;
|
||||||
g_free(bh);
|
qemu_free(bh);
|
||||||
} else {
|
} else
|
||||||
bhp = &bh->next;
|
bhp = &bh->next;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
qemu_mutex_unlock(&ctx->bh_lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
@@ -110,227 +170,48 @@ void qemu_bh_schedule_idle(QEMUBH *bh)
|
|||||||
{
|
{
|
||||||
if (bh->scheduled)
|
if (bh->scheduled)
|
||||||
return;
|
return;
|
||||||
bh->idle = 1;
|
|
||||||
/* Make sure that idle & any writes needed by the callback are done
|
|
||||||
* before the locations are read in the aio_bh_poll.
|
|
||||||
*/
|
|
||||||
smp_wmb();
|
|
||||||
bh->scheduled = 1;
|
bh->scheduled = 1;
|
||||||
|
bh->idle = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qemu_bh_schedule(QEMUBH *bh)
|
void qemu_bh_schedule(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
AioContext *ctx;
|
|
||||||
|
|
||||||
if (bh->scheduled)
|
if (bh->scheduled)
|
||||||
return;
|
return;
|
||||||
ctx = bh->ctx;
|
|
||||||
bh->idle = 0;
|
|
||||||
/* Make sure that:
|
|
||||||
* 1. idle & any writes needed by the callback are done before the
|
|
||||||
* locations are read in the aio_bh_poll.
|
|
||||||
* 2. ctx is loaded before scheduled is set and the callback has a chance
|
|
||||||
* to execute.
|
|
||||||
*/
|
|
||||||
smp_mb();
|
|
||||||
bh->scheduled = 1;
|
bh->scheduled = 1;
|
||||||
aio_notify(ctx);
|
bh->idle = 0;
|
||||||
|
/* stop the currently executing CPU to execute the BH ASAP */
|
||||||
|
qemu_notify_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* This func is async.
|
|
||||||
*/
|
|
||||||
void qemu_bh_cancel(QEMUBH *bh)
|
void qemu_bh_cancel(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
bh->scheduled = 0;
|
bh->scheduled = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* This func is async.The bottom half will do the delete action at the finial
|
|
||||||
* end.
|
|
||||||
*/
|
|
||||||
void qemu_bh_delete(QEMUBH *bh)
|
void qemu_bh_delete(QEMUBH *bh)
|
||||||
{
|
{
|
||||||
bh->scheduled = 0;
|
bh->scheduled = 0;
|
||||||
bh->deleted = 1;
|
bh->deleted = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t
|
void qemu_bh_update_timeout(int *timeout)
|
||||||
aio_compute_timeout(AioContext *ctx)
|
|
||||||
{
|
{
|
||||||
int64_t deadline;
|
|
||||||
int timeout = -1;
|
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
|
|
||||||
for (bh = ctx->first_bh; bh; bh = bh->next) {
|
for (bh = async_context->first_bh; bh; bh = bh->next) {
|
||||||
if (!bh->deleted && bh->scheduled) {
|
if (!bh->deleted && bh->scheduled) {
|
||||||
if (bh->idle) {
|
if (bh->idle) {
|
||||||
/* idle bottom halves will be polled at least
|
/* idle bottom halves will be polled at least
|
||||||
* every 10ms */
|
* every 10ms */
|
||||||
timeout = 10000000;
|
*timeout = MIN(10, *timeout);
|
||||||
} else {
|
} else {
|
||||||
/* non-idle bottom halves will be executed
|
/* non-idle bottom halves will be executed
|
||||||
* immediately */
|
* immediately */
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deadline = timerlistgroup_deadline_ns(&ctx->tlg);
|
|
||||||
if (deadline == 0) {
|
|
||||||
return 0;
|
|
||||||
} else {
|
|
||||||
return qemu_soonest_timeout(timeout, deadline);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
aio_ctx_prepare(GSource *source, gint *timeout)
|
|
||||||
{
|
|
||||||
AioContext *ctx = (AioContext *) source;
|
|
||||||
|
|
||||||
/* We assume there is no timeout already supplied */
|
|
||||||
*timeout = qemu_timeout_ns_to_ms(aio_compute_timeout(ctx));
|
|
||||||
|
|
||||||
if (aio_prepare(ctx)) {
|
|
||||||
*timeout = 0;
|
*timeout = 0;
|
||||||
}
|
break;
|
||||||
|
|
||||||
return *timeout == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) || (timerlistgroup_deadline_ns(&ctx->tlg) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static gboolean
|
|
||||||
aio_ctx_dispatch(GSource *source,
|
|
||||||
GSourceFunc callback,
|
|
||||||
gpointer user_data)
|
|
||||||
{
|
|
||||||
AioContext *ctx = (AioContext *) source;
|
|
||||||
|
|
||||||
assert(callback == NULL);
|
|
||||||
aio_dispatch(ctx);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
aio_ctx_finalize(GSource *source)
|
|
||||||
{
|
|
||||||
AioContext *ctx = (AioContext *) source;
|
|
||||||
|
|
||||||
thread_pool_free(ctx->thread_pool);
|
|
||||||
aio_set_event_notifier(ctx, &ctx->notifier, NULL);
|
|
||||||
event_notifier_cleanup(&ctx->notifier);
|
|
||||||
rfifolock_destroy(&ctx->lock);
|
|
||||||
qemu_mutex_destroy(&ctx->bh_lock);
|
|
||||||
g_array_free(ctx->pollfds, TRUE);
|
|
||||||
timerlistgroup_deinit(&ctx->tlg);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
ThreadPool *aio_get_thread_pool(AioContext *ctx)
|
|
||||||
{
|
|
||||||
if (!ctx->thread_pool) {
|
|
||||||
ctx->thread_pool = thread_pool_new(ctx);
|
|
||||||
}
|
|
||||||
return ctx->thread_pool;
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_set_dispatching(AioContext *ctx, bool dispatching)
|
|
||||||
{
|
|
||||||
ctx->dispatching = dispatching;
|
|
||||||
if (!dispatching) {
|
|
||||||
/* Write ctx->dispatching before reading e.g. bh->scheduled.
|
|
||||||
* Optimization: this is only needed when we're entering the "unsafe"
|
|
||||||
* phase where other threads must call event_notifier_set.
|
|
||||||
*/
|
|
||||||
smp_mb();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void aio_notify(AioContext *ctx)
|
|
||||||
{
|
|
||||||
/* Write e.g. bh->scheduled before reading ctx->dispatching. */
|
|
||||||
smp_mb();
|
|
||||||
if (!ctx->dispatching) {
|
|
||||||
event_notifier_set(&ctx->notifier);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void aio_timerlist_notify(void *opaque)
|
|
||||||
{
|
|
||||||
aio_notify(opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void aio_rfifolock_cb(void *opaque)
|
|
||||||
{
|
|
||||||
/* Kick owner thread in case they are blocked in aio_poll() */
|
|
||||||
aio_notify(opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
AioContext *aio_context_new(Error **errp)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
AioContext *ctx;
|
|
||||||
ctx = (AioContext *) g_source_new(&aio_source_funcs, sizeof(AioContext));
|
|
||||||
ret = event_notifier_init(&ctx->notifier, false);
|
|
||||||
if (ret < 0) {
|
|
||||||
g_source_destroy(&ctx->source);
|
|
||||||
error_setg_errno(errp, -ret, "Failed to initialize event notifier");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
g_source_set_can_recurse(&ctx->source, true);
|
|
||||||
aio_set_event_notifier(ctx, &ctx->notifier,
|
|
||||||
(EventNotifierHandler *)
|
|
||||||
event_notifier_test_and_clear);
|
|
||||||
ctx->pollfds = g_array_new(FALSE, FALSE, sizeof(GPollFD));
|
|
||||||
ctx->thread_pool = NULL;
|
|
||||||
qemu_mutex_init(&ctx->bh_lock);
|
|
||||||
rfifolock_init(&ctx->lock, aio_rfifolock_cb, ctx);
|
|
||||||
timerlistgroup_init(&ctx->tlg, aio_timerlist_notify, ctx);
|
|
||||||
|
|
||||||
return ctx;
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_context_ref(AioContext *ctx)
|
|
||||||
{
|
|
||||||
g_source_ref(&ctx->source);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_context_unref(AioContext *ctx)
|
|
||||||
{
|
|
||||||
g_source_unref(&ctx->source);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_context_acquire(AioContext *ctx)
|
|
||||||
{
|
|
||||||
rfifolock_lock(&ctx->lock);
|
|
||||||
}
|
|
||||||
|
|
||||||
void aio_context_release(AioContext *ctx)
|
|
||||||
{
|
|
||||||
rfifolock_unlock(&ctx->lock);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
common-obj-y = audio.o noaudio.o wavaudio.o mixeng.o
|
|
||||||
common-obj-$(CONFIG_SDL) += sdlaudio.o
|
|
||||||
common-obj-$(CONFIG_OSS) += ossaudio.o
|
|
||||||
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
|
|
||||||
|
|
||||||
$(obj)/audio.o $(obj)/fmodaudio.o: QEMU_CFLAGS += $(FMOD_CFLAGS)
|
|
||||||
sdlaudio.o-cflags := $(SDL_CFLAGS)
|
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
*/
|
*/
|
||||||
#include <alsa/asoundlib.h>
|
#include <alsa/asoundlib.h>
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/main-loop.h"
|
#include "qemu-char.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
#if QEMU_GNUC_PREREQ(4, 3)
|
#if QEMU_GNUC_PREREQ(4, 3)
|
||||||
@@ -136,7 +136,7 @@ static void alsa_fini_poll (struct pollhlp *hlp)
|
|||||||
for (i = 0; i < hlp->count; ++i) {
|
for (i = 0; i < hlp->count; ++i) {
|
||||||
qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
|
qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
g_free (pfds);
|
qemu_free (pfds);
|
||||||
}
|
}
|
||||||
hlp->pfds = NULL;
|
hlp->pfds = NULL;
|
||||||
hlp->count = 0;
|
hlp->count = 0;
|
||||||
@@ -260,7 +260,7 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
|
|||||||
if (err < 0) {
|
if (err < 0) {
|
||||||
alsa_logerr (err, "Could not initialize poll mode\n"
|
alsa_logerr (err, "Could not initialize poll mode\n"
|
||||||
"Could not obtain poll descriptors\n");
|
"Could not obtain poll descriptors\n");
|
||||||
g_free (pfds);
|
qemu_free (pfds);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,7 +288,7 @@ static int alsa_poll_helper (snd_pcm_t *handle, struct pollhlp *hlp, int mask)
|
|||||||
while (i--) {
|
while (i--) {
|
||||||
qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
|
qemu_set_fd_handler (pfds[i].fd, NULL, NULL, NULL);
|
||||||
}
|
}
|
||||||
g_free (pfds);
|
qemu_free (pfds);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -815,8 +815,10 @@ static void alsa_fini_out (HWVoiceOut *hw)
|
|||||||
ldebug ("alsa_fini\n");
|
ldebug ("alsa_fini\n");
|
||||||
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
||||||
|
|
||||||
g_free(alsa->pcm_buf);
|
if (alsa->pcm_buf) {
|
||||||
|
qemu_free (alsa->pcm_buf);
|
||||||
alsa->pcm_buf = NULL;
|
alsa->pcm_buf = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
static int alsa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
@@ -976,8 +978,10 @@ static void alsa_fini_in (HWVoiceIn *hw)
|
|||||||
|
|
||||||
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
alsa_anal_close (&alsa->handle, &alsa->pollhlp);
|
||||||
|
|
||||||
g_free(alsa->pcm_buf);
|
if (alsa->pcm_buf) {
|
||||||
|
qemu_free (alsa->pcm_buf);
|
||||||
alsa->pcm_buf = NULL;
|
alsa->pcm_buf = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int alsa_run_in (HWVoiceIn *hw)
|
static int alsa_run_in (HWVoiceIn *hw)
|
||||||
|
|||||||
@@ -23,9 +23,9 @@
|
|||||||
*/
|
*/
|
||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "monitor/monitor.h"
|
#include "monitor.h"
|
||||||
#include "qemu/timer.h"
|
#include "qemu-timer.h"
|
||||||
#include "sysemu/sysemu.h"
|
#include "sysemu.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "audio"
|
#define AUDIO_CAP "audio"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
@@ -95,7 +95,7 @@ static struct {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
.period = { .hertz = 100 },
|
.period = { .hertz = 250 },
|
||||||
.plive = 0,
|
.plive = 0,
|
||||||
.log_to_monitor = 0,
|
.log_to_monitor = 0,
|
||||||
.try_poll_in = 1,
|
.try_poll_in = 1,
|
||||||
@@ -196,7 +196,7 @@ void *audio_calloc (const char *funcname, int nmemb, size_t size)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return g_malloc0 (len);
|
return qemu_mallocz (len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *audio_alloc_prefix (const char *s)
|
static char *audio_alloc_prefix (const char *s)
|
||||||
@@ -210,7 +210,7 @@ static char *audio_alloc_prefix (const char *s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
len = strlen (s);
|
len = strlen (s);
|
||||||
r = g_malloc (len + sizeof (qemu_prefix));
|
r = qemu_malloc (len + sizeof (qemu_prefix));
|
||||||
|
|
||||||
u = r + sizeof (qemu_prefix) - 1;
|
u = r + sizeof (qemu_prefix) - 1;
|
||||||
|
|
||||||
@@ -425,7 +425,7 @@ static void audio_print_options (const char *prefix,
|
|||||||
printf (" %s\n", opt->descr);
|
printf (" %s\n", opt->descr);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (uprefix);
|
qemu_free (uprefix);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void audio_process_options (const char *prefix,
|
static void audio_process_options (const char *prefix,
|
||||||
@@ -462,7 +462,7 @@ static void audio_process_options (const char *prefix,
|
|||||||
* (includes trailing zero) + zero + underscore (on behalf of
|
* (includes trailing zero) + zero + underscore (on behalf of
|
||||||
* sizeof) */
|
* sizeof) */
|
||||||
optlen = len + preflen + sizeof (qemu_prefix) + 1;
|
optlen = len + preflen + sizeof (qemu_prefix) + 1;
|
||||||
optname = g_malloc (optlen);
|
optname = qemu_malloc (optlen);
|
||||||
|
|
||||||
pstrcpy (optname, optlen, qemu_prefix);
|
pstrcpy (optname, optlen, qemu_prefix);
|
||||||
|
|
||||||
@@ -507,7 +507,7 @@ static void audio_process_options (const char *prefix,
|
|||||||
opt->overriddenp = &opt->overridden;
|
opt->overriddenp = &opt->overridden;
|
||||||
}
|
}
|
||||||
*opt->overriddenp = !def;
|
*opt->overriddenp = !def;
|
||||||
g_free (optname);
|
qemu_free (optname);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -585,20 +585,17 @@ static int audio_pcm_info_eq (struct audio_pcm_info *info, struct audsettings *a
|
|||||||
switch (as->fmt) {
|
switch (as->fmt) {
|
||||||
case AUD_FMT_S8:
|
case AUD_FMT_S8:
|
||||||
sign = 1;
|
sign = 1;
|
||||||
/* fall through */
|
|
||||||
case AUD_FMT_U8:
|
case AUD_FMT_U8:
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AUD_FMT_S16:
|
case AUD_FMT_S16:
|
||||||
sign = 1;
|
sign = 1;
|
||||||
/* fall through */
|
|
||||||
case AUD_FMT_U16:
|
case AUD_FMT_U16:
|
||||||
bits = 16;
|
bits = 16;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case AUD_FMT_S32:
|
case AUD_FMT_S32:
|
||||||
sign = 1;
|
sign = 1;
|
||||||
/* fall through */
|
|
||||||
case AUD_FMT_U32:
|
case AUD_FMT_U32:
|
||||||
bits = 32;
|
bits = 32;
|
||||||
break;
|
break;
|
||||||
@@ -781,7 +778,7 @@ static void audio_detach_capture (HWVoiceOut *hw)
|
|||||||
|
|
||||||
QLIST_REMOVE (sw, entries);
|
QLIST_REMOVE (sw, entries);
|
||||||
QLIST_REMOVE (sc, entries);
|
QLIST_REMOVE (sc, entries);
|
||||||
g_free (sc);
|
qemu_free (sc);
|
||||||
if (was_active) {
|
if (was_active) {
|
||||||
/* We have removed soft voice from the capture:
|
/* We have removed soft voice from the capture:
|
||||||
this might have changed the overall status of the capture
|
this might have changed the overall status of the capture
|
||||||
@@ -818,19 +815,17 @@ static int audio_attach_capture (HWVoiceOut *hw)
|
|||||||
sw->active = hw->enabled;
|
sw->active = hw->enabled;
|
||||||
sw->conv = noop_conv;
|
sw->conv = noop_conv;
|
||||||
sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
|
sw->ratio = ((int64_t) hw_cap->info.freq << 32) / sw->info.freq;
|
||||||
sw->vol = nominal_volume;
|
|
||||||
sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
|
sw->rate = st_rate_start (sw->info.freq, hw_cap->info.freq);
|
||||||
if (!sw->rate) {
|
if (!sw->rate) {
|
||||||
dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
|
dolog ("Could not start rate conversion for `%s'\n", SW_NAME (sw));
|
||||||
g_free (sw);
|
qemu_free (sw);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
|
QLIST_INSERT_HEAD (&hw_cap->sw_head, sw, entries);
|
||||||
QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
|
QLIST_INSERT_HEAD (&hw->cap_head, sc, entries);
|
||||||
#ifdef DEBUG_CAPTURE
|
#ifdef DEBUG_CAPTURE
|
||||||
sw->name = g_strdup_printf ("for %p %d,%d,%d",
|
asprintf (&sw->name, "for %p %d,%d,%d",
|
||||||
hw, sw->info.freq, sw->info.bits,
|
hw, sw->info.freq, sw->info.bits, sw->info.nchannels);
|
||||||
sw->info.nchannels);
|
|
||||||
dolog ("Added %s active = %d\n", sw->name, sw->active);
|
dolog ("Added %s active = %d\n", sw->name, sw->active);
|
||||||
#endif
|
#endif
|
||||||
if (sw->active) {
|
if (sw->active) {
|
||||||
@@ -959,9 +954,7 @@ int audio_pcm_sw_read (SWVoiceIn *sw, void *buf, int size)
|
|||||||
total += isamp;
|
total += isamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(hw->ctl_caps & VOICE_VOLUME_CAP)) {
|
|
||||||
mixeng_volume (sw->buf, ret, &sw->vol);
|
mixeng_volume (sw->buf, ret, &sw->vol);
|
||||||
}
|
|
||||||
|
|
||||||
sw->clip (buf, sw->buf, ret);
|
sw->clip (buf, sw->buf, ret);
|
||||||
sw->total_hw_samples_acquired += total;
|
sw->total_hw_samples_acquired += total;
|
||||||
@@ -1045,11 +1038,8 @@ int audio_pcm_sw_write (SWVoiceOut *sw, void *buf, int size)
|
|||||||
swlim = audio_MIN (swlim, samples);
|
swlim = audio_MIN (swlim, samples);
|
||||||
if (swlim) {
|
if (swlim) {
|
||||||
sw->conv (sw->buf, buf, swlim);
|
sw->conv (sw->buf, buf, swlim);
|
||||||
|
|
||||||
if (!(sw->hw->ctl_caps & VOICE_VOLUME_CAP)) {
|
|
||||||
mixeng_volume (sw->buf, swlim, &sw->vol);
|
mixeng_volume (sw->buf, swlim, &sw->vol);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
while (swlim) {
|
while (swlim) {
|
||||||
dead = hwsamples - live;
|
dead = hwsamples - live;
|
||||||
@@ -1124,11 +1114,10 @@ static int audio_is_timer_needed (void)
|
|||||||
static void audio_reset_timer (AudioState *s)
|
static void audio_reset_timer (AudioState *s)
|
||||||
{
|
{
|
||||||
if (audio_is_timer_needed ()) {
|
if (audio_is_timer_needed ()) {
|
||||||
timer_mod (s->ts,
|
qemu_mod_timer (s->ts, qemu_get_clock_ns (vm_clock) + 1);
|
||||||
qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + conf.period.ticks);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
timer_del (s->ts);
|
qemu_del_timer (s->ts);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1676,7 +1665,7 @@ static void audio_pp_nb_voices (const char *typ, int nb)
|
|||||||
printf ("Theoretically supports many %s voices\n", typ);
|
printf ("Theoretically supports many %s voices\n", typ);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
printf ("Theoretically supports up to %d %s voices\n", nb, typ);
|
printf ("Theoretically supports upto %d %s voices\n", nb, typ);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1754,7 +1743,7 @@ static int audio_driver_init (AudioState *s, struct audio_driver *drv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void audio_vm_change_state_handler (void *opaque, int running,
|
static void audio_vm_change_state_handler (void *opaque, int running,
|
||||||
RunState state)
|
int reason)
|
||||||
{
|
{
|
||||||
AudioState *s = opaque;
|
AudioState *s = opaque;
|
||||||
HWVoiceOut *hwo = NULL;
|
HWVoiceOut *hwo = NULL;
|
||||||
@@ -1778,12 +1767,10 @@ static void audio_atexit (void)
|
|||||||
HWVoiceOut *hwo = NULL;
|
HWVoiceOut *hwo = NULL;
|
||||||
HWVoiceIn *hwi = NULL;
|
HWVoiceIn *hwi = NULL;
|
||||||
|
|
||||||
while ((hwo = audio_pcm_hw_find_any_out (hwo))) {
|
while ((hwo = audio_pcm_hw_find_any_enabled_out (hwo))) {
|
||||||
SWVoiceCap *sc;
|
SWVoiceCap *sc;
|
||||||
|
|
||||||
if (hwo->enabled) {
|
|
||||||
hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
|
hwo->pcm_ops->ctl_out (hwo, VOICE_DISABLE);
|
||||||
}
|
|
||||||
hwo->pcm_ops->fini_out (hwo);
|
hwo->pcm_ops->fini_out (hwo);
|
||||||
|
|
||||||
for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
for (sc = hwo->cap_head.lh_first; sc; sc = sc->entries.le_next) {
|
||||||
@@ -1796,10 +1783,8 @@ static void audio_atexit (void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((hwi = audio_pcm_hw_find_any_in (hwi))) {
|
while ((hwi = audio_pcm_hw_find_any_enabled_in (hwi))) {
|
||||||
if (hwi->enabled) {
|
|
||||||
hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
|
hwi->pcm_ops->ctl_in (hwi, VOICE_DISABLE);
|
||||||
}
|
|
||||||
hwi->pcm_ops->fini_in (hwi);
|
hwi->pcm_ops->fini_in (hwi);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1812,7 +1797,8 @@ static const VMStateDescription vmstate_audio = {
|
|||||||
.name = "audio",
|
.name = "audio",
|
||||||
.version_id = 1,
|
.version_id = 1,
|
||||||
.minimum_version_id = 1,
|
.minimum_version_id = 1,
|
||||||
.fields = (VMStateField[]) {
|
.minimum_version_id_old = 1,
|
||||||
|
.fields = (VMStateField []) {
|
||||||
VMSTATE_END_OF_LIST()
|
VMSTATE_END_OF_LIST()
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -1834,7 +1820,7 @@ static void audio_init (void)
|
|||||||
QLIST_INIT (&s->cap_head);
|
QLIST_INIT (&s->cap_head);
|
||||||
atexit (audio_atexit);
|
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) {
|
if (!s->ts) {
|
||||||
hw_error("Could not create audio timer\n");
|
hw_error("Could not create audio timer\n");
|
||||||
}
|
}
|
||||||
@@ -1921,7 +1907,7 @@ static void audio_init (void)
|
|||||||
void AUD_register_card (const char *name, QEMUSoundCard *card)
|
void AUD_register_card (const char *name, QEMUSoundCard *card)
|
||||||
{
|
{
|
||||||
audio_init ();
|
audio_init ();
|
||||||
card->name = g_strdup (name);
|
card->name = qemu_strdup (name);
|
||||||
memset (&card->entries, 0, sizeof (card->entries));
|
memset (&card->entries, 0, sizeof (card->entries));
|
||||||
QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
|
QLIST_INSERT_HEAD (&glob_audio_state.card_head, card, entries);
|
||||||
}
|
}
|
||||||
@@ -1929,7 +1915,7 @@ void AUD_register_card (const char *name, QEMUSoundCard *card)
|
|||||||
void AUD_remove_card (QEMUSoundCard *card)
|
void AUD_remove_card (QEMUSoundCard *card)
|
||||||
{
|
{
|
||||||
QLIST_REMOVE (card, entries);
|
QLIST_REMOVE (card, entries);
|
||||||
g_free (card->name);
|
qemu_free (card->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -2014,11 +2000,11 @@ CaptureVoiceOut *AUD_add_capture (
|
|||||||
return cap;
|
return cap;
|
||||||
|
|
||||||
err3:
|
err3:
|
||||||
g_free (cap->hw.mix_buf);
|
qemu_free (cap->hw.mix_buf);
|
||||||
err2:
|
err2:
|
||||||
g_free (cap);
|
qemu_free (cap);
|
||||||
err1:
|
err1:
|
||||||
g_free (cb);
|
qemu_free (cb);
|
||||||
err0:
|
err0:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -2032,7 +2018,7 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
|
|||||||
if (cb->opaque == cb_opaque) {
|
if (cb->opaque == cb_opaque) {
|
||||||
cb->ops.destroy (cb_opaque);
|
cb->ops.destroy (cb_opaque);
|
||||||
QLIST_REMOVE (cb, entries);
|
QLIST_REMOVE (cb, entries);
|
||||||
g_free (cb);
|
qemu_free (cb);
|
||||||
|
|
||||||
if (!cap->cb_head.lh_first) {
|
if (!cap->cb_head.lh_first) {
|
||||||
SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
|
SWVoiceOut *sw = cap->hw.sw_head.lh_first, *sw1;
|
||||||
@@ -2050,11 +2036,11 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
|
|||||||
}
|
}
|
||||||
QLIST_REMOVE (sw, entries);
|
QLIST_REMOVE (sw, entries);
|
||||||
QLIST_REMOVE (sc, entries);
|
QLIST_REMOVE (sc, entries);
|
||||||
g_free (sc);
|
qemu_free (sc);
|
||||||
sw = sw1;
|
sw = sw1;
|
||||||
}
|
}
|
||||||
QLIST_REMOVE (cap, entries);
|
QLIST_REMOVE (cap, entries);
|
||||||
g_free (cap);
|
qemu_free (cap);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -2064,29 +2050,17 @@ void AUD_del_capture (CaptureVoiceOut *cap, void *cb_opaque)
|
|||||||
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
|
void AUD_set_volume_out (SWVoiceOut *sw, int mute, uint8_t lvol, uint8_t rvol)
|
||||||
{
|
{
|
||||||
if (sw) {
|
if (sw) {
|
||||||
HWVoiceOut *hw = sw->hw;
|
|
||||||
|
|
||||||
sw->vol.mute = mute;
|
sw->vol.mute = mute;
|
||||||
sw->vol.l = nominal_volume.l * lvol / 255;
|
sw->vol.l = nominal_volume.l * lvol / 255;
|
||||||
sw->vol.r = nominal_volume.r * rvol / 255;
|
sw->vol.r = nominal_volume.r * rvol / 255;
|
||||||
|
|
||||||
if (hw->pcm_ops->ctl_out) {
|
|
||||||
hw->pcm_ops->ctl_out (hw, VOICE_VOLUME, sw);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
|
void AUD_set_volume_in (SWVoiceIn *sw, int mute, uint8_t lvol, uint8_t rvol)
|
||||||
{
|
{
|
||||||
if (sw) {
|
if (sw) {
|
||||||
HWVoiceIn *hw = sw->hw;
|
|
||||||
|
|
||||||
sw->vol.mute = mute;
|
sw->vol.mute = mute;
|
||||||
sw->vol.l = nominal_volume.l * lvol / 255;
|
sw->vol.l = nominal_volume.l * lvol / 255;
|
||||||
sw->vol.r = nominal_volume.r * rvol / 255;
|
sw->vol.r = nominal_volume.r * rvol / 255;
|
||||||
|
|
||||||
if (hw->pcm_ops->ctl_in) {
|
|
||||||
hw->pcm_ops->ctl_in (hw, VOICE_VOLUME, sw);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
#define QEMU_AUDIO_H
|
#define QEMU_AUDIO_H
|
||||||
|
|
||||||
#include "config-host.h"
|
#include "config-host.h"
|
||||||
#include "qemu/queue.h"
|
#include "qemu-queue.h"
|
||||||
|
|
||||||
typedef void (*audio_callback_fn) (void *opaque, int avail);
|
typedef void (*audio_callback_fn) (void *opaque, int avail);
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,6 @@ typedef struct HWVoiceOut {
|
|||||||
int samples;
|
int samples;
|
||||||
QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
|
QLIST_HEAD (sw_out_listhead, SWVoiceOut) sw_head;
|
||||||
QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
|
QLIST_HEAD (sw_cap_listhead, SWVoiceCap) cap_head;
|
||||||
int ctl_caps;
|
|
||||||
struct audio_pcm_ops *pcm_ops;
|
struct audio_pcm_ops *pcm_ops;
|
||||||
QLIST_ENTRY (HWVoiceOut) entries;
|
QLIST_ENTRY (HWVoiceOut) entries;
|
||||||
} HWVoiceOut;
|
} HWVoiceOut;
|
||||||
@@ -102,7 +101,6 @@ typedef struct HWVoiceIn {
|
|||||||
|
|
||||||
int samples;
|
int samples;
|
||||||
QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
|
QLIST_HEAD (sw_in_listhead, SWVoiceIn) sw_head;
|
||||||
int ctl_caps;
|
|
||||||
struct audio_pcm_ops *pcm_ops;
|
struct audio_pcm_ops *pcm_ops;
|
||||||
QLIST_ENTRY (HWVoiceIn) entries;
|
QLIST_ENTRY (HWVoiceIn) entries;
|
||||||
} HWVoiceIn;
|
} HWVoiceIn;
|
||||||
@@ -152,7 +150,6 @@ struct audio_driver {
|
|||||||
int max_voices_in;
|
int max_voices_in;
|
||||||
int voice_size_out;
|
int voice_size_out;
|
||||||
int voice_size_in;
|
int voice_size_in;
|
||||||
int ctl_caps;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct audio_pcm_ops {
|
struct audio_pcm_ops {
|
||||||
@@ -234,22 +231,44 @@ void audio_run (const char *msg);
|
|||||||
|
|
||||||
#define VOICE_ENABLE 1
|
#define VOICE_ENABLE 1
|
||||||
#define VOICE_DISABLE 2
|
#define VOICE_DISABLE 2
|
||||||
#define VOICE_VOLUME 3
|
|
||||||
|
|
||||||
#define VOICE_VOLUME_CAP (1 << VOICE_VOLUME)
|
|
||||||
|
|
||||||
static inline int audio_ring_dist (int dst, int src, int len)
|
static inline int audio_ring_dist (int dst, int src, int len)
|
||||||
{
|
{
|
||||||
return (dst >= src) ? (dst - src) : (len - src + dst);
|
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
|
#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
|
#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
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#undef GCC_ATTR
|
||||||
|
|
||||||
#define AUDIO_STRINGIFY_(n) #n
|
#define AUDIO_STRINGIFY_(n) #n
|
||||||
#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
|
#define AUDIO_STRINGIFY(n) AUDIO_STRINGIFY_(n)
|
||||||
|
|||||||
@@ -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)
|
static void glue (audio_pcm_hw_free_resources_, TYPE) (HW *hw)
|
||||||
{
|
{
|
||||||
g_free (HWBUF);
|
if (HWBUF) {
|
||||||
|
qemu_free (HWBUF);
|
||||||
|
}
|
||||||
|
|
||||||
HWBUF = NULL;
|
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)
|
static void glue (audio_pcm_sw_free_resources_, TYPE) (SW *sw)
|
||||||
{
|
{
|
||||||
g_free (sw->buf);
|
if (sw->buf) {
|
||||||
|
qemu_free (sw->buf);
|
||||||
|
}
|
||||||
|
|
||||||
if (sw->rate) {
|
if (sw->rate) {
|
||||||
st_rate_stop (sw->rate);
|
st_rate_stop (sw->rate);
|
||||||
@@ -118,7 +123,7 @@ static int glue (audio_pcm_sw_alloc_resources_, TYPE) (SW *sw)
|
|||||||
sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
|
sw->rate = st_rate_start (sw->hw->info.freq, sw->info.freq);
|
||||||
#endif
|
#endif
|
||||||
if (!sw->rate) {
|
if (!sw->rate) {
|
||||||
g_free (sw->buf);
|
qemu_free (sw->buf);
|
||||||
sw->buf = NULL;
|
sw->buf = NULL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -155,10 +160,10 @@ static int glue (audio_pcm_sw_init_, TYPE) (
|
|||||||
[sw->info.swap_endianness]
|
[sw->info.swap_endianness]
|
||||||
[audio_bits_to_index (sw->info.bits)];
|
[audio_bits_to_index (sw->info.bits)];
|
||||||
|
|
||||||
sw->name = g_strdup (name);
|
sw->name = qemu_strdup (name);
|
||||||
err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
|
err = glue (audio_pcm_sw_alloc_resources_, TYPE) (sw);
|
||||||
if (err) {
|
if (err) {
|
||||||
g_free (sw->name);
|
qemu_free (sw->name);
|
||||||
sw->name = NULL;
|
sw->name = NULL;
|
||||||
}
|
}
|
||||||
return err;
|
return err;
|
||||||
@@ -167,8 +172,10 @@ static int glue (audio_pcm_sw_init_, TYPE) (
|
|||||||
static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
|
static void glue (audio_pcm_sw_fini_, TYPE) (SW *sw)
|
||||||
{
|
{
|
||||||
glue (audio_pcm_sw_free_resources_, TYPE) (sw);
|
glue (audio_pcm_sw_free_resources_, TYPE) (sw);
|
||||||
g_free (sw->name);
|
if (sw->name) {
|
||||||
|
qemu_free (sw->name);
|
||||||
sw->name = NULL;
|
sw->name = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
|
static void glue (audio_pcm_hw_add_sw_, TYPE) (HW *hw, SW *sw)
|
||||||
@@ -191,10 +198,10 @@ static void glue (audio_pcm_hw_gc_, TYPE) (HW **hwp)
|
|||||||
audio_detach_capture (hw);
|
audio_detach_capture (hw);
|
||||||
#endif
|
#endif
|
||||||
QLIST_REMOVE (hw, entries);
|
QLIST_REMOVE (hw, entries);
|
||||||
glue (hw->pcm_ops->fini_, TYPE) (hw);
|
|
||||||
glue (s->nb_hw_voices_, TYPE) += 1;
|
glue (s->nb_hw_voices_, TYPE) += 1;
|
||||||
glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
|
glue (audio_pcm_hw_free_resources_ ,TYPE) (hw);
|
||||||
g_free (hw);
|
glue (hw->pcm_ops->fini_, TYPE) (hw);
|
||||||
|
qemu_free (hw);
|
||||||
*hwp = NULL;
|
*hwp = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -256,8 +263,6 @@ static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
|
|||||||
}
|
}
|
||||||
|
|
||||||
hw->pcm_ops = drv->pcm_ops;
|
hw->pcm_ops = drv->pcm_ops;
|
||||||
hw->ctl_caps = drv->ctl_caps;
|
|
||||||
|
|
||||||
QLIST_INIT (&hw->sw_head);
|
QLIST_INIT (&hw->sw_head);
|
||||||
#ifdef DAC
|
#ifdef DAC
|
||||||
QLIST_INIT (&hw->cap_head);
|
QLIST_INIT (&hw->cap_head);
|
||||||
@@ -295,7 +300,7 @@ static HW *glue (audio_pcm_hw_add_new_, TYPE) (struct audsettings *as)
|
|||||||
err1:
|
err1:
|
||||||
glue (hw->pcm_ops->fini_, TYPE) (hw);
|
glue (hw->pcm_ops->fini_, TYPE) (hw);
|
||||||
err0:
|
err0:
|
||||||
g_free (hw);
|
qemu_free (hw);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -363,7 +368,7 @@ err3:
|
|||||||
glue (audio_pcm_hw_del_sw_, TYPE) (sw);
|
glue (audio_pcm_hw_del_sw_, TYPE) (sw);
|
||||||
glue (audio_pcm_hw_gc_, TYPE) (&hw);
|
glue (audio_pcm_hw_gc_, TYPE) (&hw);
|
||||||
err2:
|
err2:
|
||||||
g_free (sw);
|
qemu_free (sw);
|
||||||
err1:
|
err1:
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -373,7 +378,7 @@ static void glue (audio_close_, TYPE) (SW *sw)
|
|||||||
glue (audio_pcm_sw_fini_, TYPE) (sw);
|
glue (audio_pcm_sw_fini_, TYPE) (sw);
|
||||||
glue (audio_pcm_hw_del_sw_, TYPE) (sw);
|
glue (audio_pcm_hw_del_sw_, TYPE) (sw);
|
||||||
glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
|
glue (audio_pcm_hw_gc_, TYPE) (&sw->hw);
|
||||||
g_free (sw);
|
qemu_free (sw);
|
||||||
}
|
}
|
||||||
|
|
||||||
void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
|
void glue (AUD_close_, TYPE) (QEMUSoundCard *card, SW *sw)
|
||||||
@@ -403,15 +408,15 @@ SW *glue (AUD_open_, TYPE) (
|
|||||||
SW *old_sw = NULL;
|
SW *old_sw = NULL;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
|
||||||
|
name, as->freq, as->nchannels, as->fmt);
|
||||||
|
|
||||||
if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
|
if (audio_bug (AUDIO_FUNC, !card || !name || !callback_fn || !as)) {
|
||||||
dolog ("card=%p name=%p callback_fn=%p as=%p\n",
|
dolog ("card=%p name=%p callback_fn=%p as=%p\n",
|
||||||
card, name, callback_fn, as);
|
card, name, callback_fn, as);
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
ldebug ("open %s, freq %d, nchannels %d, fmt %d\n",
|
|
||||||
name, as->freq, as->nchannels, as->fmt);
|
|
||||||
|
|
||||||
if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
|
if (audio_bug (AUDIO_FUNC, audio_validate_settings (as))) {
|
||||||
audio_print_settings (as);
|
audio_print_settings (as);
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/* public domain */
|
/* public domain */
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
|
#include "audio.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "win-int"
|
#define AUDIO_CAP "win-int"
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|||||||
@@ -201,7 +201,7 @@ static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
case AUD_FMT_S32:
|
case AUD_FMT_S32:
|
||||||
case AUD_FMT_U32:
|
case AUD_FMT_U32:
|
||||||
dolog ("Will use 16 instead of 32 bit samples\n");
|
dolog ("Will use 16 instead of 32 bit samples\n");
|
||||||
/* fall through */
|
|
||||||
case AUD_FMT_S16:
|
case AUD_FMT_S16:
|
||||||
case AUD_FMT_U16:
|
case AUD_FMT_U16:
|
||||||
deffmt:
|
deffmt:
|
||||||
@@ -246,7 +246,7 @@ static int qesd_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
esd->fd = -1;
|
esd->fd = -1;
|
||||||
|
|
||||||
fail1:
|
fail1:
|
||||||
g_free (esd->pcm_buf);
|
qemu_free (esd->pcm_buf);
|
||||||
esd->pcm_buf = NULL;
|
esd->pcm_buf = NULL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -270,7 +270,7 @@ static void qesd_fini_out (HWVoiceOut *hw)
|
|||||||
|
|
||||||
audio_pt_fini (&esd->pt, AUDIO_FUNC);
|
audio_pt_fini (&esd->pt, AUDIO_FUNC);
|
||||||
|
|
||||||
g_free (esd->pcm_buf);
|
qemu_free (esd->pcm_buf);
|
||||||
esd->pcm_buf = NULL;
|
esd->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -453,7 +453,7 @@ static int qesd_init_in (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
esd->fd = -1;
|
esd->fd = -1;
|
||||||
|
|
||||||
fail1:
|
fail1:
|
||||||
g_free (esd->pcm_buf);
|
qemu_free (esd->pcm_buf);
|
||||||
esd->pcm_buf = NULL;
|
esd->pcm_buf = NULL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -477,7 +477,7 @@ static void qesd_fini_in (HWVoiceIn *hw)
|
|||||||
|
|
||||||
audio_pt_fini (&esd->pt, AUDIO_FUNC);
|
audio_pt_fini (&esd->pt, AUDIO_FUNC);
|
||||||
|
|
||||||
g_free (esd->pcm_buf);
|
qemu_free (esd->pcm_buf);
|
||||||
esd->pcm_buf = NULL;
|
esd->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -343,7 +343,7 @@ static void fmod_fini_out (HWVoiceOut *hw)
|
|||||||
|
|
||||||
static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
|
static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
{
|
{
|
||||||
int mode, channel;
|
int bits16, mode, channel;
|
||||||
FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
|
FMODVoiceOut *fmd = (FMODVoiceOut *) hw;
|
||||||
struct audsettings obt_as = *as;
|
struct audsettings obt_as = *as;
|
||||||
|
|
||||||
@@ -374,6 +374,7 @@ static int fmod_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
/* FMOD always operates on little endian frames? */
|
/* FMOD always operates on little endian frames? */
|
||||||
obt_as.endianness = 0;
|
obt_as.endianness = 0;
|
||||||
audio_pcm_init_info (&hw->info, &obt_as);
|
audio_pcm_init_info (&hw->info, &obt_as);
|
||||||
|
bits16 = (mode & FSOUND_16BITS) != 0;
|
||||||
hw->samples = conf.nb_samples;
|
hw->samples = conf.nb_samples;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -404,7 +405,7 @@ static int fmod_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
|
|
||||||
static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
|
static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
|
||||||
{
|
{
|
||||||
int mode;
|
int bits16, mode;
|
||||||
FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
|
FMODVoiceIn *fmd = (FMODVoiceIn *) hw;
|
||||||
struct audsettings obt_as = *as;
|
struct audsettings obt_as = *as;
|
||||||
|
|
||||||
@@ -431,6 +432,7 @@ static int fmod_init_in (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
/* FMOD always operates on little endian frames? */
|
/* FMOD always operates on little endian frames? */
|
||||||
obt_as.endianness = 0;
|
obt_as.endianness = 0;
|
||||||
audio_pcm_init_info (&hw->info, &obt_as);
|
audio_pcm_init_info (&hw->info, &obt_as);
|
||||||
|
bits16 = (mode & FSOUND_16BITS) != 0;
|
||||||
hw->samples = conf.nb_samples;
|
hw->samples = conf.nb_samples;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,7 @@
|
|||||||
#define ENDIAN_CONVERT(v) (v)
|
#define ENDIAN_CONVERT(v) (v)
|
||||||
|
|
||||||
/* Signed 8 bit */
|
/* Signed 8 bit */
|
||||||
#define BSIZE 8
|
#define IN_T int8_t
|
||||||
#define ITYPE int
|
|
||||||
#define IN_MIN SCHAR_MIN
|
#define IN_MIN SCHAR_MIN
|
||||||
#define IN_MAX SCHAR_MAX
|
#define IN_MAX SCHAR_MAX
|
||||||
#define SIGNED
|
#define SIGNED
|
||||||
@@ -43,29 +42,25 @@
|
|||||||
#undef SIGNED
|
#undef SIGNED
|
||||||
#undef IN_MAX
|
#undef IN_MAX
|
||||||
#undef IN_MIN
|
#undef IN_MIN
|
||||||
#undef BSIZE
|
#undef IN_T
|
||||||
#undef ITYPE
|
|
||||||
#undef SHIFT
|
#undef SHIFT
|
||||||
|
|
||||||
/* Unsigned 8 bit */
|
/* Unsigned 8 bit */
|
||||||
#define BSIZE 8
|
#define IN_T uint8_t
|
||||||
#define ITYPE uint
|
|
||||||
#define IN_MIN 0
|
#define IN_MIN 0
|
||||||
#define IN_MAX UCHAR_MAX
|
#define IN_MAX UCHAR_MAX
|
||||||
#define SHIFT 8
|
#define SHIFT 8
|
||||||
#include "mixeng_template.h"
|
#include "mixeng_template.h"
|
||||||
#undef IN_MAX
|
#undef IN_MAX
|
||||||
#undef IN_MIN
|
#undef IN_MIN
|
||||||
#undef BSIZE
|
#undef IN_T
|
||||||
#undef ITYPE
|
|
||||||
#undef SHIFT
|
#undef SHIFT
|
||||||
|
|
||||||
#undef ENDIAN_CONVERT
|
#undef ENDIAN_CONVERT
|
||||||
#undef ENDIAN_CONVERSION
|
#undef ENDIAN_CONVERSION
|
||||||
|
|
||||||
/* Signed 16 bit */
|
/* Signed 16 bit */
|
||||||
#define BSIZE 16
|
#define IN_T int16_t
|
||||||
#define ITYPE int
|
|
||||||
#define IN_MIN SHRT_MIN
|
#define IN_MIN SHRT_MIN
|
||||||
#define IN_MAX SHRT_MAX
|
#define IN_MAX SHRT_MAX
|
||||||
#define SIGNED
|
#define SIGNED
|
||||||
@@ -83,13 +78,11 @@
|
|||||||
#undef SIGNED
|
#undef SIGNED
|
||||||
#undef IN_MAX
|
#undef IN_MAX
|
||||||
#undef IN_MIN
|
#undef IN_MIN
|
||||||
#undef BSIZE
|
#undef IN_T
|
||||||
#undef ITYPE
|
|
||||||
#undef SHIFT
|
#undef SHIFT
|
||||||
|
|
||||||
/* Unsigned 16 bit */
|
/* Unsigned 16 bit */
|
||||||
#define BSIZE 16
|
#define IN_T uint16_t
|
||||||
#define ITYPE uint
|
|
||||||
#define IN_MIN 0
|
#define IN_MIN 0
|
||||||
#define IN_MAX USHRT_MAX
|
#define IN_MAX USHRT_MAX
|
||||||
#define SHIFT 16
|
#define SHIFT 16
|
||||||
@@ -105,13 +98,11 @@
|
|||||||
#undef ENDIAN_CONVERSION
|
#undef ENDIAN_CONVERSION
|
||||||
#undef IN_MAX
|
#undef IN_MAX
|
||||||
#undef IN_MIN
|
#undef IN_MIN
|
||||||
#undef BSIZE
|
#undef IN_T
|
||||||
#undef ITYPE
|
|
||||||
#undef SHIFT
|
#undef SHIFT
|
||||||
|
|
||||||
/* Signed 32 bit */
|
/* Signed 32 bit */
|
||||||
#define BSIZE 32
|
#define IN_T int32_t
|
||||||
#define ITYPE int
|
|
||||||
#define IN_MIN INT32_MIN
|
#define IN_MIN INT32_MIN
|
||||||
#define IN_MAX INT32_MAX
|
#define IN_MAX INT32_MAX
|
||||||
#define SIGNED
|
#define SIGNED
|
||||||
@@ -129,13 +120,11 @@
|
|||||||
#undef SIGNED
|
#undef SIGNED
|
||||||
#undef IN_MAX
|
#undef IN_MAX
|
||||||
#undef IN_MIN
|
#undef IN_MIN
|
||||||
#undef BSIZE
|
#undef IN_T
|
||||||
#undef ITYPE
|
|
||||||
#undef SHIFT
|
#undef SHIFT
|
||||||
|
|
||||||
/* Unsigned 32 bit */
|
/* Unsigned 32 bit */
|
||||||
#define BSIZE 32
|
#define IN_T uint32_t
|
||||||
#define ITYPE uint
|
|
||||||
#define IN_MIN 0
|
#define IN_MIN 0
|
||||||
#define IN_MAX UINT32_MAX
|
#define IN_MAX UINT32_MAX
|
||||||
#define SHIFT 32
|
#define SHIFT 32
|
||||||
@@ -151,8 +140,7 @@
|
|||||||
#undef ENDIAN_CONVERSION
|
#undef ENDIAN_CONVERSION
|
||||||
#undef IN_MAX
|
#undef IN_MAX
|
||||||
#undef IN_MIN
|
#undef IN_MIN
|
||||||
#undef BSIZE
|
#undef IN_T
|
||||||
#undef ITYPE
|
|
||||||
#undef SHIFT
|
#undef SHIFT
|
||||||
|
|
||||||
t_sample *mixeng_conv[2][2][2][3] = {
|
t_sample *mixeng_conv[2][2][2][3] = {
|
||||||
@@ -338,7 +326,7 @@ void *st_rate_start (int inrate, int outrate)
|
|||||||
|
|
||||||
void st_rate_stop (void *opaque)
|
void st_rate_stop (void *opaque)
|
||||||
{
|
{
|
||||||
g_free (opaque);
|
qemu_free (opaque);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mixeng_clear (struct st_sample *buf, int len)
|
void mixeng_clear (struct st_sample *buf, int len)
|
||||||
@@ -348,6 +336,7 @@ void mixeng_clear (struct st_sample *buf, int len)
|
|||||||
|
|
||||||
void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
|
void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
|
||||||
{
|
{
|
||||||
|
#ifdef CONFIG_MIXEMU
|
||||||
if (vol->mute) {
|
if (vol->mute) {
|
||||||
mixeng_clear (buf, len);
|
mixeng_clear (buf, len);
|
||||||
return;
|
return;
|
||||||
@@ -363,4 +352,9 @@ void mixeng_volume (struct st_sample *buf, int len, struct mixeng_volume *vol)
|
|||||||
#endif
|
#endif
|
||||||
buf += 1;
|
buf += 1;
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
(void) buf;
|
||||||
|
(void) len;
|
||||||
|
(void) vol;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,11 +31,10 @@
|
|||||||
#define HALF (IN_MAX >> 1)
|
#define HALF (IN_MAX >> 1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define ET glue (ENDIAN_CONVERSION, glue (glue (glue (_, ITYPE), BSIZE), _t))
|
#define ET glue (ENDIAN_CONVERSION, glue (_, IN_T))
|
||||||
#define IN_T glue (glue (ITYPE, BSIZE), _t)
|
|
||||||
|
|
||||||
#ifdef FLOAT_MIXENG
|
#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);
|
IN_T nv = ENDIAN_CONVERT (v);
|
||||||
|
|
||||||
@@ -54,7 +53,7 @@ static inline mixeng_real glue (conv_, ET) (IN_T v)
|
|||||||
#endif
|
#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) {
|
if (v >= 0.5) {
|
||||||
return IN_MAX;
|
return IN_MAX;
|
||||||
@@ -151,4 +150,3 @@ static void glue (glue (clip_, ET), _from_mono)
|
|||||||
|
|
||||||
#undef ET
|
#undef ET
|
||||||
#undef HALF
|
#undef HALF
|
||||||
#undef IN_T
|
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
*/
|
*/
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "qemu/timer.h"
|
#include "qemu-timer.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "noaudio"
|
#define AUDIO_CAP "noaudio"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
@@ -46,7 +46,7 @@ static int no_run_out (HWVoiceOut *hw, int live)
|
|||||||
int64_t ticks;
|
int64_t ticks;
|
||||||
int64_t bytes;
|
int64_t bytes;
|
||||||
|
|
||||||
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
now = qemu_get_clock_ns (vm_clock);
|
||||||
ticks = now - no->old_ticks;
|
ticks = now - no->old_ticks;
|
||||||
bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
bytes = muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
||||||
bytes = audio_MIN (bytes, INT_MAX);
|
bytes = audio_MIN (bytes, INT_MAX);
|
||||||
@@ -102,7 +102,7 @@ static int no_run_in (HWVoiceIn *hw)
|
|||||||
int samples = 0;
|
int samples = 0;
|
||||||
|
|
||||||
if (dead) {
|
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 ticks = now - no->old_ticks;
|
||||||
int64_t bytes =
|
int64_t bytes =
|
||||||
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
||||||
|
|||||||
@@ -25,10 +25,14 @@
|
|||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
#include <soundcard.h>
|
||||||
|
#else
|
||||||
#include <sys/soundcard.h>
|
#include <sys/soundcard.h>
|
||||||
|
#endif
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/main-loop.h"
|
#include "host-utils.h"
|
||||||
#include "qemu/host-utils.h"
|
#include "qemu-char.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "oss"
|
#define AUDIO_CAP "oss"
|
||||||
@@ -504,7 +508,7 @@ static void oss_fini_out (HWVoiceOut *hw)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
g_free (oss->pcm_buf);
|
qemu_free (oss->pcm_buf);
|
||||||
}
|
}
|
||||||
oss->pcm_buf = NULL;
|
oss->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
@@ -736,8 +740,10 @@ static void oss_fini_in (HWVoiceIn *hw)
|
|||||||
|
|
||||||
oss_anal_close (&oss->fd);
|
oss_anal_close (&oss->fd);
|
||||||
|
|
||||||
g_free(oss->pcm_buf);
|
if (oss->pcm_buf) {
|
||||||
|
qemu_free (oss->pcm_buf);
|
||||||
oss->pcm_buf = NULL;
|
oss->pcm_buf = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int oss_run_in (HWVoiceIn *hw)
|
static int oss_run_in (HWVoiceIn *hw)
|
||||||
@@ -847,10 +853,6 @@ static int oss_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
|||||||
|
|
||||||
static void *oss_audio_init (void)
|
static void *oss_audio_init (void)
|
||||||
{
|
{
|
||||||
if (access(conf.devpath_in, R_OK | W_OK) < 0 ||
|
|
||||||
access(conf.devpath_out, R_OK | W_OK) < 0) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return &conf;
|
return &conf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
515
audio/paaudio.c
515
audio/paaudio.c
@@ -2,7 +2,8 @@
|
|||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
#include <pulse/pulseaudio.h>
|
#include <pulse/simple.h>
|
||||||
|
#include <pulse/error.h>
|
||||||
|
|
||||||
#define AUDIO_CAP "pulseaudio"
|
#define AUDIO_CAP "pulseaudio"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
@@ -14,7 +15,7 @@ typedef struct {
|
|||||||
int live;
|
int live;
|
||||||
int decr;
|
int decr;
|
||||||
int rpos;
|
int rpos;
|
||||||
pa_stream *stream;
|
pa_simple *s;
|
||||||
void *pcm_buf;
|
void *pcm_buf;
|
||||||
struct audio_pt pt;
|
struct audio_pt pt;
|
||||||
} PAVoiceOut;
|
} PAVoiceOut;
|
||||||
@@ -25,23 +26,17 @@ typedef struct {
|
|||||||
int dead;
|
int dead;
|
||||||
int incr;
|
int incr;
|
||||||
int wpos;
|
int wpos;
|
||||||
pa_stream *stream;
|
pa_simple *s;
|
||||||
void *pcm_buf;
|
void *pcm_buf;
|
||||||
struct audio_pt pt;
|
struct audio_pt pt;
|
||||||
const void *read_data;
|
|
||||||
size_t read_index, read_length;
|
|
||||||
} PAVoiceIn;
|
} PAVoiceIn;
|
||||||
|
|
||||||
typedef struct {
|
static struct {
|
||||||
int samples;
|
int samples;
|
||||||
char *server;
|
char *server;
|
||||||
char *sink;
|
char *sink;
|
||||||
char *source;
|
char *source;
|
||||||
pa_threaded_mainloop *mainloop;
|
} conf = {
|
||||||
pa_context *context;
|
|
||||||
} paaudio;
|
|
||||||
|
|
||||||
static paaudio glob_paaudio = {
|
|
||||||
.samples = 4096,
|
.samples = 4096,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -56,146 +51,6 @@ static void GCC_FMT_ATTR (2, 3) qpa_logerr (int err, const char *fmt, ...)
|
|||||||
AUD_log (AUDIO_CAP, "Reason: %s\n", pa_strerror (err));
|
AUD_log (AUDIO_CAP, "Reason: %s\n", pa_strerror (err));
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef PA_CONTEXT_IS_GOOD
|
|
||||||
static inline int PA_CONTEXT_IS_GOOD(pa_context_state_t x)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
x == PA_CONTEXT_CONNECTING ||
|
|
||||||
x == PA_CONTEXT_AUTHORIZING ||
|
|
||||||
x == PA_CONTEXT_SETTING_NAME ||
|
|
||||||
x == PA_CONTEXT_READY;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef PA_STREAM_IS_GOOD
|
|
||||||
static inline int PA_STREAM_IS_GOOD(pa_stream_state_t x)
|
|
||||||
{
|
|
||||||
return
|
|
||||||
x == PA_STREAM_CREATING ||
|
|
||||||
x == PA_STREAM_READY;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define CHECK_SUCCESS_GOTO(c, rerror, expression, label) \
|
|
||||||
do { \
|
|
||||||
if (!(expression)) { \
|
|
||||||
if (rerror) { \
|
|
||||||
*(rerror) = pa_context_errno ((c)->context); \
|
|
||||||
} \
|
|
||||||
goto label; \
|
|
||||||
} \
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
#define CHECK_DEAD_GOTO(c, stream, rerror, label) \
|
|
||||||
do { \
|
|
||||||
if (!(c)->context || !PA_CONTEXT_IS_GOOD (pa_context_get_state((c)->context)) || \
|
|
||||||
!(stream) || !PA_STREAM_IS_GOOD (pa_stream_get_state ((stream)))) { \
|
|
||||||
if (((c)->context && pa_context_get_state ((c)->context) == PA_CONTEXT_FAILED) || \
|
|
||||||
((stream) && pa_stream_get_state ((stream)) == PA_STREAM_FAILED)) { \
|
|
||||||
if (rerror) { \
|
|
||||||
*(rerror) = pa_context_errno ((c)->context); \
|
|
||||||
} \
|
|
||||||
} else { \
|
|
||||||
if (rerror) { \
|
|
||||||
*(rerror) = PA_ERR_BADSTATE; \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
goto label; \
|
|
||||||
} \
|
|
||||||
} while (0);
|
|
||||||
|
|
||||||
static int qpa_simple_read (PAVoiceIn *p, void *data, size_t length, int *rerror)
|
|
||||||
{
|
|
||||||
paaudio *g = &glob_paaudio;
|
|
||||||
|
|
||||||
pa_threaded_mainloop_lock (g->mainloop);
|
|
||||||
|
|
||||||
CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
|
|
||||||
|
|
||||||
while (length > 0) {
|
|
||||||
size_t l;
|
|
||||||
|
|
||||||
while (!p->read_data) {
|
|
||||||
int r;
|
|
||||||
|
|
||||||
r = pa_stream_peek (p->stream, &p->read_data, &p->read_length);
|
|
||||||
CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail);
|
|
||||||
|
|
||||||
if (!p->read_data) {
|
|
||||||
pa_threaded_mainloop_wait (g->mainloop);
|
|
||||||
CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
|
|
||||||
} else {
|
|
||||||
p->read_index = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
l = p->read_length < length ? p->read_length : length;
|
|
||||||
memcpy (data, (const uint8_t *) p->read_data+p->read_index, l);
|
|
||||||
|
|
||||||
data = (uint8_t *) data + l;
|
|
||||||
length -= l;
|
|
||||||
|
|
||||||
p->read_index += l;
|
|
||||||
p->read_length -= l;
|
|
||||||
|
|
||||||
if (!p->read_length) {
|
|
||||||
int r;
|
|
||||||
|
|
||||||
r = pa_stream_drop (p->stream);
|
|
||||||
p->read_data = NULL;
|
|
||||||
p->read_length = 0;
|
|
||||||
p->read_index = 0;
|
|
||||||
|
|
||||||
CHECK_SUCCESS_GOTO (g, rerror, r == 0, unlock_and_fail);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
unlock_and_fail:
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int qpa_simple_write (PAVoiceOut *p, const void *data, size_t length, int *rerror)
|
|
||||||
{
|
|
||||||
paaudio *g = &glob_paaudio;
|
|
||||||
|
|
||||||
pa_threaded_mainloop_lock (g->mainloop);
|
|
||||||
|
|
||||||
CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
|
|
||||||
|
|
||||||
while (length > 0) {
|
|
||||||
size_t l;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
while (!(l = pa_stream_writable_size (p->stream))) {
|
|
||||||
pa_threaded_mainloop_wait (g->mainloop);
|
|
||||||
CHECK_DEAD_GOTO (g, p->stream, rerror, unlock_and_fail);
|
|
||||||
}
|
|
||||||
|
|
||||||
CHECK_SUCCESS_GOTO (g, rerror, l != (size_t) -1, unlock_and_fail);
|
|
||||||
|
|
||||||
if (l > length) {
|
|
||||||
l = length;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = pa_stream_write (p->stream, data, l, NULL, 0LL, PA_SEEK_RELATIVE);
|
|
||||||
CHECK_SUCCESS_GOTO (g, rerror, r >= 0, unlock_and_fail);
|
|
||||||
|
|
||||||
data = (const uint8_t *) data + l;
|
|
||||||
length -= l;
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
unlock_and_fail:
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void *qpa_thread_out (void *arg)
|
static void *qpa_thread_out (void *arg)
|
||||||
{
|
{
|
||||||
PAVoiceOut *pa = arg;
|
PAVoiceOut *pa = arg;
|
||||||
@@ -222,7 +77,7 @@ static void *qpa_thread_out (void *arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
decr = to_mix = audio_MIN (pa->live, glob_paaudio.samples >> 2);
|
decr = to_mix = audio_MIN (pa->live, conf.samples >> 2);
|
||||||
rpos = pa->rpos;
|
rpos = pa->rpos;
|
||||||
|
|
||||||
if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
|
if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
|
||||||
@@ -236,7 +91,7 @@ static void *qpa_thread_out (void *arg)
|
|||||||
|
|
||||||
hw->clip (pa->pcm_buf, src, chunk);
|
hw->clip (pa->pcm_buf, src, chunk);
|
||||||
|
|
||||||
if (qpa_simple_write (pa, pa->pcm_buf,
|
if (pa_simple_write (pa->s, pa->pcm_buf,
|
||||||
chunk << hw->info.shift, &error) < 0) {
|
chunk << hw->info.shift, &error) < 0) {
|
||||||
qpa_logerr (error, "pa_simple_write failed\n");
|
qpa_logerr (error, "pa_simple_write failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -314,7 +169,7 @@ static void *qpa_thread_in (void *arg)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
incr = to_grab = audio_MIN (pa->dead, glob_paaudio.samples >> 2);
|
incr = to_grab = audio_MIN (pa->dead, conf.samples >> 2);
|
||||||
wpos = pa->wpos;
|
wpos = pa->wpos;
|
||||||
|
|
||||||
if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
|
if (audio_pt_unlock (&pa->pt, AUDIO_FUNC)) {
|
||||||
@@ -326,7 +181,7 @@ static void *qpa_thread_in (void *arg)
|
|||||||
int chunk = audio_MIN (to_grab, hw->samples - wpos);
|
int chunk = audio_MIN (to_grab, hw->samples - wpos);
|
||||||
void *buf = advance (pa->pcm_buf, wpos);
|
void *buf = advance (pa->pcm_buf, wpos);
|
||||||
|
|
||||||
if (qpa_simple_read (pa, buf,
|
if (pa_simple_read (pa->s, buf,
|
||||||
chunk << hw->info.shift, &error) < 0) {
|
chunk << hw->info.shift, &error) < 0) {
|
||||||
qpa_logerr (error, "pa_simple_read failed\n");
|
qpa_logerr (error, "pa_simple_read failed\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -428,112 +283,6 @@ 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 = &glob_paaudio;
|
|
||||||
|
|
||||||
switch (pa_context_get_state(c)) {
|
|
||||||
case PA_CONTEXT_READY:
|
|
||||||
case PA_CONTEXT_TERMINATED:
|
|
||||||
case PA_CONTEXT_FAILED:
|
|
||||||
pa_threaded_mainloop_signal (g->mainloop, 0);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PA_CONTEXT_UNCONNECTED:
|
|
||||||
case PA_CONTEXT_CONNECTING:
|
|
||||||
case PA_CONTEXT_AUTHORIZING:
|
|
||||||
case PA_CONTEXT_SETTING_NAME:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void stream_state_cb (pa_stream *s, void * userdata)
|
|
||||||
{
|
|
||||||
paaudio *g = &glob_paaudio;
|
|
||||||
|
|
||||||
switch (pa_stream_get_state (s)) {
|
|
||||||
|
|
||||||
case PA_STREAM_READY:
|
|
||||||
case PA_STREAM_FAILED:
|
|
||||||
case PA_STREAM_TERMINATED:
|
|
||||||
pa_threaded_mainloop_signal (g->mainloop, 0);
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PA_STREAM_UNCONNECTED:
|
|
||||||
case PA_STREAM_CREATING:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void stream_request_cb (pa_stream *s, size_t length, void *userdata)
|
|
||||||
{
|
|
||||||
paaudio *g = &glob_paaudio;
|
|
||||||
|
|
||||||
pa_threaded_mainloop_signal (g->mainloop, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static pa_stream *qpa_simple_new (
|
|
||||||
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;
|
|
||||||
|
|
||||||
pa_threaded_mainloop_lock (g->mainloop);
|
|
||||||
|
|
||||||
stream = pa_stream_new (g->context, name, ss, map);
|
|
||||||
if (!stream) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_stream_set_state_callback (stream, stream_state_cb, g);
|
|
||||||
pa_stream_set_read_callback (stream, stream_request_cb, g);
|
|
||||||
pa_stream_set_write_callback (stream, stream_request_cb, g);
|
|
||||||
|
|
||||||
if (dir == PA_STREAM_PLAYBACK) {
|
|
||||||
r = pa_stream_connect_playback (stream, dev, attr,
|
|
||||||
PA_STREAM_INTERPOLATE_TIMING
|
|
||||||
#ifdef PA_STREAM_ADJUST_LATENCY
|
|
||||||
|PA_STREAM_ADJUST_LATENCY
|
|
||||||
#endif
|
|
||||||
|PA_STREAM_AUTO_TIMING_UPDATE, NULL, NULL);
|
|
||||||
} else {
|
|
||||||
r = pa_stream_connect_record (stream, dev, attr,
|
|
||||||
PA_STREAM_INTERPOLATE_TIMING
|
|
||||||
#ifdef PA_STREAM_ADJUST_LATENCY
|
|
||||||
|PA_STREAM_ADJUST_LATENCY
|
|
||||||
#endif
|
|
||||||
|PA_STREAM_AUTO_TIMING_UPDATE);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (r < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
|
|
||||||
return stream;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
|
|
||||||
if (stream) {
|
|
||||||
pa_stream_unref (stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
*rerror = pa_context_errno (g->context);
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
||||||
{
|
{
|
||||||
int error;
|
int error;
|
||||||
@@ -547,34 +296,34 @@ static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
ss.rate = as->freq;
|
ss.rate = as->freq;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* qemu audio tick runs at 100 Hz (by default), so processing
|
* qemu audio tick runs at 250 Hz (by default), so processing
|
||||||
* data chunks worth 10 ms of sound should be a good fit.
|
* data chunks worth 4 ms of sound should be a good fit.
|
||||||
*/
|
*/
|
||||||
ba.tlength = pa_usec_to_bytes (10 * 1000, &ss);
|
ba.tlength = pa_usec_to_bytes (4 * 1000, &ss);
|
||||||
ba.minreq = pa_usec_to_bytes (5 * 1000, &ss);
|
ba.minreq = pa_usec_to_bytes (2 * 1000, &ss);
|
||||||
ba.maxlength = -1;
|
ba.maxlength = -1;
|
||||||
ba.prebuf = -1;
|
ba.prebuf = -1;
|
||||||
|
|
||||||
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
||||||
|
|
||||||
pa->stream = qpa_simple_new (
|
pa->s = pa_simple_new (
|
||||||
glob_paaudio.server,
|
conf.server,
|
||||||
"qemu",
|
"qemu",
|
||||||
PA_STREAM_PLAYBACK,
|
PA_STREAM_PLAYBACK,
|
||||||
glob_paaudio.sink,
|
conf.sink,
|
||||||
"pcm.playback",
|
"pcm.playback",
|
||||||
&ss,
|
&ss,
|
||||||
NULL, /* channel map */
|
NULL, /* channel map */
|
||||||
&ba, /* buffering attributes */
|
&ba, /* buffering attributes */
|
||||||
&error
|
&error
|
||||||
);
|
);
|
||||||
if (!pa->stream) {
|
if (!pa->s) {
|
||||||
qpa_logerr (error, "pa_simple_new for playback failed\n");
|
qpa_logerr (error, "pa_simple_new for playback failed\n");
|
||||||
goto fail1;
|
goto fail1;
|
||||||
}
|
}
|
||||||
|
|
||||||
audio_pcm_init_info (&hw->info, &obt_as);
|
audio_pcm_init_info (&hw->info, &obt_as);
|
||||||
hw->samples = glob_paaudio.samples;
|
hw->samples = conf.samples;
|
||||||
pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
||||||
pa->rpos = hw->rpos;
|
pa->rpos = hw->rpos;
|
||||||
if (!pa->pcm_buf) {
|
if (!pa->pcm_buf) {
|
||||||
@@ -590,13 +339,11 @@ static int qpa_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fail3:
|
fail3:
|
||||||
g_free (pa->pcm_buf);
|
qemu_free (pa->pcm_buf);
|
||||||
pa->pcm_buf = NULL;
|
pa->pcm_buf = NULL;
|
||||||
fail2:
|
fail2:
|
||||||
if (pa->stream) {
|
pa_simple_free (pa->s);
|
||||||
pa_stream_unref (pa->stream);
|
pa->s = NULL;
|
||||||
pa->stream = NULL;
|
|
||||||
}
|
|
||||||
fail1:
|
fail1:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -614,24 +361,24 @@ static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
|
|
||||||
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
obt_as.fmt = pa_to_audfmt (ss.format, &obt_as.endianness);
|
||||||
|
|
||||||
pa->stream = qpa_simple_new (
|
pa->s = pa_simple_new (
|
||||||
glob_paaudio.server,
|
conf.server,
|
||||||
"qemu",
|
"qemu",
|
||||||
PA_STREAM_RECORD,
|
PA_STREAM_RECORD,
|
||||||
glob_paaudio.source,
|
conf.source,
|
||||||
"pcm.capture",
|
"pcm.capture",
|
||||||
&ss,
|
&ss,
|
||||||
NULL, /* channel map */
|
NULL, /* channel map */
|
||||||
NULL, /* buffering attributes */
|
NULL, /* buffering attributes */
|
||||||
&error
|
&error
|
||||||
);
|
);
|
||||||
if (!pa->stream) {
|
if (!pa->s) {
|
||||||
qpa_logerr (error, "pa_simple_new for capture failed\n");
|
qpa_logerr (error, "pa_simple_new for capture failed\n");
|
||||||
goto fail1;
|
goto fail1;
|
||||||
}
|
}
|
||||||
|
|
||||||
audio_pcm_init_info (&hw->info, &obt_as);
|
audio_pcm_init_info (&hw->info, &obt_as);
|
||||||
hw->samples = glob_paaudio.samples;
|
hw->samples = conf.samples;
|
||||||
pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
pa->pcm_buf = audio_calloc (AUDIO_FUNC, hw->samples, 1 << hw->info.shift);
|
||||||
pa->wpos = hw->wpos;
|
pa->wpos = hw->wpos;
|
||||||
if (!pa->pcm_buf) {
|
if (!pa->pcm_buf) {
|
||||||
@@ -647,13 +394,11 @@ static int qpa_init_in (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fail3:
|
fail3:
|
||||||
g_free (pa->pcm_buf);
|
qemu_free (pa->pcm_buf);
|
||||||
pa->pcm_buf = NULL;
|
pa->pcm_buf = NULL;
|
||||||
fail2:
|
fail2:
|
||||||
if (pa->stream) {
|
pa_simple_free (pa->s);
|
||||||
pa_stream_unref (pa->stream);
|
pa->s = NULL;
|
||||||
pa->stream = NULL;
|
|
||||||
}
|
|
||||||
fail1:
|
fail1:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -668,13 +413,13 @@ static void qpa_fini_out (HWVoiceOut *hw)
|
|||||||
audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
|
audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
|
||||||
audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
|
audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
|
||||||
|
|
||||||
if (pa->stream) {
|
if (pa->s) {
|
||||||
pa_stream_unref (pa->stream);
|
pa_simple_free (pa->s);
|
||||||
pa->stream = NULL;
|
pa->s = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
audio_pt_fini (&pa->pt, AUDIO_FUNC);
|
audio_pt_fini (&pa->pt, AUDIO_FUNC);
|
||||||
g_free (pa->pcm_buf);
|
qemu_free (pa->pcm_buf);
|
||||||
pa->pcm_buf = NULL;
|
pa->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -688,225 +433,64 @@ static void qpa_fini_in (HWVoiceIn *hw)
|
|||||||
audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
|
audio_pt_unlock_and_signal (&pa->pt, AUDIO_FUNC);
|
||||||
audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
|
audio_pt_join (&pa->pt, &ret, AUDIO_FUNC);
|
||||||
|
|
||||||
if (pa->stream) {
|
if (pa->s) {
|
||||||
pa_stream_unref (pa->stream);
|
pa_simple_free (pa->s);
|
||||||
pa->stream = NULL;
|
pa->s = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
audio_pt_fini (&pa->pt, AUDIO_FUNC);
|
audio_pt_fini (&pa->pt, AUDIO_FUNC);
|
||||||
g_free (pa->pcm_buf);
|
qemu_free (pa->pcm_buf);
|
||||||
pa->pcm_buf = NULL;
|
pa->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
static int qpa_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
||||||
{
|
{
|
||||||
PAVoiceOut *pa = (PAVoiceOut *) hw;
|
(void) hw;
|
||||||
pa_operation *op;
|
(void) cmd;
|
||||||
pa_cvolume v;
|
|
||||||
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+ */
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch (cmd) {
|
|
||||||
case VOICE_VOLUME:
|
|
||||||
{
|
|
||||||
SWVoiceOut *sw;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start (ap, cmd);
|
|
||||||
sw = va_arg (ap, SWVoiceOut *);
|
|
||||||
va_end (ap);
|
|
||||||
|
|
||||||
v.channels = 2;
|
|
||||||
v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX;
|
|
||||||
v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX;
|
|
||||||
|
|
||||||
pa_threaded_mainloop_lock (g->mainloop);
|
|
||||||
|
|
||||||
op = pa_context_set_sink_input_volume (g->context,
|
|
||||||
pa_stream_get_index (pa->stream),
|
|
||||||
&v, NULL, NULL);
|
|
||||||
if (!op)
|
|
||||||
qpa_logerr (pa_context_errno (g->context),
|
|
||||||
"set_sink_input_volume() failed\n");
|
|
||||||
else
|
|
||||||
pa_operation_unref (op);
|
|
||||||
|
|
||||||
op = pa_context_set_sink_input_mute (g->context,
|
|
||||||
pa_stream_get_index (pa->stream),
|
|
||||||
sw->vol.mute, NULL, NULL);
|
|
||||||
if (!op) {
|
|
||||||
qpa_logerr (pa_context_errno (g->context),
|
|
||||||
"set_sink_input_mute() failed\n");
|
|
||||||
} else {
|
|
||||||
pa_operation_unref (op);
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
static int qpa_ctl_in (HWVoiceIn *hw, int cmd, ...)
|
||||||
{
|
{
|
||||||
PAVoiceIn *pa = (PAVoiceIn *) hw;
|
(void) hw;
|
||||||
pa_operation *op;
|
(void) cmd;
|
||||||
pa_cvolume v;
|
|
||||||
paaudio *g = &glob_paaudio;
|
|
||||||
|
|
||||||
#ifdef PA_CHECK_VERSION
|
|
||||||
pa_cvolume_init (&v);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
switch (cmd) {
|
|
||||||
case VOICE_VOLUME:
|
|
||||||
{
|
|
||||||
SWVoiceIn *sw;
|
|
||||||
va_list ap;
|
|
||||||
|
|
||||||
va_start (ap, cmd);
|
|
||||||
sw = va_arg (ap, SWVoiceIn *);
|
|
||||||
va_end (ap);
|
|
||||||
|
|
||||||
v.channels = 2;
|
|
||||||
v.values[0] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.l) / UINT32_MAX;
|
|
||||||
v.values[1] = ((PA_VOLUME_NORM - PA_VOLUME_MUTED) * sw->vol.r) / UINT32_MAX;
|
|
||||||
|
|
||||||
pa_threaded_mainloop_lock (g->mainloop);
|
|
||||||
|
|
||||||
/* 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_volume() failed\n");
|
|
||||||
} else {
|
|
||||||
pa_operation_unref(op);
|
|
||||||
}
|
|
||||||
|
|
||||||
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_mute() failed\n");
|
|
||||||
} else {
|
|
||||||
pa_operation_unref (op);
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* common */
|
/* common */
|
||||||
static void *qpa_audio_init (void)
|
static void *qpa_audio_init (void)
|
||||||
{
|
{
|
||||||
paaudio *g = &glob_paaudio;
|
return &conf;
|
||||||
|
|
||||||
g->mainloop = pa_threaded_mainloop_new ();
|
|
||||||
if (!g->mainloop) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
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, glob_paaudio.server, 0, NULL) < 0) {
|
|
||||||
qpa_logerr (pa_context_errno (g->context),
|
|
||||||
"pa_context_connect() failed\n");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_lock (g->mainloop);
|
|
||||||
|
|
||||||
if (pa_threaded_mainloop_start (g->mainloop) < 0) {
|
|
||||||
goto unlock_and_fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (;;) {
|
|
||||||
pa_context_state_t state;
|
|
||||||
|
|
||||||
state = pa_context_get_state (g->context);
|
|
||||||
|
|
||||||
if (state == PA_CONTEXT_READY) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!PA_CONTEXT_IS_GOOD (state)) {
|
|
||||||
qpa_logerr (pa_context_errno (g->context),
|
|
||||||
"Wrong context state\n");
|
|
||||||
goto unlock_and_fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Wait until the context is ready */
|
|
||||||
pa_threaded_mainloop_wait (g->mainloop);
|
|
||||||
}
|
|
||||||
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
|
|
||||||
return &glob_paaudio;
|
|
||||||
|
|
||||||
unlock_and_fail:
|
|
||||||
pa_threaded_mainloop_unlock (g->mainloop);
|
|
||||||
fail:
|
|
||||||
AUD_log (AUDIO_CAP, "Failed to initialize PA context");
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void qpa_audio_fini (void *opaque)
|
static void qpa_audio_fini (void *opaque)
|
||||||
{
|
{
|
||||||
paaudio *g = opaque;
|
(void) opaque;
|
||||||
|
|
||||||
if (g->mainloop) {
|
|
||||||
pa_threaded_mainloop_stop (g->mainloop);
|
|
||||||
}
|
|
||||||
|
|
||||||
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->mainloop = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct audio_option qpa_options[] = {
|
struct audio_option qpa_options[] = {
|
||||||
{
|
{
|
||||||
.name = "SAMPLES",
|
.name = "SAMPLES",
|
||||||
.tag = AUD_OPT_INT,
|
.tag = AUD_OPT_INT,
|
||||||
.valp = &glob_paaudio.samples,
|
.valp = &conf.samples,
|
||||||
.descr = "buffer size in samples"
|
.descr = "buffer size in samples"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "SERVER",
|
.name = "SERVER",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_paaudio.server,
|
.valp = &conf.server,
|
||||||
.descr = "server address"
|
.descr = "server address"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "SINK",
|
.name = "SINK",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_paaudio.sink,
|
.valp = &conf.sink,
|
||||||
.descr = "sink device name"
|
.descr = "sink device name"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.name = "SOURCE",
|
.name = "SOURCE",
|
||||||
.tag = AUD_OPT_STR,
|
.tag = AUD_OPT_STR,
|
||||||
.valp = &glob_paaudio.source,
|
.valp = &conf.source,
|
||||||
.descr = "source device name"
|
.descr = "source device name"
|
||||||
},
|
},
|
||||||
{ /* End of list */ }
|
{ /* End of list */ }
|
||||||
@@ -937,6 +521,5 @@ struct audio_driver pa_audio_driver = {
|
|||||||
.max_voices_out = INT_MAX,
|
.max_voices_out = INT_MAX,
|
||||||
.max_voices_in = INT_MAX,
|
.max_voices_in = INT_MAX,
|
||||||
.voice_size_out = sizeof (PAVoiceOut),
|
.voice_size_out = sizeof (PAVoiceOut),
|
||||||
.voice_size_in = sizeof (PAVoiceIn),
|
.voice_size_in = sizeof (PAVoiceIn)
|
||||||
.ctl_caps = VOICE_VOLUME_CAP
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -18,24 +18,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "qemu/timer.h"
|
#include "qemu-timer.h"
|
||||||
#include "ui/qemu-spice.h"
|
#include "ui/qemu-spice.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "spice"
|
#define AUDIO_CAP "spice"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
#include "audio_int.h"
|
#include "audio_int.h"
|
||||||
|
|
||||||
#if SPICE_INTERFACE_PLAYBACK_MAJOR > 1 || SPICE_INTERFACE_PLAYBACK_MINOR >= 3
|
#define LINE_IN_SAMPLES 1024
|
||||||
#define LINE_OUT_SAMPLES (480 * 4)
|
#define LINE_OUT_SAMPLES 1024
|
||||||
#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
|
|
||||||
|
|
||||||
typedef struct SpiceRateCtl {
|
typedef struct SpiceRateCtl {
|
||||||
int64_t start_ticks;
|
int64_t start_ticks;
|
||||||
@@ -90,7 +81,7 @@ static void spice_audio_fini (void *opaque)
|
|||||||
static void rate_start (SpiceRateCtl *rate)
|
static void rate_start (SpiceRateCtl *rate)
|
||||||
{
|
{
|
||||||
memset (rate, 0, sizeof (*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)
|
static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
|
||||||
@@ -100,12 +91,12 @@ static int rate_get_samples (struct audio_pcm_info *info, SpiceRateCtl *rate)
|
|||||||
int64_t bytes;
|
int64_t bytes;
|
||||||
int64_t samples;
|
int64_t samples;
|
||||||
|
|
||||||
now = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
|
now = qemu_get_clock_ns (vm_clock);
|
||||||
ticks = now - rate->start_ticks;
|
ticks = now - rate->start_ticks;
|
||||||
bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
|
bytes = muldiv64 (ticks, info->bytes_per_second, get_ticks_per_sec ());
|
||||||
samples = (bytes - rate->bytes_sent) >> info->shift;
|
samples = (bytes - rate->bytes_sent) >> info->shift;
|
||||||
if (samples < 0 || samples > 65536) {
|
if (samples < 0 || samples > 65536) {
|
||||||
error_report("Resetting rate control (%" PRId64 " samples)", samples);
|
fprintf (stderr, "Resetting rate control (%" PRId64 " samples)\n", samples);
|
||||||
rate_start (rate);
|
rate_start (rate);
|
||||||
samples = 0;
|
samples = 0;
|
||||||
}
|
}
|
||||||
@@ -120,11 +111,7 @@ static int line_out_init (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
SpiceVoiceOut *out = container_of (hw, SpiceVoiceOut, hw);
|
||||||
struct audsettings settings;
|
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;
|
settings.freq = SPICE_INTERFACE_PLAYBACK_FREQ;
|
||||||
#endif
|
|
||||||
settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
|
settings.nchannels = SPICE_INTERFACE_PLAYBACK_CHAN;
|
||||||
settings.fmt = AUD_FMT_S16;
|
settings.fmt = AUD_FMT_S16;
|
||||||
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
||||||
@@ -135,9 +122,6 @@ static int line_out_init (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
|
|
||||||
out->sin.base.sif = &playback_sif.base;
|
out->sin.base.sif = &playback_sif.base;
|
||||||
qemu_spice_add_interface (&out->sin.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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -218,26 +202,7 @@ static int line_out_ctl (HWVoiceOut *hw, int cmd, ...)
|
|||||||
}
|
}
|
||||||
spice_server_playback_stop (&out->sin);
|
spice_server_playback_stop (&out->sin);
|
||||||
break;
|
break;
|
||||||
case VOICE_VOLUME:
|
|
||||||
{
|
|
||||||
#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
|
|
||||||
SWVoiceOut *sw;
|
|
||||||
va_list ap;
|
|
||||||
uint16_t vol[2];
|
|
||||||
|
|
||||||
va_start (ap, cmd);
|
|
||||||
sw = va_arg (ap, SWVoiceOut *);
|
|
||||||
va_end (ap);
|
|
||||||
|
|
||||||
vol[0] = sw->vol.l / ((1ULL << 16) + 1);
|
|
||||||
vol[1] = sw->vol.r / ((1ULL << 16) + 1);
|
|
||||||
spice_server_playback_set_volume (&out->sin, 2, vol);
|
|
||||||
spice_server_playback_set_mute (&out->sin, sw->vol.mute);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,11 +213,7 @@ static int line_in_init (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
SpiceVoiceIn *in = container_of (hw, SpiceVoiceIn, hw);
|
||||||
struct audsettings settings;
|
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;
|
settings.freq = SPICE_INTERFACE_RECORD_FREQ;
|
||||||
#endif
|
|
||||||
settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
|
settings.nchannels = SPICE_INTERFACE_RECORD_CHAN;
|
||||||
settings.fmt = AUD_FMT_S16;
|
settings.fmt = AUD_FMT_S16;
|
||||||
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
settings.endianness = AUDIO_HOST_ENDIANNESS;
|
||||||
@@ -263,9 +224,6 @@ static int line_in_init (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
|
|
||||||
in->sin.base.sif = &record_sif.base;
|
in->sin.base.sif = &record_sif.base;
|
||||||
qemu_spice_add_interface (&in->sin.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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -346,26 +304,7 @@ static int line_in_ctl (HWVoiceIn *hw, int cmd, ...)
|
|||||||
in->active = 0;
|
in->active = 0;
|
||||||
spice_server_record_stop (&in->sin);
|
spice_server_record_stop (&in->sin);
|
||||||
break;
|
break;
|
||||||
case VOICE_VOLUME:
|
|
||||||
{
|
|
||||||
#if ((SPICE_INTERFACE_RECORD_MAJOR >= 2) && (SPICE_INTERFACE_RECORD_MINOR >= 2))
|
|
||||||
SWVoiceIn *sw;
|
|
||||||
va_list ap;
|
|
||||||
uint16_t vol[2];
|
|
||||||
|
|
||||||
va_start (ap, cmd);
|
|
||||||
sw = va_arg (ap, SWVoiceIn *);
|
|
||||||
va_end (ap);
|
|
||||||
|
|
||||||
vol[0] = sw->vol.l / ((1ULL << 16) + 1);
|
|
||||||
vol[1] = sw->vol.r / ((1ULL << 16) + 1);
|
|
||||||
spice_server_record_set_volume (&in->sin, 2, vol);
|
|
||||||
spice_server_record_set_mute (&in->sin, sw->vol.mute);
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -398,9 +337,6 @@ struct audio_driver spice_audio_driver = {
|
|||||||
.max_voices_in = 1,
|
.max_voices_in = 1,
|
||||||
.voice_size_out = sizeof (SpiceVoiceOut),
|
.voice_size_out = sizeof (SpiceVoiceOut),
|
||||||
.voice_size_in = sizeof (SpiceVoiceIn),
|
.voice_size_in = sizeof (SpiceVoiceIn),
|
||||||
#if ((SPICE_INTERFACE_PLAYBACK_MAJOR >= 1) && (SPICE_INTERFACE_PLAYBACK_MINOR >= 2))
|
|
||||||
.ctl_caps = VOICE_VOLUME_CAP
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void qemu_spice_audio_init (void)
|
void qemu_spice_audio_init (void)
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "qemu/timer.h"
|
#include "qemu-timer.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "wav"
|
#define AUDIO_CAP "wav"
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
typedef struct WAVVoiceOut {
|
typedef struct WAVVoiceOut {
|
||||||
HWVoiceOut hw;
|
HWVoiceOut hw;
|
||||||
FILE *f;
|
QEMUFile *f;
|
||||||
int64_t old_ticks;
|
int64_t old_ticks;
|
||||||
void *pcm_buf;
|
void *pcm_buf;
|
||||||
int total_samples;
|
int total_samples;
|
||||||
@@ -52,7 +52,7 @@ static int wav_run_out (HWVoiceOut *hw, int live)
|
|||||||
int rpos, decr, samples;
|
int rpos, decr, samples;
|
||||||
uint8_t *dst;
|
uint8_t *dst;
|
||||||
struct st_sample *src;
|
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 ticks = now - wav->old_ticks;
|
||||||
int64_t bytes =
|
int64_t bytes =
|
||||||
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
muldiv64 (ticks, hw->info.bytes_per_second, get_ticks_per_sec ());
|
||||||
@@ -76,10 +76,7 @@ static int wav_run_out (HWVoiceOut *hw, int live)
|
|||||||
dst = advance (wav->pcm_buf, rpos << hw->info.shift);
|
dst = advance (wav->pcm_buf, rpos << hw->info.shift);
|
||||||
|
|
||||||
hw->clip (dst, src, convert_samples);
|
hw->clip (dst, src, convert_samples);
|
||||||
if (fwrite (dst, convert_samples << hw->info.shift, 1, wav->f) != 1) {
|
qemu_put_buffer (wav->f, dst, convert_samples << hw->info.shift);
|
||||||
dolog ("wav_run_out: fwrite of %d bytes failed\nReaons: %s\n",
|
|
||||||
convert_samples << hw->info.shift, strerror (errno));
|
|
||||||
}
|
|
||||||
|
|
||||||
rpos = (rpos + convert_samples) % hw->samples;
|
rpos = (rpos + convert_samples) % hw->samples;
|
||||||
samples -= convert_samples;
|
samples -= convert_samples;
|
||||||
@@ -155,20 +152,16 @@ static int wav_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4);
|
le_store (hdr + 28, hw->info.freq << (bits16 + stereo), 4);
|
||||||
le_store (hdr + 32, 1 << (bits16 + stereo), 2);
|
le_store (hdr + 32, 1 << (bits16 + stereo), 2);
|
||||||
|
|
||||||
wav->f = fopen (conf.wav_path, "wb");
|
wav->f = qemu_fopen (conf.wav_path, "wb");
|
||||||
if (!wav->f) {
|
if (!wav->f) {
|
||||||
dolog ("Failed to open wave file `%s'\nReason: %s\n",
|
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);
|
qemu_free (wav->pcm_buf);
|
||||||
wav->pcm_buf = NULL;
|
wav->pcm_buf = NULL;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) {
|
qemu_put_buffer (wav->f, hdr, sizeof (hdr));
|
||||||
dolog ("wav_init_out: failed to write header\nReason: %s\n",
|
|
||||||
strerror(errno));
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,35 +180,16 @@ static void wav_fini_out (HWVoiceOut *hw)
|
|||||||
le_store (rlen, rifflen, 4);
|
le_store (rlen, rifflen, 4);
|
||||||
le_store (dlen, datalen, 4);
|
le_store (dlen, datalen, 4);
|
||||||
|
|
||||||
if (fseek (wav->f, 4, SEEK_SET)) {
|
qemu_fseek (wav->f, 4, SEEK_SET);
|
||||||
dolog ("wav_fini_out: fseek to rlen failed\nReason: %s\n",
|
qemu_put_buffer (wav->f, rlen, 4);
|
||||||
strerror(errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
if (fwrite (rlen, 4, 1, wav->f) != 1) {
|
|
||||||
dolog ("wav_fini_out: failed to write rlen\nReason: %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
if (fseek (wav->f, 32, SEEK_CUR)) {
|
|
||||||
dolog ("wav_fini_out: fseek to dlen failed\nReason: %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
if (fwrite (dlen, 4, 1, wav->f) != 1) {
|
|
||||||
dolog ("wav_fini_out: failed to write dlen\nReaons: %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
|
|
||||||
doclose:
|
qemu_fseek (wav->f, 32, SEEK_CUR);
|
||||||
if (fclose (wav->f)) {
|
qemu_put_buffer (wav->f, dlen, 4);
|
||||||
dolog ("wav_fini_out: fclose %p failed\nReason: %s\n",
|
|
||||||
wav->f, strerror (errno));
|
qemu_fclose (wav->f);
|
||||||
}
|
|
||||||
wav->f = NULL;
|
wav->f = NULL;
|
||||||
|
|
||||||
g_free (wav->pcm_buf);
|
qemu_free (wav->pcm_buf);
|
||||||
wav->pcm_buf = NULL;
|
wav->pcm_buf = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#include "hw/hw.h"
|
#include "hw/hw.h"
|
||||||
#include "monitor/monitor.h"
|
#include "monitor.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
FILE *f;
|
QEMUFile *f;
|
||||||
int bytes;
|
int bytes;
|
||||||
char *path;
|
char *path;
|
||||||
int freq;
|
int freq;
|
||||||
@@ -35,49 +35,27 @@ static void wav_destroy (void *opaque)
|
|||||||
uint8_t dlen[4];
|
uint8_t dlen[4];
|
||||||
uint32_t datalen = wav->bytes;
|
uint32_t datalen = wav->bytes;
|
||||||
uint32_t rifflen = datalen + 36;
|
uint32_t rifflen = datalen + 36;
|
||||||
Monitor *mon = cur_mon;
|
|
||||||
|
|
||||||
if (wav->f) {
|
if (wav->f) {
|
||||||
le_store (rlen, rifflen, 4);
|
le_store (rlen, rifflen, 4);
|
||||||
le_store (dlen, datalen, 4);
|
le_store (dlen, datalen, 4);
|
||||||
|
|
||||||
if (fseek (wav->f, 4, SEEK_SET)) {
|
qemu_fseek (wav->f, 4, SEEK_SET);
|
||||||
monitor_printf (mon, "wav_destroy: rlen fseek failed\nReason: %s\n",
|
qemu_put_buffer (wav->f, rlen, 4);
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
qemu_fseek (wav->f, 32, SEEK_CUR);
|
||||||
}
|
qemu_put_buffer (wav->f, dlen, 4);
|
||||||
if (fwrite (rlen, 4, 1, wav->f) != 1) {
|
qemu_fclose (wav->f);
|
||||||
monitor_printf (mon, "wav_destroy: rlen fwrite failed\nReason %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
if (fseek (wav->f, 32, SEEK_CUR)) {
|
|
||||||
monitor_printf (mon, "wav_destroy: dlen fseek failed\nReason %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
if (fwrite (dlen, 1, 4, wav->f) != 4) {
|
|
||||||
monitor_printf (mon, "wav_destroy: dlen fwrite failed\nReason %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto doclose;
|
|
||||||
}
|
|
||||||
doclose:
|
|
||||||
if (fclose (wav->f)) {
|
|
||||||
error_report("wav_destroy: fclose failed: %s", strerror(errno));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (wav->path);
|
qemu_free (wav->path);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void wav_capture (void *opaque, void *buf, int size)
|
static void wav_capture (void *opaque, void *buf, int size)
|
||||||
{
|
{
|
||||||
WAVState *wav = opaque;
|
WAVState *wav = opaque;
|
||||||
|
|
||||||
if (fwrite (buf, size, 1, wav->f) != 1) {
|
qemu_put_buffer (wav->f, buf, size);
|
||||||
monitor_printf (cur_mon, "wav_capture: fwrite error\nReason: %s",
|
|
||||||
strerror (errno));
|
|
||||||
}
|
|
||||||
wav->bytes += size;
|
wav->bytes += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,7 +71,7 @@ static void wav_capture_info (void *opaque)
|
|||||||
WAVState *wav = opaque;
|
WAVState *wav = opaque;
|
||||||
char *path = wav->path;
|
char *path = wav->path;
|
||||||
|
|
||||||
monitor_printf (cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
|
monitor_printf(cur_mon, "Capturing audio(%d,%d,%d) to %s: %d bytes\n",
|
||||||
wav->freq, wav->bits, wav->nchannels,
|
wav->freq, wav->bits, wav->nchannels,
|
||||||
path ? path : "<not available>", wav->bytes);
|
path ? path : "<not available>", wav->bytes);
|
||||||
}
|
}
|
||||||
@@ -120,12 +98,12 @@ int wav_start_capture (CaptureState *s, const char *path, int freq,
|
|||||||
CaptureVoiceOut *cap;
|
CaptureVoiceOut *cap;
|
||||||
|
|
||||||
if (bits != 8 && bits != 16) {
|
if (bits != 8 && bits != 16) {
|
||||||
monitor_printf (mon, "incorrect bit count %d, must be 8 or 16\n", bits);
|
monitor_printf(mon, "incorrect bit count %d, must be 8 or 16\n", bits);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nchannels != 1 && nchannels != 2) {
|
if (nchannels != 1 && nchannels != 2) {
|
||||||
monitor_printf (mon, "incorrect channel count %d, must be 1 or 2\n",
|
monitor_printf(mon, "incorrect channel count %d, must be 1 or 2\n",
|
||||||
nchannels);
|
nchannels);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -142,7 +120,7 @@ int wav_start_capture (CaptureState *s, const char *path, int freq,
|
|||||||
ops.capture = wav_capture;
|
ops.capture = wav_capture;
|
||||||
ops.destroy = wav_destroy;
|
ops.destroy = wav_destroy;
|
||||||
|
|
||||||
wav = g_malloc0 (sizeof (*wav));
|
wav = qemu_mallocz (sizeof (*wav));
|
||||||
|
|
||||||
shift = bits16 + stereo;
|
shift = bits16 + stereo;
|
||||||
hdr[34] = bits16 ? 0x10 : 0x08;
|
hdr[34] = bits16 ? 0x10 : 0x08;
|
||||||
@@ -152,42 +130,32 @@ int wav_start_capture (CaptureState *s, const char *path, int freq,
|
|||||||
le_store (hdr + 28, freq << shift, 4);
|
le_store (hdr + 28, freq << shift, 4);
|
||||||
le_store (hdr + 32, 1 << shift, 2);
|
le_store (hdr + 32, 1 << shift, 2);
|
||||||
|
|
||||||
wav->f = fopen (path, "wb");
|
wav->f = qemu_fopen (path, "wb");
|
||||||
if (!wav->f) {
|
if (!wav->f) {
|
||||||
monitor_printf (mon, "Failed to open wave file `%s'\nReason: %s\n",
|
monitor_printf(mon, "Failed to open wave file `%s'\nReason: %s\n",
|
||||||
path, strerror (errno));
|
path, strerror (errno));
|
||||||
g_free (wav);
|
qemu_free (wav);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
wav->path = g_strdup (path);
|
wav->path = qemu_strdup (path);
|
||||||
wav->bits = bits;
|
wav->bits = bits;
|
||||||
wav->nchannels = nchannels;
|
wav->nchannels = nchannels;
|
||||||
wav->freq = freq;
|
wav->freq = freq;
|
||||||
|
|
||||||
if (fwrite (hdr, sizeof (hdr), 1, wav->f) != 1) {
|
qemu_put_buffer (wav->f, hdr, sizeof (hdr));
|
||||||
monitor_printf (mon, "Failed to write header\nReason: %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
goto error_free;
|
|
||||||
}
|
|
||||||
|
|
||||||
cap = AUD_add_capture (&as, &ops, wav);
|
cap = AUD_add_capture (&as, &ops, wav);
|
||||||
if (!cap) {
|
if (!cap) {
|
||||||
monitor_printf (mon, "Failed to add audio capture\n");
|
monitor_printf(mon, "Failed to add audio capture\n");
|
||||||
goto error_free;
|
qemu_free (wav->path);
|
||||||
|
qemu_fclose (wav->f);
|
||||||
|
qemu_free (wav);
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
wav->cap = cap;
|
wav->cap = cap;
|
||||||
s->opaque = wav;
|
s->opaque = wav;
|
||||||
s->ops = wav_capture_ops;
|
s->ops = wav_capture_ops;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
error_free:
|
|
||||||
g_free (wav->path);
|
|
||||||
if (fclose (wav->f)) {
|
|
||||||
monitor_printf (mon, "Failed to close wave file\nReason: %s\n",
|
|
||||||
strerror (errno));
|
|
||||||
}
|
|
||||||
g_free (wav);
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
/* public domain */
|
/* public domain */
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "sysemu/sysemu.h"
|
#include "sysemu.h"
|
||||||
#include "audio.h"
|
#include "audio.h"
|
||||||
|
|
||||||
#define AUDIO_CAP "winwave"
|
#define AUDIO_CAP "winwave"
|
||||||
@@ -72,7 +72,7 @@ static void winwave_log_mmresult (MMRESULT mr)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case MMSYSERR_NOMEM:
|
case MMSYSERR_NOMEM:
|
||||||
str = "Unable to allocate or lock memory";
|
str = "Unable to allocate or locl memory";
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WAVERR_SYNC:
|
case WAVERR_SYNC:
|
||||||
@@ -222,9 +222,9 @@ static int winwave_init_out (HWVoiceOut *hw, struct audsettings *as)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
err4:
|
err4:
|
||||||
g_free (wave->pcm_buf);
|
qemu_free (wave->pcm_buf);
|
||||||
err3:
|
err3:
|
||||||
g_free (wave->hdrs);
|
qemu_free (wave->hdrs);
|
||||||
err2:
|
err2:
|
||||||
winwave_anal_close_out (wave);
|
winwave_anal_close_out (wave);
|
||||||
err1:
|
err1:
|
||||||
@@ -310,10 +310,10 @@ static void winwave_fini_out (HWVoiceOut *hw)
|
|||||||
wave->event = NULL;
|
wave->event = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (wave->pcm_buf);
|
qemu_free (wave->pcm_buf);
|
||||||
wave->pcm_buf = NULL;
|
wave->pcm_buf = NULL;
|
||||||
|
|
||||||
g_free (wave->hdrs);
|
qemu_free (wave->hdrs);
|
||||||
wave->hdrs = NULL;
|
wave->hdrs = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,15 +349,21 @@ static int winwave_ctl_out (HWVoiceOut *hw, int cmd, ...)
|
|||||||
else {
|
else {
|
||||||
hw->poll_mode = 0;
|
hw->poll_mode = 0;
|
||||||
}
|
}
|
||||||
|
if (wave->paused) {
|
||||||
|
mr = waveOutRestart (wave->hwo);
|
||||||
|
if (mr != MMSYSERR_NOERROR) {
|
||||||
|
winwave_logerr (mr, "waveOutRestart");
|
||||||
|
}
|
||||||
wave->paused = 0;
|
wave->paused = 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
case VOICE_DISABLE:
|
case VOICE_DISABLE:
|
||||||
if (!wave->paused) {
|
if (!wave->paused) {
|
||||||
mr = waveOutReset (wave->hwo);
|
mr = waveOutPause (wave->hwo);
|
||||||
if (mr != MMSYSERR_NOERROR) {
|
if (mr != MMSYSERR_NOERROR) {
|
||||||
winwave_logerr (mr, "waveOutReset");
|
winwave_logerr (mr, "waveOutPause");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
wave->paused = 1;
|
wave->paused = 1;
|
||||||
@@ -505,9 +511,9 @@ static int winwave_init_in (HWVoiceIn *hw, struct audsettings *as)
|
|||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
err4:
|
err4:
|
||||||
g_free (wave->pcm_buf);
|
qemu_free (wave->pcm_buf);
|
||||||
err3:
|
err3:
|
||||||
g_free (wave->hdrs);
|
qemu_free (wave->hdrs);
|
||||||
err2:
|
err2:
|
||||||
winwave_anal_close_in (wave);
|
winwave_anal_close_in (wave);
|
||||||
err1:
|
err1:
|
||||||
@@ -544,10 +550,10 @@ static void winwave_fini_in (HWVoiceIn *hw)
|
|||||||
wave->event = NULL;
|
wave->event = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
g_free (wave->pcm_buf);
|
qemu_free (wave->pcm_buf);
|
||||||
wave->pcm_buf = NULL;
|
wave->pcm_buf = NULL;
|
||||||
|
|
||||||
g_free (wave->hdrs);
|
qemu_free (wave->hdrs);
|
||||||
wave->hdrs = NULL;
|
wave->hdrs = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
common-obj-y += rng.o rng-egd.o
|
|
||||||
common-obj-$(CONFIG_POSIX) += rng-random.o
|
|
||||||
|
|
||||||
common-obj-y += msmouse.o testdev.o
|
|
||||||
common-obj-$(CONFIG_BRLAPI) += baum.o
|
|
||||||
baum.o-cflags := $(SDL_CFLAGS)
|
|
||||||
|
|
||||||
common-obj-$(CONFIG_TPM) += tpm.o
|
|
||||||
|
|
||||||
common-obj-y += hostmem.o hostmem-ram.o
|
|
||||||
common-obj-$(CONFIG_LINUX) += hostmem-file.o
|
|
||||||
@@ -1,134 +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-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 (!memory_region_size(&backend->mr)) {
|
|
||||||
backend->force_prealloc = mem_prealloc;
|
|
||||||
memory_region_init_ram_from_file(&backend->mr, OBJECT(backend),
|
|
||||||
object_get_canonical_path(OBJECT(backend)),
|
|
||||||
backend->size, fb->share,
|
|
||||||
fb->mem_path, errp);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
file_backend_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
HostMemoryBackendClass *bc = MEMORY_BACKEND_CLASS(oc);
|
|
||||||
|
|
||||||
bc->alloc = file_backend_memory_alloc;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 (memory_region_size(&backend->mr)) {
|
|
||||||
error_setg(errp, "cannot change property value");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (fb->mem_path) {
|
|
||||||
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 (memory_region_size(&backend->mr)) {
|
|
||||||
error_setg(errp, "cannot change property value");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
fb->share = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
file_backend_instance_init(Object *o)
|
|
||||||
{
|
|
||||||
object_property_add_bool(o, "share",
|
|
||||||
file_memory_backend_get_share,
|
|
||||||
file_memory_backend_set_share, NULL);
|
|
||||||
object_property_add_str(o, "mem-path", get_mem_path,
|
|
||||||
set_mem_path, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo file_backend_info = {
|
|
||||||
.name = TYPE_MEMORY_BACKEND_FILE,
|
|
||||||
.parent = TYPE_MEMORY_BACKEND,
|
|
||||||
.class_init = file_backend_class_init,
|
|
||||||
.instance_init = file_backend_instance_init,
|
|
||||||
.instance_size = sizeof(HostMemoryBackendFile),
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&file_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -1,53 +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 "sysemu/hostmem.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,365 +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 "sysemu/hostmem.h"
|
|
||||||
#include "qapi/visitor.h"
|
|
||||||
#include "qapi-types.h"
|
|
||||||
#include "qapi-visit.h"
|
|
||||||
#include "qapi/qmp/qerror.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, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
uint64_t value = backend->size;
|
|
||||||
|
|
||||||
visit_type_size(v, &value, name, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_set_size(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
Error *local_err = NULL;
|
|
||||||
uint64_t value;
|
|
||||||
|
|
||||||
if (memory_region_size(&backend->mr)) {
|
|
||||||
error_setg(&local_err, "cannot change property value");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
visit_type_size(v, &value, name, &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, void *opaque,
|
|
||||||
const char *name, 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, &host_nodes, name, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_set_host_nodes(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
#ifdef CONFIG_NUMA
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
uint16List *l = NULL;
|
|
||||||
|
|
||||||
visit_type_uint16List(v, &l, name, 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 void
|
|
||||||
host_memory_backend_get_policy(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
int policy = backend->policy;
|
|
||||||
|
|
||||||
visit_type_enum(v, &policy, HostMemPolicy_lookup, NULL, name, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_set_policy(Object *obj, Visitor *v, void *opaque,
|
|
||||||
const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
int policy;
|
|
||||||
|
|
||||||
visit_type_enum(v, &policy, HostMemPolicy_lookup, NULL, name, errp);
|
|
||||||
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 (!memory_region_size(&backend->mr)) {
|
|
||||||
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 (!memory_region_size(&backend->mr)) {
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
if (backend->force_prealloc) {
|
|
||||||
if (value) {
|
|
||||||
error_setg(errp,
|
|
||||||
"remove -mem-prealloc to use the prealloc property");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!memory_region_size(&backend->mr)) {
|
|
||||||
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);
|
|
||||||
backend->prealloc = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void host_memory_backend_init(Object *obj)
|
|
||||||
{
|
|
||||||
HostMemoryBackend *backend = MEMORY_BACKEND(obj);
|
|
||||||
|
|
||||||
backend->merge = qemu_opt_get_bool(qemu_get_machine_opts(),
|
|
||||||
"mem-merge", true);
|
|
||||||
backend->dump = qemu_opt_get_bool(qemu_get_machine_opts(),
|
|
||||||
"dump-guest-core", true);
|
|
||||||
backend->prealloc = mem_prealloc;
|
|
||||||
|
|
||||||
object_property_add_bool(obj, "merge",
|
|
||||||
host_memory_backend_get_merge,
|
|
||||||
host_memory_backend_set_merge, NULL);
|
|
||||||
object_property_add_bool(obj, "dump",
|
|
||||||
host_memory_backend_get_dump,
|
|
||||||
host_memory_backend_set_dump, NULL);
|
|
||||||
object_property_add_bool(obj, "prealloc",
|
|
||||||
host_memory_backend_get_prealloc,
|
|
||||||
host_memory_backend_set_prealloc, NULL);
|
|
||||||
object_property_add(obj, "size", "int",
|
|
||||||
host_memory_backend_get_size,
|
|
||||||
host_memory_backend_set_size, NULL, NULL, NULL);
|
|
||||||
object_property_add(obj, "host-nodes", "int",
|
|
||||||
host_memory_backend_get_host_nodes,
|
|
||||||
host_memory_backend_set_host_nodes, NULL, NULL, NULL);
|
|
||||||
object_property_add(obj, "policy", "str",
|
|
||||||
host_memory_backend_get_policy,
|
|
||||||
host_memory_backend_set_policy, NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
MemoryRegion *
|
|
||||||
host_memory_backend_get_memory(HostMemoryBackend *backend, Error **errp)
|
|
||||||
{
|
|
||||||
return memory_region_size(&backend->mr) ? &backend->mr : NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
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)) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
host_memory_backend_class_init(ObjectClass *oc, void *data)
|
|
||||||
{
|
|
||||||
UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
|
|
||||||
|
|
||||||
ucc->complete = host_memory_backend_memory_complete;
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
.interfaces = (InterfaceInfo[]) {
|
|
||||||
{ TYPE_USER_CREATABLE },
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&host_memory_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -1,232 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Random Number Generator Backend
|
|
||||||
*
|
|
||||||
* Copyright IBM, Corp. 2012
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Anthony Liguori <aliguori@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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "sysemu/rng.h"
|
|
||||||
#include "sysemu/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)
|
|
||||||
|
|
||||||
typedef struct RngEgd
|
|
||||||
{
|
|
||||||
RngBackend parent;
|
|
||||||
|
|
||||||
CharDriverState *chr;
|
|
||||||
char *chr_name;
|
|
||||||
|
|
||||||
GSList *requests;
|
|
||||||
} RngEgd;
|
|
||||||
|
|
||||||
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);
|
|
||||||
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];
|
|
||||||
uint8_t len = MIN(size, 255);
|
|
||||||
|
|
||||||
/* synchronous entropy request */
|
|
||||||
header[0] = 0x02;
|
|
||||||
header[1] = len;
|
|
||||||
|
|
||||||
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);
|
|
||||||
GSList *i;
|
|
||||||
int size = 0;
|
|
||||||
|
|
||||||
for (i = s->requests; i; i = i->next) {
|
|
||||||
RngRequest *req = i->data;
|
|
||||||
size += req->size - req->offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
return size;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 && 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;
|
|
||||||
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_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);
|
|
||||||
|
|
||||||
if (s->chr_name == NULL) {
|
|
||||||
error_set(errp, QERR_INVALID_PARAMETER_VALUE,
|
|
||||||
"chardev", "a valid character device");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->chr = qemu_chr_find(s->chr_name);
|
|
||||||
if (s->chr == NULL) {
|
|
||||||
error_set(errp, QERR_DEVICE_NOT_FOUND, s->chr_name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (qemu_chr_fe_claim(s->chr) != 0) {
|
|
||||||
error_set(errp, QERR_DEVICE_IN_USE, s->chr_name);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* FIXME we should resubmit pending requests when the CDS reconnects. */
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
RngBackend *b = RNG_BACKEND(obj);
|
|
||||||
RngEgd *s = RNG_EGD(b);
|
|
||||||
|
|
||||||
if (b->opened) {
|
|
||||||
error_set(errp, QERR_PERMISSION_DENIED);
|
|
||||||
} else {
|
|
||||||
g_free(s->chr_name);
|
|
||||||
s->chr_name = g_strdup(value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *rng_egd_get_chardev(Object *obj, Error **errp)
|
|
||||||
{
|
|
||||||
RngEgd *s = RNG_EGD(obj);
|
|
||||||
|
|
||||||
if (s->chr && s->chr->label) {
|
|
||||||
return g_strdup(s->chr->label);
|
|
||||||
}
|
|
||||||
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_egd_init(Object *obj)
|
|
||||||
{
|
|
||||||
object_property_add_str(obj, "chardev",
|
|
||||||
rng_egd_get_chardev, rng_egd_set_chardev,
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_egd_finalize(Object *obj)
|
|
||||||
{
|
|
||||||
RngEgd *s = RNG_EGD(obj);
|
|
||||||
|
|
||||||
if (s->chr) {
|
|
||||||
qemu_chr_add_handlers(s->chr, NULL, NULL, NULL, NULL);
|
|
||||||
qemu_chr_fe_release(s->chr);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(s->chr_name);
|
|
||||||
|
|
||||||
rng_egd_free_requests(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo rng_egd_info = {
|
|
||||||
.name = TYPE_RNG_EGD,
|
|
||||||
.parent = TYPE_RNG_BACKEND,
|
|
||||||
.instance_size = sizeof(RngEgd),
|
|
||||||
.class_init = rng_egd_class_init,
|
|
||||||
.instance_init = rng_egd_init,
|
|
||||||
.instance_finalize = rng_egd_finalize,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&rng_egd_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -1,156 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Random Number Generator Backend
|
|
||||||
*
|
|
||||||
* Copyright IBM, Corp. 2012
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Anthony Liguori <aliguori@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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "sysemu/rng-random.h"
|
|
||||||
#include "sysemu/rng.h"
|
|
||||||
#include "qapi/qmp/qerror.h"
|
|
||||||
#include "qemu/main-loop.h"
|
|
||||||
|
|
||||||
struct RndRandom
|
|
||||||
{
|
|
||||||
RngBackend parent;
|
|
||||||
|
|
||||||
int fd;
|
|
||||||
char *filename;
|
|
||||||
|
|
||||||
EntropyReceiveFunc *receive_func;
|
|
||||||
void *opaque;
|
|
||||||
size_t size;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A simple and incomplete backend to request entropy from /dev/random.
|
|
||||||
*
|
|
||||||
* This backend exposes an additional "filename" property that can be used to
|
|
||||||
* set the filename to use to open the backend.
|
|
||||||
*/
|
|
||||||
|
|
||||||
static void entropy_available(void *opaque)
|
|
||||||
{
|
|
||||||
RndRandom *s = RNG_RANDOM(opaque);
|
|
||||||
uint8_t buffer[s->size];
|
|
||||||
ssize_t len;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_random_request_entropy(RngBackend *b, size_t size,
|
|
||||||
EntropyReceiveFunc *receive_entropy,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
RndRandom *s = RNG_RANDOM(b);
|
|
||||||
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
RndRandom *s = RNG_RANDOM(b);
|
|
||||||
|
|
||||||
if (s->filename == NULL) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static char *rng_random_get_filename(Object *obj, Error **errp)
|
|
||||||
{
|
|
||||||
RndRandom *s = RNG_RANDOM(obj);
|
|
||||||
|
|
||||||
return g_strdup(s->filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_random_set_filename(Object *obj, const char *filename,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
RngBackend *b = RNG_BACKEND(obj);
|
|
||||||
RndRandom *s = RNG_RANDOM(obj);
|
|
||||||
|
|
||||||
if (b->opened) {
|
|
||||||
error_set(errp, QERR_PERMISSION_DENIED);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(s->filename);
|
|
||||||
s->filename = g_strdup(filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_random_init(Object *obj)
|
|
||||||
{
|
|
||||||
RndRandom *s = RNG_RANDOM(obj);
|
|
||||||
|
|
||||||
object_property_add_str(obj, "filename",
|
|
||||||
rng_random_get_filename,
|
|
||||||
rng_random_set_filename,
|
|
||||||
NULL);
|
|
||||||
|
|
||||||
s->filename = g_strdup("/dev/random");
|
|
||||||
s->fd = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_random_finalize(Object *obj)
|
|
||||||
{
|
|
||||||
RndRandom *s = RNG_RANDOM(obj);
|
|
||||||
|
|
||||||
if (s->fd != -1) {
|
|
||||||
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
|
|
||||||
qemu_close(s->fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(s->filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_random_class_init(ObjectClass *klass, void *data)
|
|
||||||
{
|
|
||||||
RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
|
|
||||||
|
|
||||||
rbc->request_entropy = rng_random_request_entropy;
|
|
||||||
rbc->opened = rng_random_opened;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const TypeInfo rng_random_info = {
|
|
||||||
.name = TYPE_RNG_RANDOM,
|
|
||||||
.parent = TYPE_RNG_BACKEND,
|
|
||||||
.instance_size = sizeof(RndRandom),
|
|
||||||
.class_init = rng_random_class_init,
|
|
||||||
.instance_init = rng_random_init,
|
|
||||||
.instance_finalize = rng_random_finalize,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&rng_random_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
109
backends/rng.c
109
backends/rng.c
@@ -1,109 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Random Number Generator Backend
|
|
||||||
*
|
|
||||||
* Copyright IBM, Corp. 2012
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Anthony Liguori <aliguori@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.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "sysemu/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);
|
|
||||||
|
|
||||||
if (k->request_entropy) {
|
|
||||||
k->request_entropy(s, size, receive_entropy, opaque);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void rng_backend_cancel_requests(RngBackend *s)
|
|
||||||
{
|
|
||||||
RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
|
|
||||||
|
|
||||||
if (k->cancel_requests) {
|
|
||||||
k->cancel_requests(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
|
|
||||||
{
|
|
||||||
RngBackend *s = RNG_BACKEND(obj);
|
|
||||||
|
|
||||||
return s->opened;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void rng_backend_complete(UserCreatable *uc, Error **errp)
|
|
||||||
{
|
|
||||||
object_property_set_bool(OBJECT(uc), 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_set(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 rng_backend_init(Object *obj)
|
|
||||||
{
|
|
||||||
object_property_add_bool(obj, "opened",
|
|
||||||
rng_backend_prop_get_opened,
|
|
||||||
rng_backend_prop_set_opened,
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
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,
|
|
||||||
.class_size = sizeof(RngBackendClass),
|
|
||||||
.class_init = rng_backend_class_init,
|
|
||||||
.abstract = true,
|
|
||||||
.interfaces = (InterfaceInfo[]) {
|
|
||||||
{ TYPE_USER_CREATABLE },
|
|
||||||
{ }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
type_register_static(&rng_backend_info);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
@@ -1,131 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Char Device for testsuite control
|
|
||||||
*
|
|
||||||
* Copyright (c) 2014 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* Author: Paolo Bonzini <pbonzini@redhat.com>
|
|
||||||
*
|
|
||||||
* 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 "sysemu/char.h"
|
|
||||||
|
|
||||||
#define BUF_SIZE 32
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
CharDriverState *chr;
|
|
||||||
uint8_t in_buf[32];
|
|
||||||
int in_buf_used;
|
|
||||||
} TestdevCharState;
|
|
||||||
|
|
||||||
/* Try to interpret a whole incoming packet */
|
|
||||||
static int testdev_eat_packet(TestdevCharState *testdev)
|
|
||||||
{
|
|
||||||
const uint8_t *cur = testdev->in_buf;
|
|
||||||
int len = testdev->in_buf_used;
|
|
||||||
uint8_t c;
|
|
||||||
int arg;
|
|
||||||
|
|
||||||
#define EAT(c) do { \
|
|
||||||
if (!len--) { \
|
|
||||||
return 0; \
|
|
||||||
} \
|
|
||||||
c = *cur++; \
|
|
||||||
} while (0)
|
|
||||||
|
|
||||||
EAT(c);
|
|
||||||
|
|
||||||
while (isspace(c)) {
|
|
||||||
EAT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
arg = 0;
|
|
||||||
while (isdigit(c)) {
|
|
||||||
arg = arg * 10 + c - '0';
|
|
||||||
EAT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (isspace(c)) {
|
|
||||||
EAT(c);
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (c) {
|
|
||||||
case 'q':
|
|
||||||
exit((arg << 1) | 1);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return cur - testdev->in_buf;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The other end is writing some data. Store it and try to interpret */
|
|
||||||
static int testdev_write(CharDriverState *chr, const uint8_t *buf, int len)
|
|
||||||
{
|
|
||||||
TestdevCharState *testdev = chr->opaque;
|
|
||||||
int tocopy, eaten, orig_len = len;
|
|
||||||
|
|
||||||
while (len) {
|
|
||||||
/* Complete our buffer as much as possible */
|
|
||||||
tocopy = MIN(len, BUF_SIZE - testdev->in_buf_used);
|
|
||||||
|
|
||||||
memcpy(testdev->in_buf + testdev->in_buf_used, buf, tocopy);
|
|
||||||
testdev->in_buf_used += tocopy;
|
|
||||||
buf += tocopy;
|
|
||||||
len -= tocopy;
|
|
||||||
|
|
||||||
/* Interpret it as much as possible */
|
|
||||||
while (testdev->in_buf_used > 0 &&
|
|
||||||
(eaten = testdev_eat_packet(testdev)) > 0) {
|
|
||||||
memmove(testdev->in_buf, testdev->in_buf + eaten,
|
|
||||||
testdev->in_buf_used - eaten);
|
|
||||||
testdev->in_buf_used -= eaten;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return orig_len;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void testdev_close(struct CharDriverState *chr)
|
|
||||||
{
|
|
||||||
TestdevCharState *testdev = chr->opaque;
|
|
||||||
|
|
||||||
g_free(testdev);
|
|
||||||
}
|
|
||||||
|
|
||||||
CharDriverState *chr_testdev_init(void)
|
|
||||||
{
|
|
||||||
TestdevCharState *testdev;
|
|
||||||
CharDriverState *chr;
|
|
||||||
|
|
||||||
testdev = g_malloc0(sizeof(TestdevCharState));
|
|
||||||
testdev->chr = chr = g_malloc0(sizeof(CharDriverState));
|
|
||||||
|
|
||||||
chr->opaque = testdev;
|
|
||||||
chr->chr_write = testdev_write;
|
|
||||||
chr->chr_close = testdev_close;
|
|
||||||
|
|
||||||
return chr;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void register_types(void)
|
|
||||||
{
|
|
||||||
register_char_driver("testdev", CHARDEV_BACKEND_KIND_TESTDEV, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
type_init(register_types);
|
|
||||||
193
backends/tpm.c
193
backends/tpm.c
@@ -1,193 +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 "sysemu/tpm_backend.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);
|
|
||||||
|
|
||||||
return 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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_set(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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void tpm_backend_thread_tpm_reset(TPMBackendThread *tbt,
|
|
||||||
GFunc func, gpointer user_data)
|
|
||||||
{
|
|
||||||
if (!tbt->pool) {
|
|
||||||
tpm_backend_thread_create(tbt, func, user_data);
|
|
||||||
} else {
|
|
||||||
g_thread_pool_push(tbt->pool, (gpointer)TPM_BACKEND_CMD_TPM_RESET,
|
|
||||||
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);
|
|
||||||
164
balloon.c
164
balloon.c
@@ -24,33 +24,18 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "monitor/monitor.h"
|
#include "monitor.h"
|
||||||
#include "exec/cpu-common.h"
|
#include "qjson.h"
|
||||||
#include "sysemu/kvm.h"
|
#include "qint.h"
|
||||||
#include "sysemu/balloon.h"
|
#include "cpu-common.h"
|
||||||
|
#include "kvm.h"
|
||||||
|
#include "balloon.h"
|
||||||
#include "trace.h"
|
#include "trace.h"
|
||||||
#include "qmp-commands.h"
|
|
||||||
#include "qapi/qmp/qjson.h"
|
|
||||||
|
|
||||||
static QEMUBalloonEvent *balloon_event_fn;
|
static QEMUBalloonEvent *balloon_event_fn;
|
||||||
static QEMUBalloonStatus *balloon_stat_fn;
|
static QEMUBalloonStatus *balloon_stat_fn;
|
||||||
static void *balloon_opaque;
|
static void *balloon_opaque;
|
||||||
|
|
||||||
static bool have_ballon(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,
|
int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
||||||
QEMUBalloonStatus *stat_func, void *opaque)
|
QEMUBalloonStatus *stat_func, void *opaque)
|
||||||
{
|
{
|
||||||
@@ -67,40 +52,113 @@ int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void qemu_remove_balloon_handler(void *opaque)
|
static int qemu_balloon(ram_addr_t target)
|
||||||
{
|
{
|
||||||
if (balloon_opaque != opaque) {
|
if (!balloon_event_fn) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
balloon_event_fn = NULL;
|
|
||||||
balloon_stat_fn = NULL;
|
|
||||||
balloon_opaque = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
BalloonInfo *qmp_query_balloon(Error **errp)
|
|
||||||
{
|
|
||||||
BalloonInfo *info;
|
|
||||||
|
|
||||||
if (!have_ballon(errp)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
info = g_malloc0(sizeof(*info));
|
|
||||||
balloon_stat_fn(balloon_opaque, info);
|
|
||||||
return info;
|
|
||||||
}
|
|
||||||
|
|
||||||
void qmp_balloon(int64_t target, Error **errp)
|
|
||||||
{
|
|
||||||
if (!have_ballon(errp)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (target <= 0) {
|
|
||||||
error_set(errp, QERR_INVALID_PARAMETER_VALUE, "target", "a size");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
trace_balloon_event(balloon_opaque, target);
|
trace_balloon_event(balloon_opaque, target);
|
||||||
balloon_event_fn(balloon_opaque, target);
|
balloon_event_fn(balloon_opaque, target);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int qemu_balloon_status(MonitorCompletion cb, void *opaque)
|
||||||
|
{
|
||||||
|
if (!balloon_stat_fn) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
balloon_stat_fn(balloon_opaque, cb, opaque);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void print_balloon_stat(const char *key, QObject *obj, void *opaque)
|
||||||
|
{
|
||||||
|
Monitor *mon = opaque;
|
||||||
|
|
||||||
|
if (strcmp(key, "actual")) {
|
||||||
|
monitor_printf(mon, ",%s=%" PRId64, key,
|
||||||
|
qint_get_int(qobject_to_qint(obj)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void monitor_print_balloon(Monitor *mon, const QObject *data)
|
||||||
|
{
|
||||||
|
QDict *qdict;
|
||||||
|
|
||||||
|
qdict = qobject_to_qdict(data);
|
||||||
|
if (!qdict_haskey(qdict, "actual")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
monitor_printf(mon, "balloon: actual=%" PRId64,
|
||||||
|
qdict_get_int(qdict, "actual") >> 20);
|
||||||
|
qdict_iter(qdict, print_balloon_stat, mon);
|
||||||
|
monitor_printf(mon, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* do_info_balloon(): Balloon information
|
||||||
|
*
|
||||||
|
* Make an asynchronous request for balloon info. When the request completes
|
||||||
|
* a QDict will be returned according to the following specification:
|
||||||
|
*
|
||||||
|
* - "actual": current balloon value in bytes
|
||||||
|
* The following fields may or may not be present:
|
||||||
|
* - "mem_swapped_in": Amount of memory swapped in (bytes)
|
||||||
|
* - "mem_swapped_out": Amount of memory swapped out (bytes)
|
||||||
|
* - "major_page_faults": Number of major faults
|
||||||
|
* - "minor_page_faults": Number of minor faults
|
||||||
|
* - "free_mem": Total amount of free and unused memory (bytes)
|
||||||
|
* - "total_mem": Total amount of available memory (bytes)
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* { "actual": 1073741824, "mem_swapped_in": 0, "mem_swapped_out": 0,
|
||||||
|
* "major_page_faults": 142, "minor_page_faults": 239245,
|
||||||
|
* "free_mem": 1014185984, "total_mem": 1044668416 }
|
||||||
|
*/
|
||||||
|
int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque)
|
||||||
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
||||||
|
qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = qemu_balloon_status(cb, opaque);
|
||||||
|
if (!ret) {
|
||||||
|
qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* do_balloon(): Request VM to change its memory allocation
|
||||||
|
*/
|
||||||
|
int do_balloon(Monitor *mon, const QDict *params,
|
||||||
|
MonitorCompletion cb, void *opaque)
|
||||||
|
{
|
||||||
|
int64_t target;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
if (kvm_enabled() && !kvm_has_sync_mmu()) {
|
||||||
|
qerror_report(QERR_KVM_MISSING_CAP, "synchronous MMU", "balloon");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
target = qdict_get_int(params, "value");
|
||||||
|
if (target <= 0) {
|
||||||
|
qerror_report(QERR_INVALID_PARAMETER_VALUE, "target", "a size");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
ret = qemu_balloon(target);
|
||||||
|
if (ret == 0) {
|
||||||
|
qerror_report(QERR_DEVICE_NOT_ACTIVE, "balloon");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
cb(opaque, NULL);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
31
balloon.h
Normal file
31
balloon.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
/*
|
||||||
|
* Balloon
|
||||||
|
*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _QEMU_BALLOON_H
|
||||||
|
#define _QEMU_BALLOON_H
|
||||||
|
|
||||||
|
#include "monitor.h"
|
||||||
|
|
||||||
|
typedef void (QEMUBalloonEvent)(void *opaque, ram_addr_t target);
|
||||||
|
typedef void (QEMUBalloonStatus)(void *opaque, MonitorCompletion cb,
|
||||||
|
void *cb_data);
|
||||||
|
|
||||||
|
int qemu_add_balloon_handler(QEMUBalloonEvent *event_func,
|
||||||
|
QEMUBalloonStatus *stat_func, void *opaque);
|
||||||
|
|
||||||
|
void monitor_print_balloon(Monitor *mon, const QObject *data);
|
||||||
|
int do_info_balloon(Monitor *mon, MonitorCompletion cb, void *opaque);
|
||||||
|
int do_balloon(Monitor *mon, const QDict *params,
|
||||||
|
MonitorCompletion cb, void *opaque);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -9,8 +9,8 @@
|
|||||||
* Version 2.
|
* Version 2.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qemu/bitops.h"
|
#include "bitops.h"
|
||||||
#include "qemu/bitmap.h"
|
#include "bitmap.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* bitmaps provide an array of bits, implemented using an an
|
* bitmaps provide an array of bits, implemented using an an
|
||||||
@@ -36,9 +36,9 @@
|
|||||||
* endian architectures.
|
* endian architectures.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int slow_bitmap_empty(const unsigned long *bitmap, long bits)
|
int slow_bitmap_empty(const unsigned long *bitmap, int bits)
|
||||||
{
|
{
|
||||||
long k, lim = bits/BITS_PER_LONG;
|
int k, lim = bits/BITS_PER_LONG;
|
||||||
|
|
||||||
for (k = 0; k < lim; ++k) {
|
for (k = 0; k < lim; ++k) {
|
||||||
if (bitmap[k]) {
|
if (bitmap[k]) {
|
||||||
@@ -54,9 +54,9 @@ int slow_bitmap_empty(const unsigned long *bitmap, long bits)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int slow_bitmap_full(const unsigned long *bitmap, long bits)
|
int slow_bitmap_full(const unsigned long *bitmap, int bits)
|
||||||
{
|
{
|
||||||
long k, lim = bits/BITS_PER_LONG;
|
int k, lim = bits/BITS_PER_LONG;
|
||||||
|
|
||||||
for (k = 0; k < lim; ++k) {
|
for (k = 0; k < lim; ++k) {
|
||||||
if (~bitmap[k]) {
|
if (~bitmap[k]) {
|
||||||
@@ -74,9 +74,9 @@ int slow_bitmap_full(const unsigned long *bitmap, long bits)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int slow_bitmap_equal(const unsigned long *bitmap1,
|
int slow_bitmap_equal(const unsigned long *bitmap1,
|
||||||
const unsigned long *bitmap2, long bits)
|
const unsigned long *bitmap2, int bits)
|
||||||
{
|
{
|
||||||
long k, lim = bits/BITS_PER_LONG;
|
int k, lim = bits/BITS_PER_LONG;
|
||||||
|
|
||||||
for (k = 0; k < lim; ++k) {
|
for (k = 0; k < lim; ++k) {
|
||||||
if (bitmap1[k] != bitmap2[k]) {
|
if (bitmap1[k] != bitmap2[k]) {
|
||||||
@@ -94,9 +94,9 @@ int slow_bitmap_equal(const unsigned long *bitmap1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
|
void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
|
||||||
long bits)
|
int bits)
|
||||||
{
|
{
|
||||||
long k, lim = bits/BITS_PER_LONG;
|
int k, lim = bits/BITS_PER_LONG;
|
||||||
|
|
||||||
for (k = 0; k < lim; ++k) {
|
for (k = 0; k < lim; ++k) {
|
||||||
dst[k] = ~src[k];
|
dst[k] = ~src[k];
|
||||||
@@ -108,10 +108,10 @@ void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
const unsigned long *bitmap2, long bits)
|
const unsigned long *bitmap2, int bits)
|
||||||
{
|
{
|
||||||
long k;
|
int k;
|
||||||
long nr = BITS_TO_LONGS(bits);
|
int nr = BITS_TO_LONGS(bits);
|
||||||
unsigned long result = 0;
|
unsigned long result = 0;
|
||||||
|
|
||||||
for (k = 0; k < nr; k++) {
|
for (k = 0; k < nr; k++) {
|
||||||
@@ -121,10 +121,10 @@ int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
const unsigned long *bitmap2, long bits)
|
const unsigned long *bitmap2, int bits)
|
||||||
{
|
{
|
||||||
long k;
|
int k;
|
||||||
long nr = BITS_TO_LONGS(bits);
|
int nr = BITS_TO_LONGS(bits);
|
||||||
|
|
||||||
for (k = 0; k < nr; k++) {
|
for (k = 0; k < nr; k++) {
|
||||||
dst[k] = bitmap1[k] | bitmap2[k];
|
dst[k] = bitmap1[k] | bitmap2[k];
|
||||||
@@ -132,10 +132,10 @@ void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
const unsigned long *bitmap2, long bits)
|
const unsigned long *bitmap2, int bits)
|
||||||
{
|
{
|
||||||
long k;
|
int k;
|
||||||
long nr = BITS_TO_LONGS(bits);
|
int nr = BITS_TO_LONGS(bits);
|
||||||
|
|
||||||
for (k = 0; k < nr; k++) {
|
for (k = 0; k < nr; k++) {
|
||||||
dst[k] = bitmap1[k] ^ bitmap2[k];
|
dst[k] = bitmap1[k] ^ bitmap2[k];
|
||||||
@@ -143,10 +143,10 @@ void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
const unsigned long *bitmap2, long bits)
|
const unsigned long *bitmap2, int bits)
|
||||||
{
|
{
|
||||||
long k;
|
int k;
|
||||||
long nr = BITS_TO_LONGS(bits);
|
int nr = BITS_TO_LONGS(bits);
|
||||||
unsigned long result = 0;
|
unsigned long result = 0;
|
||||||
|
|
||||||
for (k = 0; k < nr; k++) {
|
for (k = 0; k < nr; k++) {
|
||||||
@@ -157,10 +157,10 @@ int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
|||||||
|
|
||||||
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
|
#define BITMAP_FIRST_WORD_MASK(start) (~0UL << ((start) % BITS_PER_LONG))
|
||||||
|
|
||||||
void bitmap_set(unsigned long *map, long start, long nr)
|
void bitmap_set(unsigned long *map, int start, int nr)
|
||||||
{
|
{
|
||||||
unsigned long *p = map + BIT_WORD(start);
|
unsigned long *p = map + BIT_WORD(start);
|
||||||
const long size = start + nr;
|
const int size = start + nr;
|
||||||
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
|
int bits_to_set = BITS_PER_LONG - (start % BITS_PER_LONG);
|
||||||
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
|
unsigned long mask_to_set = BITMAP_FIRST_WORD_MASK(start);
|
||||||
|
|
||||||
@@ -177,10 +177,10 @@ void bitmap_set(unsigned long *map, long start, long nr)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void bitmap_clear(unsigned long *map, long start, long nr)
|
void bitmap_clear(unsigned long *map, int start, int nr)
|
||||||
{
|
{
|
||||||
unsigned long *p = map + BIT_WORD(start);
|
unsigned long *p = map + BIT_WORD(start);
|
||||||
const long size = start + nr;
|
const int size = start + nr;
|
||||||
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
|
int bits_to_clear = BITS_PER_LONG - (start % BITS_PER_LONG);
|
||||||
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
|
unsigned long mask_to_clear = BITMAP_FIRST_WORD_MASK(start);
|
||||||
|
|
||||||
@@ -214,7 +214,7 @@ void bitmap_clear(unsigned long *map, long start, long nr)
|
|||||||
unsigned long bitmap_find_next_zero_area(unsigned long *map,
|
unsigned long bitmap_find_next_zero_area(unsigned long *map,
|
||||||
unsigned long size,
|
unsigned long size,
|
||||||
unsigned long start,
|
unsigned long start,
|
||||||
unsigned long nr,
|
unsigned int nr,
|
||||||
unsigned long align_mask)
|
unsigned long align_mask)
|
||||||
{
|
{
|
||||||
unsigned long index, end, i;
|
unsigned long index, end, i;
|
||||||
@@ -237,9 +237,9 @@ again:
|
|||||||
}
|
}
|
||||||
|
|
||||||
int slow_bitmap_intersects(const unsigned long *bitmap1,
|
int slow_bitmap_intersects(const unsigned long *bitmap1,
|
||||||
const unsigned long *bitmap2, long bits)
|
const unsigned long *bitmap2, int bits)
|
||||||
{
|
{
|
||||||
long k, lim = bits/BITS_PER_LONG;
|
int k, lim = bits/BITS_PER_LONG;
|
||||||
|
|
||||||
for (k = 0; k < lim; ++k) {
|
for (k = 0; k < lim; ++k) {
|
||||||
if (bitmap1[k] & bitmap2[k]) {
|
if (bitmap1[k] & bitmap2[k]) {
|
||||||
222
bitmap.h
Normal file
222
bitmap.h
Normal file
@@ -0,0 +1,222 @@
|
|||||||
|
/*
|
||||||
|
* Bitmap Module
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
|
||||||
|
*
|
||||||
|
* Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
|
||||||
|
*
|
||||||
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
||||||
|
* See the COPYING.LIB file in the top-level directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BITMAP_H
|
||||||
|
#define BITMAP_H
|
||||||
|
|
||||||
|
#include "qemu-common.h"
|
||||||
|
#include "bitops.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
* The available bitmap operations and their rough meaning in the
|
||||||
|
* case that the bitmap is a single unsigned long are thus:
|
||||||
|
*
|
||||||
|
* Note that nbits should be always a compile time evaluable constant.
|
||||||
|
* Otherwise many inlines will generate horrible code.
|
||||||
|
*
|
||||||
|
* bitmap_zero(dst, nbits) *dst = 0UL
|
||||||
|
* bitmap_fill(dst, nbits) *dst = ~0UL
|
||||||
|
* bitmap_copy(dst, src, nbits) *dst = *src
|
||||||
|
* bitmap_and(dst, src1, src2, nbits) *dst = *src1 & *src2
|
||||||
|
* bitmap_or(dst, src1, src2, nbits) *dst = *src1 | *src2
|
||||||
|
* bitmap_xor(dst, src1, src2, nbits) *dst = *src1 ^ *src2
|
||||||
|
* bitmap_andnot(dst, src1, src2, nbits) *dst = *src1 & ~(*src2)
|
||||||
|
* bitmap_complement(dst, src, nbits) *dst = ~(*src)
|
||||||
|
* bitmap_equal(src1, src2, nbits) Are *src1 and *src2 equal?
|
||||||
|
* bitmap_intersects(src1, src2, nbits) Do *src1 and *src2 overlap?
|
||||||
|
* bitmap_empty(src, nbits) Are all bits zero in *src?
|
||||||
|
* bitmap_full(src, nbits) Are all bits set in *src?
|
||||||
|
* bitmap_set(dst, pos, nbits) Set specified bit area
|
||||||
|
* bitmap_clear(dst, pos, nbits) Clear specified bit area
|
||||||
|
* bitmap_find_next_zero_area(buf, len, pos, n, mask) Find bit free area
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Also the following operations apply to bitmaps.
|
||||||
|
*
|
||||||
|
* set_bit(bit, addr) *addr |= bit
|
||||||
|
* clear_bit(bit, addr) *addr &= ~bit
|
||||||
|
* change_bit(bit, addr) *addr ^= bit
|
||||||
|
* test_bit(bit, addr) Is bit set in *addr?
|
||||||
|
* test_and_set_bit(bit, addr) Set bit and return old value
|
||||||
|
* test_and_clear_bit(bit, addr) Clear bit and return old value
|
||||||
|
* test_and_change_bit(bit, addr) Change bit and return old value
|
||||||
|
* find_first_zero_bit(addr, nbits) Position first zero bit in *addr
|
||||||
|
* find_first_bit(addr, nbits) Position first set bit in *addr
|
||||||
|
* find_next_zero_bit(addr, nbits, bit) Position next zero bit in *addr >= bit
|
||||||
|
* find_next_bit(addr, nbits, bit) Position next set bit in *addr >= bit
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define BITMAP_LAST_WORD_MASK(nbits) \
|
||||||
|
( \
|
||||||
|
((nbits) % BITS_PER_LONG) ? \
|
||||||
|
(1UL<<((nbits) % BITS_PER_LONG))-1 : ~0UL \
|
||||||
|
)
|
||||||
|
|
||||||
|
#define DECLARE_BITMAP(name,bits) \
|
||||||
|
unsigned long name[BITS_TO_LONGS(bits)]
|
||||||
|
|
||||||
|
#define small_nbits(nbits) \
|
||||||
|
((nbits) <= BITS_PER_LONG)
|
||||||
|
|
||||||
|
int slow_bitmap_empty(const unsigned long *bitmap, int bits);
|
||||||
|
int slow_bitmap_full(const unsigned long *bitmap, int bits);
|
||||||
|
int slow_bitmap_equal(const unsigned long *bitmap1,
|
||||||
|
const unsigned long *bitmap2, int bits);
|
||||||
|
void slow_bitmap_complement(unsigned long *dst, const unsigned long *src,
|
||||||
|
int bits);
|
||||||
|
void slow_bitmap_shift_right(unsigned long *dst,
|
||||||
|
const unsigned long *src, int shift, int bits);
|
||||||
|
void slow_bitmap_shift_left(unsigned long *dst,
|
||||||
|
const unsigned long *src, int shift, int bits);
|
||||||
|
int slow_bitmap_and(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
|
const unsigned long *bitmap2, int bits);
|
||||||
|
void slow_bitmap_or(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
|
const unsigned long *bitmap2, int bits);
|
||||||
|
void slow_bitmap_xor(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
|
const unsigned long *bitmap2, int bits);
|
||||||
|
int slow_bitmap_andnot(unsigned long *dst, const unsigned long *bitmap1,
|
||||||
|
const unsigned long *bitmap2, int bits);
|
||||||
|
int slow_bitmap_intersects(const unsigned long *bitmap1,
|
||||||
|
const unsigned long *bitmap2, int bits);
|
||||||
|
|
||||||
|
static inline unsigned long *bitmap_new(int nbits)
|
||||||
|
{
|
||||||
|
int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||||
|
return qemu_mallocz(len);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_zero(unsigned long *dst, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
*dst = 0UL;
|
||||||
|
} else {
|
||||||
|
int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||||
|
memset(dst, 0, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_fill(unsigned long *dst, int nbits)
|
||||||
|
{
|
||||||
|
size_t nlongs = BITS_TO_LONGS(nbits);
|
||||||
|
if (!small_nbits(nbits)) {
|
||||||
|
int len = (nlongs - 1) * sizeof(unsigned long);
|
||||||
|
memset(dst, 0xff, len);
|
||||||
|
}
|
||||||
|
dst[nlongs - 1] = BITMAP_LAST_WORD_MASK(nbits);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_copy(unsigned long *dst, const unsigned long *src,
|
||||||
|
int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
*dst = *src;
|
||||||
|
} else {
|
||||||
|
int len = BITS_TO_LONGS(nbits) * sizeof(unsigned long);
|
||||||
|
memcpy(dst, src, len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_and(unsigned long *dst, const unsigned long *src1,
|
||||||
|
const unsigned long *src2, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
return (*dst = *src1 & *src2) != 0;
|
||||||
|
}
|
||||||
|
return slow_bitmap_and(dst, src1, src2, nbits);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_or(unsigned long *dst, const unsigned long *src1,
|
||||||
|
const unsigned long *src2, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
*dst = *src1 | *src2;
|
||||||
|
} else {
|
||||||
|
slow_bitmap_or(dst, src1, src2, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_xor(unsigned long *dst, const unsigned long *src1,
|
||||||
|
const unsigned long *src2, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
*dst = *src1 ^ *src2;
|
||||||
|
} else {
|
||||||
|
slow_bitmap_xor(dst, src1, src2, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_andnot(unsigned long *dst, const unsigned long *src1,
|
||||||
|
const unsigned long *src2, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
return (*dst = *src1 & ~(*src2)) != 0;
|
||||||
|
}
|
||||||
|
return slow_bitmap_andnot(dst, src1, src2, nbits);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void bitmap_complement(unsigned long *dst, const unsigned long *src,
|
||||||
|
int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
*dst = ~(*src) & BITMAP_LAST_WORD_MASK(nbits);
|
||||||
|
} else {
|
||||||
|
slow_bitmap_complement(dst, src, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_equal(const unsigned long *src1,
|
||||||
|
const unsigned long *src2, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
return ! ((*src1 ^ *src2) & BITMAP_LAST_WORD_MASK(nbits));
|
||||||
|
} else {
|
||||||
|
return slow_bitmap_equal(src1, src2, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_empty(const unsigned long *src, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
return ! (*src & BITMAP_LAST_WORD_MASK(nbits));
|
||||||
|
} else {
|
||||||
|
return slow_bitmap_empty(src, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_full(const unsigned long *src, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
return ! (~(*src) & BITMAP_LAST_WORD_MASK(nbits));
|
||||||
|
} else {
|
||||||
|
return slow_bitmap_full(src, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int bitmap_intersects(const unsigned long *src1,
|
||||||
|
const unsigned long *src2, int nbits)
|
||||||
|
{
|
||||||
|
if (small_nbits(nbits)) {
|
||||||
|
return ((*src1 & *src2) & BITMAP_LAST_WORD_MASK(nbits)) != 0;
|
||||||
|
} else {
|
||||||
|
return slow_bitmap_intersects(src1, src2, nbits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void bitmap_set(unsigned long *map, int i, int len);
|
||||||
|
void bitmap_clear(unsigned long *map, int start, int nr);
|
||||||
|
unsigned long bitmap_find_next_zero_area(unsigned long *map,
|
||||||
|
unsigned long size,
|
||||||
|
unsigned long start,
|
||||||
|
unsigned int nr,
|
||||||
|
unsigned long align_mask);
|
||||||
|
|
||||||
|
#endif /* BITMAP_H */
|
||||||
@@ -11,7 +11,7 @@
|
|||||||
* 2 of the License, or (at your option) any later version.
|
* 2 of the License, or (at your option) any later version.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qemu/bitops.h"
|
#include "bitops.h"
|
||||||
|
|
||||||
#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
|
#define BITOP_WORD(nr) ((nr) / BITS_PER_LONG)
|
||||||
|
|
||||||
@@ -42,23 +42,7 @@ unsigned long find_next_bit(const unsigned long *addr, unsigned long size,
|
|||||||
size -= BITS_PER_LONG;
|
size -= BITS_PER_LONG;
|
||||||
result += BITS_PER_LONG;
|
result += BITS_PER_LONG;
|
||||||
}
|
}
|
||||||
while (size >= 4*BITS_PER_LONG) {
|
while (size & ~(BITS_PER_LONG-1)) {
|
||||||
unsigned long d1, d2, d3;
|
|
||||||
tmp = *p;
|
|
||||||
d1 = *(p+1);
|
|
||||||
d2 = *(p+2);
|
|
||||||
d3 = *(p+3);
|
|
||||||
if (tmp) {
|
|
||||||
goto found_middle;
|
|
||||||
}
|
|
||||||
if (d1 | d2 | d3) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
p += 4;
|
|
||||||
result += 4*BITS_PER_LONG;
|
|
||||||
size -= 4*BITS_PER_LONG;
|
|
||||||
}
|
|
||||||
while (size >= BITS_PER_LONG) {
|
|
||||||
if ((tmp = *(p++))) {
|
if ((tmp = *(p++))) {
|
||||||
goto found_middle;
|
goto found_middle;
|
||||||
}
|
}
|
||||||
@@ -76,7 +60,7 @@ found_first:
|
|||||||
return result + size; /* Nope. */
|
return result + size; /* Nope. */
|
||||||
}
|
}
|
||||||
found_middle:
|
found_middle:
|
||||||
return result + ctzl(tmp);
|
return result + bitops_ffsl(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -125,7 +109,7 @@ found_first:
|
|||||||
return result + size; /* Nope. */
|
return result + size; /* Nope. */
|
||||||
}
|
}
|
||||||
found_middle:
|
found_middle:
|
||||||
return result + ctzl(~tmp);
|
return result + ffz(tmp);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
|
unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
|
||||||
@@ -149,7 +133,7 @@ unsigned long find_last_bit(const unsigned long *addr, unsigned long size)
|
|||||||
tmp = addr[--words];
|
tmp = addr[--words];
|
||||||
if (tmp) {
|
if (tmp) {
|
||||||
found:
|
found:
|
||||||
return words * BITS_PER_LONG + BITS_PER_LONG - 1 - clzl(tmp);
|
return words * BITS_PER_LONG + bitops_flsl(tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
272
bitops.h
Normal file
272
bitops.h
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
/*
|
||||||
|
* Bitops Module
|
||||||
|
*
|
||||||
|
* Copyright (C) 2010 Corentin Chary <corentin.chary@gmail.com>
|
||||||
|
*
|
||||||
|
* Mostly inspired by (stolen from) linux/bitmap.h and linux/bitops.h
|
||||||
|
*
|
||||||
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
||||||
|
* See the COPYING.LIB file in the top-level directory.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BITOPS_H
|
||||||
|
#define BITOPS_H
|
||||||
|
|
||||||
|
#include "qemu-common.h"
|
||||||
|
|
||||||
|
#define BITS_PER_BYTE CHAR_BIT
|
||||||
|
#define BITS_PER_LONG (sizeof (unsigned long) * BITS_PER_BYTE)
|
||||||
|
|
||||||
|
#define BIT(nr) (1UL << (nr))
|
||||||
|
#define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG))
|
||||||
|
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
|
||||||
|
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bitops_ffs - find first bit in word.
|
||||||
|
* @word: The word to search
|
||||||
|
*
|
||||||
|
* Undefined if no bit exists, so code should check against 0 first.
|
||||||
|
*/
|
||||||
|
static unsigned long bitops_ffsl(unsigned long word)
|
||||||
|
{
|
||||||
|
int num = 0;
|
||||||
|
|
||||||
|
#if LONG_MAX > 0x7FFFFFFF
|
||||||
|
if ((word & 0xffffffff) == 0) {
|
||||||
|
num += 32;
|
||||||
|
word >>= 32;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if ((word & 0xffff) == 0) {
|
||||||
|
num += 16;
|
||||||
|
word >>= 16;
|
||||||
|
}
|
||||||
|
if ((word & 0xff) == 0) {
|
||||||
|
num += 8;
|
||||||
|
word >>= 8;
|
||||||
|
}
|
||||||
|
if ((word & 0xf) == 0) {
|
||||||
|
num += 4;
|
||||||
|
word >>= 4;
|
||||||
|
}
|
||||||
|
if ((word & 0x3) == 0) {
|
||||||
|
num += 2;
|
||||||
|
word >>= 2;
|
||||||
|
}
|
||||||
|
if ((word & 0x1) == 0) {
|
||||||
|
num += 1;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* bitops_fls - find last (most-significant) set bit in a long word
|
||||||
|
* @word: the word to search
|
||||||
|
*
|
||||||
|
* Undefined if no set bit exists, so code should check against 0 first.
|
||||||
|
*/
|
||||||
|
static inline unsigned long bitops_flsl(unsigned long word)
|
||||||
|
{
|
||||||
|
int num = BITS_PER_LONG - 1;
|
||||||
|
|
||||||
|
#if LONG_MAX > 0x7FFFFFFF
|
||||||
|
if (!(word & (~0ul << 32))) {
|
||||||
|
num -= 32;
|
||||||
|
word <<= 32;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (!(word & (~0ul << (BITS_PER_LONG-16)))) {
|
||||||
|
num -= 16;
|
||||||
|
word <<= 16;
|
||||||
|
}
|
||||||
|
if (!(word & (~0ul << (BITS_PER_LONG-8)))) {
|
||||||
|
num -= 8;
|
||||||
|
word <<= 8;
|
||||||
|
}
|
||||||
|
if (!(word & (~0ul << (BITS_PER_LONG-4)))) {
|
||||||
|
num -= 4;
|
||||||
|
word <<= 4;
|
||||||
|
}
|
||||||
|
if (!(word & (~0ul << (BITS_PER_LONG-2)))) {
|
||||||
|
num -= 2;
|
||||||
|
|
||||||
|
word <<= 2;
|
||||||
|
}
|
||||||
|
if (!(word & (~0ul << (BITS_PER_LONG-1))))
|
||||||
|
num -= 1;
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ffz - find first zero in word.
|
||||||
|
* @word: The word to search
|
||||||
|
*
|
||||||
|
* Undefined if no zero exists, so code should check against ~0UL first.
|
||||||
|
*/
|
||||||
|
static inline unsigned long ffz(unsigned long word)
|
||||||
|
{
|
||||||
|
return bitops_ffsl(~word);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set_bit - Set a bit in memory
|
||||||
|
* @nr: the bit to set
|
||||||
|
* @addr: the address to start counting from
|
||||||
|
*/
|
||||||
|
static inline void set_bit(int nr, volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
unsigned long mask = BIT_MASK(nr);
|
||||||
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
||||||
|
|
||||||
|
*p |= mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* clear_bit - Clears a bit in memory
|
||||||
|
* @nr: Bit to clear
|
||||||
|
* @addr: Address to start counting from
|
||||||
|
*/
|
||||||
|
static inline void clear_bit(int nr, volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
unsigned long mask = BIT_MASK(nr);
|
||||||
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
||||||
|
|
||||||
|
*p &= ~mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* change_bit - Toggle a bit in memory
|
||||||
|
* @nr: Bit to change
|
||||||
|
* @addr: Address to start counting from
|
||||||
|
*/
|
||||||
|
static inline void change_bit(int nr, volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
unsigned long mask = BIT_MASK(nr);
|
||||||
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
||||||
|
|
||||||
|
*p ^= mask;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_and_set_bit - Set a bit and return its old value
|
||||||
|
* @nr: Bit to set
|
||||||
|
* @addr: Address to count from
|
||||||
|
*/
|
||||||
|
static inline int test_and_set_bit(int nr, volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
unsigned long mask = BIT_MASK(nr);
|
||||||
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
||||||
|
unsigned long old = *p;
|
||||||
|
|
||||||
|
*p = old | mask;
|
||||||
|
return (old & mask) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_and_clear_bit - Clear a bit and return its old value
|
||||||
|
* @nr: Bit to clear
|
||||||
|
* @addr: Address to count from
|
||||||
|
*/
|
||||||
|
static inline int test_and_clear_bit(int nr, volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
unsigned long mask = BIT_MASK(nr);
|
||||||
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
||||||
|
unsigned long old = *p;
|
||||||
|
|
||||||
|
*p = old & ~mask;
|
||||||
|
return (old & mask) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_and_change_bit - Change a bit and return its old value
|
||||||
|
* @nr: Bit to change
|
||||||
|
* @addr: Address to count from
|
||||||
|
*/
|
||||||
|
static inline int test_and_change_bit(int nr, volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
unsigned long mask = BIT_MASK(nr);
|
||||||
|
unsigned long *p = ((unsigned long *)addr) + BIT_WORD(nr);
|
||||||
|
unsigned long old = *p;
|
||||||
|
|
||||||
|
*p = old ^ mask;
|
||||||
|
return (old & mask) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* test_bit - Determine whether a bit is set
|
||||||
|
* @nr: bit number to test
|
||||||
|
* @addr: Address to start counting from
|
||||||
|
*/
|
||||||
|
static inline int test_bit(int nr, const volatile unsigned long *addr)
|
||||||
|
{
|
||||||
|
return 1UL & (addr[BIT_WORD(nr)] >> (nr & (BITS_PER_LONG-1)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find_last_bit - find the last set bit in a memory region
|
||||||
|
* @addr: The address to start the search at
|
||||||
|
* @size: The maximum size to search
|
||||||
|
*
|
||||||
|
* Returns the bit number of the first set bit, or size.
|
||||||
|
*/
|
||||||
|
unsigned long find_last_bit(const unsigned long *addr,
|
||||||
|
unsigned long size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find_next_bit - find the next set bit in a memory region
|
||||||
|
* @addr: The address to base the search on
|
||||||
|
* @offset: The bitnumber to start searching at
|
||||||
|
* @size: The bitmap size in bits
|
||||||
|
*/
|
||||||
|
unsigned long find_next_bit(const unsigned long *addr,
|
||||||
|
unsigned long size, unsigned long offset);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find_next_zero_bit - find the next cleared bit in a memory region
|
||||||
|
* @addr: The address to base the search on
|
||||||
|
* @offset: The bitnumber to start searching at
|
||||||
|
* @size: The bitmap size in bits
|
||||||
|
*/
|
||||||
|
|
||||||
|
unsigned long find_next_zero_bit(const unsigned long *addr,
|
||||||
|
unsigned long size,
|
||||||
|
unsigned long offset);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find_first_bit - find the first set bit in a memory region
|
||||||
|
* @addr: The address to start the search at
|
||||||
|
* @size: The maximum size to search
|
||||||
|
*
|
||||||
|
* Returns the bit number of the first set bit.
|
||||||
|
*/
|
||||||
|
static inline unsigned long find_first_bit(const unsigned long *addr,
|
||||||
|
unsigned long size)
|
||||||
|
{
|
||||||
|
return find_next_bit(addr, size, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* find_first_zero_bit - find the first cleared bit in a memory region
|
||||||
|
* @addr: The address to start the search at
|
||||||
|
* @size: The maximum size to search
|
||||||
|
*
|
||||||
|
* Returns the bit number of the first cleared bit.
|
||||||
|
*/
|
||||||
|
static inline unsigned long find_first_zero_bit(const unsigned long *addr,
|
||||||
|
unsigned long size)
|
||||||
|
{
|
||||||
|
return find_next_zero_bit(addr, size, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline unsigned long hweight_long(unsigned long w)
|
||||||
|
{
|
||||||
|
unsigned long count;
|
||||||
|
|
||||||
|
for (count = 0; w; w >>= 1) {
|
||||||
|
count += w & 1;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
731
block-migration.c
Normal file
731
block-migration.c
Normal file
@@ -0,0 +1,731 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "qemu-common.h"
|
||||||
|
#include "block_int.h"
|
||||||
|
#include "hw/hw.h"
|
||||||
|
#include "qemu-queue.h"
|
||||||
|
#include "qemu-timer.h"
|
||||||
|
#include "monitor.h"
|
||||||
|
#include "block-migration.h"
|
||||||
|
#include "migration.h"
|
||||||
|
#include "blockdev.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#define BLOCK_SIZE (BDRV_SECTORS_PER_DIRTY_CHUNK << 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 total_time;
|
||||||
|
long double prev_time_offset;
|
||||||
|
int reads;
|
||||||
|
} 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 inline long double compute_read_bwidth(void)
|
||||||
|
{
|
||||||
|
assert(block_mig_state.total_time != 0);
|
||||||
|
return (block_mig_state.reads / block_mig_state.total_time) * BLOCK_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 = qemu_mallocz(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.reads++;
|
||||||
|
block_mig_state.total_time += (curr_time - block_mig_state.prev_time_offset);
|
||||||
|
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(Monitor *mon, 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 = qemu_malloc(sizeof(BlkMigBlock));
|
||||||
|
blk->buf = qemu_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);
|
||||||
|
if (!blk->aiocb) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
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);
|
||||||
|
|
||||||
|
error:
|
||||||
|
monitor_printf(mon, "Error reading sector %" PRId64 "\n", cur_sector);
|
||||||
|
qemu_file_set_error(f);
|
||||||
|
qemu_free(blk->buf);
|
||||||
|
qemu_free(blk);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_blk_migration_it(void *opaque, BlockDriverState *bs)
|
||||||
|
{
|
||||||
|
Monitor *mon = opaque;
|
||||||
|
BlkMigDevState *bmds;
|
||||||
|
int64_t sectors;
|
||||||
|
|
||||||
|
if (!bdrv_is_read_only(bs)) {
|
||||||
|
sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
|
||||||
|
if (sectors <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bmds = qemu_mallocz(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) {
|
||||||
|
monitor_printf(mon, "Start migration for %s with shared base "
|
||||||
|
"image\n",
|
||||||
|
bs->device_name);
|
||||||
|
} else {
|
||||||
|
monitor_printf(mon, "Start full migration for %s\n",
|
||||||
|
bs->device_name);
|
||||||
|
}
|
||||||
|
|
||||||
|
QSIMPLEQ_INSERT_TAIL(&block_mig_state.bmds_list, bmds, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_blk_migration(Monitor *mon, 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;
|
||||||
|
block_mig_state.total_time = 0;
|
||||||
|
block_mig_state.reads = 0;
|
||||||
|
|
||||||
|
bdrv_iterate(init_blk_migration_it, mon);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int blk_mig_save_bulked_block(Monitor *mon, 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(mon, 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);
|
||||||
|
monitor_printf(mon, "Completed %d %%\r", progress);
|
||||||
|
monitor_flush(mon);
|
||||||
|
}
|
||||||
|
|
||||||
|
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(Monitor *mon, QEMUFile *f,
|
||||||
|
BlkMigDevState *bmds, int is_async)
|
||||||
|
{
|
||||||
|
BlkMigBlock *blk;
|
||||||
|
int64_t total_sectors = bmds->total_sectors;
|
||||||
|
int64_t sector;
|
||||||
|
int nr_sectors;
|
||||||
|
|
||||||
|
for (sector = bmds->cur_dirty; sector < bmds->total_sectors;) {
|
||||||
|
if (bmds_aio_inflight(bmds, sector)) {
|
||||||
|
qemu_aio_flush();
|
||||||
|
}
|
||||||
|
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 = qemu_malloc(sizeof(BlkMigBlock));
|
||||||
|
blk->buf = qemu_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);
|
||||||
|
if (!blk->aiocb) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
block_mig_state.submitted++;
|
||||||
|
bmds_set_aio_inflight(bmds, sector, nr_sectors, 1);
|
||||||
|
} else {
|
||||||
|
if (bdrv_read(bmds->bs, sector, blk->buf,
|
||||||
|
nr_sectors) < 0) {
|
||||||
|
goto error;
|
||||||
|
}
|
||||||
|
blk_send(f, blk);
|
||||||
|
|
||||||
|
qemu_free(blk->buf);
|
||||||
|
qemu_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:
|
||||||
|
monitor_printf(mon, "Error reading sector %" PRId64 "\n", sector);
|
||||||
|
qemu_file_set_error(f);
|
||||||
|
qemu_free(blk->buf);
|
||||||
|
qemu_free(blk);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int blk_mig_save_dirty_block(Monitor *mon, QEMUFile *f, int is_async)
|
||||||
|
{
|
||||||
|
BlkMigDevState *bmds;
|
||||||
|
int ret = 0;
|
||||||
|
|
||||||
|
QSIMPLEQ_FOREACH(bmds, &block_mig_state.bmds_list, entry) {
|
||||||
|
if (mig_save_device_dirty(mon, f, bmds, is_async) == 0) {
|
||||||
|
ret = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void flush_blks(QEMUFile* f)
|
||||||
|
{
|
||||||
|
BlkMigBlock *blk;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
qemu_file_set_error(f);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
blk_send(f, blk);
|
||||||
|
|
||||||
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
||||||
|
qemu_free(blk->buf);
|
||||||
|
qemu_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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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 * BLOCK_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int is_stage2_completed(void)
|
||||||
|
{
|
||||||
|
int64_t remaining_dirty;
|
||||||
|
long double bwidth;
|
||||||
|
|
||||||
|
if (block_mig_state.bulk_completed == 1) {
|
||||||
|
|
||||||
|
remaining_dirty = get_remaining_dirty();
|
||||||
|
if (remaining_dirty == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bwidth = compute_read_bwidth();
|
||||||
|
|
||||||
|
if ((remaining_dirty / bwidth) <=
|
||||||
|
migrate_max_downtime()) {
|
||||||
|
/* finish stage2 because we think that we can finish remaing work
|
||||||
|
below max_downtime */
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void blk_mig_cleanup(Monitor *mon)
|
||||||
|
{
|
||||||
|
BlkMigDevState *bmds;
|
||||||
|
BlkMigBlock *blk;
|
||||||
|
|
||||||
|
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));
|
||||||
|
qemu_free(bmds->aio_bitmap);
|
||||||
|
qemu_free(bmds);
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((blk = QSIMPLEQ_FIRST(&block_mig_state.blk_list)) != NULL) {
|
||||||
|
QSIMPLEQ_REMOVE_HEAD(&block_mig_state.blk_list, entry);
|
||||||
|
qemu_free(blk->buf);
|
||||||
|
qemu_free(blk);
|
||||||
|
}
|
||||||
|
|
||||||
|
monitor_printf(mon, "\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
static int block_save_live(Monitor *mon, QEMUFile *f, int stage, void *opaque)
|
||||||
|
{
|
||||||
|
DPRINTF("Enter save live stage %d submitted %d transferred %d\n",
|
||||||
|
stage, block_mig_state.submitted, block_mig_state.transferred);
|
||||||
|
|
||||||
|
if (stage < 0) {
|
||||||
|
blk_mig_cleanup(mon);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (block_mig_state.blk_enable != 1) {
|
||||||
|
/* no need to migrate storage */
|
||||||
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stage == 1) {
|
||||||
|
init_blk_migration(mon, f);
|
||||||
|
|
||||||
|
/* start track dirty blocks */
|
||||||
|
set_dirty_tracking(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
flush_blks(f);
|
||||||
|
|
||||||
|
if (qemu_file_has_error(f)) {
|
||||||
|
blk_mig_cleanup(mon);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
blk_mig_reset_dirty_cursor();
|
||||||
|
|
||||||
|
if (stage == 2) {
|
||||||
|
/* 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(mon, f) == 0) {
|
||||||
|
/* finished saving bulk on all devices */
|
||||||
|
block_mig_state.bulk_completed = 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (blk_mig_save_dirty_block(mon, f, 1) == 0) {
|
||||||
|
/* no more dirty blocks */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
flush_blks(f);
|
||||||
|
|
||||||
|
if (qemu_file_has_error(f)) {
|
||||||
|
blk_mig_cleanup(mon);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (stage == 3) {
|
||||||
|
/* we know for sure that save bulk is completed and
|
||||||
|
all async read completed */
|
||||||
|
assert(block_mig_state.submitted == 0);
|
||||||
|
|
||||||
|
while (blk_mig_save_dirty_block(mon, f, 0) != 0);
|
||||||
|
blk_mig_cleanup(mon);
|
||||||
|
|
||||||
|
/* report completion */
|
||||||
|
qemu_put_be64(f, (100 << BDRV_SECTOR_BITS) | BLK_MIG_FLAG_PROGRESS);
|
||||||
|
|
||||||
|
if (qemu_file_has_error(f)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
monitor_printf(mon, "Block migration completed\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
qemu_put_be64(f, BLK_MIG_FLAG_EOS);
|
||||||
|
|
||||||
|
return ((stage == 2) && is_stage2_completed());
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
do {
|
||||||
|
addr = qemu_get_be64(f);
|
||||||
|
|
||||||
|
flags = addr & ~BDRV_SECTOR_MASK;
|
||||||
|
addr >>= BDRV_SECTOR_BITS;
|
||||||
|
|
||||||
|
if (flags & BLK_MIG_FLAG_DEVICE_BLOCK) {
|
||||||
|
int ret;
|
||||||
|
/* 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 = qemu_malloc(BLOCK_SIZE);
|
||||||
|
|
||||||
|
qemu_get_buffer(f, buf, BLOCK_SIZE);
|
||||||
|
ret = bdrv_write(bs, addr, buf, nr_sectors);
|
||||||
|
|
||||||
|
qemu_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 flags\n");
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
if (qemu_file_has_error(f)) {
|
||||||
|
return -EIO;
|
||||||
|
}
|
||||||
|
} while (!(flags & BLK_MIG_FLAG_EOS));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void block_set_params(int blk_enable, int shared_base, void *opaque)
|
||||||
|
{
|
||||||
|
block_mig_state.blk_enable = blk_enable;
|
||||||
|
block_mig_state.shared_base = shared_base;
|
||||||
|
|
||||||
|
/* shared base means that blk_enable = 1 */
|
||||||
|
block_mig_state.blk_enable |= shared_base;
|
||||||
|
}
|
||||||
|
|
||||||
|
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, block_set_params,
|
||||||
|
block_save_live, NULL, block_load, &block_mig_state);
|
||||||
|
}
|
||||||
305
block.h
Normal file
305
block.h
Normal file
@@ -0,0 +1,305 @@
|
|||||||
|
#ifndef BLOCK_H
|
||||||
|
#define BLOCK_H
|
||||||
|
|
||||||
|
#include "qemu-aio.h"
|
||||||
|
#include "qemu-common.h"
|
||||||
|
#include "qemu-option.h"
|
||||||
|
#include "qobject.h"
|
||||||
|
|
||||||
|
/* block.c */
|
||||||
|
typedef struct BlockDriver BlockDriver;
|
||||||
|
|
||||||
|
typedef struct BlockDriverInfo {
|
||||||
|
/* in bytes, 0 if irrelevant */
|
||||||
|
int cluster_size;
|
||||||
|
/* offset at which the VM state can be saved (0 if not possible) */
|
||||||
|
int64_t vm_state_offset;
|
||||||
|
} BlockDriverInfo;
|
||||||
|
|
||||||
|
typedef struct QEMUSnapshotInfo {
|
||||||
|
char id_str[128]; /* unique snapshot id */
|
||||||
|
/* the following fields are informative. They are not needed for
|
||||||
|
the consistency of the snapshot */
|
||||||
|
char name[256]; /* user choosen name */
|
||||||
|
uint32_t vm_state_size; /* VM state info size */
|
||||||
|
uint32_t date_sec; /* UTC date of the snapshot */
|
||||||
|
uint32_t date_nsec;
|
||||||
|
uint64_t vm_clock_nsec; /* VM clock relative to boot */
|
||||||
|
} QEMUSnapshotInfo;
|
||||||
|
|
||||||
|
#define BDRV_O_RDWR 0x0002
|
||||||
|
#define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save writes in a snapshot */
|
||||||
|
#define BDRV_O_NOCACHE 0x0020 /* do not use the host page cache */
|
||||||
|
#define BDRV_O_CACHE_WB 0x0040 /* use write-back caching */
|
||||||
|
#define BDRV_O_NATIVE_AIO 0x0080 /* use native AIO instead of the thread pool */
|
||||||
|
#define BDRV_O_NO_BACKING 0x0100 /* don't open the backing file */
|
||||||
|
#define BDRV_O_NO_FLUSH 0x0200 /* disable flushing on this disk */
|
||||||
|
|
||||||
|
#define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_CACHE_WB | BDRV_O_NO_FLUSH)
|
||||||
|
|
||||||
|
#define BDRV_SECTOR_BITS 9
|
||||||
|
#define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS)
|
||||||
|
#define BDRV_SECTOR_MASK ~(BDRV_SECTOR_SIZE - 1)
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BLOCK_ERR_REPORT, BLOCK_ERR_IGNORE, BLOCK_ERR_STOP_ENOSPC,
|
||||||
|
BLOCK_ERR_STOP_ANY
|
||||||
|
} BlockErrorAction;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BDRV_ACTION_REPORT, BDRV_ACTION_IGNORE, BDRV_ACTION_STOP
|
||||||
|
} BlockMonEventAction;
|
||||||
|
|
||||||
|
void bdrv_mon_event(const BlockDriverState *bdrv,
|
||||||
|
BlockMonEventAction action, int is_read);
|
||||||
|
void bdrv_info_print(Monitor *mon, const QObject *data);
|
||||||
|
void bdrv_info(Monitor *mon, QObject **ret_data);
|
||||||
|
void bdrv_stats_print(Monitor *mon, const QObject *data);
|
||||||
|
void bdrv_info_stats(Monitor *mon, QObject **ret_data);
|
||||||
|
|
||||||
|
void bdrv_init(void);
|
||||||
|
void bdrv_init_with_whitelist(void);
|
||||||
|
BlockDriver *bdrv_find_protocol(const char *filename);
|
||||||
|
BlockDriver *bdrv_find_format(const char *format_name);
|
||||||
|
BlockDriver *bdrv_find_whitelisted_format(const char *format_name);
|
||||||
|
int bdrv_create(BlockDriver *drv, const char* filename,
|
||||||
|
QEMUOptionParameter *options);
|
||||||
|
int bdrv_create_file(const char* filename, QEMUOptionParameter *options);
|
||||||
|
BlockDriverState *bdrv_new(const char *device_name);
|
||||||
|
void bdrv_make_anon(BlockDriverState *bs);
|
||||||
|
void bdrv_delete(BlockDriverState *bs);
|
||||||
|
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags);
|
||||||
|
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
|
||||||
|
BlockDriver *drv);
|
||||||
|
void bdrv_close(BlockDriverState *bs);
|
||||||
|
int bdrv_attach(BlockDriverState *bs, DeviceState *qdev);
|
||||||
|
void bdrv_detach(BlockDriverState *bs, DeviceState *qdev);
|
||||||
|
DeviceState *bdrv_get_attached(BlockDriverState *bs);
|
||||||
|
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
uint8_t *buf, int nb_sectors);
|
||||||
|
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
const uint8_t *buf, int nb_sectors);
|
||||||
|
int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
||||||
|
void *buf, int count);
|
||||||
|
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
||||||
|
const void *buf, int count);
|
||||||
|
int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
|
||||||
|
const void *buf, int count);
|
||||||
|
int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
const uint8_t *buf, int nb_sectors);
|
||||||
|
int bdrv_truncate(BlockDriverState *bs, int64_t offset);
|
||||||
|
int64_t bdrv_getlength(BlockDriverState *bs);
|
||||||
|
int64_t bdrv_get_allocated_file_size(BlockDriverState *bs);
|
||||||
|
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr);
|
||||||
|
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs);
|
||||||
|
int bdrv_commit(BlockDriverState *bs);
|
||||||
|
void bdrv_commit_all(void);
|
||||||
|
int bdrv_change_backing_file(BlockDriverState *bs,
|
||||||
|
const char *backing_file, const char *backing_fmt);
|
||||||
|
void bdrv_register(BlockDriver *bdrv);
|
||||||
|
|
||||||
|
|
||||||
|
typedef struct BdrvCheckResult {
|
||||||
|
int corruptions;
|
||||||
|
int leaks;
|
||||||
|
int check_errors;
|
||||||
|
} BdrvCheckResult;
|
||||||
|
|
||||||
|
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res);
|
||||||
|
|
||||||
|
/* async block I/O */
|
||||||
|
typedef struct BlockDriverAIOCB BlockDriverAIOCB;
|
||||||
|
typedef void BlockDriverCompletionFunc(void *opaque, int ret);
|
||||||
|
typedef void BlockDriverDirtyHandler(BlockDriverState *bs, int64_t sector,
|
||||||
|
int sector_num);
|
||||||
|
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
QEMUIOVector *iov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque);
|
||||||
|
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
QEMUIOVector *iov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque);
|
||||||
|
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque);
|
||||||
|
void bdrv_aio_cancel(BlockDriverAIOCB *acb);
|
||||||
|
|
||||||
|
typedef struct BlockRequest {
|
||||||
|
/* Fields to be filled by multiwrite caller */
|
||||||
|
int64_t sector;
|
||||||
|
int nb_sectors;
|
||||||
|
QEMUIOVector *qiov;
|
||||||
|
BlockDriverCompletionFunc *cb;
|
||||||
|
void *opaque;
|
||||||
|
|
||||||
|
/* Filled by multiwrite implementation */
|
||||||
|
int error;
|
||||||
|
} BlockRequest;
|
||||||
|
|
||||||
|
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs,
|
||||||
|
int num_reqs);
|
||||||
|
|
||||||
|
/* sg packet commands */
|
||||||
|
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf);
|
||||||
|
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
|
||||||
|
unsigned long int req, void *buf,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque);
|
||||||
|
|
||||||
|
/* Ensure contents are flushed to disk. */
|
||||||
|
int bdrv_flush(BlockDriverState *bs);
|
||||||
|
void bdrv_flush_all(void);
|
||||||
|
void bdrv_close_all(void);
|
||||||
|
|
||||||
|
int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors);
|
||||||
|
int bdrv_has_zero_init(BlockDriverState *bs);
|
||||||
|
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
|
||||||
|
int *pnum);
|
||||||
|
|
||||||
|
#define BIOS_ATA_TRANSLATION_AUTO 0
|
||||||
|
#define BIOS_ATA_TRANSLATION_NONE 1
|
||||||
|
#define BIOS_ATA_TRANSLATION_LBA 2
|
||||||
|
#define BIOS_ATA_TRANSLATION_LARGE 3
|
||||||
|
#define BIOS_ATA_TRANSLATION_RECHS 4
|
||||||
|
|
||||||
|
void bdrv_set_geometry_hint(BlockDriverState *bs,
|
||||||
|
int cyls, int heads, int secs);
|
||||||
|
void bdrv_set_translation_hint(BlockDriverState *bs, int translation);
|
||||||
|
void bdrv_get_geometry_hint(BlockDriverState *bs,
|
||||||
|
int *pcyls, int *pheads, int *psecs);
|
||||||
|
typedef enum FDriveType {
|
||||||
|
FDRIVE_DRV_144 = 0x00, /* 1.44 MB 3"5 drive */
|
||||||
|
FDRIVE_DRV_288 = 0x01, /* 2.88 MB 3"5 drive */
|
||||||
|
FDRIVE_DRV_120 = 0x02, /* 1.2 MB 5"25 drive */
|
||||||
|
FDRIVE_DRV_NONE = 0x03, /* No drive connected */
|
||||||
|
} FDriveType;
|
||||||
|
|
||||||
|
void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
|
||||||
|
int *max_track, int *last_sect,
|
||||||
|
FDriveType drive_in, FDriveType *drive);
|
||||||
|
int bdrv_get_translation_hint(BlockDriverState *bs);
|
||||||
|
void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
|
||||||
|
BlockErrorAction on_write_error);
|
||||||
|
BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read);
|
||||||
|
void bdrv_set_removable(BlockDriverState *bs, int removable);
|
||||||
|
int bdrv_is_removable(BlockDriverState *bs);
|
||||||
|
int bdrv_is_read_only(BlockDriverState *bs);
|
||||||
|
int bdrv_is_sg(BlockDriverState *bs);
|
||||||
|
int bdrv_enable_write_cache(BlockDriverState *bs);
|
||||||
|
int bdrv_is_inserted(BlockDriverState *bs);
|
||||||
|
int bdrv_media_changed(BlockDriverState *bs);
|
||||||
|
int bdrv_is_locked(BlockDriverState *bs);
|
||||||
|
void bdrv_set_locked(BlockDriverState *bs, int locked);
|
||||||
|
int bdrv_eject(BlockDriverState *bs, int eject_flag);
|
||||||
|
void bdrv_set_change_cb(BlockDriverState *bs,
|
||||||
|
void (*change_cb)(void *opaque, int reason),
|
||||||
|
void *opaque);
|
||||||
|
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size);
|
||||||
|
BlockDriverState *bdrv_find(const char *name);
|
||||||
|
BlockDriverState *bdrv_next(BlockDriverState *bs);
|
||||||
|
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs),
|
||||||
|
void *opaque);
|
||||||
|
int bdrv_is_encrypted(BlockDriverState *bs);
|
||||||
|
int bdrv_key_required(BlockDriverState *bs);
|
||||||
|
int bdrv_set_key(BlockDriverState *bs, const char *key);
|
||||||
|
int bdrv_query_missing_keys(void);
|
||||||
|
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
|
||||||
|
void *opaque);
|
||||||
|
const char *bdrv_get_device_name(BlockDriverState *bs);
|
||||||
|
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
const uint8_t *buf, int nb_sectors);
|
||||||
|
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi);
|
||||||
|
|
||||||
|
const char *bdrv_get_encrypted_filename(BlockDriverState *bs);
|
||||||
|
void bdrv_get_backing_filename(BlockDriverState *bs,
|
||||||
|
char *filename, int filename_size);
|
||||||
|
int bdrv_can_snapshot(BlockDriverState *bs);
|
||||||
|
int bdrv_is_snapshot(BlockDriverState *bs);
|
||||||
|
BlockDriverState *bdrv_snapshots(void);
|
||||||
|
int bdrv_snapshot_create(BlockDriverState *bs,
|
||||||
|
QEMUSnapshotInfo *sn_info);
|
||||||
|
int bdrv_snapshot_goto(BlockDriverState *bs,
|
||||||
|
const char *snapshot_id);
|
||||||
|
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
|
||||||
|
int bdrv_snapshot_list(BlockDriverState *bs,
|
||||||
|
QEMUSnapshotInfo **psn_info);
|
||||||
|
int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
||||||
|
const char *snapshot_name);
|
||||||
|
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
|
||||||
|
|
||||||
|
char *get_human_readable_size(char *buf, int buf_size, int64_t size);
|
||||||
|
int path_is_absolute(const char *path);
|
||||||
|
void path_combine(char *dest, int dest_size,
|
||||||
|
const char *base_path,
|
||||||
|
const char *filename);
|
||||||
|
|
||||||
|
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
|
||||||
|
int64_t pos, int size);
|
||||||
|
|
||||||
|
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
|
||||||
|
int64_t pos, int size);
|
||||||
|
|
||||||
|
int bdrv_img_create(const char *filename, const char *fmt,
|
||||||
|
const char *base_filename, const char *base_fmt,
|
||||||
|
char *options, uint64_t img_size, int flags);
|
||||||
|
|
||||||
|
#define BDRV_SECTORS_PER_DIRTY_CHUNK 2048
|
||||||
|
|
||||||
|
void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable);
|
||||||
|
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector);
|
||||||
|
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
|
||||||
|
int nr_sectors);
|
||||||
|
int64_t bdrv_get_dirty_count(BlockDriverState *bs);
|
||||||
|
|
||||||
|
void bdrv_set_in_use(BlockDriverState *bs, int in_use);
|
||||||
|
int bdrv_in_use(BlockDriverState *bs);
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BLKDBG_L1_UPDATE,
|
||||||
|
|
||||||
|
BLKDBG_L1_GROW_ALLOC_TABLE,
|
||||||
|
BLKDBG_L1_GROW_WRITE_TABLE,
|
||||||
|
BLKDBG_L1_GROW_ACTIVATE_TABLE,
|
||||||
|
|
||||||
|
BLKDBG_L2_LOAD,
|
||||||
|
BLKDBG_L2_UPDATE,
|
||||||
|
BLKDBG_L2_UPDATE_COMPRESSED,
|
||||||
|
BLKDBG_L2_ALLOC_COW_READ,
|
||||||
|
BLKDBG_L2_ALLOC_WRITE,
|
||||||
|
|
||||||
|
BLKDBG_READ,
|
||||||
|
BLKDBG_READ_AIO,
|
||||||
|
BLKDBG_READ_BACKING,
|
||||||
|
BLKDBG_READ_BACKING_AIO,
|
||||||
|
BLKDBG_READ_COMPRESSED,
|
||||||
|
|
||||||
|
BLKDBG_WRITE_AIO,
|
||||||
|
BLKDBG_WRITE_COMPRESSED,
|
||||||
|
|
||||||
|
BLKDBG_VMSTATE_LOAD,
|
||||||
|
BLKDBG_VMSTATE_SAVE,
|
||||||
|
|
||||||
|
BLKDBG_COW_READ,
|
||||||
|
BLKDBG_COW_WRITE,
|
||||||
|
|
||||||
|
BLKDBG_REFTABLE_LOAD,
|
||||||
|
BLKDBG_REFTABLE_GROW,
|
||||||
|
|
||||||
|
BLKDBG_REFBLOCK_LOAD,
|
||||||
|
BLKDBG_REFBLOCK_UPDATE,
|
||||||
|
BLKDBG_REFBLOCK_UPDATE_PART,
|
||||||
|
BLKDBG_REFBLOCK_ALLOC,
|
||||||
|
BLKDBG_REFBLOCK_ALLOC_HOOKUP,
|
||||||
|
BLKDBG_REFBLOCK_ALLOC_WRITE,
|
||||||
|
BLKDBG_REFBLOCK_ALLOC_WRITE_BLOCKS,
|
||||||
|
BLKDBG_REFBLOCK_ALLOC_WRITE_TABLE,
|
||||||
|
BLKDBG_REFBLOCK_ALLOC_SWITCH_TABLE,
|
||||||
|
|
||||||
|
BLKDBG_CLUSTER_ALLOC,
|
||||||
|
BLKDBG_CLUSTER_ALLOC_BYTES,
|
||||||
|
BLKDBG_CLUSTER_FREE,
|
||||||
|
|
||||||
|
BLKDBG_EVENT_MAX,
|
||||||
|
} BlkDebugEvent;
|
||||||
|
|
||||||
|
#define BLKDBG_EVENT(bs, evt) bdrv_debug_event(bs, evt)
|
||||||
|
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
block-obj-y += raw_bsd.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-$(CONFIG_VHDX) += vhdx.o vhdx-endian.o vhdx-log.o
|
|
||||||
block-obj-$(CONFIG_QUORUM) += quorum.o
|
|
||||||
block-obj-y += parallels.o blkdebug.o blkverify.o
|
|
||||||
block-obj-y += block-backend.o snapshot.o qapi.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
|
|
||||||
|
|
||||||
block-obj-y += nbd.o nbd-client.o sheepdog.o
|
|
||||||
block-obj-$(CONFIG_LIBISCSI) += iscsi.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_ARCHIPELAGO) += archipelago.o
|
|
||||||
block-obj-$(CONFIG_LIBSSH2) += ssh.o
|
|
||||||
block-obj-y += accounting.o
|
|
||||||
block-obj-y += write-threshold.o
|
|
||||||
|
|
||||||
common-obj-y += stream.o
|
|
||||||
common-obj-y += commit.o
|
|
||||||
common-obj-y += backup.o
|
|
||||||
|
|
||||||
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)
|
|
||||||
ssh.o-cflags := $(LIBSSH2_CFLAGS)
|
|
||||||
ssh.o-libs := $(LIBSSH2_LIBS)
|
|
||||||
archipelago.o-libs := $(ARCHIPELAGO_LIBS)
|
|
||||||
dmg.o-libs := $(BZIP2_LIBS)
|
|
||||||
qcow.o-libs := -lz
|
|
||||||
linux-aio.o-libs := -laio
|
|
||||||
@@ -1,63 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU System Emulator block accounting
|
|
||||||
*
|
|
||||||
* Copyright (c) 2011 Christoph Hellwig
|
|
||||||
*
|
|
||||||
* 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 "block/accounting.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "qemu/timer.h"
|
|
||||||
|
|
||||||
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(QEMU_CLOCK_REALTIME);
|
|
||||||
cookie->type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
|
|
||||||
{
|
|
||||||
assert(cookie->type < BLOCK_MAX_IOTYPE);
|
|
||||||
|
|
||||||
stats->nr_bytes[cookie->type] += cookie->bytes;
|
|
||||||
stats->nr_ops[cookie->type]++;
|
|
||||||
stats->total_time_ns[cookie->type] +=
|
|
||||||
qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - cookie->start_time_ns;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void block_acct_highest_sector(BlockAcctStats *stats, int64_t sector_num,
|
|
||||||
unsigned int nb_sectors)
|
|
||||||
{
|
|
||||||
if (stats->wr_highest_sector < sector_num + nb_sectors - 1) {
|
|
||||||
stats->wr_highest_sector = sector_num + nb_sectors - 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
|
|
||||||
int num_requests)
|
|
||||||
{
|
|
||||||
assert(type < BLOCK_MAX_IOTYPE);
|
|
||||||
stats->merged[type] += num_requests;
|
|
||||||
}
|
|
||||||
1084
block/archipelago.c
1084
block/archipelago.c
File diff suppressed because it is too large
Load Diff
437
block/backup.c
437
block/backup.c
@@ -1,437 +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 <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "trace.h"
|
|
||||||
#include "block/block.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "block/blockjob.h"
|
|
||||||
#include "qemu/ratelimit.h"
|
|
||||||
|
|
||||||
#define BACKUP_CLUSTER_BITS 16
|
|
||||||
#define BACKUP_CLUSTER_SIZE (1 << BACKUP_CLUSTER_BITS)
|
|
||||||
#define BACKUP_SECTORS_PER_CLUSTER (BACKUP_CLUSTER_SIZE / BDRV_SECTOR_SIZE)
|
|
||||||
|
|
||||||
#define SLICE_TIME 100000000ULL /* ns */
|
|
||||||
|
|
||||||
typedef struct CowRequest {
|
|
||||||
int64_t start;
|
|
||||||
int64_t end;
|
|
||||||
QLIST_ENTRY(CowRequest) list;
|
|
||||||
CoQueue wait_queue; /* coroutines blocked on this request */
|
|
||||||
} CowRequest;
|
|
||||||
|
|
||||||
typedef struct BackupBlockJob {
|
|
||||||
BlockJob common;
|
|
||||||
BlockDriverState *target;
|
|
||||||
MirrorSyncMode sync_mode;
|
|
||||||
RateLimit limit;
|
|
||||||
BlockdevOnError on_source_error;
|
|
||||||
BlockdevOnError on_target_error;
|
|
||||||
CoRwlock flush_rwlock;
|
|
||||||
uint64_t sectors_read;
|
|
||||||
HBitmap *bitmap;
|
|
||||||
QLIST_HEAD(, CowRequest) inflight_reqs;
|
|
||||||
} BackupBlockJob;
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
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(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
bool *error_is_read)
|
|
||||||
{
|
|
||||||
BackupBlockJob *job = (BackupBlockJob *)bs->job;
|
|
||||||
CowRequest cow_request;
|
|
||||||
struct iovec iov;
|
|
||||||
QEMUIOVector bounce_qiov;
|
|
||||||
void *bounce_buffer = NULL;
|
|
||||||
int ret = 0;
|
|
||||||
int64_t start, end;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
qemu_co_rwlock_rdlock(&job->flush_rwlock);
|
|
||||||
|
|
||||||
start = sector_num / BACKUP_SECTORS_PER_CLUSTER;
|
|
||||||
end = DIV_ROUND_UP(sector_num + nb_sectors, BACKUP_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 (hbitmap_get(job->bitmap, start)) {
|
|
||||||
trace_backup_do_cow_skip(job, start);
|
|
||||||
continue; /* already copied */
|
|
||||||
}
|
|
||||||
|
|
||||||
trace_backup_do_cow_process(job, start);
|
|
||||||
|
|
||||||
n = MIN(BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
job->common.len / BDRV_SECTOR_SIZE -
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER);
|
|
||||||
|
|
||||||
if (!bounce_buffer) {
|
|
||||||
bounce_buffer = qemu_blockalign(bs, BACKUP_CLUSTER_SIZE);
|
|
||||||
}
|
|
||||||
iov.iov_base = bounce_buffer;
|
|
||||||
iov.iov_len = n * BDRV_SECTOR_SIZE;
|
|
||||||
qemu_iovec_init_external(&bounce_qiov, &iov, 1);
|
|
||||||
|
|
||||||
ret = bdrv_co_readv(bs, start * BACKUP_SECTORS_PER_CLUSTER, n,
|
|
||||||
&bounce_qiov);
|
|
||||||
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 = bdrv_co_write_zeroes(job->target,
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
n, BDRV_REQ_MAY_UNMAP);
|
|
||||||
} else {
|
|
||||||
ret = bdrv_co_writev(job->target,
|
|
||||||
start * BACKUP_SECTORS_PER_CLUSTER, n,
|
|
||||||
&bounce_qiov);
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
trace_backup_do_cow_write_fail(job, start, ret);
|
|
||||||
if (error_is_read) {
|
|
||||||
*error_is_read = false;
|
|
||||||
}
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
hbitmap_set(job->bitmap, start, 1);
|
|
||||||
|
|
||||||
/* 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)
|
|
||||||
{
|
|
||||||
BdrvTrackedRequest *req = opaque;
|
|
||||||
int64_t sector_num = req->offset >> BDRV_SECTOR_BITS;
|
|
||||||
int nb_sectors = req->bytes >> BDRV_SECTOR_BITS;
|
|
||||||
|
|
||||||
assert((req->offset & (BDRV_SECTOR_SIZE - 1)) == 0);
|
|
||||||
assert((req->bytes & (BDRV_SECTOR_SIZE - 1)) == 0);
|
|
||||||
|
|
||||||
return backup_do_cow(req->bs, sector_num, nb_sectors, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void backup_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
|
|
||||||
if (speed < 0) {
|
|
||||||
error_set(errp, QERR_INVALID_PARAMETER, "speed");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void backup_iostatus_reset(BlockJob *job)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
|
|
||||||
bdrv_iostatus_reset(s->target);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const BlockJobDriver backup_job_driver = {
|
|
||||||
.instance_size = sizeof(BackupBlockJob),
|
|
||||||
.job_type = BLOCK_JOB_TYPE_BACKUP,
|
|
||||||
.set_speed = backup_set_speed,
|
|
||||||
.iostatus_reset = backup_iostatus_reset,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockErrorAction backup_error_action(BackupBlockJob *job,
|
|
||||||
bool read, int error)
|
|
||||||
{
|
|
||||||
if (read) {
|
|
||||||
return block_job_error_action(&job->common, job->common.bs,
|
|
||||||
job->on_source_error, true, error);
|
|
||||||
} else {
|
|
||||||
return block_job_error_action(&job->common, job->target,
|
|
||||||
job->on_target_error, false, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int ret;
|
|
||||||
} BackupCompleteData;
|
|
||||||
|
|
||||||
static void backup_complete(BlockJob *job, void *opaque)
|
|
||||||
{
|
|
||||||
BackupBlockJob *s = container_of(job, BackupBlockJob, common);
|
|
||||||
BackupCompleteData *data = opaque;
|
|
||||||
|
|
||||||
bdrv_unref(s->target);
|
|
||||||
|
|
||||||
block_job_completed(job, data->ret);
|
|
||||||
g_free(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void coroutine_fn backup_run(void *opaque)
|
|
||||||
{
|
|
||||||
BackupBlockJob *job = opaque;
|
|
||||||
BackupCompleteData *data;
|
|
||||||
BlockDriverState *bs = job->common.bs;
|
|
||||||
BlockDriverState *target = job->target;
|
|
||||||
BlockdevOnError on_target_error = job->on_target_error;
|
|
||||||
NotifierWithReturn before_write = {
|
|
||||||
.notify = backup_before_write_notify,
|
|
||||||
};
|
|
||||||
int64_t start, end;
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
QLIST_INIT(&job->inflight_reqs);
|
|
||||||
qemu_co_rwlock_init(&job->flush_rwlock);
|
|
||||||
|
|
||||||
start = 0;
|
|
||||||
end = DIV_ROUND_UP(job->common.len / BDRV_SECTOR_SIZE,
|
|
||||||
BACKUP_SECTORS_PER_CLUSTER);
|
|
||||||
|
|
||||||
job->bitmap = hbitmap_alloc(end, 0);
|
|
||||||
|
|
||||||
bdrv_set_enable_write_cache(target, true);
|
|
||||||
bdrv_set_on_error(target, on_target_error, on_target_error);
|
|
||||||
bdrv_iostatus_enable(target);
|
|
||||||
|
|
||||||
bdrv_add_before_write_notifier(bs, &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. */
|
|
||||||
job->common.busy = false;
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
job->common.busy = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* Both FULL and TOP SYNC_MODE's require copying.. */
|
|
||||||
for (; start < end; start++) {
|
|
||||||
bool error_is_read;
|
|
||||||
|
|
||||||
if (block_job_is_cancelled(&job->common)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* we need to yield so that qemu_aio_flush() 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)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (job->sync_mode == MIRROR_SYNC_MODE_TOP) {
|
|
||||||
int i, n;
|
|
||||||
int alloced = 0;
|
|
||||||
|
|
||||||
/* Check to see if these blocks are already in the
|
|
||||||
* backing file. */
|
|
||||||
|
|
||||||
for (i = 0; i < BACKUP_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 * BACKUP_SECTORS_PER_CLUSTER + i,
|
|
||||||
BACKUP_SECTORS_PER_CLUSTER - i, &n);
|
|
||||||
i += n;
|
|
||||||
|
|
||||||
if (alloced == 1 || 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. */
|
|
||||||
ret = backup_do_cow(bs, start * BACKUP_SECTORS_PER_CLUSTER,
|
|
||||||
BACKUP_SECTORS_PER_CLUSTER, &error_is_read);
|
|
||||||
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(&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);
|
|
||||||
|
|
||||||
hbitmap_free(job->bitmap);
|
|
||||||
|
|
||||||
bdrv_iostatus_disable(target);
|
|
||||||
bdrv_op_unblock_all(target, job->common.blocker);
|
|
||||||
|
|
||||||
data = g_malloc(sizeof(*data));
|
|
||||||
data->ret = ret;
|
|
||||||
block_job_defer_to_main_loop(&job->common, backup_complete, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
void backup_start(BlockDriverState *bs, BlockDriverState *target,
|
|
||||||
int64_t speed, MirrorSyncMode sync_mode,
|
|
||||||
BlockdevOnError on_source_error,
|
|
||||||
BlockdevOnError on_target_error,
|
|
||||||
BlockCompletionFunc *cb, void *opaque,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
int64_t len;
|
|
||||||
|
|
||||||
assert(bs);
|
|
||||||
assert(target);
|
|
||||||
assert(cb);
|
|
||||||
|
|
||||||
if (bs == target) {
|
|
||||||
error_setg(errp, "Source and target cannot be the same");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
|
|
||||||
on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
|
||||||
!bdrv_iostatus_is_enabled(bs)) {
|
|
||||||
error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bdrv_is_inserted(bs)) {
|
|
||||||
error_setg(errp, "Device is not inserted: %s",
|
|
||||||
bdrv_get_device_name(bs));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!bdrv_is_inserted(target)) {
|
|
||||||
error_setg(errp, "Device is not inserted: %s",
|
|
||||||
bdrv_get_device_name(target));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_BACKUP_SOURCE, errp)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bdrv_op_is_blocked(target, BLOCK_OP_TYPE_BACKUP_TARGET, errp)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = bdrv_getlength(bs);
|
|
||||||
if (len < 0) {
|
|
||||||
error_setg_errno(errp, -len, "unable to get length for '%s'",
|
|
||||||
bdrv_get_device_name(bs));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
BackupBlockJob *job = block_job_create(&backup_job_driver, bs, speed,
|
|
||||||
cb, opaque, errp);
|
|
||||||
if (!job) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_op_block_all(target, job->common.blocker);
|
|
||||||
|
|
||||||
job->on_source_error = on_source_error;
|
|
||||||
job->on_target_error = on_target_error;
|
|
||||||
job->target = target;
|
|
||||||
job->sync_mode = sync_mode;
|
|
||||||
job->common.len = len;
|
|
||||||
job->common.co = qemu_coroutine_create(backup_run);
|
|
||||||
qemu_coroutine_enter(job->common.co, job);
|
|
||||||
}
|
|
||||||
546
block/blkdebug.c
546
block/blkdebug.c
@@ -23,43 +23,45 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/config-file.h"
|
#include "block_int.h"
|
||||||
#include "block/block_int.h"
|
#include "module.h"
|
||||||
#include "qemu/module.h"
|
|
||||||
#include "qapi/qmp/qbool.h"
|
typedef struct BlkdebugVars {
|
||||||
#include "qapi/qmp/qdict.h"
|
int state;
|
||||||
#include "qapi/qmp/qint.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
/* If inject_errno != 0, an error is injected for requests */
|
||||||
|
int inject_errno;
|
||||||
|
|
||||||
|
/* Decides if all future requests fail (false) or only the next one and
|
||||||
|
* after the next request inject_errno is reset to 0 (true) */
|
||||||
|
bool inject_once;
|
||||||
|
|
||||||
|
/* Decides if aio_readv/writev fails right away (true) or returns an error
|
||||||
|
* return value only in the callback (false) */
|
||||||
|
bool inject_immediately;
|
||||||
|
} BlkdebugVars;
|
||||||
|
|
||||||
typedef struct BDRVBlkdebugState {
|
typedef struct BDRVBlkdebugState {
|
||||||
int state;
|
BlkdebugVars vars;
|
||||||
int new_state;
|
QLIST_HEAD(list, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
|
||||||
|
|
||||||
QLIST_HEAD(, BlkdebugRule) rules[BLKDBG_EVENT_MAX];
|
|
||||||
QSIMPLEQ_HEAD(, BlkdebugRule) active_rules;
|
|
||||||
QLIST_HEAD(, BlkdebugSuspendedReq) suspended_reqs;
|
|
||||||
} BDRVBlkdebugState;
|
} BDRVBlkdebugState;
|
||||||
|
|
||||||
typedef struct BlkdebugAIOCB {
|
typedef struct BlkdebugAIOCB {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
int ret;
|
int ret;
|
||||||
} BlkdebugAIOCB;
|
} BlkdebugAIOCB;
|
||||||
|
|
||||||
typedef struct BlkdebugSuspendedReq {
|
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb);
|
||||||
Coroutine *co;
|
|
||||||
char *tag;
|
|
||||||
QLIST_ENTRY(BlkdebugSuspendedReq) next;
|
|
||||||
} BlkdebugSuspendedReq;
|
|
||||||
|
|
||||||
static const AIOCBInfo blkdebug_aiocb_info = {
|
static AIOPool blkdebug_aio_pool = {
|
||||||
.aiocb_size = sizeof(BlkdebugAIOCB),
|
.aiocb_size = sizeof(BlkdebugAIOCB),
|
||||||
|
.cancel = blkdebug_aio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
ACTION_INJECT_ERROR,
|
ACTION_INJECT_ERROR,
|
||||||
ACTION_SET_STATE,
|
ACTION_SET_STATE,
|
||||||
ACTION_SUSPEND,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct BlkdebugRule {
|
typedef struct BlkdebugRule {
|
||||||
@@ -71,17 +73,12 @@ typedef struct BlkdebugRule {
|
|||||||
int error;
|
int error;
|
||||||
int immediately;
|
int immediately;
|
||||||
int once;
|
int once;
|
||||||
int64_t sector;
|
|
||||||
} inject;
|
} inject;
|
||||||
struct {
|
struct {
|
||||||
int new_state;
|
int new_state;
|
||||||
} set_state;
|
} set_state;
|
||||||
struct {
|
|
||||||
char *tag;
|
|
||||||
} suspend;
|
|
||||||
} options;
|
} options;
|
||||||
QLIST_ENTRY(BlkdebugRule) next;
|
QLIST_ENTRY(BlkdebugRule) next;
|
||||||
QSIMPLEQ_ENTRY(BlkdebugRule) active_next;
|
|
||||||
} BlkdebugRule;
|
} BlkdebugRule;
|
||||||
|
|
||||||
static QemuOptsList inject_error_opts = {
|
static QemuOptsList inject_error_opts = {
|
||||||
@@ -100,10 +97,6 @@ static QemuOptsList inject_error_opts = {
|
|||||||
.name = "errno",
|
.name = "errno",
|
||||||
.type = QEMU_OPT_NUMBER,
|
.type = QEMU_OPT_NUMBER,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
.name = "sector",
|
|
||||||
.type = QEMU_OPT_NUMBER,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
.name = "once",
|
.name = "once",
|
||||||
.type = QEMU_OPT_BOOL,
|
.type = QEMU_OPT_BOOL,
|
||||||
@@ -154,7 +147,9 @@ static const char *event_names[BLKDBG_EVENT_MAX] = {
|
|||||||
[BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read",
|
[BLKDBG_L2_ALLOC_COW_READ] = "l2_alloc.cow_read",
|
||||||
[BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write",
|
[BLKDBG_L2_ALLOC_WRITE] = "l2_alloc.write",
|
||||||
|
|
||||||
|
[BLKDBG_READ] = "read",
|
||||||
[BLKDBG_READ_AIO] = "read_aio",
|
[BLKDBG_READ_AIO] = "read_aio",
|
||||||
|
[BLKDBG_READ_BACKING] = "read_backing",
|
||||||
[BLKDBG_READ_BACKING_AIO] = "read_backing_aio",
|
[BLKDBG_READ_BACKING_AIO] = "read_backing_aio",
|
||||||
[BLKDBG_READ_COMPRESSED] = "read_compressed",
|
[BLKDBG_READ_COMPRESSED] = "read_compressed",
|
||||||
|
|
||||||
@@ -169,7 +164,6 @@ static const char *event_names[BLKDBG_EVENT_MAX] = {
|
|||||||
|
|
||||||
[BLKDBG_REFTABLE_LOAD] = "reftable_load",
|
[BLKDBG_REFTABLE_LOAD] = "reftable_load",
|
||||||
[BLKDBG_REFTABLE_GROW] = "reftable_grow",
|
[BLKDBG_REFTABLE_GROW] = "reftable_grow",
|
||||||
[BLKDBG_REFTABLE_UPDATE] = "reftable_update",
|
|
||||||
|
|
||||||
[BLKDBG_REFBLOCK_LOAD] = "refblock_load",
|
[BLKDBG_REFBLOCK_LOAD] = "refblock_load",
|
||||||
[BLKDBG_REFBLOCK_UPDATE] = "refblock_update",
|
[BLKDBG_REFBLOCK_UPDATE] = "refblock_update",
|
||||||
@@ -184,19 +178,6 @@ static const char *event_names[BLKDBG_EVENT_MAX] = {
|
|||||||
[BLKDBG_CLUSTER_ALLOC] = "cluster_alloc",
|
[BLKDBG_CLUSTER_ALLOC] = "cluster_alloc",
|
||||||
[BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes",
|
[BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes",
|
||||||
[BLKDBG_CLUSTER_FREE] = "cluster_free",
|
[BLKDBG_CLUSTER_FREE] = "cluster_free",
|
||||||
|
|
||||||
[BLKDBG_FLUSH_TO_OS] = "flush_to_os",
|
|
||||||
[BLKDBG_FLUSH_TO_DISK] = "flush_to_disk",
|
|
||||||
|
|
||||||
[BLKDBG_PWRITEV_RMW_HEAD] = "pwritev_rmw.head",
|
|
||||||
[BLKDBG_PWRITEV_RMW_AFTER_HEAD] = "pwritev_rmw.after_head",
|
|
||||||
[BLKDBG_PWRITEV_RMW_TAIL] = "pwritev_rmw.tail",
|
|
||||||
[BLKDBG_PWRITEV_RMW_AFTER_TAIL] = "pwritev_rmw.after_tail",
|
|
||||||
[BLKDBG_PWRITEV] = "pwritev",
|
|
||||||
[BLKDBG_PWRITEV_ZERO] = "pwritev_zero",
|
|
||||||
[BLKDBG_PWRITEV_DONE] = "pwritev_done",
|
|
||||||
|
|
||||||
[BLKDBG_EMPTY_IMAGE_PREPARE] = "empty_image_prepare",
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int get_event_by_name(const char *name, BlkDebugEvent *event)
|
static int get_event_by_name(const char *name, BlkDebugEvent *event)
|
||||||
@@ -216,7 +197,6 @@ static int get_event_by_name(const char *name, BlkDebugEvent *event)
|
|||||||
struct add_rule_data {
|
struct add_rule_data {
|
||||||
BDRVBlkdebugState *s;
|
BDRVBlkdebugState *s;
|
||||||
int action;
|
int action;
|
||||||
Error **errp;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static int add_rule(QemuOpts *opts, void *opaque)
|
static int add_rule(QemuOpts *opts, void *opaque)
|
||||||
@@ -229,16 +209,12 @@ static int add_rule(QemuOpts *opts, void *opaque)
|
|||||||
|
|
||||||
/* Find the right event for the rule */
|
/* Find the right event for the rule */
|
||||||
event_name = qemu_opt_get(opts, "event");
|
event_name = qemu_opt_get(opts, "event");
|
||||||
if (!event_name) {
|
if (!event_name || get_event_by_name(event_name, &event) < 0) {
|
||||||
error_setg(d->errp, "Missing event name for rule");
|
|
||||||
return -1;
|
|
||||||
} else if (get_event_by_name(event_name, &event) < 0) {
|
|
||||||
error_setg(d->errp, "Invalid event name \"%s\"", event_name);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Set attributes common for all actions */
|
/* Set attributes common for all actions */
|
||||||
rule = g_malloc0(sizeof(*rule));
|
rule = qemu_mallocz(sizeof(*rule));
|
||||||
*rule = (struct BlkdebugRule) {
|
*rule = (struct BlkdebugRule) {
|
||||||
.event = event,
|
.event = event,
|
||||||
.action = d->action,
|
.action = d->action,
|
||||||
@@ -252,18 +228,12 @@ static int add_rule(QemuOpts *opts, void *opaque)
|
|||||||
rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
|
rule->options.inject.once = qemu_opt_get_bool(opts, "once", 0);
|
||||||
rule->options.inject.immediately =
|
rule->options.inject.immediately =
|
||||||
qemu_opt_get_bool(opts, "immediately", 0);
|
qemu_opt_get_bool(opts, "immediately", 0);
|
||||||
rule->options.inject.sector = qemu_opt_get_number(opts, "sector", -1);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACTION_SET_STATE:
|
case ACTION_SET_STATE:
|
||||||
rule->options.set_state.new_state =
|
rule->options.set_state.new_state =
|
||||||
qemu_opt_get_number(opts, "new_state", 0);
|
qemu_opt_get_number(opts, "new_state", 0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACTION_SUSPEND:
|
|
||||||
rule->options.suspend.tag =
|
|
||||||
g_strdup(qemu_opt_get(opts, "tag"));
|
|
||||||
break;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Add the rule */
|
/* Add the rule */
|
||||||
@@ -272,189 +242,75 @@ static int add_rule(QemuOpts *opts, void *opaque)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void remove_rule(BlkdebugRule *rule)
|
static int read_config(BDRVBlkdebugState *s, const char *filename)
|
||||||
{
|
{
|
||||||
switch (rule->action) {
|
FILE *f;
|
||||||
case ACTION_INJECT_ERROR:
|
|
||||||
case ACTION_SET_STATE:
|
|
||||||
break;
|
|
||||||
case ACTION_SUSPEND:
|
|
||||||
g_free(rule->options.suspend.tag);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
QLIST_REMOVE(rule, next);
|
|
||||||
g_free(rule);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int read_config(BDRVBlkdebugState *s, const char *filename,
|
|
||||||
QDict *options, Error **errp)
|
|
||||||
{
|
|
||||||
FILE *f = NULL;
|
|
||||||
int ret;
|
int ret;
|
||||||
struct add_rule_data d;
|
struct add_rule_data d;
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
if (filename) {
|
|
||||||
f = fopen(filename, "r");
|
f = fopen(filename, "r");
|
||||||
if (f == NULL) {
|
if (f == NULL) {
|
||||||
error_setg_errno(errp, errno, "Could not read blkdebug config file");
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = qemu_config_parse(f, config_groups, filename);
|
ret = qemu_config_parse(f, config_groups, filename);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_setg(errp, "Could not parse blkdebug config file");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_config_parse_qdict(options, config_groups, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
d.s = s;
|
d.s = s;
|
||||||
d.action = ACTION_INJECT_ERROR;
|
d.action = ACTION_INJECT_ERROR;
|
||||||
d.errp = &local_err;
|
qemu_opts_foreach(&inject_error_opts, add_rule, &d, 0);
|
||||||
qemu_opts_foreach(&inject_error_opts, add_rule, &d, 1);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
d.action = ACTION_SET_STATE;
|
d.action = ACTION_SET_STATE;
|
||||||
qemu_opts_foreach(&set_state_opts, add_rule, &d, 1);
|
qemu_opts_foreach(&set_state_opts, add_rule, &d, 0);
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
ret = 0;
|
||||||
fail:
|
fail:
|
||||||
qemu_opts_reset(&inject_error_opts);
|
qemu_opts_reset(&inject_error_opts);
|
||||||
qemu_opts_reset(&set_state_opts);
|
qemu_opts_reset(&set_state_opts);
|
||||||
if (f) {
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
}
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
|
/* Valid blkdebug filenames look like blkdebug:path/to/config:path/to/image */
|
||||||
static void blkdebug_parse_filename(const char *filename, QDict *options,
|
static int blkdebug_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
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(options, "x-image", qstring_from_str(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(options, "x-image", qstring_from_str(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",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int blkdebug_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
const char *config;
|
|
||||||
uint64_t align;
|
|
||||||
int ret;
|
int ret;
|
||||||
|
char *config, *c;
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
/* Parse the blkdebug: prefix */
|
||||||
qemu_opts_absorb_qdict(opts, options, &local_err);
|
if (strncmp(filename, "blkdebug:", strlen("blkdebug:"))) {
|
||||||
if (local_err) {
|
return -EINVAL;
|
||||||
error_propagate(errp, local_err);
|
}
|
||||||
ret = -EINVAL;
|
filename += strlen("blkdebug:");
|
||||||
goto out;
|
|
||||||
|
/* Read rules from config file */
|
||||||
|
c = strchr(filename, ':');
|
||||||
|
if (c == NULL) {
|
||||||
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Read rules from config file or command line options */
|
config = strdup(filename);
|
||||||
config = qemu_opt_get(opts, "config");
|
config[c - filename] = '\0';
|
||||||
ret = read_config(s, config, options, errp);
|
ret = read_config(s, config);
|
||||||
if (ret) {
|
free(config);
|
||||||
goto out;
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
filename = c + 1;
|
||||||
|
|
||||||
/* Set initial state */
|
/* Set initial state */
|
||||||
s->state = 1;
|
s->vars.state = 1;
|
||||||
|
|
||||||
/* Open the backing file */
|
/* Open the backing file */
|
||||||
assert(bs->file == NULL);
|
ret = bdrv_file_open(&bs->file, filename, flags);
|
||||||
ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-image"), options, "image",
|
|
||||||
flags | BDRV_O_PROTOCOL, false, &local_err);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Set request alignment */
|
|
||||||
align = qemu_opt_get_size(opts, "align", bs->request_alignment);
|
|
||||||
if (align > 0 && align < INT_MAX && !(align & (align - 1))) {
|
|
||||||
bs->request_alignment = align;
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "Invalid alignment");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail_unref;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
goto out;
|
|
||||||
|
|
||||||
fail_unref:
|
|
||||||
bdrv_unref(bs->file);
|
|
||||||
out:
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return ret;
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void error_callback_bh(void *opaque)
|
static void error_callback_bh(void *opaque)
|
||||||
@@ -462,99 +318,71 @@ static void error_callback_bh(void *opaque)
|
|||||||
struct BlkdebugAIOCB *acb = opaque;
|
struct BlkdebugAIOCB *acb = opaque;
|
||||||
qemu_bh_delete(acb->bh);
|
qemu_bh_delete(acb->bh);
|
||||||
acb->common.cb(acb->common.opaque, acb->ret);
|
acb->common.cb(acb->common.opaque, acb->ret);
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *inject_error(BlockDriverState *bs,
|
static void blkdebug_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
BlockCompletionFunc *cb, void *opaque, BlkdebugRule *rule)
|
{
|
||||||
|
BlkdebugAIOCB *acb = container_of(blockacb, BlkdebugAIOCB, common);
|
||||||
|
qemu_aio_release(acb);
|
||||||
|
}
|
||||||
|
|
||||||
|
static BlockDriverAIOCB *inject_error(BlockDriverState *bs,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
int error = rule->options.inject.error;
|
int error = s->vars.inject_errno;
|
||||||
struct BlkdebugAIOCB *acb;
|
struct BlkdebugAIOCB *acb;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
|
|
||||||
if (rule->options.inject.once) {
|
if (s->vars.inject_once) {
|
||||||
QSIMPLEQ_INIT(&s->active_rules);
|
s->vars.inject_errno = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rule->options.inject.immediately) {
|
if (s->vars.inject_immediately) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
acb = qemu_aio_get(&blkdebug_aiocb_info, bs, cb, opaque);
|
acb = qemu_aio_get(&blkdebug_aio_pool, bs, cb, opaque);
|
||||||
acb->ret = -error;
|
acb->ret = -error;
|
||||||
|
|
||||||
bh = aio_bh_new(bdrv_get_aio_context(bs), error_callback_bh, acb);
|
bh = qemu_bh_new(error_callback_bh, acb);
|
||||||
acb->bh = bh;
|
acb->bh = bh;
|
||||||
qemu_bh_schedule(bh);
|
qemu_bh_schedule(bh);
|
||||||
|
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkdebug_aio_readv(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
if (s->vars.inject_errno) {
|
||||||
if (rule->options.inject.sector == -1 ||
|
return inject_error(bs, cb, opaque);
|
||||||
(rule->options.inject.sector >= sector_num &&
|
|
||||||
rule->options.inject.sector < sector_num + nb_sectors)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
BlockDriverAIOCB *acb =
|
||||||
return inject_error(bs, cb, opaque, rule);
|
bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
|
||||||
}
|
return acb;
|
||||||
|
|
||||||
return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkdebug_aio_writev(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
if (s->vars.inject_errno) {
|
||||||
if (rule->options.inject.sector == -1 ||
|
return inject_error(bs, cb, opaque);
|
||||||
(rule->options.inject.sector >= sector_num &&
|
|
||||||
rule->options.inject.sector < sector_num + nb_sectors)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
BlockDriverAIOCB *acb =
|
||||||
return inject_error(bs, cb, opaque, rule);
|
bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
|
||||||
}
|
return acb;
|
||||||
|
|
||||||
return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
|
||||||
BlkdebugRule *rule = NULL;
|
|
||||||
|
|
||||||
QSIMPLEQ_FOREACH(rule, &s->active_rules, active_next) {
|
|
||||||
if (rule->options.inject.sector == -1) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rule && rule->options.inject.error) {
|
|
||||||
return inject_error(bs, cb, opaque, rule);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_flush(bs->file, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void blkdebug_close(BlockDriverState *bs)
|
static void blkdebug_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
@@ -563,232 +391,76 @@ static void blkdebug_close(BlockDriverState *bs)
|
|||||||
|
|
||||||
for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
|
for (i = 0; i < BLKDBG_EVENT_MAX; i++) {
|
||||||
QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
|
QLIST_FOREACH_SAFE(rule, &s->rules[i], next, next) {
|
||||||
remove_rule(rule);
|
QLIST_REMOVE(rule, next);
|
||||||
|
qemu_free(rule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void suspend_request(BlockDriverState *bs, BlkdebugRule *rule)
|
static int blkdebug_flush(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
return bdrv_flush(bs->file);
|
||||||
BlkdebugSuspendedReq r;
|
|
||||||
|
|
||||||
r = (BlkdebugSuspendedReq) {
|
|
||||||
.co = qemu_coroutine_self(),
|
|
||||||
.tag = g_strdup(rule->options.suspend.tag),
|
|
||||||
};
|
|
||||||
|
|
||||||
remove_rule(rule);
|
|
||||||
QLIST_INSERT_HEAD(&s->suspended_reqs, &r, next);
|
|
||||||
|
|
||||||
printf("blkdebug: Suspended request '%s'\n", r.tag);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
printf("blkdebug: Resuming request '%s'\n", r.tag);
|
|
||||||
|
|
||||||
QLIST_REMOVE(&r, next);
|
|
||||||
g_free(r.tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
|
static BlockDriverAIOCB *blkdebug_aio_flush(BlockDriverState *bs,
|
||||||
bool injected)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
|
{
|
||||||
|
return bdrv_aio_flush(bs->file, cb, opaque);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void process_rule(BlockDriverState *bs, struct BlkdebugRule *rule,
|
||||||
|
BlkdebugVars *old_vars)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
|
BlkdebugVars *vars = &s->vars;
|
||||||
|
|
||||||
/* Only process rules for the current state */
|
/* Only process rules for the current state */
|
||||||
if (rule->state && rule->state != s->state) {
|
if (rule->state && rule->state != old_vars->state) {
|
||||||
return injected;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Take the action */
|
/* Take the action */
|
||||||
switch (rule->action) {
|
switch (rule->action) {
|
||||||
case ACTION_INJECT_ERROR:
|
case ACTION_INJECT_ERROR:
|
||||||
if (!injected) {
|
vars->inject_errno = rule->options.inject.error;
|
||||||
QSIMPLEQ_INIT(&s->active_rules);
|
vars->inject_once = rule->options.inject.once;
|
||||||
injected = true;
|
vars->inject_immediately = rule->options.inject.immediately;
|
||||||
}
|
|
||||||
QSIMPLEQ_INSERT_HEAD(&s->active_rules, rule, active_next);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ACTION_SET_STATE:
|
case ACTION_SET_STATE:
|
||||||
s->new_state = rule->options.set_state.new_state;
|
vars->state = rule->options.set_state.new_state;
|
||||||
break;
|
|
||||||
|
|
||||||
case ACTION_SUSPEND:
|
|
||||||
suspend_request(bs, rule);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return injected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
static void blkdebug_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
||||||
{
|
{
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
BDRVBlkdebugState *s = bs->opaque;
|
||||||
struct BlkdebugRule *rule, *next;
|
struct BlkdebugRule *rule;
|
||||||
bool injected;
|
BlkdebugVars old_vars = s->vars;
|
||||||
|
|
||||||
assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
|
assert((int)event >= 0 && event < BLKDBG_EVENT_MAX);
|
||||||
|
|
||||||
injected = false;
|
QLIST_FOREACH(rule, &s->rules[event], next) {
|
||||||
s->new_state = s->state;
|
process_rule(bs, rule, &old_vars);
|
||||||
QLIST_FOREACH_SAFE(rule, &s->rules[event], next, next) {
|
|
||||||
injected = process_rule(bs, rule, injected);
|
|
||||||
}
|
}
|
||||||
s->state = s->new_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int blkdebug_debug_breakpoint(BlockDriverState *bs, const char *event,
|
|
||||||
const char *tag)
|
|
||||||
{
|
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
|
||||||
struct BlkdebugRule *rule;
|
|
||||||
BlkDebugEvent blkdebug_event;
|
|
||||||
|
|
||||||
if (get_event_by_name(event, &blkdebug_event) < 0) {
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
rule = g_malloc(sizeof(*rule));
|
|
||||||
*rule = (struct BlkdebugRule) {
|
|
||||||
.event = blkdebug_event,
|
|
||||||
.action = ACTION_SUSPEND,
|
|
||||||
.state = 0,
|
|
||||||
.options.suspend.tag = g_strdup(tag),
|
|
||||||
};
|
|
||||||
|
|
||||||
QLIST_INSERT_HEAD(&s->rules[blkdebug_event], rule, next);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int blkdebug_debug_resume(BlockDriverState *bs, const char *tag)
|
|
||||||
{
|
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
|
||||||
BlkdebugSuspendedReq *r, *next;
|
|
||||||
|
|
||||||
QLIST_FOREACH_SAFE(r, &s->suspended_reqs, next, next) {
|
|
||||||
if (!strcmp(r->tag, tag)) {
|
|
||||||
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_EVENT_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, NULL);
|
|
||||||
ret = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool blkdebug_debug_is_suspended(BlockDriverState *bs, const char *tag)
|
|
||||||
{
|
|
||||||
BDRVBlkdebugState *s = bs->opaque;
|
|
||||||
BlkdebugSuspendedReq *r;
|
|
||||||
|
|
||||||
QLIST_FOREACH(r, &s->suspended_reqs, next) {
|
|
||||||
if (!strcmp(r->tag, tag)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t blkdebug_getlength(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
return bdrv_getlength(bs->file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blkdebug_refresh_filename(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
QDict *opts;
|
|
||||||
const QDictEntry *e;
|
|
||||||
bool force_json = false;
|
|
||||||
|
|
||||||
for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
|
|
||||||
if (strcmp(qdict_entry_key(e), "config") &&
|
|
||||||
strcmp(qdict_entry_key(e), "x-image") &&
|
|
||||||
strcmp(qdict_entry_key(e), "image") &&
|
|
||||||
strncmp(qdict_entry_key(e), "image.", strlen("image.")))
|
|
||||||
{
|
|
||||||
force_json = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (force_json && !bs->file->full_open_options) {
|
|
||||||
/* The config file cannot be recreated, so creating a plain filename
|
|
||||||
* is impossible */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!force_json && bs->file->exact_filename[0]) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"blkdebug:%s:%s",
|
|
||||||
qdict_get_try_str(bs->options, "config") ?: "",
|
|
||||||
bs->file->exact_filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
opts = qdict_new();
|
|
||||||
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkdebug")));
|
|
||||||
|
|
||||||
QINCREF(bs->file->full_open_options);
|
|
||||||
qdict_put_obj(opts, "image", QOBJECT(bs->file->full_open_options));
|
|
||||||
|
|
||||||
for (e = qdict_first(bs->options); e; e = qdict_next(bs->options, e)) {
|
|
||||||
if (strcmp(qdict_entry_key(e), "x-image") &&
|
|
||||||
strcmp(qdict_entry_key(e), "image") &&
|
|
||||||
strncmp(qdict_entry_key(e), "image.", strlen("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 BlockDriver bdrv_blkdebug = {
|
static BlockDriver bdrv_blkdebug = {
|
||||||
.format_name = "blkdebug",
|
.format_name = "blkdebug",
|
||||||
.protocol_name = "blkdebug",
|
.protocol_name = "blkdebug",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVBlkdebugState),
|
.instance_size = sizeof(BDRVBlkdebugState),
|
||||||
|
|
||||||
.bdrv_parse_filename = blkdebug_parse_filename,
|
|
||||||
.bdrv_file_open = blkdebug_open,
|
.bdrv_file_open = blkdebug_open,
|
||||||
.bdrv_close = blkdebug_close,
|
.bdrv_close = blkdebug_close,
|
||||||
.bdrv_getlength = blkdebug_getlength,
|
.bdrv_flush = blkdebug_flush,
|
||||||
.bdrv_refresh_filename = blkdebug_refresh_filename,
|
|
||||||
|
|
||||||
.bdrv_aio_readv = blkdebug_aio_readv,
|
.bdrv_aio_readv = blkdebug_aio_readv,
|
||||||
.bdrv_aio_writev = blkdebug_aio_writev,
|
.bdrv_aio_writev = blkdebug_aio_writev,
|
||||||
.bdrv_aio_flush = blkdebug_aio_flush,
|
.bdrv_aio_flush = blkdebug_aio_flush,
|
||||||
|
|
||||||
.bdrv_debug_event = blkdebug_debug_event,
|
.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,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_blkdebug_init(void)
|
static void bdrv_blkdebug_init(void)
|
||||||
|
|||||||
@@ -8,10 +8,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include "qemu/sockets.h" /* for EINPROGRESS on Windows */
|
#include "qemu_socket.h" /* for EINPROGRESS on Windows */
|
||||||
#include "block/block_int.h"
|
#include "block_int.h"
|
||||||
#include "qapi/qmp/qdict.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
BlockDriverState *test_file;
|
BlockDriverState *test_file;
|
||||||
@@ -19,7 +17,7 @@ typedef struct {
|
|||||||
|
|
||||||
typedef struct BlkverifyAIOCB BlkverifyAIOCB;
|
typedef struct BlkverifyAIOCB BlkverifyAIOCB;
|
||||||
struct BlkverifyAIOCB {
|
struct BlkverifyAIOCB {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
QEMUBH *bh;
|
QEMUBH *bh;
|
||||||
|
|
||||||
/* Request metadata */
|
/* Request metadata */
|
||||||
@@ -29,6 +27,7 @@ struct BlkverifyAIOCB {
|
|||||||
|
|
||||||
int ret; /* first completed request's result */
|
int ret; /* first completed request's result */
|
||||||
unsigned int done; /* completion counter */
|
unsigned int done; /* completion counter */
|
||||||
|
bool *finished; /* completion signal for cancel */
|
||||||
|
|
||||||
QEMUIOVector *qiov; /* user I/O vector */
|
QEMUIOVector *qiov; /* user I/O vector */
|
||||||
QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
|
QEMUIOVector raw_qiov; /* cloned I/O vector for raw file */
|
||||||
@@ -37,8 +36,21 @@ struct BlkverifyAIOCB {
|
|||||||
void (*verify)(BlkverifyAIOCB *acb);
|
void (*verify)(BlkverifyAIOCB *acb);
|
||||||
};
|
};
|
||||||
|
|
||||||
static const AIOCBInfo blkverify_aiocb_info = {
|
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 AIOPool blkverify_aio_pool = {
|
||||||
.aiocb_size = sizeof(BlkverifyAIOCB),
|
.aiocb_size = sizeof(BlkverifyAIOCB),
|
||||||
|
.cancel = blkverify_aio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
||||||
@@ -57,104 +69,61 @@ static void GCC_FMT_ATTR(2, 3) blkverify_err(BlkverifyAIOCB *acb,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
|
/* Valid blkverify filenames look like blkverify:path/to/raw_image:path/to/image */
|
||||||
static void blkverify_parse_filename(const char *filename, QDict *options,
|
static int blkverify_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
const char *c;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
QString *raw_path;
|
int ret;
|
||||||
|
char *raw, *c;
|
||||||
|
|
||||||
/* Parse the blkverify: prefix */
|
/* Parse the blkverify: prefix */
|
||||||
if (!strstart(filename, "blkverify:", &filename)) {
|
if (strncmp(filename, "blkverify:", strlen("blkverify:"))) {
|
||||||
/* There was no prefix; therefore, all options have to be already
|
return -EINVAL;
|
||||||
present in the QDict (except for the filename) */
|
|
||||||
qdict_put(options, "x-image", qstring_from_str(filename));
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
filename += strlen("blkverify:");
|
||||||
|
|
||||||
/* Parse the raw image filename */
|
/* Parse the raw image filename */
|
||||||
c = strchr(filename, ':');
|
c = strchr(filename, ':');
|
||||||
if (c == NULL) {
|
if (c == NULL) {
|
||||||
error_setg(errp, "blkverify requires raw copy and original image path");
|
return -EINVAL;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO Implement option pass-through and set raw.filename here */
|
raw = strdup(filename);
|
||||||
raw_path = qstring_from_substr(filename, 0, c - filename - 1);
|
raw[c - filename] = '\0';
|
||||||
qdict_put(options, "x-raw", raw_path);
|
ret = bdrv_file_open(&bs->file, raw, flags);
|
||||||
|
free(raw);
|
||||||
/* TODO Allow multi-level nesting and set file.filename here */
|
|
||||||
filename = c + 1;
|
|
||||||
qdict_put(options, "x-image", qstring_from_str(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 */
|
|
||||||
assert(bs->file == NULL);
|
|
||||||
ret = bdrv_open_image(&bs->file, qemu_opt_get(opts, "x-raw"), options,
|
|
||||||
"raw", flags | BDRV_O_PROTOCOL, false, &local_err);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
return ret;
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
|
filename = c + 1;
|
||||||
|
|
||||||
/* Open the test file */
|
/* Open the test file */
|
||||||
assert(s->test_file == NULL);
|
s->test_file = bdrv_new("");
|
||||||
ret = bdrv_open_image(&s->test_file, qemu_opt_get(opts, "x-image"), options,
|
ret = bdrv_open(s->test_file, filename, flags, NULL);
|
||||||
"test", flags, false, &local_err);
|
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
error_propagate(errp, local_err);
|
bdrv_delete(s->test_file);
|
||||||
s->test_file = NULL;
|
s->test_file = NULL;
|
||||||
goto fail;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = 0;
|
return 0;
|
||||||
fail:
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blkverify_close(BlockDriverState *bs)
|
static void blkverify_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
|
|
||||||
bdrv_unref(s->test_file);
|
bdrv_delete(s->test_file);
|
||||||
s->test_file = NULL;
|
s->test_file = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int blkverify_flush(BlockDriverState *bs)
|
||||||
|
{
|
||||||
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
|
|
||||||
|
/* Only flush test file, the raw file is not important */
|
||||||
|
return bdrv_flush(s->test_file);
|
||||||
|
}
|
||||||
|
|
||||||
static int64_t blkverify_getlength(BlockDriverState *bs)
|
static int64_t blkverify_getlength(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
@@ -162,13 +131,117 @@ static int64_t blkverify_getlength(BlockDriverState *bs)
|
|||||||
return bdrv_getlength(s->test_file);
|
return bdrv_getlength(s->test_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
ssize_t offset = 0;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int src_index;
|
||||||
|
struct iovec *src_iov;
|
||||||
|
void *dest_base;
|
||||||
|
} IOVectorSortElem;
|
||||||
|
|
||||||
|
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 sortelem_cmp_src_index(const void *a, const void *b)
|
||||||
|
{
|
||||||
|
const IOVectorSortElem *elem_a = a;
|
||||||
|
const IOVectorSortElem *elem_b = b;
|
||||||
|
|
||||||
|
return elem_a->src_index - elem_b->src_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
||||||
int64_t sector_num, QEMUIOVector *qiov,
|
int64_t sector_num, QEMUIOVector *qiov,
|
||||||
int nb_sectors,
|
int nb_sectors,
|
||||||
BlockCompletionFunc *cb,
|
BlockDriverCompletionFunc *cb,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aiocb_info, bs, cb, opaque);
|
BlkverifyAIOCB *acb = qemu_aio_get(&blkverify_aio_pool, bs, cb, opaque);
|
||||||
|
|
||||||
acb->bh = NULL;
|
acb->bh = NULL;
|
||||||
acb->is_write = is_write;
|
acb->is_write = is_write;
|
||||||
@@ -179,6 +252,7 @@ static BlkverifyAIOCB *blkverify_aio_get(BlockDriverState *bs, bool is_write,
|
|||||||
acb->qiov = qiov;
|
acb->qiov = qiov;
|
||||||
acb->buf = NULL;
|
acb->buf = NULL;
|
||||||
acb->verify = NULL;
|
acb->verify = NULL;
|
||||||
|
acb->finished = NULL;
|
||||||
return acb;
|
return acb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -192,7 +266,10 @@ static void blkverify_aio_bh(void *opaque)
|
|||||||
qemu_vfree(acb->buf);
|
qemu_vfree(acb->buf);
|
||||||
}
|
}
|
||||||
acb->common.cb(acb->common.opaque, acb->ret);
|
acb->common.cb(acb->common.opaque, acb->ret);
|
||||||
qemu_aio_unref(acb);
|
if (acb->finished) {
|
||||||
|
*acb->finished = true;
|
||||||
|
}
|
||||||
|
qemu_aio_release(acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void blkverify_aio_cb(void *opaque, int ret)
|
static void blkverify_aio_cb(void *opaque, int ret)
|
||||||
@@ -213,8 +290,7 @@ static void blkverify_aio_cb(void *opaque, int ret)
|
|||||||
acb->verify(acb);
|
acb->verify(acb);
|
||||||
}
|
}
|
||||||
|
|
||||||
acb->bh = aio_bh_new(bdrv_get_aio_context(acb->common.bs),
|
acb->bh = qemu_bh_new(blkverify_aio_bh, acb);
|
||||||
blkverify_aio_bh, acb);
|
|
||||||
qemu_bh_schedule(acb->bh);
|
qemu_bh_schedule(acb->bh);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -222,16 +298,16 @@ static void blkverify_aio_cb(void *opaque, int ret)
|
|||||||
|
|
||||||
static void blkverify_verify_readv(BlkverifyAIOCB *acb)
|
static void blkverify_verify_readv(BlkverifyAIOCB *acb)
|
||||||
{
|
{
|
||||||
ssize_t offset = qemu_iovec_compare(acb->qiov, &acb->raw_qiov);
|
ssize_t offset = blkverify_iovec_compare(acb->qiov, &acb->raw_qiov);
|
||||||
if (offset != -1) {
|
if (offset != -1) {
|
||||||
blkverify_err(acb, "contents mismatch in sector %" PRId64,
|
blkverify_err(acb, "contents mismatch in sector %" PRId64,
|
||||||
acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
|
acb->sector_num + (int64_t)(offset / BDRV_SECTOR_SIZE));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
|
BlkverifyAIOCB *acb = blkverify_aio_get(bs, false, sector_num, qiov,
|
||||||
@@ -240,32 +316,40 @@ static BlockAIOCB *blkverify_aio_readv(BlockDriverState *bs,
|
|||||||
acb->verify = blkverify_verify_readv;
|
acb->verify = blkverify_verify_readv;
|
||||||
acb->buf = qemu_blockalign(bs->file, qiov->size);
|
acb->buf = qemu_blockalign(bs->file, qiov->size);
|
||||||
qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
|
qemu_iovec_init(&acb->raw_qiov, acb->qiov->niov);
|
||||||
qemu_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
|
blkverify_iovec_clone(&acb->raw_qiov, qiov, acb->buf);
|
||||||
|
|
||||||
bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
|
if (!bdrv_aio_readv(s->test_file, sector_num, qiov, nb_sectors,
|
||||||
blkverify_aio_cb, acb);
|
blkverify_aio_cb, acb)) {
|
||||||
bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
|
blkverify_aio_cb(acb, -EIO);
|
||||||
blkverify_aio_cb, acb);
|
}
|
||||||
|
if (!bdrv_aio_readv(bs->file, sector_num, &acb->raw_qiov, nb_sectors,
|
||||||
|
blkverify_aio_cb, acb)) {
|
||||||
|
blkverify_aio_cb(acb, -EIO);
|
||||||
|
}
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkverify_aio_writev(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkverify_aio_writev(BlockDriverState *bs,
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
|
BlkverifyAIOCB *acb = blkverify_aio_get(bs, true, sector_num, qiov,
|
||||||
nb_sectors, cb, opaque);
|
nb_sectors, cb, opaque);
|
||||||
|
|
||||||
bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
|
if (!bdrv_aio_writev(s->test_file, sector_num, qiov, nb_sectors,
|
||||||
blkverify_aio_cb, acb);
|
blkverify_aio_cb, acb)) {
|
||||||
bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
|
blkverify_aio_cb(acb, -EIO);
|
||||||
blkverify_aio_cb, acb);
|
}
|
||||||
|
if (!bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors,
|
||||||
|
blkverify_aio_cb, acb)) {
|
||||||
|
blkverify_aio_cb(acb, -EIO);
|
||||||
|
}
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
static BlockDriverAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
||||||
BlockCompletionFunc *cb,
|
BlockDriverCompletionFunc *cb,
|
||||||
void *opaque)
|
void *opaque)
|
||||||
{
|
{
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
BDRVBlkverifyState *s = bs->opaque;
|
||||||
@@ -274,82 +358,21 @@ static BlockAIOCB *blkverify_aio_flush(BlockDriverState *bs,
|
|||||||
return bdrv_aio_flush(s->test_file, cb, opaque);
|
return bdrv_aio_flush(s->test_file, cb, opaque);
|
||||||
}
|
}
|
||||||
|
|
||||||
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, candidate);
|
|
||||||
|
|
||||||
if (perm) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_recurse_is_first_non_filter(s->test_file, candidate);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Propagate AioContext changes to ->test_file */
|
|
||||||
static void blkverify_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
bdrv_detach_aio_context(s->test_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blkverify_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
bdrv_attach_aio_context(s->test_file, new_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blkverify_refresh_filename(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVBlkverifyState *s = bs->opaque;
|
|
||||||
|
|
||||||
/* bs->file has already been refreshed */
|
|
||||||
bdrv_refresh_filename(s->test_file);
|
|
||||||
|
|
||||||
if (bs->file->full_open_options && s->test_file->full_open_options) {
|
|
||||||
QDict *opts = qdict_new();
|
|
||||||
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("blkverify")));
|
|
||||||
|
|
||||||
QINCREF(bs->file->full_open_options);
|
|
||||||
qdict_put_obj(opts, "raw", QOBJECT(bs->file->full_open_options));
|
|
||||||
QINCREF(s->test_file->full_open_options);
|
|
||||||
qdict_put_obj(opts, "test", QOBJECT(s->test_file->full_open_options));
|
|
||||||
|
|
||||||
bs->full_open_options = opts;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bs->file->exact_filename[0] && s->test_file->exact_filename[0]) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"blkverify:%s:%s",
|
|
||||||
bs->file->exact_filename, s->test_file->exact_filename);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockDriver bdrv_blkverify = {
|
static BlockDriver bdrv_blkverify = {
|
||||||
.format_name = "blkverify",
|
.format_name = "blkverify",
|
||||||
.protocol_name = "blkverify",
|
.protocol_name = "blkverify",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVBlkverifyState),
|
.instance_size = sizeof(BDRVBlkverifyState),
|
||||||
|
|
||||||
.bdrv_parse_filename = blkverify_parse_filename,
|
.bdrv_getlength = blkverify_getlength,
|
||||||
|
|
||||||
.bdrv_file_open = blkverify_open,
|
.bdrv_file_open = blkverify_open,
|
||||||
.bdrv_close = blkverify_close,
|
.bdrv_close = blkverify_close,
|
||||||
.bdrv_getlength = blkverify_getlength,
|
.bdrv_flush = blkverify_flush,
|
||||||
.bdrv_refresh_filename = blkverify_refresh_filename,
|
|
||||||
|
|
||||||
.bdrv_aio_readv = blkverify_aio_readv,
|
.bdrv_aio_readv = blkverify_aio_readv,
|
||||||
.bdrv_aio_writev = blkverify_aio_writev,
|
.bdrv_aio_writev = blkverify_aio_writev,
|
||||||
.bdrv_aio_flush = blkverify_aio_flush,
|
.bdrv_aio_flush = blkverify_aio_flush,
|
||||||
|
|
||||||
.bdrv_attach_aio_context = blkverify_attach_aio_context,
|
|
||||||
.bdrv_detach_aio_context = blkverify_detach_aio_context,
|
|
||||||
|
|
||||||
.is_filter = true,
|
|
||||||
.bdrv_recurse_is_first_non_filter = blkverify_recurse_is_first_non_filter,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_blkverify_init(void)
|
static void bdrv_blkverify_init(void)
|
||||||
|
|||||||
@@ -1,894 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Block backends
|
|
||||||
*
|
|
||||||
* Copyright (C) 2014 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Markus Armbruster <armbru@redhat.com>,
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU LGPL, version 2.1
|
|
||||||
* or later. See the COPYING.LIB file in the top-level directory.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "sysemu/block-backend.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "sysemu/blockdev.h"
|
|
||||||
#include "qapi-event.h"
|
|
||||||
|
|
||||||
/* Number of coroutines to reserve per attached device model */
|
|
||||||
#define COROUTINE_POOL_RESERVATION 64
|
|
||||||
|
|
||||||
struct BlockBackend {
|
|
||||||
char *name;
|
|
||||||
int refcnt;
|
|
||||||
BlockDriverState *bs;
|
|
||||||
DriveInfo *legacy_dinfo; /* null unless created by drive_new() */
|
|
||||||
QTAILQ_ENTRY(BlockBackend) link; /* for blk_backends */
|
|
||||||
|
|
||||||
void *dev; /* attached device model, if any */
|
|
||||||
/* TODO change to DeviceState when all users are qdevified */
|
|
||||||
const BlockDevOps *dev_ops;
|
|
||||||
void *dev_opaque;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct BlockBackendAIOCB {
|
|
||||||
BlockAIOCB common;
|
|
||||||
QEMUBH *bh;
|
|
||||||
int ret;
|
|
||||||
} BlockBackendAIOCB;
|
|
||||||
|
|
||||||
static const AIOCBInfo block_backend_aiocb_info = {
|
|
||||||
.aiocb_size = sizeof(BlockBackendAIOCB),
|
|
||||||
};
|
|
||||||
|
|
||||||
static void drive_info_del(DriveInfo *dinfo);
|
|
||||||
|
|
||||||
/* All the BlockBackends (except for hidden ones) */
|
|
||||||
static QTAILQ_HEAD(, BlockBackend) blk_backends =
|
|
||||||
QTAILQ_HEAD_INITIALIZER(blk_backends);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Create a new BlockBackend with @name, with a reference count of one.
|
|
||||||
* @name must not be null or empty.
|
|
||||||
* Fail if a BlockBackend with this name already exists.
|
|
||||||
* Store an error through @errp on failure, unless it's null.
|
|
||||||
* Return the new BlockBackend on success, null on failure.
|
|
||||||
*/
|
|
||||||
BlockBackend *blk_new(const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
BlockBackend *blk;
|
|
||||||
|
|
||||||
assert(name && name[0]);
|
|
||||||
if (!id_wellformed(name)) {
|
|
||||||
error_setg(errp, "Invalid device name");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (blk_by_name(name)) {
|
|
||||||
error_setg(errp, "Device with id '%s' already exists", name);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (bdrv_find_node(name)) {
|
|
||||||
error_setg(errp,
|
|
||||||
"Device name '%s' conflicts with an existing node name",
|
|
||||||
name);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
blk = g_new0(BlockBackend, 1);
|
|
||||||
blk->name = g_strdup(name);
|
|
||||||
blk->refcnt = 1;
|
|
||||||
QTAILQ_INSERT_TAIL(&blk_backends, blk, link);
|
|
||||||
return blk;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Create a new BlockBackend with a new BlockDriverState attached.
|
|
||||||
* Otherwise just like blk_new(), which see.
|
|
||||||
*/
|
|
||||||
BlockBackend *blk_new_with_bs(const char *name, Error **errp)
|
|
||||||
{
|
|
||||||
BlockBackend *blk;
|
|
||||||
BlockDriverState *bs;
|
|
||||||
|
|
||||||
blk = blk_new(name, errp);
|
|
||||||
if (!blk) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bs = bdrv_new_root();
|
|
||||||
blk->bs = bs;
|
|
||||||
bs->blk = blk;
|
|
||||||
return blk;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState.
|
|
||||||
*
|
|
||||||
* Just as with bdrv_open(), after having called this function the reference to
|
|
||||||
* @options belongs to the block layer (even on failure).
|
|
||||||
*
|
|
||||||
* TODO: Remove @filename and @flags; it should be possible to specify a whole
|
|
||||||
* BDS tree just by specifying the @options QDict (or @reference,
|
|
||||||
* alternatively). At the time of adding this function, this is not possible,
|
|
||||||
* though, so callers of this function have to be able to specify @filename and
|
|
||||||
* @flags.
|
|
||||||
*/
|
|
||||||
BlockBackend *blk_new_open(const char *name, const char *filename,
|
|
||||||
const char *reference, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
BlockBackend *blk;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
blk = blk_new_with_bs(name, errp);
|
|
||||||
if (!blk) {
|
|
||||||
QDECREF(options);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_open(&blk->bs, filename, reference, options, flags, NULL, errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
blk_unref(blk);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
return blk;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void blk_delete(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
assert(!blk->refcnt);
|
|
||||||
assert(!blk->dev);
|
|
||||||
if (blk->bs) {
|
|
||||||
assert(blk->bs->blk == blk);
|
|
||||||
blk->bs->blk = NULL;
|
|
||||||
bdrv_unref(blk->bs);
|
|
||||||
blk->bs = NULL;
|
|
||||||
}
|
|
||||||
/* Avoid double-remove after blk_hide_on_behalf_of_hmp_drive_del() */
|
|
||||||
if (blk->name[0]) {
|
|
||||||
QTAILQ_REMOVE(&blk_backends, blk, link);
|
|
||||||
}
|
|
||||||
g_free(blk->name);
|
|
||||||
drive_info_del(blk->legacy_dinfo);
|
|
||||||
g_free(blk);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void drive_info_del(DriveInfo *dinfo)
|
|
||||||
{
|
|
||||||
if (!dinfo) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
qemu_opts_del(dinfo->opts);
|
|
||||||
g_free(dinfo->serial);
|
|
||||||
g_free(dinfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Increment @blk's reference count.
|
|
||||||
* @blk must not be null.
|
|
||||||
*/
|
|
||||||
void blk_ref(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
blk->refcnt++;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Decrement @blk's reference count.
|
|
||||||
* If this drops it to zero, destroy @blk.
|
|
||||||
* For convenience, do nothing if @blk is null.
|
|
||||||
*/
|
|
||||||
void blk_unref(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
if (blk) {
|
|
||||||
assert(blk->refcnt > 0);
|
|
||||||
if (!--blk->refcnt) {
|
|
||||||
blk_delete(blk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the BlockBackend after @blk.
|
|
||||||
* If @blk is null, return the first one.
|
|
||||||
* Else, return @blk's next sibling, which may be null.
|
|
||||||
*
|
|
||||||
* To iterate over all BlockBackends, do
|
|
||||||
* for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
|
|
||||||
* ...
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
BlockBackend *blk_next(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return blk ? QTAILQ_NEXT(blk, link) : QTAILQ_FIRST(&blk_backends);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return @blk's name, a non-null string.
|
|
||||||
* Wart: the name is empty iff @blk has been hidden with
|
|
||||||
* blk_hide_on_behalf_of_hmp_drive_del().
|
|
||||||
*/
|
|
||||||
const char *blk_name(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return blk->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the BlockBackend with name @name if it exists, else null.
|
|
||||||
* @name must not be null.
|
|
||||||
*/
|
|
||||||
BlockBackend *blk_by_name(const char *name)
|
|
||||||
{
|
|
||||||
BlockBackend *blk;
|
|
||||||
|
|
||||||
assert(name);
|
|
||||||
QTAILQ_FOREACH(blk, &blk_backends, link) {
|
|
||||||
if (!strcmp(name, blk->name)) {
|
|
||||||
return blk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the BlockDriverState attached to @blk if any, else null.
|
|
||||||
*/
|
|
||||||
BlockDriverState *blk_bs(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return blk->bs;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return @blk's DriveInfo if any, else null.
|
|
||||||
*/
|
|
||||||
DriveInfo *blk_legacy_dinfo(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return blk->legacy_dinfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Set @blk's DriveInfo to @dinfo, and return it.
|
|
||||||
* @blk must not have a DriveInfo set already.
|
|
||||||
* No other BlockBackend may have the same DriveInfo set.
|
|
||||||
*/
|
|
||||||
DriveInfo *blk_set_legacy_dinfo(BlockBackend *blk, DriveInfo *dinfo)
|
|
||||||
{
|
|
||||||
assert(!blk->legacy_dinfo);
|
|
||||||
return blk->legacy_dinfo = dinfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the BlockBackend with DriveInfo @dinfo.
|
|
||||||
* It must exist.
|
|
||||||
*/
|
|
||||||
BlockBackend *blk_by_legacy_dinfo(DriveInfo *dinfo)
|
|
||||||
{
|
|
||||||
BlockBackend *blk;
|
|
||||||
|
|
||||||
QTAILQ_FOREACH(blk, &blk_backends, link) {
|
|
||||||
if (blk->legacy_dinfo == dinfo) {
|
|
||||||
return blk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Hide @blk.
|
|
||||||
* @blk must not have been hidden already.
|
|
||||||
* Make attached BlockDriverState, if any, anonymous.
|
|
||||||
* Once hidden, @blk is invisible to all functions that don't receive
|
|
||||||
* it as argument. For example, blk_by_name() won't return it.
|
|
||||||
* Strictly for use by do_drive_del().
|
|
||||||
* TODO get rid of it!
|
|
||||||
*/
|
|
||||||
void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
QTAILQ_REMOVE(&blk_backends, blk, link);
|
|
||||||
blk->name[0] = 0;
|
|
||||||
if (blk->bs) {
|
|
||||||
bdrv_make_anon(blk->bs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Attach device model @dev to @blk.
|
|
||||||
* Return 0 on success, -EBUSY when a device model is attached already.
|
|
||||||
*/
|
|
||||||
int blk_attach_dev(BlockBackend *blk, void *dev)
|
|
||||||
/* TODO change to DeviceState *dev when all users are qdevified */
|
|
||||||
{
|
|
||||||
if (blk->dev) {
|
|
||||||
return -EBUSY;
|
|
||||||
}
|
|
||||||
blk_ref(blk);
|
|
||||||
blk->dev = dev;
|
|
||||||
bdrv_iostatus_reset(blk->bs);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Attach device model @dev to @blk.
|
|
||||||
* @blk must not have a device model attached already.
|
|
||||||
* TODO qdevified devices don't use this, remove when devices are qdevified
|
|
||||||
*/
|
|
||||||
void blk_attach_dev_nofail(BlockBackend *blk, void *dev)
|
|
||||||
{
|
|
||||||
if (blk_attach_dev(blk, dev) < 0) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Detach device model @dev from @blk.
|
|
||||||
* @dev must be currently attached to @blk.
|
|
||||||
*/
|
|
||||||
void blk_detach_dev(BlockBackend *blk, void *dev)
|
|
||||||
/* TODO change to DeviceState *dev when all users are qdevified */
|
|
||||||
{
|
|
||||||
assert(blk->dev == dev);
|
|
||||||
blk->dev = NULL;
|
|
||||||
blk->dev_ops = NULL;
|
|
||||||
blk->dev_opaque = NULL;
|
|
||||||
bdrv_set_guest_block_size(blk->bs, 512);
|
|
||||||
blk_unref(blk);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Return the device model attached to @blk if any, else null.
|
|
||||||
*/
|
|
||||||
void *blk_get_attached_dev(BlockBackend *blk)
|
|
||||||
/* TODO change to return DeviceState * when all users are qdevified */
|
|
||||||
{
|
|
||||||
return blk->dev;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Set @blk's device model callbacks to @ops.
|
|
||||||
* @opaque is the opaque argument to pass to the callbacks.
|
|
||||||
* This is for use by device models.
|
|
||||||
*/
|
|
||||||
void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
blk->dev_ops = ops;
|
|
||||||
blk->dev_opaque = opaque;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Notify @blk's attached device model of media change.
|
|
||||||
* If @load is true, notify of media load.
|
|
||||||
* Else, notify of media eject.
|
|
||||||
* Also send DEVICE_TRAY_MOVED events as appropriate.
|
|
||||||
*/
|
|
||||||
void blk_dev_change_media_cb(BlockBackend *blk, bool load)
|
|
||||||
{
|
|
||||||
if (blk->dev_ops && blk->dev_ops->change_media_cb) {
|
|
||||||
bool tray_was_closed = !blk_dev_is_tray_open(blk);
|
|
||||||
|
|
||||||
blk->dev_ops->change_media_cb(blk->dev_opaque, load);
|
|
||||||
if (tray_was_closed) {
|
|
||||||
/* tray open */
|
|
||||||
qapi_event_send_device_tray_moved(blk_name(blk),
|
|
||||||
true, &error_abort);
|
|
||||||
}
|
|
||||||
if (load) {
|
|
||||||
/* tray close */
|
|
||||||
qapi_event_send_device_tray_moved(blk_name(blk),
|
|
||||||
false, &error_abort);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Does @blk's attached device model have removable media?
|
|
||||||
* %true if no device model is attached.
|
|
||||||
*/
|
|
||||||
bool blk_dev_has_removable_media(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return !blk->dev || (blk->dev_ops && blk->dev_ops->change_media_cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Notify @blk's attached device model of a media eject request.
|
|
||||||
* If @force is true, the medium is about to be yanked out forcefully.
|
|
||||||
*/
|
|
||||||
void blk_dev_eject_request(BlockBackend *blk, bool force)
|
|
||||||
{
|
|
||||||
if (blk->dev_ops && blk->dev_ops->eject_request_cb) {
|
|
||||||
blk->dev_ops->eject_request_cb(blk->dev_opaque, force);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Does @blk's attached device model have a tray, and is it open?
|
|
||||||
*/
|
|
||||||
bool blk_dev_is_tray_open(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
if (blk->dev_ops && blk->dev_ops->is_tray_open) {
|
|
||||||
return blk->dev_ops->is_tray_open(blk->dev_opaque);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Does @blk's attached device model have the medium locked?
|
|
||||||
* %false if the device model has no such lock.
|
|
||||||
*/
|
|
||||||
bool blk_dev_is_medium_locked(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
if (blk->dev_ops && blk->dev_ops->is_medium_locked) {
|
|
||||||
return blk->dev_ops->is_medium_locked(blk->dev_opaque);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Notify @blk's attached device model of a backend size change.
|
|
||||||
*/
|
|
||||||
void blk_dev_resize_cb(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
if (blk->dev_ops && blk->dev_ops->resize_cb) {
|
|
||||||
blk->dev_ops->resize_cb(blk->dev_opaque);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_iostatus_enable(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
bdrv_iostatus_enable(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int blk_check_byte_request(BlockBackend *blk, int64_t offset,
|
|
||||||
size_t size)
|
|
||||||
{
|
|
||||||
int64_t len;
|
|
||||||
|
|
||||||
if (size > INT_MAX) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!blk_is_inserted(blk)) {
|
|
||||||
return -ENOMEDIUM;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = blk_getlength(blk);
|
|
||||||
if (len < 0) {
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (offset < 0) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (offset > len || len - offset < size) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int blk_check_request(BlockBackend *blk, int64_t sector_num,
|
|
||||||
int nb_sectors)
|
|
||||||
{
|
|
||||||
if (sector_num < 0 || sector_num > INT64_MAX / BDRV_SECTOR_SIZE) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) {
|
|
||||||
return -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return blk_check_byte_request(blk, sector_num * BDRV_SECTOR_SIZE,
|
|
||||||
nb_sectors * BDRV_SECTOR_SIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_read(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
|
|
||||||
int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_read(blk->bs, sector_num, buf, nb_sectors);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_read_unthrottled(BlockBackend *blk, int64_t sector_num, uint8_t *buf,
|
|
||||||
int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_read_unthrottled(blk->bs, sector_num, buf, nb_sectors);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_write(BlockBackend *blk, int64_t sector_num, const uint8_t *buf,
|
|
||||||
int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_write(blk->bs, sector_num, buf, nb_sectors);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void error_callback_bh(void *opaque)
|
|
||||||
{
|
|
||||||
struct BlockBackendAIOCB *acb = opaque;
|
|
||||||
qemu_bh_delete(acb->bh);
|
|
||||||
acb->common.cb(acb->common.opaque, acb->ret);
|
|
||||||
qemu_aio_unref(acb);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *abort_aio_request(BlockBackend *blk, BlockCompletionFunc *cb,
|
|
||||||
void *opaque, int ret)
|
|
||||||
{
|
|
||||||
struct BlockBackendAIOCB *acb;
|
|
||||||
QEMUBH *bh;
|
|
||||||
|
|
||||||
acb = blk_aio_get(&block_backend_aiocb_info, blk, cb, opaque);
|
|
||||||
acb->ret = ret;
|
|
||||||
|
|
||||||
bh = aio_bh_new(blk_get_aio_context(blk), error_callback_bh, acb);
|
|
||||||
acb->bh = bh;
|
|
||||||
qemu_bh_schedule(bh);
|
|
||||||
|
|
||||||
return &acb->common;
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *blk_aio_write_zeroes(BlockBackend *blk, int64_t sector_num,
|
|
||||||
int nb_sectors, BdrvRequestFlags flags,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return abort_aio_request(blk, cb, opaque, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_write_zeroes(blk->bs, sector_num, nb_sectors, flags,
|
|
||||||
cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_pread(BlockBackend *blk, int64_t offset, void *buf, int count)
|
|
||||||
{
|
|
||||||
int ret = blk_check_byte_request(blk, offset, count);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_pread(blk->bs, offset, buf, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_pwrite(BlockBackend *blk, int64_t offset, const void *buf, int count)
|
|
||||||
{
|
|
||||||
int ret = blk_check_byte_request(blk, offset, count);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_pwrite(blk->bs, offset, buf, count);
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t blk_getlength(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_getlength(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_get_geometry(BlockBackend *blk, uint64_t *nb_sectors_ptr)
|
|
||||||
{
|
|
||||||
bdrv_get_geometry(blk->bs, nb_sectors_ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
int64_t blk_nb_sectors(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_nb_sectors(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *blk_aio_readv(BlockBackend *blk, int64_t sector_num,
|
|
||||||
QEMUIOVector *iov, int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return abort_aio_request(blk, cb, opaque, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_readv(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *blk_aio_writev(BlockBackend *blk, int64_t sector_num,
|
|
||||||
QEMUIOVector *iov, int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return abort_aio_request(blk, cb, opaque, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_writev(blk->bs, sector_num, iov, nb_sectors, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *blk_aio_flush(BlockBackend *blk,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
return bdrv_aio_flush(blk->bs, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *blk_aio_discard(BlockBackend *blk,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return abort_aio_request(blk, cb, opaque, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_discard(blk->bs, sector_num, nb_sectors, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_aio_cancel(BlockAIOCB *acb)
|
|
||||||
{
|
|
||||||
bdrv_aio_cancel(acb);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_aio_cancel_async(BlockAIOCB *acb)
|
|
||||||
{
|
|
||||||
bdrv_aio_cancel_async(acb);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_aio_multiwrite(BlockBackend *blk, BlockRequest *reqs, int num_reqs)
|
|
||||||
{
|
|
||||||
int i, ret;
|
|
||||||
|
|
||||||
for (i = 0; i < num_reqs; i++) {
|
|
||||||
ret = blk_check_request(blk, reqs[i].sector, reqs[i].nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_aio_multiwrite(blk->bs, reqs, num_reqs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_ioctl(BlockBackend *blk, unsigned long int req, void *buf)
|
|
||||||
{
|
|
||||||
return bdrv_ioctl(blk->bs, req, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *blk_aio_ioctl(BlockBackend *blk, unsigned long int req, void *buf,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
return bdrv_aio_ioctl(blk->bs, req, buf, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_co_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_co_discard(blk->bs, sector_num, nb_sectors);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_co_flush(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_co_flush(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_flush(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_flush(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_flush_all(void)
|
|
||||||
{
|
|
||||||
return bdrv_flush_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_drain_all(void)
|
|
||||||
{
|
|
||||||
bdrv_drain_all();
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockdevOnError blk_get_on_error(BlockBackend *blk, bool is_read)
|
|
||||||
{
|
|
||||||
return bdrv_get_on_error(blk->bs, is_read);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockErrorAction blk_get_error_action(BlockBackend *blk, bool is_read,
|
|
||||||
int error)
|
|
||||||
{
|
|
||||||
return bdrv_get_error_action(blk->bs, is_read, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_error_action(BlockBackend *blk, BlockErrorAction action,
|
|
||||||
bool is_read, int error)
|
|
||||||
{
|
|
||||||
bdrv_error_action(blk->bs, action, is_read, error);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_is_read_only(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_is_read_only(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_is_sg(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_is_sg(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_enable_write_cache(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_enable_write_cache(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_set_enable_write_cache(BlockBackend *blk, bool wce)
|
|
||||||
{
|
|
||||||
bdrv_set_enable_write_cache(blk->bs, wce);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_invalidate_cache(BlockBackend *blk, Error **errp)
|
|
||||||
{
|
|
||||||
bdrv_invalidate_cache(blk->bs, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_is_inserted(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_is_inserted(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_lock_medium(BlockBackend *blk, bool locked)
|
|
||||||
{
|
|
||||||
bdrv_lock_medium(blk->bs, locked);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_eject(BlockBackend *blk, bool eject_flag)
|
|
||||||
{
|
|
||||||
bdrv_eject(blk->bs, eject_flag);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_get_flags(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_get_flags(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_get_max_transfer_length(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return blk->bs->bl.max_transfer_length;
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_set_guest_block_size(BlockBackend *blk, int align)
|
|
||||||
{
|
|
||||||
bdrv_set_guest_block_size(blk->bs, align);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *blk_blockalign(BlockBackend *blk, size_t size)
|
|
||||||
{
|
|
||||||
return qemu_blockalign(blk ? blk->bs : NULL, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool blk_op_is_blocked(BlockBackend *blk, BlockOpType op, Error **errp)
|
|
||||||
{
|
|
||||||
return bdrv_op_is_blocked(blk->bs, op, errp);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_op_unblock(BlockBackend *blk, BlockOpType op, Error *reason)
|
|
||||||
{
|
|
||||||
bdrv_op_unblock(blk->bs, op, reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_op_block_all(BlockBackend *blk, Error *reason)
|
|
||||||
{
|
|
||||||
bdrv_op_block_all(blk->bs, reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_op_unblock_all(BlockBackend *blk, Error *reason)
|
|
||||||
{
|
|
||||||
bdrv_op_unblock_all(blk->bs, reason);
|
|
||||||
}
|
|
||||||
|
|
||||||
AioContext *blk_get_aio_context(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_get_aio_context(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_set_aio_context(BlockBackend *blk, AioContext *new_context)
|
|
||||||
{
|
|
||||||
bdrv_set_aio_context(blk->bs, new_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_add_aio_context_notifier(BlockBackend *blk,
|
|
||||||
void (*attached_aio_context)(AioContext *new_context, void *opaque),
|
|
||||||
void (*detach_aio_context)(void *opaque), void *opaque)
|
|
||||||
{
|
|
||||||
bdrv_add_aio_context_notifier(blk->bs, attached_aio_context,
|
|
||||||
detach_aio_context, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_remove_aio_context_notifier(BlockBackend *blk,
|
|
||||||
void (*attached_aio_context)(AioContext *,
|
|
||||||
void *),
|
|
||||||
void (*detach_aio_context)(void *),
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
bdrv_remove_aio_context_notifier(blk->bs, attached_aio_context,
|
|
||||||
detach_aio_context, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_add_close_notifier(BlockBackend *blk, Notifier *notify)
|
|
||||||
{
|
|
||||||
bdrv_add_close_notifier(blk->bs, notify);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_io_plug(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
bdrv_io_plug(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void blk_io_unplug(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
bdrv_io_unplug(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAcctStats *blk_get_stats(BlockBackend *blk)
|
|
||||||
{
|
|
||||||
return bdrv_get_stats(blk->bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *blk_aio_get(const AIOCBInfo *aiocb_info, BlockBackend *blk,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
return qemu_aio_get(aiocb_info, blk_bs(blk), cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
int coroutine_fn blk_co_write_zeroes(BlockBackend *blk, int64_t sector_num,
|
|
||||||
int nb_sectors, BdrvRequestFlags flags)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_co_write_zeroes(blk->bs, sector_num, nb_sectors, flags);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_write_compressed(BlockBackend *blk, int64_t sector_num,
|
|
||||||
const uint8_t *buf, int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_write_compressed(blk->bs, sector_num, buf, nb_sectors);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_truncate(BlockBackend *blk, int64_t offset)
|
|
||||||
{
|
|
||||||
return bdrv_truncate(blk->bs, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret = blk_check_request(blk, sector_num, nb_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return bdrv_discard(blk->bs, sector_num, nb_sectors);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,
|
|
||||||
int64_t pos, int size)
|
|
||||||
{
|
|
||||||
return bdrv_save_vmstate(blk->bs, buf, pos, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
int blk_load_vmstate(BlockBackend *blk, uint8_t *buf, int64_t pos, int size)
|
|
||||||
{
|
|
||||||
return bdrv_load_vmstate(blk->bs, buf, pos, size);
|
|
||||||
}
|
|
||||||
191
block/bochs.c
191
block/bochs.c
@@ -23,8 +23,8 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block_int.h"
|
||||||
#include "qemu/module.h"
|
#include "module.h"
|
||||||
|
|
||||||
/**************************************************************/
|
/**************************************************************/
|
||||||
|
|
||||||
@@ -39,41 +39,55 @@
|
|||||||
// not allocated: 0xffffffff
|
// not allocated: 0xffffffff
|
||||||
|
|
||||||
// always little-endian
|
// always little-endian
|
||||||
struct bochs_header {
|
struct bochs_header_v1 {
|
||||||
char magic[32]; /* "Bochs Virtual HD Image" */
|
char magic[32]; // "Bochs Virtual HD Image"
|
||||||
char type[16]; /* "Redolog" */
|
char type[16]; // "Redolog"
|
||||||
char subtype[16]; /* "Undoable" / "Volatile" / "Growing" */
|
char subtype[16]; // "Undoable" / "Volatile" / "Growing"
|
||||||
uint32_t version;
|
uint32_t version;
|
||||||
uint32_t header; /* size of header */
|
uint32_t header; // size of header
|
||||||
|
|
||||||
uint32_t catalog; /* num of entries */
|
|
||||||
uint32_t bitmap; /* bitmap size */
|
|
||||||
uint32_t extent; /* extent size */
|
|
||||||
|
|
||||||
union {
|
union {
|
||||||
struct {
|
struct {
|
||||||
uint32_t reserved; /* for ??? */
|
uint32_t catalog; // num of entries
|
||||||
uint64_t disk; /* disk size */
|
uint32_t bitmap; // bitmap size
|
||||||
char padding[HEADER_SIZE - 64 - 20 - 12];
|
uint32_t extent; // extent size
|
||||||
} QEMU_PACKED redolog;
|
uint64_t disk; // disk size
|
||||||
struct {
|
char padding[HEADER_SIZE - 64 - 8 - 20];
|
||||||
uint64_t disk; /* disk size */
|
} redolog;
|
||||||
char padding[HEADER_SIZE - 64 - 20 - 8];
|
char padding[HEADER_SIZE - 64 - 8];
|
||||||
} QEMU_PACKED redolog_v1;
|
|
||||||
char padding[HEADER_SIZE - 64 - 20];
|
|
||||||
} extra;
|
} extra;
|
||||||
} QEMU_PACKED;
|
};
|
||||||
|
|
||||||
|
// always little-endian
|
||||||
|
struct bochs_header {
|
||||||
|
char magic[32]; // "Bochs Virtual HD Image"
|
||||||
|
char type[16]; // "Redolog"
|
||||||
|
char subtype[16]; // "Undoable" / "Volatile" / "Growing"
|
||||||
|
uint32_t version;
|
||||||
|
uint32_t header; // size of header
|
||||||
|
|
||||||
|
union {
|
||||||
|
struct {
|
||||||
|
uint32_t catalog; // num of entries
|
||||||
|
uint32_t bitmap; // bitmap size
|
||||||
|
uint32_t extent; // extent size
|
||||||
|
uint32_t reserved; // for ???
|
||||||
|
uint64_t disk; // disk size
|
||||||
|
char padding[HEADER_SIZE - 64 - 8 - 24];
|
||||||
|
} redolog;
|
||||||
|
char padding[HEADER_SIZE - 64 - 8];
|
||||||
|
} extra;
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct BDRVBochsState {
|
typedef struct BDRVBochsState {
|
||||||
CoMutex lock;
|
|
||||||
uint32_t *catalog_bitmap;
|
uint32_t *catalog_bitmap;
|
||||||
uint32_t catalog_size;
|
int catalog_size;
|
||||||
|
|
||||||
uint32_t data_offset;
|
int data_offset;
|
||||||
|
|
||||||
uint32_t bitmap_blocks;
|
int bitmap_blocks;
|
||||||
uint32_t extent_blocks;
|
int extent_blocks;
|
||||||
uint32_t extent_size;
|
int extent_size;
|
||||||
} BDRVBochsState;
|
} BDRVBochsState;
|
||||||
|
|
||||||
static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
|
static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
@@ -93,19 +107,17 @@ static int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
static int bochs_open(BlockDriverState *bs, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVBochsState *s = bs->opaque;
|
BDRVBochsState *s = bs->opaque;
|
||||||
uint32_t i;
|
int i;
|
||||||
struct bochs_header bochs;
|
struct bochs_header bochs;
|
||||||
int ret;
|
struct bochs_header_v1 header_v1;
|
||||||
|
|
||||||
bs->read_only = 1; // no write support yet
|
bs->read_only = 1; // no write support yet
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, 0, &bochs, sizeof(bochs));
|
if (bdrv_pread(bs->file, 0, &bochs, sizeof(bochs)) != sizeof(bochs)) {
|
||||||
if (ret < 0) {
|
goto fail;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(bochs.magic, HEADER_MAGIC) ||
|
if (strcmp(bochs.magic, HEADER_MAGIC) ||
|
||||||
@@ -113,107 +125,62 @@ static int bochs_open(BlockDriverState *bs, QDict *options, int flags,
|
|||||||
strcmp(bochs.subtype, GROWING_TYPE) ||
|
strcmp(bochs.subtype, GROWING_TYPE) ||
|
||||||
((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
|
((le32_to_cpu(bochs.version) != HEADER_VERSION) &&
|
||||||
(le32_to_cpu(bochs.version) != HEADER_V1))) {
|
(le32_to_cpu(bochs.version) != HEADER_V1))) {
|
||||||
error_setg(errp, "Image not in Bochs format");
|
goto fail;
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (le32_to_cpu(bochs.version) == HEADER_V1) {
|
if (le32_to_cpu(bochs.version) == HEADER_V1) {
|
||||||
bs->total_sectors = le64_to_cpu(bochs.extra.redolog_v1.disk) / 512;
|
memcpy(&header_v1, &bochs, sizeof(bochs));
|
||||||
|
bs->total_sectors = le64_to_cpu(header_v1.extra.redolog.disk) / 512;
|
||||||
} else {
|
} else {
|
||||||
bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
|
bs->total_sectors = le64_to_cpu(bochs.extra.redolog.disk) / 512;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Limit to 1M entries to avoid unbounded allocation. This is what is
|
s->catalog_size = le32_to_cpu(bochs.extra.redolog.catalog);
|
||||||
* needed for the largest image that bximage can create (~8 TB). */
|
s->catalog_bitmap = qemu_malloc(s->catalog_size * 4);
|
||||||
s->catalog_size = le32_to_cpu(bochs.catalog);
|
if (bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
|
||||||
if (s->catalog_size > 0x100000) {
|
s->catalog_size * 4) != s->catalog_size * 4)
|
||||||
error_setg(errp, "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;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, le32_to_cpu(bochs.header), s->catalog_bitmap,
|
|
||||||
s->catalog_size * 4);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < s->catalog_size; i++)
|
for (i = 0; i < s->catalog_size; i++)
|
||||||
le32_to_cpus(&s->catalog_bitmap[i]);
|
le32_to_cpus(&s->catalog_bitmap[i]);
|
||||||
|
|
||||||
s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
|
s->data_offset = le32_to_cpu(bochs.header) + (s->catalog_size * 4);
|
||||||
|
|
||||||
s->bitmap_blocks = 1 + (le32_to_cpu(bochs.bitmap) - 1) / 512;
|
s->bitmap_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.bitmap) - 1) / 512;
|
||||||
s->extent_blocks = 1 + (le32_to_cpu(bochs.extent) - 1) / 512;
|
s->extent_blocks = 1 + (le32_to_cpu(bochs.extra.redolog.extent) - 1) / 512;
|
||||||
|
|
||||||
s->extent_size = le32_to_cpu(bochs.extent);
|
s->extent_size = le32_to_cpu(bochs.extra.redolog.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;
|
|
||||||
} else if (s->extent_size > 0x800000) {
|
|
||||||
error_setg(errp, "Extent size %" PRIu32 " is too large",
|
|
||||||
s->extent_size);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
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");
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_co_mutex_init(&s->lock);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
fail:
|
||||||
fail:
|
return -1;
|
||||||
g_free(s->catalog_bitmap);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
|
||||||
{
|
{
|
||||||
BDRVBochsState *s = bs->opaque;
|
BDRVBochsState *s = bs->opaque;
|
||||||
uint64_t offset = sector_num * 512;
|
int64_t offset = sector_num * 512;
|
||||||
uint64_t extent_index, extent_offset, bitmap_offset;
|
int64_t extent_index, extent_offset, bitmap_offset;
|
||||||
char bitmap_entry;
|
char bitmap_entry;
|
||||||
int ret;
|
|
||||||
|
|
||||||
// seek to sector
|
// seek to sector
|
||||||
extent_index = offset / s->extent_size;
|
extent_index = offset / s->extent_size;
|
||||||
extent_offset = (offset % s->extent_size) / 512;
|
extent_offset = (offset % s->extent_size) / 512;
|
||||||
|
|
||||||
if (s->catalog_bitmap[extent_index] == 0xffffffff) {
|
if (s->catalog_bitmap[extent_index] == 0xffffffff) {
|
||||||
return 0; /* not allocated */
|
return -1; /* not allocated */
|
||||||
}
|
}
|
||||||
|
|
||||||
bitmap_offset = s->data_offset +
|
bitmap_offset = s->data_offset + (512 * s->catalog_bitmap[extent_index] *
|
||||||
(512 * (uint64_t) s->catalog_bitmap[extent_index] *
|
|
||||||
(s->extent_blocks + s->bitmap_blocks));
|
(s->extent_blocks + s->bitmap_blocks));
|
||||||
|
|
||||||
/* read in bitmap for current extent */
|
/* read in bitmap for current extent */
|
||||||
ret = bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
|
if (bdrv_pread(bs->file, bitmap_offset + (extent_offset / 8),
|
||||||
&bitmap_entry, 1);
|
&bitmap_entry, 1) != 1) {
|
||||||
if (ret < 0) {
|
return -1;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!((bitmap_entry >> (extent_offset % 8)) & 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));
|
return bitmap_offset + (512 * (s->bitmap_blocks + extent_offset));
|
||||||
@@ -226,16 +193,13 @@ static int bochs_read(BlockDriverState *bs, int64_t sector_num,
|
|||||||
|
|
||||||
while (nb_sectors > 0) {
|
while (nb_sectors > 0) {
|
||||||
int64_t block_offset = seek_to_sector(bs, sector_num);
|
int64_t block_offset = seek_to_sector(bs, sector_num);
|
||||||
if (block_offset < 0) {
|
if (block_offset >= 0) {
|
||||||
return block_offset;
|
|
||||||
} else if (block_offset > 0) {
|
|
||||||
ret = bdrv_pread(bs->file, block_offset, buf, 512);
|
ret = bdrv_pread(bs->file, block_offset, buf, 512);
|
||||||
if (ret < 0) {
|
if (ret != 512) {
|
||||||
return ret;
|
return -1;
|
||||||
}
|
}
|
||||||
} else {
|
} else
|
||||||
memset(buf, 0, 512);
|
memset(buf, 0, 512);
|
||||||
}
|
|
||||||
nb_sectors--;
|
nb_sectors--;
|
||||||
sector_num++;
|
sector_num++;
|
||||||
buf += 512;
|
buf += 512;
|
||||||
@@ -243,21 +207,10 @@ static int bochs_read(BlockDriverState *bs, int64_t sector_num,
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bochs_close(BlockDriverState *bs)
|
static void bochs_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVBochsState *s = bs->opaque;
|
BDRVBochsState *s = bs->opaque;
|
||||||
g_free(s->catalog_bitmap);
|
qemu_free(s->catalog_bitmap);
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockDriver bdrv_bochs = {
|
static BlockDriver bdrv_bochs = {
|
||||||
@@ -265,7 +218,7 @@ static BlockDriver bdrv_bochs = {
|
|||||||
.instance_size = sizeof(BDRVBochsState),
|
.instance_size = sizeof(BDRVBochsState),
|
||||||
.bdrv_probe = bochs_probe,
|
.bdrv_probe = bochs_probe,
|
||||||
.bdrv_open = bochs_open,
|
.bdrv_open = bochs_open,
|
||||||
.bdrv_read = bochs_co_read,
|
.bdrv_read = bochs_read,
|
||||||
.bdrv_close = bochs_close,
|
.bdrv_close = bochs_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
208
block/cloop.c
208
block/cloop.c
@@ -22,18 +22,14 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block_int.h"
|
||||||
#include "qemu/module.h"
|
#include "module.h"
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
|
|
||||||
/* Maximum compressed block size */
|
|
||||||
#define MAX_BLOCK_SIZE (64 * 1024 * 1024)
|
|
||||||
|
|
||||||
typedef struct BDRVCloopState {
|
typedef struct BDRVCloopState {
|
||||||
CoMutex lock;
|
|
||||||
uint32_t block_size;
|
uint32_t block_size;
|
||||||
uint32_t n_blocks;
|
uint32_t n_blocks;
|
||||||
uint64_t *offsets;
|
uint64_t* offsets;
|
||||||
uint32_t sectors_per_block;
|
uint32_t sectors_per_block;
|
||||||
uint32_t current_block;
|
uint32_t current_block;
|
||||||
uint8_t *compressed_block;
|
uint8_t *compressed_block;
|
||||||
@@ -43,184 +39,89 @@ typedef struct BDRVCloopState {
|
|||||||
|
|
||||||
static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
|
static int cloop_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
{
|
{
|
||||||
const char *magic_version_2_0 = "#!/bin/sh\n"
|
const char* magic_version_2_0="#!/bin/sh\n"
|
||||||
"#V2.0 Format\n"
|
"#V2.0 Format\n"
|
||||||
"modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n";
|
"modprobe cloop file=$0 && mount -r -t iso9660 /dev/cloop $1\n";
|
||||||
int length = strlen(magic_version_2_0);
|
int length=strlen(magic_version_2_0);
|
||||||
if (length > buf_size) {
|
if(length>buf_size)
|
||||||
length = buf_size;
|
length=buf_size;
|
||||||
}
|
if(!memcmp(magic_version_2_0,buf,length))
|
||||||
if (!memcmp(magic_version_2_0, buf, length)) {
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cloop_open(BlockDriverState *bs, QDict *options, int flags,
|
static int cloop_open(BlockDriverState *bs, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVCloopState *s = bs->opaque;
|
BDRVCloopState *s = bs->opaque;
|
||||||
uint32_t offsets_size, max_compressed_block_size = 1, i;
|
uint32_t offsets_size,max_compressed_block_size=1,i;
|
||||||
int ret;
|
|
||||||
|
|
||||||
bs->read_only = 1;
|
bs->read_only = 1;
|
||||||
|
|
||||||
/* read header */
|
/* read header */
|
||||||
ret = bdrv_pread(bs->file, 128, &s->block_size, 4);
|
if (bdrv_pread(bs->file, 128, &s->block_size, 4) < 4) {
|
||||||
if (ret < 0) {
|
goto cloop_close;
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
s->block_size = be32_to_cpu(s->block_size);
|
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);
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
if (s->block_size == 0) {
|
|
||||||
error_setg(errp, "block_size cannot be zero");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* cloop's create_compressed_fs.c warns about block sizes beyond 256 KB but
|
if (bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4) < 4) {
|
||||||
* we can accept more. Prevent ridiculous values like 4 GB - 1 since we
|
goto cloop_close;
|
||||||
* 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));
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, 128 + 4, &s->n_blocks, 4);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
s->n_blocks = be32_to_cpu(s->n_blocks);
|
s->n_blocks = be32_to_cpu(s->n_blocks);
|
||||||
|
|
||||||
/* read offsets */
|
/* read offsets */
|
||||||
if (s->n_blocks > (UINT32_MAX - 1) / sizeof(uint64_t)) {
|
offsets_size = s->n_blocks * sizeof(uint64_t);
|
||||||
/* Prevent integer overflow */
|
s->offsets = qemu_malloc(offsets_size);
|
||||||
error_setg(errp, "n_blocks %" PRIu32 " must be %zu or less",
|
if (bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size) <
|
||||||
s->n_blocks,
|
offsets_size) {
|
||||||
(UINT32_MAX - 1) / sizeof(uint64_t));
|
goto cloop_close;
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
offsets_size = (s->n_blocks + 1) * sizeof(uint64_t);
|
for(i=0;i<s->n_blocks;i++) {
|
||||||
if (offsets_size > 512 * 1024 * 1024) {
|
s->offsets[i]=be64_to_cpu(s->offsets[i]);
|
||||||
/* Prevent ridiculous offsets_size which causes memory allocation to
|
if(i>0) {
|
||||||
* fail or overflows bdrv_pread() size. In practice the 512 MB
|
uint32_t size=s->offsets[i]-s->offsets[i-1];
|
||||||
* offsets[] limit supports 16 TB images at 256 KB block size.
|
if(size>max_compressed_block_size)
|
||||||
*/
|
max_compressed_block_size=size;
|
||||||
error_setg(errp, "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;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, 128 + 4 + 4, s->offsets, offsets_size);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (i = 0; i < s->n_blocks + 1; i++) {
|
|
||||||
uint64_t size;
|
|
||||||
|
|
||||||
s->offsets[i] = be64_to_cpu(s->offsets[i]);
|
|
||||||
if (i == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s->offsets[i] < s->offsets[i - 1]) {
|
|
||||||
error_setg(errp, "offsets not monotonically increasing at "
|
|
||||||
"index %" PRIu32 ", image file is corrupt", i);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
size = s->offsets[i] - s->offsets[i - 1];
|
|
||||||
|
|
||||||
/* Compressed blocks should be smaller than the uncompressed block size
|
|
||||||
* but maybe compression performed poorly so the compressed block is
|
|
||||||
* actually bigger. Clamp down on unrealistic values to prevent
|
|
||||||
* 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);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (size > max_compressed_block_size) {
|
|
||||||
max_compressed_block_size = size;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize zlib engine */
|
/* initialize zlib engine */
|
||||||
s->compressed_block = g_try_malloc(max_compressed_block_size + 1);
|
s->compressed_block = qemu_malloc(max_compressed_block_size+1);
|
||||||
if (s->compressed_block == NULL) {
|
s->uncompressed_block = qemu_malloc(s->block_size);
|
||||||
error_setg(errp, "Could not allocate compressed_block");
|
if(inflateInit(&s->zstream) != Z_OK)
|
||||||
ret = -ENOMEM;
|
goto cloop_close;
|
||||||
goto fail;
|
s->current_block=s->n_blocks;
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inflateInit(&s->zstream) != Z_OK) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
s->current_block = s->n_blocks;
|
|
||||||
|
|
||||||
s->sectors_per_block = s->block_size/512;
|
s->sectors_per_block = s->block_size/512;
|
||||||
bs->total_sectors = s->n_blocks * s->sectors_per_block;
|
bs->total_sectors = s->n_blocks*s->sectors_per_block;
|
||||||
qemu_co_mutex_init(&s->lock);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fail:
|
cloop_close:
|
||||||
g_free(s->offsets);
|
return -1;
|
||||||
g_free(s->compressed_block);
|
|
||||||
g_free(s->uncompressed_block);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int cloop_read_block(BlockDriverState *bs, int block_num)
|
static inline int cloop_read_block(BlockDriverState *bs, int block_num)
|
||||||
{
|
{
|
||||||
BDRVCloopState *s = bs->opaque;
|
BDRVCloopState *s = bs->opaque;
|
||||||
|
|
||||||
if (s->current_block != block_num) {
|
if(s->current_block != block_num) {
|
||||||
int ret;
|
int ret;
|
||||||
uint32_t bytes = s->offsets[block_num + 1] - s->offsets[block_num];
|
uint32_t bytes = s->offsets[block_num+1]-s->offsets[block_num];
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
|
ret = bdrv_pread(bs->file, s->offsets[block_num], s->compressed_block,
|
||||||
bytes);
|
bytes);
|
||||||
if (ret != bytes) {
|
if (ret != bytes)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
s->zstream.next_in = s->compressed_block;
|
s->zstream.next_in = s->compressed_block;
|
||||||
s->zstream.avail_in = bytes;
|
s->zstream.avail_in = bytes;
|
||||||
s->zstream.next_out = s->uncompressed_block;
|
s->zstream.next_out = s->uncompressed_block;
|
||||||
s->zstream.avail_out = s->block_size;
|
s->zstream.avail_out = s->block_size;
|
||||||
ret = inflateReset(&s->zstream);
|
ret = inflateReset(&s->zstream);
|
||||||
if (ret != Z_OK) {
|
if(ret != Z_OK)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
ret = inflate(&s->zstream, Z_FINISH);
|
ret = inflate(&s->zstream, Z_FINISH);
|
||||||
if (ret != Z_STREAM_END || s->zstream.total_out != s->block_size) {
|
if(ret != Z_STREAM_END || s->zstream.total_out != s->block_size)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
s->current_block = block_num;
|
s->current_block = block_num;
|
||||||
}
|
}
|
||||||
@@ -233,36 +134,23 @@ static int cloop_read(BlockDriverState *bs, int64_t sector_num,
|
|||||||
BDRVCloopState *s = bs->opaque;
|
BDRVCloopState *s = bs->opaque;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < nb_sectors; i++) {
|
for(i=0;i<nb_sectors;i++) {
|
||||||
uint32_t sector_offset_in_block =
|
uint32_t sector_offset_in_block=((sector_num+i)%s->sectors_per_block),
|
||||||
((sector_num + i) % s->sectors_per_block),
|
block_num=(sector_num+i)/s->sectors_per_block;
|
||||||
block_num = (sector_num + i) / s->sectors_per_block;
|
if(cloop_read_block(bs, block_num) != 0)
|
||||||
if (cloop_read_block(bs, block_num) != 0) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
memcpy(buf+i*512,s->uncompressed_block+sector_offset_in_block*512,512);
|
||||||
memcpy(buf + i * 512,
|
|
||||||
s->uncompressed_block + sector_offset_in_block * 512, 512);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void cloop_close(BlockDriverState *bs)
|
static void cloop_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVCloopState *s = bs->opaque;
|
BDRVCloopState *s = bs->opaque;
|
||||||
g_free(s->offsets);
|
if(s->n_blocks>0)
|
||||||
g_free(s->compressed_block);
|
free(s->offsets);
|
||||||
g_free(s->uncompressed_block);
|
free(s->compressed_block);
|
||||||
|
free(s->uncompressed_block);
|
||||||
inflateEnd(&s->zstream);
|
inflateEnd(&s->zstream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -271,7 +159,7 @@ static BlockDriver bdrv_cloop = {
|
|||||||
.instance_size = sizeof(BDRVCloopState),
|
.instance_size = sizeof(BDRVCloopState),
|
||||||
.bdrv_probe = cloop_probe,
|
.bdrv_probe = cloop_probe,
|
||||||
.bdrv_open = cloop_open,
|
.bdrv_open = cloop_open,
|
||||||
.bdrv_read = cloop_co_read,
|
.bdrv_read = cloop_read,
|
||||||
.bdrv_close = cloop_close,
|
.bdrv_close = cloop_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
273
block/commit.c
273
block/commit.c
@@ -1,273 +0,0 @@
|
|||||||
/*
|
|
||||||
* Live block commit
|
|
||||||
*
|
|
||||||
* Copyright Red Hat, Inc. 2012
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Jeff Cody <jcody@redhat.com>
|
|
||||||
* Based on stream.c by Stefan Hajnoczi
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
||||||
* See the COPYING.LIB file in the top-level directory.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "trace.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "block/blockjob.h"
|
|
||||||
#include "qemu/ratelimit.h"
|
|
||||||
|
|
||||||
enum {
|
|
||||||
/*
|
|
||||||
* Size of data buffer for populating the image file. This should be large
|
|
||||||
* enough to process multiple clusters in a single call, so that populating
|
|
||||||
* contiguous regions of the image is efficient.
|
|
||||||
*/
|
|
||||||
COMMIT_BUFFER_SIZE = 512 * 1024, /* in bytes */
|
|
||||||
};
|
|
||||||
|
|
||||||
#define SLICE_TIME 100000000ULL /* ns */
|
|
||||||
|
|
||||||
typedef struct CommitBlockJob {
|
|
||||||
BlockJob common;
|
|
||||||
RateLimit limit;
|
|
||||||
BlockDriverState *active;
|
|
||||||
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(BlockDriverState *bs,
|
|
||||||
BlockDriverState *base,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
void *buf)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
ret = bdrv_read(bs, sector_num, buf, nb_sectors);
|
|
||||||
if (ret) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 = s->top;
|
|
||||||
BlockDriverState *base = s->base;
|
|
||||||
BlockDriverState *overlay_bs;
|
|
||||||
int ret = data->ret;
|
|
||||||
|
|
||||||
if (!block_job_is_cancelled(&s->common) && ret == 0) {
|
|
||||||
/* success */
|
|
||||||
ret = bdrv_drop_intermediate(active, top, base, s->backing_file_str);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 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);
|
|
||||||
}
|
|
||||||
g_free(s->backing_file_str);
|
|
||||||
block_job_completed(&s->common, ret);
|
|
||||||
g_free(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void coroutine_fn commit_run(void *opaque)
|
|
||||||
{
|
|
||||||
CommitBlockJob *s = opaque;
|
|
||||||
CommitCompleteData *data;
|
|
||||||
BlockDriverState *top = s->top;
|
|
||||||
BlockDriverState *base = s->base;
|
|
||||||
int64_t sector_num, end;
|
|
||||||
int ret = 0;
|
|
||||||
int n = 0;
|
|
||||||
void *buf = NULL;
|
|
||||||
int bytes_written = 0;
|
|
||||||
int64_t base_len;
|
|
||||||
|
|
||||||
ret = s->common.len = bdrv_getlength(top);
|
|
||||||
|
|
||||||
|
|
||||||
if (s->common.len < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = base_len = bdrv_getlength(base);
|
|
||||||
if (base_len < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (base_len < s->common.len) {
|
|
||||||
ret = bdrv_truncate(base, s->common.len);
|
|
||||||
if (ret) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
end = s->common.len >> BDRV_SECTOR_BITS;
|
|
||||||
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);
|
|
||||||
if (block_job_is_cancelled(&s->common)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
/* Copy if allocated above the base */
|
|
||||||
ret = bdrv_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) {
|
|
||||||
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) {
|
|
||||||
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 out;
|
|
||||||
} else {
|
|
||||||
n = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* Publish progress */
|
|
||||||
s->common.offset += n * BDRV_SECTOR_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
out:
|
|
||||||
qemu_vfree(buf);
|
|
||||||
|
|
||||||
data = g_malloc(sizeof(*data));
|
|
||||||
data->ret = ret;
|
|
||||||
block_job_defer_to_main_loop(&s->common, commit_complete, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void commit_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
|
||||||
{
|
|
||||||
CommitBlockJob *s = container_of(job, CommitBlockJob, common);
|
|
||||||
|
|
||||||
if (speed < 0) {
|
|
||||||
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 = {
|
|
||||||
.instance_size = sizeof(CommitBlockJob),
|
|
||||||
.job_type = BLOCK_JOB_TYPE_COMMIT,
|
|
||||||
.set_speed = commit_set_speed,
|
|
||||||
};
|
|
||||||
|
|
||||||
void commit_start(BlockDriverState *bs, BlockDriverState *base,
|
|
||||||
BlockDriverState *top, int64_t speed,
|
|
||||||
BlockdevOnError on_error, BlockCompletionFunc *cb,
|
|
||||||
void *opaque, const char *backing_file_str, Error **errp)
|
|
||||||
{
|
|
||||||
CommitBlockJob *s;
|
|
||||||
BlockReopenQueue *reopen_queue = NULL;
|
|
||||||
int orig_overlay_flags;
|
|
||||||
int orig_base_flags;
|
|
||||||
BlockDriverState *overlay_bs;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
if ((on_error == BLOCKDEV_ON_ERROR_STOP ||
|
|
||||||
on_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
|
||||||
!bdrv_iostatus_is_enabled(bs)) {
|
|
||||||
error_setg(errp, "Invalid parameter combination");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(top != bs);
|
|
||||||
if (top == base) {
|
|
||||||
error_setg(errp, "Invalid files for merge: top and base are the same");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
overlay_bs = bdrv_find_overlay(bs, top);
|
|
||||||
|
|
||||||
if (overlay_bs == NULL) {
|
|
||||||
error_setg(errp, "Could not find overlay image for %s:", top->filename);
|
|
||||||
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,
|
|
||||||
orig_base_flags | BDRV_O_RDWR);
|
|
||||||
}
|
|
||||||
if (!(orig_overlay_flags & BDRV_O_RDWR)) {
|
|
||||||
reopen_queue = bdrv_reopen_queue(reopen_queue, overlay_bs,
|
|
||||||
orig_overlay_flags | BDRV_O_RDWR);
|
|
||||||
}
|
|
||||||
if (reopen_queue) {
|
|
||||||
bdrv_reopen_multiple(reopen_queue, &local_err);
|
|
||||||
if (local_err != NULL) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
s = block_job_create(&commit_job_driver, 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, s->common.co, opaque);
|
|
||||||
qemu_coroutine_enter(s->common.co, s);
|
|
||||||
}
|
|
||||||
324
block/cow.c
Normal file
324
block/cow.c
Normal file
@@ -0,0 +1,324 @@
|
|||||||
|
/*
|
||||||
|
* Block driver for the COW format
|
||||||
|
*
|
||||||
|
* Copyright (c) 2004 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_int.h"
|
||||||
|
#include "module.h"
|
||||||
|
|
||||||
|
/**************************************************************/
|
||||||
|
/* COW block driver using file system holes */
|
||||||
|
|
||||||
|
/* user mode linux compatible COW file */
|
||||||
|
#define COW_MAGIC 0x4f4f4f4d /* MOOO */
|
||||||
|
#define COW_VERSION 2
|
||||||
|
|
||||||
|
struct cow_header_v2 {
|
||||||
|
uint32_t magic;
|
||||||
|
uint32_t version;
|
||||||
|
char backing_file[1024];
|
||||||
|
int32_t mtime;
|
||||||
|
uint64_t size;
|
||||||
|
uint32_t sectorsize;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct BDRVCowState {
|
||||||
|
int64_t cow_sectors_offset;
|
||||||
|
} BDRVCowState;
|
||||||
|
|
||||||
|
static int cow_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
|
{
|
||||||
|
const struct cow_header_v2 *cow_header = (const void *)buf;
|
||||||
|
|
||||||
|
if (buf_size >= sizeof(struct cow_header_v2) &&
|
||||||
|
be32_to_cpu(cow_header->magic) == COW_MAGIC &&
|
||||||
|
be32_to_cpu(cow_header->version) == COW_VERSION)
|
||||||
|
return 100;
|
||||||
|
else
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_open(BlockDriverState *bs, int flags)
|
||||||
|
{
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
struct cow_header_v2 cow_header;
|
||||||
|
int bitmap_size;
|
||||||
|
int64_t size;
|
||||||
|
|
||||||
|
/* see if it is a cow image */
|
||||||
|
if (bdrv_pread(bs->file, 0, &cow_header, sizeof(cow_header)) !=
|
||||||
|
sizeof(cow_header)) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (be32_to_cpu(cow_header.magic) != COW_MAGIC ||
|
||||||
|
be32_to_cpu(cow_header.version) != COW_VERSION) {
|
||||||
|
goto fail;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* cow image found */
|
||||||
|
size = be64_to_cpu(cow_header.size);
|
||||||
|
bs->total_sectors = size / 512;
|
||||||
|
|
||||||
|
pstrcpy(bs->backing_file, sizeof(bs->backing_file),
|
||||||
|
cow_header.backing_file);
|
||||||
|
|
||||||
|
bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header);
|
||||||
|
s->cow_sectors_offset = (bitmap_size + 511) & ~511;
|
||||||
|
return 0;
|
||||||
|
fail:
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XXX(hch): right now these functions are extremly ineffcient.
|
||||||
|
* We should just read the whole bitmap we'll need in one go instead.
|
||||||
|
*/
|
||||||
|
static inline int cow_set_bit(BlockDriverState *bs, int64_t bitnum)
|
||||||
|
{
|
||||||
|
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
|
||||||
|
uint8_t bitmap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
bitmap |= (1 << (bitnum % 8));
|
||||||
|
|
||||||
|
ret = bdrv_pwrite_sync(bs->file, offset, &bitmap, sizeof(bitmap));
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)
|
||||||
|
{
|
||||||
|
uint64_t offset = sizeof(struct cow_header_v2) + bitnum / 8;
|
||||||
|
uint8_t bitmap;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = bdrv_pread(bs->file, offset, &bitmap, sizeof(bitmap));
|
||||||
|
if (ret < 0) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return !!(bitmap & (1 << (bitnum % 8)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Return true if first block has been changed (ie. current version is
|
||||||
|
* in COW file). Set the number of continuous blocks for which that
|
||||||
|
* is true. */
|
||||||
|
static int cow_is_allocated(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
int nb_sectors, int *num_same)
|
||||||
|
{
|
||||||
|
int changed;
|
||||||
|
|
||||||
|
if (nb_sectors == 0) {
|
||||||
|
*num_same = nb_sectors;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
changed = is_bit_set(bs, sector_num);
|
||||||
|
if (changed < 0) {
|
||||||
|
return 0; /* XXX: how to return I/O errors? */
|
||||||
|
}
|
||||||
|
|
||||||
|
for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) {
|
||||||
|
if (is_bit_set(bs, sector_num + *num_same) != changed)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
int nb_sectors)
|
||||||
|
{
|
||||||
|
int error = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
for (i = 0; i < nb_sectors; i++) {
|
||||||
|
error = cow_set_bit(bs, sector_num + i);
|
||||||
|
if (error) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
uint8_t *buf, int nb_sectors)
|
||||||
|
{
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
int ret, n;
|
||||||
|
|
||||||
|
while (nb_sectors > 0) {
|
||||||
|
if (cow_is_allocated(bs, sector_num, nb_sectors, &n)) {
|
||||||
|
ret = bdrv_pread(bs->file,
|
||||||
|
s->cow_sectors_offset + sector_num * 512,
|
||||||
|
buf, n * 512);
|
||||||
|
if (ret != n * 512)
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
if (bs->backing_hd) {
|
||||||
|
/* read from the base image */
|
||||||
|
ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
|
||||||
|
if (ret < 0)
|
||||||
|
return -1;
|
||||||
|
} else {
|
||||||
|
memset(buf, 0, n * 512);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nb_sectors -= n;
|
||||||
|
sector_num += n;
|
||||||
|
buf += n * 512;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_write(BlockDriverState *bs, int64_t sector_num,
|
||||||
|
const uint8_t *buf, int nb_sectors)
|
||||||
|
{
|
||||||
|
BDRVCowState *s = bs->opaque;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
ret = bdrv_pwrite(bs->file, s->cow_sectors_offset + sector_num * 512,
|
||||||
|
buf, nb_sectors * 512);
|
||||||
|
if (ret != nb_sectors * 512)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return cow_update_bitmap(bs, sector_num, nb_sectors);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void cow_close(BlockDriverState *bs)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_create(const char *filename, QEMUOptionParameter *options)
|
||||||
|
{
|
||||||
|
int fd, cow_fd;
|
||||||
|
struct cow_header_v2 cow_header;
|
||||||
|
struct stat st;
|
||||||
|
int64_t image_sectors = 0;
|
||||||
|
const char *image_filename = NULL;
|
||||||
|
int ret;
|
||||||
|
|
||||||
|
/* Read out options */
|
||||||
|
while (options && options->name) {
|
||||||
|
if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
|
||||||
|
image_sectors = options->value.n / 512;
|
||||||
|
} else if (!strcmp(options->name, BLOCK_OPT_BACKING_FILE)) {
|
||||||
|
image_filename = options->value.s;
|
||||||
|
}
|
||||||
|
options++;
|
||||||
|
}
|
||||||
|
|
||||||
|
cow_fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
|
||||||
|
0644);
|
||||||
|
if (cow_fd < 0)
|
||||||
|
return -errno;
|
||||||
|
memset(&cow_header, 0, sizeof(cow_header));
|
||||||
|
cow_header.magic = cpu_to_be32(COW_MAGIC);
|
||||||
|
cow_header.version = cpu_to_be32(COW_VERSION);
|
||||||
|
if (image_filename) {
|
||||||
|
/* Note: if no file, we put a dummy mtime */
|
||||||
|
cow_header.mtime = cpu_to_be32(0);
|
||||||
|
|
||||||
|
fd = open(image_filename, O_RDONLY | O_BINARY);
|
||||||
|
if (fd < 0) {
|
||||||
|
close(cow_fd);
|
||||||
|
goto mtime_fail;
|
||||||
|
}
|
||||||
|
if (fstat(fd, &st) != 0) {
|
||||||
|
close(fd);
|
||||||
|
goto mtime_fail;
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
cow_header.mtime = cpu_to_be32(st.st_mtime);
|
||||||
|
mtime_fail:
|
||||||
|
pstrcpy(cow_header.backing_file, sizeof(cow_header.backing_file),
|
||||||
|
image_filename);
|
||||||
|
}
|
||||||
|
cow_header.sectorsize = cpu_to_be32(512);
|
||||||
|
cow_header.size = cpu_to_be64(image_sectors * 512);
|
||||||
|
ret = qemu_write_full(cow_fd, &cow_header, sizeof(cow_header));
|
||||||
|
if (ret != sizeof(cow_header)) {
|
||||||
|
ret = -errno;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* resize to include at least all the bitmap */
|
||||||
|
ret = ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3));
|
||||||
|
if (ret) {
|
||||||
|
ret = -errno;
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
exit:
|
||||||
|
close(cow_fd);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int cow_flush(BlockDriverState *bs)
|
||||||
|
{
|
||||||
|
return bdrv_flush(bs->file);
|
||||||
|
}
|
||||||
|
|
||||||
|
static QEMUOptionParameter cow_create_options[] = {
|
||||||
|
{
|
||||||
|
.name = BLOCK_OPT_SIZE,
|
||||||
|
.type = OPT_SIZE,
|
||||||
|
.help = "Virtual disk size"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
.name = BLOCK_OPT_BACKING_FILE,
|
||||||
|
.type = OPT_STRING,
|
||||||
|
.help = "File name of a base image"
|
||||||
|
},
|
||||||
|
{ NULL }
|
||||||
|
};
|
||||||
|
|
||||||
|
static BlockDriver bdrv_cow = {
|
||||||
|
.format_name = "cow",
|
||||||
|
.instance_size = sizeof(BDRVCowState),
|
||||||
|
.bdrv_probe = cow_probe,
|
||||||
|
.bdrv_open = cow_open,
|
||||||
|
.bdrv_read = cow_read,
|
||||||
|
.bdrv_write = cow_write,
|
||||||
|
.bdrv_close = cow_close,
|
||||||
|
.bdrv_create = cow_create,
|
||||||
|
.bdrv_flush = cow_flush,
|
||||||
|
.bdrv_is_allocated = cow_is_allocated,
|
||||||
|
|
||||||
|
.create_options = cow_create_options,
|
||||||
|
};
|
||||||
|
|
||||||
|
static void bdrv_cow_init(void)
|
||||||
|
{
|
||||||
|
bdrv_register(&bdrv_cow);
|
||||||
|
}
|
||||||
|
|
||||||
|
block_init(bdrv_cow_init);
|
||||||
555
block/curl.c
555
block/curl.c
@@ -22,11 +22,10 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block_int.h"
|
||||||
#include "qapi/qmp/qbool.h"
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
|
|
||||||
// #define DEBUG_CURL
|
// #define DEBUG
|
||||||
// #define DEBUG_VERBOSE
|
// #define DEBUG_VERBOSE
|
||||||
|
|
||||||
#ifdef DEBUG_CURL
|
#ifdef DEBUG_CURL
|
||||||
@@ -35,57 +34,20 @@
|
|||||||
#define DPRINTF(fmt, ...) do { } while (0)
|
#define DPRINTF(fmt, ...) do { } while (0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if LIBCURL_VERSION_NUM >= 0x071000
|
|
||||||
/* The multi interface timer callback was introduced in 7.16.0 */
|
|
||||||
#define NEED_CURL_TIMER_CALLBACK
|
|
||||||
#define HAVE_SOCKET_ACTION
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef HAVE_SOCKET_ACTION
|
|
||||||
/* If curl_multi_socket_action isn't available, define it statically here in
|
|
||||||
* terms of curl_multi_socket. Note that ev_bitmask will be ignored, which is
|
|
||||||
* less efficient but still safe. */
|
|
||||||
static CURLMcode __curl_multi_socket_action(CURLM *multi_handle,
|
|
||||||
curl_socket_t sockfd,
|
|
||||||
int ev_bitmask,
|
|
||||||
int *running_handles)
|
|
||||||
{
|
|
||||||
return curl_multi_socket(multi_handle, sockfd, running_handles);
|
|
||||||
}
|
|
||||||
#define curl_multi_socket_action __curl_multi_socket_action
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define PROTOCOLS (CURLPROTO_HTTP | CURLPROTO_HTTPS | \
|
|
||||||
CURLPROTO_FTP | CURLPROTO_FTPS | \
|
|
||||||
CURLPROTO_TFTP)
|
|
||||||
|
|
||||||
#define CURL_NUM_STATES 8
|
#define CURL_NUM_STATES 8
|
||||||
#define CURL_NUM_ACB 8
|
#define CURL_NUM_ACB 8
|
||||||
#define SECTOR_SIZE 512
|
#define SECTOR_SIZE 512
|
||||||
#define READ_AHEAD_DEFAULT (256 * 1024)
|
#define READ_AHEAD_SIZE (256 * 1024)
|
||||||
#define CURL_TIMEOUT_DEFAULT 5
|
|
||||||
#define CURL_TIMEOUT_MAX 10000
|
|
||||||
|
|
||||||
#define FIND_RET_NONE 0
|
#define FIND_RET_NONE 0
|
||||||
#define FIND_RET_OK 1
|
#define FIND_RET_OK 1
|
||||||
#define FIND_RET_WAIT 2
|
#define FIND_RET_WAIT 2
|
||||||
|
|
||||||
#define CURL_BLOCK_OPT_URL "url"
|
|
||||||
#define CURL_BLOCK_OPT_READAHEAD "readahead"
|
|
||||||
#define CURL_BLOCK_OPT_SSLVERIFY "sslverify"
|
|
||||||
#define CURL_BLOCK_OPT_TIMEOUT "timeout"
|
|
||||||
#define CURL_BLOCK_OPT_COOKIE "cookie"
|
|
||||||
|
|
||||||
struct BDRVCURLState;
|
struct BDRVCURLState;
|
||||||
|
|
||||||
typedef struct CURLAIOCB {
|
typedef struct CURLAIOCB {
|
||||||
BlockAIOCB common;
|
BlockDriverAIOCB common;
|
||||||
QEMUBH *bh;
|
|
||||||
QEMUIOVector *qiov;
|
QEMUIOVector *qiov;
|
||||||
|
|
||||||
int64_t sector_num;
|
|
||||||
int nb_sectors;
|
|
||||||
|
|
||||||
size_t start;
|
size_t start;
|
||||||
size_t end;
|
size_t end;
|
||||||
} CURLAIOCB;
|
} CURLAIOCB;
|
||||||
@@ -95,7 +57,6 @@ typedef struct CURLState
|
|||||||
struct BDRVCURLState *s;
|
struct BDRVCURLState *s;
|
||||||
CURLAIOCB *acb[CURL_NUM_ACB];
|
CURLAIOCB *acb[CURL_NUM_ACB];
|
||||||
CURL *curl;
|
CURL *curl;
|
||||||
curl_socket_t sock_fd;
|
|
||||||
char *orig_buf;
|
char *orig_buf;
|
||||||
size_t buf_start;
|
size_t buf_start;
|
||||||
size_t buf_off;
|
size_t buf_off;
|
||||||
@@ -107,78 +68,46 @@ typedef struct CURLState
|
|||||||
|
|
||||||
typedef struct BDRVCURLState {
|
typedef struct BDRVCURLState {
|
||||||
CURLM *multi;
|
CURLM *multi;
|
||||||
QEMUTimer timer;
|
|
||||||
size_t len;
|
size_t len;
|
||||||
CURLState states[CURL_NUM_STATES];
|
CURLState states[CURL_NUM_STATES];
|
||||||
char *url;
|
char *url;
|
||||||
size_t readahead_size;
|
size_t readahead_size;
|
||||||
bool sslverify;
|
|
||||||
uint64_t timeout;
|
|
||||||
char *cookie;
|
|
||||||
bool accept_range;
|
|
||||||
AioContext *aio_context;
|
|
||||||
} BDRVCURLState;
|
} BDRVCURLState;
|
||||||
|
|
||||||
static void curl_clean_state(CURLState *s);
|
static void curl_clean_state(CURLState *s);
|
||||||
static void curl_multi_do(void *arg);
|
static void curl_multi_do(void *arg);
|
||||||
static void curl_multi_read(void *arg);
|
|
||||||
|
|
||||||
#ifdef NEED_CURL_TIMER_CALLBACK
|
|
||||||
static int curl_timer_cb(CURLM *multi, long timeout_ms, void *opaque)
|
|
||||||
{
|
|
||||||
BDRVCURLState *s = opaque;
|
|
||||||
|
|
||||||
DPRINTF("CURL: timer callback timeout_ms %ld\n", timeout_ms);
|
|
||||||
if (timeout_ms == -1) {
|
|
||||||
timer_del(&s->timer);
|
|
||||||
} else {
|
|
||||||
int64_t timeout_ns = (int64_t)timeout_ms * 1000 * 1000;
|
|
||||||
timer_mod(&s->timer,
|
|
||||||
qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + timeout_ns);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
|
static int curl_sock_cb(CURL *curl, curl_socket_t fd, int action,
|
||||||
void *userp, void *sp)
|
void *s, void *sp)
|
||||||
{
|
{
|
||||||
BDRVCURLState *s;
|
|
||||||
CURLState *state = NULL;
|
|
||||||
curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&state);
|
|
||||||
state->sock_fd = fd;
|
|
||||||
s = state->s;
|
|
||||||
|
|
||||||
DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
|
DPRINTF("CURL (AIO): Sock action %d on fd %d\n", action, fd);
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case CURL_POLL_IN:
|
case CURL_POLL_IN:
|
||||||
aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
|
qemu_aio_set_fd_handler(fd, curl_multi_do, NULL, NULL, NULL, s);
|
||||||
NULL, state);
|
|
||||||
break;
|
break;
|
||||||
case CURL_POLL_OUT:
|
case CURL_POLL_OUT:
|
||||||
aio_set_fd_handler(s->aio_context, fd, NULL, curl_multi_do, state);
|
qemu_aio_set_fd_handler(fd, NULL, curl_multi_do, NULL, NULL, s);
|
||||||
break;
|
break;
|
||||||
case CURL_POLL_INOUT:
|
case CURL_POLL_INOUT:
|
||||||
aio_set_fd_handler(s->aio_context, fd, curl_multi_read,
|
qemu_aio_set_fd_handler(fd, curl_multi_do,
|
||||||
curl_multi_do, state);
|
curl_multi_do, NULL, NULL, s);
|
||||||
break;
|
break;
|
||||||
case CURL_POLL_REMOVE:
|
case CURL_POLL_REMOVE:
|
||||||
aio_set_fd_handler(s->aio_context, fd, NULL, NULL, NULL);
|
qemu_aio_set_fd_handler(fd, NULL, NULL, NULL, NULL, NULL);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t curl_header_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
static size_t curl_size_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
||||||
{
|
{
|
||||||
BDRVCURLState *s = opaque;
|
CURLState *s = ((CURLState*)opaque);
|
||||||
size_t realsize = size * nmemb;
|
size_t realsize = size * nmemb;
|
||||||
const char *accept_line = "Accept-Ranges: bytes";
|
size_t fsize;
|
||||||
|
|
||||||
if (realsize >= strlen(accept_line)
|
if(sscanf(ptr, "Content-Length: %zd", &fsize) == 1) {
|
||||||
&& strncmp((char *)ptr, accept_line, strlen(accept_line)) == 0) {
|
s->s->len = fsize;
|
||||||
s->accept_range = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return realsize;
|
return realsize;
|
||||||
@@ -193,13 +122,8 @@ static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
|||||||
DPRINTF("CURL: Just reading %zd bytes\n", realsize);
|
DPRINTF("CURL: Just reading %zd bytes\n", realsize);
|
||||||
|
|
||||||
if (!s || !s->orig_buf)
|
if (!s || !s->orig_buf)
|
||||||
return 0;
|
goto read_end;
|
||||||
|
|
||||||
if (s->buf_off >= s->buf_len) {
|
|
||||||
/* buffer full, read nothing */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
realsize = MIN(realsize, s->buf_len - s->buf_off);
|
|
||||||
memcpy(s->orig_buf + s->buf_off, ptr, realsize);
|
memcpy(s->orig_buf + s->buf_off, ptr, realsize);
|
||||||
s->buf_off += realsize;
|
s->buf_off += realsize;
|
||||||
|
|
||||||
@@ -210,14 +134,15 @@ static size_t curl_read_cb(void *ptr, size_t size, size_t nmemb, void *opaque)
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ((s->buf_off >= acb->end)) {
|
if ((s->buf_off >= acb->end)) {
|
||||||
qemu_iovec_from_buf(acb->qiov, 0, s->orig_buf + acb->start,
|
qemu_iovec_from_buffer(acb->qiov, s->orig_buf + acb->start,
|
||||||
acb->end - acb->start);
|
acb->end - acb->start);
|
||||||
acb->common.cb(acb->common.opaque, 0);
|
acb->common.cb(acb->common.opaque, 0);
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
s->acb[i] = NULL;
|
s->acb[i] = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
read_end:
|
||||||
return realsize;
|
return realsize;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,15 +170,14 @@ static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
|
|||||||
{
|
{
|
||||||
char *buf = state->orig_buf + (start - state->buf_start);
|
char *buf = state->orig_buf + (start - state->buf_start);
|
||||||
|
|
||||||
qemu_iovec_from_buf(acb->qiov, 0, buf, len);
|
qemu_iovec_from_buffer(acb->qiov, buf, len);
|
||||||
acb->common.cb(acb->common.opaque, 0);
|
acb->common.cb(acb->common.opaque, 0);
|
||||||
|
|
||||||
return FIND_RET_OK;
|
return FIND_RET_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for unfinished chunks
|
// Wait for unfinished chunks
|
||||||
if (state->in_use &&
|
if ((start >= state->buf_start) &&
|
||||||
(start >= state->buf_start) &&
|
|
||||||
(start <= buf_fend) &&
|
(start <= buf_fend) &&
|
||||||
(end >= state->buf_start) &&
|
(end >= state->buf_start) &&
|
||||||
(end <= buf_fend))
|
(end <= buf_fend))
|
||||||
@@ -275,90 +199,47 @@ static int curl_find_buf(BDRVCURLState *s, size_t start, size_t len,
|
|||||||
return FIND_RET_NONE;
|
return FIND_RET_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_multi_check_completion(BDRVCURLState *s)
|
static void curl_multi_do(void *arg)
|
||||||
{
|
{
|
||||||
|
BDRVCURLState *s = (BDRVCURLState *)arg;
|
||||||
|
int running;
|
||||||
|
int r;
|
||||||
int msgs_in_queue;
|
int msgs_in_queue;
|
||||||
|
|
||||||
|
if (!s->multi)
|
||||||
|
return;
|
||||||
|
|
||||||
|
do {
|
||||||
|
r = curl_multi_socket_all(s->multi, &running);
|
||||||
|
} while(r == CURLM_CALL_MULTI_PERFORM);
|
||||||
|
|
||||||
/* Try to find done transfers, so we can free the easy
|
/* Try to find done transfers, so we can free the easy
|
||||||
* handle again. */
|
* handle again. */
|
||||||
for (;;) {
|
do {
|
||||||
CURLMsg *msg;
|
CURLMsg *msg;
|
||||||
msg = curl_multi_info_read(s->multi, &msgs_in_queue);
|
msg = curl_multi_info_read(s->multi, &msgs_in_queue);
|
||||||
|
|
||||||
/* Quit when there are no more completions */
|
|
||||||
if (!msg)
|
if (!msg)
|
||||||
break;
|
break;
|
||||||
|
if (msg->msg == CURLMSG_NONE)
|
||||||
|
break;
|
||||||
|
|
||||||
if (msg->msg == CURLMSG_DONE) {
|
switch (msg->msg) {
|
||||||
|
case CURLMSG_DONE:
|
||||||
|
{
|
||||||
CURLState *state = NULL;
|
CURLState *state = NULL;
|
||||||
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE,
|
curl_easy_getinfo(msg->easy_handle, CURLINFO_PRIVATE, (char**)&state);
|
||||||
(char **)&state);
|
|
||||||
|
|
||||||
/* ACBs for successful messages get completed in curl_read_cb */
|
|
||||||
if (msg->data.result != CURLE_OK) {
|
|
||||||
int i;
|
|
||||||
for (i = 0; i < CURL_NUM_ACB; i++) {
|
|
||||||
CURLAIOCB *acb = state->acb[i];
|
|
||||||
|
|
||||||
if (acb == NULL) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
acb->common.cb(acb->common.opaque, -EIO);
|
|
||||||
qemu_aio_unref(acb);
|
|
||||||
state->acb[i] = NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_clean_state(state);
|
curl_clean_state(state);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
msgs_in_queue = 0;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
} while(msgs_in_queue);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_multi_do(void *arg)
|
static CURLState *curl_init_state(BDRVCURLState *s)
|
||||||
{
|
|
||||||
CURLState *s = (CURLState *)arg;
|
|
||||||
int running;
|
|
||||||
int r;
|
|
||||||
|
|
||||||
if (!s->s->multi) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
r = curl_multi_socket_action(s->s->multi, s->sock_fd, 0, &running);
|
|
||||||
} while(r == CURLM_CALL_MULTI_PERFORM);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_multi_read(void *arg)
|
|
||||||
{
|
|
||||||
CURLState *s = (CURLState *)arg;
|
|
||||||
|
|
||||||
curl_multi_do(arg);
|
|
||||||
curl_multi_check_completion(s->s);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_multi_timeout_do(void *arg)
|
|
||||||
{
|
|
||||||
#ifdef NEED_CURL_TIMER_CALLBACK
|
|
||||||
BDRVCURLState *s = (BDRVCURLState *)arg;
|
|
||||||
int running;
|
|
||||||
|
|
||||||
if (!s->multi) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
|
|
||||||
|
|
||||||
curl_multi_check_completion(s);
|
|
||||||
#else
|
|
||||||
abort();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
|
|
||||||
{
|
{
|
||||||
CURLState *state = NULL;
|
CURLState *state = NULL;
|
||||||
int i, j;
|
int i, j;
|
||||||
@@ -376,47 +257,32 @@ static CURLState *curl_init_state(BlockDriverState *bs, BDRVCURLState *s)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (!state) {
|
if (!state) {
|
||||||
aio_poll(bdrv_get_aio_context(bs), true);
|
usleep(100);
|
||||||
|
curl_multi_do(s);
|
||||||
}
|
}
|
||||||
} while(!state);
|
} while(!state);
|
||||||
|
|
||||||
if (!state->curl) {
|
if (state->curl)
|
||||||
|
goto has_curl;
|
||||||
|
|
||||||
state->curl = curl_easy_init();
|
state->curl = curl_easy_init();
|
||||||
if (!state->curl) {
|
if (!state->curl)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
|
curl_easy_setopt(state->curl, CURLOPT_URL, s->url);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_SSL_VERIFYPEER,
|
curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, 5);
|
||||||
(long) s->sslverify);
|
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
|
||||||
if (s->cookie) {
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_COOKIE, s->cookie);
|
|
||||||
}
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_TIMEOUT, (long)s->timeout);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION,
|
|
||||||
(void *)curl_read_cb);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
|
curl_easy_setopt(state->curl, CURLOPT_WRITEDATA, (void *)state);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
|
curl_easy_setopt(state->curl, CURLOPT_PRIVATE, (void *)state);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
|
curl_easy_setopt(state->curl, CURLOPT_AUTOREFERER, 1);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
|
curl_easy_setopt(state->curl, CURLOPT_FOLLOWLOCATION, 1);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
|
curl_easy_setopt(state->curl, CURLOPT_NOSIGNAL, 1);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
|
curl_easy_setopt(state->curl, CURLOPT_ERRORBUFFER, state->errmsg);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_FAILONERROR, 1);
|
|
||||||
|
|
||||||
/* Restrict supported protocols to avoid security issues in the more
|
|
||||||
* obscure protocols. For example, do not allow POP3/SMTP/IMAP see
|
|
||||||
* CVE-2013-0249.
|
|
||||||
*
|
|
||||||
* Restricting protocols is only supported from 7.19.4 upwards.
|
|
||||||
*/
|
|
||||||
#if LIBCURL_VERSION_NUM >= 0x071304
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_PROTOCOLS, PROTOCOLS);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_REDIR_PROTOCOLS, PROTOCOLS);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef DEBUG_VERBOSE
|
#ifdef DEBUG_VERBOSE
|
||||||
curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
|
curl_easy_setopt(state->curl, CURLOPT_VERBOSE, 1);
|
||||||
#endif
|
#endif
|
||||||
}
|
|
||||||
|
has_curl:
|
||||||
|
|
||||||
state->s = s;
|
state->s = s;
|
||||||
|
|
||||||
@@ -430,287 +296,194 @@ static void curl_clean_state(CURLState *s)
|
|||||||
s->in_use = 0;
|
s->in_use = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_parse_filename(const char *filename, QDict *options,
|
static int curl_open(BlockDriverState *bs, const char *filename, int flags)
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
qdict_put(options, CURL_BLOCK_OPT_URL, qstring_from_str(filename));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVCURLState *s = bs->opaque;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < CURL_NUM_STATES; i++) {
|
|
||||||
if (s->states[i].in_use) {
|
|
||||||
curl_clean_state(&s->states[i]);
|
|
||||||
}
|
|
||||||
if (s->states[i].curl) {
|
|
||||||
curl_easy_cleanup(s->states[i].curl);
|
|
||||||
s->states[i].curl = NULL;
|
|
||||||
}
|
|
||||||
g_free(s->states[i].orig_buf);
|
|
||||||
s->states[i].orig_buf = NULL;
|
|
||||||
}
|
|
||||||
if (s->multi) {
|
|
||||||
curl_multi_cleanup(s->multi);
|
|
||||||
s->multi = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
timer_del(&s->timer);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void curl_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
BDRVCURLState *s = bs->opaque;
|
|
||||||
|
|
||||||
aio_timer_init(new_context, &s->timer,
|
|
||||||
QEMU_CLOCK_REALTIME, SCALE_NS,
|
|
||||||
curl_multi_timeout_do, s);
|
|
||||||
|
|
||||||
assert(!s->multi);
|
|
||||||
s->multi = curl_multi_init();
|
|
||||||
s->aio_context = new_context;
|
|
||||||
curl_multi_setopt(s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb);
|
|
||||||
#ifdef NEED_CURL_TIMER_CALLBACK
|
|
||||||
curl_multi_setopt(s->multi, CURLMOPT_TIMERDATA, s);
|
|
||||||
curl_multi_setopt(s->multi, CURLMOPT_TIMERFUNCTION, curl_timer_cb);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "curl",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_URL,
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "URL to open",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_READAHEAD,
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "Readahead size",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_SSLVERIFY,
|
|
||||||
.type = QEMU_OPT_BOOL,
|
|
||||||
.help = "Verify SSL certificate"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_TIMEOUT,
|
|
||||||
.type = QEMU_OPT_NUMBER,
|
|
||||||
.help = "Curl timeout"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = CURL_BLOCK_OPT_COOKIE,
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "Pass the cookie or list of cookies with each request"
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int curl_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVCURLState *s = bs->opaque;
|
BDRVCURLState *s = bs->opaque;
|
||||||
CURLState *state = NULL;
|
CURLState *state = NULL;
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
const char *file;
|
|
||||||
const char *cookie;
|
|
||||||
double d;
|
double d;
|
||||||
|
|
||||||
|
#define RA_OPTSTR ":readahead="
|
||||||
|
char *file;
|
||||||
|
char *ra;
|
||||||
|
const char *ra_val;
|
||||||
|
int parse_state = 0;
|
||||||
|
|
||||||
static int inited = 0;
|
static int inited = 0;
|
||||||
|
|
||||||
if (flags & BDRV_O_RDWR) {
|
file = qemu_strdup(filename);
|
||||||
error_setg(errp, "curl block device does not support writes");
|
s->readahead_size = READ_AHEAD_SIZE;
|
||||||
return -EROFS;
|
|
||||||
|
/* Parse a trailing ":readahead=#:" param, if present. */
|
||||||
|
ra = file + strlen(file) - 1;
|
||||||
|
while (ra >= file) {
|
||||||
|
if (parse_state == 0) {
|
||||||
|
if (*ra == ':')
|
||||||
|
parse_state++;
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
} else if (parse_state == 1) {
|
||||||
|
if (*ra > '9' || *ra < '0') {
|
||||||
|
char *opt_start = ra - strlen(RA_OPTSTR) + 1;
|
||||||
|
if (opt_start > file &&
|
||||||
|
strncmp(opt_start, RA_OPTSTR, strlen(RA_OPTSTR)) == 0) {
|
||||||
|
ra_val = ra + 1;
|
||||||
|
ra -= strlen(RA_OPTSTR) - 1;
|
||||||
|
*ra = '\0';
|
||||||
|
s->readahead_size = atoi(ra_val);
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ra--;
|
||||||
}
|
}
|
||||||
|
|
||||||
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);
|
|
||||||
goto out_noclean;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->readahead_size = qemu_opt_get_size(opts, CURL_BLOCK_OPT_READAHEAD,
|
|
||||||
READ_AHEAD_DEFAULT);
|
|
||||||
if ((s->readahead_size & 0x1ff) != 0) {
|
if ((s->readahead_size & 0x1ff) != 0) {
|
||||||
error_setg(errp, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512",
|
fprintf(stderr, "HTTP_READAHEAD_SIZE %zd is not a multiple of 512\n",
|
||||||
s->readahead_size);
|
s->readahead_size);
|
||||||
goto out_noclean;
|
goto out_noclean;
|
||||||
}
|
}
|
||||||
|
|
||||||
s->timeout = qemu_opt_get_number(opts, CURL_BLOCK_OPT_TIMEOUT,
|
|
||||||
CURL_TIMEOUT_DEFAULT);
|
|
||||||
if (s->timeout > CURL_TIMEOUT_MAX) {
|
|
||||||
error_setg(errp, "timeout parameter is too large or negative");
|
|
||||||
goto out_noclean;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->sslverify = qemu_opt_get_bool(opts, CURL_BLOCK_OPT_SSLVERIFY, true);
|
|
||||||
|
|
||||||
cookie = qemu_opt_get(opts, CURL_BLOCK_OPT_COOKIE);
|
|
||||||
s->cookie = g_strdup(cookie);
|
|
||||||
|
|
||||||
file = qemu_opt_get(opts, CURL_BLOCK_OPT_URL);
|
|
||||||
if (file == NULL) {
|
|
||||||
error_setg(errp, "curl block driver requires an 'url' option");
|
|
||||||
goto out_noclean;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inited) {
|
if (!inited) {
|
||||||
curl_global_init(CURL_GLOBAL_ALL);
|
curl_global_init(CURL_GLOBAL_ALL);
|
||||||
inited = 1;
|
inited = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
DPRINTF("CURL: Opening %s\n", file);
|
DPRINTF("CURL: Opening %s\n", file);
|
||||||
s->aio_context = bdrv_get_aio_context(bs);
|
s->url = file;
|
||||||
s->url = g_strdup(file);
|
state = curl_init_state(s);
|
||||||
state = curl_init_state(bs, s);
|
|
||||||
if (!state)
|
if (!state)
|
||||||
goto out_noclean;
|
goto out_noclean;
|
||||||
|
|
||||||
// Get file size
|
// Get file size
|
||||||
|
|
||||||
s->accept_range = false;
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
|
curl_easy_setopt(state->curl, CURLOPT_NOBODY, 1);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_HEADERFUNCTION,
|
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_size_cb);
|
||||||
curl_header_cb);
|
|
||||||
curl_easy_setopt(state->curl, CURLOPT_HEADERDATA, s);
|
|
||||||
if (curl_easy_perform(state->curl))
|
if (curl_easy_perform(state->curl))
|
||||||
goto out;
|
goto out;
|
||||||
curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
|
curl_easy_getinfo(state->curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d);
|
||||||
|
curl_easy_setopt(state->curl, CURLOPT_WRITEFUNCTION, (void *)curl_read_cb);
|
||||||
|
curl_easy_setopt(state->curl, CURLOPT_NOBODY, 0);
|
||||||
if (d)
|
if (d)
|
||||||
s->len = (size_t)d;
|
s->len = (size_t)d;
|
||||||
else if(!s->len)
|
else if(!s->len)
|
||||||
goto out;
|
goto out;
|
||||||
if ((!strncasecmp(s->url, "http://", strlen("http://"))
|
|
||||||
|| !strncasecmp(s->url, "https://", strlen("https://")))
|
|
||||||
&& !s->accept_range) {
|
|
||||||
pstrcpy(state->errmsg, CURL_ERROR_SIZE,
|
|
||||||
"Server does not support 'range' (byte ranges).");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
DPRINTF("CURL: Size = %zd\n", s->len);
|
DPRINTF("CURL: Size = %zd\n", s->len);
|
||||||
|
|
||||||
curl_clean_state(state);
|
curl_clean_state(state);
|
||||||
curl_easy_cleanup(state->curl);
|
curl_easy_cleanup(state->curl);
|
||||||
state->curl = NULL;
|
state->curl = NULL;
|
||||||
|
|
||||||
curl_attach_aio_context(bs, bdrv_get_aio_context(bs));
|
// Now we know the file exists and its size, so let's
|
||||||
|
// initialize the multi interface!
|
||||||
|
|
||||||
|
s->multi = curl_multi_init();
|
||||||
|
curl_multi_setopt( s->multi, CURLMOPT_SOCKETDATA, s);
|
||||||
|
curl_multi_setopt( s->multi, CURLMOPT_SOCKETFUNCTION, curl_sock_cb );
|
||||||
|
curl_multi_do(s);
|
||||||
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
error_setg(errp, "CURL: Error opening file: %s", state->errmsg);
|
fprintf(stderr, "CURL: Error opening file: %s\n", state->errmsg);
|
||||||
curl_easy_cleanup(state->curl);
|
curl_easy_cleanup(state->curl);
|
||||||
state->curl = NULL;
|
state->curl = NULL;
|
||||||
out_noclean:
|
out_noclean:
|
||||||
g_free(s->cookie);
|
qemu_free(file);
|
||||||
g_free(s->url);
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const AIOCBInfo curl_aiocb_info = {
|
static void curl_aio_cancel(BlockDriverAIOCB *blockacb)
|
||||||
|
{
|
||||||
|
// Do we have to implement canceling? Seems to work without...
|
||||||
|
}
|
||||||
|
|
||||||
|
static AIOPool curl_aio_pool = {
|
||||||
.aiocb_size = sizeof(CURLAIOCB),
|
.aiocb_size = sizeof(CURLAIOCB),
|
||||||
|
.cancel = curl_aio_cancel,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static BlockDriverAIOCB *curl_aio_readv(BlockDriverState *bs,
|
||||||
static void curl_readv_bh_cb(void *p)
|
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
||||||
|
BlockDriverCompletionFunc *cb, void *opaque)
|
||||||
{
|
{
|
||||||
CURLState *state;
|
BDRVCURLState *s = bs->opaque;
|
||||||
int running;
|
CURLAIOCB *acb;
|
||||||
|
size_t start = sector_num * SECTOR_SIZE;
|
||||||
CURLAIOCB *acb = p;
|
|
||||||
BDRVCURLState *s = acb->common.bs->opaque;
|
|
||||||
|
|
||||||
qemu_bh_delete(acb->bh);
|
|
||||||
acb->bh = NULL;
|
|
||||||
|
|
||||||
size_t start = acb->sector_num * SECTOR_SIZE;
|
|
||||||
size_t end;
|
size_t end;
|
||||||
|
CURLState *state;
|
||||||
|
|
||||||
|
acb = qemu_aio_get(&curl_aio_pool, bs, cb, opaque);
|
||||||
|
if (!acb)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
acb->qiov = qiov;
|
||||||
|
|
||||||
// In case we have the requested data already (e.g. read-ahead),
|
// In case we have the requested data already (e.g. read-ahead),
|
||||||
// we can just call the callback and be done.
|
// we can just call the callback and be done.
|
||||||
switch (curl_find_buf(s, start, acb->nb_sectors * SECTOR_SIZE, acb)) {
|
|
||||||
|
switch (curl_find_buf(s, start, nb_sectors * SECTOR_SIZE, acb)) {
|
||||||
case FIND_RET_OK:
|
case FIND_RET_OK:
|
||||||
qemu_aio_unref(acb);
|
qemu_aio_release(acb);
|
||||||
// fall through
|
// fall through
|
||||||
case FIND_RET_WAIT:
|
case FIND_RET_WAIT:
|
||||||
return;
|
return &acb->common;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
// No cache found, so let's start a new request
|
// No cache found, so let's start a new request
|
||||||
state = curl_init_state(acb->common.bs, s);
|
|
||||||
if (!state) {
|
state = curl_init_state(s);
|
||||||
acb->common.cb(acb->common.opaque, -EIO);
|
if (!state)
|
||||||
qemu_aio_unref(acb);
|
return NULL;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
acb->start = 0;
|
acb->start = 0;
|
||||||
acb->end = (acb->nb_sectors * SECTOR_SIZE);
|
acb->end = (nb_sectors * SECTOR_SIZE);
|
||||||
|
|
||||||
state->buf_off = 0;
|
state->buf_off = 0;
|
||||||
g_free(state->orig_buf);
|
if (state->orig_buf)
|
||||||
|
qemu_free(state->orig_buf);
|
||||||
state->buf_start = start;
|
state->buf_start = start;
|
||||||
state->buf_len = acb->end + s->readahead_size;
|
state->buf_len = acb->end + s->readahead_size;
|
||||||
end = MIN(start + state->buf_len, s->len) - 1;
|
end = MIN(start + state->buf_len, s->len) - 1;
|
||||||
state->orig_buf = g_try_malloc(state->buf_len);
|
state->orig_buf = qemu_malloc(state->buf_len);
|
||||||
if (state->buf_len && state->orig_buf == NULL) {
|
|
||||||
curl_clean_state(state);
|
|
||||||
acb->common.cb(acb->common.opaque, -ENOMEM);
|
|
||||||
qemu_aio_unref(acb);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
state->acb[0] = acb;
|
state->acb[0] = acb;
|
||||||
|
|
||||||
snprintf(state->range, 127, "%zd-%zd", start, end);
|
snprintf(state->range, 127, "%zd-%zd", start, end);
|
||||||
DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
|
DPRINTF("CURL (AIO): Reading %d at %zd (%s)\n",
|
||||||
(acb->nb_sectors * SECTOR_SIZE), start, state->range);
|
(nb_sectors * SECTOR_SIZE), start, state->range);
|
||||||
curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
|
curl_easy_setopt(state->curl, CURLOPT_RANGE, state->range);
|
||||||
|
|
||||||
curl_multi_add_handle(s->multi, state->curl);
|
curl_multi_add_handle(s->multi, state->curl);
|
||||||
|
curl_multi_do(s);
|
||||||
|
|
||||||
/* Tell curl it needs to kick things off */
|
|
||||||
curl_multi_socket_action(s->multi, CURL_SOCKET_TIMEOUT, 0, &running);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *curl_aio_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb, void *opaque)
|
|
||||||
{
|
|
||||||
CURLAIOCB *acb;
|
|
||||||
|
|
||||||
acb = qemu_aio_get(&curl_aiocb_info, bs, cb, opaque);
|
|
||||||
|
|
||||||
acb->qiov = qiov;
|
|
||||||
acb->sector_num = sector_num;
|
|
||||||
acb->nb_sectors = nb_sectors;
|
|
||||||
|
|
||||||
acb->bh = aio_bh_new(bdrv_get_aio_context(bs), curl_readv_bh_cb, acb);
|
|
||||||
qemu_bh_schedule(acb->bh);
|
|
||||||
return &acb->common;
|
return &acb->common;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void curl_close(BlockDriverState *bs)
|
static void curl_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVCURLState *s = bs->opaque;
|
BDRVCURLState *s = bs->opaque;
|
||||||
|
int i;
|
||||||
|
|
||||||
DPRINTF("CURL: Close\n");
|
DPRINTF("CURL: Close\n");
|
||||||
curl_detach_aio_context(bs);
|
for (i=0; i<CURL_NUM_STATES; i++) {
|
||||||
|
if (s->states[i].in_use)
|
||||||
g_free(s->cookie);
|
curl_clean_state(&s->states[i]);
|
||||||
g_free(s->url);
|
if (s->states[i].curl) {
|
||||||
|
curl_easy_cleanup(s->states[i].curl);
|
||||||
|
s->states[i].curl = NULL;
|
||||||
|
}
|
||||||
|
if (s->states[i].orig_buf) {
|
||||||
|
qemu_free(s->states[i].orig_buf);
|
||||||
|
s->states[i].orig_buf = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (s->multi)
|
||||||
|
curl_multi_cleanup(s->multi);
|
||||||
|
if (s->url)
|
||||||
|
free(s->url);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t curl_getlength(BlockDriverState *bs)
|
static int64_t curl_getlength(BlockDriverState *bs)
|
||||||
@@ -724,15 +497,11 @@ static BlockDriver bdrv_http = {
|
|||||||
.protocol_name = "http",
|
.protocol_name = "http",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_https = {
|
static BlockDriver bdrv_https = {
|
||||||
@@ -740,15 +509,11 @@ static BlockDriver bdrv_https = {
|
|||||||
.protocol_name = "https",
|
.protocol_name = "https",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_ftp = {
|
static BlockDriver bdrv_ftp = {
|
||||||
@@ -756,15 +521,11 @@ static BlockDriver bdrv_ftp = {
|
|||||||
.protocol_name = "ftp",
|
.protocol_name = "ftp",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_ftps = {
|
static BlockDriver bdrv_ftps = {
|
||||||
@@ -772,15 +533,11 @@ static BlockDriver bdrv_ftps = {
|
|||||||
.protocol_name = "ftps",
|
.protocol_name = "ftps",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static BlockDriver bdrv_tftp = {
|
static BlockDriver bdrv_tftp = {
|
||||||
@@ -788,15 +545,11 @@ static BlockDriver bdrv_tftp = {
|
|||||||
.protocol_name = "tftp",
|
.protocol_name = "tftp",
|
||||||
|
|
||||||
.instance_size = sizeof(BDRVCURLState),
|
.instance_size = sizeof(BDRVCURLState),
|
||||||
.bdrv_parse_filename = curl_parse_filename,
|
|
||||||
.bdrv_file_open = curl_open,
|
.bdrv_file_open = curl_open,
|
||||||
.bdrv_close = curl_close,
|
.bdrv_close = curl_close,
|
||||||
.bdrv_getlength = curl_getlength,
|
.bdrv_getlength = curl_getlength,
|
||||||
|
|
||||||
.bdrv_aio_readv = curl_aio_readv,
|
.bdrv_aio_readv = curl_aio_readv,
|
||||||
|
|
||||||
.bdrv_detach_aio_context = curl_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = curl_attach_aio_context,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void curl_block_init(void)
|
static void curl_block_init(void)
|
||||||
|
|||||||
682
block/dmg.c
682
block/dmg.c
@@ -22,25 +22,12 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
#include "qemu-common.h"
|
#include "qemu-common.h"
|
||||||
#include "block/block_int.h"
|
#include "block_int.h"
|
||||||
#include "qemu/bswap.h"
|
#include "bswap.h"
|
||||||
#include "qemu/module.h"
|
#include "module.h"
|
||||||
#include <zlib.h>
|
#include <zlib.h>
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
#include <bzlib.h>
|
|
||||||
#endif
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
enum {
|
|
||||||
/* Limit chunk sizes to prevent unreasonable amounts of memory being used
|
|
||||||
* or truncating when converting to 32-bit types
|
|
||||||
*/
|
|
||||||
DMG_LENGTHS_MAX = 64 * 1024 * 1024, /* 64 MB */
|
|
||||||
DMG_SECTORCOUNTS_MAX = DMG_LENGTHS_MAX / 512,
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct BDRVDMGState {
|
typedef struct BDRVDMGState {
|
||||||
CoMutex lock;
|
|
||||||
/* each chunk contains a certain number of sectors,
|
/* each chunk contains a certain number of sectors,
|
||||||
* offsets[i] is the offset in the .dmg file,
|
* offsets[i] is the offset in the .dmg file,
|
||||||
* lengths[i] is the length of the compressed chunk,
|
* lengths[i] is the length of the compressed chunk,
|
||||||
@@ -59,596 +46,217 @@ typedef struct BDRVDMGState {
|
|||||||
uint8_t *compressed_chunk;
|
uint8_t *compressed_chunk;
|
||||||
uint8_t *uncompressed_chunk;
|
uint8_t *uncompressed_chunk;
|
||||||
z_stream zstream;
|
z_stream zstream;
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
bz_stream bzstream;
|
|
||||||
#endif
|
|
||||||
} BDRVDMGState;
|
} BDRVDMGState;
|
||||||
|
|
||||||
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
|
static int dmg_probe(const uint8_t *buf, int buf_size, const char *filename)
|
||||||
{
|
{
|
||||||
int len;
|
int len=strlen(filename);
|
||||||
|
if(len>4 && !strcmp(filename+len-4,".dmg"))
|
||||||
if (!filename) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = strlen(filename);
|
|
||||||
if (len > 4 && !strcmp(filename + len - 4, ".dmg")) {
|
|
||||||
return 2;
|
return 2;
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_uint64(BlockDriverState *bs, int64_t offset, uint64_t *result)
|
static off_t read_off(BlockDriverState *bs, int64_t offset)
|
||||||
{
|
{
|
||||||
uint64_t buffer;
|
uint64_t buffer;
|
||||||
int ret;
|
if (bdrv_pread(bs->file, offset, &buffer, 8) < 8)
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, offset, &buffer, 8);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
*result = be64_to_cpu(buffer);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
return be64_to_cpu(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_uint32(BlockDriverState *bs, int64_t offset, uint32_t *result)
|
static off_t read_uint32(BlockDriverState *bs, int64_t offset)
|
||||||
{
|
{
|
||||||
uint32_t buffer;
|
uint32_t buffer;
|
||||||
int ret;
|
if (bdrv_pread(bs->file, offset, &buffer, 4) < 4)
|
||||||
|
|
||||||
ret = bdrv_pread(bs->file, offset, &buffer, 4);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
*result = be32_to_cpu(buffer);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
return be32_to_cpu(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint64_t buff_read_uint64(const uint8_t *buffer, int64_t offset)
|
static int dmg_open(BlockDriverState *bs, int flags)
|
||||||
{
|
|
||||||
return be64_to_cpu(*(uint64_t *)&buffer[offset]);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline uint32_t buff_read_uint32(const uint8_t *buffer, int64_t offset)
|
|
||||||
{
|
|
||||||
return be32_to_cpu(*(uint32_t *)&buffer[offset]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Increase max chunk sizes, if necessary. This function is used to calculate
|
|
||||||
* the buffer sizes needed for compressed/uncompressed chunk I/O.
|
|
||||||
*/
|
|
||||||
static void update_max_chunk_size(BDRVDMGState *s, uint32_t chunk,
|
|
||||||
uint32_t *max_compressed_size,
|
|
||||||
uint32_t *max_sectors_per_chunk)
|
|
||||||
{
|
|
||||||
uint32_t compressed_size = 0;
|
|
||||||
uint32_t uncompressed_sectors = 0;
|
|
||||||
|
|
||||||
switch (s->types[chunk]) {
|
|
||||||
case 0x80000005: /* zlib compressed */
|
|
||||||
case 0x80000006: /* bzip2 compressed */
|
|
||||||
compressed_size = s->lengths[chunk];
|
|
||||||
uncompressed_sectors = s->sectorcounts[chunk];
|
|
||||||
break;
|
|
||||||
case 1: /* copy */
|
|
||||||
uncompressed_sectors = (s->lengths[chunk] + 511) / 512;
|
|
||||||
break;
|
|
||||||
case 2: /* zero */
|
|
||||||
/* as the all-zeroes block may be large, it is treated specially: the
|
|
||||||
* sector is not copied from a large buffer, a simple memset is used
|
|
||||||
* instead. Therefore uncompressed_sectors does not need to be set. */
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (compressed_size > *max_compressed_size) {
|
|
||||||
*max_compressed_size = compressed_size;
|
|
||||||
}
|
|
||||||
if (uncompressed_sectors > *max_sectors_per_chunk) {
|
|
||||||
*max_sectors_per_chunk = uncompressed_sectors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t dmg_find_koly_offset(BlockDriverState *file_bs, Error **errp)
|
|
||||||
{
|
|
||||||
int64_t length;
|
|
||||||
int64_t offset = 0;
|
|
||||||
uint8_t buffer[515];
|
|
||||||
int i, ret;
|
|
||||||
|
|
||||||
/* bdrv_getlength returns a multiple of block size (512), rounded up. Since
|
|
||||||
* dmg images can have odd sizes, try to look for the "koly" magic which
|
|
||||||
* marks the begin of the UDIF trailer (512 bytes). This magic can be found
|
|
||||||
* in the last 511 bytes of the second-last sector or the first 4 bytes of
|
|
||||||
* the last sector (search space: 515 bytes) */
|
|
||||||
length = bdrv_getlength(file_bs);
|
|
||||||
if (length < 0) {
|
|
||||||
error_setg_errno(errp, -length,
|
|
||||||
"Failed to get file size while reading UDIF trailer");
|
|
||||||
return length;
|
|
||||||
} else if (length < 512) {
|
|
||||||
error_setg(errp, "dmg file must be at least 512 bytes long");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
if (length > 511 + 512) {
|
|
||||||
offset = length - 511 - 512;
|
|
||||||
}
|
|
||||||
length = length < 515 ? length : 515;
|
|
||||||
ret = bdrv_pread(file_bs, offset, buffer, length);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg_errno(errp, -ret, "Failed while reading UDIF trailer");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
for (i = 0; i < length - 3; i++) {
|
|
||||||
if (buffer[i] == 'k' && buffer[i+1] == 'o' &&
|
|
||||||
buffer[i+2] == 'l' && buffer[i+3] == 'y') {
|
|
||||||
return offset + i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
error_setg(errp, "Could not locate UDIF trailer in dmg file");
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* used when building the sector table */
|
|
||||||
typedef struct DmgHeaderState {
|
|
||||||
/* used internally by dmg_read_mish_block to remember offsets of blocks
|
|
||||||
* across calls */
|
|
||||||
uint64_t data_fork_offset;
|
|
||||||
/* exported for dmg_open */
|
|
||||||
uint32_t max_compressed_size;
|
|
||||||
uint32_t max_sectors_per_chunk;
|
|
||||||
} DmgHeaderState;
|
|
||||||
|
|
||||||
static bool dmg_is_known_block_type(uint32_t entry_type)
|
|
||||||
{
|
|
||||||
switch (entry_type) {
|
|
||||||
case 0x00000001: /* uncompressed */
|
|
||||||
case 0x00000002: /* zeroes */
|
|
||||||
case 0x80000005: /* zlib */
|
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
case 0x80000006: /* bzip2 */
|
|
||||||
#endif
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_read_mish_block(BDRVDMGState *s, DmgHeaderState *ds,
|
|
||||||
uint8_t *buffer, uint32_t count)
|
|
||||||
{
|
|
||||||
uint32_t type, i;
|
|
||||||
int ret;
|
|
||||||
size_t new_size;
|
|
||||||
uint32_t chunk_count;
|
|
||||||
int64_t offset = 0;
|
|
||||||
uint64_t data_offset;
|
|
||||||
uint64_t in_offset = ds->data_fork_offset;
|
|
||||||
uint64_t out_offset;
|
|
||||||
|
|
||||||
type = buff_read_uint32(buffer, offset);
|
|
||||||
/* skip data that is not a valid MISH block (invalid magic or too small) */
|
|
||||||
if (type != 0x6d697368 || count < 244) {
|
|
||||||
/* assume success for now */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* chunk offsets are relative to this sector number */
|
|
||||||
out_offset = buff_read_uint64(buffer, offset + 8);
|
|
||||||
|
|
||||||
/* location in data fork for (compressed) blob (in bytes) */
|
|
||||||
data_offset = buff_read_uint64(buffer, offset + 0x18);
|
|
||||||
in_offset += data_offset;
|
|
||||||
|
|
||||||
/* move to begin of chunk entries */
|
|
||||||
offset += 204;
|
|
||||||
|
|
||||||
chunk_count = (count - 204) / 40;
|
|
||||||
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
|
|
||||||
s->types = g_realloc(s->types, new_size / 2);
|
|
||||||
s->offsets = g_realloc(s->offsets, new_size);
|
|
||||||
s->lengths = g_realloc(s->lengths, new_size);
|
|
||||||
s->sectors = g_realloc(s->sectors, new_size);
|
|
||||||
s->sectorcounts = g_realloc(s->sectorcounts, new_size);
|
|
||||||
|
|
||||||
for (i = s->n_chunks; i < s->n_chunks + chunk_count; i++) {
|
|
||||||
s->types[i] = buff_read_uint32(buffer, offset);
|
|
||||||
if (!dmg_is_known_block_type(s->types[i])) {
|
|
||||||
chunk_count--;
|
|
||||||
i--;
|
|
||||||
offset += 40;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* sector number */
|
|
||||||
s->sectors[i] = buff_read_uint64(buffer, offset + 8);
|
|
||||||
s->sectors[i] += out_offset;
|
|
||||||
|
|
||||||
/* sector count */
|
|
||||||
s->sectorcounts[i] = buff_read_uint64(buffer, offset + 0x10);
|
|
||||||
|
|
||||||
/* all-zeroes sector (type 2) does not need to be "uncompressed" and can
|
|
||||||
* therefore be unbounded. */
|
|
||||||
if (s->types[i] != 2 && s->sectorcounts[i] > DMG_SECTORCOUNTS_MAX) {
|
|
||||||
error_report("sector count %" PRIu64 " for chunk %" PRIu32
|
|
||||||
" is larger than max (%u)",
|
|
||||||
s->sectorcounts[i], i, DMG_SECTORCOUNTS_MAX);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* offset in (compressed) data fork */
|
|
||||||
s->offsets[i] = buff_read_uint64(buffer, offset + 0x18);
|
|
||||||
s->offsets[i] += in_offset;
|
|
||||||
|
|
||||||
/* length in (compressed) data fork */
|
|
||||||
s->lengths[i] = buff_read_uint64(buffer, offset + 0x20);
|
|
||||||
|
|
||||||
if (s->lengths[i] > DMG_LENGTHS_MAX) {
|
|
||||||
error_report("length %" PRIu64 " for chunk %" PRIu32
|
|
||||||
" is larger than max (%u)",
|
|
||||||
s->lengths[i], i, DMG_LENGTHS_MAX);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
update_max_chunk_size(s, i, &ds->max_compressed_size,
|
|
||||||
&ds->max_sectors_per_chunk);
|
|
||||||
offset += 40;
|
|
||||||
}
|
|
||||||
s->n_chunks += chunk_count;
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_read_resource_fork(BlockDriverState *bs, DmgHeaderState *ds,
|
|
||||||
uint64_t info_begin, uint64_t info_length)
|
|
||||||
{
|
{
|
||||||
BDRVDMGState *s = bs->opaque;
|
BDRVDMGState *s = bs->opaque;
|
||||||
int ret;
|
off_t info_begin,info_end,last_in_offset,last_out_offset;
|
||||||
uint32_t count, rsrc_data_offset;
|
uint32_t count;
|
||||||
uint8_t *buffer = NULL;
|
uint32_t max_compressed_size=1,max_sectors_per_chunk=1,i;
|
||||||
uint64_t info_end;
|
|
||||||
uint64_t offset;
|
|
||||||
|
|
||||||
/* read offset from begin of resource fork (info_begin) to resource data */
|
|
||||||
ret = read_uint32(bs, info_begin, &rsrc_data_offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (rsrc_data_offset > info_length) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* read length of resource data */
|
|
||||||
ret = read_uint32(bs, info_begin + 8, &count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (count == 0 || rsrc_data_offset + count > info_length) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* begin of resource data (consisting of one or more resources) */
|
|
||||||
offset = info_begin + rsrc_data_offset;
|
|
||||||
|
|
||||||
/* end of resource data (there is possibly a following resource map
|
|
||||||
* which will be ignored). */
|
|
||||||
info_end = offset + count;
|
|
||||||
|
|
||||||
/* read offsets (mish blocks) from one or more resources in resource data */
|
|
||||||
while (offset < info_end) {
|
|
||||||
/* size of following resource */
|
|
||||||
ret = read_uint32(bs, offset, &count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (count == 0 || count > info_end - offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
offset += 4;
|
|
||||||
|
|
||||||
buffer = g_realloc(buffer, count);
|
|
||||||
ret = bdrv_pread(bs->file, offset, buffer, count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = dmg_read_mish_block(s, ds, buffer, count);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
/* advance offset by size of resource */
|
|
||||||
offset += count;
|
|
||||||
}
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
g_free(buffer);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_read_plist_xml(BlockDriverState *bs, DmgHeaderState *ds,
|
|
||||||
uint64_t info_begin, uint64_t info_length)
|
|
||||||
{
|
|
||||||
BDRVDMGState *s = bs->opaque;
|
|
||||||
int ret;
|
|
||||||
uint8_t *buffer = NULL;
|
|
||||||
char *data_begin, *data_end;
|
|
||||||
|
|
||||||
/* Have at least some length to avoid NULL for g_malloc. Attempt to set a
|
|
||||||
* safe upper cap on the data length. A test sample had a XML length of
|
|
||||||
* about 1 MiB. */
|
|
||||||
if (info_length == 0 || info_length > 16 * 1024 * 1024) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer = g_malloc(info_length + 1);
|
|
||||||
buffer[info_length] = '\0';
|
|
||||||
ret = bdrv_pread(bs->file, info_begin, buffer, info_length);
|
|
||||||
if (ret != info_length) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* look for <data>...</data>. The data is 284 (0x11c) bytes after base64
|
|
||||||
* decode. The actual data element has 431 (0x1af) bytes which includes tabs
|
|
||||||
* and line feeds. */
|
|
||||||
data_end = (char *)buffer;
|
|
||||||
while ((data_begin = strstr(data_end, "<data>")) != NULL) {
|
|
||||||
guchar *mish;
|
|
||||||
gsize out_len = 0;
|
|
||||||
|
|
||||||
data_begin += 6;
|
|
||||||
data_end = strstr(data_begin, "</data>");
|
|
||||||
/* malformed XML? */
|
|
||||||
if (data_end == NULL) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
*data_end++ = '\0';
|
|
||||||
mish = g_base64_decode(data_begin, &out_len);
|
|
||||||
ret = dmg_read_mish_block(s, ds, mish, (uint32_t)out_len);
|
|
||||||
g_free(mish);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ret = 0;
|
|
||||||
|
|
||||||
fail:
|
|
||||||
g_free(buffer);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int dmg_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
BDRVDMGState *s = bs->opaque;
|
|
||||||
DmgHeaderState ds;
|
|
||||||
uint64_t rsrc_fork_offset, rsrc_fork_length;
|
|
||||||
uint64_t plist_xml_offset, plist_xml_length;
|
|
||||||
int64_t offset;
|
int64_t offset;
|
||||||
int ret;
|
|
||||||
|
|
||||||
bs->read_only = 1;
|
bs->read_only = 1;
|
||||||
s->n_chunks = 0;
|
s->n_chunks = 0;
|
||||||
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
s->offsets = s->lengths = s->sectors = s->sectorcounts = NULL;
|
||||||
/* used by dmg_read_mish_block to keep track of the current I/O position */
|
|
||||||
ds.data_fork_offset = 0;
|
|
||||||
ds.max_compressed_size = 1;
|
|
||||||
ds.max_sectors_per_chunk = 1;
|
|
||||||
|
|
||||||
/* locate the UDIF trailer */
|
/* read offset of info blocks */
|
||||||
offset = dmg_find_koly_offset(bs->file, errp);
|
offset = bdrv_getlength(bs->file);
|
||||||
if (offset < 0) {
|
if (offset < 0) {
|
||||||
ret = offset;
|
goto fail;
|
||||||
|
}
|
||||||
|
offset -= 0x1d8;
|
||||||
|
|
||||||
|
info_begin = read_off(bs, offset);
|
||||||
|
if (info_begin == 0) {
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* offset of data fork (DataForkOffset) */
|
if (read_uint32(bs, info_begin) != 0x100) {
|
||||||
ret = read_uint64(bs, offset + 0x18, &ds.data_fork_offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
} else if (ds.data_fork_offset > offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* offset of resource fork (RsrcForkOffset) */
|
count = read_uint32(bs, info_begin + 4);
|
||||||
ret = read_uint64(bs, offset + 0x28, &rsrc_fork_offset);
|
if (count == 0) {
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
ret = read_uint64(bs, offset + 0x30, &rsrc_fork_length);
|
info_end = info_begin + count;
|
||||||
if (ret < 0) {
|
|
||||||
|
offset = info_begin + 0x100;
|
||||||
|
|
||||||
|
/* read offsets */
|
||||||
|
last_in_offset = last_out_offset = 0;
|
||||||
|
while (offset < info_end) {
|
||||||
|
uint32_t type;
|
||||||
|
|
||||||
|
count = read_uint32(bs, offset);
|
||||||
|
if(count==0)
|
||||||
goto fail;
|
goto fail;
|
||||||
|
offset += 4;
|
||||||
|
|
||||||
|
type = read_uint32(bs, offset);
|
||||||
|
if (type == 0x6d697368 && count >= 244) {
|
||||||
|
int new_size, chunk_count;
|
||||||
|
|
||||||
|
offset += 4;
|
||||||
|
offset += 200;
|
||||||
|
|
||||||
|
chunk_count = (count-204)/40;
|
||||||
|
new_size = sizeof(uint64_t) * (s->n_chunks + chunk_count);
|
||||||
|
s->types = qemu_realloc(s->types, new_size/2);
|
||||||
|
s->offsets = qemu_realloc(s->offsets, new_size);
|
||||||
|
s->lengths = qemu_realloc(s->lengths, new_size);
|
||||||
|
s->sectors = qemu_realloc(s->sectors, new_size);
|
||||||
|
s->sectorcounts = qemu_realloc(s->sectorcounts, new_size);
|
||||||
|
|
||||||
|
for(i=s->n_chunks;i<s->n_chunks+chunk_count;i++) {
|
||||||
|
s->types[i] = read_uint32(bs, offset);
|
||||||
|
offset += 4;
|
||||||
|
if(s->types[i]!=0x80000005 && s->types[i]!=1 && s->types[i]!=2) {
|
||||||
|
if(s->types[i]==0xffffffff) {
|
||||||
|
last_in_offset = s->offsets[i-1]+s->lengths[i-1];
|
||||||
|
last_out_offset = s->sectors[i-1]+s->sectorcounts[i-1];
|
||||||
}
|
}
|
||||||
if (rsrc_fork_offset >= offset ||
|
chunk_count--;
|
||||||
rsrc_fork_length > offset - rsrc_fork_offset) {
|
i--;
|
||||||
ret = -EINVAL;
|
offset += 36;
|
||||||
goto fail;
|
continue;
|
||||||
}
|
}
|
||||||
/* offset of property list (XMLOffset) */
|
offset += 4;
|
||||||
ret = read_uint64(bs, offset + 0xd8, &plist_xml_offset);
|
|
||||||
if (ret < 0) {
|
s->sectors[i] = last_out_offset+read_off(bs, offset);
|
||||||
goto fail;
|
offset += 8;
|
||||||
|
|
||||||
|
s->sectorcounts[i] = read_off(bs, offset);
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
|
s->offsets[i] = last_in_offset+read_off(bs, offset);
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
|
s->lengths[i] = read_off(bs, offset);
|
||||||
|
offset += 8;
|
||||||
|
|
||||||
|
if(s->lengths[i]>max_compressed_size)
|
||||||
|
max_compressed_size = s->lengths[i];
|
||||||
|
if(s->sectorcounts[i]>max_sectors_per_chunk)
|
||||||
|
max_sectors_per_chunk = s->sectorcounts[i];
|
||||||
}
|
}
|
||||||
ret = read_uint64(bs, offset + 0xe0, &plist_xml_length);
|
s->n_chunks+=chunk_count;
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
if (plist_xml_offset >= offset ||
|
|
||||||
plist_xml_length > offset - plist_xml_offset) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
ret = read_uint64(bs, offset + 0x1ec, (uint64_t *)&bs->total_sectors);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (bs->total_sectors < 0) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (rsrc_fork_length != 0) {
|
|
||||||
ret = dmg_read_resource_fork(bs, &ds,
|
|
||||||
rsrc_fork_offset, rsrc_fork_length);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
} else if (plist_xml_length != 0) {
|
|
||||||
ret = dmg_read_plist_xml(bs, &ds, plist_xml_offset, plist_xml_length);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* initialize zlib engine */
|
/* initialize zlib engine */
|
||||||
s->compressed_chunk = qemu_try_blockalign(bs->file,
|
s->compressed_chunk = qemu_malloc(max_compressed_size+1);
|
||||||
ds.max_compressed_size + 1);
|
s->uncompressed_chunk = qemu_malloc(512*max_sectors_per_chunk);
|
||||||
s->uncompressed_chunk = qemu_try_blockalign(bs->file,
|
if(inflateInit(&s->zstream) != Z_OK)
|
||||||
512 * ds.max_sectors_per_chunk);
|
|
||||||
if (s->compressed_chunk == NULL || s->uncompressed_chunk == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
|
||||||
|
|
||||||
if (inflateInit(&s->zstream) != Z_OK) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->current_chunk = s->n_chunks;
|
s->current_chunk = s->n_chunks;
|
||||||
|
|
||||||
qemu_co_mutex_init(&s->lock);
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
fail:
|
fail:
|
||||||
g_free(s->types);
|
return -1;
|
||||||
g_free(s->offsets);
|
|
||||||
g_free(s->lengths);
|
|
||||||
g_free(s->sectors);
|
|
||||||
g_free(s->sectorcounts);
|
|
||||||
qemu_vfree(s->compressed_chunk);
|
|
||||||
qemu_vfree(s->uncompressed_chunk);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int is_sector_in_chunk(BDRVDMGState* s,
|
static inline int is_sector_in_chunk(BDRVDMGState* s,
|
||||||
uint32_t chunk_num, uint64_t sector_num)
|
uint32_t chunk_num,int sector_num)
|
||||||
{
|
{
|
||||||
if (chunk_num >= s->n_chunks || s->sectors[chunk_num] > sector_num ||
|
if(chunk_num>=s->n_chunks || s->sectors[chunk_num]>sector_num ||
|
||||||
s->sectors[chunk_num] + s->sectorcounts[chunk_num] <= sector_num) {
|
s->sectors[chunk_num]+s->sectorcounts[chunk_num]<=sector_num)
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
else
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline uint32_t search_chunk(BDRVDMGState *s, uint64_t sector_num)
|
static inline uint32_t search_chunk(BDRVDMGState* s,int sector_num)
|
||||||
{
|
{
|
||||||
/* binary search */
|
/* binary search */
|
||||||
uint32_t chunk1 = 0, chunk2 = s->n_chunks, chunk3;
|
uint32_t chunk1=0,chunk2=s->n_chunks,chunk3;
|
||||||
while (chunk1 != chunk2) {
|
while(chunk1!=chunk2) {
|
||||||
chunk3 = (chunk1 + chunk2) / 2;
|
chunk3 = (chunk1+chunk2)/2;
|
||||||
if (s->sectors[chunk3] > sector_num) {
|
if(s->sectors[chunk3]>sector_num)
|
||||||
chunk2 = chunk3;
|
chunk2 = chunk3;
|
||||||
} else if (s->sectors[chunk3] + s->sectorcounts[chunk3] > sector_num) {
|
else if(s->sectors[chunk3]+s->sectorcounts[chunk3]>sector_num)
|
||||||
return chunk3;
|
return chunk3;
|
||||||
} else {
|
else
|
||||||
chunk1 = chunk3;
|
chunk1 = chunk3;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return s->n_chunks; /* error */
|
return s->n_chunks; /* error */
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num)
|
static inline int dmg_read_chunk(BlockDriverState *bs, int sector_num)
|
||||||
{
|
{
|
||||||
BDRVDMGState *s = bs->opaque;
|
BDRVDMGState *s = bs->opaque;
|
||||||
|
|
||||||
if (!is_sector_in_chunk(s, s->current_chunk, sector_num)) {
|
if(!is_sector_in_chunk(s,s->current_chunk,sector_num)) {
|
||||||
int ret;
|
int ret;
|
||||||
uint32_t chunk = search_chunk(s, sector_num);
|
uint32_t chunk = search_chunk(s,sector_num);
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
uint64_t total_out;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (chunk >= s->n_chunks) {
|
if(chunk>=s->n_chunks)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
s->current_chunk = s->n_chunks;
|
s->current_chunk = s->n_chunks;
|
||||||
switch (s->types[chunk]) { /* block entry type */
|
switch(s->types[chunk]) {
|
||||||
case 0x80000005: { /* zlib compressed */
|
case 0x80000005: { /* zlib compressed */
|
||||||
|
int i;
|
||||||
|
|
||||||
/* we need to buffer, because only the chunk as whole can be
|
/* we need to buffer, because only the chunk as whole can be
|
||||||
* inflated. */
|
* inflated. */
|
||||||
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
i=0;
|
||||||
s->compressed_chunk, s->lengths[chunk]);
|
do {
|
||||||
if (ret != s->lengths[chunk]) {
|
ret = bdrv_pread(bs->file, s->offsets[chunk] + i,
|
||||||
|
s->compressed_chunk+i, s->lengths[chunk]-i);
|
||||||
|
if(ret<0 && errno==EINTR)
|
||||||
|
ret=0;
|
||||||
|
i+=ret;
|
||||||
|
} while(ret>=0 && ret+i<s->lengths[chunk]);
|
||||||
|
|
||||||
|
if (ret != s->lengths[chunk])
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
|
|
||||||
s->zstream.next_in = s->compressed_chunk;
|
s->zstream.next_in = s->compressed_chunk;
|
||||||
s->zstream.avail_in = s->lengths[chunk];
|
s->zstream.avail_in = s->lengths[chunk];
|
||||||
s->zstream.next_out = s->uncompressed_chunk;
|
s->zstream.next_out = s->uncompressed_chunk;
|
||||||
s->zstream.avail_out = 512 * s->sectorcounts[chunk];
|
s->zstream.avail_out = 512*s->sectorcounts[chunk];
|
||||||
ret = inflateReset(&s->zstream);
|
ret = inflateReset(&s->zstream);
|
||||||
if (ret != Z_OK) {
|
if(ret != Z_OK)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
ret = inflate(&s->zstream, Z_FINISH);
|
ret = inflate(&s->zstream, Z_FINISH);
|
||||||
if (ret != Z_STREAM_END ||
|
if(ret != Z_STREAM_END || s->zstream.total_out != 512*s->sectorcounts[chunk])
|
||||||
s->zstream.total_out != 512 * s->sectorcounts[chunk]) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
break; }
|
break; }
|
||||||
#ifdef CONFIG_BZIP2
|
|
||||||
case 0x80000006: /* bzip2 compressed */
|
|
||||||
/* we need to buffer, because only the chunk as whole can be
|
|
||||||
* inflated. */
|
|
||||||
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
|
||||||
s->compressed_chunk, s->lengths[chunk]);
|
|
||||||
if (ret != s->lengths[chunk]) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = BZ2_bzDecompressInit(&s->bzstream, 0, 0);
|
|
||||||
if (ret != BZ_OK) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
s->bzstream.next_in = (char *)s->compressed_chunk;
|
|
||||||
s->bzstream.avail_in = (unsigned int) s->lengths[chunk];
|
|
||||||
s->bzstream.next_out = (char *)s->uncompressed_chunk;
|
|
||||||
s->bzstream.avail_out = (unsigned int) 512 * s->sectorcounts[chunk];
|
|
||||||
ret = BZ2_bzDecompress(&s->bzstream);
|
|
||||||
total_out = ((uint64_t)s->bzstream.total_out_hi32 << 32) +
|
|
||||||
s->bzstream.total_out_lo32;
|
|
||||||
BZ2_bzDecompressEnd(&s->bzstream);
|
|
||||||
if (ret != BZ_STREAM_END ||
|
|
||||||
total_out != 512 * s->sectorcounts[chunk]) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
#endif /* CONFIG_BZIP2 */
|
|
||||||
case 1: /* copy */
|
case 1: /* copy */
|
||||||
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
ret = bdrv_pread(bs->file, s->offsets[chunk],
|
||||||
s->uncompressed_chunk, s->lengths[chunk]);
|
s->uncompressed_chunk, s->lengths[chunk]);
|
||||||
if (ret != s->lengths[chunk]) {
|
if (ret != s->lengths[chunk])
|
||||||
return -1;
|
return -1;
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 2: /* zero */
|
case 2: /* zero */
|
||||||
/* see dmg_read, it is treated specially. No buffer needs to be
|
memset(s->uncompressed_chunk, 0, 512*s->sectorcounts[chunk]);
|
||||||
* pre-filled, the zeroes can be set directly. */
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
s->current_chunk = chunk;
|
s->current_chunk = chunk;
|
||||||
@@ -662,48 +270,28 @@ static int dmg_read(BlockDriverState *bs, int64_t sector_num,
|
|||||||
BDRVDMGState *s = bs->opaque;
|
BDRVDMGState *s = bs->opaque;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < nb_sectors; i++) {
|
for(i=0;i<nb_sectors;i++) {
|
||||||
uint32_t sector_offset_in_chunk;
|
uint32_t sector_offset_in_chunk;
|
||||||
if (dmg_read_chunk(bs, sector_num + i) != 0) {
|
if(dmg_read_chunk(bs, sector_num+i) != 0)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
sector_offset_in_chunk = sector_num+i-s->sectors[s->current_chunk];
|
||||||
/* Special case: current chunk is all zeroes. Do not perform a memcpy as
|
memcpy(buf+i*512,s->uncompressed_chunk+sector_offset_in_chunk*512,512);
|
||||||
* s->uncompressed_chunk may be too small to cover the large all-zeroes
|
|
||||||
* section. dmg_read_chunk is called to find s->current_chunk */
|
|
||||||
if (s->types[s->current_chunk] == 2) { /* all zeroes block entry */
|
|
||||||
memset(buf + i * 512, 0, 512);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
sector_offset_in_chunk = sector_num + i - s->sectors[s->current_chunk];
|
|
||||||
memcpy(buf + i * 512,
|
|
||||||
s->uncompressed_chunk + sector_offset_in_chunk * 512, 512);
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static coroutine_fn int dmg_co_read(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
uint8_t *buf, int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
BDRVDMGState *s = bs->opaque;
|
|
||||||
qemu_co_mutex_lock(&s->lock);
|
|
||||||
ret = dmg_read(bs, sector_num, buf, nb_sectors);
|
|
||||||
qemu_co_mutex_unlock(&s->lock);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void dmg_close(BlockDriverState *bs)
|
static void dmg_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVDMGState *s = bs->opaque;
|
BDRVDMGState *s = bs->opaque;
|
||||||
|
if(s->n_chunks>0) {
|
||||||
g_free(s->types);
|
free(s->types);
|
||||||
g_free(s->offsets);
|
free(s->offsets);
|
||||||
g_free(s->lengths);
|
free(s->lengths);
|
||||||
g_free(s->sectors);
|
free(s->sectors);
|
||||||
g_free(s->sectorcounts);
|
free(s->sectorcounts);
|
||||||
qemu_vfree(s->compressed_chunk);
|
}
|
||||||
qemu_vfree(s->uncompressed_chunk);
|
free(s->compressed_chunk);
|
||||||
|
free(s->uncompressed_chunk);
|
||||||
inflateEnd(&s->zstream);
|
inflateEnd(&s->zstream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -712,7 +300,7 @@ static BlockDriver bdrv_dmg = {
|
|||||||
.instance_size = sizeof(BDRVDMGState),
|
.instance_size = sizeof(BDRVDMGState),
|
||||||
.bdrv_probe = dmg_probe,
|
.bdrv_probe = dmg_probe,
|
||||||
.bdrv_open = dmg_open,
|
.bdrv_open = dmg_open,
|
||||||
.bdrv_read = dmg_co_read,
|
.bdrv_read = dmg_read,
|
||||||
.bdrv_close = dmg_close,
|
.bdrv_close = dmg_close,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
833
block/gluster.c
833
block/gluster.c
@@ -1,833 +0,0 @@
|
|||||||
/*
|
|
||||||
* GlusterFS backend for QEMU
|
|
||||||
*
|
|
||||||
* Copyright (C) 2012 Bharata B Rao <bharata@linux.vnet.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.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#include <glusterfs/api/glfs.h>
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "qemu/uri.h"
|
|
||||||
|
|
||||||
typedef struct GlusterAIOCB {
|
|
||||||
int64_t size;
|
|
||||||
int ret;
|
|
||||||
QEMUBH *bh;
|
|
||||||
Coroutine *coroutine;
|
|
||||||
AioContext *aio_context;
|
|
||||||
} GlusterAIOCB;
|
|
||||||
|
|
||||||
typedef struct BDRVGlusterState {
|
|
||||||
struct glfs *glfs;
|
|
||||||
struct glfs_fd *fd;
|
|
||||||
} BDRVGlusterState;
|
|
||||||
|
|
||||||
typedef struct GlusterConf {
|
|
||||||
char *server;
|
|
||||||
int port;
|
|
||||||
char *volname;
|
|
||||||
char *image;
|
|
||||||
char *transport;
|
|
||||||
} GlusterConf;
|
|
||||||
|
|
||||||
static void qemu_gluster_gconf_free(GlusterConf *gconf)
|
|
||||||
{
|
|
||||||
if (gconf) {
|
|
||||||
g_free(gconf->server);
|
|
||||||
g_free(gconf->volname);
|
|
||||||
g_free(gconf->image);
|
|
||||||
g_free(gconf->transport);
|
|
||||||
g_free(gconf);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int parse_volume_options(GlusterConf *gconf, char *path)
|
|
||||||
{
|
|
||||||
char *p, *q;
|
|
||||||
|
|
||||||
if (!path) {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* volume */
|
|
||||||
p = q = path + strspn(path, "/");
|
|
||||||
p += strcspn(p, "/");
|
|
||||||
if (*p == '\0') {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
gconf->volname = g_strndup(q, p - q);
|
|
||||||
|
|
||||||
/* image */
|
|
||||||
p += strspn(p, "/");
|
|
||||||
if (*p == '\0') {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
gconf->image = g_strdup(p);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* file=gluster[+transport]://[server[:port]]/volname/image[?socket=...]
|
|
||||||
*
|
|
||||||
* 'gluster' is the protocol.
|
|
||||||
*
|
|
||||||
* 'transport' specifies the transport type used to connect to gluster
|
|
||||||
* management daemon (glusterd). Valid transport types are
|
|
||||||
* tcp, unix and rdma. If a transport type isn't specified, then tcp
|
|
||||||
* type is assumed.
|
|
||||||
*
|
|
||||||
* 'server' specifies the server where the volume file specification for
|
|
||||||
* the given volume resides. This can be either hostname, ipv4 address
|
|
||||||
* or ipv6 address. ipv6 address needs to be within square brackets [ ].
|
|
||||||
* If transport type is 'unix', then 'server' field should not be specified.
|
|
||||||
* The 'socket' field needs to be populated with the path to unix domain
|
|
||||||
* socket.
|
|
||||||
*
|
|
||||||
* 'port' is the port number on which glusterd is listening. This is optional
|
|
||||||
* and if not specified, QEMU will send 0 which will make gluster to use the
|
|
||||||
* default port. If the transport type is unix, then 'port' should not be
|
|
||||||
* specified.
|
|
||||||
*
|
|
||||||
* 'volname' is the name of the gluster volume which contains the VM image.
|
|
||||||
*
|
|
||||||
* 'image' is the path to the actual VM image that resides on gluster volume.
|
|
||||||
*
|
|
||||||
* Examples:
|
|
||||||
*
|
|
||||||
* file=gluster://1.2.3.4/testvol/a.img
|
|
||||||
* file=gluster+tcp://1.2.3.4/testvol/a.img
|
|
||||||
* file=gluster+tcp://1.2.3.4:24007/testvol/dir/a.img
|
|
||||||
* file=gluster+tcp://[1:2:3:4:5:6:7:8]/testvol/dir/a.img
|
|
||||||
* file=gluster+tcp://[1:2:3:4:5:6:7:8]:24007/testvol/dir/a.img
|
|
||||||
* file=gluster+tcp://server.domain.com:24007/testvol/dir/a.img
|
|
||||||
* file=gluster+unix:///testvol/dir/a.img?socket=/tmp/glusterd.socket
|
|
||||||
* file=gluster+rdma://1.2.3.4:24007/testvol/a.img
|
|
||||||
*/
|
|
||||||
static int qemu_gluster_parseuri(GlusterConf *gconf, const char *filename)
|
|
||||||
{
|
|
||||||
URI *uri;
|
|
||||||
QueryParams *qp = NULL;
|
|
||||||
bool is_unix = false;
|
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
uri = uri_parse(filename);
|
|
||||||
if (!uri) {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* transport */
|
|
||||||
if (!uri->scheme || !strcmp(uri->scheme, "gluster")) {
|
|
||||||
gconf->transport = g_strdup("tcp");
|
|
||||||
} else if (!strcmp(uri->scheme, "gluster+tcp")) {
|
|
||||||
gconf->transport = g_strdup("tcp");
|
|
||||||
} else if (!strcmp(uri->scheme, "gluster+unix")) {
|
|
||||||
gconf->transport = g_strdup("unix");
|
|
||||||
is_unix = true;
|
|
||||||
} else if (!strcmp(uri->scheme, "gluster+rdma")) {
|
|
||||||
gconf->transport = g_strdup("rdma");
|
|
||||||
} else {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = parse_volume_options(gconf, uri->path);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
qp = query_params_parse(uri->query);
|
|
||||||
if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_unix) {
|
|
||||||
if (uri->server || uri->port) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
if (strcmp(qp->p[0].name, "socket")) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
gconf->server = g_strdup(qp->p[0].value);
|
|
||||||
} else {
|
|
||||||
gconf->server = g_strdup(uri->server ? uri->server : "localhost");
|
|
||||||
gconf->port = uri->port;
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (qp) {
|
|
||||||
query_params_free(qp);
|
|
||||||
}
|
|
||||||
uri_free(uri);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct glfs *qemu_gluster_init(GlusterConf *gconf, const char *filename,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
struct glfs *glfs = NULL;
|
|
||||||
int ret;
|
|
||||||
int old_errno;
|
|
||||||
|
|
||||||
ret = qemu_gluster_parseuri(gconf, filename);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Usage: file=gluster[+transport]://[server[:port]]/"
|
|
||||||
"volname/image[?socket=...]");
|
|
||||||
errno = -ret;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
glfs = glfs_new(gconf->volname);
|
|
||||||
if (!glfs) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = glfs_set_volfile_server(glfs, gconf->transport, gconf->server,
|
|
||||||
gconf->port);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* TODO: Use GF_LOG_ERROR instead of hard code value of 4 here when
|
|
||||||
* GlusterFS makes GF_LOG_* macros available to libgfapi users.
|
|
||||||
*/
|
|
||||||
ret = glfs_set_logging(glfs, "-", 4);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = glfs_init(glfs);
|
|
||||||
if (ret) {
|
|
||||||
error_setg_errno(errp, errno,
|
|
||||||
"Gluster connection failed for server=%s port=%d "
|
|
||||||
"volume=%s image=%s transport=%s", gconf->server,
|
|
||||||
gconf->port, gconf->volname, gconf->image,
|
|
||||||
gconf->transport);
|
|
||||||
|
|
||||||
/* glfs_init sometimes doesn't set errno although docs suggest that */
|
|
||||||
if (errno == 0)
|
|
||||||
errno = EINVAL;
|
|
||||||
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
return glfs;
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (glfs) {
|
|
||||||
old_errno = errno;
|
|
||||||
glfs_fini(glfs);
|
|
||||||
errno = old_errno;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void qemu_gluster_complete_aio(void *opaque)
|
|
||||||
{
|
|
||||||
GlusterAIOCB *acb = (GlusterAIOCB *)opaque;
|
|
||||||
|
|
||||||
qemu_bh_delete(acb->bh);
|
|
||||||
acb->bh = NULL;
|
|
||||||
qemu_coroutine_enter(acb->coroutine, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* AIO callback routine called from GlusterFS thread.
|
|
||||||
*/
|
|
||||||
static void gluster_finish_aiocb(struct glfs_fd *fd, ssize_t ret, void *arg)
|
|
||||||
{
|
|
||||||
GlusterAIOCB *acb = (GlusterAIOCB *)arg;
|
|
||||||
|
|
||||||
if (!ret || ret == acb->size) {
|
|
||||||
acb->ret = 0; /* Success */
|
|
||||||
} else if (ret < 0) {
|
|
||||||
acb->ret = ret; /* Read/Write failed */
|
|
||||||
} else {
|
|
||||||
acb->ret = -EIO; /* Partial read/write - fail it */
|
|
||||||
}
|
|
||||||
|
|
||||||
acb->bh = aio_bh_new(acb->aio_context, qemu_gluster_complete_aio, acb);
|
|
||||||
qemu_bh_schedule(acb->bh);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO Convert to fine grained options */
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "gluster",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "filename",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "URL to the gluster image",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static void qemu_gluster_parse_flags(int bdrv_flags, int *open_flags)
|
|
||||||
{
|
|
||||||
assert(open_flags != NULL);
|
|
||||||
|
|
||||||
*open_flags |= O_BINARY;
|
|
||||||
|
|
||||||
if (bdrv_flags & BDRV_O_RDWR) {
|
|
||||||
*open_flags |= O_RDWR;
|
|
||||||
} else {
|
|
||||||
*open_flags |= O_RDONLY;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((bdrv_flags & BDRV_O_NOCACHE)) {
|
|
||||||
*open_flags |= O_DIRECT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int qemu_gluster_open(BlockDriverState *bs, QDict *options,
|
|
||||||
int bdrv_flags, Error **errp)
|
|
||||||
{
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
int open_flags = 0;
|
|
||||||
int ret = 0;
|
|
||||||
GlusterConf *gconf = g_new0(GlusterConf, 1);
|
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
const char *filename;
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
filename = qemu_opt_get(opts, "filename");
|
|
||||||
|
|
||||||
s->glfs = qemu_gluster_init(gconf, filename, errp);
|
|
||||||
if (!s->glfs) {
|
|
||||||
ret = -errno;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_gluster_parse_flags(bdrv_flags, &open_flags);
|
|
||||||
|
|
||||||
s->fd = glfs_open(s->glfs, gconf->image, open_flags);
|
|
||||||
if (!s->fd) {
|
|
||||||
ret = -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
qemu_gluster_gconf_free(gconf);
|
|
||||||
if (!ret) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
if (s->fd) {
|
|
||||||
glfs_close(s->fd);
|
|
||||||
}
|
|
||||||
if (s->glfs) {
|
|
||||||
glfs_fini(s->glfs);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct BDRVGlusterReopenState {
|
|
||||||
struct glfs *glfs;
|
|
||||||
struct glfs_fd *fd;
|
|
||||||
} BDRVGlusterReopenState;
|
|
||||||
|
|
||||||
|
|
||||||
static int qemu_gluster_reopen_prepare(BDRVReopenState *state,
|
|
||||||
BlockReopenQueue *queue, Error **errp)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
BDRVGlusterReopenState *reop_s;
|
|
||||||
GlusterConf *gconf = NULL;
|
|
||||||
int open_flags = 0;
|
|
||||||
|
|
||||||
assert(state != NULL);
|
|
||||||
assert(state->bs != NULL);
|
|
||||||
|
|
||||||
state->opaque = g_new0(BDRVGlusterReopenState, 1);
|
|
||||||
reop_s = state->opaque;
|
|
||||||
|
|
||||||
qemu_gluster_parse_flags(state->flags, &open_flags);
|
|
||||||
|
|
||||||
gconf = g_new0(GlusterConf, 1);
|
|
||||||
|
|
||||||
reop_s->glfs = qemu_gluster_init(gconf, state->bs->filename, errp);
|
|
||||||
if (reop_s->glfs == NULL) {
|
|
||||||
ret = -errno;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
reop_s->fd = glfs_open(reop_s->glfs, gconf->image, open_flags);
|
|
||||||
if (reop_s->fd == NULL) {
|
|
||||||
/* reops->glfs will be cleaned up in _abort */
|
|
||||||
ret = -errno;
|
|
||||||
goto exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
exit:
|
|
||||||
/* state->opaque will be freed in either the _abort or _commit */
|
|
||||||
qemu_gluster_gconf_free(gconf);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void qemu_gluster_reopen_commit(BDRVReopenState *state)
|
|
||||||
{
|
|
||||||
BDRVGlusterReopenState *reop_s = state->opaque;
|
|
||||||
BDRVGlusterState *s = state->bs->opaque;
|
|
||||||
|
|
||||||
|
|
||||||
/* close the old */
|
|
||||||
if (s->fd) {
|
|
||||||
glfs_close(s->fd);
|
|
||||||
}
|
|
||||||
if (s->glfs) {
|
|
||||||
glfs_fini(s->glfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* use the newly opened image / connection */
|
|
||||||
s->fd = reop_s->fd;
|
|
||||||
s->glfs = reop_s->glfs;
|
|
||||||
|
|
||||||
g_free(state->opaque);
|
|
||||||
state->opaque = NULL;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void qemu_gluster_reopen_abort(BDRVReopenState *state)
|
|
||||||
{
|
|
||||||
BDRVGlusterReopenState *reop_s = state->opaque;
|
|
||||||
|
|
||||||
if (reop_s == NULL) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reop_s->fd) {
|
|
||||||
glfs_close(reop_s->fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reop_s->glfs) {
|
|
||||||
glfs_fini(reop_s->glfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(state->opaque);
|
|
||||||
state->opaque = NULL;
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
static coroutine_fn int qemu_gluster_co_write_zeroes(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB *acb = g_slice_new(GlusterAIOCB);
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
off_t size = nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
off_t offset = sector_num * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
acb->size = size;
|
|
||||||
acb->ret = 0;
|
|
||||||
acb->coroutine = qemu_coroutine_self();
|
|
||||||
acb->aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
ret = glfs_zerofill_async(s->fd, offset, size, &gluster_finish_aiocb, acb);
|
|
||||||
if (ret < 0) {
|
|
||||||
ret = -errno;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
ret = acb->ret;
|
|
||||||
|
|
||||||
out:
|
|
||||||
g_slice_free(GlusterAIOCB, acb);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline bool gluster_supports_zerofill(void)
|
|
||||||
{
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
|
|
||||||
int64_t size)
|
|
||||||
{
|
|
||||||
return glfs_zerofill(fd, offset, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
#else
|
|
||||||
static inline bool gluster_supports_zerofill(void)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline int qemu_gluster_zerofill(struct glfs_fd *fd, int64_t offset,
|
|
||||||
int64_t size)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int qemu_gluster_create(const char *filename,
|
|
||||||
QemuOpts *opts, Error **errp)
|
|
||||||
{
|
|
||||||
struct glfs *glfs;
|
|
||||||
struct glfs_fd *fd;
|
|
||||||
int ret = 0;
|
|
||||||
int prealloc = 0;
|
|
||||||
int64_t total_size = 0;
|
|
||||||
char *tmp = NULL;
|
|
||||||
GlusterConf *gconf = g_new0(GlusterConf, 1);
|
|
||||||
|
|
||||||
glfs = qemu_gluster_init(gconf, filename, errp);
|
|
||||||
if (!glfs) {
|
|
||||||
ret = -errno;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
|
||||||
BDRV_SECTOR_SIZE);
|
|
||||||
|
|
||||||
tmp = qemu_opt_get_del(opts, BLOCK_OPT_PREALLOC);
|
|
||||||
if (!tmp || !strcmp(tmp, "off")) {
|
|
||||||
prealloc = 0;
|
|
||||||
} else if (!strcmp(tmp, "full") &&
|
|
||||||
gluster_supports_zerofill()) {
|
|
||||||
prealloc = 1;
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "Invalid preallocation mode: '%s'"
|
|
||||||
" or GlusterFS doesn't support zerofill API",
|
|
||||||
tmp);
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
fd = glfs_creat(glfs, gconf->image,
|
|
||||||
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, S_IRUSR | S_IWUSR);
|
|
||||||
if (!fd) {
|
|
||||||
ret = -errno;
|
|
||||||
} else {
|
|
||||||
if (!glfs_ftruncate(fd, total_size)) {
|
|
||||||
if (prealloc && qemu_gluster_zerofill(fd, 0, total_size)) {
|
|
||||||
ret = -errno;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (glfs_close(fd) != 0) {
|
|
||||||
ret = -errno;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
out:
|
|
||||||
g_free(tmp);
|
|
||||||
qemu_gluster_gconf_free(gconf);
|
|
||||||
if (glfs) {
|
|
||||||
glfs_fini(glfs);
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_rw(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int write)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB *acb = g_slice_new(GlusterAIOCB);
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
size_t size = nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
off_t offset = sector_num * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
acb->size = size;
|
|
||||||
acb->ret = 0;
|
|
||||||
acb->coroutine = qemu_coroutine_self();
|
|
||||||
acb->aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
if (write) {
|
|
||||||
ret = glfs_pwritev_async(s->fd, qiov->iov, qiov->niov, offset, 0,
|
|
||||||
&gluster_finish_aiocb, acb);
|
|
||||||
} else {
|
|
||||||
ret = glfs_preadv_async(s->fd, qiov->iov, qiov->niov, offset, 0,
|
|
||||||
&gluster_finish_aiocb, acb);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
ret = -errno;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
ret = acb->ret;
|
|
||||||
|
|
||||||
out:
|
|
||||||
g_slice_free(GlusterAIOCB, acb);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int qemu_gluster_truncate(BlockDriverState *bs, int64_t offset)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
|
|
||||||
ret = glfs_ftruncate(s->fd, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -errno;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return qemu_gluster_co_rw(bs, sector_num, nb_sectors, qiov, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int qemu_gluster_co_flush_to_disk(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB *acb = g_slice_new(GlusterAIOCB);
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
|
|
||||||
acb->size = 0;
|
|
||||||
acb->ret = 0;
|
|
||||||
acb->coroutine = qemu_coroutine_self();
|
|
||||||
acb->aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
ret = glfs_fsync_async(s->fd, &gluster_finish_aiocb, acb);
|
|
||||||
if (ret < 0) {
|
|
||||||
ret = -errno;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
ret = acb->ret;
|
|
||||||
|
|
||||||
out:
|
|
||||||
g_slice_free(GlusterAIOCB, acb);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
static coroutine_fn int qemu_gluster_co_discard(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
GlusterAIOCB *acb = g_slice_new(GlusterAIOCB);
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
size_t size = nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
off_t offset = sector_num * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
acb->size = 0;
|
|
||||||
acb->ret = 0;
|
|
||||||
acb->coroutine = qemu_coroutine_self();
|
|
||||||
acb->aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
ret = glfs_discard_async(s->fd, offset, size, &gluster_finish_aiocb, acb);
|
|
||||||
if (ret < 0) {
|
|
||||||
ret = -errno;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
ret = acb->ret;
|
|
||||||
|
|
||||||
out:
|
|
||||||
g_slice_free(GlusterAIOCB, acb);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
static int64_t qemu_gluster_getlength(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
int64_t ret;
|
|
||||||
|
|
||||||
ret = glfs_lseek(s->fd, 0, SEEK_END);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -errno;
|
|
||||||
} else {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t qemu_gluster_allocated_file_size(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
struct stat st;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = glfs_fstat(s->fd, &st);
|
|
||||||
if (ret < 0) {
|
|
||||||
return -errno;
|
|
||||||
} else {
|
|
||||||
return st.st_blocks * 512;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void qemu_gluster_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVGlusterState *s = bs->opaque;
|
|
||||||
|
|
||||||
if (s->fd) {
|
|
||||||
glfs_close(s->fd);
|
|
||||||
s->fd = NULL;
|
|
||||||
}
|
|
||||||
glfs_fini(s->glfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int qemu_gluster_has_zero_init(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
/* GlusterFS volume could be backed by a block device */
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList qemu_gluster_create_opts = {
|
|
||||||
.name = "qemu-gluster-create-opts",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(qemu_gluster_create_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = BLOCK_OPT_SIZE,
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "Virtual disk size"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = BLOCK_OPT_PREALLOC,
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "Preallocation mode (allowed values: off, full)"
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster = {
|
|
||||||
.format_name = "gluster",
|
|
||||||
.protocol_name = "gluster",
|
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
|
||||||
.bdrv_create = qemu_gluster_create,
|
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster_tcp = {
|
|
||||||
.format_name = "gluster",
|
|
||||||
.protocol_name = "gluster+tcp",
|
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
|
||||||
.bdrv_create = qemu_gluster_create,
|
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster_unix = {
|
|
||||||
.format_name = "gluster",
|
|
||||||
.protocol_name = "gluster+unix",
|
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
|
||||||
.bdrv_create = qemu_gluster_create,
|
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_gluster_rdma = {
|
|
||||||
.format_name = "gluster",
|
|
||||||
.protocol_name = "gluster+rdma",
|
|
||||||
.instance_size = sizeof(BDRVGlusterState),
|
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.bdrv_file_open = qemu_gluster_open,
|
|
||||||
.bdrv_reopen_prepare = qemu_gluster_reopen_prepare,
|
|
||||||
.bdrv_reopen_commit = qemu_gluster_reopen_commit,
|
|
||||||
.bdrv_reopen_abort = qemu_gluster_reopen_abort,
|
|
||||||
.bdrv_close = qemu_gluster_close,
|
|
||||||
.bdrv_create = qemu_gluster_create,
|
|
||||||
.bdrv_getlength = qemu_gluster_getlength,
|
|
||||||
.bdrv_get_allocated_file_size = qemu_gluster_allocated_file_size,
|
|
||||||
.bdrv_truncate = qemu_gluster_truncate,
|
|
||||||
.bdrv_co_readv = qemu_gluster_co_readv,
|
|
||||||
.bdrv_co_writev = qemu_gluster_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = qemu_gluster_co_flush_to_disk,
|
|
||||||
.bdrv_has_zero_init = qemu_gluster_has_zero_init,
|
|
||||||
#ifdef CONFIG_GLUSTERFS_DISCARD
|
|
||||||
.bdrv_co_discard = qemu_gluster_co_discard,
|
|
||||||
#endif
|
|
||||||
#ifdef CONFIG_GLUSTERFS_ZEROFILL
|
|
||||||
.bdrv_co_write_zeroes = qemu_gluster_co_write_zeroes,
|
|
||||||
#endif
|
|
||||||
.create_opts = &qemu_gluster_create_opts,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void bdrv_gluster_init(void)
|
|
||||||
{
|
|
||||||
bdrv_register(&bdrv_gluster_rdma);
|
|
||||||
bdrv_register(&bdrv_gluster_unix);
|
|
||||||
bdrv_register(&bdrv_gluster_tcp);
|
|
||||||
bdrv_register(&bdrv_gluster);
|
|
||||||
}
|
|
||||||
|
|
||||||
block_init(bdrv_gluster_init);
|
|
||||||
1713
block/iscsi.c
1713
block/iscsi.c
File diff suppressed because it is too large
Load Diff
@@ -1,337 +0,0 @@
|
|||||||
/*
|
|
||||||
* Linux native AIO support.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2009 IBM, Corp.
|
|
||||||
* Copyright (C) 2009 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* 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-common.h"
|
|
||||||
#include "block/aio.h"
|
|
||||||
#include "qemu/queue.h"
|
|
||||||
#include "block/raw-aio.h"
|
|
||||||
#include "qemu/event_notifier.h"
|
|
||||||
|
|
||||||
#include <libaio.h>
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Queue size (per-device).
|
|
||||||
*
|
|
||||||
* XXX: eventually we need to communicate this to the guest and/or make it
|
|
||||||
* tunable by the guest. If we get more outstanding requests at a time
|
|
||||||
* than this we will get EAGAIN from io_submit which is communicated to
|
|
||||||
* the guest as an I/O error.
|
|
||||||
*/
|
|
||||||
#define MAX_EVENTS 128
|
|
||||||
|
|
||||||
#define MAX_QUEUED_IO 128
|
|
||||||
|
|
||||||
struct qemu_laiocb {
|
|
||||||
BlockAIOCB common;
|
|
||||||
struct qemu_laio_state *ctx;
|
|
||||||
struct iocb iocb;
|
|
||||||
ssize_t ret;
|
|
||||||
size_t nbytes;
|
|
||||||
QEMUIOVector *qiov;
|
|
||||||
bool is_read;
|
|
||||||
QSIMPLEQ_ENTRY(qemu_laiocb) next;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int plugged;
|
|
||||||
unsigned int n;
|
|
||||||
bool blocked;
|
|
||||||
QSIMPLEQ_HEAD(, qemu_laiocb) pending;
|
|
||||||
} LaioQueue;
|
|
||||||
|
|
||||||
struct qemu_laio_state {
|
|
||||||
io_context_t ctx;
|
|
||||||
EventNotifier e;
|
|
||||||
|
|
||||||
/* io queue for submit at batch */
|
|
||||||
LaioQueue io_q;
|
|
||||||
|
|
||||||
/* I/O completion processing */
|
|
||||||
QEMUBH *completion_bh;
|
|
||||||
struct io_event events[MAX_EVENTS];
|
|
||||||
int event_idx;
|
|
||||||
int event_max;
|
|
||||||
};
|
|
||||||
|
|
||||||
static void ioq_submit(struct qemu_laio_state *s);
|
|
||||||
|
|
||||||
static inline ssize_t io_event_ret(struct io_event *ev)
|
|
||||||
{
|
|
||||||
return (ssize_t)(((uint64_t)ev->res2 << 32) | ev->res);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Completes an AIO request (calls the callback and frees the ACB).
|
|
||||||
*/
|
|
||||||
static void qemu_laio_process_completion(struct qemu_laio_state *s,
|
|
||||||
struct qemu_laiocb *laiocb)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = laiocb->ret;
|
|
||||||
if (ret != -ECANCELED) {
|
|
||||||
if (ret == laiocb->nbytes) {
|
|
||||||
ret = 0;
|
|
||||||
} else if (ret >= 0) {
|
|
||||||
/* Short reads mean EOF, pad with zeros. */
|
|
||||||
if (laiocb->is_read) {
|
|
||||||
qemu_iovec_memset(laiocb->qiov, ret, 0,
|
|
||||||
laiocb->qiov->size - ret);
|
|
||||||
} else {
|
|
||||||
ret = -EINVAL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
laiocb->common.cb(laiocb->common.opaque, ret);
|
|
||||||
|
|
||||||
qemu_aio_unref(laiocb);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The completion BH fetches completed I/O requests and invokes their
|
|
||||||
* callbacks.
|
|
||||||
*
|
|
||||||
* The function is somewhat tricky because it supports nested event loops, for
|
|
||||||
* example when a request callback invokes aio_poll(). In order to do this,
|
|
||||||
* the completion events array and index are kept in qemu_laio_state. The BH
|
|
||||||
* reschedules itself as long as there are completions pending so it will
|
|
||||||
* either be called again in a nested event loop or will be called after all
|
|
||||||
* events have been completed. When there are no events left to complete, the
|
|
||||||
* BH returns without rescheduling.
|
|
||||||
*/
|
|
||||||
static void qemu_laio_completion_bh(void *opaque)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = opaque;
|
|
||||||
|
|
||||||
/* Fetch more completion events when empty */
|
|
||||||
if (s->event_idx == s->event_max) {
|
|
||||||
do {
|
|
||||||
struct timespec ts = { 0 };
|
|
||||||
s->event_max = io_getevents(s->ctx, MAX_EVENTS, MAX_EVENTS,
|
|
||||||
s->events, &ts);
|
|
||||||
} while (s->event_max == -EINTR);
|
|
||||||
|
|
||||||
s->event_idx = 0;
|
|
||||||
if (s->event_max <= 0) {
|
|
||||||
s->event_max = 0;
|
|
||||||
return; /* no more events */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Reschedule so nested event loops see currently pending completions */
|
|
||||||
qemu_bh_schedule(s->completion_bh);
|
|
||||||
|
|
||||||
/* Process completion events */
|
|
||||||
while (s->event_idx < s->event_max) {
|
|
||||||
struct iocb *iocb = s->events[s->event_idx].obj;
|
|
||||||
struct qemu_laiocb *laiocb =
|
|
||||||
container_of(iocb, struct qemu_laiocb, iocb);
|
|
||||||
|
|
||||||
laiocb->ret = io_event_ret(&s->events[s->event_idx]);
|
|
||||||
s->event_idx++;
|
|
||||||
|
|
||||||
qemu_laio_process_completion(s, laiocb);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!s->io_q.plugged && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
|
|
||||||
ioq_submit(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void qemu_laio_completion_cb(EventNotifier *e)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = container_of(e, struct qemu_laio_state, e);
|
|
||||||
|
|
||||||
if (event_notifier_test_and_clear(&s->e)) {
|
|
||||||
qemu_bh_schedule(s->completion_bh);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void laio_cancel(BlockAIOCB *blockacb)
|
|
||||||
{
|
|
||||||
struct qemu_laiocb *laiocb = (struct qemu_laiocb *)blockacb;
|
|
||||||
struct io_event event;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (laiocb->ret != -EINPROGRESS) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ret = io_cancel(laiocb->ctx->ctx, &laiocb->iocb, &event);
|
|
||||||
laiocb->ret = -ECANCELED;
|
|
||||||
if (ret != 0) {
|
|
||||||
/* iocb is not cancelled, cb will be called by the event loop later */
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
laiocb->common.cb(laiocb->common.opaque, laiocb->ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const AIOCBInfo laio_aiocb_info = {
|
|
||||||
.aiocb_size = sizeof(struct qemu_laiocb),
|
|
||||||
.cancel_async = laio_cancel,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void ioq_init(LaioQueue *io_q)
|
|
||||||
{
|
|
||||||
QSIMPLEQ_INIT(&io_q->pending);
|
|
||||||
io_q->plugged = 0;
|
|
||||||
io_q->n = 0;
|
|
||||||
io_q->blocked = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void ioq_submit(struct qemu_laio_state *s)
|
|
||||||
{
|
|
||||||
int ret, len;
|
|
||||||
struct qemu_laiocb *aiocb;
|
|
||||||
struct iocb *iocbs[MAX_QUEUED_IO];
|
|
||||||
QSIMPLEQ_HEAD(, qemu_laiocb) completed;
|
|
||||||
|
|
||||||
do {
|
|
||||||
len = 0;
|
|
||||||
QSIMPLEQ_FOREACH(aiocb, &s->io_q.pending, next) {
|
|
||||||
iocbs[len++] = &aiocb->iocb;
|
|
||||||
if (len == MAX_QUEUED_IO) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = io_submit(s->ctx, len, iocbs);
|
|
||||||
if (ret == -EAGAIN) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
|
|
||||||
s->io_q.n -= ret;
|
|
||||||
aiocb = container_of(iocbs[ret - 1], struct qemu_laiocb, iocb);
|
|
||||||
QSIMPLEQ_SPLIT_AFTER(&s->io_q.pending, aiocb, next, &completed);
|
|
||||||
} while (ret == len && !QSIMPLEQ_EMPTY(&s->io_q.pending));
|
|
||||||
s->io_q.blocked = (s->io_q.n > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_io_plug(BlockDriverState *bs, void *aio_ctx)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = aio_ctx;
|
|
||||||
|
|
||||||
s->io_q.plugged++;
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_io_unplug(BlockDriverState *bs, void *aio_ctx, bool unplug)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = aio_ctx;
|
|
||||||
|
|
||||||
assert(s->io_q.plugged > 0 || !unplug);
|
|
||||||
|
|
||||||
if (unplug && --s->io_q.plugged > 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!s->io_q.blocked && !QSIMPLEQ_EMPTY(&s->io_q.pending)) {
|
|
||||||
ioq_submit(s);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BlockAIOCB *laio_submit(BlockDriverState *bs, void *aio_ctx, int fd,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb, void *opaque, int type)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = aio_ctx;
|
|
||||||
struct qemu_laiocb *laiocb;
|
|
||||||
struct iocb *iocbs;
|
|
||||||
off_t offset = sector_num * 512;
|
|
||||||
|
|
||||||
laiocb = qemu_aio_get(&laio_aiocb_info, bs, cb, opaque);
|
|
||||||
laiocb->nbytes = nb_sectors * 512;
|
|
||||||
laiocb->ctx = s;
|
|
||||||
laiocb->ret = -EINPROGRESS;
|
|
||||||
laiocb->is_read = (type == QEMU_AIO_READ);
|
|
||||||
laiocb->qiov = qiov;
|
|
||||||
|
|
||||||
iocbs = &laiocb->iocb;
|
|
||||||
|
|
||||||
switch (type) {
|
|
||||||
case QEMU_AIO_WRITE:
|
|
||||||
io_prep_pwritev(iocbs, fd, qiov->iov, qiov->niov, offset);
|
|
||||||
break;
|
|
||||||
case QEMU_AIO_READ:
|
|
||||||
io_prep_preadv(iocbs, fd, qiov->iov, qiov->niov, offset);
|
|
||||||
break;
|
|
||||||
/* Currently Linux kernel does not support other operations */
|
|
||||||
default:
|
|
||||||
fprintf(stderr, "%s: invalid AIO request type 0x%x.\n",
|
|
||||||
__func__, type);
|
|
||||||
goto out_free_aiocb;
|
|
||||||
}
|
|
||||||
io_set_eventfd(&laiocb->iocb, event_notifier_get_fd(&s->e));
|
|
||||||
|
|
||||||
QSIMPLEQ_INSERT_TAIL(&s->io_q.pending, laiocb, next);
|
|
||||||
s->io_q.n++;
|
|
||||||
if (!s->io_q.blocked &&
|
|
||||||
(!s->io_q.plugged || s->io_q.n >= MAX_QUEUED_IO)) {
|
|
||||||
ioq_submit(s);
|
|
||||||
}
|
|
||||||
return &laiocb->common;
|
|
||||||
|
|
||||||
out_free_aiocb:
|
|
||||||
qemu_aio_unref(laiocb);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_detach_aio_context(void *s_, AioContext *old_context)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = s_;
|
|
||||||
|
|
||||||
aio_set_event_notifier(old_context, &s->e, NULL);
|
|
||||||
qemu_bh_delete(s->completion_bh);
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_attach_aio_context(void *s_, AioContext *new_context)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = s_;
|
|
||||||
|
|
||||||
s->completion_bh = aio_bh_new(new_context, qemu_laio_completion_bh, s);
|
|
||||||
aio_set_event_notifier(new_context, &s->e, qemu_laio_completion_cb);
|
|
||||||
}
|
|
||||||
|
|
||||||
void *laio_init(void)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s;
|
|
||||||
|
|
||||||
s = g_malloc0(sizeof(*s));
|
|
||||||
if (event_notifier_init(&s->e, false) < 0) {
|
|
||||||
goto out_free_state;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (io_setup(MAX_EVENTS, &s->ctx) != 0) {
|
|
||||||
goto out_close_efd;
|
|
||||||
}
|
|
||||||
|
|
||||||
ioq_init(&s->io_q);
|
|
||||||
|
|
||||||
return s;
|
|
||||||
|
|
||||||
out_close_efd:
|
|
||||||
event_notifier_cleanup(&s->e);
|
|
||||||
out_free_state:
|
|
||||||
g_free(s);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void laio_cleanup(void *s_)
|
|
||||||
{
|
|
||||||
struct qemu_laio_state *s = s_;
|
|
||||||
|
|
||||||
event_notifier_cleanup(&s->e);
|
|
||||||
|
|
||||||
if (io_destroy(s->ctx) != 0) {
|
|
||||||
fprintf(stderr, "%s: destroy AIO context %p failed\n",
|
|
||||||
__func__, &s->ctx);
|
|
||||||
}
|
|
||||||
g_free(s);
|
|
||||||
}
|
|
||||||
795
block/mirror.c
795
block/mirror.c
@@ -1,795 +0,0 @@
|
|||||||
/*
|
|
||||||
* Image mirroring
|
|
||||||
*
|
|
||||||
* Copyright Red Hat, Inc. 2012
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Paolo Bonzini <pbonzini@redhat.com>
|
|
||||||
*
|
|
||||||
* This work is licensed under the terms of the GNU LGPL, version 2 or later.
|
|
||||||
* See the COPYING.LIB file in the top-level directory.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "trace.h"
|
|
||||||
#include "block/blockjob.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "qemu/ratelimit.h"
|
|
||||||
#include "qemu/bitmap.h"
|
|
||||||
|
|
||||||
#define SLICE_TIME 100000000ULL /* ns */
|
|
||||||
#define MAX_IN_FLIGHT 16
|
|
||||||
|
|
||||||
/* The mirroring buffer is a list of granularity-sized chunks.
|
|
||||||
* Free chunks are organized in a list.
|
|
||||||
*/
|
|
||||||
typedef struct MirrorBuffer {
|
|
||||||
QSIMPLEQ_ENTRY(MirrorBuffer) next;
|
|
||||||
} MirrorBuffer;
|
|
||||||
|
|
||||||
typedef struct MirrorBlockJob {
|
|
||||||
BlockJob common;
|
|
||||||
RateLimit limit;
|
|
||||||
BlockDriverState *target;
|
|
||||||
BlockDriverState *base;
|
|
||||||
/* The name of the graph node to replace */
|
|
||||||
char *replaces;
|
|
||||||
/* The BDS to replace */
|
|
||||||
BlockDriverState *to_replace;
|
|
||||||
/* Used to block operations on the drive-mirror-replace target */
|
|
||||||
Error *replace_blocker;
|
|
||||||
bool is_none_mode;
|
|
||||||
BlockdevOnError on_source_error, on_target_error;
|
|
||||||
bool synced;
|
|
||||||
bool should_complete;
|
|
||||||
int64_t sector_num;
|
|
||||||
int64_t granularity;
|
|
||||||
size_t buf_size;
|
|
||||||
int64_t bdev_length;
|
|
||||||
unsigned long *cow_bitmap;
|
|
||||||
BdrvDirtyBitmap *dirty_bitmap;
|
|
||||||
HBitmapIter hbi;
|
|
||||||
uint8_t *buf;
|
|
||||||
QSIMPLEQ_HEAD(, MirrorBuffer) buf_free;
|
|
||||||
int buf_free_count;
|
|
||||||
|
|
||||||
unsigned long *in_flight_bitmap;
|
|
||||||
int in_flight;
|
|
||||||
int sectors_in_flight;
|
|
||||||
int ret;
|
|
||||||
} MirrorBlockJob;
|
|
||||||
|
|
||||||
typedef struct MirrorOp {
|
|
||||||
MirrorBlockJob *s;
|
|
||||||
QEMUIOVector qiov;
|
|
||||||
int64_t sector_num;
|
|
||||||
int nb_sectors;
|
|
||||||
} MirrorOp;
|
|
||||||
|
|
||||||
static BlockErrorAction mirror_error_action(MirrorBlockJob *s, bool read,
|
|
||||||
int error)
|
|
||||||
{
|
|
||||||
s->synced = false;
|
|
||||||
if (read) {
|
|
||||||
return block_job_error_action(&s->common, s->common.bs,
|
|
||||||
s->on_source_error, true, error);
|
|
||||||
} else {
|
|
||||||
return block_job_error_action(&s->common, s->target,
|
|
||||||
s->on_target_error, false, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_iteration_done(MirrorOp *op, int ret)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = op->s;
|
|
||||||
struct iovec *iov;
|
|
||||||
int64_t chunk_num;
|
|
||||||
int i, nb_chunks, sectors_per_chunk;
|
|
||||||
|
|
||||||
trace_mirror_iteration_done(s, op->sector_num, op->nb_sectors, ret);
|
|
||||||
|
|
||||||
s->in_flight--;
|
|
||||||
s->sectors_in_flight -= op->nb_sectors;
|
|
||||||
iov = op->qiov.iov;
|
|
||||||
for (i = 0; i < op->qiov.niov; i++) {
|
|
||||||
MirrorBuffer *buf = (MirrorBuffer *) iov[i].iov_base;
|
|
||||||
QSIMPLEQ_INSERT_TAIL(&s->buf_free, buf, next);
|
|
||||||
s->buf_free_count++;
|
|
||||||
}
|
|
||||||
|
|
||||||
sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
|
|
||||||
chunk_num = op->sector_num / sectors_per_chunk;
|
|
||||||
nb_chunks = op->nb_sectors / sectors_per_chunk;
|
|
||||||
bitmap_clear(s->in_flight_bitmap, chunk_num, nb_chunks);
|
|
||||||
if (ret >= 0) {
|
|
||||||
if (s->cow_bitmap) {
|
|
||||||
bitmap_set(s->cow_bitmap, chunk_num, nb_chunks);
|
|
||||||
}
|
|
||||||
s->common.offset += (uint64_t)op->nb_sectors * BDRV_SECTOR_SIZE;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_iovec_destroy(&op->qiov);
|
|
||||||
g_slice_free(MirrorOp, op);
|
|
||||||
|
|
||||||
/* Enter coroutine when it is not sleeping. The coroutine sleeps to
|
|
||||||
* rate-limit itself. The coroutine will eventually resume since there is
|
|
||||||
* a sleep timeout so don't wake it early.
|
|
||||||
*/
|
|
||||||
if (s->common.busy) {
|
|
||||||
qemu_coroutine_enter(s->common.co, NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_write_complete(void *opaque, int ret)
|
|
||||||
{
|
|
||||||
MirrorOp *op = opaque;
|
|
||||||
MirrorBlockJob *s = op->s;
|
|
||||||
if (ret < 0) {
|
|
||||||
BlockDriverState *source = s->common.bs;
|
|
||||||
BlockErrorAction action;
|
|
||||||
|
|
||||||
bdrv_set_dirty_bitmap(source, s->dirty_bitmap, op->sector_num,
|
|
||||||
op->nb_sectors);
|
|
||||||
action = mirror_error_action(s, false, -ret);
|
|
||||||
if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
|
|
||||||
s->ret = ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mirror_iteration_done(op, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_read_complete(void *opaque, int ret)
|
|
||||||
{
|
|
||||||
MirrorOp *op = opaque;
|
|
||||||
MirrorBlockJob *s = op->s;
|
|
||||||
if (ret < 0) {
|
|
||||||
BlockDriverState *source = s->common.bs;
|
|
||||||
BlockErrorAction action;
|
|
||||||
|
|
||||||
bdrv_set_dirty_bitmap(source, s->dirty_bitmap, op->sector_num,
|
|
||||||
op->nb_sectors);
|
|
||||||
action = mirror_error_action(s, true, -ret);
|
|
||||||
if (action == BLOCK_ERROR_ACTION_REPORT && s->ret >= 0) {
|
|
||||||
s->ret = ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
mirror_iteration_done(op, ret);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
bdrv_aio_writev(s->target, op->sector_num, &op->qiov, op->nb_sectors,
|
|
||||||
mirror_write_complete, op);
|
|
||||||
}
|
|
||||||
|
|
||||||
static uint64_t coroutine_fn mirror_iteration(MirrorBlockJob *s)
|
|
||||||
{
|
|
||||||
BlockDriverState *source = s->common.bs;
|
|
||||||
int nb_sectors, sectors_per_chunk, nb_chunks;
|
|
||||||
int64_t end, sector_num, next_chunk, next_sector, hbitmap_next_sector;
|
|
||||||
uint64_t delay_ns = 0;
|
|
||||||
MirrorOp *op;
|
|
||||||
|
|
||||||
s->sector_num = hbitmap_iter_next(&s->hbi);
|
|
||||||
if (s->sector_num < 0) {
|
|
||||||
bdrv_dirty_iter_init(source, s->dirty_bitmap, &s->hbi);
|
|
||||||
s->sector_num = hbitmap_iter_next(&s->hbi);
|
|
||||||
trace_mirror_restart_iter(s,
|
|
||||||
bdrv_get_dirty_count(source, s->dirty_bitmap));
|
|
||||||
assert(s->sector_num >= 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
hbitmap_next_sector = s->sector_num;
|
|
||||||
sector_num = s->sector_num;
|
|
||||||
sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
|
|
||||||
end = s->bdev_length / BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
/* Extend the QEMUIOVector to include all adjacent blocks that will
|
|
||||||
* be copied in this operation.
|
|
||||||
*
|
|
||||||
* We have to do this if we have no backing file yet in the destination,
|
|
||||||
* and the cluster size is very large. Then we need to do COW ourselves.
|
|
||||||
* The first time a cluster is copied, copy it entirely. Note that,
|
|
||||||
* because both the granularity and the cluster size are powers of two,
|
|
||||||
* the number of sectors to copy cannot exceed one cluster.
|
|
||||||
*
|
|
||||||
* We also want to extend the QEMUIOVector to include more adjacent
|
|
||||||
* dirty blocks if possible, to limit the number of I/O operations and
|
|
||||||
* run efficiently even with a small granularity.
|
|
||||||
*/
|
|
||||||
nb_chunks = 0;
|
|
||||||
nb_sectors = 0;
|
|
||||||
next_sector = sector_num;
|
|
||||||
next_chunk = sector_num / sectors_per_chunk;
|
|
||||||
|
|
||||||
/* Wait for I/O to this cluster (from a previous iteration) to be done. */
|
|
||||||
while (test_bit(next_chunk, s->in_flight_bitmap)) {
|
|
||||||
trace_mirror_yield_in_flight(s, sector_num, s->in_flight);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
do {
|
|
||||||
int added_sectors, added_chunks;
|
|
||||||
|
|
||||||
if (!bdrv_get_dirty(source, s->dirty_bitmap, next_sector) ||
|
|
||||||
test_bit(next_chunk, s->in_flight_bitmap)) {
|
|
||||||
assert(nb_sectors > 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
added_sectors = sectors_per_chunk;
|
|
||||||
if (s->cow_bitmap && !test_bit(next_chunk, s->cow_bitmap)) {
|
|
||||||
bdrv_round_to_clusters(s->target,
|
|
||||||
next_sector, added_sectors,
|
|
||||||
&next_sector, &added_sectors);
|
|
||||||
|
|
||||||
/* On the first iteration, the rounding may make us copy
|
|
||||||
* sectors before the first dirty one.
|
|
||||||
*/
|
|
||||||
if (next_sector < sector_num) {
|
|
||||||
assert(nb_sectors == 0);
|
|
||||||
sector_num = next_sector;
|
|
||||||
next_chunk = next_sector / sectors_per_chunk;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
added_sectors = MIN(added_sectors, end - (sector_num + nb_sectors));
|
|
||||||
added_chunks = (added_sectors + sectors_per_chunk - 1) / sectors_per_chunk;
|
|
||||||
|
|
||||||
/* When doing COW, it may happen that there is not enough space for
|
|
||||||
* a full cluster. Wait if that is the case.
|
|
||||||
*/
|
|
||||||
while (nb_chunks == 0 && s->buf_free_count < added_chunks) {
|
|
||||||
trace_mirror_yield_buf_busy(s, nb_chunks, s->in_flight);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
if (s->buf_free_count < nb_chunks + added_chunks) {
|
|
||||||
trace_mirror_break_buf_busy(s, nb_chunks, s->in_flight);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* We have enough free space to copy these sectors. */
|
|
||||||
bitmap_set(s->in_flight_bitmap, next_chunk, added_chunks);
|
|
||||||
|
|
||||||
nb_sectors += added_sectors;
|
|
||||||
nb_chunks += added_chunks;
|
|
||||||
next_sector += added_sectors;
|
|
||||||
next_chunk += added_chunks;
|
|
||||||
if (!s->synced && s->common.speed) {
|
|
||||||
delay_ns = ratelimit_calculate_delay(&s->limit, added_sectors);
|
|
||||||
}
|
|
||||||
} while (delay_ns == 0 && next_sector < end);
|
|
||||||
|
|
||||||
/* Allocate a MirrorOp that is used as an AIO callback. */
|
|
||||||
op = g_slice_new(MirrorOp);
|
|
||||||
op->s = s;
|
|
||||||
op->sector_num = sector_num;
|
|
||||||
op->nb_sectors = nb_sectors;
|
|
||||||
|
|
||||||
/* Now make a QEMUIOVector taking enough granularity-sized chunks
|
|
||||||
* from s->buf_free.
|
|
||||||
*/
|
|
||||||
qemu_iovec_init(&op->qiov, nb_chunks);
|
|
||||||
next_sector = sector_num;
|
|
||||||
while (nb_chunks-- > 0) {
|
|
||||||
MirrorBuffer *buf = QSIMPLEQ_FIRST(&s->buf_free);
|
|
||||||
size_t remaining = (nb_sectors * BDRV_SECTOR_SIZE) - op->qiov.size;
|
|
||||||
|
|
||||||
QSIMPLEQ_REMOVE_HEAD(&s->buf_free, next);
|
|
||||||
s->buf_free_count--;
|
|
||||||
qemu_iovec_add(&op->qiov, buf, MIN(s->granularity, remaining));
|
|
||||||
|
|
||||||
/* Advance the HBitmapIter in parallel, so that we do not examine
|
|
||||||
* the same sector twice.
|
|
||||||
*/
|
|
||||||
if (next_sector > hbitmap_next_sector
|
|
||||||
&& bdrv_get_dirty(source, s->dirty_bitmap, next_sector)) {
|
|
||||||
hbitmap_next_sector = hbitmap_iter_next(&s->hbi);
|
|
||||||
}
|
|
||||||
|
|
||||||
next_sector += sectors_per_chunk;
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_reset_dirty_bitmap(source, s->dirty_bitmap, sector_num,
|
|
||||||
nb_sectors);
|
|
||||||
|
|
||||||
/* Copy the dirty cluster. */
|
|
||||||
s->in_flight++;
|
|
||||||
s->sectors_in_flight += nb_sectors;
|
|
||||||
trace_mirror_one_iteration(s, sector_num, nb_sectors);
|
|
||||||
bdrv_aio_readv(source, sector_num, &op->qiov, nb_sectors,
|
|
||||||
mirror_read_complete, op);
|
|
||||||
return delay_ns;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_free_init(MirrorBlockJob *s)
|
|
||||||
{
|
|
||||||
int granularity = s->granularity;
|
|
||||||
size_t buf_size = s->buf_size;
|
|
||||||
uint8_t *buf = s->buf;
|
|
||||||
|
|
||||||
assert(s->buf_free_count == 0);
|
|
||||||
QSIMPLEQ_INIT(&s->buf_free);
|
|
||||||
while (buf_size != 0) {
|
|
||||||
MirrorBuffer *cur = (MirrorBuffer *)buf;
|
|
||||||
QSIMPLEQ_INSERT_TAIL(&s->buf_free, cur, next);
|
|
||||||
s->buf_free_count++;
|
|
||||||
buf_size -= granularity;
|
|
||||||
buf += granularity;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_drain(MirrorBlockJob *s)
|
|
||||||
{
|
|
||||||
while (s->in_flight > 0) {
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int ret;
|
|
||||||
} MirrorExitData;
|
|
||||||
|
|
||||||
static void mirror_exit(BlockJob *job, void *opaque)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
||||||
MirrorExitData *data = opaque;
|
|
||||||
AioContext *replace_aio_context = NULL;
|
|
||||||
|
|
||||||
if (s->to_replace) {
|
|
||||||
replace_aio_context = bdrv_get_aio_context(s->to_replace);
|
|
||||||
aio_context_acquire(replace_aio_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s->should_complete && data->ret == 0) {
|
|
||||||
BlockDriverState *to_replace = s->common.bs;
|
|
||||||
if (s->to_replace) {
|
|
||||||
to_replace = s->to_replace;
|
|
||||||
}
|
|
||||||
if (bdrv_get_flags(s->target) != bdrv_get_flags(to_replace)) {
|
|
||||||
bdrv_reopen(s->target, bdrv_get_flags(to_replace), NULL);
|
|
||||||
}
|
|
||||||
bdrv_swap(s->target, to_replace);
|
|
||||||
if (s->common.driver->job_type == BLOCK_JOB_TYPE_COMMIT) {
|
|
||||||
/* drop the bs loop chain formed by the swap: break the loop then
|
|
||||||
* trigger the unref from the top one */
|
|
||||||
BlockDriverState *p = s->base->backing_hd;
|
|
||||||
bdrv_set_backing_hd(s->base, NULL);
|
|
||||||
bdrv_unref(p);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (s->to_replace) {
|
|
||||||
bdrv_op_unblock_all(s->to_replace, s->replace_blocker);
|
|
||||||
error_free(s->replace_blocker);
|
|
||||||
bdrv_unref(s->to_replace);
|
|
||||||
}
|
|
||||||
if (replace_aio_context) {
|
|
||||||
aio_context_release(replace_aio_context);
|
|
||||||
}
|
|
||||||
g_free(s->replaces);
|
|
||||||
bdrv_unref(s->target);
|
|
||||||
block_job_completed(&s->common, data->ret);
|
|
||||||
g_free(data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void coroutine_fn mirror_run(void *opaque)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = opaque;
|
|
||||||
MirrorExitData *data;
|
|
||||||
BlockDriverState *bs = s->common.bs;
|
|
||||||
int64_t sector_num, end, sectors_per_chunk, length;
|
|
||||||
uint64_t last_pause_ns;
|
|
||||||
BlockDriverInfo bdi;
|
|
||||||
char backing_filename[2]; /* we only need 2 characters because we are only
|
|
||||||
checking for a NULL string */
|
|
||||||
int ret = 0;
|
|
||||||
int n;
|
|
||||||
|
|
||||||
if (block_job_is_cancelled(&s->common)) {
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->bdev_length = bdrv_getlength(bs);
|
|
||||||
if (s->bdev_length < 0) {
|
|
||||||
ret = s->bdev_length;
|
|
||||||
goto immediate_exit;
|
|
||||||
} else if (s->bdev_length == 0) {
|
|
||||||
/* Report BLOCK_JOB_READY and wait for complete. */
|
|
||||||
block_job_event_ready(&s->common);
|
|
||||||
s->synced = true;
|
|
||||||
while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
|
|
||||||
block_job_yield(&s->common);
|
|
||||||
}
|
|
||||||
s->common.cancelled = false;
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
length = DIV_ROUND_UP(s->bdev_length, s->granularity);
|
|
||||||
s->in_flight_bitmap = bitmap_new(length);
|
|
||||||
|
|
||||||
/* If we have no backing file yet in the destination, we cannot let
|
|
||||||
* the destination do COW. Instead, we copy sectors around the
|
|
||||||
* dirty data if needed. We need a bitmap to do that.
|
|
||||||
*/
|
|
||||||
bdrv_get_backing_filename(s->target, backing_filename,
|
|
||||||
sizeof(backing_filename));
|
|
||||||
if (backing_filename[0] && !s->target->backing_hd) {
|
|
||||||
ret = bdrv_get_info(s->target, &bdi);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
if (s->granularity < bdi.cluster_size) {
|
|
||||||
s->buf_size = MAX(s->buf_size, bdi.cluster_size);
|
|
||||||
s->cow_bitmap = bitmap_new(length);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
end = s->bdev_length / BDRV_SECTOR_SIZE;
|
|
||||||
s->buf = qemu_try_blockalign(bs, s->buf_size);
|
|
||||||
if (s->buf == NULL) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
sectors_per_chunk = s->granularity >> BDRV_SECTOR_BITS;
|
|
||||||
mirror_free_init(s);
|
|
||||||
|
|
||||||
if (!s->is_none_mode) {
|
|
||||||
/* First part, loop on the sectors and initialize the dirty bitmap. */
|
|
||||||
BlockDriverState *base = s->base;
|
|
||||||
for (sector_num = 0; sector_num < end; ) {
|
|
||||||
int64_t next = (sector_num | (sectors_per_chunk - 1)) + 1;
|
|
||||||
ret = bdrv_is_allocated_above(bs, base,
|
|
||||||
sector_num, next - sector_num, &n);
|
|
||||||
|
|
||||||
if (ret < 0) {
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(n > 0);
|
|
||||||
if (ret == 1) {
|
|
||||||
bdrv_set_dirty_bitmap(bs, s->dirty_bitmap, sector_num, n);
|
|
||||||
sector_num = next;
|
|
||||||
} else {
|
|
||||||
sector_num += n;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_dirty_iter_init(bs, s->dirty_bitmap, &s->hbi);
|
|
||||||
last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
||||||
for (;;) {
|
|
||||||
uint64_t delay_ns = 0;
|
|
||||||
int64_t cnt;
|
|
||||||
bool should_complete;
|
|
||||||
|
|
||||||
if (s->ret < 0) {
|
|
||||||
ret = s->ret;
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
|
|
||||||
/* s->common.offset contains the number of bytes already processed so
|
|
||||||
* far, cnt is the number of dirty sectors remaining and
|
|
||||||
* s->sectors_in_flight is the number of sectors currently being
|
|
||||||
* processed; together those are the current total operation length */
|
|
||||||
s->common.len = s->common.offset +
|
|
||||||
(cnt + s->sectors_in_flight) * BDRV_SECTOR_SIZE;
|
|
||||||
|
|
||||||
/* Note that even when no rate limit is applied we need to yield
|
|
||||||
* periodically with no pending I/O so that qemu_aio_flush() returns.
|
|
||||||
* We do so every SLICE_TIME nanoseconds, or when there is an error,
|
|
||||||
* or when the source is clean, whichever comes first.
|
|
||||||
*/
|
|
||||||
if (qemu_clock_get_ns(QEMU_CLOCK_REALTIME) - last_pause_ns < SLICE_TIME &&
|
|
||||||
s->common.iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
|
|
||||||
if (s->in_flight == MAX_IN_FLIGHT || s->buf_free_count == 0 ||
|
|
||||||
(cnt == 0 && s->in_flight > 0)) {
|
|
||||||
trace_mirror_yield(s, s->in_flight, s->buf_free_count, cnt);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
continue;
|
|
||||||
} else if (cnt != 0) {
|
|
||||||
delay_ns = mirror_iteration(s);
|
|
||||||
if (delay_ns == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
should_complete = false;
|
|
||||||
if (s->in_flight == 0 && cnt == 0) {
|
|
||||||
trace_mirror_before_flush(s);
|
|
||||||
ret = bdrv_flush(s->target);
|
|
||||||
if (ret < 0) {
|
|
||||||
if (mirror_error_action(s, false, -ret) ==
|
|
||||||
BLOCK_ERROR_ACTION_REPORT) {
|
|
||||||
goto immediate_exit;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
/* We're out of the streaming phase. From now on, if the job
|
|
||||||
* is cancelled we will actually complete all pending I/O and
|
|
||||||
* report completion. This way, block-job-cancel will leave
|
|
||||||
* the target in a consistent state.
|
|
||||||
*/
|
|
||||||
if (!s->synced) {
|
|
||||||
block_job_event_ready(&s->common);
|
|
||||||
s->synced = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
should_complete = s->should_complete ||
|
|
||||||
block_job_is_cancelled(&s->common);
|
|
||||||
cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cnt == 0 && should_complete) {
|
|
||||||
/* The dirty bitmap is not updated while operations are pending.
|
|
||||||
* If we're about to exit, wait for pending operations before
|
|
||||||
* calling bdrv_get_dirty_count(bs), or we may exit while the
|
|
||||||
* source has dirty data to copy!
|
|
||||||
*
|
|
||||||
* Note that I/O can be submitted by the guest while
|
|
||||||
* mirror_populate runs.
|
|
||||||
*/
|
|
||||||
trace_mirror_before_drain(s, cnt);
|
|
||||||
bdrv_drain(bs);
|
|
||||||
cnt = bdrv_get_dirty_count(bs, s->dirty_bitmap);
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = 0;
|
|
||||||
trace_mirror_before_sleep(s, cnt, s->synced, delay_ns);
|
|
||||||
if (!s->synced) {
|
|
||||||
block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
|
|
||||||
if (block_job_is_cancelled(&s->common)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else if (!should_complete) {
|
|
||||||
delay_ns = (s->in_flight == 0 && cnt == 0 ? SLICE_TIME : 0);
|
|
||||||
block_job_sleep_ns(&s->common, QEMU_CLOCK_REALTIME, delay_ns);
|
|
||||||
} else if (cnt == 0) {
|
|
||||||
/* The two disks are in sync. Exit and report successful
|
|
||||||
* completion.
|
|
||||||
*/
|
|
||||||
assert(QLIST_EMPTY(&bs->tracked_requests));
|
|
||||||
s->common.cancelled = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
last_pause_ns = qemu_clock_get_ns(QEMU_CLOCK_REALTIME);
|
|
||||||
}
|
|
||||||
|
|
||||||
immediate_exit:
|
|
||||||
if (s->in_flight > 0) {
|
|
||||||
/* We get here only if something went wrong. Either the job failed,
|
|
||||||
* or it was cancelled prematurely so that we do not guarantee that
|
|
||||||
* the target is a copy of the source.
|
|
||||||
*/
|
|
||||||
assert(ret < 0 || (!s->synced && block_job_is_cancelled(&s->common)));
|
|
||||||
mirror_drain(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(s->in_flight == 0);
|
|
||||||
qemu_vfree(s->buf);
|
|
||||||
g_free(s->cow_bitmap);
|
|
||||||
g_free(s->in_flight_bitmap);
|
|
||||||
bdrv_release_dirty_bitmap(bs, s->dirty_bitmap);
|
|
||||||
bdrv_iostatus_disable(s->target);
|
|
||||||
|
|
||||||
data = g_malloc(sizeof(*data));
|
|
||||||
data->ret = ret;
|
|
||||||
block_job_defer_to_main_loop(&s->common, mirror_exit, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_set_speed(BlockJob *job, int64_t speed, Error **errp)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
||||||
|
|
||||||
if (speed < 0) {
|
|
||||||
error_set(errp, QERR_INVALID_PARAMETER, "speed");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
ratelimit_set_speed(&s->limit, speed / BDRV_SECTOR_SIZE, SLICE_TIME);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_iostatus_reset(BlockJob *job)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
||||||
|
|
||||||
bdrv_iostatus_reset(s->target);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void mirror_complete(BlockJob *job, Error **errp)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s = container_of(job, MirrorBlockJob, common);
|
|
||||||
Error *local_err = NULL;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
ret = bdrv_open_backing_file(s->target, NULL, &local_err);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!s->synced) {
|
|
||||||
error_set(errp, QERR_BLOCK_JOB_NOT_READY,
|
|
||||||
bdrv_get_device_name(job->bs));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* check the target bs is not blocked and block all operations on it */
|
|
||||||
if (s->replaces) {
|
|
||||||
AioContext *replace_aio_context;
|
|
||||||
|
|
||||||
s->to_replace = check_to_replace_node(s->replaces, &local_err);
|
|
||||||
if (!s->to_replace) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
replace_aio_context = bdrv_get_aio_context(s->to_replace);
|
|
||||||
aio_context_acquire(replace_aio_context);
|
|
||||||
|
|
||||||
error_setg(&s->replace_blocker,
|
|
||||||
"block device is in use by block-job-complete");
|
|
||||||
bdrv_op_block_all(s->to_replace, s->replace_blocker);
|
|
||||||
bdrv_ref(s->to_replace);
|
|
||||||
|
|
||||||
aio_context_release(replace_aio_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
s->should_complete = true;
|
|
||||||
block_job_resume(job);
|
|
||||||
}
|
|
||||||
|
|
||||||
static const BlockJobDriver mirror_job_driver = {
|
|
||||||
.instance_size = sizeof(MirrorBlockJob),
|
|
||||||
.job_type = BLOCK_JOB_TYPE_MIRROR,
|
|
||||||
.set_speed = mirror_set_speed,
|
|
||||||
.iostatus_reset= mirror_iostatus_reset,
|
|
||||||
.complete = mirror_complete,
|
|
||||||
};
|
|
||||||
|
|
||||||
static const BlockJobDriver commit_active_job_driver = {
|
|
||||||
.instance_size = sizeof(MirrorBlockJob),
|
|
||||||
.job_type = BLOCK_JOB_TYPE_COMMIT,
|
|
||||||
.set_speed = mirror_set_speed,
|
|
||||||
.iostatus_reset
|
|
||||||
= mirror_iostatus_reset,
|
|
||||||
.complete = mirror_complete,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void mirror_start_job(BlockDriverState *bs, BlockDriverState *target,
|
|
||||||
const char *replaces,
|
|
||||||
int64_t speed, int64_t granularity,
|
|
||||||
int64_t buf_size,
|
|
||||||
BlockdevOnError on_source_error,
|
|
||||||
BlockdevOnError on_target_error,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque, Error **errp,
|
|
||||||
const BlockJobDriver *driver,
|
|
||||||
bool is_none_mode, BlockDriverState *base)
|
|
||||||
{
|
|
||||||
MirrorBlockJob *s;
|
|
||||||
|
|
||||||
if (granularity == 0) {
|
|
||||||
/* Choose the default granularity based on the target file's cluster
|
|
||||||
* size, clamped between 4k and 64k. */
|
|
||||||
BlockDriverInfo bdi;
|
|
||||||
if (bdrv_get_info(target, &bdi) >= 0 && bdi.cluster_size != 0) {
|
|
||||||
granularity = MAX(4096, bdi.cluster_size);
|
|
||||||
granularity = MIN(65536, granularity);
|
|
||||||
} else {
|
|
||||||
granularity = 65536;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert ((granularity & (granularity - 1)) == 0);
|
|
||||||
|
|
||||||
if ((on_source_error == BLOCKDEV_ON_ERROR_STOP ||
|
|
||||||
on_source_error == BLOCKDEV_ON_ERROR_ENOSPC) &&
|
|
||||||
!bdrv_iostatus_is_enabled(bs)) {
|
|
||||||
error_set(errp, QERR_INVALID_PARAMETER, "on-source-error");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
s = block_job_create(driver, bs, speed, cb, opaque, errp);
|
|
||||||
if (!s) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->replaces = g_strdup(replaces);
|
|
||||||
s->on_source_error = on_source_error;
|
|
||||||
s->on_target_error = on_target_error;
|
|
||||||
s->target = target;
|
|
||||||
s->is_none_mode = is_none_mode;
|
|
||||||
s->base = base;
|
|
||||||
s->granularity = granularity;
|
|
||||||
s->buf_size = MAX(buf_size, granularity);
|
|
||||||
|
|
||||||
s->dirty_bitmap = bdrv_create_dirty_bitmap(bs, granularity, errp);
|
|
||||||
if (!s->dirty_bitmap) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
bdrv_set_enable_write_cache(s->target, true);
|
|
||||||
bdrv_set_on_error(s->target, on_target_error, on_target_error);
|
|
||||||
bdrv_iostatus_enable(s->target);
|
|
||||||
s->common.co = qemu_coroutine_create(mirror_run);
|
|
||||||
trace_mirror_start(bs, s, s->common.co, opaque);
|
|
||||||
qemu_coroutine_enter(s->common.co, s);
|
|
||||||
}
|
|
||||||
|
|
||||||
void mirror_start(BlockDriverState *bs, BlockDriverState *target,
|
|
||||||
const char *replaces,
|
|
||||||
int64_t speed, int64_t granularity, int64_t buf_size,
|
|
||||||
MirrorSyncMode mode, BlockdevOnError on_source_error,
|
|
||||||
BlockdevOnError on_target_error,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque, Error **errp)
|
|
||||||
{
|
|
||||||
bool is_none_mode;
|
|
||||||
BlockDriverState *base;
|
|
||||||
|
|
||||||
is_none_mode = mode == MIRROR_SYNC_MODE_NONE;
|
|
||||||
base = mode == MIRROR_SYNC_MODE_TOP ? bs->backing_hd : NULL;
|
|
||||||
mirror_start_job(bs, target, replaces,
|
|
||||||
speed, granularity, buf_size,
|
|
||||||
on_source_error, on_target_error, cb, opaque, errp,
|
|
||||||
&mirror_job_driver, is_none_mode, base);
|
|
||||||
}
|
|
||||||
|
|
||||||
void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
|
|
||||||
int64_t speed,
|
|
||||||
BlockdevOnError on_error,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque, Error **errp)
|
|
||||||
{
|
|
||||||
int64_t length, base_length;
|
|
||||||
int orig_base_flags;
|
|
||||||
int ret;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
orig_base_flags = bdrv_get_flags(base);
|
|
||||||
|
|
||||||
if (bdrv_reopen(base, bs->open_flags, errp)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
length = bdrv_getlength(bs);
|
|
||||||
if (length < 0) {
|
|
||||||
error_setg_errno(errp, -length,
|
|
||||||
"Unable to determine length of %s", bs->filename);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
base_length = bdrv_getlength(base);
|
|
||||||
if (base_length < 0) {
|
|
||||||
error_setg_errno(errp, -base_length,
|
|
||||||
"Unable to determine length of %s", base->filename);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (length > base_length) {
|
|
||||||
ret = bdrv_truncate(base, length);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg_errno(errp, -ret,
|
|
||||||
"Top image %s is larger than base image %s, and "
|
|
||||||
"resize of base image failed",
|
|
||||||
bs->filename, base->filename);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bdrv_ref(base);
|
|
||||||
mirror_start_job(bs, base, NULL, speed, 0, 0,
|
|
||||||
on_error, on_error, cb, opaque, &local_err,
|
|
||||||
&commit_active_job_driver, false, base);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
goto error_restore_flags;
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
|
||||||
|
|
||||||
error_restore_flags:
|
|
||||||
/* ignore error and errp for bdrv_reopen, because we want to propagate
|
|
||||||
* the original error */
|
|
||||||
bdrv_reopen(base, orig_base_flags, NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
@@ -1,408 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Block driver for NBD
|
|
||||||
*
|
|
||||||
* Copyright (C) 2008 Bull S.A.S.
|
|
||||||
* Author: Laurent Vivier <Laurent.Vivier@bull.net>
|
|
||||||
*
|
|
||||||
* Some parts:
|
|
||||||
* Copyright (C) 2007 Anthony Liguori <anthony@codemonkey.ws>
|
|
||||||
*
|
|
||||||
* 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 "nbd-client.h"
|
|
||||||
#include "qemu/sockets.h"
|
|
||||||
|
|
||||||
#define HANDLE_TO_INDEX(bs, handle) ((handle) ^ ((uint64_t)(intptr_t)bs))
|
|
||||||
#define INDEX_TO_HANDLE(bs, index) ((index) ^ ((uint64_t)(intptr_t)bs))
|
|
||||||
|
|
||||||
static void nbd_recv_coroutines_enter_all(NbdClientSession *s)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
for (i = 0; i < MAX_NBD_REQUESTS; i++) {
|
|
||||||
if (s->recv_coroutine[i]) {
|
|
||||||
qemu_coroutine_enter(s->recv_coroutine[i], NULL);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_teardown_connection(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
|
|
||||||
/* finish any pending coroutines */
|
|
||||||
shutdown(client->sock, 2);
|
|
||||||
nbd_recv_coroutines_enter_all(client);
|
|
||||||
|
|
||||||
nbd_client_detach_aio_context(bs);
|
|
||||||
closesocket(client->sock);
|
|
||||||
client->sock = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_reply_ready(void *opaque)
|
|
||||||
{
|
|
||||||
BlockDriverState *bs = opaque;
|
|
||||||
NbdClientSession *s = nbd_get_client_session(bs);
|
|
||||||
uint64_t i;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
if (s->reply.handle == 0) {
|
|
||||||
/* No reply already in flight. Fetch a header. It is possible
|
|
||||||
* that another thread has done the same thing in parallel, so
|
|
||||||
* the socket is not readable anymore.
|
|
||||||
*/
|
|
||||||
ret = nbd_receive_reply(s->sock, &s->reply);
|
|
||||||
if (ret == -EAGAIN) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ret < 0) {
|
|
||||||
s->reply.handle = 0;
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* There's no need for a mutex on the receive side, because the
|
|
||||||
* handler acts as a synchronization point and ensures that only
|
|
||||||
* one coroutine is called until the reply finishes. */
|
|
||||||
i = HANDLE_TO_INDEX(s, s->reply.handle);
|
|
||||||
if (i >= MAX_NBD_REQUESTS) {
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (s->recv_coroutine[i]) {
|
|
||||||
qemu_coroutine_enter(s->recv_coroutine[i], NULL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
fail:
|
|
||||||
nbd_teardown_connection(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_restart_write(void *opaque)
|
|
||||||
{
|
|
||||||
BlockDriverState *bs = opaque;
|
|
||||||
|
|
||||||
qemu_coroutine_enter(nbd_get_client_session(bs)->send_coroutine, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_send_request(BlockDriverState *bs,
|
|
||||||
struct nbd_request *request,
|
|
||||||
QEMUIOVector *qiov, int offset)
|
|
||||||
{
|
|
||||||
NbdClientSession *s = nbd_get_client_session(bs);
|
|
||||||
AioContext *aio_context;
|
|
||||||
int rc, ret, i;
|
|
||||||
|
|
||||||
qemu_co_mutex_lock(&s->send_mutex);
|
|
||||||
|
|
||||||
for (i = 0; i < MAX_NBD_REQUESTS; i++) {
|
|
||||||
if (s->recv_coroutine[i] == NULL) {
|
|
||||||
s->recv_coroutine[i] = qemu_coroutine_self();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(i < MAX_NBD_REQUESTS);
|
|
||||||
request->handle = INDEX_TO_HANDLE(s, i);
|
|
||||||
s->send_coroutine = qemu_coroutine_self();
|
|
||||||
aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
aio_set_fd_handler(aio_context, s->sock,
|
|
||||||
nbd_reply_ready, nbd_restart_write, bs);
|
|
||||||
if (qiov) {
|
|
||||||
if (!s->is_unix) {
|
|
||||||
socket_set_cork(s->sock, 1);
|
|
||||||
}
|
|
||||||
rc = nbd_send_request(s->sock, request);
|
|
||||||
if (rc >= 0) {
|
|
||||||
ret = qemu_co_sendv(s->sock, qiov->iov, qiov->niov,
|
|
||||||
offset, request->len);
|
|
||||||
if (ret != request->len) {
|
|
||||||
rc = -EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!s->is_unix) {
|
|
||||||
socket_set_cork(s->sock, 0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
rc = nbd_send_request(s->sock, request);
|
|
||||||
}
|
|
||||||
aio_set_fd_handler(aio_context, s->sock, nbd_reply_ready, NULL, bs);
|
|
||||||
s->send_coroutine = NULL;
|
|
||||||
qemu_co_mutex_unlock(&s->send_mutex);
|
|
||||||
return rc;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_co_receive_reply(NbdClientSession *s,
|
|
||||||
struct nbd_request *request, struct nbd_reply *reply,
|
|
||||||
QEMUIOVector *qiov, int offset)
|
|
||||||
{
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* Wait until we're woken up by the read handler. TODO: perhaps
|
|
||||||
* peek at the next reply and avoid yielding if it's ours? */
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
*reply = s->reply;
|
|
||||||
if (reply->handle != request->handle) {
|
|
||||||
reply->error = EIO;
|
|
||||||
} else {
|
|
||||||
if (qiov && reply->error == 0) {
|
|
||||||
ret = qemu_co_recvv(s->sock, qiov->iov, qiov->niov,
|
|
||||||
offset, request->len);
|
|
||||||
if (ret != request->len) {
|
|
||||||
reply->error = EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Tell the read handler to read another header. */
|
|
||||||
s->reply.handle = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_coroutine_start(NbdClientSession *s,
|
|
||||||
struct nbd_request *request)
|
|
||||||
{
|
|
||||||
/* Poor man semaphore. The free_sema is locked when no other request
|
|
||||||
* can be accepted, and unlocked after receiving one reply. */
|
|
||||||
if (s->in_flight >= MAX_NBD_REQUESTS - 1) {
|
|
||||||
qemu_co_mutex_lock(&s->free_sema);
|
|
||||||
assert(s->in_flight < MAX_NBD_REQUESTS);
|
|
||||||
}
|
|
||||||
s->in_flight++;
|
|
||||||
|
|
||||||
/* s->recv_coroutine[i] is set as soon as we get the send_lock. */
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_coroutine_end(NbdClientSession *s,
|
|
||||||
struct nbd_request *request)
|
|
||||||
{
|
|
||||||
int i = HANDLE_TO_INDEX(s, request->handle);
|
|
||||||
s->recv_coroutine[i] = NULL;
|
|
||||||
if (s->in_flight-- == MAX_NBD_REQUESTS) {
|
|
||||||
qemu_co_mutex_unlock(&s->free_sema);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_readv_1(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov,
|
|
||||||
int offset)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_READ };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
request.from = sector_num * 512;
|
|
||||||
request.len = nb_sectors * 512;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, NULL, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, qiov, offset);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_writev_1(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov,
|
|
||||||
int offset)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_WRITE };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
if (!bdrv_enable_write_cache(bs) &&
|
|
||||||
(client->nbdflags & NBD_FLAG_SEND_FUA)) {
|
|
||||||
request.type |= NBD_CMD_FLAG_FUA;
|
|
||||||
}
|
|
||||||
|
|
||||||
request.from = sector_num * 512;
|
|
||||||
request.len = nb_sectors * 512;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, qiov, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, NULL, 0);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* qemu-nbd has a limit of slightly less than 1M per request. Try to
|
|
||||||
* remain aligned to 4K. */
|
|
||||||
#define NBD_MAX_SECTORS 2040
|
|
||||||
|
|
||||||
int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
int offset = 0;
|
|
||||||
int ret;
|
|
||||||
while (nb_sectors > NBD_MAX_SECTORS) {
|
|
||||||
ret = nbd_co_readv_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
offset += NBD_MAX_SECTORS * 512;
|
|
||||||
sector_num += NBD_MAX_SECTORS;
|
|
||||||
nb_sectors -= NBD_MAX_SECTORS;
|
|
||||||
}
|
|
||||||
return nbd_co_readv_1(bs, sector_num, nb_sectors, qiov, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
int offset = 0;
|
|
||||||
int ret;
|
|
||||||
while (nb_sectors > NBD_MAX_SECTORS) {
|
|
||||||
ret = nbd_co_writev_1(bs, sector_num, NBD_MAX_SECTORS, qiov, offset);
|
|
||||||
if (ret < 0) {
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
offset += NBD_MAX_SECTORS * 512;
|
|
||||||
sector_num += NBD_MAX_SECTORS;
|
|
||||||
nb_sectors -= NBD_MAX_SECTORS;
|
|
||||||
}
|
|
||||||
return nbd_co_writev_1(bs, sector_num, nb_sectors, qiov, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_co_flush(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_FLUSH };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
if (!(client->nbdflags & NBD_FLAG_SEND_FLUSH)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (client->nbdflags & NBD_FLAG_SEND_FUA) {
|
|
||||||
request.type |= NBD_CMD_FLAG_FUA;
|
|
||||||
}
|
|
||||||
|
|
||||||
request.from = 0;
|
|
||||||
request.len = 0;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, NULL, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, NULL, 0);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = { .type = NBD_CMD_TRIM };
|
|
||||||
struct nbd_reply reply;
|
|
||||||
ssize_t ret;
|
|
||||||
|
|
||||||
if (!(client->nbdflags & NBD_FLAG_SEND_TRIM)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
request.from = sector_num * 512;
|
|
||||||
request.len = nb_sectors * 512;
|
|
||||||
|
|
||||||
nbd_coroutine_start(client, &request);
|
|
||||||
ret = nbd_co_send_request(bs, &request, NULL, 0);
|
|
||||||
if (ret < 0) {
|
|
||||||
reply.error = -ret;
|
|
||||||
} else {
|
|
||||||
nbd_co_receive_reply(client, &request, &reply, NULL, 0);
|
|
||||||
}
|
|
||||||
nbd_coroutine_end(client, &request);
|
|
||||||
return -reply.error;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void nbd_client_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
aio_set_fd_handler(bdrv_get_aio_context(bs),
|
|
||||||
nbd_get_client_session(bs)->sock, NULL, NULL, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void nbd_client_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
aio_set_fd_handler(new_context, nbd_get_client_session(bs)->sock,
|
|
||||||
nbd_reply_ready, NULL, bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
void nbd_client_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
struct nbd_request request = {
|
|
||||||
.type = NBD_CMD_DISC,
|
|
||||||
.from = 0,
|
|
||||||
.len = 0
|
|
||||||
};
|
|
||||||
|
|
||||||
if (client->sock == -1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
nbd_send_request(client->sock, &request);
|
|
||||||
|
|
||||||
nbd_teardown_connection(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
int nbd_client_init(BlockDriverState *bs, int sock, const char *export,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
NbdClientSession *client = nbd_get_client_session(bs);
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
/* NBD handshake */
|
|
||||||
logout("session init %s\n", export);
|
|
||||||
qemu_set_block(sock);
|
|
||||||
ret = nbd_receive_negotiate(sock, export,
|
|
||||||
&client->nbdflags, &client->size,
|
|
||||||
&client->blocksize, errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
logout("Failed to negotiate with the NBD server\n");
|
|
||||||
closesocket(sock);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_co_mutex_init(&client->send_mutex);
|
|
||||||
qemu_co_mutex_init(&client->free_sema);
|
|
||||||
client->sock = sock;
|
|
||||||
|
|
||||||
/* Now that we're connected, set the socket to be non-blocking and
|
|
||||||
* kick the reply mechanism. */
|
|
||||||
qemu_set_nonblock(sock);
|
|
||||||
nbd_client_attach_aio_context(bs, bdrv_get_aio_context(bs));
|
|
||||||
|
|
||||||
logout("Established connection with NBD server\n");
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
#ifndef NBD_CLIENT_H
|
|
||||||
#define NBD_CLIENT_H
|
|
||||||
|
|
||||||
#include "qemu-common.h"
|
|
||||||
#include "block/nbd.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
|
|
||||||
/* #define DEBUG_NBD */
|
|
||||||
|
|
||||||
#if defined(DEBUG_NBD)
|
|
||||||
#define logout(fmt, ...) \
|
|
||||||
fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
|
|
||||||
#else
|
|
||||||
#define logout(fmt, ...) ((void)0)
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define MAX_NBD_REQUESTS 16
|
|
||||||
|
|
||||||
typedef struct NbdClientSession {
|
|
||||||
int sock;
|
|
||||||
uint32_t nbdflags;
|
|
||||||
off_t size;
|
|
||||||
size_t blocksize;
|
|
||||||
|
|
||||||
CoMutex send_mutex;
|
|
||||||
CoMutex free_sema;
|
|
||||||
Coroutine *send_coroutine;
|
|
||||||
int in_flight;
|
|
||||||
|
|
||||||
Coroutine *recv_coroutine[MAX_NBD_REQUESTS];
|
|
||||||
struct nbd_reply reply;
|
|
||||||
|
|
||||||
bool is_unix;
|
|
||||||
} NbdClientSession;
|
|
||||||
|
|
||||||
NbdClientSession *nbd_get_client_session(BlockDriverState *bs);
|
|
||||||
|
|
||||||
int nbd_client_init(BlockDriverState *bs, int sock, const char *export_name,
|
|
||||||
Error **errp);
|
|
||||||
void nbd_client_close(BlockDriverState *bs);
|
|
||||||
|
|
||||||
int nbd_client_co_discard(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors);
|
|
||||||
int nbd_client_co_flush(BlockDriverState *bs);
|
|
||||||
int nbd_client_co_writev(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov);
|
|
||||||
int nbd_client_co_readv(BlockDriverState *bs, int64_t sector_num,
|
|
||||||
int nb_sectors, QEMUIOVector *qiov);
|
|
||||||
|
|
||||||
void nbd_client_detach_aio_context(BlockDriverState *bs);
|
|
||||||
void nbd_client_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context);
|
|
||||||
|
|
||||||
#endif /* NBD_CLIENT_H */
|
|
||||||
466
block/nbd.c
466
block/nbd.c
@@ -26,128 +26,46 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "block/nbd-client.h"
|
#include "qemu-common.h"
|
||||||
#include "qemu/uri.h"
|
#include "nbd.h"
|
||||||
#include "block/block_int.h"
|
#include "module.h"
|
||||||
#include "qemu/module.h"
|
#include "qemu_socket.h"
|
||||||
#include "qemu/sockets.h"
|
|
||||||
#include "qapi/qmp/qdict.h"
|
|
||||||
#include "qapi/qmp/qjson.h"
|
|
||||||
#include "qapi/qmp/qint.h"
|
|
||||||
#include "qapi/qmp/qstring.h"
|
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#define EN_OPTSTR ":exportname="
|
#define EN_OPTSTR ":exportname="
|
||||||
|
|
||||||
|
/* #define DEBUG_NBD */
|
||||||
|
|
||||||
|
#if defined(DEBUG_NBD)
|
||||||
|
#define logout(fmt, ...) \
|
||||||
|
fprintf(stderr, "nbd\t%-24s" fmt, __func__, ##__VA_ARGS__)
|
||||||
|
#else
|
||||||
|
#define logout(fmt, ...) ((void)0)
|
||||||
|
#endif
|
||||||
|
|
||||||
typedef struct BDRVNBDState {
|
typedef struct BDRVNBDState {
|
||||||
NbdClientSession client;
|
int sock;
|
||||||
QemuOpts *socket_opts;
|
off_t size;
|
||||||
|
size_t blocksize;
|
||||||
|
char *export_name; /* An NBD server may export several devices */
|
||||||
|
|
||||||
|
/* If it begins with '/', this is a UNIX domain socket. Otherwise,
|
||||||
|
* it's a string of the form <hostname|ip4|\[ip6\]>:port
|
||||||
|
*/
|
||||||
|
char *host_spec;
|
||||||
} BDRVNBDState;
|
} BDRVNBDState;
|
||||||
|
|
||||||
static int nbd_parse_uri(const char *filename, QDict *options)
|
static int nbd_config(BDRVNBDState *s, const char *filename, int flags)
|
||||||
{
|
|
||||||
URI *uri;
|
|
||||||
const char *p;
|
|
||||||
QueryParams *qp = NULL;
|
|
||||||
int ret = 0;
|
|
||||||
bool is_unix;
|
|
||||||
|
|
||||||
uri = uri_parse(filename);
|
|
||||||
if (!uri) {
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* transport */
|
|
||||||
if (!strcmp(uri->scheme, "nbd")) {
|
|
||||||
is_unix = false;
|
|
||||||
} else if (!strcmp(uri->scheme, "nbd+tcp")) {
|
|
||||||
is_unix = false;
|
|
||||||
} else if (!strcmp(uri->scheme, "nbd+unix")) {
|
|
||||||
is_unix = true;
|
|
||||||
} else {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
p = uri->path ? uri->path : "/";
|
|
||||||
p += strspn(p, "/");
|
|
||||||
if (p[0]) {
|
|
||||||
qdict_put(options, "export", qstring_from_str(p));
|
|
||||||
}
|
|
||||||
|
|
||||||
qp = query_params_parse(uri->query);
|
|
||||||
if (qp->n > 1 || (is_unix && !qp->n) || (!is_unix && qp->n)) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (is_unix) {
|
|
||||||
/* nbd+unix:///export?socket=path */
|
|
||||||
if (uri->server || uri->port || strcmp(qp->p[0].name, "socket")) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
qdict_put(options, "path", qstring_from_str(qp->p[0].value));
|
|
||||||
} else {
|
|
||||||
QString *host;
|
|
||||||
/* nbd[+tcp]://host[:port]/export */
|
|
||||||
if (!uri->server) {
|
|
||||||
ret = -EINVAL;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* strip braces from literal IPv6 address */
|
|
||||||
if (uri->server[0] == '[') {
|
|
||||||
host = qstring_from_substr(uri->server, 1,
|
|
||||||
strlen(uri->server) - 2);
|
|
||||||
} else {
|
|
||||||
host = qstring_from_str(uri->server);
|
|
||||||
}
|
|
||||||
|
|
||||||
qdict_put(options, "host", host);
|
|
||||||
if (uri->port) {
|
|
||||||
char* port_str = g_strdup_printf("%d", uri->port);
|
|
||||||
qdict_put(options, "port", qstring_from_str(port_str));
|
|
||||||
g_free(port_str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
out:
|
|
||||||
if (qp) {
|
|
||||||
query_params_free(qp);
|
|
||||||
}
|
|
||||||
uri_free(uri);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_parse_filename(const char *filename, QDict *options,
|
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
char *file;
|
char *file;
|
||||||
char *export_name;
|
char *export_name;
|
||||||
const char *host_spec;
|
const char *host_spec;
|
||||||
const char *unixpath;
|
const char *unixpath;
|
||||||
|
int err = -EINVAL;
|
||||||
|
|
||||||
if (qdict_haskey(options, "host")
|
file = qemu_strdup(filename);
|
||||||
|| qdict_haskey(options, "port")
|
|
||||||
|| qdict_haskey(options, "path"))
|
|
||||||
{
|
|
||||||
error_setg(errp, "host/port/path and a file name may not be specified "
|
|
||||||
"at the same time");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (strstr(filename, "://")) {
|
|
||||||
int ret = nbd_parse_uri(filename, options);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "No valid URL specified");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
file = g_strdup(filename);
|
|
||||||
|
|
||||||
export_name = strstr(file, EN_OPTSTR);
|
export_name = strstr(file, EN_OPTSTR);
|
||||||
if (export_name) {
|
if (export_name) {
|
||||||
@@ -156,291 +74,199 @@ static void nbd_parse_filename(const char *filename, QDict *options,
|
|||||||
}
|
}
|
||||||
export_name[0] = 0; /* truncate 'file' */
|
export_name[0] = 0; /* truncate 'file' */
|
||||||
export_name += strlen(EN_OPTSTR);
|
export_name += strlen(EN_OPTSTR);
|
||||||
|
s->export_name = qemu_strdup(export_name);
|
||||||
qdict_put(options, "export", qstring_from_str(export_name));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* extract the host_spec - fail if it's not nbd:... */
|
/* extract the host_spec - fail if it's not nbd:... */
|
||||||
if (!strstart(file, "nbd:", &host_spec)) {
|
if (!strstart(file, "nbd:", &host_spec)) {
|
||||||
error_setg(errp, "File name string for NBD must start with 'nbd:'");
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!*host_spec) {
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* are we a UNIX or TCP socket? */
|
/* are we a UNIX or TCP socket? */
|
||||||
if (strstart(host_spec, "unix:", &unixpath)) {
|
if (strstart(host_spec, "unix:", &unixpath)) {
|
||||||
qdict_put(options, "path", qstring_from_str(unixpath));
|
if (unixpath[0] != '/') { /* We demand an absolute path*/
|
||||||
} else {
|
|
||||||
InetSocketAddress *addr = NULL;
|
|
||||||
|
|
||||||
addr = inet_parse(host_spec, errp);
|
|
||||||
if (!addr) {
|
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
s->host_spec = qemu_strdup(unixpath);
|
||||||
qdict_put(options, "host", qstring_from_str(addr->host));
|
} else {
|
||||||
qdict_put(options, "port", qstring_from_str(addr->port));
|
s->host_spec = qemu_strdup(host_spec);
|
||||||
qapi_free_InetSocketAddress(addr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = 0;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
g_free(file);
|
qemu_free(file);
|
||||||
|
if (err != 0) {
|
||||||
|
qemu_free(s->export_name);
|
||||||
|
qemu_free(s->host_spec);
|
||||||
|
}
|
||||||
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nbd_config(BDRVNBDState *s, QDict *options, char **export,
|
static int nbd_establish_connection(BlockDriverState *bs)
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
if (qdict_haskey(options, "path") == qdict_haskey(options, "host")) {
|
|
||||||
if (qdict_haskey(options, "path")) {
|
|
||||||
error_setg(errp, "path and host may not be used at the same time.");
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "one of path and host must be specified.");
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
s->client.is_unix = qdict_haskey(options, "path");
|
|
||||||
s->socket_opts = qemu_opts_create(&socket_optslist, NULL, 0,
|
|
||||||
&error_abort);
|
|
||||||
|
|
||||||
qemu_opts_absorb_qdict(s->socket_opts, options, &local_err);
|
|
||||||
if (local_err) {
|
|
||||||
error_propagate(errp, local_err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!qemu_opt_get(s->socket_opts, "port")) {
|
|
||||||
qemu_opt_set_number(s->socket_opts, "port", NBD_DEFAULT_PORT);
|
|
||||||
}
|
|
||||||
|
|
||||||
*export = g_strdup(qdict_get_try_str(options, "export"));
|
|
||||||
if (*export) {
|
|
||||||
qdict_del(options, "export");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
NbdClientSession *nbd_get_client_session(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVNBDState *s = bs->opaque;
|
|
||||||
return &s->client;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_establish_connection(BlockDriverState *bs, Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
int sock;
|
int sock;
|
||||||
|
int ret;
|
||||||
|
off_t size;
|
||||||
|
size_t blocksize;
|
||||||
|
uint32_t nbdflags;
|
||||||
|
|
||||||
if (s->client.is_unix) {
|
if (s->host_spec[0] == '/') {
|
||||||
sock = unix_connect_opts(s->socket_opts, errp, NULL, NULL);
|
sock = unix_socket_outgoing(s->host_spec);
|
||||||
} else {
|
} else {
|
||||||
sock = inet_connect_opts(s->socket_opts, errp, NULL, NULL);
|
sock = tcp_socket_outgoing_spec(s->host_spec);
|
||||||
if (sock >= 0) {
|
|
||||||
socket_set_nodelay(sock);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Failed to establish connection */
|
/* Failed to establish connection */
|
||||||
if (sock < 0) {
|
if (sock == -1) {
|
||||||
logout("Failed to establish connection to NBD server\n");
|
logout("Failed to establish connection to NBD server\n");
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
return sock;
|
/* NBD handshake */
|
||||||
|
ret = nbd_receive_negotiate(sock, s->export_name, &nbdflags, &size,
|
||||||
|
&blocksize);
|
||||||
|
if (ret == -1) {
|
||||||
|
logout("Failed to negotiate with the NBD server\n");
|
||||||
|
closesocket(sock);
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Now that we're connected, set the socket to be non-blocking */
|
||||||
|
socket_set_nonblock(sock);
|
||||||
|
|
||||||
|
s->sock = sock;
|
||||||
|
s->size = size;
|
||||||
|
s->blocksize = blocksize;
|
||||||
|
|
||||||
|
logout("Established connection with NBD server\n");
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_open(BlockDriverState *bs, QDict *options, int flags,
|
static void nbd_teardown_connection(BlockDriverState *bs)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
char *export = NULL;
|
struct nbd_request request;
|
||||||
int result, sock;
|
|
||||||
Error *local_err = NULL;
|
request.type = NBD_CMD_DISC;
|
||||||
|
request.handle = (uint64_t)(intptr_t)bs;
|
||||||
|
request.from = 0;
|
||||||
|
request.len = 0;
|
||||||
|
nbd_send_request(s->sock, &request);
|
||||||
|
|
||||||
|
closesocket(s->sock);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int nbd_open(BlockDriverState *bs, const char* filename, int flags)
|
||||||
|
{
|
||||||
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
int result;
|
||||||
|
|
||||||
/* Pop the config into our state object. Exit if invalid. */
|
/* Pop the config into our state object. Exit if invalid. */
|
||||||
nbd_config(s, options, &export, &local_err);
|
result = nbd_config(s, filename, flags);
|
||||||
if (local_err) {
|
if (result != 0) {
|
||||||
error_propagate(errp, local_err);
|
return result;
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* establish TCP connection, return error if it fails
|
/* establish TCP connection, return error if it fails
|
||||||
* TODO: Configurable retry-until-timeout behaviour.
|
* TODO: Configurable retry-until-timeout behaviour.
|
||||||
*/
|
*/
|
||||||
sock = nbd_establish_connection(bs, errp);
|
result = nbd_establish_connection(bs);
|
||||||
if (sock < 0) {
|
|
||||||
return sock;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* NBD handshake */
|
|
||||||
result = nbd_client_init(bs, sock, export, errp);
|
|
||||||
g_free(export);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_co_readv(BlockDriverState *bs, int64_t sector_num,
|
static int nbd_read(BlockDriverState *bs, int64_t sector_num,
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
uint8_t *buf, int nb_sectors)
|
||||||
{
|
{
|
||||||
return nbd_client_co_readv(bs, sector_num, nb_sectors, qiov);
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
struct nbd_request request;
|
||||||
|
struct nbd_reply reply;
|
||||||
|
|
||||||
|
request.type = NBD_CMD_READ;
|
||||||
|
request.handle = (uint64_t)(intptr_t)bs;
|
||||||
|
request.from = sector_num * 512;;
|
||||||
|
request.len = nb_sectors * 512;
|
||||||
|
|
||||||
|
if (nbd_send_request(s->sock, &request) == -1)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
if (nbd_receive_reply(s->sock, &reply) == -1)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
if (reply.error !=0)
|
||||||
|
return -reply.error;
|
||||||
|
|
||||||
|
if (reply.handle != request.handle)
|
||||||
|
return -EIO;
|
||||||
|
|
||||||
|
if (nbd_wr_sync(s->sock, buf, request.len, 1) != request.len)
|
||||||
|
return -EIO;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nbd_co_writev(BlockDriverState *bs, int64_t sector_num,
|
static int nbd_write(BlockDriverState *bs, int64_t sector_num,
|
||||||
int nb_sectors, QEMUIOVector *qiov)
|
const uint8_t *buf, int nb_sectors)
|
||||||
{
|
{
|
||||||
return nbd_client_co_writev(bs, sector_num, nb_sectors, qiov);
|
BDRVNBDState *s = bs->opaque;
|
||||||
}
|
struct nbd_request request;
|
||||||
|
struct nbd_reply reply;
|
||||||
|
|
||||||
static int nbd_co_flush(BlockDriverState *bs)
|
request.type = NBD_CMD_WRITE;
|
||||||
{
|
request.handle = (uint64_t)(intptr_t)bs;
|
||||||
return nbd_client_co_flush(bs);
|
request.from = sector_num * 512;;
|
||||||
}
|
request.len = nb_sectors * 512;
|
||||||
|
|
||||||
static void nbd_refresh_limits(BlockDriverState *bs, Error **errp)
|
if (nbd_send_request(s->sock, &request) == -1)
|
||||||
{
|
return -errno;
|
||||||
bs->bl.max_discard = UINT32_MAX >> BDRV_SECTOR_BITS;
|
|
||||||
bs->bl.max_transfer_length = UINT32_MAX >> BDRV_SECTOR_BITS;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nbd_co_discard(BlockDriverState *bs, int64_t sector_num,
|
if (nbd_wr_sync(s->sock, (uint8_t*)buf, request.len, 0) != request.len)
|
||||||
int nb_sectors)
|
return -EIO;
|
||||||
{
|
|
||||||
return nbd_client_co_discard(bs, sector_num, nb_sectors);
|
if (nbd_receive_reply(s->sock, &reply) == -1)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
if (reply.error !=0)
|
||||||
|
return -reply.error;
|
||||||
|
|
||||||
|
if (reply.handle != request.handle)
|
||||||
|
return -EIO;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void nbd_close(BlockDriverState *bs)
|
static void nbd_close(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
qemu_free(s->export_name);
|
||||||
|
qemu_free(s->host_spec);
|
||||||
|
|
||||||
qemu_opts_del(s->socket_opts);
|
nbd_teardown_connection(bs);
|
||||||
nbd_client_close(bs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int64_t nbd_getlength(BlockDriverState *bs)
|
static int64_t nbd_getlength(BlockDriverState *bs)
|
||||||
{
|
{
|
||||||
BDRVNBDState *s = bs->opaque;
|
BDRVNBDState *s = bs->opaque;
|
||||||
|
|
||||||
return s->client.size;
|
return s->size;
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
nbd_client_detach_aio_context(bs);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
nbd_client_attach_aio_context(bs, new_context);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nbd_refresh_filename(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
QDict *opts = qdict_new();
|
|
||||||
const char *path = qdict_get_try_str(bs->options, "path");
|
|
||||||
const char *host = qdict_get_try_str(bs->options, "host");
|
|
||||||
const char *port = qdict_get_try_str(bs->options, "port");
|
|
||||||
const char *export = qdict_get_try_str(bs->options, "export");
|
|
||||||
|
|
||||||
qdict_put_obj(opts, "driver", QOBJECT(qstring_from_str("nbd")));
|
|
||||||
|
|
||||||
if (path && export) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd+unix:///%s?socket=%s", export, path);
|
|
||||||
} else if (path && !export) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd+unix://?socket=%s", path);
|
|
||||||
} else if (!path && export && port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s:%s/%s", host, port, export);
|
|
||||||
} else if (!path && export && !port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s/%s", host, export);
|
|
||||||
} else if (!path && !export && port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s:%s", host, port);
|
|
||||||
} else if (!path && !export && !port) {
|
|
||||||
snprintf(bs->exact_filename, sizeof(bs->exact_filename),
|
|
||||||
"nbd://%s", host);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (path) {
|
|
||||||
qdict_put_obj(opts, "path", QOBJECT(qstring_from_str(path)));
|
|
||||||
} else if (port) {
|
|
||||||
qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
|
|
||||||
qdict_put_obj(opts, "port", QOBJECT(qstring_from_str(port)));
|
|
||||||
} else {
|
|
||||||
qdict_put_obj(opts, "host", QOBJECT(qstring_from_str(host)));
|
|
||||||
}
|
|
||||||
if (export) {
|
|
||||||
qdict_put_obj(opts, "export", QOBJECT(qstring_from_str(export)));
|
|
||||||
}
|
|
||||||
|
|
||||||
bs->full_open_options = opts;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static BlockDriver bdrv_nbd = {
|
static BlockDriver bdrv_nbd = {
|
||||||
.format_name = "nbd",
|
.format_name = "nbd",
|
||||||
|
.instance_size = sizeof(BDRVNBDState),
|
||||||
|
.bdrv_file_open = nbd_open,
|
||||||
|
.bdrv_read = nbd_read,
|
||||||
|
.bdrv_write = nbd_write,
|
||||||
|
.bdrv_close = nbd_close,
|
||||||
|
.bdrv_getlength = nbd_getlength,
|
||||||
.protocol_name = "nbd",
|
.protocol_name = "nbd",
|
||||||
.instance_size = sizeof(BDRVNBDState),
|
|
||||||
.bdrv_parse_filename = nbd_parse_filename,
|
|
||||||
.bdrv_file_open = nbd_open,
|
|
||||||
.bdrv_co_readv = nbd_co_readv,
|
|
||||||
.bdrv_co_writev = nbd_co_writev,
|
|
||||||
.bdrv_close = nbd_close,
|
|
||||||
.bdrv_co_flush_to_os = nbd_co_flush,
|
|
||||||
.bdrv_co_discard = nbd_co_discard,
|
|
||||||
.bdrv_refresh_limits = nbd_refresh_limits,
|
|
||||||
.bdrv_getlength = nbd_getlength,
|
|
||||||
.bdrv_detach_aio_context = nbd_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nbd_attach_aio_context,
|
|
||||||
.bdrv_refresh_filename = nbd_refresh_filename,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_nbd_tcp = {
|
|
||||||
.format_name = "nbd",
|
|
||||||
.protocol_name = "nbd+tcp",
|
|
||||||
.instance_size = sizeof(BDRVNBDState),
|
|
||||||
.bdrv_parse_filename = nbd_parse_filename,
|
|
||||||
.bdrv_file_open = nbd_open,
|
|
||||||
.bdrv_co_readv = nbd_co_readv,
|
|
||||||
.bdrv_co_writev = nbd_co_writev,
|
|
||||||
.bdrv_close = nbd_close,
|
|
||||||
.bdrv_co_flush_to_os = nbd_co_flush,
|
|
||||||
.bdrv_co_discard = nbd_co_discard,
|
|
||||||
.bdrv_refresh_limits = nbd_refresh_limits,
|
|
||||||
.bdrv_getlength = nbd_getlength,
|
|
||||||
.bdrv_detach_aio_context = nbd_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nbd_attach_aio_context,
|
|
||||||
.bdrv_refresh_filename = nbd_refresh_filename,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_nbd_unix = {
|
|
||||||
.format_name = "nbd",
|
|
||||||
.protocol_name = "nbd+unix",
|
|
||||||
.instance_size = sizeof(BDRVNBDState),
|
|
||||||
.bdrv_parse_filename = nbd_parse_filename,
|
|
||||||
.bdrv_file_open = nbd_open,
|
|
||||||
.bdrv_co_readv = nbd_co_readv,
|
|
||||||
.bdrv_co_writev = nbd_co_writev,
|
|
||||||
.bdrv_close = nbd_close,
|
|
||||||
.bdrv_co_flush_to_os = nbd_co_flush,
|
|
||||||
.bdrv_co_discard = nbd_co_discard,
|
|
||||||
.bdrv_refresh_limits = nbd_refresh_limits,
|
|
||||||
.bdrv_getlength = nbd_getlength,
|
|
||||||
.bdrv_detach_aio_context = nbd_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nbd_attach_aio_context,
|
|
||||||
.bdrv_refresh_filename = nbd_refresh_filename,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static void bdrv_nbd_init(void)
|
static void bdrv_nbd_init(void)
|
||||||
{
|
{
|
||||||
bdrv_register(&bdrv_nbd);
|
bdrv_register(&bdrv_nbd);
|
||||||
bdrv_register(&bdrv_nbd_tcp);
|
|
||||||
bdrv_register(&bdrv_nbd_unix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
block_init(bdrv_nbd_init);
|
block_init(bdrv_nbd_init);
|
||||||
|
|||||||
509
block/nfs.c
509
block/nfs.c
@@ -1,509 +0,0 @@
|
|||||||
/*
|
|
||||||
* QEMU Block driver for native access to files on NFS shares
|
|
||||||
*
|
|
||||||
* Copyright (c) 2014 Peter Lieven <pl@kamp.de>
|
|
||||||
*
|
|
||||||
* 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 "config-host.h"
|
|
||||||
|
|
||||||
#include <poll.h>
|
|
||||||
#include "qemu-common.h"
|
|
||||||
#include "qemu/config-file.h"
|
|
||||||
#include "qemu/error-report.h"
|
|
||||||
#include "block/block_int.h"
|
|
||||||
#include "trace.h"
|
|
||||||
#include "qemu/iov.h"
|
|
||||||
#include "qemu/uri.h"
|
|
||||||
#include "sysemu/sysemu.h"
|
|
||||||
#include <nfsc/libnfs.h>
|
|
||||||
|
|
||||||
typedef struct NFSClient {
|
|
||||||
struct nfs_context *context;
|
|
||||||
struct nfsfh *fh;
|
|
||||||
int events;
|
|
||||||
bool has_zero_init;
|
|
||||||
AioContext *aio_context;
|
|
||||||
} NFSClient;
|
|
||||||
|
|
||||||
typedef struct NFSRPC {
|
|
||||||
int ret;
|
|
||||||
int complete;
|
|
||||||
QEMUIOVector *iov;
|
|
||||||
struct stat *st;
|
|
||||||
Coroutine *co;
|
|
||||||
QEMUBH *bh;
|
|
||||||
NFSClient *client;
|
|
||||||
} NFSRPC;
|
|
||||||
|
|
||||||
static void nfs_process_read(void *arg);
|
|
||||||
static void nfs_process_write(void *arg);
|
|
||||||
|
|
||||||
static void nfs_set_events(NFSClient *client)
|
|
||||||
{
|
|
||||||
int ev = nfs_which_events(client->context);
|
|
||||||
if (ev != client->events) {
|
|
||||||
aio_set_fd_handler(client->aio_context,
|
|
||||||
nfs_get_fd(client->context),
|
|
||||||
(ev & POLLIN) ? nfs_process_read : NULL,
|
|
||||||
(ev & POLLOUT) ? nfs_process_write : NULL,
|
|
||||||
client);
|
|
||||||
|
|
||||||
}
|
|
||||||
client->events = ev;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_process_read(void *arg)
|
|
||||||
{
|
|
||||||
NFSClient *client = arg;
|
|
||||||
nfs_service(client->context, POLLIN);
|
|
||||||
nfs_set_events(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_process_write(void *arg)
|
|
||||||
{
|
|
||||||
NFSClient *client = arg;
|
|
||||||
nfs_service(client->context, POLLOUT);
|
|
||||||
nfs_set_events(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_co_init_task(NFSClient *client, NFSRPC *task)
|
|
||||||
{
|
|
||||||
*task = (NFSRPC) {
|
|
||||||
.co = qemu_coroutine_self(),
|
|
||||||
.client = client,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_co_generic_bh_cb(void *opaque)
|
|
||||||
{
|
|
||||||
NFSRPC *task = opaque;
|
|
||||||
task->complete = 1;
|
|
||||||
qemu_bh_delete(task->bh);
|
|
||||||
qemu_coroutine_enter(task->co, NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
|
||||||
nfs_co_generic_cb(int ret, struct nfs_context *nfs, void *data,
|
|
||||||
void *private_data)
|
|
||||||
{
|
|
||||||
NFSRPC *task = private_data;
|
|
||||||
task->ret = ret;
|
|
||||||
if (task->ret > 0 && task->iov) {
|
|
||||||
if (task->ret <= task->iov->size) {
|
|
||||||
qemu_iovec_from_buf(task->iov, 0, data, task->ret);
|
|
||||||
} else {
|
|
||||||
task->ret = -EIO;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (task->ret == 0 && task->st) {
|
|
||||||
memcpy(task->st, data, sizeof(struct stat));
|
|
||||||
}
|
|
||||||
if (task->ret < 0) {
|
|
||||||
error_report("NFS Error: %s", nfs_get_error(nfs));
|
|
||||||
}
|
|
||||||
if (task->co) {
|
|
||||||
task->bh = aio_bh_new(task->client->aio_context,
|
|
||||||
nfs_co_generic_bh_cb, task);
|
|
||||||
qemu_bh_schedule(task->bh);
|
|
||||||
} else {
|
|
||||||
task->complete = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn nfs_co_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *iov)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task;
|
|
||||||
|
|
||||||
nfs_co_init_task(client, &task);
|
|
||||||
task.iov = iov;
|
|
||||||
|
|
||||||
if (nfs_pread_async(client->context, client->fh,
|
|
||||||
sector_num * BDRV_SECTOR_SIZE,
|
|
||||||
nb_sectors * BDRV_SECTOR_SIZE,
|
|
||||||
nfs_co_generic_cb, &task) != 0) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (task.ret < 0) {
|
|
||||||
return task.ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* zero pad short reads */
|
|
||||||
if (task.ret < iov->size) {
|
|
||||||
qemu_iovec_memset(iov, task.ret, 0, iov->size - task.ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn nfs_co_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *iov)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task;
|
|
||||||
char *buf = NULL;
|
|
||||||
|
|
||||||
nfs_co_init_task(client, &task);
|
|
||||||
|
|
||||||
buf = g_try_malloc(nb_sectors * BDRV_SECTOR_SIZE);
|
|
||||||
if (nb_sectors && buf == NULL) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
qemu_iovec_to_buf(iov, 0, buf, nb_sectors * BDRV_SECTOR_SIZE);
|
|
||||||
|
|
||||||
if (nfs_pwrite_async(client->context, client->fh,
|
|
||||||
sector_num * BDRV_SECTOR_SIZE,
|
|
||||||
nb_sectors * BDRV_SECTOR_SIZE,
|
|
||||||
buf, nfs_co_generic_cb, &task) != 0) {
|
|
||||||
g_free(buf);
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free(buf);
|
|
||||||
|
|
||||||
if (task.ret != nb_sectors * BDRV_SECTOR_SIZE) {
|
|
||||||
return task.ret < 0 ? task.ret : -EIO;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int coroutine_fn nfs_co_flush(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task;
|
|
||||||
|
|
||||||
nfs_co_init_task(client, &task);
|
|
||||||
|
|
||||||
if (nfs_fsync_async(client->context, client->fh, nfs_co_generic_cb,
|
|
||||||
&task) != 0) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
qemu_coroutine_yield();
|
|
||||||
}
|
|
||||||
|
|
||||||
return task.ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* TODO Convert to fine grained options */
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "nfs",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "filename",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "URL to the NFS file",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static void nfs_detach_aio_context(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
|
|
||||||
aio_set_fd_handler(client->aio_context,
|
|
||||||
nfs_get_fd(client->context),
|
|
||||||
NULL, NULL, NULL);
|
|
||||||
client->events = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_attach_aio_context(BlockDriverState *bs,
|
|
||||||
AioContext *new_context)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
|
|
||||||
client->aio_context = new_context;
|
|
||||||
nfs_set_events(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_client_close(NFSClient *client)
|
|
||||||
{
|
|
||||||
if (client->context) {
|
|
||||||
if (client->fh) {
|
|
||||||
nfs_close(client->context, client->fh);
|
|
||||||
}
|
|
||||||
aio_set_fd_handler(client->aio_context,
|
|
||||||
nfs_get_fd(client->context),
|
|
||||||
NULL, NULL, NULL);
|
|
||||||
nfs_destroy_context(client->context);
|
|
||||||
}
|
|
||||||
memset(client, 0, sizeof(NFSClient));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void nfs_file_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
nfs_client_close(client);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t nfs_client_open(NFSClient *client, const char *filename,
|
|
||||||
int flags, Error **errp)
|
|
||||||
{
|
|
||||||
int ret = -EINVAL, i;
|
|
||||||
struct stat st;
|
|
||||||
URI *uri;
|
|
||||||
QueryParams *qp = NULL;
|
|
||||||
char *file = NULL, *strp = NULL;
|
|
||||||
|
|
||||||
uri = uri_parse(filename);
|
|
||||||
if (!uri) {
|
|
||||||
error_setg(errp, "Invalid URL specified");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!uri->server) {
|
|
||||||
error_setg(errp, "Invalid URL specified");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
strp = strrchr(uri->path, '/');
|
|
||||||
if (strp == NULL) {
|
|
||||||
error_setg(errp, "Invalid URL specified");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
file = g_strdup(strp);
|
|
||||||
*strp = 0;
|
|
||||||
|
|
||||||
client->context = nfs_init_context();
|
|
||||||
if (client->context == NULL) {
|
|
||||||
error_setg(errp, "Failed to init NFS context");
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
qp = query_params_parse(uri->query);
|
|
||||||
for (i = 0; i < qp->n; i++) {
|
|
||||||
unsigned long long val;
|
|
||||||
if (!qp->p[i].value) {
|
|
||||||
error_setg(errp, "Value for NFS parameter expected: %s",
|
|
||||||
qp->p[i].name);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (parse_uint_full(qp->p[i].value, &val, 0)) {
|
|
||||||
error_setg(errp, "Illegal value for NFS parameter: %s",
|
|
||||||
qp->p[i].name);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
if (!strcmp(qp->p[i].name, "uid")) {
|
|
||||||
nfs_set_uid(client->context, val);
|
|
||||||
} else if (!strcmp(qp->p[i].name, "gid")) {
|
|
||||||
nfs_set_gid(client->context, val);
|
|
||||||
} else if (!strcmp(qp->p[i].name, "tcp-syncnt")) {
|
|
||||||
nfs_set_tcp_syncnt(client->context, val);
|
|
||||||
#ifdef LIBNFS_FEATURE_READAHEAD
|
|
||||||
} else if (!strcmp(qp->p[i].name, "readahead")) {
|
|
||||||
nfs_set_readahead(client->context, val);
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
error_setg(errp, "Unknown NFS parameter name: %s",
|
|
||||||
qp->p[i].name);
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = nfs_mount(client->context, uri->server, uri->path);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to mount nfs share: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (flags & O_CREAT) {
|
|
||||||
ret = nfs_creat(client->context, file, 0600, &client->fh);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to create file: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = nfs_open(client->context, file, flags, &client->fh);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to open file : %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = nfs_fstat(client->context, client->fh, &st);
|
|
||||||
if (ret < 0) {
|
|
||||||
error_setg(errp, "Failed to fstat file: %s",
|
|
||||||
nfs_get_error(client->context));
|
|
||||||
goto fail;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = DIV_ROUND_UP(st.st_size, BDRV_SECTOR_SIZE);
|
|
||||||
client->has_zero_init = S_ISREG(st.st_mode);
|
|
||||||
goto out;
|
|
||||||
fail:
|
|
||||||
nfs_client_close(client);
|
|
||||||
out:
|
|
||||||
if (qp) {
|
|
||||||
query_params_free(qp);
|
|
||||||
}
|
|
||||||
uri_free(uri);
|
|
||||||
g_free(file);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp) {
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
int64_t ret;
|
|
||||||
QemuOpts *opts;
|
|
||||||
Error *local_err = NULL;
|
|
||||||
|
|
||||||
client->aio_context = bdrv_get_aio_context(bs);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
ret = nfs_client_open(client, qemu_opt_get(opts, "filename"),
|
|
||||||
(flags & BDRV_O_RDWR) ? O_RDWR : O_RDONLY,
|
|
||||||
errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
bs->total_sectors = ret;
|
|
||||||
ret = 0;
|
|
||||||
out:
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static QemuOptsList nfs_create_opts = {
|
|
||||||
.name = "nfs-create-opts",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(nfs_create_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = BLOCK_OPT_SIZE,
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "Virtual disk size"
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static int nfs_file_create(const char *url, QemuOpts *opts, Error **errp)
|
|
||||||
{
|
|
||||||
int ret = 0;
|
|
||||||
int64_t total_size = 0;
|
|
||||||
NFSClient *client = g_new0(NFSClient, 1);
|
|
||||||
|
|
||||||
client->aio_context = qemu_get_aio_context();
|
|
||||||
|
|
||||||
/* Read out options */
|
|
||||||
total_size = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
|
|
||||||
BDRV_SECTOR_SIZE);
|
|
||||||
|
|
||||||
ret = nfs_client_open(client, url, O_CREAT, errp);
|
|
||||||
if (ret < 0) {
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
ret = nfs_ftruncate(client->context, client->fh, total_size);
|
|
||||||
nfs_client_close(client);
|
|
||||||
out:
|
|
||||||
g_free(client);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nfs_has_zero_init(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
return client->has_zero_init;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t nfs_get_allocated_file_size(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
NFSRPC task = {0};
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
task.st = &st;
|
|
||||||
if (nfs_fstat_async(client->context, client->fh, nfs_co_generic_cb,
|
|
||||||
&task) != 0) {
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!task.complete) {
|
|
||||||
nfs_set_events(client);
|
|
||||||
aio_poll(client->aio_context, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (task.ret < 0 ? task.ret : st.st_blocks * st.st_blksize);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int nfs_file_truncate(BlockDriverState *bs, int64_t offset)
|
|
||||||
{
|
|
||||||
NFSClient *client = bs->opaque;
|
|
||||||
return nfs_ftruncate(client->context, client->fh, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockDriver bdrv_nfs = {
|
|
||||||
.format_name = "nfs",
|
|
||||||
.protocol_name = "nfs",
|
|
||||||
|
|
||||||
.instance_size = sizeof(NFSClient),
|
|
||||||
.bdrv_needs_filename = true,
|
|
||||||
.create_opts = &nfs_create_opts,
|
|
||||||
|
|
||||||
.bdrv_has_zero_init = nfs_has_zero_init,
|
|
||||||
.bdrv_get_allocated_file_size = nfs_get_allocated_file_size,
|
|
||||||
.bdrv_truncate = nfs_file_truncate,
|
|
||||||
|
|
||||||
.bdrv_file_open = nfs_file_open,
|
|
||||||
.bdrv_close = nfs_file_close,
|
|
||||||
.bdrv_create = nfs_file_create,
|
|
||||||
|
|
||||||
.bdrv_co_readv = nfs_co_readv,
|
|
||||||
.bdrv_co_writev = nfs_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = nfs_co_flush,
|
|
||||||
|
|
||||||
.bdrv_detach_aio_context = nfs_detach_aio_context,
|
|
||||||
.bdrv_attach_aio_context = nfs_attach_aio_context,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void nfs_block_init(void)
|
|
||||||
{
|
|
||||||
bdrv_register(&bdrv_nfs);
|
|
||||||
}
|
|
||||||
|
|
||||||
block_init(nfs_block_init);
|
|
||||||
168
block/null.c
168
block/null.c
@@ -1,168 +0,0 @@
|
|||||||
/*
|
|
||||||
* Null block driver
|
|
||||||
*
|
|
||||||
* Authors:
|
|
||||||
* Fam Zheng <famz@redhat.com>
|
|
||||||
*
|
|
||||||
* Copyright (C) 2014 Red Hat, Inc.
|
|
||||||
*
|
|
||||||
* 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 "block/block_int.h"
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
int64_t length;
|
|
||||||
} BDRVNullState;
|
|
||||||
|
|
||||||
static QemuOptsList runtime_opts = {
|
|
||||||
.name = "null",
|
|
||||||
.head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
|
|
||||||
.desc = {
|
|
||||||
{
|
|
||||||
.name = "filename",
|
|
||||||
.type = QEMU_OPT_STRING,
|
|
||||||
.help = "",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
.name = BLOCK_OPT_SIZE,
|
|
||||||
.type = QEMU_OPT_SIZE,
|
|
||||||
.help = "size of the null block",
|
|
||||||
},
|
|
||||||
{ /* end of list */ }
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
|
|
||||||
Error **errp)
|
|
||||||
{
|
|
||||||
QemuOpts *opts;
|
|
||||||
BDRVNullState *s = bs->opaque;
|
|
||||||
|
|
||||||
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
|
|
||||||
qemu_opts_absorb_qdict(opts, options, &error_abort);
|
|
||||||
s->length =
|
|
||||||
qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
|
|
||||||
qemu_opts_del(opts);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void null_close(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
static int64_t null_getlength(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
BDRVNullState *s = bs->opaque;
|
|
||||||
return s->length;
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int null_co_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int null_co_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, int nb_sectors,
|
|
||||||
QEMUIOVector *qiov)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static coroutine_fn int null_co_flush(BlockDriverState *bs)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
BlockAIOCB common;
|
|
||||||
QEMUBH *bh;
|
|
||||||
} NullAIOCB;
|
|
||||||
|
|
||||||
static const AIOCBInfo null_aiocb_info = {
|
|
||||||
.aiocb_size = sizeof(NullAIOCB),
|
|
||||||
};
|
|
||||||
|
|
||||||
static void null_bh_cb(void *opaque)
|
|
||||||
{
|
|
||||||
NullAIOCB *acb = opaque;
|
|
||||||
acb->common.cb(acb->common.opaque, 0);
|
|
||||||
qemu_bh_delete(acb->bh);
|
|
||||||
qemu_aio_unref(acb);
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
NullAIOCB *acb;
|
|
||||||
|
|
||||||
acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
|
|
||||||
acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
|
|
||||||
qemu_bh_schedule(acb->bh);
|
|
||||||
return &acb->common;
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov,
|
|
||||||
int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
return null_aio_common(bs, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
|
|
||||||
int64_t sector_num, QEMUIOVector *qiov,
|
|
||||||
int nb_sectors,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
return null_aio_common(bs, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
|
|
||||||
BlockCompletionFunc *cb,
|
|
||||||
void *opaque)
|
|
||||||
{
|
|
||||||
return null_aio_common(bs, cb, opaque);
|
|
||||||
}
|
|
||||||
|
|
||||||
static BlockDriver bdrv_null_co = {
|
|
||||||
.format_name = "null-co",
|
|
||||||
.protocol_name = "null-co",
|
|
||||||
.instance_size = sizeof(BDRVNullState),
|
|
||||||
|
|
||||||
.bdrv_file_open = null_file_open,
|
|
||||||
.bdrv_close = null_close,
|
|
||||||
.bdrv_getlength = null_getlength,
|
|
||||||
|
|
||||||
.bdrv_co_readv = null_co_readv,
|
|
||||||
.bdrv_co_writev = null_co_writev,
|
|
||||||
.bdrv_co_flush_to_disk = null_co_flush,
|
|
||||||
};
|
|
||||||
|
|
||||||
static BlockDriver bdrv_null_aio = {
|
|
||||||
.format_name = "null-aio",
|
|
||||||
.protocol_name = "null-aio",
|
|
||||||
.instance_size = sizeof(BDRVNullState),
|
|
||||||
|
|
||||||
.bdrv_file_open = null_file_open,
|
|
||||||
.bdrv_close = null_close,
|
|
||||||
.bdrv_getlength = null_getlength,
|
|
||||||
|
|
||||||
.bdrv_aio_readv = null_aio_readv,
|
|
||||||
.bdrv_aio_writev = null_aio_writev,
|
|
||||||
.bdrv_aio_flush = null_aio_flush,
|
|
||||||
};
|
|
||||||
|
|
||||||
static void bdrv_null_init(void)
|
|
||||||
{
|
|
||||||
bdrv_register(&bdrv_null_co);
|
|
||||||
bdrv_register(&bdrv_null_aio);
|
|
||||||
}
|
|
||||||
|
|
||||||
block_init(bdrv_null_init);
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user