samples/vfio-mdev/mtty.c
Source file repositories/reference/linux-study-clean/samples/vfio-mdev/mtty.c
File Facts
- System
- Linux kernel
- Corpus path
samples/vfio-mdev/mtty.c- Extension
.c- Size
- 48066 bytes
- Lines
- 2036
- Domain
- Support Tooling And Documentation
- Bucket
- samples
- Inferred role
- Support Tooling And Documentation: operation-table or driver-model contract
- Status
- pattern implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Touches IRQ or DMA behavior; this matters for the representative real-device path.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/init.hlinux/module.hlinux/kernel.hlinux/fs.hlinux/poll.hlinux/slab.hlinux/cdev.hlinux/sched.hlinux/wait.hlinux/vfio.hlinux/iommu.hlinux/sysfs.hlinux/ctype.hlinux/file.hlinux/mdev.hlinux/pci.hlinux/serial.huapi/linux/serial_reg.hlinux/eventfd.hlinux/anon_inodes.h
Detected Declarations
struct mdev_region_infostruct rxtxstruct serial_portstruct mtty_datastruct mdev_statestruct mtty_migration_filestruct mdev_statefunction dump_bufferfunction is_intxfunction is_msifunction is_noirqfunction mtty_trigger_interruptfunction mtty_create_config_spacefunction handle_pci_cfg_writefunction handle_bar_writefunction handle_bar_readfunction mdev_read_basefunction mdev_accessfunction mtty_data_sizefunction mtty_disable_filefunction mtty_disable_filesfunction mtty_state_mutex_unlockfunction mtty_release_migffunction mtty_precopy_ioctlfunction mtty_save_readfunction mtty_save_statefunction mtty_load_statefunction mtty_save_device_datafunction mtty_resume_writefunction mtty_resume_device_datafunction mtty_get_statefunction mtty_get_data_sizefunction mtty_log_startfunction mtty_log_stopfunction mtty_log_read_and_clearfunction mtty_init_devfunction mtty_probefunction mtty_release_devfunction mtty_removefunction mtty_resetfunction mtty_readfunction mtty_writefunction mtty_disable_intxfunction mtty_disable_msifunction mtty_set_irqsfunction mtty_ioctl_get_region_infofunction mtty_get_irq_infofunction mtty_get_device_info
Annotated Snippet
static const struct file_operations vd_fops = {
.owner = THIS_MODULE,
};
static const struct vfio_device_ops mtty_dev_ops;
/* Helper functions */
static void dump_buffer(u8 *buf, uint32_t count)
{
#if defined(DEBUG)
int i;
pr_info("Buffer:\n");
for (i = 0; i < count; i++) {
pr_info("%2x ", *(buf + i));
if ((i + 1) % 16 == 0)
pr_info("\n");
}
#endif
}
static bool is_intx(struct mdev_state *mdev_state)
{
return mdev_state->irq_index == VFIO_PCI_INTX_IRQ_INDEX;
}
static bool is_msi(struct mdev_state *mdev_state)
{
return mdev_state->irq_index == VFIO_PCI_MSI_IRQ_INDEX;
}
static bool is_noirq(struct mdev_state *mdev_state)
{
return !is_intx(mdev_state) && !is_msi(mdev_state);
}
static void mtty_trigger_interrupt(struct mdev_state *mdev_state)
{
lockdep_assert_held(&mdev_state->ops_lock);
if (is_msi(mdev_state)) {
if (mdev_state->msi_evtfd)
eventfd_signal(mdev_state->msi_evtfd);
} else if (is_intx(mdev_state)) {
if (mdev_state->intx_evtfd && !mdev_state->intx_mask) {
eventfd_signal(mdev_state->intx_evtfd);
mdev_state->intx_mask = true;
}
}
}
static void mtty_create_config_space(struct mdev_state *mdev_state)
{
/* PCI dev ID */
STORE_LE32((u32 *) &mdev_state->vconfig[0x0], 0x32534348);
/* Control: I/O+, Mem-, BusMaster- */
STORE_LE16((u16 *) &mdev_state->vconfig[0x4], 0x0001);
/* Status: capabilities list absent */
STORE_LE16((u16 *) &mdev_state->vconfig[0x6], 0x0200);
/* Rev ID */
mdev_state->vconfig[0x8] = 0x10;
/* programming interface class : 16550-compatible serial controller */
mdev_state->vconfig[0x9] = 0x02;
/* Sub class : 00 */
mdev_state->vconfig[0xa] = 0x00;
/* Base class : Simple Communication controllers */
mdev_state->vconfig[0xb] = 0x07;
/* base address registers */
/* BAR0: IO space */
STORE_LE32((u32 *) &mdev_state->vconfig[0x10], 0x000001);
mdev_state->bar_mask[0] = ~(MTTY_IO_BAR_SIZE) + 1;
if (mdev_state->nr_ports == 2) {
/* BAR1: IO space */
STORE_LE32((u32 *) &mdev_state->vconfig[0x14], 0x000001);
mdev_state->bar_mask[1] = ~(MTTY_IO_BAR_SIZE) + 1;
}
/* Subsystem ID */
STORE_LE32((u32 *) &mdev_state->vconfig[0x2c], 0x32534348);
mdev_state->vconfig[0x34] = 0x00; /* Cap Ptr */
Annotation
- Immediate include surface: `linux/init.h`, `linux/module.h`, `linux/kernel.h`, `linux/fs.h`, `linux/poll.h`, `linux/slab.h`, `linux/cdev.h`, `linux/sched.h`.
- Detected declarations: `struct mdev_region_info`, `struct rxtx`, `struct serial_port`, `struct mtty_data`, `struct mdev_state`, `struct mtty_migration_file`, `struct mdev_state`, `function dump_buffer`, `function is_intx`, `function is_msi`.
- Atlas domain: Support Tooling And Documentation / samples.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
- IRQ or DMA behavior appears here, which is relevant to the selected PCIe/NVMe device path.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.