Write to port 0cf8h fails with segfault

天大地大妈咪最大 提交于 2019-12-02 11:58:23

问题


I have an AMD processor of e2-2000 model. THis is family 0fh. According to family 0fh BKDG I have this code to read device and vendor ID:

ReadPCIConfiguration:
movq    $0x80000100, %rax
movq    $0x0cf8, %rdx
outl    %eax, %dx          # sigsegv caught here
movq    $0x0cfc, %rdx
inl     %dx, %eax
ret

As far as I know the algorithm to read/write PCI configuration is as follows:

  • write target bus number, device number function number and offset or register number to configuration address port
  • perform 1-, 2- or 4-byte r/w operation from/to configuration data port

Ports 0xcf8..0xcfb - configuration address port (doubleword)

Bits meaning:

  • 31 - EnReg - enable the transaction (R/W)
  • 24..31 - reserved (R/O)
  • 16..23 - BusNum (R/W)
  • 11..15 - DevNum (R/W)
  • 8..10 - FuncNum (R/W)
  • 2..7 - RegNum (R/W)
  • 0..1 - Reserved (R/O)

So, I write to bus - 0, dev - 0, func - 1, reg - 00

Am I doing something wrong?

(I run the compiled and linked application from userspace, GNU/Debian "Wheezy" Linux 3.11.6)


回答1:


Linux does not, by default, allow userland code to write to I/O ports. (Doing so can be quite dangerous from a security perspective.) If you’d like Linux to give your process access to the I/O ports, you have two options:

  1. You can use the ioperm system call. However, ioperm has been deprecated for some time, and Josh Triplett recently pushed a patch that allows users to remove it from the kernel. Avoid ioperm if you want your code to continue working for the forseeable future.

  2. You can read from and write to /dev/port. See mem(4). Your process will, obviously, need read and write permissions for /dev/mem; on Wheezy, that means it needs to run as root, unless you change the permissions on the device.



来源:https://stackoverflow.com/questions/20306425/write-to-port-0cf8h-fails-with-segfault

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!