复习
本次课回答的问题
本次课主要内容
I/O 设备的主要功能:
操作系统:设备 = 支持各类操作的对象 (文件)
把系统调用 (read/write/ioctl/...) “翻译” 成与设备寄存器的交互
例子:/dev/
中的对象
/dev/pts/[x]
- pseudo terminal/dev/zero
- “零” 设备/dev/null
- “null” 设备/dev/random
, /dev/urandom
- 随机数生成器head -c 512 [device] | xxd
设备模型
typedef struct devops {
int (*init)(device_t *dev);
int (*read) (device_t *dev, int offset, void *buf, int count);
int (*write)(device_t *dev, int offset, void *buf, int count);
} devops_t;
I/O 设备看起来是个 “黑盒子”
设备不仅仅是数据,还有
例子
“字节流” 以内的功能
“字节流” 以外的功能
我们希望实现一个最简单的 “软件定义核弹”
#include <fcntl.h>
#define SECRET "\x01\x14\x05\x14"
int main() {
int fd = open("/dev/nuke", O_WRONLY);
if (fd > 0) {
write(fd, SECRET, sizeof(SECRET) - 1);
close(fd);
} else {
perror("launcher");
}
}
内核模块:一段可以被内核动态加载执行的代码
launcher.c: 驱动程序模块
struct file_operations
的对象struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
int (*mmap) (struct file *, struct vm_area_struct *);
unsigned long mmap_supported_flags;
int (*open) (struct inode *, struct file *);
int (*release) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*flock) (struct file *, int, struct file_lock *);
...
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
unlocked_ioctl
: BKL (Big Kernel Lock) 时代的遗产ioctl
ioctl
执行时默认持有 BKLunlocked_ioctl
避免锁ioctl
从 struct file_operations
中移除compact_ioctl
: 机器字长的兼容性Single Instruction, Multiple Thread
mandelbrot.cu 和 GPU 惊人的计算力
nvprof 结果
==2994086== Profiling result:
Time(%) Time Name
95.75% 1.76911s mandelbrot_kernel
4.25% 78.506ms [CUDA memcpy DtoH] (12800 x 12800 data)
0.00% 1.5360us [CUDA memcpy HtoD]
RTFM: Parallel Thread Execution ISA Application Guide
该有的工具都有
GPU 驱动非常复杂
NVIDIA 在 2022 年开源了驱动!(名场面)
磁盘 (存储设备) 的访问特性
对比一下终端和 GPU,的确是很不一样的设备
文件系统和磁盘设备之间的接口
Many storage devices, ... come with volatile write back caches
我们当然可以提供一个 ioctl
| REQ_PREFLUSH
之前的数据落盘后才开始| REQ_FUA
(force unit access),数据落盘后才返回文件系统
实现文件系统
本次课回答的问题
Takeaway messages