45 Commits

Author SHA1 Message Date
Andre Przywara 15542bab78 avoid casts when initializing structures
Due to our kernel heritage we have code in kvmtool that relies on
the (still) implicit -std=gnu89 compiler switch.
It turns out that this just affects some structure initialization,
where we currently provide a cast to the type, which upsets GCC for
anything beyond -std=gnu89 (for instance gnu99 or gnu11).
We do need the casts when initializing structures that are not
assigned to the same type, so we put it there explicitly.

This allows us to compile with all the three GNU standards GCC
currently supports: gnu89/90, gnu99 and gnu11.
GCC threatens people with moving to gnu11 as the new default standard,
so lets fix this better sooner than later.
(Compiling without GNU extensions still breaks and I don't bother to
fix that without very good reasons.)

Signed-off-by: Andre Przywara <andre.przywara@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
2015-07-20 18:25:47 +01:00
Marc Zyngier bd3608271e kvmtool: convert console backend to support bi-endianness
Configure the queues to follow the guest endianness, and make sure
the configuration space is doing the same.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:55 +01:00
Will Deacon d97dadecbd kvm tools: ARM: allow default virtio transport to be passed on cmdline
This patch changes VIRTIO_DEFAULT_TRANS to take a struct kvm parameter,
allowing architectures to choose the default transport dynamically.

For ARM, this is driven by an arch-specific cmdline option.

Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:55 +01:00
Jonathan Austin 97bb5a9dfb kvm tools: stop virtio console doing unnecessary input handling
The asynchronous nature of the virtio input handling (using a job queue)
can result in unnecessary jobs being created if there is some delay in
handing input (the original function to handle the input returns immediately
without the file having been read, and hence poll returns immediately
informing us of data to read).

This patch adds synchronisation to the threads so that we don't start
polling input files again until we've read from the console.

Signed-off-by: Jonathan Austin <jonathan.austin@arm.com>
Acked-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:54 +01:00
Sasha Levin e7e2950aa6 kvm tools: fix boot of guests with more than 4gb of ram
Commit "kvm tools: virtio: remove hardcoded assumptions
about guest page size" has introduced a bug that prevented
guests with more than 4gb of ram from booting.

The issue is that 'pfn' is a 32bit integer, so when multiplying
it by page size to get the actual page will cause an overflow if
the pfn referred to a memory area above 4gb.

Acked-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:54 +01:00
Marc Zyngier 7652598814 kvm tools: console: unconditionally output to any console
Kvmtool suppresses any output to a console that has not been elected
as *the* console.

While this makes sense on the input side (we want the input to be sent
to one console driver only), it seems to be the wrong thing to do on
the output side, as it effectively prevents the guest from switching
from one console to another (think earlyprintk using 8250 to virtio
console).

After all, the guest *does* poke this device and outputs something
there.

Just remove the kvm->cfg.active_console test from the output paths.

Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:54 +01:00
Will Deacon c59ba304d5 kvm tools: virtio: remove hardcoded assumptions about guest page size
virtio-based PCI devices deal only with 4k memory granules, making
direct use of the VIRTIO_PCI_VRING_ALIGN and VIRTIO_PCI_QUEUE_ADDR_SHIFT
constants when initialising the virtqueues for a device.

For MMIO-based devices, the guest page size is arbitrary and may differ
from that of the host (this is the case on AArch64, where both 4k and
64k pages are supported).

This patch fixes the virtio drivers to honour the guest page size passed
when configuring the virtio device and align the virtqueues accordingly.

Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:53 +01:00
Will Deacon ae06ce7165 kvm tools: allow arch to specify default virtio transport
Some architectures may provide only a restricted PCI implementation and
therefore prefer MMIO as the transport for virtio devices.

This patch allows the arch backend to specify the default virtio
transport. Some devices (e.g. net) allow the transport to be overriden
by the user and are left alone by this change.

Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:53 +01:00
Will Deacon 7aba29c1f8 kvm tools: virtio: add dummy set_size_vq implementations
When a guest kernel initialises a virtio device using an MMIO transport,
it attempts to set the size of the virtual queue. For devices that expect
a PCI transport, this is not the case and, as such, our console, 9p and
rng virtio devices do not set a pointer for this function and subsequently
SEGV.

This patch adds a dummy implementation of the function to avoid the fatal
signal.

Signed-off-by: Will Deacon <will.deacon@arm.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:53 +01:00
Sasha Levin d3476f7d3b kvm tools: use mutex abstraction instead of pthread mutex
We already have something to wrap pthread with mutex_[init,lock,unlock]
calls. This patch creates a new struct mutex abstraction and moves
everything to work with it.

Signed-off-by: Sasha Levin <sasha.levin@oracle.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:53 +01:00
Sasha Levin 4346fd8f1e kvm tools: remove global kvm object
This was ugly, and now we get rid of it.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:53 +01:00
Sasha Levin 49a8afd1b9 kvm tools: use init/exit where possible
Switch to using init/exit calls instead of the repeating call blocks in builtin-run.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:53 +01:00
Sasha Levin a3fa3f86b8 kvm tools: virtio-console init/exit
Make the init/exit of virtio-console self-contained, so the global init code
won't need to check if it was selected or not.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:52 +01:00
Sasha Levin 2651ea581e kvm tools: move active_console into struct kvm_config
This config option was 'extern'ed between different objects. Clean it up
and move it into struct kvm_config.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:52 +01:00
Sasha Levin c5ae742b4b kvm tools: simplify virtio config handling
Instead of a get/set for config values, just request the address of the
config region, and handle that by simply reading directly from that region.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:52 +01:00
Asias He d278197dc9 kvm tools: Fix compat message
compat_id is initialized to -1 for each type of device. We should add
compat message if the compat_id == -1 which means we haven't added
compat message for this type of device.

Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:52 +01:00
Asias He 52f34d2c8b kvm tools: Introduce virtio_compat_add_message() helper
This patch introduces a helper virtio_compat_add_message() to simplify
adding compat message for virtio device.

Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:52 +01:00
Asias He 0af1e5a77c kvm tools: Code cleanup for virtio/console.c
Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:52 +01:00
Asias He 02eca50ce5 kvm tools: Rework on virtio transport abstraction.
This patch reworks on virtio transport abstraction.

* Move virtio transport operations to virtio operations and drop
  virtio/trans.c This makes the abstraction much cleaner.

* Rename struct virtio_trans to struct virtio_device

  struct virtio_trans {
       void                    *virtio;
       enum virtio_trans_type  type;
       struct virtio_trans_ops *trans_ops;
       struct virtio_ops       *virtio_ops;
  };

  struct virtio_device {
       void                    *virtio;
       struct virtio_ops       *ops;
  };

  The virtio_trans struct is bit confusing since it also includes virtio
  operations.

* Introduce virtio_init()
  To init device, e.g.
  Before:
	virtio_trans_init()
	ndev->vtrans.trans_ops->init()
	ndev->vtrans.virtio_ops = &net_dev_virtio_ops
  After:
	virtio_init()

Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:51 +01:00
Cyrill Gorcunov a28574790c kvm tools: Add BUG_ON() helper to make a run-time critical tests
Also drop useless assert.h inclusions.

Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:50 +01:00
Sasha Levin 1c47ce695a kvm tools: Add abstract virtio transport layer
Since virtio-mmio was introduced in 3.2, virtio-pci isn't the only transport
layer between the kernel and virtio devices.

This patch adds an abstract virtio-transport layer which allows to easily
use different transports while making it transparent to the device.

This is the first step in adding virtio-mmio support.

Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:48 +01:00
Asias He 507e02d818 kvm tools: Set correct virtio pci device class code
All the virtio pci device is set to class code 0x010000, this is wrong.
This patch sets the correct class code and makes lspci report correct
class type. For example, the net device:

Before:
$ lspci
00:03.0 SCSI storage controller: Red Hat, Inc Virtio network device

After:
$ lspci
00:03.0 Ethernet controller: Red Hat, Inc Virtio network device

Acked-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:48 +01:00
Sasha Levin fc835ab38f kvm tools: Improve compat message
Suggested-by: Ingo Molnar <mingo@elte.hu>
Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:48 +01:00
Sasha Levin 312c62d1c9 kvm tools: Use compat message per device instead of per instance
This prevents multiple messages for the same type of device.

Suggested-by: Ingo Molnar <mingo@elte.hu>
Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:48 +01:00
Sasha Levin 1add4b76d6 kvm tools: Allow remapping guest TTY into host PTS
This patch adds the '-tty' option to 'kvm run' which allows the user to
remap a guest TTY into a PTS on the host.

Usage:
        'kvm run --tty [id]'

The tty will be mapped to a pts and will be printed on the screen:
        '  Info: Assigned terminal 1 to pty /dev/pts/X'

At this point, it is possible to communicate with the guest using that pty.

This is useful for debugging guest kernel using KGDB:

1. Run the guest:
        'kvm run -k [vmlinuz] -p "kgdboc=ttyS1 kgdbwait" --tty 1'

And see which PTY got assigned to ttyS1.

2. Run GDB on the host:
        'gdb [vmlinuz]'

3. Connect to the guest (from within GDB):
        'target remote /dev/pty/X'

4. Start debugging! (enter 'continue' to continue boot).

Cc: David Evensky <evensky@dancer.ca.sandia.gov>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
2015-06-01 16:39:47 +01:00
Sasha Levin a398c5ce3c kvm tools: Fix compat message formatting
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:47 +01:00
Sasha Levin c85b4b4de6 kvm tools: Separate pci layer out of virtio-console
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:47 +01:00
Sasha Levin e59b1ed04e kvm tools: Add guest compatability warning to virtio-console
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:47 +01:00
Xiao Guangrong c9f6a0375f kvm tools: remove count in io emulation callbacks
'count' parameter in io emulation callbacks is useless, just remove it

Signed-off-by: Xiao Guangrong <xiaoguangrong@cn.fujitsu.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:47 +01:00
Sasha Levin df0c7f571e kvm tools: Don't dynamically allocate threadpool jobs
To allow efficient use of shorter-term threadpool jobs, don't
allocate them dynamically upon creation. Instead, store them
within 'job' structures.

This will prevent some overhead creating/destroying jobs which live
for a short time.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:45 +01:00
Sasha Levin 4b385ac410 kvm tools: Use dynamic IO port allocation in virtio-console
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:44 +01:00
Sasha Levin 3d62dea656 kvm tools: Add optional parameter used in ioport callbacks
Allow specifying an optional parameter when registering an
ioport range. The callback functions provided by the registering
module will be called with the same parameter.

This may be used to keep context during callbacks on IO operations.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:44 +01:00
Cyrill Gorcunov 4542f27608 kvm tools: Prefix error() and friends helpers with pr_
To look more familiar with kernel functions.

Suggested-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:44 +01:00
Sasha Levin 0a7ab0c664 kvm tools: Use virtio IDs from <linux/virtio_ids.h>
Instead of redefining virtio IDs in our headers, use IDs defined
in <linux/virtio_ids.h>.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Sasha Levin 43835ac90f kvm tools: Rename 'self' variables
Give proper names to vars named 'self'.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Sasha Levin b8f43678fa kvm tools: Use definitions from kernel headers
Instead of redefining virtio pci constants (or not using them at all), use
constants from kernel header.

Acked-and-tested-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Sasha Levin aaf0b4453e kvm tools: Prevent PFN wraparound
queue->pfn may be used to point at addresses larger
than 32 bit.
Prevent a wraparound when shifting it left.

Acked-and-tested-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Asias He 20f0438a95 kvm tools: Fix virtio console hangs by removing IRQ injection for tx path
As virtio spec says:

"""
 Because this is high importance and low bandwidth, the current Linux
 implementation polls for the buffer to be used, rather than waiting
 for an interrupt, simplifying the implementation signicantly.
"""

drivers/char/virtio_console.c
 send_buf() {
 ...
	/* Tell Host to go! */
	virtqueue_kick(out_vq);
 ...
        while (!virtqueue_get_buf(out_vq, &len))
                cpu_relax();
 ...
 }

The console hangs can simply be reproduced by yes command which
gives tremendous console IOs and IRQs.

[   16.786440] irq 4: nobody cared (try booting with the "irqpoll" option)
[   16.786440] Pid: 1437, comm: yes Tainted: G        W 2.6.39-rc6+ #56
[   16.786440] Call Trace:
[   16.786440]  [<c16578eb>] __report_bad_irq+0x30/0x89
[   16.786440]  [<c10980e6>] note_interrupt+0x118/0x17a
[   16.786440]  [<c1096e7d>] handle_irq_event_percpu+0x168/0x179
[   16.786440]  [<c1096eba>] handle_irq_event+0x2c/0x46
[   16.786440]  [<c1098516>] ? unmask_irq+0x1e/0x1e
[   16.786440]  [<c1098566>] handle_level_irq+0x50/0x6e
[   16.786440]  <IRQ>  [<c102fa69>] ? do_IRQ+0x35/0x7f
[   16.786440]  [<c1665ea9>] ? common_interrupt+0x29/0x30
[   16.786440]  [<c16610d6>] ? _raw_spin_unlock_irqrestore+0x7/0x28
[   16.786440]  [<c1364f65>] ? hvc_write+0x88/0x9e
[   16.786440]  [<c1355500>] ? do_output_char+0x88/0x18a
[   16.786440]  [<c1355631>] ? process_output+0x2f/0x42
[   16.786440]  [<c1355af6>] ? n_tty_write+0x211/0x2dc
[   16.786440]  [<c1059d77>] ? try_to_wake_up+0x226/0x226
[   16.786440]  [<c13534a4>] ? tty_write+0x15e/0x1d1
[   16.786440]  [<c12c1644>] ? security_file_permission+0x22/0x26
[   16.786440]  [<c13558e5>] ? process_echoes+0x241/0x241
[   16.786440]  [<c10dd9d2>] ? vfs_write+0x84/0xd7
[   16.786440]  [<c1353346>] ? tty_write_lock+0x3d/0x3d
[   16.786440]  [<c10ddb92>] ? sys_write+0x3b/0x5d
[   16.786440]  [<c166594c>] ? sysenter_do_call+0x12/0x22
[   16.786440] handlers:
[   16.786440] [<c1351397>] (vp_interrupt+0x0/0x3a)
[   16.786440] Disabling IRQ #4

Tested-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Asias He 0ba84a57a0 kvm tools: Use virt_queue__trigger_irq() to trigger IRQ for virtio console
This patch uses IRQ injection mechanism introduced by
virt_queue__trigger_irq() which respect virtio IRQ status
and VRING_AVAIL_F_NO_INTERRUPT.

Signed-off-by: Asias He <asias.hejun@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Sasha Levin 2449f6e369 kvm tools: Convert virtio devices to use IRQ registry
Instead of using static IRQ/device data, register the device
upon initialization and use the assign parameters when issuing
IRQs.

Clean up static definitions of IRQs.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Cyrill Gorcunov 31638bca30 kvm tools: Gather Virtio-PCI constants into one place
It's better than have them sprinkled in.c files. Note
that pin for ring device is changed so it no longer shared
with block device (it is done in a sake of simplicity).

Also comment style if a bit tuned up in virtio-pci.h
just to be consistent.

Reported-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Pekka Enberg 407475bf94 kvm tools: Cleanup virtio code some more
This patch cleans up some more style problems in virtio code.

Cc: Asias He <asias.hejun@gmail.com>
Cc: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Prasad Joshi <prasadjoshi124@gmail.com>
Cc: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Sasha Levin a643306e20 kvm tools: virtio-console code cleanup
Clean coding style and naming within virtio-console.

Acked-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Sasha Levin 3fdf659d95 kvm tools: Abolishment of uint*_t types
Clean uint*_t type from the code.

Signed-off-by: Sasha Levin <levinsasha928@gmail.com>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00
Pekka Enberg 3c8d07a7be kvm tools: Move virtio drivers under virtio directory
This patch moves the virtio drivers under virtio directory.

Suggested-by: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Pekka Enberg <penberg@kernel.org>
2015-06-01 16:39:43 +01:00