【问题标题】:Making a Device Driver in Minix在 Minix 中制作设备驱动程序
【发布时间】:2015-10-27 04:43:29
【问题描述】:

我正在尝试在 Minix 上创建字符设备驱动程序。我希望它能够接受read()write() 呼叫。我的理解是,我需要将sys_safecopyfrom() 用于运行read() 函数的函数,将sys_safecopyto() 用于运行write() 函数的函数。问题是当我像这样运行它时,我不断收到类似的错误(虽然不完全相同,但我认为差异是内存位置)。错误是:

verify_grant: grant verify failed: access invalid: want 0x..., have 0x...
grant 2 verify to copy ... -> ... by ... failed err -1
read: Operation not permitted

“...”是内存位置,错误与写入类似,但内存位置除外,它在最后一行显示“写入”而不是“读取”。

我认为相关代码如下:

#include <minix/drivers.h>
#include <minix/chardriver.h>
#include <stdio.h>
#include <stdlib.h>
#include <minix/ds.h>
...
static struct chardriver hello_tab =
{
    .cdr_open   = hello_open,
    .cdr_close  = hello_close,
    .cdr_read   = hello_read,
    .cdr_write  = hello_write,
};
...
static ssize_t hello_read(devminor_t UNUSED(minor), u64_t position,
    endpoint_t endpt, cp_grant_id_t grant, size_t size, int UNUSED(flags),
    cdev_id_t UNUSED(id))
{
    u64_t dev_size;
    char *ptr;
    int ret;
    char *buf = HELLO_MESSAGE;

    printf("hello_read()\n");

    /* This is the total size of our device. */
    dev_size = (u64_t) strlen(buf);

    /* Check for EOF, and possibly limit the read size. */
    if (position >= dev_size) return 0;     /* EOF */
    if (position + size > dev_size)
        size = (size_t)(dev_size - position);   /* limit size */

    /* Copy the requested part to the caller. */
    ptr = buf + (size_t)position;
    if ((ret = sys_safecopyfrom(endpt, grant, 0, (vir_bytes) ptr, size)) != OK)
        return ret;

    /* Return the number of bytes read. */
    printf("Message is :%s", ptr);
    return size;
}
static ssize_t hello_write(devminor_t UNUSED(minor), u64_t position,
    endpoint_t endpt, cp_grant_id_t grant, size_t size, int UNUSED(flags),
    cdev_id_t UNUSED(id))
{
    u64_t dev_size;
    char *ptr;
    int ret;
    char *buf = HELLO_MESSAGE;

    printf("hello_write()\n");

    /* This is the total size of our device. */
    dev_size = (u64_t) strlen(buf);

    /* Check for EOF, and possibly limit the read size. */
    if (position >= dev_size) return 0;     /* EOF */
    if (position + size > dev_size)
        size = (size_t)(dev_size - position);   /* limit size */

    /* Copy the requested part to the caller. */
    ptr = buf + (size_t)position;
    if ((ret = sys_safecopyto(endpt, grant, 0, (vir_bytes) ptr, size)) != OK)
        return ret;

    /* Return the number of bytes read. */
    return size;
}

hello_read 函数基于 hello_write 函数,但我认为它仍然可以工作,并且应该将信息读入 ptr。

另外,我对如何在hello_write() 函数中获取write() 函数(缓冲区)中的第二个参数有点模糊。它是否包含在hello_read() 的参数之一中?

感谢您的帮助!

【问题讨论】:

    标签: c driver minix


    【解决方案1】:

    所以,我知道已经很久了,这里没有任何活动,但我想我会回答这个问题。

    我将首先说将错误的参数传递到 sys_safecopyto/from 时发生错误。

    现在要真正调试它,我想看看你拥有的其余代码。但是对于遇到此问题的其他人,我将提供一些提示

    • 查看传递给 sys_safecopy 函数的字节数
    • 确保在写入时将正确的偏移量放入缓冲区。为了 我使用它的情况是(buffer_ptr + current_size)
    • 确保如果您使用的是较早版本的 minix,您将正确数量的参数放入 sys_safecopy 函数中(可以是 5 个参数或 6 个参数,旧版本 minix 上用于 hello 驱动程序的最后一个参数将只是“D”;))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多