QEMU源码全解析 —— virtio(20)

接前一篇文章:

上回书重点解析了virtio_pci_modern_probe函数。再来回顾一下其中相关的数据结构:

  • struct virtio_pci_device

struct virtio_pci_device的定义在Linux内核源码/drivers/virtio/virtio_pci_common.h中,如下:

/* Our device structure */
struct virtio_pci_device {struct virtio_device vdev;struct pci_dev *pci_dev;union {struct virtio_pci_legacy_device ldev;struct virtio_pci_modern_device mdev;};bool is_legacy;/* Where to read and clear interrupt */u8 __iomem *isr;/* a list of queues so we can dispatch IRQs */spinlock_t lock;struct list_head virtqueues;/* array of all queues for house-keeping */struct virtio_pci_vq_info **vqs;/* MSI-X support */int msix_enabled;int intx_enabled;cpumask_var_t *msix_affinity_masks;/* Name strings for interrupts. This size should be enough,* and I'm too lazy to allocate each name separately. */char (*msix_names)[256];/* Number of available vectors */unsigned int msix_vectors;/* Vectors allocated, excluding per-vq vectors if any */unsigned int msix_used_vectors;/* Whether we have vector per vq */bool per_vq_vectors;struct virtqueue *(*setup_vq)(struct virtio_pci_device *vp_dev,struct virtio_pci_vq_info *info,unsigned int idx,void (*callback)(struct virtqueue *vq),const char *name,bool ctx,u16 msix_vec);void (*del_vq)(struct virtio_pci_vq_info *info);u16 (*config_vector)(struct virtio_pci_device *vp_dev, u16 vector);
};

virtio_pci_modern_probe执行完成后,相关数据结构如下图所示:

回到virtio_pci_probe函数。在Linux内核源码/drivers/virtio/virtio_pci_common.c中,代码如下:

static int virtio_pci_probe(struct pci_dev *pci_dev,const struct pci_device_id *id)
{struct virtio_pci_device *vp_dev, *reg_dev = NULL;int rc;/* allocate our structure and fill it out */vp_dev = kzalloc(sizeof(struct virtio_pci_device), GFP_KERNEL);if (!vp_dev)return -ENOMEM;pci_set_drvdata(pci_dev, vp_dev);vp_dev->vdev.dev.parent = &pci_dev->dev;vp_dev->vdev.dev.release = virtio_pci_release_dev;vp_dev->pci_dev = pci_dev;INIT_LIST_HEAD(&vp_dev->virtqueues);spin_lock_init(&vp_dev->lock);/* enable the device */rc = pci_enable_device(pci_dev);if (rc)goto err_enable_device;if (force_legacy) {rc = virtio_pci_legacy_probe(vp_dev);/* Also try modern mode if we can't map BAR0 (no IO space). */if (rc == -ENODEV || rc == -ENOMEM)rc = virtio_pci_modern_probe(vp_dev);if (rc)goto err_probe;} else {rc = virtio_pci_modern_probe(vp_dev);if (rc == -ENODEV)rc = virtio_pci_legacy_probe(vp_dev);if (rc)goto err_probe;}pci_set_master(pci_dev);rc = register_virtio_device(&vp_dev->vdev);reg_dev = vp_dev;if (rc)goto err_register;return 0;err_register:if (vp_dev->is_legacy)virtio_pci_legacy_remove(vp_dev);elsevirtio_pci_modern_remove(vp_dev);
err_probe:pci_disable_device(pci_dev);
err_enable_device:if (reg_dev)put_device(&vp_dev->vdev.dev);elsekfree(vp_dev);return rc;
}

接QEMU源码全解析 —— virtio(18)中的内容,前文书讲到了virtio_pci_probe函数的第5步,

“(5)调用virtio_pci_legacy_probe或者virtio_pci_modern_probe函数来初始化该PCI设备对应的virtio设备。”,继续往下进行。

(6)virtio_pci_probe函数在调用virtio_pci_modern_probe函数之后,接下来会调用register_virtio_device。代码片段如下:

	rc = register_virtio_device(&vp_dev->vdev);reg_dev = vp_dev;if (rc)goto err_register;

register_virtio_device函数在Linux内核源码/drivers/virtio/virtio.c中,代码如下:

/*** register_virtio_device - register virtio device* @dev        : virtio device to be registered** On error, the caller must call put_device on &@dev->dev (and not kfree),* as another code path may have obtained a reference to @dev.** Returns: 0 on suceess, -error on failure*/
int register_virtio_device(struct virtio_device *dev)
{int err;dev->dev.bus = &virtio_bus;device_initialize(&dev->dev);/* Assign a unique device index and hence name. */err = ida_alloc(&virtio_index_ida, GFP_KERNEL);if (err < 0)goto out;dev->index = err;err = dev_set_name(&dev->dev, "virtio%u", dev->index);if (err)goto out_ida_remove;err = virtio_device_of_init(dev);if (err)goto out_ida_remove;spin_lock_init(&dev->config_lock);dev->config_enabled = false;dev->config_change_pending = false;INIT_LIST_HEAD(&dev->vqs);spin_lock_init(&dev->vqs_list_lock);/* We always start by resetting the device, in case a previous* driver messed it up.  This also tests that code path a little. */virtio_reset_device(dev);/* Acknowledge that we've seen the device. */virtio_add_status(dev, VIRTIO_CONFIG_S_ACKNOWLEDGE);/** device_add() causes the bus infrastructure to look for a matching* driver.*/err = device_add(&dev->dev);if (err)goto out_of_node_put;return 0;out_of_node_put:of_node_put(dev->dev.of_node);
out_ida_remove:ida_free(&virtio_index_ida, dev->index);
out:virtio_add_status(dev, VIRTIO_CONFIG_S_FAILED);return err;
}
EXPORT_SYMBOL_GPL(register_virtio_device);

前文已提到,vp_dev->vdev的类型为struct virtio_device,而传给register_virtio_device函数的实参为vp_dev->vdev的地址,即&vp_dev->vdev。从函数名以及参数类型就能看出,register_virtio_device函数的作用是将一个virtio device注册到系统中。具体步骤如下:

(1)设置virtio设备的Bus为virtio_bus。代码片段如下:

    dev->dev.bus = &virtio_bus;

virtio_bus在系统初始化的时候会注册到系统中。

virtio_bus在Linux内核源码/drivers/virtio/virtio.c中初始化,代码如下:

static struct bus_type virtio_bus = {.name  = "virtio",.match = virtio_dev_match,.dev_groups = virtio_dev_groups,.uevent = virtio_uevent,.probe = virtio_dev_probe,.remove = virtio_dev_remove,
};int register_virtio_driver(struct virtio_driver *driver)
{/* Catch this early. */BUG_ON(driver->feature_table_size && !driver->feature_table);driver->driver.bus = &virtio_bus;return driver_register(&driver->driver);
}
EXPORT_SYMBOL_GPL(register_virtio_driver);

在系统初始化的时候,通过register_virtio_driver函数注册到系统中。

(2)设置virtio设备的名字为类似"virtio0"、"virtio1"的字符串。代码片段如下:

    /* Assign a unique device index and hence name. */err = ida_alloc(&virtio_index_ida, GFP_KERNEL);if (err < 0)goto out;dev->index = err;err = dev_set_name(&dev->dev, "virtio%u", dev->index);if (err)goto out_ida_remove;

dev_set_name函数在Linux内核源码/drivers/base/core.c中,代码如下:

/*** dev_set_name - set a device name* @dev: device* @fmt: format string for the device's name*/
int dev_set_name(struct device *dev, const char *fmt, ...)
{va_list vargs;int err;va_start(vargs, fmt);err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);va_end(vargs);return err;
}
EXPORT_SYMBOL_GPL(dev_set_name);

(3)然后调用virtio_reset_device函数重置设备。代码片段如下:

    /* We always start by resetting the device, in case a previous* driver messed it up.  This also tests that code path a little. */virtio_reset_device(dev);

(4)最后,调用device_add函数,将设备注册到系统中。代码片段如下:

    /** device_add() causes the bus infrastructure to look for a matching* driver.*/err = device_add(&dev->dev);if (err)goto out_of_node_put;

这里,老版本代码中是调用的是device_register函数。device_register函数跟设备驱动相关性较大,在此简单介绍一下其作用。

device_register函数在Linux内核源码/drivers/base/core.c中,代码如下:

/*** device_register - register a device with the system.* @dev: pointer to the device structure** This happens in two clean steps - initialize the device* and add it to the system. The two steps can be called* separately, but this is the easiest and most common.* I.e. you should only call the two helpers separately if* have a clearly defined need to use and refcount the device* before it is added to the hierarchy.** For more information, see the kerneldoc for device_initialize()* and device_add().** NOTE: _Never_ directly free @dev after calling this function, even* if it returned an error! Always use put_device() to give up the* reference initialized in this function instead.*/
int device_register(struct device *dev)
{device_initialize(dev);return device_add(dev);
}
EXPORT_SYMBOL_GPL(device_register);

device_register函数向系统注册一个设备。其分为两个简单的步骤——初始化设备(device_initialize(dev))并将其添加到系统中(device_add(dev))。这两个步骤可以分别调用,但放在一起即使用device_register函数是最简单和最常见的。例如,如果有明确的需求在其添加到层级之前使用和重新计数设备,那么应该分别独立地调用这两个助手(函数)。

从此处的代码就可以知道,老版本的内核代码中确实是直接调用了device_register函数,而新版本内核代码在此处则是在register_virtio_device函数的前边先调用了device_initialize(&dev->dev),而后在这里调用了device_add(&dev->dev)。即采用了分开调用的方式。

device_register函数会调用device_add函数,将设备加到系统中,并且会发送一个uevent消息到用户空间,这个uevent消息中包含了virtio设备的vendor id、device id。 udev接收到此消息之后,会加载virtio设备对应的驱动。然后,device_add函数会调用bus_probe_device函数,最终调用到Bus的probe函数和设备的probe函数,也就是virtio_dev_probe函数和virtballoon_probe函数。

device_add函数也在Linux内核源码/drivers/base/core.c中,就在device_register函数上边,代码如下:

/*** device_add - add device to device hierarchy.* @dev: device.** This is part 2 of device_register(), though may be called* separately _iff_ device_initialize() has been called separately.** This adds @dev to the kobject hierarchy via kobject_add(), adds it* to the global and sibling lists for the device, then* adds it to the other relevant subsystems of the driver model.** Do not call this routine or device_register() more than once for* any device structure.  The driver model core is not designed to work* with devices that get unregistered and then spring back to life.* (Among other things, it's very hard to guarantee that all references* to the previous incarnation of @dev have been dropped.)  Allocate* and register a fresh new struct device instead.** NOTE: _Never_ directly free @dev after calling this function, even* if it returned an error! Always use put_device() to give up your* reference instead.** Rule of thumb is: if device_add() succeeds, you should call* device_del() when you want to get rid of it. If device_add() has* *not* succeeded, use *only* put_device() to drop the reference* count.*/
int device_add(struct device *dev)
{struct subsys_private *sp;struct device *parent;struct kobject *kobj;struct class_interface *class_intf;int error = -EINVAL;struct kobject *glue_dir = NULL;dev = get_device(dev);if (!dev)goto done;if (!dev->p) {error = device_private_init(dev);if (error)goto done;}/** for statically allocated devices, which should all be converted* some day, we need to initialize the name. We prevent reading back* the name, and force the use of dev_name()*/if (dev->init_name) {error = dev_set_name(dev, "%s", dev->init_name);dev->init_name = NULL;}if (dev_name(dev))error = 0;/* subsystems can specify simple device enumeration */else if (dev->bus && dev->bus->dev_name)error = dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);elseerror = -EINVAL;if (error)goto name_error;pr_debug("device: '%s': %s\n", dev_name(dev), __func__);parent = get_device(dev->parent);kobj = get_device_parent(dev, parent);if (IS_ERR(kobj)) {error = PTR_ERR(kobj);goto parent_error;}if (kobj)dev->kobj.parent = kobj;/* use parent numa_node */if (parent && (dev_to_node(dev) == NUMA_NO_NODE))set_dev_node(dev, dev_to_node(parent));/* first, register with generic layer. *//* we require the name to be set before, and pass NULL */error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);if (error) {glue_dir = kobj;goto Error;}/* notify platform of device entry */device_platform_notify(dev);error = device_create_file(dev, &dev_attr_uevent);if (error)goto attrError;error = device_add_class_symlinks(dev);if (error)goto SymlinkError;error = device_add_attrs(dev);if (error)goto AttrsError;error = bus_add_device(dev);if (error)goto BusError;error = dpm_sysfs_add(dev);if (error)goto DPMError;device_pm_add(dev);if (MAJOR(dev->devt)) {error = device_create_file(dev, &dev_attr_dev);if (error)goto DevAttrError;error = device_create_sys_dev_entry(dev);if (error)goto SysEntryError;devtmpfs_create_node(dev);}/* Notify clients of device addition.  This call must come* after dpm_sysfs_add() and before kobject_uevent().*/bus_notify(dev, BUS_NOTIFY_ADD_DEVICE);kobject_uevent(&dev->kobj, KOBJ_ADD);/** Check if any of the other devices (consumers) have been waiting for* this device (supplier) to be added so that they can create a device* link to it.** This needs to happen after device_pm_add() because device_link_add()* requires the supplier be registered before it's called.** But this also needs to happen before bus_probe_device() to make sure* waiting consumers can link to it before the driver is bound to the* device and the driver sync_state callback is called for this device.*/if (dev->fwnode && !dev->fwnode->dev) {dev->fwnode->dev = dev;fw_devlink_link_device(dev);}bus_probe_device(dev);/** If all driver registration is done and a newly added device doesn't* match with any driver, don't block its consumers from probing in* case the consumer device is able to operate without this supplier.*/if (dev->fwnode && fw_devlink_drv_reg_done && !dev->can_match)fw_devlink_unblock_consumers(dev);if (parent)klist_add_tail(&dev->p->knode_parent,&parent->p->klist_children);sp = class_to_subsys(dev->class);if (sp) {mutex_lock(&sp->mutex);/* tie the class to the device */klist_add_tail(&dev->p->knode_class, &sp->klist_devices);/* notify any interfaces that the device is here */list_for_each_entry(class_intf, &sp->interfaces, node)if (class_intf->add_dev)class_intf->add_dev(dev);mutex_unlock(&sp->mutex);subsys_put(sp);}
done:put_device(dev);return error;SysEntryError:if (MAJOR(dev->devt))device_remove_file(dev, &dev_attr_dev);DevAttrError:device_pm_remove(dev);dpm_sysfs_remove(dev);DPMError:dev->driver = NULL;bus_remove_device(dev);BusError:device_remove_attrs(dev);AttrsError:device_remove_class_symlinks(dev);SymlinkError:device_remove_file(dev, &dev_attr_uevent);attrError:device_platform_notify_remove(dev);kobject_uevent(&dev->kobj, KOBJ_REMOVE);glue_dir = get_glue_dir(dev);kobject_del(&dev->kobj);Error:cleanup_glue_dir(dev, glue_dir);
parent_error:put_device(parent);
name_error:kfree(dev->p);dev->p = NULL;goto done;
}
EXPORT_SYMBOL_GPL(device_add);

其中的代码片段:

    /* Notify clients of device addition.  This call must come* after dpm_sysfs_add() and before kobject_uevent().*/bus_notify(dev, BUS_NOTIFY_ADD_DEVICE);kobject_uevent(&dev->kobj, KOBJ_ADD);

    bus_probe_device(dev);

就是上边所讲到的:

device_register函数会调用device_add函数,将设备加到系统中,并且会发送一个uevent消息到用户空间,这个uevent消息中包含了virtio设备的vendor id、device id。 udev接收到此消息之后,会加载virtio设备对应的驱动。

然后,device_add函数会调用bus_probe_device函数,最终调用到Bus的probe函数和设备的probe函数,也就是virtio_dev_probe函数和virtballoon_probe函数。

欲知后事如何,且看下回分解。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/260870.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

智慧驿站_智慧文旅驿站_轻松的驿站智慧公厕_5G智慧公厕驿站_5G模块化智慧公厕

多功能城市智慧驿站是在智慧城市建设背景下&#xff0c;所涌现的一种创新型社会配套设施。其中&#xff0c;智慧公厕作为城市智慧驿站的重要功能基础&#xff0c;具备社会配套不可缺少的特点&#xff0c;所以在应用场景上&#xff0c;拥有广泛的需求和要求。那么&#xff0c;城…

springcloud-网关(gateway)

springcloud-网关(gateway) 概述 \Spring Cloud Gateway旨在提供一种简单而有效的方式来路由到API&#xff0c;并为其提供跨领域的关注&#xff0c;如&#xff1a;安全、监控/指标和容错 常用术语 Route&#xff08;路由&#xff09;: 网关的基本构件。它由一个ID、一个目的地…

【ArcGIS微课1000例】0103:导出点、线、面要素的折点坐标值

点要素对应的是一个或者若干个坐标,线要素对应的是对个坐标值对应的点连起来,面要素是多个坐标值对应的点连起来构成的封闭多边形。本文讲述导出点的坐标值。 文章目录 一、点要素坐标导出1. 计算点坐标2. 导出点坐标二、线要素坐标导出1. 生成线要素折点2. 计算折点坐标3. 导…

【打工日常】使用docker部署Dashdot工具箱

一、Dashdot介绍 dashdot是一个简洁清晰的服务器数据仪表板&#xff0c;基于React实现 &#xff0c;主要是显示操作系统、进程、存储、内存、网络这五个的数据。 二、本次实践介绍 1. 本次实践简介 本次实践部署环境为个人测试环境 2. 本地环境规划 本次实践环境规划&#xf…

S-35390A开发

计时芯片 S-35390A芯片是计时芯片&#xff0c;一般用来计算时间。低功耗&#xff0c;宽电压&#xff0c;受温度影响小&#xff0c;适用于很多电路。它有一个问题&#xff0c;不阻止用户设置不存在的时间&#xff0c;设置进去之后计时或者闹钟定时会出错。 规格书阅读 首先我…

成为大佬之路--linux软件安装使用第000000003篇--vmare安装centos

准备工作 1.下载centos安装包 2.安装vmare 建议直接百度 绿色版 直接上最新版本旗舰版(pro) 新建虚拟机 1.打开vamre,点击文件--新建虚拟机 2.直接默认选择 "典型",点击下一步 3. 选择稍后安装操作系统,点击下一步 4.选择linux版本 因为安装的是centos直接选…

Arcmap excel转shp

使用excel表格转shp的时候&#xff0c;如果你的excel里面有很多字段&#xff0c;直接转很大概率会出现转换结果错误的情况&#xff0c;那么就需要精简一下字段的个数。将原来的表格文件另存一份&#xff0c;在另存为的文件中只保留关键的经度、纬度、和用于匹配的字段即可&…

【C++】C++11下线程库

C11下线程库 1. thread类的简单介绍2.线程函数参数3.原子性操作库(atomic)4.mutex的种类5. RAII风格加锁解锁5.1Lock_guard5.2unique_lock 6.condition_variable 1. thread类的简单介绍 在C11之前&#xff0c;涉及到多线程问题&#xff0c;都是和平台相关的&#xff0c;比如wi…

代码随想录第二十一天 701.二叉搜索树中的插入操作 108.将有序数组转换为二叉搜索树

701.二叉搜索树中的插入操作 题目描述 给定二叉搜索树&#xff08;BST&#xff09;的根节点 root 和要插入树中的值 value &#xff0c;将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据 保证 &#xff0c;新值和原始二叉搜索树中的任意节点值都不同。 注意&a…

pom.xml常见依赖及其作用

1.org.mybatis.spring.boot下的mybatis-spring-boot-starter&#xff1a;这个依赖是mybatis和springboot的集成库&#xff0c;简化了springboot项目中使用mybatis进行持久化操作的配置和管理 2.org.projectlombok下的lombok&#xff1a;常用注解Data、NoArgsConstructor、AllA…

ArcGIS学习(八)基于GIS平台的控规编制办法

ArcGIS学习(八)基于GIS平台的控规编制办法 上一任务我们学习了”如何进行图片数据的矢量化?" 这一关我们来学习一个比较简单的案例一一”如何在ArcGIS中录入控规指标,绘制控规图纸?" 首先,先来看看这个案例的分析思路以及导入CAD格式的控规图纸。 接着,来看…

使用静态CRLSP配置MPLS TE隧道

正文共&#xff1a;1591 字 13 图&#xff0c;预估阅读时间&#xff1a;4 分钟 静态CRLSP&#xff08;Constraint-based Routed Label Switched Paths&#xff0c;基于约束路由的LSP&#xff09;是指在报文经过的每一跳设备上&#xff08;包括Ingress、Transit和Egress&#xf…

如何邀请媒体参加活动报道?媒体邀约的几大步骤?

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 邀请媒体参加活动报道通常需要发送邀请函、提供详细活动信息&#xff0c;并通过电话或邮件进行跟进确认。 在邀请媒体之前&#xff0c;应该制定一个详细的媒体规划表&#xff0c;包括拟…

Unity2023.1.19_ShaderGraph节点说明以及使用技巧

Unity2023.1.19_ShaderGraph节点说明以及使用技巧 目录 Unity2023.1.19_ShaderGraph节点说明以及使用技巧 1. 快捷键CtrlG完成和UE蓝图使用快捷键C一样的蓝图分组注释效果&#xff1a; 2. Tiling And Offset&#xff1a; 3. 以下是两组URP材质渲染的效果对比&#xff1a; 4…

探索JDK8的新特性

1. JDK8简介 1.1 简述 Java 8由Oracle从2014年3月18日发布&#xff0c;此版本是自Java 5&#xff08;发布于2004年&#xff09;之后的一个重量级版本&#xff0c;也是java发展史上的一个里程碑式的版本。这个版本在JVM、编译器、库、Java语法特性等方面都做了很大改进&#x…

k8s除了可以直接运行docker镜像之外,还可以运行什么? springboot项目打包成的压缩包可以直接运行在docker容器中吗?

Kubernetes&#xff08;k8s&#xff09;主要设计用于自动部署、扩展和管理容器化应用程序。虽然它与Docker容器最为密切相关&#xff0c;Kubernetes实际上是与容器运行时技术无关的&#xff0c;这意味着它不仅仅能够管理Docker容器。Kubernetes支持多种容器运行时&#xff0c;包…

NestJS入门6:日志中间件

前文参考&#xff1a; NestJS入门1 NestJS入门2&#xff1a;创建模块 NestJS入门3&#xff1a;不同请求方式前后端写法 NestJS入门4&#xff1a;MySQL typeorm 增删改查 NestJS入门5&#xff1a;加入Swagger 1. 安装 nest g middleware logger middleware​ ​ ​ 2. lo…

深入理解java虚拟机---自动内存管理

2.2 运行时数据区域 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同的数据区域。这些区域有各自的用途&#xff0c;以及创建和销毁的时间&#xff0c;有的区域随着虚拟机进程的启动而一直存在&#xff0c;有些区域则是依赖用户线程的启动和结束而建立和销…

BioTech - 大型蛋白质复合物的组装流程 (CombFold)

欢迎关注我的CSDN&#xff1a;https://spike.blog.csdn.net/ 本文地址&#xff1a;https://spike.blog.csdn.net/article/details/136187314 CombFold是用于预测大型蛋白质复合物结构的组合和分层组装算法&#xff0c;利用AlphaFold2预测的亚基之间的成对相互作用。CombFold的组…

亿道丨三防平板pad丨三防平板是指哪三防丨三防工业级平板电脑

三防工业级平板电脑成为许多行业中的重要工具。本文将介绍三防工业级平板电脑的特点以及其在各个领域中的广泛应用。 三防工业级平板电脑的特点 三防工业级平板电脑是指具备防水、防尘和防震功能的平板电脑。这些特点使得它们能够在恶劣环境中工作&#xff0c;如沙尘飞扬的工地…