paravirtualization/paravirt_encryption/frontend.c
static int crypto_chrdev_open(struct inode *inode, struct file *filp) {

    int ret = 0;
    int err;
    //Better have fixed sgs indexes using macros than hardcoded counters
    unsigned int len, input_num=0, output_num=0;
    struct crypto_open_file *crof;
    struct crypto_device *crdev;
    struct scatterlist syscall_type_sg, host_fd_sg, *sgs[2];

    unsigned int *syscall_type;
    int *host_fd;

    debug("Entering");

    ret = -ENODEV;
    if ((ret = nonseekable_open(inode, filp)) < 0)
        goto fail;

    /* Associate this open file with the relevant crypto device. */
    debug("device with %u minor", iminor(inode));
    crdev = get_crypto_dev_by_minor(iminor(inode));
    if (!crdev) {
        debug("Could not find crypto device with %u minor", iminor(inode));
        ret = -ENODEV;
        goto fail;
    }

//    sema_init(&crdev->lock,1);
    crof = kmalloc(sizeof(*crof), GFP_KERNEL);
    if (!crof) {
        ret = -ENOMEM;
        goto fail;
    }
    crof->crdev = crdev;
    crof->host_fd = -1;
    filp->private_data = crof;

    /**
     * We need two sg lists, one for syscall_type and one to get the
     * file descriptor from the host.
     **/

    syscall_type = kmalloc(sizeof(*syscall_type), GFP_KERNEL);
    *syscall_type = VIRTIO_CRYPTO_SYSCALL_OPEN;

    host_fd = kmalloc(sizeof(*host_fd), GFP_KERNEL);
    *host_fd = -1;


    sg_init_one(&syscall_type_sg, syscall_type, sizeof(*syscall_type));
    sgs[output_num] = &syscall_type_sg;

    output_num++;
    sg_init_one(&host_fd_sg, host_fd, sizeof(*host_fd));
    sgs[output_num + input_num] = &host_fd_sg;
    input_num++;
    /* Going into critical section. We need to lock */

    if (down_interruptible(&crdev->lock))
        return -ERESTARTSYS;

    err = virtqueue_add_sgs(crdev->vq, sgs, output_num, input_num, &syscall_type_sg,
            GFP_ATOMIC);
    virtqueue_kick(crdev->vq);
    while (virtqueue_get_buf(crdev->vq, &len) == NULL)
        /* do nothing */;

    up(&crdev->lock);

    /*
     ** Unlock the Semaphore.
     ** If host failed to open() return -ENODEV.
     */

    if (*host_fd < 0) {
        ret = -ENODEV;
        goto fail;
    }

    crof->host_fd = *host_fd;

    kfree(syscall_type);
    kfree(host_fd);

fail:
    debug("Leaving");
    return ret;
}
/*************************************
 * Implementation of file operations
 * for the Crypto character device
 *************************************/
Implementation of file operations
for the Crypto character device
Implementation of file operations for the Crypto character device
static int crypto_chrdev_open(struct inode *inode, struct file *filp) {
    int ret = 0;
    int err;
    unsigned int len, input_num=0, output_num=0;
    struct crypto_open_file *crof;
    struct crypto_device *crdev;
    struct scatterlist syscall_type_sg, host_fd_sg, *sgs[2];
    unsigned int *syscall_type;
    int *host_fd;
    debug("Entering");
    ret = -ENODEV;
    if ((ret = nonseekable_open(inode, filp)) < 0)
        goto fail;
    debug("device with %u minor", iminor(inode));
    crdev = get_crypto_dev_by_minor(iminor(inode));
    if (!crdev) {
        debug("Could not find crypto device with %u minor", iminor(inode));
        ret = -ENODEV;
        goto fail;
    }
    crof = kmalloc(sizeof(*crof), GFP_KERNEL);
    if (!crof) {
        ret = -ENOMEM;
        goto fail;
    }
    crof->crdev = crdev;
    crof->host_fd = -1;
    filp->private_data = crof;
    syscall_type = kmalloc(sizeof(*syscall_type), GFP_KERNEL);
    *syscall_type = VIRTIO_CRYPTO_SYSCALL_OPEN;
    host_fd = kmalloc(sizeof(*host_fd), GFP_KERNEL);
    *host_fd = -1;
    sg_init_one(&syscall_type_sg, syscall_type, sizeof(*syscall_type));
    sgs[output_num] = &syscall_type_sg;
    output_num++;
    sg_init_one(&host_fd_sg, host_fd, sizeof(*host_fd));
    sgs[output_num + input_num] = &host_fd_sg;
    input_num++;
    if (down_interruptible(&crdev->lock))
        return -ERESTARTSYS;
    err = virtqueue_add_sgs(crdev->vq, sgs, output_num, input_num, &syscall_type_sg,
            GFP_ATOMIC);
    virtqueue_kick(crdev->vq);
    while (virtqueue_get_buf(crdev->vq, &len) == NULL)
        ;
    up(&crdev->lock);
    if (*host_fd < 0) {
        ret = -ENODEV;
        goto fail;
    }
    crof->host_fd = *host_fd;
    kfree(syscall_type);
    kfree(host_fd);
fail:
    debug("Leaving");
    return ret;
}
static int crypto_chrdev_open ( struct inode * inode , struct file * filp ) { int ret = 0 ; int err ; unsigned int len , input_num = 0 , output_num = 0 ; struct crypto_open_file * crof ; struct crypto_device * crdev ; struct scatterlist syscall_type_sg , host_fd_sg , * sgs [ 2 ] ; unsigned int * syscall_type ; int * host_fd ; debug ( " " ) ; ret = - ENODEV ; if ( ( ret = nonseekable_open ( inode , filp ) ) < 0 ) goto fail ; debug ( " " , iminor ( inode ) ) ; crdev = get_crypto_dev_by_minor ( iminor ( inode ) ) ; if ( ! crdev ) { debug ( " " , iminor ( inode ) ) ; ret = - ENODEV ; goto fail ; } crof = kmalloc ( sizeof ( * crof ) , GFP_KERNEL ) ; if ( ! crof ) { ret = - ENOMEM ; goto fail ; } crof -> crdev = crdev ; crof -> host_fd = -1 ; filp -> private_data = crof ; syscall_type = kmalloc ( sizeof ( * syscall_type ) , GFP_KERNEL ) ; * syscall_type = VIRTIO_CRYPTO_SYSCALL_OPEN ; host_fd = kmalloc ( sizeof ( * host_fd ) , GFP_KERNEL ) ; * host_fd = -1 ; sg_init_one ( & syscall_type_sg , syscall_type , sizeof ( * syscall_type ) ) ; sgs [ output_num ] = & syscall_type_sg ; output_num ++ ; sg_init_one ( & host_fd_sg , host_fd , sizeof ( * host_fd ) ) ; sgs [ output_num + input_num ] = & host_fd_sg ; input_num ++ ; if ( down_interruptible ( & crdev -> lock ) ) return - ERESTARTSYS ; err = virtqueue_add_sgs ( crdev -> vq , sgs , output_num , input_num , & syscall_type_sg , GFP_ATOMIC ) ; virtqueue_kick ( crdev -> vq ) ; while ( virtqueue_get_buf ( crdev -> vq , & len ) == NULL ) ; up ( & crdev -> lock ) ; if ( * host_fd < 0 ) { ret = - ENODEV ; goto fail ; } crof -> host_fd = * host_fd ; kfree ( syscall_type ) ; kfree ( host_fd ) ; fail : debug ( " " ) ; return ret ; }
Implementation of file operations
 for the Crypto character device
Implementation of file operations for the Crypto character device
//Better have fixed sgs indexes using macros than hardcoded counters /* Associate this open file with the relevant crypto device. */ //    sema_init(&crdev->lock,1); /**
     * We need two sg lists, one for syscall_type and one to get the
     * file descriptor from the host.
     **/ /* Going into critical section. We need to lock */ /* do nothing */ /*
     ** Unlock the Semaphore.
     ** If host failed to open() return -ENODEV.
     */