【发布时间】:2015-05-11 17:52:54
【问题描述】:
我参考this answer for crash 帮助分析导致问题的这段代码。每个人的上下文,我正在工作一个字符驱动程序,它将充当从用户空间直接到硬件的通道,用于 ahci 驱动程序。为此,我正在相应地修改 ahci 驱动程序。
我从小处着手。我想查看我的 VM 上 AHCI HBA 的 HBA 端口 0 的端口寄存器。我的字符驱动ioctl代码:
switch (cmd) {
case AHCIP_GPORT_REG:
pPciDev = pci_get_device(0x8086, 0x2829, NULL);
if (pPciDev) {
/* This will set ret to the value that it needs to be. This
* is true of __put_user() too */
if ((ret = __get_user(off, (u32*)obj))) {
printk(KERN_INFO "unable to read from user space\n");
goto ioctl_quick_out;
}
reg = get_port_reg(&pPciDev->dev, off);
if ((ret = __put_user(reg, (u32*)obj)))
{
printk(KERN_INFO "Unable to write to user space\n");
}
pci_dev_put(pPciDev);
}
// This break wasn't in the code when it crashed
break;
default:
// POSIX compliance with this one (REF of LDD3)
ret = -ENOTTY;
}
此字符驱动程序调用的我修改后的 ahci.c 版本的代码:
u32 get_port_reg(struct device *dev, u32 off)
{
struct Scsi_Host *shost = class_to_shost(dev);
struct ata_port *ap = ata_shost_to_port(shost);
void __iomem *port_mmio = ahci_port_base(ap);
return ioread32(port_mmio + off);
}
EXPORT_SYMBOL(get_port_reg);
这导致的内核 oops 发生在这里:
PID: 3357 TASK: ffff88011c9b7500 CPU: 0 COMMAND: "peek"
#0 [ffff8800abfc79f0] machine_kexec at ffffffff8103b5bb
#1 [ffff8800abfc7a50] crash_kexec at ffffffff810c9852
#2 [ffff8800abfc7b20] oops_end at ffffffff8152e0f0
#3 [ffff8800abfc7b50] no_context at ffffffff8104c80b
#4 [ffff8800abfc7ba0] __bad_area_nosemaphore at ffffffff8104ca95
#5 [ffff8800abfc7bf0] bad_area at ffffffff8104cbbe
#6 [ffff8800abfc7c20] __do_page_fault at ffffffff8104d36f
#7 [ffff8800abfc7d40] do_page_fault at ffffffff8153003e
#8 [ffff8800abfc7d70] page_fault at ffffffff8152d3f5
[exception RIP: get_port_reg+18]
RIP: ffffffffa03c4cd2 RSP: ffff8800abfc7e28 RFLAGS: 00010246
RAX: 0000000000020101 RBX: 00007fff17273960 RCX: ffffffff812b0710
RDX: ffff88011ddd5000 RSI: 0000000000000000 RDI: ffff88011ddd5090
RBP: ffff8800abfc7e28 R8: 0000000000000000 R9: 0000000000000000
R10: 00000000000007d5 R11: 0000000000000006 R12: ffff88011ddd5000
R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000000000
ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018
如您所见,指令指针为get_port_reg+18。由于这个函数很小,这里是完整的反汇编
crash> dis get_port_reg
0xffffffffa03c4cc0 <get_port_reg>: push %rbp
0xffffffffa03c4cc1 <get_port_reg+1>: mov %rsp,%rbp
0xffffffffa03c4cc4 <get_port_reg+4>: nopl 0x0(%rax,%rax,1)
0xffffffffa03c4cc9 <get_port_reg+9>: mov 0x240(%rdi),%rax
0xffffffffa03c4cd0 <get_port_reg+16>: mov %esi,%esi
0xffffffffa03c4cd2 <get_port_reg+18>: mov 0x2838(%rax),%rdx
0xffffffffa03c4cd9 <get_port_reg+25>: mov 0x28(%rax),%eax
0xffffffffa03c4cdc <get_port_reg+28>: mov 0x10(%rdx),%rdx
0xffffffffa03c4ce0 <get_port_reg+32>: shl $0x7,%eax
0xffffffffa03c4ce3 <get_port_reg+35>: mov %eax,%eax
0xffffffffa03c4ce5 <get_port_reg+37>: add 0x28(%rdx),%rax
0xffffffffa03c4ce9 <get_port_reg+41>: lea 0x100(%rax,%rsi,1),%rdi
0xffffffffa03c4cf1 <get_port_reg+49>: callq 0xffffffff8129dde0 <ioread32>
0xffffffffa03c4cf6 <get_port_reg+54>: leaveq
0xffffffffa03c4cf7 <get_port_reg+55>: retq
0xffffffffa03c4cf8 <get_port_reg+56>: nopl 0x0(%rax,%rax,1)
您可能已经猜到了,我是一个组装新手。哪一行代码是get_port_reg+18?我很困惑,因为我在该函数的每一行都调用函数,但我看到的唯一调用是ioread32()。
作为参考,我在ahci_show_port_cmd() within the same file 之后建模了我的函数get_port_reg。我想不出任何其他方法来获得运行它所必需的struct pci_dev 结构。我是不是在滥用get_pci_device() 和pci_dev_put()?这根本不是问题吗?
感谢您的帮助
安迪
【问题讨论】:
-
pci_get_device()可能返回一个 pci 设备,而您需要相应的 SCSI 主机设备——它们有不同的类。 -
看看
ata_pci_remove_one:lxr.free-electrons.com/source/drivers/ata/libata-core.c#L6314,它展示了如何从PCI设备获取ata_host并从中获取ata_port应该很简单。 -
看来问题出在这里 - ata_shost_to_port。我可以从技术角度告诉你,没有代码理解:函数class_to_shost基本上是一个container_of,所以它假设struct device(dev)嵌入到struct Scsi_host中,因此它将dev适当地转换为Scsi_host。接下来,您的代码尝试取消引用 shost 以获取 ata_port (*(struct ata_port **)&host->hostdata[0])。并且繁荣......似乎这里发生了page_fault。所以这意味着可能有一个垃圾而不是 struct Scsi_host ...
标签: linux-kernel linux-device-driver