kvm tools: Add optional parameter used in ioport callbacks
PATCH v2 1/8 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.
--- tools/kvm/include/kvm/ioport.h | 3 ++ tools/kvm/ioport.c | 54 +++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 14 deletions(-)
tools/kvm/include/kvm/ioport.h +3 -0
@@ -25,11 +25,14 @@ struct kvm;struct ioport_operations {bool (*io_in)(struct kvm *kvm,u16 port,void *data,int size,u32 count);bool (*io_out)(struct kvm *kvm,u16 port,void *data,int size,u32 count); +bool (*io_in_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param); +bool (*io_out_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param); };void ioport__setup_legacy (void );void ioport__register (u16 port,struct ioport_operations *ops,int count); +void ioport__register_param (u16 port,struct ioport_operations *ops,int count,void *param);static inline u8 ioport__read8 (u8 *data) {
tools/kvm/ioport.c +40 -14
82 lines not on this page · raw message
Signature · 1 line
1.7.5.rc3
PATCH v2 2/8 kvm tools: Add basic ioport dynamic allocation
Add a very simple allocation of ioports.
This prevents the need to coordinate ioports between different modules.
--- tools/kvm/include/kvm/ioport.h | 11 +++++++++-- tools/kvm/ioport.c | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-)
tools/kvm/include/kvm/ioport.h +9 -2
@@ -7,6 +7,9 @@/* some ports we reserve for own use */ #define IOPORT_DBG 0xe0 +#define IOPORT_START 0x6200 +#define IOPORT_SIZE 0x400 +#define IOPORT_VESA 0xa200#define IOPORT_VESA_SIZE 256#define IOPORT_VIRTIO_P9 0xb200/* Virtio 9P device */
@@ -20,6 +23,8 @@#define IOPORT_VIRTIO_RNG 0xf200/* Virtio network device */ #define IOPORT_VIRTIO_RNG_SIZE 256 +#define IOPORT_EMPTY USHRT_MAX +struct kvm ;struct ioport_operations {
@@ -31,8 +36,10 @@ struct ioport_operations {void ioport__setup_legacy (void ); -void ioport__register (u16 port,struct ioport_operations *ops,int count); -void ioport__register_param (u16 port,struct ioport_operations *ops,int count,void *param); +u16 ioport__register (u16 port,struct ioport_operations *ops,int count); +u16 ioport__register_param (u16 port,struct ioport_operations *ops,int count,void *param); + +u16 ioport__find_free_range (void );static inline u8 ioport__read8 (u8 *data) {
tools/kvm/ioport.c +34 -2
@@ -3,6 +3,7 @@#include "kvm/kvm.h" #include "kvm/util.h" #include "kvm/rbtree-interval.h" +#include "kvm/mutex.h" #include <linux/kvm.h> /* for KVM_EXIT_* */ #include <linux/types.h>
@@ -21,9 +22,23 @@ struct ioport_entry {void *param; }; +static u16 free_io_port_idx; +DEFINE_MUTEX (free_io_port_idx_lock);static struct rb_root ioport_tree =RB_ROOT ;bool ioport_debug; +static u16 ioport__find_free_port (void ) +{ +u16 free_port; + +mutex_lock (&free_io_port_idx_lock); + free_port =IOPORT_START + free_io_port_idx *IOPORT_SIZE ; + free_io_port_idx++; +mutex_unlock (&free_io_port_idx_lock); + +return free_port; +} +static struct ioport_entry *ioport_search (struct rb_root *root,u64 addr) {struct rb_int_node *node;
@@ -68,10 +83,13 @@ static struct ioport_operations dummy_write_only_ioport_ops = { .io_out = dummy_io_out, }; -void ioport__register (u16 port,struct ioport_operations *ops,int count) +u16 ioport__register (u16 port,struct ioport_operations *ops,int count) {struct ioport_entry *entry; +if (port ==IOPORT_EMPTY ) + port =ioport__find_free_port (); + entry =ioport_search (&ioport_tree, port);if (entry) {pr_warning ("ioport re-registered: %x" , port);
@@ -88,12 +106,17 @@ void ioport__register(u16 port, struct ioport_operations *ops, int count) };ioport_insert (&ioport_tree, entry); + +return port; } -void ioport__register_param (u16 port,struct ioport_operations *ops,int count,void *param) +u16 ioport__register_param (u16 port,struct ioport_operations *ops,int count,void *param) {struct ioport_entry *entry; +if (port ==IOPORT_EMPTY ) + port =ioport__find_free_port (); + entry =ioport_search (&ioport_tree, port);if (entry) {pr_warning ("ioport re-registered: %x" , port);
@@ -111,6 +134,8 @@ void ioport__register_param(u16 port, struct ioport_operations *ops, int count, };ioport_insert (&ioport_tree, entry); + +return port; }static const char *to_direction (int direction)
@@ -126,6 +151,13 @@ static void ioport_error(u16 port, void *data, int direction, int size, u32 counfprintf (stderr,"IO error: %s port=%x, size=%d, count=%u\n" ,to_direction (direction), port, size, count); } +u16 ioport__find_free_range (void ) +{ +static u16 cur_loc; + +return IOPORT_START + (cur_loc++ *IOPORT_SIZE ); +} +bool kvm__emulate_io (struct kvm *kvm,u16 port,void *data,int direction,int size,u32 count) {struct ioport_operations *ops;
Signature · 1 line
1.7.5.rc3
PATCH v2 3/8 kvm tools: Use ioport context to control blk devices
In reply to Sasha LevinSince ioports now has the ability to pass context to its callbacks, we can implement multiple blk devices more efficiently.
We can get a ptr to the 'current' blk dev on each ioport call, which means that we don't need to keep track of the blk device allocation and ioport distribution within the module.
The advantages are easier management of multiple blk devices and removal of any hardcoded limits to the amount of possible blk devices.
--- tools/kvm/include/kvm/ioport.h | 2 - tools/kvm/virtio/blk.c | 75 ++++++++++++++-------------------------- 2 files changed, 26 insertions(+), 51 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -2
@@ -14,8 +14,6 @@#define IOPORT_VESA_SIZE 256#define IOPORT_VIRTIO_P9 0xb200/* Virtio 9P device */ #define IOPORT_VIRTIO_P9_SIZE 256 -#define IOPORT_VIRTIO_BLK 0xc200/* Virtio block device */ -#define IOPORT_VIRTIO_BLK_SIZE 0x200#define IOPORT_VIRTIO_CONSOLE 0xd200/* Virtio console device */ #define IOPORT_VIRTIO_CONSOLE_SIZE 256#define IOPORT_VIRTIO_NET 0xe200/* Virtio network device */
tools/kvm/virtio/blk.c +26 -49
161 lines not on this page · raw message
Signature · 1 line
1.7.5.rc3
PATCH v2 4/8 kvm tools: Add support for multiple virtio-rng devices
In reply to Sasha LevinSince multiple hardware rng devices of the same type are currently unsupported by the kernel, this serves more as an example of a basic virtio driver under kvm tools and can be used to debug the PCI layer.
--- tools/kvm/include/kvm/ioport.h | 2 - tools/kvm/include/kvm/parse-options.h | 9 +++ tools/kvm/include/kvm/virtio-rng.h | 1 + tools/kvm/kvm-run.c | 8 ++- tools/kvm/virtio/rng.c | 126 ++++++++++++++++++++++----------- 5 files changed, 100 insertions(+), 46 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -2
@@ -18,8 +18,6 @@#define IOPORT_VIRTIO_CONSOLE_SIZE 256#define IOPORT_VIRTIO_NET 0xe200/* Virtio network device */ #define IOPORT_VIRTIO_NET_SIZE 256 -#define IOPORT_VIRTIO_RNG 0xf200/* Virtio network device */ -#define IOPORT_VIRTIO_RNG_SIZE 256#define IOPORT_EMPTY USHRT_MAX
tools/kvm/include/kvm/parse-options.h +9 -0
@@ -132,6 +132,15 @@ intptr_t defval; .help = (h) \ } +#define OPT_INCR (s, l, v, h) \ +{ \ + .type = OPTION_INCR, \ + .short_name = (s), \ + .long_name = (l), \ + .value = check_vtype(v, int *), \ + .help = (h) \ +} +#define OPT_GROUP (h) \ { \ .type = OPTION_GROUP, \
tools/kvm/include/kvm/virtio-rng.h +1 -0
@@ -4,5 +4,6 @@struct kvm ;void virtio_rng__init (struct kvm *kvm); +void virtio_rng__delete_all (struct kvm *kvm);#endif /* KVM__RNG_VIRTIO_H */
tools/kvm/kvm-run.c +5 -3
@@ -52,6 +52,7 @@ static __thread struct kvm_cpu *current_kvm_cpu;static u64 ram_size;static u8 image_count; +static int virtio_rng;static const char *kernel_cmdline;static const char *kernel_filename;static const char *vmlinux_filename;
@@ -66,7 +67,6 @@ static const char *script;static const char *virtio_9p_dir;static bool single_step;static bool readonly_image[MAX_DISK_IMAGES ]; -static bool virtio_rng;static bool vnc;extern bool ioport_debug;extern int active_console;
@@ -107,7 +107,7 @@ static const struct option options[] = {OPT_CALLBACK ('d' ,"disk" ,NULL ,"image" ,"Disk image" , img_name_parser),OPT_STRING ('\0' ,"console" , &console,"serial or virtio" ,"Console to use" ), -OPT_BOOLEAN ('\0' ,"rng" , &virtio_rng, +OPT_INCR ('\0' ,"rng" , &virtio_rng,"Enable virtio Random Number Generator" ),OPT_STRING ('\0' ,"kvm-dev" , &kvm_dev,"kvm-dev" ,"KVM device file" ), OPT_STRING('\0', "virtio-9p", &virtio_9p_dir, "root dir",
@@ -570,7 +570,8 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix)virtio_console__init (kvm);if (virtio_rng) -virtio_rng__init (kvm); +while (virtio_rng--) +virtio_rng__init (kvm);if (!network) network =DEFAULT_NETWORK ;
@@ -631,6 +632,7 @@ int kvm_cmd_run(int argc, const char **argv, const char *prefix) }virtio_blk__delete_all (kvm); +virtio_rng__delete_all (kvm);disk_image__close_all (kvm->disks, image_count);kvm__delete (kvm);
tools/kvm/virtio/rng.c +85 -41
244 lines not on this page · raw message
Signature · 1 line
1.7.5.rc3
PATCH v2 5/8 kvm tools: Use dynamic IO port allocation in vesa driver
In reply to Sasha Levin--- tools/kvm/hw/vesa.c | 7 +++---- tools/kvm/include/kvm/ioport.h | 2 -- 2 files changed, 3 insertions(+), 6 deletions(-)
tools/kvm/hw/vesa.c +3 -4
@@ -49,7 +49,6 @@ static struct pci_device_header vesa_pci_device = { .class = 0x030000, .subsys_vendor_id = PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET, .subsys_id = PCI_SUBSYSTEM_ID_VESA, - .bar[0] = IOPORT_VESA | PCI_BASE_ADDRESS_SPACE_IO, .bar[1] = VESA_MEM_ADDR | PCI_BASE_ADDRESS_SPACE_MEMORY, };
@@ -66,17 +65,17 @@ void vesa__init(struct kvm *kvm) {u8 dev, line, pin;pthread_t thread; +u16 vesa_base_addr;if (irq__register_device (PCI_DEVICE_ID_VESA , &dev, &pin, &line) <0 )return ; vesa_pci_device.irq_pin = pin; vesa_pci_device.irq_line = line; - + vesa_base_addr =ioport__register (IOPORT_EMPTY , &vesa_io_ops,IOPORT_SIZE ); + vesa_pci_device.bar[0 ] = vesa_base_addr |PCI_BASE_ADDRESS_SPACE_IO ;pci__register (&vesa_pci_device, dev); -ioport__register (IOPORT_VESA , &vesa_io_ops,IOPORT_VESA_SIZE ); -kvm__register_mmio (VESA_MEM_ADDR ,VESA_MEM_SIZE , &vesa_mmio_callback);pthread_create (&thread,NULL , vesa__dovnc, kvm);
tools/kvm/include/kvm/ioport.h +0 -2
@@ -10,8 +10,6 @@#define IOPORT_START 0x6200#define IOPORT_SIZE 0x400 -#define IOPORT_VESA 0xa200 -#define IOPORT_VESA_SIZE 256#define IOPORT_VIRTIO_P9 0xb200/* Virtio 9P device */ #define IOPORT_VIRTIO_P9_SIZE 256#define IOPORT_VIRTIO_CONSOLE 0xd200/* Virtio console device */
Signature · 1 line
1.7.5.rc3
PATCH v2 6/8 kvm tools: Use dynamic IO port allocation in 9p driver
In reply to Sasha Levin--- tools/kvm/include/kvm/ioport.h | 2 -- tools/kvm/virtio/9p.c | 12 +++++++----- 2 files changed, 7 insertions(+), 7 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -2
@@ -10,8 +10,6 @@#define IOPORT_START 0x6200#define IOPORT_SIZE 0x400 -#define IOPORT_VIRTIO_P9 0xb200/* Virtio 9P device */ -#define IOPORT_VIRTIO_P9_SIZE 256#define IOPORT_VIRTIO_CONSOLE 0xd200/* Virtio console device */ #define IOPORT_VIRTIO_CONSOLE_SIZE 256#define IOPORT_VIRTIO_NET 0xe200/* Virtio network device */
tools/kvm/virtio/9p.c +7 -5
@@ -50,7 +50,6 @@ static struct pci_device_header virtio_p9_pci_device = { .class = 0x010000, .subsys_vendor_id = PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET, .subsys_id = VIRTIO_ID_9P, - .bar[0] = IOPORT_VIRTIO_P9 | PCI_BASE_ADDRESS_SPACE_IO, };struct p9_dev {
@@ -59,6 +58,7 @@ struct p9_dev {u16 config_vector;u32 features;struct virtio_9p_config *config; +u16 base_addr;/* virtio queue */ u16 queue_selector;
@@ -96,7 +96,7 @@ static bool virtio_p9_pci_io_in(struct kvm *kvm, u16 port, void *data, int size,unsigned long offset;bool ret = true; - offset = port -IOPORT_VIRTIO_P9 ; + offset = port - p9dev.base_addr;switch (offset) {case VIRTIO_PCI_HOST_FEATURES :
@@ -584,7 +584,7 @@ static bool virtio_p9_pci_io_out(struct kvm *kvm, u16 port, void *data, int sizeunsigned long offset;bool ret = true; - offset = port -IOPORT_VIRTIO_P9 ; + offset = port - p9dev.base_addr;switch (offset) {case VIRTIO_MSI_QUEUE_VECTOR :
@@ -636,6 +636,7 @@ void virtio_9p__init(struct kvm *kvm, const char *root) {u8 pin, line, dev;u32 i, root_len; +u16 p9_base_addr; p9dev.config =calloc (1 ,sizeof (*p9dev.config) +sizeof (VIRTIO_P9_TAG ));if (p9dev.config ==NULL )
@@ -662,7 +663,8 @@ void virtio_9p__init(struct kvm *kvm, const char *root) virtio_p9_pci_device.irq_pin = pin; virtio_p9_pci_device.irq_line = line; + p9_base_addr =ioport__register (IOPORT_EMPTY , &virtio_p9_io_ops,IOPORT_SIZE ); + virtio_p9_pci_device.bar[0 ] = p9_base_addr |PCI_BASE_ADDRESS_SPACE_IO ; + p9dev.base_addr = p9_base_addr;pci__register (&virtio_p9_pci_device, dev); - -ioport__register (IOPORT_VIRTIO_P9 , &virtio_p9_io_ops,IOPORT_VIRTIO_P9_SIZE ); }
Signature · 1 line
1.7.5.rc3
PATCH v2 7/8 kvm tools: Use dynamic IO port allocation in virtio-console
In reply to Sasha Levin--- tools/kvm/include/kvm/ioport.h | 2 -- tools/kvm/virtio/console.c | 11 +++++++---- 2 files changed, 7 insertions(+), 6 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -2
@@ -10,8 +10,6 @@#define IOPORT_START 0x6200#define IOPORT_SIZE 0x400 -#define IOPORT_VIRTIO_CONSOLE 0xd200/* Virtio console device */ -#define IOPORT_VIRTIO_CONSOLE_SIZE 256#define IOPORT_VIRTIO_NET 0xe200/* Virtio network device */ #define IOPORT_VIRTIO_NET_SIZE 256
tools/kvm/virtio/console.c +7 -4
@@ -36,7 +36,6 @@ static struct pci_device_header virtio_console_pci_device = { .class = 0x078000, .subsys_vendor_id = PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET, .subsys_id = VIRTIO_ID_CONSOLE, - .bar[0] = IOPORT_VIRTIO_CONSOLE | PCI_BASE_ADDRESS_SPACE_IO, };struct con_dev {
@@ -50,6 +49,7 @@ struct con_dev {u8 status;u8 isr;u16 queue_selector; +u16 base_addr;void *jobs[VIRTIO_CONSOLE_NUM_QUEUES ]; };
@@ -113,7 +113,7 @@ static bool virtio_console_pci_io_device_specific_in(void *data, unsigned long ostatic bool virtio_console_pci_io_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) { -unsigned long offset = port -IOPORT_VIRTIO_CONSOLE ; +unsigned long offset = port - cdev.base_addr;bool ret = true;mutex_lock (&cdev.mutex);
@@ -181,7 +181,7 @@ static void virtio_console_handle_callback(struct kvm *kvm, void *param)static bool virtio_console_pci_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) { -unsigned long offset = port -IOPORT_VIRTIO_CONSOLE ; +unsigned long offset = port - cdev.base_addr;bool ret = true;mutex_lock (&cdev.mutex);
@@ -243,12 +243,15 @@ static struct ioport_operations virtio_console_io_ops = {void virtio_console__init (struct kvm *kvm) {u8 dev, line, pin; +u16 console_base_addr;if (irq__register_device (VIRTIO_ID_CONSOLE , &dev, &pin, &line) <0 )return ; virtio_console_pci_device.irq_pin = pin; virtio_console_pci_device.irq_line = line; + console_base_addr =ioport__register (IOPORT_EMPTY , &virtio_console_io_ops,IOPORT_SIZE ); + virtio_console_pci_device.bar[0 ] = console_base_addr |PCI_BASE_ADDRESS_SPACE_IO ; + cdev.base_addr = console_base_addr;pci__register (&virtio_console_pci_device, dev); -ioport__register (IOPORT_VIRTIO_CONSOLE , &virtio_console_io_ops,IOPORT_VIRTIO_CONSOLE_SIZE ); }
Signature · 1 line
1.7.5.rc3
PATCH v2 8/8 kvm tools: Use dynamic IO port allocation in virtio-net
In reply to Sasha Levin--- tools/kvm/include/kvm/ioport.h | 3 --- tools/kvm/virtio/net.c | 12 ++++++++---- 2 files changed, 8 insertions(+), 7 deletions(-)
tools/kvm/include/kvm/ioport.h +0 -3
@@ -10,9 +10,6 @@#define IOPORT_START 0x6200#define IOPORT_SIZE 0x400 -#define IOPORT_VIRTIO_NET 0xe200/* Virtio network device */ -#define IOPORT_VIRTIO_NET_SIZE 256 -#define IOPORT_EMPTY USHRT_MAXstruct kvm ;
tools/kvm/virtio/net.c +8 -4
@@ -37,7 +37,6 @@ static struct pci_device_header pci_header = { .class = 0x020000, .subsys_vendor_id = PCI_SUBSYSTEM_VENDOR_ID_REDHAT_QUMRANET, .subsys_id = VIRTIO_ID_NET, - .bar[0] = IOPORT_VIRTIO_NET | PCI_BASE_ADDRESS_SPACE_IO, };struct net_device {
@@ -51,6 +50,7 @@ struct net_device {u8 status;u8 isr;u16 queue_selector; +u16 base_addr;pthread_t io_rx_thread;pthread_mutex_t io_rx_lock;
@@ -166,7 +166,7 @@ static bool virtio_net_pci_io_device_specific_in(void *data, unsigned long offsestatic bool virtio_net_pci_io_in (struct kvm *kvm,u16 port,void *data,int size,u32 count) { -unsigned long offset = port -IOPORT_VIRTIO_NET ; +unsigned long offset = port - ndev.base_addr;bool ret = true;mutex_lock (&ndev.mutex);
@@ -230,7 +230,7 @@ static void virtio_net_handle_callback(struct kvm *kvm, u16 queue_index)static bool virtio_net_pci_io_out (struct kvm *kvm,u16 port,void *data,int size,u32 count) { -unsigned long offset = port -IOPORT_VIRTIO_NET ; +unsigned long offset = port - ndev.base_addr;bool ret = true;mutex_lock (&ndev.mutex);
@@ -387,14 +387,18 @@ void virtio_net__init(const struct virtio_net_parameters *params) {if (virtio_net__tap_init (params)) {u8 dev, line, pin; +u16 net_base_addr;if (irq__register_device (VIRTIO_ID_NET , &dev, &pin, &line) <0 )return ; pci_header.irq_pin = pin; pci_header.irq_line = line; + net_base_addr =ioport__register (IOPORT_EMPTY , &virtio_net_io_ops,IOPORT_SIZE ); + pci_header.bar[0 ] = net_base_addr |PCI_BASE_ADDRESS_SPACE_IO ; + ndev.base_addr = net_base_addr; +pci__register (&pci_header, dev); -ioport__register (IOPORT_VIRTIO_NET , &virtio_net_io_ops,IOPORT_VIRTIO_NET_SIZE );virtio_net__io_thread_init (params->kvm); }
Signature · 1 line
1.7.5.rc3
Quoted 17 lines · Sasha Levin
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 <[email protected]>--- tools/kvm/include/kvm/ioport.h | 3 ++ tools/kvm/ioport.c | 54 +++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 14 deletions(-)tools/kvm/include/kvm/ioport.h +0
-0
tools/kvm/include/kvm/ioport.h struct kvm; · line 29
Quoted 5 lines
tools/kvm/include/kvm/ioport.h +2
-0@@ -25,11 +25,14 @@ struct kvm;struct ioport_operations {bool (*io_in)(struct kvm *kvm,u16 port,void *data,int size,u32 count);bool (*io_out)(struct kvm *kvm,u16 port,void *data,int size,u32 count); +bool (*io_in_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param); +bool (*io_out_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param);
So why not make that 'param' unconditional for io_in and io_out and just pass NULL if it's not needed?
Quoted 13 lines · Pekka Enberg
Quoted 12 lines · Sasha Levin
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 <[email protected]>--- tools/kvm/include/kvm/ioport.h | 3 ++ tools/kvm/ioport.c | 54 +++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 14 deletions(-)
Quoted 14 lines
tools/kvm/include/kvm/ioport.h struct kvm; · line 29
Quoted 10 lines
tools/kvm/include/kvm/ioport.h +2
-0@@ -25,11 +25,14 @@ struct kvm;struct ioport_operations {bool (*io_in)(struct kvm *kvm,u16 port,void *data,int size,u32 count);bool (*io_out)(struct kvm *kvm,u16 port,void *data,int size,u32 count); +bool (*io_in_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param); +bool (*io_out_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param);So why not make that 'param' unconditional for
io_inandio_outand just pass NULL if it's not needed?
I've wanted to keep the original interface clean, Most of the IO port users don't (and probably won't) require a parameter.
Signature · 1 line
Sasha.
Quoted 29 lines · Sasha Levin
Quoted 13 lines · Pekka Enberg
Quoted 12 lines · Sasha Levin
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 <[email protected]>---
tools/kvm/include/kvm/ioport.h | 3 ++
tools/kvm/ioport.c | 54 +++++++++++++++++++++++++++++----------2 files changed, 43 insertions(+), 14 deletions(-)Quoted 14 lines
tools/kvm/include/kvm/ioport.h struct kvm; · line 29
Quoted 10 lines
tools/kvm/include/kvm/ioport.h +2
-0@@ -25,11 +25,14 @@ struct kvm; struct ioport_operations { bool (*io_in)(struct kvm *kvm, u16 port, void *data, int size, u32 count); bool (*io_out)(struct kvm *kvm, u16 port, void *data, int size, u32 count); + bool (*io_in_param)(struct kvm *kvm, u16 port, void *data, int size, u32 count, void *param); + bool (*io_out_param)(struct kvm *kvm, u16 port, void *data, int size, u32 count, void *param);So why not make that 'param' unconditional for
io_inandio_outand just pass NULL if it's not needed?
Quoted 2 lines
I've wanted to keep the original interface clean, Most of the IO port users don't (and probably won't) require a parameter.
Well now struct ioport_operations isn't very clean is it - or the code that needs to determine which function pointer to call?-)
Quoted 30 lines · Pekka Enberg
Quoted 29 lines · Sasha Levin
Quoted 27 lines · Pekka Enberg
Quoted 17 lines · Sasha Levin
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 <[email protected]>--- tools/kvm/include/kvm/ioport.h | 3 ++ tools/kvm/ioport.c | 54 +++++++++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 14 deletions(-)tools/kvm/include/kvm/ioport.h +0
-0tools/kvm/include/kvm/ioport.h struct kvm; · line 29
Quoted 5 lines
tools/kvm/include/kvm/ioport.h +2
-0@@ -25,11 +25,14 @@ struct kvm;struct ioport_operations {bool (*io_in)(struct kvm *kvm,u16 port,void *data,int size,u32 count);bool (*io_out)(struct kvm *kvm,u16 port,void *data,int size,u32 count); +bool (*io_in_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param); +bool (*io_out_param)(struct kvm *kvm,u16 port,void *data,int size,u32 count,void *param);So why not make that 'param' unconditional for
io_inandio_outand just pass NULL if it's not needed?
Quoted 5 lines
Quoted 2 lines
I've wanted to keep the original interface clean, Most of the IO port users don't (and probably won't) require a parameter.
Well now struct
ioport_operationsisn't very clean is it - or the code that needs to determine which function pointer to call?-)
struct ioport_operations is a bit more messy, but it's one spot instead of adding a 'parameter' to each module that doesn't really need it.
My assumption is that most ioport users now and in the future won't need it, it just solves several special cases more easily (multiple devices which share same handling functions).
Signature · 1 line
Sasha.
Quoted 6 lines · Sasha Levin
Quoted 5 lines
Quoted 2 lines
I've wanted to keep the original interface clean, Most of the IO port users don't (and probably won't) require a parameter.
Well now struct
ioport_operationsisn't very clean is it - or the code that needs to determine which function pointer to call?-)
Quoted 6 lines
struct
ioport_operationsis a bit more messy, but it's one spot instead of adding a 'parameter' to each module that doesn't really need it.My assumption is that most ioport users now and in the future won't need it, it just solves several special cases more easily (multiple devices which share same handling functions).
Hey, that's not an excuse to make struct ioport_operations 'bit messy'! Look at any kernel code that uses ops like we do here and you will see we don't do APIs like this.
One option here is to rename 'struct ioport_entry' to 'struct ioport' and pass a pointer to that as the first argument to all of the ops. That's what most APIs in the kernel do anyway.
Pekka
Quoted 14 lines · Pekka Enberg
Quoted 6 lines · Sasha Levin
Quoted 5 lines
Quoted 2 lines
I've wanted to keep the original interface clean, Most of the IO port users don't (and probably won't) require a parameter.
Well now struct
ioport_operationsisn't very clean is it - or the code that needs to determine which function pointer to call?-)Quoted 6 lines
struct
ioport_operationsis a bit more messy, but it's one spot instead of adding a 'parameter' to each module that doesn't really need it.My assumption is that most ioport users now and in the future won't need it, it just solves several special cases more easily (multiple devices which share same handling functions).
Quoted 7 lines
Hey, that's not an excuse to make struct
ioport_operations'bit messy'! Look at any kernel code that uses ops like we do here and you will see we don't do APIs like this.One option here is to rename 'struct
ioport_entry' to 'struct ioport' and pass a pointer to that as the first argument to all of the ops. That's what most APIs in the kernel do anyway.
Why do it like that? this way users of the callback functions will need to know the internal structure of struct ioport_entry.
Signature · 1 line
Sasha.
Quoted 6 lines · Sasha Levin
Quoted 3 lines
One option here is to rename 'struct
ioport_entry' to 'struct ioport' and pass a pointer to that as the first argument to all of the ops. That's what most APIs in the kernel do anyway.Why do it like that? this way users of the callback functions will need to know the internal structure of struct
ioport_entry.
Look at 'struct inode' or similar data structure in the kernel. That's how we do it. You can then also do s/params/priv/.
Hi Sasha,
Quoted 6 lines · Sasha Levin
Quoted 6 lines
Quoted 3 lines
One option here is to rename 'struct
ioport_entry' to 'struct ioport' and pass a pointer to that as the first argument to all of the ops. That's what most APIs in the kernel do anyway.Why do it like that? this way users of the callback functions will need to know the internal structure of struct
ioport_entry.
Quoted 2 lines · Pekka Enberg
Look at 'struct inode' or similar data structure in the kernel. That's how we do it. You can then also do s/params/priv/.
Btw, the whole notion of 'internal structure' for structs in C code is a pretty broken concept. In most cases, you just end up passing untyped fragments of the data to callers which makes following the data flow in code difficult. Passing 'struct ioport' down to the code makes the code more obvious and readable.
Encapsulation is important but emulating that with hiding structs in .c files isn't helpful at all. Face it, there's no proper support for that in C so you just need to rely on conventions to do it.
Pekka