CS计算机代考程序代写 file system COMP 3000 Operating Systems

COMP 3000 Operating Systems
Kernel Modules (part 2)
Lianying Zhao

Levels of Abstraction in the OS (example)
• I/O
• Block (raw)
/dev/sda
/dev/sdb
• Block (LVM) /dev/dm-0
/dev/mapper/vg0-lv–0
• File system
Port IN/OUT Device registers
COMP 3000 (Winter 2021)
2

Examples of Kernel Modules
• File system drivers (e.g., ntfs and msdos/vfat)
• Device drivers (e.g., USB HID for mice and keyboards)
• Kernel support for new hardware features
• E.g., Intel AES-NI, a new encryption instruction set that improves AES
performance
• Kernel-provided library routines
• E.g., CRC32c, for calculations of Cyclic Redundancy-Check
COMP 3000 (Winter 2021) 3

Commands for Working with Kernel Moules
• Each kernel module is packaged into a .ko file • Just a special ELF object file
• lsmod to see currently loaded kernel modules
• modinfo to show information about a specific module
• insmod/rmmod to insert/remove a kernel module
• modprobe to intelligently insert/remove kernel modules (works with depmod)
COMP 3000 (Winter 2021) 4

First: Learn to Create a Kernel Module
• A regular C source file
• Include necessary header files
• Two macros:
• module_init(); • module_exit();
• A Makefile (optional, but very useful)
COMP 3000 (Winter 2021) 5

Next: Make it a Device Driver
• Now, you have a skeleton kernel module to add code to
• Typical steps for character devices (there are multiple other ways) • Register your device type (to get a major number)
• Create a device class • Create your device
• device_create() will create a device node in /dev and associate it with sysfs • Most important: define your device file operations (referred to at registration)
COMP 3000 (Winter 2021) 16

Device Operations (character)
• Needed when you want the device to be visible in userspace and accessed as a (special) file
• Defined by struct file_operations {}, just similar to regular file operations
• read(), write() and open() • ioctl()
For block devices, there’s still the block layer
COMP 3000 (Winter 2021) 7

The Kernel Space – a Different World
• To print information, use printk(), not printf()
• You are outputting kernel messages, most likely in the kernel log
• Exchanging Data between “Kernel” and “User” • One of the main performance overhead sources
• put_user()/get_user()
• copy_to_user()/copy_from_user()
• Memory allocation
• kmalloc()/kfree()
COMP 3000 (Winter 2021) 8

Dependency Considerations
• Download the linux-headers package corresponding to the kernel version you are building the module for
• Important: kernel modules are specific to kernel versions. They will never work with a version they were not built for.
• Well, here is the thing: there’s the possibility that they may work, but with unpredictable behavior. You’ll see “tainted kernel”. You can force the loading.
• We have been assuming adding code to the kernel source tree (in-tree)
• Dynamic Kernel Module Support (DKMS)
• A framework for automatic build of out-of-tree kernel modules
• There is the tool dkms, used in conjunction with dkms-enabled kernel modules, e.g., virtualbox-dkms
COMP 3000 (Winter 2021) 9