c - inb() 和 outb() Linux 系统调用的包装器

标签 c linux go system-calls

以下 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 进行此调用,以免丢失简单的交叉编译。

最佳答案

inboutb 不是系统调用,它们是处理器指令。您可以用 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/

相关文章:

c - 弹性 "Unrecognized Error"

android - 不同语言的两个进程是否可以使用UDP进行通信

linux - 在管道 awk 语句之间传递变量

linux - 如何通过命令行在多个网络连接之间进行选择?

go - 需要 *os.File 作为参数的测试函数

c++ - gdb 打印软数组的数组

c++ - 我被字符串和数组困住了

php - Aerospike Golang 如何使用 POLICY_KEY_SEND?

Ruby 输入不起作用(新手)

html - 如何使用 html/javascript/jquery 将 golang http json 主体输出到网站页面