cachepc-linux

Fork of AMDESE/linux with modifications for CachePC side-channel attack
git clone https://git.sinitax.com/sinitax/cachepc-linux
Log | Files | Refs | README | LICENSE | sfeed.txt

binary_stats.c (4605B)


      1// SPDX-License-Identifier: GPL-2.0-only
      2/*
      3 * KVM binary statistics interface implementation
      4 *
      5 * Copyright 2021 Google LLC
      6 */
      7
      8#include <linux/kvm_host.h>
      9#include <linux/kvm.h>
     10#include <linux/errno.h>
     11#include <linux/uaccess.h>
     12
     13/**
     14 * kvm_stats_read() - Common function to read from the binary statistics
     15 * file descriptor.
     16 *
     17 * @id: identification string of the stats
     18 * @header: stats header for a vm or a vcpu
     19 * @desc: start address of an array of stats descriptors for a vm or a vcpu
     20 * @stats: start address of stats data block for a vm or a vcpu
     21 * @size_stats: the size of stats data block pointed by @stats
     22 * @user_buffer: start address of userspace buffer
     23 * @size: requested read size from userspace
     24 * @offset: the start position from which the content will be read for the
     25 *          corresponding vm or vcp file descriptor
     26 *
     27 * The file content of a vm/vcpu file descriptor is now defined as below:
     28 * +-------------+
     29 * |   Header    |
     30 * +-------------+
     31 * |  id string  |
     32 * +-------------+
     33 * | Descriptors |
     34 * +-------------+
     35 * | Stats Data  |
     36 * +-------------+
     37 * Although this function allows userspace to read any amount of data (as long
     38 * as in the limit) from any position, the typical usage would follow below
     39 * steps:
     40 * 1. Read header from offset 0. Get the offset of descriptors and stats data
     41 *    and some other necessary information. This is a one-time work for the
     42 *    lifecycle of the corresponding vm/vcpu stats fd.
     43 * 2. Read id string from its offset. This is a one-time work for the lifecycle
     44 *    of the corresponding vm/vcpu stats fd.
     45 * 3. Read descriptors from its offset and discover all the stats by parsing
     46 *    descriptors. This is a one-time work for the lifecycle of the
     47 *    corresponding vm/vcpu stats fd.
     48 * 4. Periodically read stats data from its offset using pread.
     49 *
     50 * Return: the number of bytes that has been successfully read
     51 */
     52ssize_t kvm_stats_read(char *id, const struct kvm_stats_header *header,
     53		       const struct _kvm_stats_desc *desc,
     54		       void *stats, size_t size_stats,
     55		       char __user *user_buffer, size_t size, loff_t *offset)
     56{
     57	ssize_t len;
     58	ssize_t copylen;
     59	ssize_t remain = size;
     60	size_t size_desc;
     61	size_t size_header;
     62	void *src;
     63	loff_t pos = *offset;
     64	char __user *dest = user_buffer;
     65
     66	size_header = sizeof(*header);
     67	size_desc = header->num_desc * sizeof(*desc);
     68
     69	len = KVM_STATS_NAME_SIZE + size_header + size_desc + size_stats - pos;
     70	len = min(len, remain);
     71	if (len <= 0)
     72		return 0;
     73	remain = len;
     74
     75	/*
     76	 * Copy kvm stats header.
     77	 * The header is the first block of content userspace usually read out.
     78	 * The pos is 0 and the copylen and remain would be the size of header.
     79	 * The copy of the header would be skipped if offset is larger than the
     80	 * size of header. That usually happens when userspace reads stats
     81	 * descriptors and stats data.
     82	 */
     83	copylen = size_header - pos;
     84	copylen = min(copylen, remain);
     85	if (copylen > 0) {
     86		src = (void *)header + pos;
     87		if (copy_to_user(dest, src, copylen))
     88			return -EFAULT;
     89		remain -= copylen;
     90		pos += copylen;
     91		dest += copylen;
     92	}
     93
     94	/*
     95	 * Copy kvm stats header id string.
     96	 * The id string is unique for every vm/vcpu, which is stored in kvm
     97	 * and kvm_vcpu structure.
     98	 * The id string is part of the stat header from the perspective of
     99	 * userspace, it is usually read out together with previous constant
    100	 * header part and could be skipped for later descriptors and stats
    101	 * data readings.
    102	 */
    103	copylen = header->id_offset + KVM_STATS_NAME_SIZE - pos;
    104	copylen = min(copylen, remain);
    105	if (copylen > 0) {
    106		src = id + pos - header->id_offset;
    107		if (copy_to_user(dest, src, copylen))
    108			return -EFAULT;
    109		remain -= copylen;
    110		pos += copylen;
    111		dest += copylen;
    112	}
    113
    114	/*
    115	 * Copy kvm stats descriptors.
    116	 * The descriptors copy would be skipped in the typical case that
    117	 * userspace periodically read stats data, since the pos would be
    118	 * greater than the end address of descriptors
    119	 * (header->header.desc_offset + size_desc) causing copylen <= 0.
    120	 */
    121	copylen = header->desc_offset + size_desc - pos;
    122	copylen = min(copylen, remain);
    123	if (copylen > 0) {
    124		src = (void *)desc + pos - header->desc_offset;
    125		if (copy_to_user(dest, src, copylen))
    126			return -EFAULT;
    127		remain -= copylen;
    128		pos += copylen;
    129		dest += copylen;
    130	}
    131
    132	/* Copy kvm stats values */
    133	copylen = header->data_offset + size_stats - pos;
    134	copylen = min(copylen, remain);
    135	if (copylen > 0) {
    136		src = stats + pos - header->data_offset;
    137		if (copy_to_user(dest, src, copylen))
    138			return -EFAULT;
    139		pos += copylen;
    140	}
    141
    142	*offset = pos;
    143	return len;
    144}