linux/drivers/s390/char/vmcp.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright IBM Corp. 2004, 2010
   4 * Interface implementation for communication with the z/VM control program
   5 *
   6 * Author(s): Christian Borntraeger <borntraeger@de.ibm.com>
   7 *
   8 * z/VMs CP offers the possibility to issue commands via the diagnose code 8
   9 * this driver implements a character device that issues these commands and
  10 * returns the answer of CP.
  11 *
  12 * The idea of this driver is based on cpint from Neale Ferguson and #CP in CMS
  13 */
  14
  15#include <linux/fs.h>
  16#include <linux/init.h>
  17#include <linux/compat.h>
  18#include <linux/kernel.h>
  19#include <linux/miscdevice.h>
  20#include <linux/slab.h>
  21#include <linux/uaccess.h>
  22#include <linux/export.h>
  23#include <linux/mutex.h>
  24#include <linux/cma.h>
  25#include <linux/mm.h>
  26#include <asm/cpcmd.h>
  27#include <asm/debug.h>
  28#include <asm/vmcp.h>
  29
  30struct vmcp_session {
  31        char *response;
  32        unsigned int bufsize;
  33        unsigned int cma_alloc : 1;
  34        int resp_size;
  35        int resp_code;
  36        struct mutex mutex;
  37};
  38
  39static debug_info_t *vmcp_debug;
  40
  41static unsigned long vmcp_cma_size __initdata = CONFIG_VMCP_CMA_SIZE * 1024 * 1024;
  42static struct cma *vmcp_cma;
  43
  44static int __init early_parse_vmcp_cma(char *p)
  45{
  46        vmcp_cma_size = ALIGN(memparse(p, NULL), PAGE_SIZE);
  47        return 0;
  48}
  49early_param("vmcp_cma", early_parse_vmcp_cma);
  50
  51void __init vmcp_cma_reserve(void)
  52{
  53        if (!MACHINE_IS_VM)
  54                return;
  55        cma_declare_contiguous(0, vmcp_cma_size, 0, 0, 0, false, "vmcp", &vmcp_cma);
  56}
  57
  58static void vmcp_response_alloc(struct vmcp_session *session)
  59{
  60        struct page *page = NULL;
  61        int nr_pages, order;
  62
  63        order = get_order(session->bufsize);
  64        nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
  65        /*
  66         * For anything below order 3 allocations rely on the buddy
  67         * allocator. If such low-order allocations can't be handled
  68         * anymore the system won't work anyway.
  69         */
  70        if (order > 2)
  71                page = cma_alloc(vmcp_cma, nr_pages, 0, false);
  72        if (page) {
  73                session->response = (char *)page_to_phys(page);
  74                session->cma_alloc = 1;
  75                return;
  76        }
  77        session->response = (char *)__get_free_pages(GFP_KERNEL | __GFP_RETRY_MAYFAIL, order);
  78}
  79
  80static void vmcp_response_free(struct vmcp_session *session)
  81{
  82        int nr_pages, order;
  83        struct page *page;
  84
  85        if (!session->response)
  86                return;
  87        order = get_order(session->bufsize);
  88        nr_pages = ALIGN(session->bufsize, PAGE_SIZE) >> PAGE_SHIFT;
  89        if (session->cma_alloc) {
  90                page = phys_to_page((unsigned long)session->response);
  91                cma_release(vmcp_cma, page, nr_pages);
  92                session->cma_alloc = 0;
  93        } else {
  94                free_pages((unsigned long)session->response, order);
  95        }
  96        session->response = NULL;
  97}
  98
  99static int vmcp_open(struct inode *inode, struct file *file)
 100{
 101        struct vmcp_session *session;
 102
 103        if (!capable(CAP_SYS_ADMIN))
 104                return -EPERM;
 105
 106        session = kmalloc(sizeof(*session), GFP_KERNEL);
 107        if (!session)
 108                return -ENOMEM;
 109
 110        session->bufsize = PAGE_SIZE;
 111        session->response = NULL;
 112        session->resp_size = 0;
 113        mutex_init(&session->mutex);
 114        file->private_data = session;
 115        return nonseekable_open(inode, file);
 116}
 117
 118static int vmcp_release(struct inode *inode, struct file *file)
 119{
 120        struct vmcp_session *session;
 121
 122        session = file->private_data;
 123        file->private_data = NULL;
 124        vmcp_response_free(session);
 125        kfree(session);
 126        return 0;
 127}
 128
 129static ssize_t
 130vmcp_read(struct file *file, char __user *buff, size_t count, loff_t *ppos)
 131{
 132        ssize_t ret;
 133        size_t size;
 134        struct vmcp_session *session;
 135
 136        session = file->private_data;
 137        if (mutex_lock_interruptible(&session->mutex))
 138                return -ERESTARTSYS;
 139        if (!session->response) {
 140                mutex_unlock(&session->mutex);
 141                return 0;
 142        }
 143        size = min_t(size_t, session->resp_size, session->bufsize);
 144        ret = simple_read_from_buffer(buff, count, ppos,
 145                                        session->response, size);
 146
 147        mutex_unlock(&session->mutex);
 148
 149        return ret;
 150}
 151
 152static ssize_t
 153vmcp_write(struct file *file, const char __user *buff, size_t count,
 154           loff_t *ppos)
 155{
 156        char *cmd;
 157        struct vmcp_session *session;
 158
 159        if (count > 240)
 160                return -EINVAL;
 161        cmd = memdup_user_nul(buff, count);
 162        if (IS_ERR(cmd))
 163                return PTR_ERR(cmd);
 164        session = file->private_data;
 165        if (mutex_lock_interruptible(&session->mutex)) {
 166                kfree(cmd);
 167                return -ERESTARTSYS;
 168        }
 169        if (!session->response)
 170                vmcp_response_alloc(session);
 171        if (!session->response) {
 172                mutex_unlock(&session->mutex);
 173                kfree(cmd);
 174                return -ENOMEM;
 175        }
 176        debug_text_event(vmcp_debug, 1, cmd);
 177        session->resp_size = cpcmd(cmd, session->response, session->bufsize,
 178                                   &session->resp_code);
 179        mutex_unlock(&session->mutex);
 180        kfree(cmd);
 181        *ppos = 0;              /* reset the file pointer after a command */
 182        return count;
 183}
 184
 185
 186/*
 187 * These ioctls are available, as the semantics of the diagnose 8 call
 188 * does not fit very well into a Linux call. Diagnose X'08' is described in
 189 * CP Programming Services SC24-6084-00
 190 *
 191 * VMCP_GETCODE: gives the CP return code back to user space
 192 * VMCP_SETBUF: sets the response buffer for the next write call. diagnose 8
 193 * expects adjacent pages in real storage and to make matters worse, we
 194 * dont know the size of the response. Therefore we default to PAGESIZE and
 195 * let userspace to change the response size, if userspace expects a bigger
 196 * response
 197 */
 198static long vmcp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 199{
 200        struct vmcp_session *session;
 201        int ret = -ENOTTY;
 202        int __user *argp;
 203
 204        session = file->private_data;
 205        if (is_compat_task())
 206                argp = compat_ptr(arg);
 207        else
 208                argp = (int __user *)arg;
 209        if (mutex_lock_interruptible(&session->mutex))
 210                return -ERESTARTSYS;
 211        switch (cmd) {
 212        case VMCP_GETCODE:
 213                ret = put_user(session->resp_code, argp);
 214                break;
 215        case VMCP_SETBUF:
 216                vmcp_response_free(session);
 217                ret = get_user(session->bufsize, argp);
 218                if (ret)
 219                        session->bufsize = PAGE_SIZE;
 220                if (!session->bufsize || get_order(session->bufsize) > 8) {
 221                        session->bufsize = PAGE_SIZE;
 222                        ret = -EINVAL;
 223                }
 224                break;
 225        case VMCP_GETSIZE:
 226                ret = put_user(session->resp_size, argp);
 227                break;
 228        default:
 229                break;
 230        }
 231        mutex_unlock(&session->mutex);
 232        return ret;
 233}
 234
 235static const struct file_operations vmcp_fops = {
 236        .owner          = THIS_MODULE,
 237        .open           = vmcp_open,
 238        .release        = vmcp_release,
 239        .read           = vmcp_read,
 240        .write          = vmcp_write,
 241        .unlocked_ioctl = vmcp_ioctl,
 242        .compat_ioctl   = vmcp_ioctl,
 243        .llseek         = no_llseek,
 244};
 245
 246static struct miscdevice vmcp_dev = {
 247        .name   = "vmcp",
 248        .minor  = MISC_DYNAMIC_MINOR,
 249        .fops   = &vmcp_fops,
 250};
 251
 252static int __init vmcp_init(void)
 253{
 254        int ret;
 255
 256        if (!MACHINE_IS_VM)
 257                return 0;
 258
 259        vmcp_debug = debug_register("vmcp", 1, 1, 240);
 260        if (!vmcp_debug)
 261                return -ENOMEM;
 262
 263        ret = debug_register_view(vmcp_debug, &debug_hex_ascii_view);
 264        if (ret) {
 265                debug_unregister(vmcp_debug);
 266                return ret;
 267        }
 268
 269        ret = misc_register(&vmcp_dev);
 270        if (ret)
 271                debug_unregister(vmcp_debug);
 272        return ret;
 273}
 274device_initcall(vmcp_init);
 275