在Minix中制作设备驱动程序

问题描述 投票:4回答:1

我正在尝试在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。

[此外,我对如何在write()函数中的hello_write()函数(缓冲区)中获取第二个参数还是有些困惑。它包含在hello_read()的参数之一中吗?

感谢您的帮助!

c driver minix
1个回答
0
投票

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

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

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

  • 查看您正在传递sys_safecopy功能的字节数
  • 请确保在写入时将正确的偏移量与缓冲区放在一起。对于我用它的情况是(buffer_ptr + current_size)
  • 请确保您使用的是Minix的早期版本,并且已将正确数量的参数放入sys_safecopy函数中(可以是5个args或6个args,对于您好驱动程序,在较早版本的minix上最后一个将是是“ D”;))
© www.soinside.com 2019 - 2024. All rights reserved.