以下 Linux 系统调用是否有 Go 包装器用于低级端口输入输出?
#include <sys/io.h>
unsigned char inb(unsigned short int port);
void outb(unsigned char value, unsigned short int port);
我只找到了姐妹调用的包装:
int ioperm(unsigned long from, unsigned long num, int turn_on);
设置对上述端口的访问。包装器位于 syscall Go 包中:
func Ioperm(from int, num int, on int) (err error)
但没有inb()和outb()的痕迹。我不想使用 cgo 进行此调用,以免丢失简单的交叉编译。
最佳答案
inb
和 outb
不是系统调用,它们是处理器指令。您可以用 C 语言为其编写包装函数,并使用 cgo
调用它们。
以下是 C 函数(除非您在 sys/io.h
中提供了它们):
unsigned char inb(unsigned short port)
{
unsigned char ret;
asm volatile("in %%dx, %%al" : "=a"(ret) : "d"(port) : "memory");
return ret;
}
void outb(unsigned char value, unsigned short port)
{
asm volatile("out %%al, %%dx" : : "a"(value), "d"(port) : "memory");
}
还有一个可以与 cgo
一起使用的小头文件:
#ifndef IOPORT_H_
#define IOPORT_H_ 1
unsigned char inb(unsigned short port);
void outb(unsigned char value, unsigned short port);
#endif
关于c - inb() 和 outb() Linux 系统调用的包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59812770/