blob: da6da386b59189d2e224b8f3c7b60a399cad58ec [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Avi Kivity6aa8b732006-12-10 02:21:36 -08002/*
3 * Kernel-based Virtual Machine driver for Linux
4 *
5 * This module enables machines with Intel VT-x extensions to run virtual
6 * machines without emulation or binary translation.
7 *
8 * Copyright (C) 2006 Qumranet, Inc.
Nicolas Kaiser9611c182010-10-06 14:23:22 +02009 * Copyright 2010 Red Hat, Inc. and/or its affiliates.
Avi Kivity6aa8b732006-12-10 02:21:36 -080010 *
11 * Authors:
12 * Avi Kivity <avi@qumranet.com>
13 * Yaniv Kamay <yaniv@qumranet.com>
Avi Kivity6aa8b732006-12-10 02:21:36 -080014 */
15
Andre Przywaraaf669ac2015-03-26 14:39:29 +000016#include <kvm/iodev.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080017
Avi Kivityedf88412007-12-16 11:02:48 +020018#include <linux/kvm_host.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080019#include <linux/kvm.h>
20#include <linux/module.h>
21#include <linux/errno.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080022#include <linux/percpu.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080023#include <linux/mm.h>
24#include <linux/miscdevice.h>
25#include <linux/vmalloc.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080026#include <linux/reboot.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080027#include <linux/debugfs.h>
28#include <linux/highmem.h>
29#include <linux/file.h>
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +010030#include <linux/syscore_ops.h>
Avi Kivity774c47f2007-02-12 00:54:47 -080031#include <linux/cpu.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010032#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010033#include <linux/sched/mm.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010034#include <linux/sched/stat.h>
Avi Kivityd9e368d2007-06-07 19:18:30 +030035#include <linux/cpumask.h>
36#include <linux/smp.h>
Avi Kivityd6d28162007-06-28 08:38:16 -040037#include <linux/anon_inodes.h>
Avi Kivity04d2cc72007-09-10 18:10:54 +030038#include <linux/profile.h>
Anthony Liguori7aa81cc2007-09-17 14:57:50 -050039#include <linux/kvm_para.h>
Izik Eidus6fc138d2007-10-09 19:20:39 +020040#include <linux/pagemap.h>
Anthony Liguori8d4e1282007-10-18 09:59:34 -050041#include <linux/mman.h>
Anthony Liguori35149e22008-04-02 14:46:56 -050042#include <linux/swap.h>
Sheng Yange56d5322009-03-12 21:45:39 +080043#include <linux/bitops.h>
Marcelo Tosatti547de292009-05-07 17:55:13 -030044#include <linux/spinlock.h>
Arnd Bergmann6ff58942009-10-22 14:19:27 +020045#include <linux/compat.h>
Marcelo Tosattibc6678a2009-12-23 14:35:21 -020046#include <linux/srcu.h>
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +010047#include <linux/hugetlb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090048#include <linux/slab.h>
Sasha Levin743eeb02011-07-27 16:00:48 +030049#include <linux/sort.h>
50#include <linux/bsearch.h>
Paolo Bonzinic011d232019-05-17 14:08:53 +020051#include <linux/io.h>
Wanpeng Li2eb06c32019-05-17 16:49:49 +080052#include <linux/lockdep.h>
Junaid Shahidc57c8042019-11-04 12:22:02 +010053#include <linux/kthread.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080054
Avi Kivitye4956062007-06-28 14:15:57 -040055#include <asm/processor.h>
David Matlack2ea75be2014-09-19 16:03:25 -070056#include <asm/ioctl.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080057#include <linux/uaccess.h>
Izik Eidus3e021bf2007-11-19 11:16:57 +020058#include <asm/pgtable.h>
Avi Kivity6aa8b732006-12-10 02:21:36 -080059
Laurent Vivier5f94c172008-05-30 16:05:54 +020060#include "coalesced_mmio.h"
Gleb Natapovaf585b92010-10-14 11:22:46 +020061#include "async_pf.h"
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +020062#include "vfio.h"
Laurent Vivier5f94c172008-05-30 16:05:54 +020063
Marcelo Tosatti229456f2009-06-17 09:22:14 -030064#define CREATE_TRACE_POINTS
65#include <trace/events/kvm.h>
66
Janosch Frank536a6f82016-05-18 13:26:23 +020067/* Worst case buffer size needed for holding an integer. */
68#define ITOA_MAX_LEN 12
69
Avi Kivity6aa8b732006-12-10 02:21:36 -080070MODULE_AUTHOR("Qumranet");
71MODULE_LICENSE("GPL");
72
David Hildenbrand920552b2015-09-18 12:34:53 +020073/* Architectures should define their poll value according to the halt latency */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110074unsigned int halt_poll_ns = KVM_HALT_POLL_NS_DEFAULT;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030075module_param(halt_poll_ns, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110076EXPORT_SYMBOL_GPL(halt_poll_ns);
Paolo Bonzinif7819512015-02-04 18:20:58 +010077
Wanpeng Liaca6ff22015-09-03 22:07:38 +080078/* Default doubles per-vcpu halt_poll_ns. */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110079unsigned int halt_poll_ns_grow = 2;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030080module_param(halt_poll_ns_grow, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110081EXPORT_SYMBOL_GPL(halt_poll_ns_grow);
Wanpeng Liaca6ff22015-09-03 22:07:38 +080082
Nir Weiner49113d32019-01-27 12:17:15 +020083/* The start value to grow halt_poll_ns from */
84unsigned int halt_poll_ns_grow_start = 10000; /* 10us */
85module_param(halt_poll_ns_grow_start, uint, 0644);
86EXPORT_SYMBOL_GPL(halt_poll_ns_grow_start);
87
Wanpeng Liaca6ff22015-09-03 22:07:38 +080088/* Default resets per-vcpu halt_poll_ns . */
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110089unsigned int halt_poll_ns_shrink;
Roman Storozhenko039c5d12017-06-27 12:51:18 +030090module_param(halt_poll_ns_shrink, uint, 0644);
Suraj Jitindar Singhec76d812016-10-14 11:53:19 +110091EXPORT_SYMBOL_GPL(halt_poll_ns_shrink);
Wanpeng Liaca6ff22015-09-03 22:07:38 +080092
Marcelo Tosattifa40a822009-06-04 15:08:24 -030093/*
94 * Ordering of locks:
95 *
Xiubo Lib7d409d2015-02-26 14:58:24 +080096 * kvm->lock --> kvm->slots_lock --> kvm->irq_lock
Marcelo Tosattifa40a822009-06-04 15:08:24 -030097 */
98
Junaid Shahid0d9ce162019-01-03 17:14:28 -080099DEFINE_MUTEX(kvm_lock);
Paolo Bonzini4a937f92013-09-10 12:58:35 +0200100static DEFINE_RAW_SPINLOCK(kvm_count_lock);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +0800101LIST_HEAD(vm_list);
Avi Kivity133de902007-02-12 00:54:44 -0800102
Rusty Russell7f59f492008-12-07 21:25:45 +1030103static cpumask_var_t cpus_hardware_enabled;
Xiubo Lif4fee932015-02-26 14:58:21 +0800104static int kvm_usage_count;
Alexander Graf10474ae2009-09-15 11:37:46 +0200105static atomic_t hardware_enable_failed;
Avi Kivity1b6c0162007-05-24 13:03:52 +0300106
Sean Christophersonaaba2982019-12-18 13:55:16 -0800107static struct kmem_cache *kvm_vcpu_cache;
Avi Kivity1165f5f2007-04-19 17:27:43 +0300108
Avi Kivity15ad7142007-07-11 18:17:21 +0300109static __read_mostly struct preempt_ops kvm_preempt_ops;
Paolo Bonzini7495e222020-01-09 09:57:19 -0500110static DEFINE_PER_CPU(struct kvm_vcpu *, kvm_running_vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300111
Hollis Blanchard76f7c872008-04-15 16:05:42 -0500112struct dentry *kvm_debugfs_dir;
Paul Mackerrase23a8082015-03-28 14:21:01 +1100113EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800114
Janosch Frank536a6f82016-05-18 13:26:23 +0200115static int kvm_debugfs_num_entries;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100116static const struct file_operations stat_fops_per_vm;
Janosch Frank536a6f82016-05-18 13:26:23 +0200117
Avi Kivitybccf2152007-02-21 18:04:26 +0200118static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
119 unsigned long arg);
Christian Borntraegerde8e5d72015-02-03 09:35:15 +0100120#ifdef CONFIG_KVM_COMPAT
Alexander Graf1dda6062011-06-08 02:45:37 +0200121static long kvm_vcpu_compat_ioctl(struct file *file, unsigned int ioctl,
122 unsigned long arg);
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +0100123#define KVM_COMPAT(c) .compat_ioctl = (c)
124#else
Marc Zyngier9cb09e72019-11-14 13:17:39 +0000125/*
126 * For architectures that don't implement a compat infrastructure,
127 * adopt a double line of defense:
128 * - Prevent a compat task from opening /dev/kvm
129 * - If the open has been done by a 64bit task, and the KVM fd
130 * passed to a compat task, let the ioctls fail.
131 */
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +0100132static long kvm_no_compat_ioctl(struct file *file, unsigned int ioctl,
133 unsigned long arg) { return -EINVAL; }
Marc Zyngierb9876e62019-11-13 16:05:23 +0000134
135static int kvm_no_compat_open(struct inode *inode, struct file *file)
136{
137 return is_compat_task() ? -ENODEV : 0;
138}
139#define KVM_COMPAT(c) .compat_ioctl = kvm_no_compat_ioctl, \
140 .open = kvm_no_compat_open
Alexander Graf1dda6062011-06-08 02:45:37 +0200141#endif
Alexander Graf10474ae2009-09-15 11:37:46 +0200142static int hardware_enable_all(void);
143static void hardware_disable_all(void);
Avi Kivitybccf2152007-02-21 18:04:26 +0200144
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200145static void kvm_io_bus_destroy(struct kvm_io_bus *bus);
Stephen Hemminger79408762013-12-29 12:12:29 -0800146
Paolo Bonzinibc009e42015-05-26 12:43:41 +0200147static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot, gfn_t gfn);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200148
Andi Kleen52480132014-02-08 08:51:57 +0100149__visible bool kvm_rebooting;
Avi Kivityb7c41452010-12-02 17:52:50 +0200150EXPORT_SYMBOL_GPL(kvm_rebooting);
Avi Kivity4ecac3f2008-05-13 13:23:38 +0300151
Claudio Imbrenda286de8f2017-07-12 17:56:44 +0200152#define KVM_EVENT_CREATE_VM 0
153#define KVM_EVENT_DESTROY_VM 1
154static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm);
155static unsigned long long kvm_createvm_count;
156static unsigned long long kvm_active_vms;
157
Michal Hocko93065ac2018-08-21 21:52:33 -0700158__weak int kvm_arch_mmu_notifier_invalidate_range(struct kvm *kvm,
159 unsigned long start, unsigned long end, bool blockable)
Radim Krčmářb1394e72017-11-30 19:05:45 +0100160{
Michal Hocko93065ac2018-08-21 21:52:33 -0700161 return 0;
Radim Krčmářb1394e72017-11-30 19:05:45 +0100162}
163
Sean Christophersona78986a2019-11-11 14:12:27 -0800164bool kvm_is_zone_device_pfn(kvm_pfn_t pfn)
165{
166 /*
167 * The metadata used by is_zone_device_page() to determine whether or
168 * not a page is ZONE_DEVICE is guaranteed to be valid if and only if
169 * the device has been pinned, e.g. by get_user_pages(). WARN if the
170 * page_count() is zero to help detect bad usage of this helper.
171 */
172 if (!pfn_valid(pfn) || WARN_ON_ONCE(!page_count(pfn_to_page(pfn))))
173 return false;
174
175 return is_zone_device_page(pfn_to_page(pfn));
176}
177
Dan Williamsba049e92016-01-15 16:56:11 -0800178bool kvm_is_reserved_pfn(kvm_pfn_t pfn)
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300179{
Sean Christophersona78986a2019-11-11 14:12:27 -0800180 /*
181 * ZONE_DEVICE pages currently set PG_reserved, but from a refcounting
182 * perspective they are "normal" pages, albeit with slightly different
183 * usage rules.
184 */
Andrea Arcangeli11feeb42013-07-25 03:04:38 +0200185 if (pfn_valid(pfn))
Sean Christophersona78986a2019-11-11 14:12:27 -0800186 return PageReserved(pfn_to_page(pfn)) &&
Zhuang Yanying7df003c2019-10-12 11:37:31 +0800187 !is_zero_pfn(pfn) &&
Sean Christophersona78986a2019-11-11 14:12:27 -0800188 !kvm_is_zone_device_pfn(pfn);
Ben-Ami Yassourcbff90a2008-07-28 19:26:24 +0300189
190 return true;
191}
192
Sean Christopherson005ba372020-01-08 12:24:36 -0800193bool kvm_is_transparent_hugepage(kvm_pfn_t pfn)
194{
195 struct page *page = pfn_to_page(pfn);
196
197 if (!PageTransCompoundMap(page))
198 return false;
199
200 return is_transparent_hugepage(compound_head(page));
201}
202
Avi Kivity6aa8b732006-12-10 02:21:36 -0800203/*
204 * Switches to specified vcpu, until a matching vcpu_put()
205 */
Christoffer Dallec7660c2017-12-04 21:35:23 +0100206void vcpu_load(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800207{
Christoffer Dallec7660c2017-12-04 21:35:23 +0100208 int cpu = get_cpu();
Paolo Bonzini7495e222020-01-09 09:57:19 -0500209
210 __this_cpu_write(kvm_running_vcpu, vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300211 preempt_notifier_register(&vcpu->preempt_notifier);
Carsten Otte313a3dc2007-10-11 19:16:52 +0200212 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300213 put_cpu();
Avi Kivitybccf2152007-02-21 18:04:26 +0200214}
Jim Mattson2f1fe812016-07-08 15:36:06 -0700215EXPORT_SYMBOL_GPL(vcpu_load);
Avi Kivitybccf2152007-02-21 18:04:26 +0200216
Carsten Otte313a3dc2007-10-11 19:16:52 +0200217void vcpu_put(struct kvm_vcpu *vcpu)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800218{
Avi Kivity15ad7142007-07-11 18:17:21 +0300219 preempt_disable();
Carsten Otte313a3dc2007-10-11 19:16:52 +0200220 kvm_arch_vcpu_put(vcpu);
Avi Kivity15ad7142007-07-11 18:17:21 +0300221 preempt_notifier_unregister(&vcpu->preempt_notifier);
Paolo Bonzini7495e222020-01-09 09:57:19 -0500222 __this_cpu_write(kvm_running_vcpu, NULL);
Avi Kivity15ad7142007-07-11 18:17:21 +0300223 preempt_enable();
Avi Kivity6aa8b732006-12-10 02:21:36 -0800224}
Jim Mattson2f1fe812016-07-08 15:36:06 -0700225EXPORT_SYMBOL_GPL(vcpu_put);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800226
Paolo Bonzini7a97cec2017-04-27 14:33:43 +0200227/* TODO: merge with kvm_arch_vcpu_should_kick */
228static bool kvm_request_needs_ipi(struct kvm_vcpu *vcpu, unsigned req)
229{
230 int mode = kvm_vcpu_exiting_guest_mode(vcpu);
231
232 /*
233 * We need to wait for the VCPU to reenable interrupts and get out of
234 * READING_SHADOW_PAGE_TABLES mode.
235 */
236 if (req & KVM_REQUEST_WAIT)
237 return mode != OUTSIDE_GUEST_MODE;
238
239 /*
240 * Need to kick a running VCPU, but otherwise there is nothing to do.
241 */
242 return mode == IN_GUEST_MODE;
243}
244
Avi Kivityd9e368d2007-06-07 19:18:30 +0300245static void ack_flush(void *_completed)
246{
Avi Kivityd9e368d2007-06-07 19:18:30 +0300247}
248
Paolo Bonzinib49defe2017-06-30 13:25:45 +0200249static inline bool kvm_kick_many_cpus(const struct cpumask *cpus, bool wait)
250{
251 if (unlikely(!cpus))
252 cpus = cpu_online_mask;
253
254 if (cpumask_empty(cpus))
255 return false;
256
257 smp_call_function_many(cpus, ack_flush, NULL, wait);
258 return true;
259}
260
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200261bool kvm_make_vcpus_request_mask(struct kvm *kvm, unsigned int req,
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500262 struct kvm_vcpu *except,
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200263 unsigned long *vcpu_bitmap, cpumask_var_t tmp)
Avi Kivityd9e368d2007-06-07 19:18:30 +0300264{
Avi Kivity597a5f52008-07-20 14:24:22 +0300265 int i, cpu, me;
Avi Kivityd9e368d2007-06-07 19:18:30 +0300266 struct kvm_vcpu *vcpu;
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200267 bool called;
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030268
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800269 me = get_cpu();
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200270
Gleb Natapov988a2ca2009-06-09 15:56:29 +0300271 kvm_for_each_vcpu(i, vcpu, kvm) {
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500272 if ((vcpu_bitmap && !test_bit(i, vcpu_bitmap)) ||
273 vcpu == except)
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200274 continue;
275
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800276 kvm_make_request(req, vcpu);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300277 cpu = vcpu->cpu;
Xiao Guangrong6b7e2d02011-01-12 15:40:31 +0800278
Radim Krčmář178f02f2017-04-26 22:32:26 +0200279 if (!(req & KVM_REQUEST_NO_WAKEUP) && kvm_vcpu_wake_up(vcpu))
280 continue;
Radim Krčmář6c6e8362017-04-26 22:32:23 +0200281
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200282 if (tmp != NULL && cpu != -1 && cpu != me &&
Paolo Bonzini7a97cec2017-04-27 14:33:43 +0200283 kvm_request_needs_ipi(vcpu, req))
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200284 __cpumask_set_cpu(cpu, tmp);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300285 }
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200286
287 called = kvm_kick_many_cpus(tmp, !!(req & KVM_REQUEST_WAIT));
Xiao Guangrong3cba4132011-01-12 15:41:22 +0800288 put_cpu();
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200289
290 return called;
291}
292
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500293bool kvm_make_all_cpus_request_except(struct kvm *kvm, unsigned int req,
294 struct kvm_vcpu *except)
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200295{
296 cpumask_var_t cpus;
297 bool called;
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200298
299 zalloc_cpumask_var(&cpus, GFP_ATOMIC);
300
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500301 called = kvm_make_vcpus_request_mask(kvm, req, except, NULL, cpus);
Vitaly Kuznetsov7053df42018-05-16 17:21:28 +0200302
Rusty Russell6ef7a1b2008-12-08 20:28:04 +1030303 free_cpumask_var(cpus);
Rusty Russell49846892008-12-08 20:26:24 +1030304 return called;
305}
306
Suravee Suthikulpanit54163a32020-05-06 08:17:53 -0500307bool kvm_make_all_cpus_request(struct kvm *kvm, unsigned int req)
308{
309 return kvm_make_all_cpus_request_except(kvm, req, NULL);
310}
311
Mario Smarducha6d51012015-01-15 15:58:52 -0800312#ifndef CONFIG_HAVE_KVM_ARCH_TLB_FLUSH_ALL
Rusty Russell49846892008-12-08 20:26:24 +1030313void kvm_flush_remote_tlbs(struct kvm *kvm)
314{
Lan Tianyu4ae3cb32016-03-13 11:10:28 +0800315 /*
316 * Read tlbs_dirty before setting KVM_REQ_TLB_FLUSH in
317 * kvm_make_all_cpus_request.
318 */
319 long dirty_count = smp_load_acquire(&kvm->tlbs_dirty);
Xiao Guangronga086f6a2014-04-17 17:06:12 +0800320
Lan Tianyu4ae3cb32016-03-13 11:10:28 +0800321 /*
322 * We want to publish modifications to the page tables before reading
323 * mode. Pairs with a memory barrier in arch-specific code.
324 * - x86: smp_mb__after_srcu_read_unlock in vcpu_enter_guest
325 * and smp_mb in walk_shadow_page_lockless_begin/end.
326 * - powerpc: smp_mb in kvmppc_prepare_to_enter.
327 *
328 * There is already an smp_mb__after_atomic() before
329 * kvm_make_all_cpus_request() reads vcpu->mode. We reuse that
330 * barrier here.
331 */
Tianyu Lanb08660e2018-07-19 08:40:17 +0000332 if (!kvm_arch_flush_remote_tlb(kvm)
333 || kvm_make_all_cpus_request(kvm, KVM_REQ_TLB_FLUSH))
Rusty Russell49846892008-12-08 20:26:24 +1030334 ++kvm->stat.remote_tlb_flush;
Xiao Guangronga086f6a2014-04-17 17:06:12 +0800335 cmpxchg(&kvm->tlbs_dirty, dirty_count, 0);
Avi Kivityd9e368d2007-06-07 19:18:30 +0300336}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +0530337EXPORT_SYMBOL_GPL(kvm_flush_remote_tlbs);
Mario Smarducha6d51012015-01-15 15:58:52 -0800338#endif
Avi Kivityd9e368d2007-06-07 19:18:30 +0300339
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500340void kvm_reload_remote_mmus(struct kvm *kvm)
341{
Tang Chen445b8232014-09-24 15:57:55 +0800342 kvm_make_all_cpus_request(kvm, KVM_REQ_MMU_RELOAD);
Marcelo Tosatti2e53d632008-02-20 14:47:24 -0500343}
344
Sean Christopherson8bd826d2019-12-18 13:55:30 -0800345static void kvm_vcpu_init(struct kvm_vcpu *vcpu, struct kvm *kvm, unsigned id)
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000346{
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000347 mutex_init(&vcpu->mutex);
348 vcpu->cpu = -1;
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000349 vcpu->kvm = kvm;
350 vcpu->vcpu_id = id;
Rik van Riel34bb10b2011-02-01 09:52:41 -0500351 vcpu->pid = NULL;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700352 rcuwait_init(&vcpu->wait);
Gleb Natapovaf585b92010-10-14 11:22:46 +0200353 kvm_async_pf_vcpu_init(vcpu);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000354
Feng Wubf9f6ac2015-09-18 22:29:55 +0800355 vcpu->pre_pcpu = -1;
356 INIT_LIST_HEAD(&vcpu->blocked_vcpu_list);
357
Raghavendra K T4c088492012-07-18 19:07:46 +0530358 kvm_vcpu_set_in_spin_loop(vcpu, false);
359 kvm_vcpu_set_dy_eligible(vcpu, false);
Raghavendra K T3a08a8f2013-03-04 23:32:07 +0530360 vcpu->preempted = false;
Wanpeng Lid73eb572019-07-18 19:39:06 +0800361 vcpu->ready = false;
Sean Christophersond5c48de2019-12-18 13:55:17 -0800362 preempt_notifier_init(&vcpu->preempt_notifier, &kvm_preempt_ops);
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000363}
Rusty Russellfb3f0f52007-07-27 17:16:56 +1000364
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800365void kvm_vcpu_destroy(struct kvm_vcpu *vcpu)
366{
367 kvm_arch_vcpu_destroy(vcpu);
Sean Christophersone529ef62019-12-18 13:55:15 -0800368
Sean Christopherson9941d222019-12-18 13:55:29 -0800369 /*
370 * No need for rcu_read_lock as VCPU_RUN is the only place that changes
371 * the vcpu->pid pointer, and at destruction time all file descriptors
372 * are already gone.
373 */
374 put_pid(rcu_dereference_protected(vcpu->pid, 1));
375
Sean Christopherson8bd826d2019-12-18 13:55:30 -0800376 free_page((unsigned long)vcpu->run);
Sean Christophersone529ef62019-12-18 13:55:15 -0800377 kmem_cache_free(kvm_vcpu_cache, vcpu);
Sean Christopherson4543bdc2019-12-18 13:55:14 -0800378}
379EXPORT_SYMBOL_GPL(kvm_vcpu_destroy);
380
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200381#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
382static inline struct kvm *mmu_notifier_to_kvm(struct mmu_notifier *mn)
383{
384 return container_of(mn, struct kvm, mmu_notifier);
385}
386
Izik Eidus3da0dd42009-09-23 21:47:18 +0300387static void kvm_mmu_notifier_change_pte(struct mmu_notifier *mn,
388 struct mm_struct *mm,
389 unsigned long address,
390 pte_t pte)
391{
392 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200393 int idx;
Izik Eidus3da0dd42009-09-23 21:47:18 +0300394
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200395 idx = srcu_read_lock(&kvm->srcu);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300396 spin_lock(&kvm->mmu_lock);
397 kvm->mmu_notifier_seq++;
Lan Tianyu0cf853c2018-12-06 21:21:11 +0800398
399 if (kvm_set_spte_hva(kvm, address, pte))
400 kvm_flush_remote_tlbs(kvm);
401
Izik Eidus3da0dd42009-09-23 21:47:18 +0300402 spin_unlock(&kvm->mmu_lock);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200403 srcu_read_unlock(&kvm->srcu, idx);
Izik Eidus3da0dd42009-09-23 21:47:18 +0300404}
405
Michal Hocko93065ac2018-08-21 21:52:33 -0700406static int kvm_mmu_notifier_invalidate_range_start(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800407 const struct mmu_notifier_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200408{
409 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200410 int need_tlb_flush = 0, idx;
Michal Hocko93065ac2018-08-21 21:52:33 -0700411 int ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200412
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200413 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200414 spin_lock(&kvm->mmu_lock);
415 /*
416 * The count increase must become visible at unlock time as no
417 * spte can be established without taking the mmu_lock and
418 * count is also read inside the mmu_lock critical section.
419 */
420 kvm->mmu_notifier_count++;
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800421 need_tlb_flush = kvm_unmap_hva_range(kvm, range->start, range->end);
Xiao Guangronga4ee1ca2010-11-23 11:13:00 +0800422 need_tlb_flush |= kvm->tlbs_dirty;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200423 /* we've to flush the tlb before the pages can be freed */
424 if (need_tlb_flush)
425 kvm_flush_remote_tlbs(kvm);
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900426
427 spin_unlock(&kvm->mmu_lock);
Radim Krčmářb1394e72017-11-30 19:05:45 +0100428
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800429 ret = kvm_arch_mmu_notifier_invalidate_range(kvm, range->start,
Jérôme Glissedfcd6662019-05-13 17:20:38 -0700430 range->end,
431 mmu_notifier_range_blockable(range));
Radim Krčmářb1394e72017-11-30 19:05:45 +0100432
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900433 srcu_read_unlock(&kvm->srcu, idx);
Michal Hocko93065ac2018-08-21 21:52:33 -0700434
435 return ret;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200436}
437
438static void kvm_mmu_notifier_invalidate_range_end(struct mmu_notifier *mn,
Jérôme Glisse5d6527a2018-12-28 00:38:05 -0800439 const struct mmu_notifier_range *range)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200440{
441 struct kvm *kvm = mmu_notifier_to_kvm(mn);
442
443 spin_lock(&kvm->mmu_lock);
444 /*
445 * This sequence increase will notify the kvm page fault that
446 * the page that is going to be mapped in the spte could have
447 * been freed.
448 */
449 kvm->mmu_notifier_seq++;
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000450 smp_wmb();
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200451 /*
452 * The above sequence increase must be visible before the
Paul Mackerrasa355aa52011-12-12 12:37:21 +0000453 * below count decrease, which is ensured by the smp_wmb above
454 * in conjunction with the smp_rmb in mmu_notifier_retry().
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200455 */
456 kvm->mmu_notifier_count--;
457 spin_unlock(&kvm->mmu_lock);
458
459 BUG_ON(kvm->mmu_notifier_count < 0);
460}
461
462static int kvm_mmu_notifier_clear_flush_young(struct mmu_notifier *mn,
463 struct mm_struct *mm,
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700464 unsigned long start,
465 unsigned long end)
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200466{
467 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200468 int young, idx;
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200469
Marcelo Tosattibc6678a2009-12-23 14:35:21 -0200470 idx = srcu_read_lock(&kvm->srcu);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200471 spin_lock(&kvm->mmu_lock);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200472
Andres Lagar-Cavilla57128462014-09-22 14:54:42 -0700473 young = kvm_age_hva(kvm, start, end);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200474 if (young)
475 kvm_flush_remote_tlbs(kvm);
476
Takuya Yoshikawa565f3be2012-02-10 15:28:31 +0900477 spin_unlock(&kvm->mmu_lock);
478 srcu_read_unlock(&kvm->srcu, idx);
479
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200480 return young;
481}
482
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700483static int kvm_mmu_notifier_clear_young(struct mmu_notifier *mn,
484 struct mm_struct *mm,
485 unsigned long start,
486 unsigned long end)
487{
488 struct kvm *kvm = mmu_notifier_to_kvm(mn);
489 int young, idx;
490
491 idx = srcu_read_lock(&kvm->srcu);
492 spin_lock(&kvm->mmu_lock);
493 /*
494 * Even though we do not flush TLB, this will still adversely
495 * affect performance on pre-Haswell Intel EPT, where there is
496 * no EPT Access Bit to clear so that we have to tear down EPT
497 * tables instead. If we find this unacceptable, we can always
498 * add a parameter to kvm_age_hva so that it effectively doesn't
499 * do anything on clear_young.
500 *
501 * Also note that currently we never issue secondary TLB flushes
502 * from clear_young, leaving this job up to the regular system
503 * cadence. If we find this inaccurate, we might come up with a
504 * more sophisticated heuristic later.
505 */
506 young = kvm_age_hva(kvm, start, end);
507 spin_unlock(&kvm->mmu_lock);
508 srcu_read_unlock(&kvm->srcu, idx);
509
510 return young;
511}
512
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800513static int kvm_mmu_notifier_test_young(struct mmu_notifier *mn,
514 struct mm_struct *mm,
515 unsigned long address)
516{
517 struct kvm *kvm = mmu_notifier_to_kvm(mn);
518 int young, idx;
519
520 idx = srcu_read_lock(&kvm->srcu);
521 spin_lock(&kvm->mmu_lock);
522 young = kvm_test_age_hva(kvm, address);
523 spin_unlock(&kvm->mmu_lock);
524 srcu_read_unlock(&kvm->srcu, idx);
525
526 return young;
527}
528
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100529static void kvm_mmu_notifier_release(struct mmu_notifier *mn,
530 struct mm_struct *mm)
531{
532 struct kvm *kvm = mmu_notifier_to_kvm(mn);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800533 int idx;
534
535 idx = srcu_read_lock(&kvm->srcu);
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300536 kvm_arch_flush_shadow_all(kvm);
Lai Jiangshaneda2bed2010-04-20 14:29:29 +0800537 srcu_read_unlock(&kvm->srcu, idx);
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100538}
539
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200540static const struct mmu_notifier_ops kvm_mmu_notifier_ops = {
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200541 .invalidate_range_start = kvm_mmu_notifier_invalidate_range_start,
542 .invalidate_range_end = kvm_mmu_notifier_invalidate_range_end,
543 .clear_flush_young = kvm_mmu_notifier_clear_flush_young,
Vladimir Davydov1d7715c2015-09-09 15:35:41 -0700544 .clear_young = kvm_mmu_notifier_clear_young,
Andrea Arcangeli8ee53822011-01-13 15:47:10 -0800545 .test_young = kvm_mmu_notifier_test_young,
Izik Eidus3da0dd42009-09-23 21:47:18 +0300546 .change_pte = kvm_mmu_notifier_change_pte,
Marcelo Tosatti85db06e2008-12-10 21:23:26 +0100547 .release = kvm_mmu_notifier_release,
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200548};
Avi Kivity4c07b0a2009-12-20 14:54:04 +0200549
550static int kvm_init_mmu_notifier(struct kvm *kvm)
551{
552 kvm->mmu_notifier.ops = &kvm_mmu_notifier_ops;
553 return mmu_notifier_register(&kvm->mmu_notifier, current->mm);
554}
555
556#else /* !(CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER) */
557
558static int kvm_init_mmu_notifier(struct kvm *kvm)
559{
560 return 0;
561}
562
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200563#endif /* CONFIG_MMU_NOTIFIER && KVM_ARCH_WANT_MMU_NOTIFIER */
564
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200565static struct kvm_memslots *kvm_alloc_memslots(void)
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800566{
567 int i;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200568 struct kvm_memslots *slots;
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800569
Ben Gardonb12ce362019-02-11 11:02:49 -0800570 slots = kvzalloc(sizeof(struct kvm_memslots), GFP_KERNEL_ACCOUNT);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200571 if (!slots)
572 return NULL;
573
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800574 for (i = 0; i < KVM_MEM_SLOTS_NUM; i++)
Sean Christopherson36947252020-02-18 13:07:32 -0800575 slots->id_to_index[i] = -1;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200576
577 return slots;
578}
579
580static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot)
581{
582 if (!memslot->dirty_bitmap)
583 return;
584
585 kvfree(memslot->dirty_bitmap);
586 memslot->dirty_bitmap = NULL;
587}
588
Sean Christophersone96c81e2020-02-18 13:07:27 -0800589static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200590{
Sean Christophersone96c81e2020-02-18 13:07:27 -0800591 kvm_destroy_dirty_bitmap(slot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200592
Sean Christophersone96c81e2020-02-18 13:07:27 -0800593 kvm_arch_free_memslot(kvm, slot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200594
Sean Christophersone96c81e2020-02-18 13:07:27 -0800595 slot->flags = 0;
596 slot->npages = 0;
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200597}
598
599static void kvm_free_memslots(struct kvm *kvm, struct kvm_memslots *slots)
600{
601 struct kvm_memory_slot *memslot;
602
603 if (!slots)
604 return;
605
606 kvm_for_each_memslot(memslot, slots)
Sean Christophersone96c81e2020-02-18 13:07:27 -0800607 kvm_free_memslot(kvm, memslot);
Paolo Bonzinia47d2b02015-05-17 11:41:37 +0200608
609 kvfree(slots);
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800610}
611
Janosch Frank536a6f82016-05-18 13:26:23 +0200612static void kvm_destroy_vm_debugfs(struct kvm *kvm)
613{
614 int i;
615
616 if (!kvm->debugfs_dentry)
617 return;
618
619 debugfs_remove_recursive(kvm->debugfs_dentry);
620
Luiz Capitulino9d5a1dc2016-09-07 14:47:21 -0400621 if (kvm->debugfs_stat_data) {
622 for (i = 0; i < kvm_debugfs_num_entries; i++)
623 kfree(kvm->debugfs_stat_data[i]);
624 kfree(kvm->debugfs_stat_data);
625 }
Janosch Frank536a6f82016-05-18 13:26:23 +0200626}
627
628static int kvm_create_vm_debugfs(struct kvm *kvm, int fd)
629{
630 char dir_name[ITOA_MAX_LEN * 2];
631 struct kvm_stat_data *stat_data;
632 struct kvm_stats_debugfs_item *p;
633
634 if (!debugfs_initialized())
635 return 0;
636
637 snprintf(dir_name, sizeof(dir_name), "%d-%d", task_pid_nr(current), fd);
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +0200638 kvm->debugfs_dentry = debugfs_create_dir(dir_name, kvm_debugfs_dir);
Janosch Frank536a6f82016-05-18 13:26:23 +0200639
640 kvm->debugfs_stat_data = kcalloc(kvm_debugfs_num_entries,
641 sizeof(*kvm->debugfs_stat_data),
Ben Gardonb12ce362019-02-11 11:02:49 -0800642 GFP_KERNEL_ACCOUNT);
Janosch Frank536a6f82016-05-18 13:26:23 +0200643 if (!kvm->debugfs_stat_data)
644 return -ENOMEM;
645
646 for (p = debugfs_entries; p->name; p++) {
Ben Gardonb12ce362019-02-11 11:02:49 -0800647 stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
Janosch Frank536a6f82016-05-18 13:26:23 +0200648 if (!stat_data)
649 return -ENOMEM;
650
651 stat_data->kvm = kvm;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100652 stat_data->dbgfs_item = p;
Janosch Frank536a6f82016-05-18 13:26:23 +0200653 kvm->debugfs_stat_data[p - debugfs_entries] = stat_data;
Milan Pandurov09cbcef2019-12-13 14:07:21 +0100654 debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
655 kvm->debugfs_dentry, stat_data,
656 &stat_fops_per_vm);
Janosch Frank536a6f82016-05-18 13:26:23 +0200657 }
658 return 0;
659}
660
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100661/*
662 * Called after the VM is otherwise initialized, but just before adding it to
663 * the vm_list.
664 */
665int __weak kvm_arch_post_init_vm(struct kvm *kvm)
666{
667 return 0;
668}
669
670/*
671 * Called just after removing the VM from the vm_list, but before doing any
672 * other destruction.
673 */
674void __weak kvm_arch_pre_destroy_vm(struct kvm *kvm)
675{
676}
677
Carsten Ottee08b9632012-01-04 10:25:20 +0100678static struct kvm *kvm_create_vm(unsigned long type)
Avi Kivity6aa8b732006-12-10 02:21:36 -0800679{
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100680 struct kvm *kvm = kvm_arch_alloc_vm();
Jim Mattson91219232019-10-24 16:03:26 -0700681 int r = -ENOMEM;
682 int i;
Avi Kivity6aa8b732006-12-10 02:21:36 -0800683
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100684 if (!kvm)
685 return ERR_PTR(-ENOMEM);
686
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100687 spin_lock_init(&kvm->mmu_lock);
Vegard Nossumf1f10072017-02-27 14:30:07 -0800688 mmgrab(current->mm);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100689 kvm->mm = current->mm;
690 kvm_eventfd_init(kvm);
691 mutex_init(&kvm->lock);
692 mutex_init(&kvm->irq_lock);
693 mutex_init(&kvm->slots_lock);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100694 INIT_LIST_HEAD(&kvm->devices);
695
Jim Mattson91219232019-10-24 16:03:26 -0700696 BUILD_BUG_ON(KVM_MEM_SLOTS_NUM > SHRT_MAX);
697
Paolo Bonzini8a441192019-11-04 12:16:49 +0100698 if (init_srcu_struct(&kvm->srcu))
699 goto out_err_no_srcu;
700 if (init_srcu_struct(&kvm->irq_srcu))
701 goto out_err_no_irq_srcu;
702
Paolo Bonzinie2d3fca2019-11-04 13:23:53 +0100703 refcount_set(&kvm->users_count, 1);
Jim Mattson91219232019-10-24 16:03:26 -0700704 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
705 struct kvm_memslots *slots = kvm_alloc_memslots();
706
707 if (!slots)
Jim Mattsona97b0e72019-10-25 13:34:58 +0200708 goto out_err_no_arch_destroy_vm;
Jim Mattson91219232019-10-24 16:03:26 -0700709 /* Generations must be different for each address space. */
710 slots->generation = i;
711 rcu_assign_pointer(kvm->memslots[i], slots);
712 }
713
714 for (i = 0; i < KVM_NR_BUSES; i++) {
715 rcu_assign_pointer(kvm->buses[i],
716 kzalloc(sizeof(struct kvm_io_bus), GFP_KERNEL_ACCOUNT));
717 if (!kvm->buses[i])
Jim Mattsona97b0e72019-10-25 13:34:58 +0200718 goto out_err_no_arch_destroy_vm;
Jim Mattson91219232019-10-24 16:03:26 -0700719 }
720
David Matlackacd05782020-04-17 15:14:46 -0700721 kvm->max_halt_poll_ns = halt_poll_ns;
722
Carsten Ottee08b9632012-01-04 10:25:20 +0100723 r = kvm_arch_init_vm(kvm, type);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100724 if (r)
Jim Mattsona97b0e72019-10-25 13:34:58 +0200725 goto out_err_no_arch_destroy_vm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200726
727 r = hardware_enable_all();
728 if (r)
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100729 goto out_err_no_disable;
Alexander Graf10474ae2009-09-15 11:37:46 +0200730
Paolo Bonzinic77dcac2014-08-06 14:24:45 +0200731#ifdef CONFIG_HAVE_KVM_IRQFD
Gleb Natapov136bdfe2009-08-24 11:54:23 +0300732 INIT_HLIST_HEAD(&kvm->irq_ack_notifier_list);
Avi Kivity75858a82009-01-04 17:10:50 +0200733#endif
Avi Kivity6aa8b732006-12-10 02:21:36 -0800734
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700735 r = kvm_init_mmu_notifier(kvm);
736 if (r)
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100737 goto out_err_no_mmu_notifier;
738
739 r = kvm_arch_post_init_vm(kvm);
740 if (r)
Mike Waychison74b5c5b2011-06-03 13:04:53 -0700741 goto out_err;
742
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800743 mutex_lock(&kvm_lock);
Rusty Russell5e58cfe2007-07-23 17:08:21 +1000744 list_add(&kvm->vm_list, &vm_list);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800745 mutex_unlock(&kvm_lock);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100746
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200747 preempt_notifier_inc();
748
Avi Kivityf17abe92007-02-21 19:28:04 +0200749 return kvm;
Alexander Graf10474ae2009-09-15 11:37:46 +0200750
751out_err:
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100752#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
753 if (kvm->mmu_notifier.ops)
754 mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
755#endif
756out_err_no_mmu_notifier:
Alexander Graf10474ae2009-09-15 11:37:46 +0200757 hardware_disable_all();
Christian Borntraeger719d93c2014-01-16 13:44:20 +0100758out_err_no_disable:
Jim Mattsona97b0e72019-10-25 13:34:58 +0200759 kvm_arch_destroy_vm(kvm);
Jim Mattsona97b0e72019-10-25 13:34:58 +0200760out_err_no_arch_destroy_vm:
Paolo Bonzinie2d3fca2019-11-04 13:23:53 +0100761 WARN_ON_ONCE(!refcount_dec_and_test(&kvm->users_count));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200762 for (i = 0; i < KVM_NR_BUSES; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200763 kfree(kvm_get_bus(kvm, i));
Paolo Bonzinif481b062015-05-17 17:30:37 +0200764 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200765 kvm_free_memslots(kvm, __kvm_memslots(kvm, i));
Paolo Bonzini8a441192019-11-04 12:16:49 +0100766 cleanup_srcu_struct(&kvm->irq_srcu);
767out_err_no_irq_srcu:
768 cleanup_srcu_struct(&kvm->srcu);
769out_err_no_srcu:
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100770 kvm_arch_free_vm(kvm);
Paolo Bonzinie9ad4ec2016-03-21 10:15:25 +0100771 mmdrop(current->mm);
Alexander Graf10474ae2009-09-15 11:37:46 +0200772 return ERR_PTR(r);
Avi Kivityf17abe92007-02-21 19:28:04 +0200773}
774
Scott Wood07f0a7b2013-04-25 14:11:23 +0000775static void kvm_destroy_devices(struct kvm *kvm)
776{
Geliang Tange6e3b5a2016-01-01 19:47:12 +0800777 struct kvm_device *dev, *tmp;
Scott Wood07f0a7b2013-04-25 14:11:23 +0000778
Christoffer Dalla28ebea2016-08-09 19:13:01 +0200779 /*
780 * We do not need to take the kvm->lock here, because nobody else
781 * has a reference to the struct kvm at this point and therefore
782 * cannot access the devices list anyhow.
783 */
Geliang Tange6e3b5a2016-01-01 19:47:12 +0800784 list_for_each_entry_safe(dev, tmp, &kvm->devices, vm_node) {
785 list_del(&dev->vm_node);
Scott Wood07f0a7b2013-04-25 14:11:23 +0000786 dev->ops->destroy(dev);
787 }
788}
789
Avi Kivityf17abe92007-02-21 19:28:04 +0200790static void kvm_destroy_vm(struct kvm *kvm)
791{
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200792 int i;
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200793 struct mm_struct *mm = kvm->mm;
794
Claudio Imbrenda286de8f2017-07-12 17:56:44 +0200795 kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm);
Janosch Frank536a6f82016-05-18 13:26:23 +0200796 kvm_destroy_vm_debugfs(kvm);
Sheng Yangad8ba2c2009-01-06 10:03:02 +0800797 kvm_arch_sync_events(kvm);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800798 mutex_lock(&kvm_lock);
Avi Kivity133de902007-02-12 00:54:44 -0800799 list_del(&kvm->vm_list);
Junaid Shahid0d9ce162019-01-03 17:14:28 -0800800 mutex_unlock(&kvm_lock);
Junaid Shahid1aa9b952019-11-04 20:26:00 +0100801 kvm_arch_pre_destroy_vm(kvm);
802
Avi Kivity399ec802008-11-19 13:58:46 +0200803 kvm_free_irq_routing(kvm);
Peter Xudf630b82017-03-15 16:01:17 +0800804 for (i = 0; i < KVM_NR_BUSES; i++) {
Paolo Bonzini3898da92017-08-02 17:55:54 +0200805 struct kvm_io_bus *bus = kvm_get_bus(kvm, i);
Christian Borntraeger4a12f952017-07-07 10:51:38 +0200806
Christian Borntraeger4a12f952017-07-07 10:51:38 +0200807 if (bus)
808 kvm_io_bus_destroy(bus);
Peter Xudf630b82017-03-15 16:01:17 +0800809 kvm->buses[i] = NULL;
810 }
Avi Kivity980da6c2009-12-20 15:13:43 +0200811 kvm_coalesced_mmio_free(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200812#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
813 mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
Gleb Natapovf00be0c2009-03-19 12:20:36 +0200814#else
Marcelo Tosatti2df72e92012-08-24 15:54:57 -0300815 kvm_arch_flush_shadow_all(kvm);
Andrea Arcangelie930bff2008-07-25 16:24:52 +0200816#endif
Zhang Xiantaod19a9cd2007-11-18 18:43:45 +0800817 kvm_arch_destroy_vm(kvm);
Scott Wood07f0a7b2013-04-25 14:11:23 +0000818 kvm_destroy_devices(kvm);
Paolo Bonzinif481b062015-05-17 17:30:37 +0200819 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++)
Paolo Bonzini3898da92017-08-02 17:55:54 +0200820 kvm_free_memslots(kvm, __kvm_memslots(kvm, i));
Paolo Bonzini820b3fc2014-06-03 13:44:17 +0200821 cleanup_srcu_struct(&kvm->irq_srcu);
Jan Kiszkad89f5ef2010-11-09 17:02:49 +0100822 cleanup_srcu_struct(&kvm->srcu);
823 kvm_arch_free_vm(kvm);
Peter Zijlstra2ecd9d22015-07-03 18:53:58 +0200824 preempt_notifier_dec();
Alexander Graf10474ae2009-09-15 11:37:46 +0200825 hardware_disable_all();
Avi Kivity6d4e4c42007-11-21 16:41:05 +0200826 mmdrop(mm);
Avi Kivityf17abe92007-02-21 19:28:04 +0200827}
828
Izik Eidusd39f13b2008-03-30 16:01:25 +0300829void kvm_get_kvm(struct kvm *kvm)
830{
Elena Reshetovae3736c32017-02-20 13:06:21 +0200831 refcount_inc(&kvm->users_count);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300832}
833EXPORT_SYMBOL_GPL(kvm_get_kvm);
834
835void kvm_put_kvm(struct kvm *kvm)
836{
Elena Reshetovae3736c32017-02-20 13:06:21 +0200837 if (refcount_dec_and_test(&kvm->users_count))
Izik Eidusd39f13b2008-03-30 16:01:25 +0300838 kvm_destroy_vm(kvm);
839}
840EXPORT_SYMBOL_GPL(kvm_put_kvm);
841
Sean Christopherson149487b2019-10-21 15:58:42 -0700842/*
843 * Used to put a reference that was taken on behalf of an object associated
844 * with a user-visible file descriptor, e.g. a vcpu or device, if installation
845 * of the new file descriptor fails and the reference cannot be transferred to
846 * its final owner. In such cases, the caller is still actively using @kvm and
847 * will fail miserably if the refcount unexpectedly hits zero.
848 */
849void kvm_put_kvm_no_destroy(struct kvm *kvm)
850{
851 WARN_ON(refcount_dec_and_test(&kvm->users_count));
852}
853EXPORT_SYMBOL_GPL(kvm_put_kvm_no_destroy);
Izik Eidusd39f13b2008-03-30 16:01:25 +0300854
Avi Kivityf17abe92007-02-21 19:28:04 +0200855static int kvm_vm_release(struct inode *inode, struct file *filp)
856{
857 struct kvm *kvm = filp->private_data;
858
Gregory Haskins721eecbf2009-05-20 10:30:49 -0400859 kvm_irqfd_release(kvm);
860
Izik Eidusd39f13b2008-03-30 16:01:25 +0300861 kvm_put_kvm(kvm);
Avi Kivity6aa8b732006-12-10 02:21:36 -0800862 return 0;
863}
864
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900865/*
866 * Allocation size is twice as large as the actual dirty bitmap size.
Sean Christopherson0dff0842020-02-18 13:07:29 -0800867 * See kvm_vm_ioctl_get_dirty_log() why this is needed.
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900868 */
Jay Zhou3c9bd402020-02-27 09:32:27 +0800869static int kvm_alloc_dirty_bitmap(struct kvm_memory_slot *memslot)
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900870{
Takuya Yoshikawa515a0122010-10-27 18:23:54 +0900871 unsigned long dirty_bytes = 2 * kvm_dirty_bitmap_bytes(memslot);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900872
Ben Gardonb12ce362019-02-11 11:02:49 -0800873 memslot->dirty_bitmap = kvzalloc(dirty_bytes, GFP_KERNEL_ACCOUNT);
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900874 if (!memslot->dirty_bitmap)
875 return -ENOMEM;
876
Takuya Yoshikawaa36a57b12010-10-27 18:22:19 +0900877 return 0;
878}
879
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800880/*
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800881 * Delete a memslot by decrementing the number of used slots and shifting all
882 * other entries in the array forward one spot.
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800883 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800884static inline void kvm_memslot_delete(struct kvm_memslots *slots,
885 struct kvm_memory_slot *memslot)
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +0800886{
Igor Mammedov063584d2014-11-13 23:00:13 +0000887 struct kvm_memory_slot *mslots = slots->memslots;
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800888 int i;
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800889
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800890 if (WARN_ON(slots->id_to_index[memslot->id] == -1))
891 return;
Igor Mammedov0e60b072014-12-01 17:29:26 +0000892
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800893 slots->used_slots--;
894
Sean Christopherson0774a962020-03-20 13:55:40 -0700895 if (atomic_read(&slots->lru_slot) >= slots->used_slots)
896 atomic_set(&slots->lru_slot, 0);
897
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800898 for (i = slots->id_to_index[memslot->id]; i < slots->used_slots; i++) {
Igor Mammedov7f379cf2014-12-01 17:29:24 +0000899 mslots[i] = mslots[i + 1];
900 slots->id_to_index[mslots[i].id] = i;
Igor Mammedov7f379cf2014-12-01 17:29:24 +0000901 }
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800902 mslots[i] = *memslot;
903 slots->id_to_index[memslot->id] = -1;
904}
905
906/*
907 * "Insert" a new memslot by incrementing the number of used slots. Returns
908 * the new slot's initial index into the memslots array.
909 */
910static inline int kvm_memslot_insert_back(struct kvm_memslots *slots)
911{
912 return slots->used_slots++;
913}
914
915/*
916 * Move a changed memslot backwards in the array by shifting existing slots
917 * with a higher GFN toward the front of the array. Note, the changed memslot
918 * itself is not preserved in the array, i.e. not swapped at this time, only
919 * its new index into the array is tracked. Returns the changed memslot's
920 * current index into the memslots array.
921 */
922static inline int kvm_memslot_move_backward(struct kvm_memslots *slots,
923 struct kvm_memory_slot *memslot)
924{
925 struct kvm_memory_slot *mslots = slots->memslots;
926 int i;
927
928 if (WARN_ON_ONCE(slots->id_to_index[memslot->id] == -1) ||
929 WARN_ON_ONCE(!slots->used_slots))
930 return -1;
Paolo Bonziniefbeec72014-12-27 18:01:00 +0100931
932 /*
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800933 * Move the target memslot backward in the array by shifting existing
934 * memslots with a higher GFN (than the target memslot) towards the
935 * front of the array.
Paolo Bonziniefbeec72014-12-27 18:01:00 +0100936 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800937 for (i = slots->id_to_index[memslot->id]; i < slots->used_slots - 1; i++) {
938 if (memslot->base_gfn > mslots[i + 1].base_gfn)
939 break;
Xiao Guangrongf85e2cb2011-11-24 17:41:54 +0800940
Sean Christopherson0577d1a2020-02-18 13:07:31 -0800941 WARN_ON_ONCE(memslot->base_gfn == mslots[i + 1].base_gfn);
942
943 /* Shift the next memslot forward one and update its index. */
944 mslots[i] = mslots[i + 1];
945 slots->id_to_index[mslots[i].id] = i;
946 }
947 return i;
948}
949
950/*
951 * Move a changed memslot forwards in the array by shifting existing slots with
952 * a lower GFN toward the back of the array. Note, the changed memslot itself
953 * is not preserved in the array, i.e. not swapped at this time, only its new
954 * index into the array is tracked. Returns the changed memslot's final index
955 * into the memslots array.
956 */
957static inline int kvm_memslot_move_forward(struct kvm_memslots *slots,
958 struct kvm_memory_slot *memslot,
959 int start)
960{
961 struct kvm_memory_slot *mslots = slots->memslots;
962 int i;
963
964 for (i = start; i > 0; i--) {
965 if (memslot->base_gfn < mslots[i - 1].base_gfn)
966 break;
967
968 WARN_ON_ONCE(memslot->base_gfn == mslots[i - 1].base_gfn);
969
970 /* Shift the next memslot back one and update its index. */
971 mslots[i] = mslots[i - 1];
972 slots->id_to_index[mslots[i].id] = i;
973 }
974 return i;
975}
976
977/*
978 * Re-sort memslots based on their GFN to account for an added, deleted, or
979 * moved memslot. Sorting memslots by GFN allows using a binary search during
980 * memslot lookup.
981 *
982 * IMPORTANT: Slots are sorted from highest GFN to lowest GFN! I.e. the entry
983 * at memslots[0] has the highest GFN.
984 *
985 * The sorting algorithm takes advantage of having initially sorted memslots
986 * and knowing the position of the changed memslot. Sorting is also optimized
987 * by not swapping the updated memslot and instead only shifting other memslots
988 * and tracking the new index for the update memslot. Only once its final
989 * index is known is the updated memslot copied into its position in the array.
990 *
991 * - When deleting a memslot, the deleted memslot simply needs to be moved to
992 * the end of the array.
993 *
994 * - When creating a memslot, the algorithm "inserts" the new memslot at the
995 * end of the array and then it forward to its correct location.
996 *
997 * - When moving a memslot, the algorithm first moves the updated memslot
998 * backward to handle the scenario where the memslot's GFN was changed to a
999 * lower value. update_memslots() then falls through and runs the same flow
1000 * as creating a memslot to move the memslot forward to handle the scenario
1001 * where its GFN was changed to a higher value.
1002 *
1003 * Note, slots are sorted from highest->lowest instead of lowest->highest for
1004 * historical reasons. Originally, invalid memslots where denoted by having
1005 * GFN=0, thus sorting from highest->lowest naturally sorted invalid memslots
1006 * to the end of the array. The current algorithm uses dedicated logic to
1007 * delete a memslot and thus does not rely on invalid memslots having GFN=0.
1008 *
1009 * The other historical motiviation for highest->lowest was to improve the
1010 * performance of memslot lookup. KVM originally used a linear search starting
1011 * at memslots[0]. On x86, the largest memslot usually has one of the highest,
1012 * if not *the* highest, GFN, as the bulk of the guest's RAM is located in a
1013 * single memslot above the 4gb boundary. As the largest memslot is also the
1014 * most likely to be referenced, sorting it to the front of the array was
1015 * advantageous. The current binary search starts from the middle of the array
1016 * and uses an LRU pointer to improve performance for all memslots and GFNs.
1017 */
1018static void update_memslots(struct kvm_memslots *slots,
1019 struct kvm_memory_slot *memslot,
1020 enum kvm_mr_change change)
1021{
1022 int i;
1023
1024 if (change == KVM_MR_DELETE) {
1025 kvm_memslot_delete(slots, memslot);
1026 } else {
1027 if (change == KVM_MR_CREATE)
1028 i = kvm_memslot_insert_back(slots);
1029 else
1030 i = kvm_memslot_move_backward(slots, memslot);
1031 i = kvm_memslot_move_forward(slots, memslot, i);
1032
1033 /*
1034 * Copy the memslot to its new position in memslots and update
1035 * its index accordingly.
1036 */
1037 slots->memslots[i] = *memslot;
1038 slots->id_to_index[memslot->id] = i;
1039 }
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +08001040}
1041
Paolo Bonzini09170a42015-05-18 13:59:39 +02001042static int check_memory_region_flags(const struct kvm_userspace_memory_region *mem)
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001043{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001044 u32 valid_flags = KVM_MEM_LOG_DIRTY_PAGES;
1045
Christoffer Dall0f8a4de2014-08-26 14:00:37 +02001046#ifdef __KVM_HAVE_READONLY_MEM
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001047 valid_flags |= KVM_MEM_READONLY;
1048#endif
1049
1050 if (mem->flags & ~valid_flags)
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001051 return -EINVAL;
1052
1053 return 0;
1054}
1055
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001056static struct kvm_memslots *install_new_memslots(struct kvm *kvm,
Paolo Bonzinif481b062015-05-17 17:30:37 +02001057 int as_id, struct kvm_memslots *slots)
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001058{
Paolo Bonzinif481b062015-05-17 17:30:37 +02001059 struct kvm_memslots *old_memslots = __kvm_memslots(kvm, as_id);
Sean Christopherson361209e2019-02-05 13:01:14 -08001060 u64 gen = old_memslots->generation;
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001061
Sean Christopherson361209e2019-02-05 13:01:14 -08001062 WARN_ON(gen & KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS);
1063 slots->generation = gen | KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
David Matlackee3d1572014-08-18 15:46:06 -07001064
Paolo Bonzinif481b062015-05-17 17:30:37 +02001065 rcu_assign_pointer(kvm->memslots[as_id], slots);
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001066 synchronize_srcu_expedited(&kvm->srcu);
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001067
David Matlackee3d1572014-08-18 15:46:06 -07001068 /*
Sean Christopherson361209e2019-02-05 13:01:14 -08001069 * Increment the new memslot generation a second time, dropping the
Miaohe Lin00116792019-12-11 14:26:23 +08001070 * update in-progress flag and incrementing the generation based on
Sean Christopherson361209e2019-02-05 13:01:14 -08001071 * the number of address spaces. This provides a unique and easily
1072 * identifiable generation number while the memslots are in flux.
1073 */
1074 gen = slots->generation & ~KVM_MEMSLOT_GEN_UPDATE_IN_PROGRESS;
1075
1076 /*
Paolo Bonzini4bd518f2017-02-03 20:44:51 -08001077 * Generations must be unique even across address spaces. We do not need
1078 * a global counter for that, instead the generation space is evenly split
1079 * across address spaces. For example, with two address spaces, address
Sean Christopherson164bf7e2019-02-05 13:01:18 -08001080 * space 0 will use generations 0, 2, 4, ... while address space 1 will
1081 * use generations 1, 3, 5, ...
David Matlackee3d1572014-08-18 15:46:06 -07001082 */
Sean Christopherson164bf7e2019-02-05 13:01:18 -08001083 gen += KVM_ADDRESS_SPACE_NUM;
David Matlackee3d1572014-08-18 15:46:06 -07001084
Sean Christopherson15248252019-02-05 12:54:17 -08001085 kvm_arch_memslots_updated(kvm, gen);
1086
1087 slots->generation = gen;
Takuya Yoshikawae59dbe02013-07-04 13:40:29 +09001088
1089 return old_memslots;
Gleb Natapov7ec4fb42012-12-24 17:49:30 +02001090}
1091
Sean Christopherson36947252020-02-18 13:07:32 -08001092/*
1093 * Note, at a minimum, the current number of used slots must be allocated, even
1094 * when deleting a memslot, as we need a complete duplicate of the memslots for
1095 * use when invalidating a memslot prior to deleting/moving the memslot.
1096 */
1097static struct kvm_memslots *kvm_dup_memslots(struct kvm_memslots *old,
1098 enum kvm_mr_change change)
1099{
1100 struct kvm_memslots *slots;
1101 size_t old_size, new_size;
1102
1103 old_size = sizeof(struct kvm_memslots) +
1104 (sizeof(struct kvm_memory_slot) * old->used_slots);
1105
1106 if (change == KVM_MR_CREATE)
1107 new_size = old_size + sizeof(struct kvm_memory_slot);
1108 else
1109 new_size = old_size;
1110
1111 slots = kvzalloc(new_size, GFP_KERNEL_ACCOUNT);
1112 if (likely(slots))
1113 memcpy(slots, old, old_size);
1114
1115 return slots;
1116}
1117
Sean Christophersoncf47f502020-02-18 13:07:23 -08001118static int kvm_set_memslot(struct kvm *kvm,
1119 const struct kvm_userspace_memory_region *mem,
Sean Christopherson9d4c1972020-02-18 13:07:24 -08001120 struct kvm_memory_slot *old,
Sean Christophersoncf47f502020-02-18 13:07:23 -08001121 struct kvm_memory_slot *new, int as_id,
1122 enum kvm_mr_change change)
1123{
1124 struct kvm_memory_slot *slot;
1125 struct kvm_memslots *slots;
1126 int r;
1127
Sean Christopherson36947252020-02-18 13:07:32 -08001128 slots = kvm_dup_memslots(__kvm_memslots(kvm, as_id), change);
Sean Christophersoncf47f502020-02-18 13:07:23 -08001129 if (!slots)
1130 return -ENOMEM;
Sean Christophersoncf47f502020-02-18 13:07:23 -08001131
1132 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE) {
1133 /*
1134 * Note, the INVALID flag needs to be in the appropriate entry
1135 * in the freshly allocated memslots, not in @old or @new.
1136 */
1137 slot = id_to_memslot(slots, old->id);
1138 slot->flags |= KVM_MEMSLOT_INVALID;
1139
1140 /*
1141 * We can re-use the old memslots, the only difference from the
1142 * newly installed memslots is the invalid flag, which will get
1143 * dropped by update_memslots anyway. We'll also revert to the
1144 * old memslots if preparing the new memory region fails.
1145 */
1146 slots = install_new_memslots(kvm, as_id, slots);
1147
1148 /* From this point no new shadow pages pointing to a deleted,
1149 * or moved, memslot will be created.
1150 *
1151 * validation of sp->gfn happens in:
1152 * - gfn_to_hva (kvm_read_guest, gfn_to_pfn)
1153 * - kvm_is_visible_gfn (mmu_check_root)
1154 */
1155 kvm_arch_flush_shadow_memslot(kvm, slot);
1156 }
1157
1158 r = kvm_arch_prepare_memory_region(kvm, new, mem, change);
1159 if (r)
1160 goto out_slots;
1161
1162 update_memslots(slots, new, change);
1163 slots = install_new_memslots(kvm, as_id, slots);
1164
1165 kvm_arch_commit_memory_region(kvm, mem, old, new, change);
1166
1167 kvfree(slots);
1168 return 0;
1169
1170out_slots:
1171 if (change == KVM_MR_DELETE || change == KVM_MR_MOVE)
1172 slots = install_new_memslots(kvm, as_id, slots);
1173 kvfree(slots);
1174 return r;
1175}
1176
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001177static int kvm_delete_memslot(struct kvm *kvm,
1178 const struct kvm_userspace_memory_region *mem,
1179 struct kvm_memory_slot *old, int as_id)
1180{
1181 struct kvm_memory_slot new;
1182 int r;
1183
1184 if (!old->npages)
1185 return -EINVAL;
1186
1187 memset(&new, 0, sizeof(new));
1188 new.id = old->id;
1189
1190 r = kvm_set_memslot(kvm, mem, old, &new, as_id, KVM_MR_DELETE);
1191 if (r)
1192 return r;
1193
Sean Christophersone96c81e2020-02-18 13:07:27 -08001194 kvm_free_memslot(kvm, old);
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001195 return 0;
1196}
1197
Avi Kivity6aa8b732006-12-10 02:21:36 -08001198/*
Avi Kivity6aa8b732006-12-10 02:21:36 -08001199 * Allocate some memory and give it an address in the guest physical address
1200 * space.
1201 *
1202 * Discontiguous memory is allowed, mostly for framebuffers.
Sheng Yangf78e0e22007-10-29 09:40:42 +08001203 *
Dominik Dingel02d5d552014-10-27 16:22:56 +01001204 * Must be called holding kvm->slots_lock for write.
Avi Kivity6aa8b732006-12-10 02:21:36 -08001205 */
Sheng Yangf78e0e22007-10-29 09:40:42 +08001206int __kvm_set_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001207 const struct kvm_userspace_memory_region *mem)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001208{
Avi Kivity6aa8b732006-12-10 02:21:36 -08001209 struct kvm_memory_slot old, new;
Sean Christopherson163da372020-02-18 13:07:28 -08001210 struct kvm_memory_slot *tmp;
Takuya Yoshikawaf64c0392013-01-29 11:00:07 +09001211 enum kvm_mr_change change;
Sean Christopherson163da372020-02-18 13:07:28 -08001212 int as_id, id;
1213 int r;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001214
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001215 r = check_memory_region_flags(mem);
1216 if (r)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001217 return r;
Xiao Guangronga50d64d2012-08-21 10:58:13 +08001218
Paolo Bonzinif481b062015-05-17 17:30:37 +02001219 as_id = mem->slot >> 16;
1220 id = (u16)mem->slot;
1221
Avi Kivity6aa8b732006-12-10 02:21:36 -08001222 /* General sanity checks */
1223 if (mem->memory_size & (PAGE_SIZE - 1))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001224 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001225 if (mem->guest_phys_addr & (PAGE_SIZE - 1))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001226 return -EINVAL;
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001227 /* We can read the guest memory with __xxx_user() later on. */
Paolo Bonzinif481b062015-05-17 17:30:37 +02001228 if ((id < KVM_USER_MEM_SLOTS) &&
Takuya Yoshikawafa3d3152011-05-07 16:35:38 +09001229 ((mem->userspace_addr & (PAGE_SIZE - 1)) ||
Linus Torvalds96d4f262019-01-03 18:57:57 -08001230 !access_ok((void __user *)(unsigned long)mem->userspace_addr,
Heiko Carstens9e3bb6b2011-05-24 07:51:27 +02001231 mem->memory_size)))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001232 return -EINVAL;
Paolo Bonzinif481b062015-05-17 17:30:37 +02001233 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_MEM_SLOTS_NUM)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001234 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001235 if (mem->guest_phys_addr + mem->memory_size < mem->guest_phys_addr)
Sean Christopherson71a4c302020-02-18 13:07:22 -08001236 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001237
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001238 /*
1239 * Make a full copy of the old memslot, the pointer will become stale
1240 * when the memslots are re-sorted by update_memslots(), and the old
1241 * memslot needs to be referenced after calling update_memslots(), e.g.
Sean Christopherson0dff0842020-02-18 13:07:29 -08001242 * to free its resources and for arch specific behavior.
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001243 */
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001244 tmp = id_to_memslot(__kvm_memslots(kvm, as_id), id);
1245 if (tmp) {
1246 old = *tmp;
1247 tmp = NULL;
1248 } else {
1249 memset(&old, 0, sizeof(old));
1250 old.id = id;
1251 }
Sean Christopherson163da372020-02-18 13:07:28 -08001252
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001253 if (!mem->memory_size)
1254 return kvm_delete_memslot(kvm, mem, &old, as_id);
1255
Paolo Bonzinif481b062015-05-17 17:30:37 +02001256 new.id = id;
Sean Christopherson163da372020-02-18 13:07:28 -08001257 new.base_gfn = mem->guest_phys_addr >> PAGE_SHIFT;
1258 new.npages = mem->memory_size >> PAGE_SHIFT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001259 new.flags = mem->flags;
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001260 new.userspace_addr = mem->userspace_addr;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001261
Sean Christopherson163da372020-02-18 13:07:28 -08001262 if (new.npages > KVM_MEM_MAX_NR_PAGES)
1263 return -EINVAL;
1264
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001265 if (!old.npages) {
1266 change = KVM_MR_CREATE;
Sean Christopherson163da372020-02-18 13:07:28 -08001267 new.dirty_bitmap = NULL;
1268 memset(&new.arch, 0, sizeof(new.arch));
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001269 } else { /* Modify an existing slot. */
1270 if ((new.userspace_addr != old.userspace_addr) ||
Sean Christopherson163da372020-02-18 13:07:28 -08001271 (new.npages != old.npages) ||
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001272 ((new.flags ^ old.flags) & KVM_MEM_READONLY))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001273 return -EINVAL;
Paolo Bonzini09170a42015-05-18 13:59:39 +02001274
Sean Christopherson163da372020-02-18 13:07:28 -08001275 if (new.base_gfn != old.base_gfn)
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001276 change = KVM_MR_MOVE;
1277 else if (new.flags != old.flags)
1278 change = KVM_MR_FLAGS_ONLY;
1279 else /* Nothing to change. */
1280 return 0;
Sean Christopherson163da372020-02-18 13:07:28 -08001281
1282 /* Copy dirty_bitmap and arch from the current memslot. */
1283 new.dirty_bitmap = old.dirty_bitmap;
1284 memcpy(&new.arch, &old.arch, sizeof(new.arch));
Paolo Bonzini09170a42015-05-18 13:59:39 +02001285 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001286
Takuya Yoshikawaf64c0392013-01-29 11:00:07 +09001287 if ((change == KVM_MR_CREATE) || (change == KVM_MR_MOVE)) {
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001288 /* Check for overlaps */
Sean Christopherson163da372020-02-18 13:07:28 -08001289 kvm_for_each_memslot(tmp, __kvm_memslots(kvm, as_id)) {
1290 if (tmp->id == id)
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001291 continue;
Sean Christopherson163da372020-02-18 13:07:28 -08001292 if (!((new.base_gfn + new.npages <= tmp->base_gfn) ||
1293 (new.base_gfn >= tmp->base_gfn + tmp->npages)))
Sean Christopherson71a4c302020-02-18 13:07:22 -08001294 return -EEXIST;
Takuya Yoshikawa0a706be2013-01-11 18:26:55 +09001295 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001296 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08001297
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001298 /* Allocate/free page dirty bitmap as needed */
Avi Kivity6aa8b732006-12-10 02:21:36 -08001299 if (!(new.flags & KVM_MEM_LOG_DIRTY_PAGES))
Al Viro8b6d44c2007-02-09 16:38:40 +00001300 new.dirty_bitmap = NULL;
Sean Christopherson414de7ab2020-02-18 13:07:20 -08001301 else if (!new.dirty_bitmap) {
Jay Zhou3c9bd402020-02-27 09:32:27 +08001302 r = kvm_alloc_dirty_bitmap(&new);
Sean Christopherson71a4c302020-02-18 13:07:22 -08001303 if (r)
1304 return r;
Jay Zhou3c9bd402020-02-27 09:32:27 +08001305
1306 if (kvm_dirty_log_manual_protect_and_init_set(kvm))
1307 bitmap_set(new.dirty_bitmap, 0, new.npages);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001308 }
1309
Sean Christophersoncf47f502020-02-18 13:07:23 -08001310 r = kvm_set_memslot(kvm, mem, &old, &new, as_id, change);
1311 if (r)
1312 goto out_bitmap;
Zhang Xiantao3ad82a72007-11-20 13:11:38 +08001313
Sean Christopherson5c0b4f32020-02-18 13:07:26 -08001314 if (old.dirty_bitmap && !new.dirty_bitmap)
1315 kvm_destroy_dirty_bitmap(&old);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001316 return 0;
1317
Sean Christophersonbd0e96f2020-02-18 13:07:21 -08001318out_bitmap:
1319 if (new.dirty_bitmap && !old.dirty_bitmap)
1320 kvm_destroy_dirty_bitmap(&new);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001321 return r;
Izik Eidus210c7c42007-10-24 23:52:57 +02001322}
Sheng Yangf78e0e22007-10-29 09:40:42 +08001323EXPORT_SYMBOL_GPL(__kvm_set_memory_region);
1324
1325int kvm_set_memory_region(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02001326 const struct kvm_userspace_memory_region *mem)
Sheng Yangf78e0e22007-10-29 09:40:42 +08001327{
1328 int r;
1329
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001330 mutex_lock(&kvm->slots_lock);
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09001331 r = __kvm_set_memory_region(kvm, mem);
Marcelo Tosatti79fac952009-12-23 14:35:26 -02001332 mutex_unlock(&kvm->slots_lock);
Sheng Yangf78e0e22007-10-29 09:40:42 +08001333 return r;
1334}
Izik Eidus210c7c42007-10-24 23:52:57 +02001335EXPORT_SYMBOL_GPL(kvm_set_memory_region);
1336
Stephen Hemminger79408762013-12-29 12:12:29 -08001337static int kvm_vm_ioctl_set_memory_region(struct kvm *kvm,
1338 struct kvm_userspace_memory_region *mem)
Izik Eidus210c7c42007-10-24 23:52:57 +02001339{
Paolo Bonzinif481b062015-05-17 17:30:37 +02001340 if ((u16)mem->slot >= KVM_USER_MEM_SLOTS)
Izik Eiduse0d62c72007-10-24 23:57:46 +02001341 return -EINVAL;
Paolo Bonzini09170a42015-05-18 13:59:39 +02001342
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09001343 return kvm_set_memory_region(kvm, mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001344}
1345
Sean Christopherson0dff0842020-02-18 13:07:29 -08001346#ifndef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
Sean Christopherson2a49f612020-02-18 13:07:30 -08001347/**
1348 * kvm_get_dirty_log - get a snapshot of dirty pages
1349 * @kvm: pointer to kvm instance
1350 * @log: slot id and address to which we copy the log
1351 * @is_dirty: set to '1' if any dirty pages were found
1352 * @memslot: set to the associated memslot, always valid on success
1353 */
1354int kvm_get_dirty_log(struct kvm *kvm, struct kvm_dirty_log *log,
1355 int *is_dirty, struct kvm_memory_slot **memslot)
Avi Kivity6aa8b732006-12-10 02:21:36 -08001356{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001357 struct kvm_memslots *slots;
Markus Elfring843574a2017-01-22 17:41:07 +01001358 int i, as_id, id;
Takuya Yoshikawa87bf6e72010-04-12 19:35:35 +09001359 unsigned long n;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001360 unsigned long any = 0;
1361
Sean Christopherson2a49f612020-02-18 13:07:30 -08001362 *memslot = NULL;
1363 *is_dirty = 0;
1364
Paolo Bonzinif481b062015-05-17 17:30:37 +02001365 as_id = log->slot >> 16;
1366 id = (u16)log->slot;
1367 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
Markus Elfring843574a2017-01-22 17:41:07 +01001368 return -EINVAL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001369
Paolo Bonzinif481b062015-05-17 17:30:37 +02001370 slots = __kvm_memslots(kvm, as_id);
Sean Christopherson2a49f612020-02-18 13:07:30 -08001371 *memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001372 if (!(*memslot) || !(*memslot)->dirty_bitmap)
Markus Elfring843574a2017-01-22 17:41:07 +01001373 return -ENOENT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001374
Sean Christopherson2a49f612020-02-18 13:07:30 -08001375 kvm_arch_sync_dirty_log(kvm, *memslot);
1376
1377 n = kvm_dirty_bitmap_bytes(*memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001378
Uri Lublincd1a4a92007-02-22 16:43:09 +02001379 for (i = 0; !any && i < n/sizeof(long); ++i)
Sean Christopherson2a49f612020-02-18 13:07:30 -08001380 any = (*memslot)->dirty_bitmap[i];
Avi Kivity6aa8b732006-12-10 02:21:36 -08001381
Sean Christopherson2a49f612020-02-18 13:07:30 -08001382 if (copy_to_user(log->dirty_bitmap, (*memslot)->dirty_bitmap, n))
Markus Elfring843574a2017-01-22 17:41:07 +01001383 return -EFAULT;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001384
Zhang Xiantao5bb064d2007-11-18 20:29:43 +08001385 if (any)
1386 *is_dirty = 1;
Markus Elfring843574a2017-01-22 17:41:07 +01001387 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08001388}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05301389EXPORT_SYMBOL_GPL(kvm_get_dirty_log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001390
Sean Christopherson0dff0842020-02-18 13:07:29 -08001391#else /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
Mario Smarduchba0513b2015-01-15 15:58:53 -08001392/**
Jiang Biaob8b00222019-04-23 19:40:30 +08001393 * kvm_get_dirty_log_protect - get a snapshot of dirty pages
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001394 * and reenable dirty page tracking for the corresponding pages.
Mario Smarduchba0513b2015-01-15 15:58:53 -08001395 * @kvm: pointer to kvm instance
1396 * @log: slot id and address to which we copy the log
Mario Smarduchba0513b2015-01-15 15:58:53 -08001397 *
1398 * We need to keep it in mind that VCPU threads can write to the bitmap
1399 * concurrently. So, to avoid losing track of dirty pages we keep the
1400 * following order:
1401 *
1402 * 1. Take a snapshot of the bit and clear it if needed.
1403 * 2. Write protect the corresponding page.
1404 * 3. Copy the snapshot to the userspace.
1405 * 4. Upon return caller flushes TLB's if needed.
1406 *
1407 * Between 2 and 4, the guest may write to the page using the remaining TLB
1408 * entry. This is not a problem because the page is reported dirty using
1409 * the snapshot taken before and step 4 ensures that writes done after
1410 * exiting to userspace will be logged for the next call.
1411 *
1412 */
Sean Christopherson0dff0842020-02-18 13:07:29 -08001413static int kvm_get_dirty_log_protect(struct kvm *kvm, struct kvm_dirty_log *log)
Mario Smarduchba0513b2015-01-15 15:58:53 -08001414{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02001415 struct kvm_memslots *slots;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001416 struct kvm_memory_slot *memslot;
Markus Elfring58d6db32017-01-22 17:30:16 +01001417 int i, as_id, id;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001418 unsigned long n;
1419 unsigned long *dirty_bitmap;
1420 unsigned long *dirty_bitmap_buffer;
Sean Christopherson0dff0842020-02-18 13:07:29 -08001421 bool flush;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001422
Paolo Bonzinif481b062015-05-17 17:30:37 +02001423 as_id = log->slot >> 16;
1424 id = (u16)log->slot;
1425 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
Markus Elfring58d6db32017-01-22 17:30:16 +01001426 return -EINVAL;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001427
Paolo Bonzinif481b062015-05-17 17:30:37 +02001428 slots = __kvm_memslots(kvm, as_id);
1429 memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001430 if (!memslot || !memslot->dirty_bitmap)
1431 return -ENOENT;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001432
1433 dirty_bitmap = memslot->dirty_bitmap;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001434
Sean Christopherson0dff0842020-02-18 13:07:29 -08001435 kvm_arch_sync_dirty_log(kvm, memslot);
1436
Mario Smarduchba0513b2015-01-15 15:58:53 -08001437 n = kvm_dirty_bitmap_bytes(memslot);
Sean Christopherson0dff0842020-02-18 13:07:29 -08001438 flush = false;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001439 if (kvm->manual_dirty_log_protect) {
1440 /*
1441 * Unlike kvm_get_dirty_log, we always return false in *flush,
1442 * because no flush is needed until KVM_CLEAR_DIRTY_LOG. There
1443 * is some code duplication between this function and
1444 * kvm_get_dirty_log, but hopefully all architecture
1445 * transition to kvm_get_dirty_log_protect and kvm_get_dirty_log
1446 * can be eliminated.
1447 */
1448 dirty_bitmap_buffer = dirty_bitmap;
1449 } else {
1450 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
1451 memset(dirty_bitmap_buffer, 0, n);
Mario Smarduchba0513b2015-01-15 15:58:53 -08001452
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001453 spin_lock(&kvm->mmu_lock);
1454 for (i = 0; i < n / sizeof(long); i++) {
1455 unsigned long mask;
1456 gfn_t offset;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001457
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001458 if (!dirty_bitmap[i])
1459 continue;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001460
Sean Christopherson0dff0842020-02-18 13:07:29 -08001461 flush = true;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001462 mask = xchg(&dirty_bitmap[i], 0);
1463 dirty_bitmap_buffer[i] = mask;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001464
Lan Tianyua67794c2019-02-02 17:20:27 +08001465 offset = i * BITS_PER_LONG;
1466 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
1467 offset, mask);
Takuya Yoshikawa58d29302015-03-17 16:19:58 +09001468 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001469 spin_unlock(&kvm->mmu_lock);
Mario Smarduchba0513b2015-01-15 15:58:53 -08001470 }
1471
Sean Christopherson0dff0842020-02-18 13:07:29 -08001472 if (flush)
1473 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
1474
Mario Smarduchba0513b2015-01-15 15:58:53 -08001475 if (copy_to_user(log->dirty_bitmap, dirty_bitmap_buffer, n))
Markus Elfring58d6db32017-01-22 17:30:16 +01001476 return -EFAULT;
1477 return 0;
Mario Smarduchba0513b2015-01-15 15:58:53 -08001478}
Sean Christopherson0dff0842020-02-18 13:07:29 -08001479
1480
1481/**
1482 * kvm_vm_ioctl_get_dirty_log - get and clear the log of dirty pages in a slot
1483 * @kvm: kvm instance
1484 * @log: slot id and address to which we copy the log
1485 *
1486 * Steps 1-4 below provide general overview of dirty page logging. See
1487 * kvm_get_dirty_log_protect() function description for additional details.
1488 *
1489 * We call kvm_get_dirty_log_protect() to handle steps 1-3, upon return we
1490 * always flush the TLB (step 4) even if previous step failed and the dirty
1491 * bitmap may be corrupt. Regardless of previous outcome the KVM logging API
1492 * does not preclude user space subsequent dirty log read. Flushing TLB ensures
1493 * writes will be marked dirty for next log read.
1494 *
1495 * 1. Take a snapshot of the bit and clear it if needed.
1496 * 2. Write protect the corresponding page.
1497 * 3. Copy the snapshot to the userspace.
1498 * 4. Flush TLB's if needed.
1499 */
1500static int kvm_vm_ioctl_get_dirty_log(struct kvm *kvm,
1501 struct kvm_dirty_log *log)
1502{
1503 int r;
1504
1505 mutex_lock(&kvm->slots_lock);
1506
1507 r = kvm_get_dirty_log_protect(kvm, log);
1508
1509 mutex_unlock(&kvm->slots_lock);
1510 return r;
1511}
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001512
1513/**
1514 * kvm_clear_dirty_log_protect - clear dirty bits in the bitmap
1515 * and reenable dirty page tracking for the corresponding pages.
1516 * @kvm: pointer to kvm instance
1517 * @log: slot id and address from which to fetch the bitmap of dirty pages
1518 */
Sean Christopherson0dff0842020-02-18 13:07:29 -08001519static int kvm_clear_dirty_log_protect(struct kvm *kvm,
1520 struct kvm_clear_dirty_log *log)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001521{
1522 struct kvm_memslots *slots;
1523 struct kvm_memory_slot *memslot;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001524 int as_id, id;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001525 gfn_t offset;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001526 unsigned long i, n;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001527 unsigned long *dirty_bitmap;
1528 unsigned long *dirty_bitmap_buffer;
Sean Christopherson0dff0842020-02-18 13:07:29 -08001529 bool flush;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001530
1531 as_id = log->slot >> 16;
1532 id = (u16)log->slot;
1533 if (as_id >= KVM_ADDRESS_SPACE_NUM || id >= KVM_USER_MEM_SLOTS)
1534 return -EINVAL;
1535
Paolo Bonzini76d58e02019-04-17 15:28:44 +02001536 if (log->first_page & 63)
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001537 return -EINVAL;
1538
1539 slots = __kvm_memslots(kvm, as_id);
1540 memslot = id_to_memslot(slots, id);
Sean Christopherson0577d1a2020-02-18 13:07:31 -08001541 if (!memslot || !memslot->dirty_bitmap)
1542 return -ENOENT;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001543
1544 dirty_bitmap = memslot->dirty_bitmap;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001545
Peter Xu4ddc9202019-05-08 17:15:45 +08001546 n = ALIGN(log->num_pages, BITS_PER_LONG) / 8;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001547
1548 if (log->first_page > memslot->npages ||
Paolo Bonzini76d58e02019-04-17 15:28:44 +02001549 log->num_pages > memslot->npages - log->first_page ||
1550 (log->num_pages < memslot->npages - log->first_page && (log->num_pages & 63)))
1551 return -EINVAL;
Tomas Bortoli98938aa2019-01-02 18:29:37 +01001552
Sean Christopherson0dff0842020-02-18 13:07:29 -08001553 kvm_arch_sync_dirty_log(kvm, memslot);
1554
1555 flush = false;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001556 dirty_bitmap_buffer = kvm_second_dirty_bitmap(memslot);
1557 if (copy_from_user(dirty_bitmap_buffer, log->dirty_bitmap, n))
1558 return -EFAULT;
1559
1560 spin_lock(&kvm->mmu_lock);
Peter Xu53eac7a2019-05-08 17:15:46 +08001561 for (offset = log->first_page, i = offset / BITS_PER_LONG,
1562 n = DIV_ROUND_UP(log->num_pages, BITS_PER_LONG); n--;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001563 i++, offset += BITS_PER_LONG) {
1564 unsigned long mask = *dirty_bitmap_buffer++;
1565 atomic_long_t *p = (atomic_long_t *) &dirty_bitmap[i];
1566 if (!mask)
1567 continue;
1568
1569 mask &= atomic_long_fetch_andnot(mask, p);
1570
1571 /*
1572 * mask contains the bits that really have been cleared. This
1573 * never includes any bits beyond the length of the memslot (if
1574 * the length is not aligned to 64 pages), therefore it is not
1575 * a problem if userspace sets them in log->dirty_bitmap.
1576 */
1577 if (mask) {
Sean Christopherson0dff0842020-02-18 13:07:29 -08001578 flush = true;
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001579 kvm_arch_mmu_enable_log_dirty_pt_masked(kvm, memslot,
1580 offset, mask);
1581 }
1582 }
1583 spin_unlock(&kvm->mmu_lock);
1584
Sean Christopherson0dff0842020-02-18 13:07:29 -08001585 if (flush)
1586 kvm_arch_flush_remote_tlbs_memslot(kvm, memslot);
1587
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02001588 return 0;
1589}
Sean Christopherson0dff0842020-02-18 13:07:29 -08001590
1591static int kvm_vm_ioctl_clear_dirty_log(struct kvm *kvm,
1592 struct kvm_clear_dirty_log *log)
1593{
1594 int r;
1595
1596 mutex_lock(&kvm->slots_lock);
1597
1598 r = kvm_clear_dirty_log_protect(kvm, log);
1599
1600 mutex_unlock(&kvm->slots_lock);
1601 return r;
1602}
1603#endif /* CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT */
Mario Smarduchba0513b2015-01-15 15:58:53 -08001604
Gleb Natapov49c77542010-10-18 15:22:23 +02001605struct kvm_memory_slot *gfn_to_memslot(struct kvm *kvm, gfn_t gfn)
1606{
1607 return __gfn_to_memslot(kvm_memslots(kvm), gfn);
1608}
Avi Kivitya1f4d3952010-06-21 11:44:20 +03001609EXPORT_SYMBOL_GPL(gfn_to_memslot);
Avi Kivity6aa8b732006-12-10 02:21:36 -08001610
Paolo Bonzini8e734852015-05-17 13:58:53 +02001611struct kvm_memory_slot *kvm_vcpu_gfn_to_memslot(struct kvm_vcpu *vcpu, gfn_t gfn)
1612{
1613 return __gfn_to_memslot(kvm_vcpu_memslots(vcpu), gfn);
1614}
Paolo Bonzinie72436b2020-04-17 12:21:06 -04001615EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_memslot);
Paolo Bonzini8e734852015-05-17 13:58:53 +02001616
Yaowei Bai33e94152015-11-14 11:21:06 +08001617bool kvm_is_visible_gfn(struct kvm *kvm, gfn_t gfn)
Izik Eiduse0d62c72007-10-24 23:57:46 +02001618{
Xiao Guangrongbf3e05b2011-11-24 17:40:57 +08001619 struct kvm_memory_slot *memslot = gfn_to_memslot(kvm, gfn);
Izik Eiduse0d62c72007-10-24 23:57:46 +02001620
Paolo Bonzinic36b7152020-04-16 09:48:07 -04001621 return kvm_is_visible_memslot(memslot);
Izik Eiduse0d62c72007-10-24 23:57:46 +02001622}
1623EXPORT_SYMBOL_GPL(kvm_is_visible_gfn);
1624
Sean Christophersonf9b84e12020-01-08 12:24:37 -08001625unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn)
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001626{
1627 struct vm_area_struct *vma;
1628 unsigned long addr, size;
1629
1630 size = PAGE_SIZE;
1631
Sean Christopherson42cde482020-01-08 12:24:38 -08001632 addr = kvm_vcpu_gfn_to_hva_prot(vcpu, gfn, NULL);
Joerg Roedel8f0b1ab2010-01-28 12:37:56 +01001633 if (kvm_is_error_hva(addr))
1634 return PAGE_SIZE;
1635
1636 down_read(&current->mm->mmap_sem);
1637 vma = find_vma(current->mm, addr);
1638 if (!vma)
1639 goto out;
1640
1641 size = vma_kernel_pagesize(vma);
1642
1643out:
1644 up_read(&current->mm->mmap_sem);
1645
1646 return size;
1647}
1648
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001649static bool memslot_is_readonly(struct kvm_memory_slot *slot)
1650{
1651 return slot->flags & KVM_MEM_READONLY;
1652}
1653
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001654static unsigned long __gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1655 gfn_t *nr_pages, bool write)
Izik Eidus539cb662007-11-11 22:05:04 +02001656{
Marcelo Tosattibc6678a2009-12-23 14:35:21 -02001657 if (!slot || slot->flags & KVM_MEMSLOT_INVALID)
Xiao Guangrongca3a4902012-08-21 11:01:50 +08001658 return KVM_HVA_ERR_BAD;
Xiao Guangrong48987782010-08-22 19:11:43 +08001659
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001660 if (memslot_is_readonly(slot) && write)
1661 return KVM_HVA_ERR_RO_BAD;
Xiao Guangrong48987782010-08-22 19:11:43 +08001662
1663 if (nr_pages)
1664 *nr_pages = slot->npages - (gfn - slot->base_gfn);
1665
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001666 return __gfn_to_hva_memslot(slot, gfn);
Izik Eidus539cb662007-11-11 22:05:04 +02001667}
Xiao Guangrong48987782010-08-22 19:11:43 +08001668
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001669static unsigned long gfn_to_hva_many(struct kvm_memory_slot *slot, gfn_t gfn,
1670 gfn_t *nr_pages)
1671{
1672 return __gfn_to_hva_many(slot, gfn, nr_pages, true);
1673}
1674
1675unsigned long gfn_to_hva_memslot(struct kvm_memory_slot *slot,
Stephen Hemminger79408762013-12-29 12:12:29 -08001676 gfn_t gfn)
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001677{
1678 return gfn_to_hva_many(slot, gfn, NULL);
1679}
1680EXPORT_SYMBOL_GPL(gfn_to_hva_memslot);
1681
Xiao Guangrong48987782010-08-22 19:11:43 +08001682unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn)
1683{
Gleb Natapov49c77542010-10-18 15:22:23 +02001684 return gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL);
Xiao Guangrong48987782010-08-22 19:11:43 +08001685}
Sheng Yang0d150292008-04-25 21:44:50 +08001686EXPORT_SYMBOL_GPL(gfn_to_hva);
Izik Eidus539cb662007-11-11 22:05:04 +02001687
Paolo Bonzini8e734852015-05-17 13:58:53 +02001688unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn)
1689{
1690 return gfn_to_hva_many(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn, NULL);
1691}
1692EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_hva);
1693
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001694/*
Wei Yang970c0d42018-10-09 10:41:15 +08001695 * Return the hva of a @gfn and the R/W attribute if possible.
1696 *
1697 * @slot: the kvm_memory_slot which contains @gfn
1698 * @gfn: the gfn to be translated
1699 * @writable: used to return the read/write attribute of the @slot if the hva
1700 * is valid and @writable is not NULL
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001701 */
Christoffer Dall64d83122014-08-19 12:15:00 +02001702unsigned long gfn_to_hva_memslot_prot(struct kvm_memory_slot *slot,
1703 gfn_t gfn, bool *writable)
Gleb Natapov80300892010-10-19 18:13:41 +02001704{
Gleb Natapova2ac07f2013-10-01 19:58:36 +03001705 unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
1706
1707 if (!kvm_is_error_hva(hva) && writable)
Paolo Bonziniba6a3542013-09-09 13:52:33 +02001708 *writable = !memslot_is_readonly(slot);
1709
Gleb Natapova2ac07f2013-10-01 19:58:36 +03001710 return hva;
Xiao Guangrong86ab8cf2012-08-21 10:59:53 +08001711}
1712
Christoffer Dall64d83122014-08-19 12:15:00 +02001713unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
1714{
1715 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
1716
1717 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1718}
1719
Paolo Bonzini8e734852015-05-17 13:58:53 +02001720unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable)
1721{
1722 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
1723
1724 return gfn_to_hva_memslot_prot(slot, gfn, writable);
1725}
1726
Huang Yingfafc3db2011-01-30 11:15:49 +08001727static inline int check_user_page_hwpoison(unsigned long addr)
1728{
Lorenzo Stoakes0d731752016-10-24 10:57:25 +01001729 int rc, flags = FOLL_HWPOISON | FOLL_WRITE;
Huang Yingfafc3db2011-01-30 11:15:49 +08001730
Lorenzo Stoakes0d731752016-10-24 10:57:25 +01001731 rc = get_user_pages(addr, 1, flags, NULL, NULL);
Huang Yingfafc3db2011-01-30 11:15:49 +08001732 return rc == -EHWPOISON;
1733}
1734
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001735/*
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001736 * The fast path to get the writable pfn which will be stored in @pfn,
1737 * true indicates success, otherwise false is returned. It's also the
Miaohe Lin311497e2019-12-11 14:26:25 +08001738 * only part that runs if we can in atomic context.
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001739 */
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001740static bool hva_to_pfn_fast(unsigned long addr, bool write_fault,
1741 bool *writable, kvm_pfn_t *pfn)
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001742{
1743 struct page *page[1];
1744 int npages;
1745
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001746 /*
1747 * Fast pin a writable pfn only if it is a write fault request
1748 * or the caller allows to map a writable pfn for a read fault
1749 * request.
1750 */
1751 if (!(write_fault || writable))
1752 return false;
1753
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001754 npages = __get_user_pages_fast(addr, 1, 1, page);
1755 if (npages == 1) {
1756 *pfn = page_to_pfn(page[0]);
1757
1758 if (writable)
1759 *writable = true;
1760 return true;
1761 }
1762
1763 return false;
1764}
1765
1766/*
1767 * The slow path to get the pfn of the specified host virtual address,
1768 * 1 indicates success, -errno is returned if error is detected.
1769 */
1770static int hva_to_pfn_slow(unsigned long addr, bool *async, bool write_fault,
Dan Williamsba049e92016-01-15 16:56:11 -08001771 bool *writable, kvm_pfn_t *pfn)
Avi Kivity954bbbc2007-03-30 14:02:32 +03001772{
Al Viroce530532017-11-19 17:47:33 -05001773 unsigned int flags = FOLL_HWPOISON;
1774 struct page *page;
Gleb Natapovaf585b92010-10-14 11:22:46 +02001775 int npages = 0;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001776
1777 might_sleep();
1778
1779 if (writable)
1780 *writable = write_fault;
1781
Al Viroce530532017-11-19 17:47:33 -05001782 if (write_fault)
1783 flags |= FOLL_WRITE;
1784 if (async)
1785 flags |= FOLL_NOWAIT;
Lorenzo Stoakesd4944b02016-10-13 01:20:12 +01001786
Al Viroce530532017-11-19 17:47:33 -05001787 npages = get_user_pages_unlocked(addr, 1, &page, flags);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001788 if (npages != 1)
1789 return npages;
1790
1791 /* map read fault as writable if possible */
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001792 if (unlikely(!write_fault) && writable) {
Al Viroce530532017-11-19 17:47:33 -05001793 struct page *wpage;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001794
Al Viroce530532017-11-19 17:47:33 -05001795 if (__get_user_pages_fast(addr, 1, 1, &wpage) == 1) {
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001796 *writable = true;
Al Viroce530532017-11-19 17:47:33 -05001797 put_page(page);
1798 page = wpage;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001799 }
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001800 }
Al Viroce530532017-11-19 17:47:33 -05001801 *pfn = page_to_pfn(page);
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001802 return npages;
1803}
1804
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001805static bool vma_is_valid(struct vm_area_struct *vma, bool write_fault)
1806{
1807 if (unlikely(!(vma->vm_flags & VM_READ)))
1808 return false;
1809
1810 if (write_fault && (unlikely(!(vma->vm_flags & VM_WRITE))))
1811 return false;
1812
1813 return true;
1814}
1815
Paolo Bonzini92176a82016-06-07 16:22:47 +02001816static int hva_to_pfn_remapped(struct vm_area_struct *vma,
1817 unsigned long addr, bool *async,
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001818 bool write_fault, bool *writable,
1819 kvm_pfn_t *p_pfn)
Paolo Bonzini92176a82016-06-07 16:22:47 +02001820{
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001821 unsigned long pfn;
1822 int r;
1823
1824 r = follow_pfn(vma, addr, &pfn);
1825 if (r) {
1826 /*
1827 * get_user_pages fails for VM_IO and VM_PFNMAP vmas and does
1828 * not call the fault handler, so do it here.
1829 */
1830 bool unlocked = false;
1831 r = fixup_user_fault(current, current->mm, addr,
1832 (write_fault ? FAULT_FLAG_WRITE : 0),
1833 &unlocked);
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001834 if (r)
1835 return r;
1836
1837 r = follow_pfn(vma, addr, &pfn);
1838 if (r)
1839 return r;
1840
1841 }
1842
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001843 if (writable)
1844 *writable = true;
Paolo Bonziniadd6a0c2016-06-07 17:51:18 +02001845
1846 /*
1847 * Get a reference here because callers of *hva_to_pfn* and
1848 * *gfn_to_pfn* ultimately call kvm_release_pfn_clean on the
1849 * returned pfn. This is only needed if the VMA has VM_MIXEDMAP
1850 * set, but the kvm_get_pfn/kvm_release_pfn_clean pair will
1851 * simply do nothing for reserved pfns.
1852 *
1853 * Whoever called remap_pfn_range is also going to call e.g.
1854 * unmap_mapping_range before the underlying pages are freed,
1855 * causing a call to our MMU notifier.
1856 */
1857 kvm_get_pfn(pfn);
1858
1859 *p_pfn = pfn;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001860 return 0;
1861}
1862
Xiao Guangrong12ce13f2012-08-21 11:00:49 +08001863/*
1864 * Pin guest page in memory and return its pfn.
1865 * @addr: host virtual address which maps memory to the guest
1866 * @atomic: whether this function can sleep
1867 * @async: whether this function need to wait IO complete if the
1868 * host page is not in the memory
1869 * @write_fault: whether we should get a writable host page
1870 * @writable: whether it allows to map a writable host page for !@write_fault
1871 *
1872 * The function will map a writable host page for these two cases:
1873 * 1): @write_fault = true
1874 * 2): @write_fault = false && @writable, @writable will tell the caller
1875 * whether the mapping is writable.
1876 */
Dan Williamsba049e92016-01-15 16:56:11 -08001877static kvm_pfn_t hva_to_pfn(unsigned long addr, bool atomic, bool *async,
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001878 bool write_fault, bool *writable)
1879{
1880 struct vm_area_struct *vma;
Dan Williamsba049e92016-01-15 16:56:11 -08001881 kvm_pfn_t pfn = 0;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001882 int npages, r;
Avi Kivity954bbbc2007-03-30 14:02:32 +03001883
Gleb Natapovaf585b92010-10-14 11:22:46 +02001884 /* we can do it either atomically or asynchronously, not both */
1885 BUG_ON(atomic && async);
1886
Paolo Bonzinib9b33da2018-07-27 17:44:41 +02001887 if (hva_to_pfn_fast(addr, write_fault, writable, &pfn))
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001888 return pfn;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001889
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001890 if (atomic)
1891 return KVM_PFN_ERR_FAULT;
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001892
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001893 npages = hva_to_pfn_slow(addr, async, write_fault, writable, &pfn);
1894 if (npages == 1)
1895 return pfn;
Gleb Natapovaf585b92010-10-14 11:22:46 +02001896
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001897 down_read(&current->mm->mmap_sem);
1898 if (npages == -EHWPOISON ||
1899 (!async && check_user_page_hwpoison(addr))) {
1900 pfn = KVM_PFN_ERR_HWPOISON;
1901 goto exit;
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001902 }
Izik Eidus539cb662007-11-11 22:05:04 +02001903
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001904 vma = find_vma_intersection(current->mm, addr, addr + 1);
Anthony Liguori8d4e1282007-10-18 09:59:34 -05001905
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001906 if (vma == NULL)
1907 pfn = KVM_PFN_ERR_FAULT;
Paolo Bonzini92176a82016-06-07 16:22:47 +02001908 else if (vma->vm_flags & (VM_IO | VM_PFNMAP)) {
KarimAllah Ahmeda340b3e2018-01-17 19:18:56 +01001909 r = hva_to_pfn_remapped(vma, addr, async, write_fault, writable, &pfn);
Paolo Bonzini92176a82016-06-07 16:22:47 +02001910 if (r < 0)
1911 pfn = KVM_PFN_ERR_FAULT;
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001912 } else {
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001913 if (async && vma_is_valid(vma, write_fault))
Xiao Guangrong2fc84312012-08-21 11:00:22 +08001914 *async = true;
1915 pfn = KVM_PFN_ERR_FAULT;
1916 }
1917exit:
1918 up_read(&current->mm->mmap_sem);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05001919 return pfn;
Anthony Liguori35149e22008-04-02 14:46:56 -05001920}
1921
Dan Williamsba049e92016-01-15 16:56:11 -08001922kvm_pfn_t __gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn,
1923 bool atomic, bool *async, bool write_fault,
1924 bool *writable)
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001925{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001926 unsigned long addr = __gfn_to_hva_many(slot, gfn, NULL, write_fault);
1927
Paolo Bonzinib2740d32016-02-23 15:36:01 +01001928 if (addr == KVM_HVA_ERR_RO_BAD) {
1929 if (writable)
1930 *writable = false;
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001931 return KVM_PFN_ERR_RO_FAULT;
Paolo Bonzinib2740d32016-02-23 15:36:01 +01001932 }
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001933
Paolo Bonzinib2740d32016-02-23 15:36:01 +01001934 if (kvm_is_error_hva(addr)) {
1935 if (writable)
1936 *writable = false;
Xiao Guangrong81c52c52012-10-16 20:10:59 +08001937 return KVM_PFN_NOSLOT;
Paolo Bonzinib2740d32016-02-23 15:36:01 +01001938 }
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001939
1940 /* Do not map writable pfn in the readonly memslot. */
1941 if (writable && memslot_is_readonly(slot)) {
1942 *writable = false;
1943 writable = NULL;
1944 }
1945
1946 return hva_to_pfn(addr, atomic, async, write_fault,
1947 writable);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001948}
Paolo Bonzini35204692015-04-02 11:20:48 +02001949EXPORT_SYMBOL_GPL(__gfn_to_pfn_memslot);
Xiao Guangrong887c08a2010-08-22 19:10:28 +08001950
Dan Williamsba049e92016-01-15 16:56:11 -08001951kvm_pfn_t gfn_to_pfn_prot(struct kvm *kvm, gfn_t gfn, bool write_fault,
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001952 bool *writable)
1953{
Paolo Bonzinie37afc62015-05-19 16:09:04 +02001954 return __gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn, false, NULL,
1955 write_fault, writable);
Marcelo Tosatti612819c2010-10-22 14:18:18 -02001956}
1957EXPORT_SYMBOL_GPL(gfn_to_pfn_prot);
1958
Dan Williamsba049e92016-01-15 16:56:11 -08001959kvm_pfn_t gfn_to_pfn_memslot(struct kvm_memory_slot *slot, gfn_t gfn)
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001960{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001961 return __gfn_to_pfn_memslot(slot, gfn, false, NULL, true, NULL);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001962}
Paolo Bonzinie37afc62015-05-19 16:09:04 +02001963EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot);
Marcelo Tosatti506f0d62009-12-23 14:35:19 -02001964
Dan Williamsba049e92016-01-15 16:56:11 -08001965kvm_pfn_t gfn_to_pfn_memslot_atomic(struct kvm_memory_slot *slot, gfn_t gfn)
Xiao Guangrong037d92d2012-08-21 10:59:12 +08001966{
Xiao Guangrong4d8b81a2012-08-21 11:02:51 +08001967 return __gfn_to_pfn_memslot(slot, gfn, true, NULL, true, NULL);
Xiao Guangrong037d92d2012-08-21 10:59:12 +08001968}
1969EXPORT_SYMBOL_GPL(gfn_to_pfn_memslot_atomic);
1970
Dan Williamsba049e92016-01-15 16:56:11 -08001971kvm_pfn_t kvm_vcpu_gfn_to_pfn_atomic(struct kvm_vcpu *vcpu, gfn_t gfn)
Paolo Bonzini8e734852015-05-17 13:58:53 +02001972{
1973 return gfn_to_pfn_memslot_atomic(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
1974}
1975EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn_atomic);
1976
Dan Williamsba049e92016-01-15 16:56:11 -08001977kvm_pfn_t gfn_to_pfn(struct kvm *kvm, gfn_t gfn)
Paolo Bonzinie37afc62015-05-19 16:09:04 +02001978{
1979 return gfn_to_pfn_memslot(gfn_to_memslot(kvm, gfn), gfn);
1980}
1981EXPORT_SYMBOL_GPL(gfn_to_pfn);
1982
Dan Williamsba049e92016-01-15 16:56:11 -08001983kvm_pfn_t kvm_vcpu_gfn_to_pfn(struct kvm_vcpu *vcpu, gfn_t gfn)
Paolo Bonzini8e734852015-05-17 13:58:53 +02001984{
1985 return gfn_to_pfn_memslot(kvm_vcpu_gfn_to_memslot(vcpu, gfn), gfn);
1986}
1987EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_pfn);
1988
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02001989int gfn_to_page_many_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
1990 struct page **pages, int nr_pages)
Xiao Guangrong48987782010-08-22 19:11:43 +08001991{
1992 unsigned long addr;
Arnd Bergmann076b9252017-08-10 14:14:39 +02001993 gfn_t entry = 0;
Xiao Guangrong48987782010-08-22 19:11:43 +08001994
Paolo Bonzinid9ef13c2015-05-19 16:01:50 +02001995 addr = gfn_to_hva_many(slot, gfn, &entry);
Xiao Guangrong48987782010-08-22 19:11:43 +08001996 if (kvm_is_error_hva(addr))
1997 return -1;
1998
1999 if (entry < nr_pages)
2000 return 0;
2001
2002 return __get_user_pages_fast(addr, nr_pages, 1, pages);
2003}
2004EXPORT_SYMBOL_GPL(gfn_to_page_many_atomic);
2005
Dan Williamsba049e92016-01-15 16:56:11 -08002006static struct page *kvm_pfn_to_page(kvm_pfn_t pfn)
Xiao Guangronga2766322012-07-26 11:58:59 +08002007{
Xiao Guangrong81c52c52012-10-16 20:10:59 +08002008 if (is_error_noslot_pfn(pfn))
Xiao Guangrong6cede2e2012-08-03 15:41:22 +08002009 return KVM_ERR_PTR_BAD_PAGE;
Xiao Guangronga2766322012-07-26 11:58:59 +08002010
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002011 if (kvm_is_reserved_pfn(pfn)) {
Xiao Guangrongcb9aaa32012-08-03 15:42:10 +08002012 WARN_ON(1);
2013 return KVM_ERR_PTR_BAD_PAGE;
2014 }
2015
Xiao Guangronga2766322012-07-26 11:58:59 +08002016 return pfn_to_page(pfn);
2017}
2018
Anthony Liguori35149e22008-04-02 14:46:56 -05002019struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn)
2020{
Dan Williamsba049e92016-01-15 16:56:11 -08002021 kvm_pfn_t pfn;
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002022
2023 pfn = gfn_to_pfn(kvm, gfn);
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002024
Xiao Guangronga2766322012-07-26 11:58:59 +08002025 return kvm_pfn_to_page(pfn);
Avi Kivity954bbbc2007-03-30 14:02:32 +03002026}
2027EXPORT_SYMBOL_GPL(gfn_to_page);
2028
Boris Ostrovsky91724812019-12-05 01:30:51 +00002029void kvm_release_pfn(kvm_pfn_t pfn, bool dirty, struct gfn_to_pfn_cache *cache)
2030{
2031 if (pfn == 0)
2032 return;
2033
2034 if (cache)
2035 cache->pfn = cache->gfn = 0;
2036
2037 if (dirty)
2038 kvm_release_pfn_dirty(pfn);
2039 else
2040 kvm_release_pfn_clean(pfn);
2041}
2042
2043static void kvm_cache_gfn_to_pfn(struct kvm_memory_slot *slot, gfn_t gfn,
2044 struct gfn_to_pfn_cache *cache, u64 gen)
2045{
2046 kvm_release_pfn(cache->pfn, cache->dirty, cache);
2047
2048 cache->pfn = gfn_to_pfn_memslot(slot, gfn);
2049 cache->gfn = gfn;
2050 cache->dirty = false;
2051 cache->generation = gen;
2052}
2053
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002054static int __kvm_map_gfn(struct kvm_memslots *slots, gfn_t gfn,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002055 struct kvm_host_map *map,
2056 struct gfn_to_pfn_cache *cache,
2057 bool atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002058{
2059 kvm_pfn_t pfn;
2060 void *hva = NULL;
2061 struct page *page = KVM_UNMAPPED_PAGE;
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002062 struct kvm_memory_slot *slot = __gfn_to_memslot(slots, gfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002063 u64 gen = slots->generation;
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002064
2065 if (!map)
2066 return -EINVAL;
2067
Boris Ostrovsky91724812019-12-05 01:30:51 +00002068 if (cache) {
2069 if (!cache->pfn || cache->gfn != gfn ||
2070 cache->generation != gen) {
2071 if (atomic)
2072 return -EAGAIN;
2073 kvm_cache_gfn_to_pfn(slot, gfn, cache, gen);
2074 }
2075 pfn = cache->pfn;
2076 } else {
2077 if (atomic)
2078 return -EAGAIN;
2079 pfn = gfn_to_pfn_memslot(slot, gfn);
2080 }
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002081 if (is_error_noslot_pfn(pfn))
2082 return -EINVAL;
2083
2084 if (pfn_valid(pfn)) {
2085 page = pfn_to_page(pfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002086 if (atomic)
2087 hva = kmap_atomic(page);
2088 else
2089 hva = kmap(page);
Paolo Bonzinid30b2142019-05-20 12:06:36 +02002090#ifdef CONFIG_HAS_IOMEM
Boris Ostrovsky91724812019-12-05 01:30:51 +00002091 } else if (!atomic) {
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002092 hva = memremap(pfn_to_hpa(pfn), PAGE_SIZE, MEMREMAP_WB);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002093 } else {
2094 return -EINVAL;
Paolo Bonzinid30b2142019-05-20 12:06:36 +02002095#endif
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002096 }
2097
2098 if (!hva)
2099 return -EFAULT;
2100
2101 map->page = page;
2102 map->hva = hva;
2103 map->pfn = pfn;
2104 map->gfn = gfn;
2105
2106 return 0;
2107}
2108
Boris Ostrovsky91724812019-12-05 01:30:51 +00002109int kvm_map_gfn(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map,
2110 struct gfn_to_pfn_cache *cache, bool atomic)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002111{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002112 return __kvm_map_gfn(kvm_memslots(vcpu->kvm), gfn, map,
2113 cache, atomic);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002114}
2115EXPORT_SYMBOL_GPL(kvm_map_gfn);
2116
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002117int kvm_vcpu_map(struct kvm_vcpu *vcpu, gfn_t gfn, struct kvm_host_map *map)
2118{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002119 return __kvm_map_gfn(kvm_vcpu_memslots(vcpu), gfn, map,
2120 NULL, false);
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002121}
2122EXPORT_SYMBOL_GPL(kvm_vcpu_map);
2123
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002124static void __kvm_unmap_gfn(struct kvm_memory_slot *memslot,
Boris Ostrovsky91724812019-12-05 01:30:51 +00002125 struct kvm_host_map *map,
2126 struct gfn_to_pfn_cache *cache,
2127 bool dirty, bool atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002128{
2129 if (!map)
2130 return;
2131
2132 if (!map->hva)
2133 return;
2134
Boris Ostrovsky91724812019-12-05 01:30:51 +00002135 if (map->page != KVM_UNMAPPED_PAGE) {
2136 if (atomic)
2137 kunmap_atomic(map->hva);
2138 else
2139 kunmap(map->page);
2140 }
Christian Borntraegereb1f2f32019-05-27 10:28:25 +02002141#ifdef CONFIG_HAS_IOMEM
Boris Ostrovsky91724812019-12-05 01:30:51 +00002142 else if (!atomic)
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002143 memunmap(map->hva);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002144 else
2145 WARN_ONCE(1, "Unexpected unmapping in atomic context");
Christian Borntraegereb1f2f32019-05-27 10:28:25 +02002146#endif
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002147
Boris Ostrovsky91724812019-12-05 01:30:51 +00002148 if (dirty)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002149 mark_page_dirty_in_slot(memslot, map->gfn);
Boris Ostrovsky91724812019-12-05 01:30:51 +00002150
2151 if (cache)
2152 cache->dirty |= dirty;
2153 else
2154 kvm_release_pfn(map->pfn, dirty, NULL);
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002155
2156 map->hva = NULL;
2157 map->page = NULL;
2158}
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002159
Boris Ostrovsky91724812019-12-05 01:30:51 +00002160int kvm_unmap_gfn(struct kvm_vcpu *vcpu, struct kvm_host_map *map,
2161 struct gfn_to_pfn_cache *cache, bool dirty, bool atomic)
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002162{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002163 __kvm_unmap_gfn(gfn_to_memslot(vcpu->kvm, map->gfn), map,
2164 cache, dirty, atomic);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002165 return 0;
2166}
2167EXPORT_SYMBOL_GPL(kvm_unmap_gfn);
2168
2169void kvm_vcpu_unmap(struct kvm_vcpu *vcpu, struct kvm_host_map *map, bool dirty)
2170{
Boris Ostrovsky91724812019-12-05 01:30:51 +00002171 __kvm_unmap_gfn(kvm_vcpu_gfn_to_memslot(vcpu, map->gfn), map, NULL,
2172 dirty, false);
Boris Ostrovsky1eff70a2019-11-12 16:35:06 +00002173}
KarimAllah Ahmede45adf62019-01-31 21:24:34 +01002174EXPORT_SYMBOL_GPL(kvm_vcpu_unmap);
2175
Paolo Bonzini8e734852015-05-17 13:58:53 +02002176struct page *kvm_vcpu_gfn_to_page(struct kvm_vcpu *vcpu, gfn_t gfn)
2177{
Dan Williamsba049e92016-01-15 16:56:11 -08002178 kvm_pfn_t pfn;
Paolo Bonzini8e734852015-05-17 13:58:53 +02002179
2180 pfn = kvm_vcpu_gfn_to_pfn(vcpu, gfn);
2181
2182 return kvm_pfn_to_page(pfn);
2183}
2184EXPORT_SYMBOL_GPL(kvm_vcpu_gfn_to_page);
2185
Izik Eidusb4231d62007-11-20 11:49:33 +02002186void kvm_release_page_clean(struct page *page)
2187{
Xiao Guangrong32cad842012-08-03 15:42:52 +08002188 WARN_ON(is_error_page(page));
2189
Anthony Liguori35149e22008-04-02 14:46:56 -05002190 kvm_release_pfn_clean(page_to_pfn(page));
Izik Eidusb4231d62007-11-20 11:49:33 +02002191}
2192EXPORT_SYMBOL_GPL(kvm_release_page_clean);
2193
Dan Williamsba049e92016-01-15 16:56:11 -08002194void kvm_release_pfn_clean(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002195{
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002196 if (!is_error_noslot_pfn(pfn) && !kvm_is_reserved_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002197 put_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002198}
2199EXPORT_SYMBOL_GPL(kvm_release_pfn_clean);
2200
Izik Eidusb4231d62007-11-20 11:49:33 +02002201void kvm_release_page_dirty(struct page *page)
Izik Eidus8a7ae052007-10-18 11:09:33 +02002202{
Xiao Guangronga2766322012-07-26 11:58:59 +08002203 WARN_ON(is_error_page(page));
2204
Anthony Liguori35149e22008-04-02 14:46:56 -05002205 kvm_release_pfn_dirty(page_to_pfn(page));
Izik Eidus8a7ae052007-10-18 11:09:33 +02002206}
Izik Eidusb4231d62007-11-20 11:49:33 +02002207EXPORT_SYMBOL_GPL(kvm_release_page_dirty);
Izik Eidus8a7ae052007-10-18 11:09:33 +02002208
David Hildenbrandf7a65092017-09-01 17:11:43 +02002209void kvm_release_pfn_dirty(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002210{
2211 kvm_set_pfn_dirty(pfn);
2212 kvm_release_pfn_clean(pfn);
2213}
David Hildenbrandf7a65092017-09-01 17:11:43 +02002214EXPORT_SYMBOL_GPL(kvm_release_pfn_dirty);
Anthony Liguori35149e22008-04-02 14:46:56 -05002215
Dan Williamsba049e92016-01-15 16:56:11 -08002216void kvm_set_pfn_dirty(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002217{
Miaohe Lind29c03a2019-12-05 11:05:05 +08002218 if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn))
2219 SetPageDirty(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002220}
2221EXPORT_SYMBOL_GPL(kvm_set_pfn_dirty);
2222
Dan Williamsba049e92016-01-15 16:56:11 -08002223void kvm_set_pfn_accessed(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002224{
Sean Christophersona78986a2019-11-11 14:12:27 -08002225 if (!kvm_is_reserved_pfn(pfn) && !kvm_is_zone_device_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002226 mark_page_accessed(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002227}
2228EXPORT_SYMBOL_GPL(kvm_set_pfn_accessed);
2229
Dan Williamsba049e92016-01-15 16:56:11 -08002230void kvm_get_pfn(kvm_pfn_t pfn)
Anthony Liguori35149e22008-04-02 14:46:56 -05002231{
Ard Biesheuvelbf4bea82014-11-10 08:33:56 +00002232 if (!kvm_is_reserved_pfn(pfn))
Anthony Liguori2e2e3732008-04-30 15:37:07 -05002233 get_page(pfn_to_page(pfn));
Anthony Liguori35149e22008-04-02 14:46:56 -05002234}
2235EXPORT_SYMBOL_GPL(kvm_get_pfn);
2236
Izik Eidus195aefd2007-10-01 22:14:18 +02002237static int next_segment(unsigned long len, int offset)
2238{
2239 if (len > PAGE_SIZE - offset)
2240 return PAGE_SIZE - offset;
2241 else
2242 return len;
2243}
2244
Paolo Bonzini8e734852015-05-17 13:58:53 +02002245static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn,
2246 void *data, int offset, int len)
Izik Eidus195aefd2007-10-01 22:14:18 +02002247{
Izik Eiduse0506bc2007-11-11 22:10:22 +02002248 int r;
2249 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02002250
Paolo Bonzini8e734852015-05-17 13:58:53 +02002251 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002252 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02002253 return -EFAULT;
Paolo Bonzini3180a7f2015-04-02 14:08:20 +02002254 r = __copy_from_user(data, (void __user *)addr + offset, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002255 if (r)
2256 return -EFAULT;
Izik Eidus195aefd2007-10-01 22:14:18 +02002257 return 0;
2258}
Paolo Bonzini8e734852015-05-17 13:58:53 +02002259
2260int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset,
2261 int len)
2262{
2263 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2264
2265 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2266}
Izik Eidus195aefd2007-10-01 22:14:18 +02002267EXPORT_SYMBOL_GPL(kvm_read_guest_page);
2268
Paolo Bonzini8e734852015-05-17 13:58:53 +02002269int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data,
2270 int offset, int len)
2271{
2272 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2273
2274 return __kvm_read_guest_page(slot, gfn, data, offset, len);
2275}
2276EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page);
2277
Izik Eidus195aefd2007-10-01 22:14:18 +02002278int kvm_read_guest(struct kvm *kvm, gpa_t gpa, void *data, unsigned long len)
2279{
2280 gfn_t gfn = gpa >> PAGE_SHIFT;
2281 int seg;
2282 int offset = offset_in_page(gpa);
2283 int ret;
2284
2285 while ((seg = next_segment(len, offset)) != 0) {
2286 ret = kvm_read_guest_page(kvm, gfn, data, offset, seg);
2287 if (ret < 0)
2288 return ret;
2289 offset = 0;
2290 len -= seg;
2291 data += seg;
2292 ++gfn;
2293 }
2294 return 0;
2295}
2296EXPORT_SYMBOL_GPL(kvm_read_guest);
2297
Paolo Bonzini8e734852015-05-17 13:58:53 +02002298int kvm_vcpu_read_guest(struct kvm_vcpu *vcpu, gpa_t gpa, void *data, unsigned long len)
2299{
2300 gfn_t gfn = gpa >> PAGE_SHIFT;
2301 int seg;
2302 int offset = offset_in_page(gpa);
2303 int ret;
2304
2305 while ((seg = next_segment(len, offset)) != 0) {
2306 ret = kvm_vcpu_read_guest_page(vcpu, gfn, data, offset, seg);
2307 if (ret < 0)
2308 return ret;
2309 offset = 0;
2310 len -= seg;
2311 data += seg;
2312 ++gfn;
2313 }
2314 return 0;
2315}
2316EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest);
2317
2318static int __kvm_read_guest_atomic(struct kvm_memory_slot *slot, gfn_t gfn,
2319 void *data, int offset, unsigned long len)
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002320{
2321 int r;
2322 unsigned long addr;
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002323
Paolo Bonzini8e734852015-05-17 13:58:53 +02002324 addr = gfn_to_hva_memslot_prot(slot, gfn, NULL);
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002325 if (kvm_is_error_hva(addr))
2326 return -EFAULT;
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01002327 pagefault_disable();
Paolo Bonzini3180a7f2015-04-02 14:08:20 +02002328 r = __copy_from_user_inatomic(data, (void __user *)addr + offset, len);
Andrea Arcangeli0aac03f2008-01-30 19:57:35 +01002329 pagefault_enable();
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002330 if (r)
2331 return -EFAULT;
2332 return 0;
2333}
Marcelo Tosatti7ec54582007-12-20 19:18:23 -05002334
Paolo Bonzini8e734852015-05-17 13:58:53 +02002335int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa,
2336 void *data, unsigned long len)
2337{
2338 gfn_t gfn = gpa >> PAGE_SHIFT;
2339 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2340 int offset = offset_in_page(gpa);
2341
2342 return __kvm_read_guest_atomic(slot, gfn, data, offset, len);
2343}
2344EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic);
2345
2346static int __kvm_write_guest_page(struct kvm_memory_slot *memslot, gfn_t gfn,
2347 const void *data, int offset, int len)
Izik Eidus195aefd2007-10-01 22:14:18 +02002348{
Izik Eiduse0506bc2007-11-11 22:10:22 +02002349 int r;
2350 unsigned long addr;
Izik Eidus195aefd2007-10-01 22:14:18 +02002351
Radim Krčmář251eb842015-04-10 21:47:27 +02002352 addr = gfn_to_hva_memslot(memslot, gfn);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002353 if (kvm_is_error_hva(addr))
Izik Eidus195aefd2007-10-01 22:14:18 +02002354 return -EFAULT;
Xiao Guangrong8b0cedf2011-05-15 23:22:04 +08002355 r = __copy_to_user((void __user *)addr + offset, data, len);
Izik Eiduse0506bc2007-11-11 22:10:22 +02002356 if (r)
2357 return -EFAULT;
Paolo Bonzinibc009e42015-05-26 12:43:41 +02002358 mark_page_dirty_in_slot(memslot, gfn);
Izik Eidus195aefd2007-10-01 22:14:18 +02002359 return 0;
2360}
Paolo Bonzini8e734852015-05-17 13:58:53 +02002361
2362int kvm_write_guest_page(struct kvm *kvm, gfn_t gfn,
2363 const void *data, int offset, int len)
2364{
2365 struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
2366
2367 return __kvm_write_guest_page(slot, gfn, data, offset, len);
2368}
Izik Eidus195aefd2007-10-01 22:14:18 +02002369EXPORT_SYMBOL_GPL(kvm_write_guest_page);
2370
Paolo Bonzini8e734852015-05-17 13:58:53 +02002371int kvm_vcpu_write_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn,
2372 const void *data, int offset, int len)
2373{
2374 struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2375
2376 return __kvm_write_guest_page(slot, gfn, data, offset, len);
2377}
2378EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest_page);
2379
Izik Eidus195aefd2007-10-01 22:14:18 +02002380int kvm_write_guest(struct kvm *kvm, gpa_t gpa, const void *data,
2381 unsigned long len)
2382{
2383 gfn_t gfn = gpa >> PAGE_SHIFT;
2384 int seg;
2385 int offset = offset_in_page(gpa);
2386 int ret;
2387
2388 while ((seg = next_segment(len, offset)) != 0) {
2389 ret = kvm_write_guest_page(kvm, gfn, data, offset, seg);
2390 if (ret < 0)
2391 return ret;
2392 offset = 0;
2393 len -= seg;
2394 data += seg;
2395 ++gfn;
2396 }
2397 return 0;
2398}
Wincy Vanff651cb2014-12-11 08:52:58 +03002399EXPORT_SYMBOL_GPL(kvm_write_guest);
Izik Eidus195aefd2007-10-01 22:14:18 +02002400
Paolo Bonzini8e734852015-05-17 13:58:53 +02002401int kvm_vcpu_write_guest(struct kvm_vcpu *vcpu, gpa_t gpa, const void *data,
2402 unsigned long len)
2403{
2404 gfn_t gfn = gpa >> PAGE_SHIFT;
2405 int seg;
2406 int offset = offset_in_page(gpa);
2407 int ret;
2408
2409 while ((seg = next_segment(len, offset)) != 0) {
2410 ret = kvm_vcpu_write_guest_page(vcpu, gfn, data, offset, seg);
2411 if (ret < 0)
2412 return ret;
2413 offset = 0;
2414 len -= seg;
2415 data += seg;
2416 ++gfn;
2417 }
2418 return 0;
2419}
2420EXPORT_SYMBOL_GPL(kvm_vcpu_write_guest);
2421
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002422static int __kvm_gfn_to_hva_cache_init(struct kvm_memslots *slots,
2423 struct gfn_to_hva_cache *ghc,
2424 gpa_t gpa, unsigned long len)
Gleb Natapov49c77542010-10-18 15:22:23 +02002425{
Gleb Natapov49c77542010-10-18 15:22:23 +02002426 int offset = offset_in_page(gpa);
Andrew Honig8f964522013-03-29 09:35:21 -07002427 gfn_t start_gfn = gpa >> PAGE_SHIFT;
2428 gfn_t end_gfn = (gpa + len - 1) >> PAGE_SHIFT;
2429 gfn_t nr_pages_needed = end_gfn - start_gfn + 1;
2430 gfn_t nr_pages_avail;
Gleb Natapov49c77542010-10-18 15:22:23 +02002431
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002432 /* Update ghc->generation before performing any error checks. */
Gleb Natapov49c77542010-10-18 15:22:23 +02002433 ghc->generation = slots->generation;
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002434
2435 if (start_gfn > end_gfn) {
2436 ghc->hva = KVM_HVA_ERR_BAD;
2437 return -EINVAL;
2438 }
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002439
2440 /*
2441 * If the requested region crosses two memslots, we still
2442 * verify that the entire region is valid here.
2443 */
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002444 for ( ; start_gfn <= end_gfn; start_gfn += nr_pages_avail) {
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002445 ghc->memslot = __gfn_to_memslot(slots, start_gfn);
2446 ghc->hva = gfn_to_hva_many(ghc->memslot, start_gfn,
2447 &nr_pages_avail);
2448 if (kvm_is_error_hva(ghc->hva))
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002449 return -EFAULT;
Andrew Honig8f964522013-03-29 09:35:21 -07002450 }
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002451
2452 /* Use the slow path for cross page reads and writes. */
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002453 if (nr_pages_needed == 1)
Jim Mattsonf1b9dd52018-12-17 13:53:33 -08002454 ghc->hva += offset;
2455 else
2456 ghc->memslot = NULL;
2457
Sean Christopherson6ad1e292020-01-09 14:58:55 -05002458 ghc->gpa = gpa;
2459 ghc->len = len;
2460 return 0;
Gleb Natapov49c77542010-10-18 15:22:23 +02002461}
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002462
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002463int kvm_gfn_to_hva_cache_init(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002464 gpa_t gpa, unsigned long len)
2465{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002466 struct kvm_memslots *slots = kvm_memslots(kvm);
Paolo Bonzini5a2d4362017-02-03 20:32:28 -08002467 return __kvm_gfn_to_hva_cache_init(slots, ghc, gpa, len);
2468}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002469EXPORT_SYMBOL_GPL(kvm_gfn_to_hva_cache_init);
Gleb Natapov49c77542010-10-18 15:22:23 +02002470
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002471int kvm_write_guest_offset_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
Jim Mattson7a86dab2018-12-14 14:34:43 -08002472 void *data, unsigned int offset,
2473 unsigned long len)
Gleb Natapov49c77542010-10-18 15:22:23 +02002474{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002475 struct kvm_memslots *slots = kvm_memslots(kvm);
Gleb Natapov49c77542010-10-18 15:22:23 +02002476 int r;
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002477 gpa_t gpa = ghc->gpa + offset;
Gleb Natapov49c77542010-10-18 15:22:23 +02002478
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002479 BUG_ON(len + offset > ghc->len);
Andrew Honig8f964522013-03-29 09:35:21 -07002480
Sean Christophersondc9ce712020-01-09 15:56:20 -08002481 if (slots->generation != ghc->generation) {
2482 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
2483 return -EFAULT;
2484 }
Andrew Honig8f964522013-03-29 09:35:21 -07002485
Gleb Natapov49c77542010-10-18 15:22:23 +02002486 if (kvm_is_error_hva(ghc->hva))
2487 return -EFAULT;
2488
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002489 if (unlikely(!ghc->memslot))
2490 return kvm_write_guest(kvm, gpa, data, len);
2491
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002492 r = __copy_to_user((void __user *)ghc->hva + offset, data, len);
Gleb Natapov49c77542010-10-18 15:22:23 +02002493 if (r)
2494 return -EFAULT;
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002495 mark_page_dirty_in_slot(ghc->memslot, gpa >> PAGE_SHIFT);
Gleb Natapov49c77542010-10-18 15:22:23 +02002496
2497 return 0;
2498}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002499EXPORT_SYMBOL_GPL(kvm_write_guest_offset_cached);
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002500
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002501int kvm_write_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2502 void *data, unsigned long len)
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002503{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002504 return kvm_write_guest_offset_cached(kvm, ghc, data, 0, len);
Pan Xinhui4ec6e862016-11-02 05:08:34 -04002505}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002506EXPORT_SYMBOL_GPL(kvm_write_guest_cached);
Gleb Natapov49c77542010-10-18 15:22:23 +02002507
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002508int kvm_read_guest_cached(struct kvm *kvm, struct gfn_to_hva_cache *ghc,
2509 void *data, unsigned long len)
Gleb Natapove03b6442011-07-11 15:28:11 -04002510{
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002511 struct kvm_memslots *slots = kvm_memslots(kvm);
Gleb Natapove03b6442011-07-11 15:28:11 -04002512 int r;
2513
Andrew Honig8f964522013-03-29 09:35:21 -07002514 BUG_ON(len > ghc->len);
2515
Sean Christophersondc9ce712020-01-09 15:56:20 -08002516 if (slots->generation != ghc->generation) {
2517 if (__kvm_gfn_to_hva_cache_init(slots, ghc, ghc->gpa, ghc->len))
2518 return -EFAULT;
2519 }
Andrew Honig8f964522013-03-29 09:35:21 -07002520
Gleb Natapove03b6442011-07-11 15:28:11 -04002521 if (kvm_is_error_hva(ghc->hva))
2522 return -EFAULT;
2523
Sean Christophersonfcfbc612020-01-09 15:56:18 -08002524 if (unlikely(!ghc->memslot))
2525 return kvm_read_guest(kvm, ghc->gpa, data, len);
2526
Gleb Natapove03b6442011-07-11 15:28:11 -04002527 r = __copy_from_user(data, (void __user *)ghc->hva, len);
2528 if (r)
2529 return -EFAULT;
2530
2531 return 0;
2532}
Paolo Bonzini4e335d92017-05-02 16:20:18 +02002533EXPORT_SYMBOL_GPL(kvm_read_guest_cached);
Gleb Natapove03b6442011-07-11 15:28:11 -04002534
Izik Eidus195aefd2007-10-01 22:14:18 +02002535int kvm_clear_guest_page(struct kvm *kvm, gfn_t gfn, int offset, int len)
2536{
Heiko Carstens8a3caa62013-11-18 10:35:55 +01002537 const void *zero_page = (const void *) __va(page_to_phys(ZERO_PAGE(0)));
2538
2539 return kvm_write_guest_page(kvm, gfn, zero_page, offset, len);
Izik Eidus195aefd2007-10-01 22:14:18 +02002540}
2541EXPORT_SYMBOL_GPL(kvm_clear_guest_page);
2542
2543int kvm_clear_guest(struct kvm *kvm, gpa_t gpa, unsigned long len)
2544{
2545 gfn_t gfn = gpa >> PAGE_SHIFT;
2546 int seg;
2547 int offset = offset_in_page(gpa);
2548 int ret;
2549
Kevin Mulveybfda0e82015-02-20 08:21:36 -05002550 while ((seg = next_segment(len, offset)) != 0) {
Izik Eidus195aefd2007-10-01 22:14:18 +02002551 ret = kvm_clear_guest_page(kvm, gfn, offset, seg);
2552 if (ret < 0)
2553 return ret;
2554 offset = 0;
2555 len -= seg;
2556 ++gfn;
2557 }
2558 return 0;
2559}
2560EXPORT_SYMBOL_GPL(kvm_clear_guest);
2561
Paolo Bonzinibc009e42015-05-26 12:43:41 +02002562static void mark_page_dirty_in_slot(struct kvm_memory_slot *memslot,
Stephen Hemminger79408762013-12-29 12:12:29 -08002563 gfn_t gfn)
Avi Kivity6aa8b732006-12-10 02:21:36 -08002564{
Rusty Russell7e9d6192007-07-31 20:41:14 +10002565 if (memslot && memslot->dirty_bitmap) {
2566 unsigned long rel_gfn = gfn - memslot->base_gfn;
Avi Kivity6aa8b732006-12-10 02:21:36 -08002567
Takuya Yoshikawab74ca3b2012-10-04 17:13:12 -07002568 set_bit_le(rel_gfn, memslot->dirty_bitmap);
Avi Kivity6aa8b732006-12-10 02:21:36 -08002569 }
2570}
2571
Gleb Natapov49c77542010-10-18 15:22:23 +02002572void mark_page_dirty(struct kvm *kvm, gfn_t gfn)
2573{
2574 struct kvm_memory_slot *memslot;
2575
2576 memslot = gfn_to_memslot(kvm, gfn);
Paolo Bonzinibc009e42015-05-26 12:43:41 +02002577 mark_page_dirty_in_slot(memslot, gfn);
Gleb Natapov49c77542010-10-18 15:22:23 +02002578}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302579EXPORT_SYMBOL_GPL(mark_page_dirty);
Gleb Natapov49c77542010-10-18 15:22:23 +02002580
Paolo Bonzini8e734852015-05-17 13:58:53 +02002581void kvm_vcpu_mark_page_dirty(struct kvm_vcpu *vcpu, gfn_t gfn)
2582{
2583 struct kvm_memory_slot *memslot;
2584
2585 memslot = kvm_vcpu_gfn_to_memslot(vcpu, gfn);
2586 mark_page_dirty_in_slot(memslot, gfn);
2587}
2588EXPORT_SYMBOL_GPL(kvm_vcpu_mark_page_dirty);
2589
Jan H. Schönherr20b70352017-11-24 22:39:01 +01002590void kvm_sigset_activate(struct kvm_vcpu *vcpu)
2591{
2592 if (!vcpu->sigset_active)
2593 return;
2594
2595 /*
2596 * This does a lockless modification of ->real_blocked, which is fine
2597 * because, only current can change ->real_blocked and all readers of
2598 * ->real_blocked don't care as long ->real_blocked is always a subset
2599 * of ->blocked.
2600 */
2601 sigprocmask(SIG_SETMASK, &vcpu->sigset, &current->real_blocked);
2602}
2603
2604void kvm_sigset_deactivate(struct kvm_vcpu *vcpu)
2605{
2606 if (!vcpu->sigset_active)
2607 return;
2608
2609 sigprocmask(SIG_SETMASK, &current->real_blocked, NULL);
2610 sigemptyset(&current->real_blocked);
2611}
2612
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002613static void grow_halt_poll_ns(struct kvm_vcpu *vcpu)
2614{
Nir Weinerdee339b2019-01-27 12:17:16 +02002615 unsigned int old, val, grow, grow_start;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002616
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002617 old = val = vcpu->halt_poll_ns;
Nir Weinerdee339b2019-01-27 12:17:16 +02002618 grow_start = READ_ONCE(halt_poll_ns_grow_start);
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002619 grow = READ_ONCE(halt_poll_ns_grow);
Nir Weiner7fa08e72019-01-27 12:17:14 +02002620 if (!grow)
2621 goto out;
2622
Nir Weinerdee339b2019-01-27 12:17:16 +02002623 val *= grow;
2624 if (val < grow_start)
2625 val = grow_start;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002626
David Matlack313f6362016-03-08 16:19:44 -08002627 if (val > halt_poll_ns)
2628 val = halt_poll_ns;
2629
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002630 vcpu->halt_poll_ns = val;
Nir Weiner7fa08e72019-01-27 12:17:14 +02002631out:
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002632 trace_kvm_halt_poll_ns_grow(vcpu->vcpu_id, val, old);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002633}
2634
2635static void shrink_halt_poll_ns(struct kvm_vcpu *vcpu)
2636{
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002637 unsigned int old, val, shrink;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002638
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002639 old = val = vcpu->halt_poll_ns;
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002640 shrink = READ_ONCE(halt_poll_ns_shrink);
2641 if (shrink == 0)
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002642 val = 0;
2643 else
Christian Borntraeger6b6de682016-02-09 13:47:55 +01002644 val /= shrink;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002645
2646 vcpu->halt_poll_ns = val;
Wanpeng Li2cbd7822015-09-03 22:07:39 +08002647 trace_kvm_halt_poll_ns_shrink(vcpu->vcpu_id, val, old);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002648}
2649
Paolo Bonzinif7819512015-02-04 18:20:58 +01002650static int kvm_vcpu_check_block(struct kvm_vcpu *vcpu)
2651{
Junaid Shahid50c28f22018-06-27 14:59:11 -07002652 int ret = -EINTR;
2653 int idx = srcu_read_lock(&vcpu->kvm->srcu);
2654
Paolo Bonzinif7819512015-02-04 18:20:58 +01002655 if (kvm_arch_vcpu_runnable(vcpu)) {
2656 kvm_make_request(KVM_REQ_UNHALT, vcpu);
Junaid Shahid50c28f22018-06-27 14:59:11 -07002657 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002658 }
2659 if (kvm_cpu_has_pending_timer(vcpu))
Junaid Shahid50c28f22018-06-27 14:59:11 -07002660 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002661 if (signal_pending(current))
Junaid Shahid50c28f22018-06-27 14:59:11 -07002662 goto out;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002663
Junaid Shahid50c28f22018-06-27 14:59:11 -07002664 ret = 0;
2665out:
2666 srcu_read_unlock(&vcpu->kvm->srcu, idx);
2667 return ret;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002668}
2669
David Matlackcb953122020-05-08 11:22:40 -07002670static inline void
2671update_halt_poll_stats(struct kvm_vcpu *vcpu, u64 poll_ns, bool waited)
2672{
2673 if (waited)
2674 vcpu->stat.halt_poll_fail_ns += poll_ns;
2675 else
2676 vcpu->stat.halt_poll_success_ns += poll_ns;
2677}
2678
Eddie Dongb6958ce2007-07-18 12:15:21 +03002679/*
2680 * The vCPU has executed a HLT instruction with in-kernel mode enabled.
2681 */
Hollis Blanchard8776e512007-10-31 17:24:24 -05002682void kvm_vcpu_block(struct kvm_vcpu *vcpu)
Eddie Dongb6958ce2007-07-18 12:15:21 +03002683{
David Matlackcb953122020-05-08 11:22:40 -07002684 ktime_t start, cur, poll_end;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002685 bool waited = false;
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002686 u64 block_ns;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002687
Marc Zyngier07ab0f82019-08-02 11:37:09 +01002688 kvm_arch_vcpu_blocking(vcpu);
2689
David Matlackcb953122020-05-08 11:22:40 -07002690 start = cur = poll_end = ktime_get();
Christian Borntraegercdd6ad32019-03-05 05:30:01 -05002691 if (vcpu->halt_poll_ns && !kvm_arch_no_poll(vcpu)) {
Wanpeng Li19020f82015-09-03 22:07:37 +08002692 ktime_t stop = ktime_add_ns(ktime_get(), vcpu->halt_poll_ns);
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08002693
Paolo Bonzini62bea5b2015-09-15 18:27:57 +02002694 ++vcpu->stat.halt_attempted_poll;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002695 do {
2696 /*
2697 * This sets KVM_REQ_UNHALT if an interrupt
2698 * arrives.
2699 */
2700 if (kvm_vcpu_check_block(vcpu) < 0) {
2701 ++vcpu->stat.halt_successful_poll;
Christian Borntraeger3491caf2016-05-13 12:16:35 +02002702 if (!vcpu_valid_wakeup(vcpu))
2703 ++vcpu->stat.halt_poll_invalid;
Paolo Bonzinif7819512015-02-04 18:20:58 +01002704 goto out;
2705 }
David Matlackcb953122020-05-08 11:22:40 -07002706 poll_end = cur = ktime_get();
Paolo Bonzinif7819512015-02-04 18:20:58 +01002707 } while (single_task_running() && ktime_before(cur, stop));
2708 }
Eddie Dongb6958ce2007-07-18 12:15:21 +03002709
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002710 prepare_to_rcuwait(&vcpu->wait);
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03002711 for (;;) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002712 set_current_state(TASK_INTERRUPTIBLE);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002713
Paolo Bonzinif7819512015-02-04 18:20:58 +01002714 if (kvm_vcpu_check_block(vcpu) < 0)
Marcelo Tosattie5c239c2008-05-08 19:47:01 -03002715 break;
2716
Paolo Bonzinif7819512015-02-04 18:20:58 +01002717 waited = true;
Eddie Dongb6958ce2007-07-18 12:15:21 +03002718 schedule();
Eddie Dongb6958ce2007-07-18 12:15:21 +03002719 }
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002720 finish_rcuwait(&vcpu->wait);
Paolo Bonzinif7819512015-02-04 18:20:58 +01002721 cur = ktime_get();
Paolo Bonzinif7819512015-02-04 18:20:58 +01002722out:
Marc Zyngier07ab0f82019-08-02 11:37:09 +01002723 kvm_arch_vcpu_unblocking(vcpu);
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002724 block_ns = ktime_to_ns(cur) - ktime_to_ns(start);
2725
David Matlackcb953122020-05-08 11:22:40 -07002726 update_halt_poll_stats(
2727 vcpu, ktime_to_ns(ktime_sub(poll_end, start)), waited);
2728
Wanpeng Li44551b22019-09-29 09:06:56 +08002729 if (!kvm_arch_no_poll(vcpu)) {
2730 if (!vcpu_valid_wakeup(vcpu)) {
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002731 shrink_halt_poll_ns(vcpu);
David Matlackacd05782020-04-17 15:14:46 -07002732 } else if (vcpu->kvm->max_halt_poll_ns) {
Wanpeng Li44551b22019-09-29 09:06:56 +08002733 if (block_ns <= vcpu->halt_poll_ns)
2734 ;
2735 /* we had a long block, shrink polling */
David Matlackacd05782020-04-17 15:14:46 -07002736 else if (vcpu->halt_poll_ns &&
2737 block_ns > vcpu->kvm->max_halt_poll_ns)
Wanpeng Li44551b22019-09-29 09:06:56 +08002738 shrink_halt_poll_ns(vcpu);
2739 /* we had a short halt and our poll time is too small */
David Matlackacd05782020-04-17 15:14:46 -07002740 else if (vcpu->halt_poll_ns < vcpu->kvm->max_halt_poll_ns &&
2741 block_ns < vcpu->kvm->max_halt_poll_ns)
Wanpeng Li44551b22019-09-29 09:06:56 +08002742 grow_halt_poll_ns(vcpu);
2743 } else {
2744 vcpu->halt_poll_ns = 0;
2745 }
2746 }
Wanpeng Liaca6ff22015-09-03 22:07:38 +08002747
Christian Borntraeger3491caf2016-05-13 12:16:35 +02002748 trace_kvm_vcpu_wakeup(block_ns, waited, vcpu_valid_wakeup(vcpu));
2749 kvm_arch_vcpu_block_finish(vcpu);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002750}
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302751EXPORT_SYMBOL_GPL(kvm_vcpu_block);
Eddie Dongb6958ce2007-07-18 12:15:21 +03002752
Radim Krčmář178f02f2017-04-26 22:32:26 +02002753bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu)
Christoffer Dallb6d33832012-03-08 16:44:24 -05002754{
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002755 struct rcuwait *waitp;
Christoffer Dallb6d33832012-03-08 16:44:24 -05002756
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002757 waitp = kvm_arch_vcpu_get_wait(vcpu);
2758 if (rcuwait_wake_up(waitp)) {
Wanpeng Lid73eb572019-07-18 19:39:06 +08002759 WRITE_ONCE(vcpu->ready, true);
Christoffer Dallb6d33832012-03-08 16:44:24 -05002760 ++vcpu->stat.halt_wakeup;
Radim Krčmář178f02f2017-04-26 22:32:26 +02002761 return true;
Christoffer Dallb6d33832012-03-08 16:44:24 -05002762 }
2763
Radim Krčmář178f02f2017-04-26 22:32:26 +02002764 return false;
Radim Krčmářdd1a4cc2016-05-04 14:09:44 -05002765}
2766EXPORT_SYMBOL_GPL(kvm_vcpu_wake_up);
2767
Paolo Bonzini0266c892017-05-04 15:14:13 +02002768#ifndef CONFIG_S390
Radim Krčmářdd1a4cc2016-05-04 14:09:44 -05002769/*
2770 * Kick a sleeping VCPU, or a guest VCPU in guest mode, into host kernel mode.
2771 */
2772void kvm_vcpu_kick(struct kvm_vcpu *vcpu)
2773{
2774 int me;
2775 int cpu = vcpu->cpu;
2776
Radim Krčmář178f02f2017-04-26 22:32:26 +02002777 if (kvm_vcpu_wake_up(vcpu))
2778 return;
2779
Christoffer Dallb6d33832012-03-08 16:44:24 -05002780 me = get_cpu();
2781 if (cpu != me && (unsigned)cpu < nr_cpu_ids && cpu_online(cpu))
2782 if (kvm_arch_vcpu_should_kick(vcpu))
2783 smp_send_reschedule(cpu);
2784 put_cpu();
2785}
Yang Zhanga20ed542013-04-11 19:25:15 +08002786EXPORT_SYMBOL_GPL(kvm_vcpu_kick);
Paolo Bonzini0266c892017-05-04 15:14:13 +02002787#endif /* !CONFIG_S390 */
Christoffer Dallb6d33832012-03-08 16:44:24 -05002788
Dan Carpenterfa933842014-05-23 13:20:42 +03002789int kvm_vcpu_yield_to(struct kvm_vcpu *target)
Konstantin Weitz41628d32012-04-25 15:30:38 +02002790{
2791 struct pid *pid;
2792 struct task_struct *task = NULL;
Dan Carpenterfa933842014-05-23 13:20:42 +03002793 int ret = 0;
Konstantin Weitz41628d32012-04-25 15:30:38 +02002794
2795 rcu_read_lock();
2796 pid = rcu_dereference(target->pid);
2797 if (pid)
Sam Bobroff27fbe64b2014-09-19 09:40:41 +10002798 task = get_pid_task(pid, PIDTYPE_PID);
Konstantin Weitz41628d32012-04-25 15:30:38 +02002799 rcu_read_unlock();
2800 if (!task)
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302801 return ret;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302802 ret = yield_to(task, 1);
Konstantin Weitz41628d32012-04-25 15:30:38 +02002803 put_task_struct(task);
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302804
2805 return ret;
Konstantin Weitz41628d32012-04-25 15:30:38 +02002806}
2807EXPORT_SYMBOL_GPL(kvm_vcpu_yield_to);
2808
Raghavendra K T06e48c52012-07-19 15:17:52 +05302809/*
2810 * Helper that checks whether a VCPU is eligible for directed yield.
2811 * Most eligible candidate to yield is decided by following heuristics:
2812 *
2813 * (a) VCPU which has not done pl-exit or cpu relax intercepted recently
2814 * (preempted lock holder), indicated by @in_spin_loop.
2815 * Set at the beiginning and cleared at the end of interception/PLE handler.
2816 *
2817 * (b) VCPU which has done pl-exit/ cpu relax intercepted but did not get
2818 * chance last time (mostly it has become eligible now since we have probably
2819 * yielded to lockholder in last iteration. This is done by toggling
2820 * @dy_eligible each time a VCPU checked for eligibility.)
2821 *
2822 * Yielding to a recently pl-exited/cpu relax intercepted VCPU before yielding
2823 * to preempted lock-holder could result in wrong VCPU selection and CPU
2824 * burning. Giving priority for a potential lock-holder increases lock
2825 * progress.
2826 *
2827 * Since algorithm is based on heuristics, accessing another VCPU data without
2828 * locking does not harm. It may result in trying to yield to same VCPU, fail
2829 * and continue with next VCPU and so on.
2830 */
Stephen Hemminger79408762013-12-29 12:12:29 -08002831static bool kvm_vcpu_eligible_for_directed_yield(struct kvm_vcpu *vcpu)
Raghavendra K T06e48c52012-07-19 15:17:52 +05302832{
Scott Wood4a55dd72014-01-09 18:43:16 -06002833#ifdef CONFIG_HAVE_KVM_CPU_RELAX_INTERCEPT
Raghavendra K T06e48c52012-07-19 15:17:52 +05302834 bool eligible;
2835
2836 eligible = !vcpu->spin_loop.in_spin_loop ||
Christian Borntraeger34656112014-09-04 21:13:31 +02002837 vcpu->spin_loop.dy_eligible;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302838
2839 if (vcpu->spin_loop.in_spin_loop)
2840 kvm_vcpu_set_dy_eligible(vcpu, !vcpu->spin_loop.dy_eligible);
2841
2842 return eligible;
Scott Wood4a55dd72014-01-09 18:43:16 -06002843#else
2844 return true;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302845#endif
Scott Wood4a55dd72014-01-09 18:43:16 -06002846}
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302847
Wanpeng Li17e433b2019-08-05 10:03:19 +08002848/*
2849 * Unlike kvm_arch_vcpu_runnable, this function is called outside
2850 * a vcpu_load/vcpu_put pair. However, for most architectures
2851 * kvm_arch_vcpu_runnable does not require vcpu_load.
2852 */
2853bool __weak kvm_arch_dy_runnable(struct kvm_vcpu *vcpu)
2854{
2855 return kvm_arch_vcpu_runnable(vcpu);
2856}
2857
2858static bool vcpu_dy_runnable(struct kvm_vcpu *vcpu)
2859{
2860 if (kvm_arch_dy_runnable(vcpu))
2861 return true;
2862
2863#ifdef CONFIG_KVM_ASYNC_PF
2864 if (!list_empty_careful(&vcpu->async_pf.done))
2865 return true;
2866#endif
2867
2868 return false;
2869}
2870
Longpeng(Mike)199b5762017-08-08 12:05:32 +08002871void kvm_vcpu_on_spin(struct kvm_vcpu *me, bool yield_to_kernel_mode)
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002872{
Rik van Riel217ece62011-02-01 09:53:28 -05002873 struct kvm *kvm = me->kvm;
2874 struct kvm_vcpu *vcpu;
2875 int last_boosted_vcpu = me->kvm->last_boosted_vcpu;
2876 int yielded = 0;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302877 int try = 3;
Rik van Riel217ece62011-02-01 09:53:28 -05002878 int pass;
2879 int i;
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002880
Raghavendra K T4c088492012-07-18 19:07:46 +05302881 kvm_vcpu_set_in_spin_loop(me, true);
Rik van Riel217ece62011-02-01 09:53:28 -05002882 /*
2883 * We boost the priority of a VCPU that is runnable but not
2884 * currently running, because it got preempted by something
2885 * else and called schedule in __vcpu_run. Hopefully that
2886 * VCPU is holding the lock that we need and will release it.
2887 * We approximate round-robin by starting at the last boosted VCPU.
2888 */
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302889 for (pass = 0; pass < 2 && !yielded && try; pass++) {
Rik van Riel217ece62011-02-01 09:53:28 -05002890 kvm_for_each_vcpu(i, vcpu, kvm) {
Rik van Riel5cfc2aa2012-06-19 16:51:04 -04002891 if (!pass && i <= last_boosted_vcpu) {
Rik van Riel217ece62011-02-01 09:53:28 -05002892 i = last_boosted_vcpu;
2893 continue;
2894 } else if (pass && i > last_boosted_vcpu)
2895 break;
Wanpeng Lid73eb572019-07-18 19:39:06 +08002896 if (!READ_ONCE(vcpu->ready))
Raghavendra K T7bc7ae22013-03-04 23:32:27 +05302897 continue;
Rik van Riel217ece62011-02-01 09:53:28 -05002898 if (vcpu == me)
2899 continue;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002900 if (rcuwait_active(&vcpu->wait) &&
2901 !vcpu_dy_runnable(vcpu))
Rik van Riel217ece62011-02-01 09:53:28 -05002902 continue;
Wanpeng Li046ddee2019-08-01 11:30:14 +08002903 if (READ_ONCE(vcpu->preempted) && yield_to_kernel_mode &&
2904 !kvm_arch_vcpu_in_kernel(vcpu))
Longpeng(Mike)199b5762017-08-08 12:05:32 +08002905 continue;
Raghavendra K T06e48c52012-07-19 15:17:52 +05302906 if (!kvm_vcpu_eligible_for_directed_yield(vcpu))
2907 continue;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302908
2909 yielded = kvm_vcpu_yield_to(vcpu);
2910 if (yielded > 0) {
Rik van Riel217ece62011-02-01 09:53:28 -05002911 kvm->last_boosted_vcpu = i;
Rik van Riel217ece62011-02-01 09:53:28 -05002912 break;
Raghavendra K Tc45c5282013-01-22 13:09:24 +05302913 } else if (yielded < 0) {
2914 try--;
2915 if (!try)
2916 break;
Rik van Riel217ece62011-02-01 09:53:28 -05002917 }
Rik van Riel217ece62011-02-01 09:53:28 -05002918 }
2919 }
Raghavendra K T4c088492012-07-18 19:07:46 +05302920 kvm_vcpu_set_in_spin_loop(me, false);
Raghavendra K T06e48c52012-07-19 15:17:52 +05302921
2922 /* Ensure vcpu is not eligible during next spinloop */
2923 kvm_vcpu_set_dy_eligible(me, false);
Zhai, Edwind255f4f2009-10-09 18:03:20 +08002924}
2925EXPORT_SYMBOL_GPL(kvm_vcpu_on_spin);
2926
Souptick Joarder1499fa82018-04-19 00:49:58 +05302927static vm_fault_t kvm_vcpu_fault(struct vm_fault *vmf)
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002928{
Dave Jiang11bac802017-02-24 14:56:41 -08002929 struct kvm_vcpu *vcpu = vmf->vma->vm_file->private_data;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002930 struct page *page;
2931
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002932 if (vmf->pgoff == 0)
Avi Kivity039576c2007-03-20 12:46:50 +02002933 page = virt_to_page(vcpu->run);
Avi Kivity09566762008-01-23 18:14:23 +02002934#ifdef CONFIG_X86
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002935 else if (vmf->pgoff == KVM_PIO_PAGE_OFFSET)
Zhang Xiantaoad312c72007-12-13 23:50:52 +08002936 page = virt_to_page(vcpu->arch.pio_data);
Avi Kivity09566762008-01-23 18:14:23 +02002937#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02002938#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02002939 else if (vmf->pgoff == KVM_COALESCED_MMIO_PAGE_OFFSET)
2940 page = virt_to_page(vcpu->kvm->coalesced_mmio_ring);
2941#endif
Avi Kivity039576c2007-03-20 12:46:50 +02002942 else
Carsten Otte5b1c1492012-01-04 10:25:23 +01002943 return kvm_arch_vcpu_fault(vcpu, vmf);
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002944 get_page(page);
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002945 vmf->page = page;
2946 return 0;
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002947}
2948
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04002949static const struct vm_operations_struct kvm_vcpu_vm_ops = {
npiggin@suse.dee4a533a2007-12-05 18:15:52 +11002950 .fault = kvm_vcpu_fault,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002951};
2952
2953static int kvm_vcpu_mmap(struct file *file, struct vm_area_struct *vma)
2954{
2955 vma->vm_ops = &kvm_vcpu_vm_ops;
2956 return 0;
2957}
2958
Avi Kivitybccf2152007-02-21 18:04:26 +02002959static int kvm_vcpu_release(struct inode *inode, struct file *filp)
2960{
2961 struct kvm_vcpu *vcpu = filp->private_data;
2962
Luiz Capitulino45b59392016-09-16 10:27:35 -04002963 debugfs_remove_recursive(vcpu->debugfs_dentry);
Al Viro66c0b392008-04-19 20:33:56 +01002964 kvm_put_kvm(vcpu->kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02002965 return 0;
2966}
2967
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01002968static struct file_operations kvm_vcpu_fops = {
Avi Kivitybccf2152007-02-21 18:04:26 +02002969 .release = kvm_vcpu_release,
2970 .unlocked_ioctl = kvm_vcpu_ioctl,
Avi Kivity9a2bb7f2007-02-22 12:58:31 +02002971 .mmap = kvm_vcpu_mmap,
Arnd Bergmann6038f372010-08-15 18:52:59 +02002972 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01002973 KVM_COMPAT(kvm_vcpu_compat_ioctl),
Avi Kivitybccf2152007-02-21 18:04:26 +02002974};
2975
2976/*
2977 * Allocates an inode for the vcpu.
2978 */
2979static int create_vcpu_fd(struct kvm_vcpu *vcpu)
2980{
Masatake YAMATOe46b4692018-01-20 04:04:22 +09002981 char name[8 + 1 + ITOA_MAX_LEN + 1];
2982
2983 snprintf(name, sizeof(name), "kvm-vcpu:%d", vcpu->vcpu_id);
2984 return anon_inode_getfd(name, &kvm_vcpu_fops, vcpu, O_RDWR | O_CLOEXEC);
Avi Kivitybccf2152007-02-21 18:04:26 +02002985}
2986
Greg KH3e7093d2019-07-31 20:56:20 +02002987static void kvm_create_vcpu_debugfs(struct kvm_vcpu *vcpu)
Luiz Capitulino45b59392016-09-16 10:27:35 -04002988{
Paolo Bonzini741cbba2019-08-03 08:14:25 +02002989#ifdef __KVM_HAVE_ARCH_VCPU_DEBUGFS
Luiz Capitulino45b59392016-09-16 10:27:35 -04002990 char dir_name[ITOA_MAX_LEN * 2];
Luiz Capitulino45b59392016-09-16 10:27:35 -04002991
Luiz Capitulino45b59392016-09-16 10:27:35 -04002992 if (!debugfs_initialized())
Greg KH3e7093d2019-07-31 20:56:20 +02002993 return;
Luiz Capitulino45b59392016-09-16 10:27:35 -04002994
2995 snprintf(dir_name, sizeof(dir_name), "vcpu%d", vcpu->vcpu_id);
2996 vcpu->debugfs_dentry = debugfs_create_dir(dir_name,
Greg KH3e7093d2019-07-31 20:56:20 +02002997 vcpu->kvm->debugfs_dentry);
Luiz Capitulino45b59392016-09-16 10:27:35 -04002998
Greg KH3e7093d2019-07-31 20:56:20 +02002999 kvm_arch_create_vcpu_debugfs(vcpu);
Paolo Bonzini741cbba2019-08-03 08:14:25 +02003000#endif
Luiz Capitulino45b59392016-09-16 10:27:35 -04003001}
3002
Avi Kivityc5ea7662007-02-20 18:41:05 +02003003/*
3004 * Creates some virtual cpus. Good luck creating more than one.
3005 */
Gleb Natapov73880c82009-06-09 15:56:28 +03003006static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
Avi Kivityc5ea7662007-02-20 18:41:05 +02003007{
3008 int r;
David Hildenbrande09fefd2015-11-05 09:03:50 +01003009 struct kvm_vcpu *vcpu;
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003010 struct page *page;
Avi Kivityc5ea7662007-02-20 18:41:05 +02003011
Greg Kurz0b1b1df2016-05-09 18:13:37 +02003012 if (id >= KVM_MAX_VCPU_ID)
Andy Honig338c7db2013-11-18 16:09:22 -08003013 return -EINVAL;
3014
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003015 mutex_lock(&kvm->lock);
3016 if (kvm->created_vcpus == KVM_MAX_VCPUS) {
3017 mutex_unlock(&kvm->lock);
3018 return -EINVAL;
3019 }
3020
3021 kvm->created_vcpus++;
3022 mutex_unlock(&kvm->lock);
3023
Sean Christopherson897cc382019-12-18 13:55:09 -08003024 r = kvm_arch_vcpu_precreate(kvm, id);
3025 if (r)
3026 goto vcpu_decrement;
3027
Sean Christophersone529ef62019-12-18 13:55:15 -08003028 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
3029 if (!vcpu) {
3030 r = -ENOMEM;
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003031 goto vcpu_decrement;
3032 }
Avi Kivityc5ea7662007-02-20 18:41:05 +02003033
Peter Xufcd97ad2020-01-09 09:57:12 -05003034 BUILD_BUG_ON(sizeof(struct kvm_run) > PAGE_SIZE);
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003035 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
3036 if (!page) {
3037 r = -ENOMEM;
Sean Christophersone529ef62019-12-18 13:55:15 -08003038 goto vcpu_free;
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003039 }
3040 vcpu->run = page_address(page);
3041
3042 kvm_vcpu_init(vcpu, kvm, id);
Sean Christophersone529ef62019-12-18 13:55:15 -08003043
3044 r = kvm_arch_vcpu_create(vcpu);
3045 if (r)
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003046 goto vcpu_free_run_page;
Sean Christophersone529ef62019-12-18 13:55:15 -08003047
Shaohua Li11ec2802007-07-23 14:51:37 +08003048 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +01003049 if (kvm_get_vcpu_by_id(kvm, id)) {
3050 r = -EEXIST;
3051 goto unlock_vcpu_destroy;
3052 }
Gleb Natapov73880c82009-06-09 15:56:28 +03003053
Radim Krčmář8750e722019-11-07 07:53:42 -05003054 vcpu->vcpu_idx = atomic_read(&kvm->online_vcpus);
3055 BUG_ON(kvm->vcpus[vcpu->vcpu_idx]);
Rusty Russellfb3f0f52007-07-27 17:16:56 +10003056
3057 /* Now it's all set up, let userspace reach it */
Al Viro66c0b392008-04-19 20:33:56 +01003058 kvm_get_kvm(kvm);
Avi Kivitybccf2152007-02-21 18:04:26 +02003059 r = create_vcpu_fd(vcpu);
Gleb Natapov73880c82009-06-09 15:56:28 +03003060 if (r < 0) {
Sean Christopherson149487b2019-10-21 15:58:42 -07003061 kvm_put_kvm_no_destroy(kvm);
Jan Kiszkad7805922011-05-23 10:33:05 +02003062 goto unlock_vcpu_destroy;
Gleb Natapov73880c82009-06-09 15:56:28 +03003063 }
3064
Radim Krčmář8750e722019-11-07 07:53:42 -05003065 kvm->vcpus[vcpu->vcpu_idx] = vcpu;
Paolo Bonzinidd489242015-07-29 11:32:20 +02003066
3067 /*
3068 * Pairs with smp_rmb() in kvm_get_vcpu. Write kvm->vcpus
3069 * before kvm->online_vcpu's incremented value.
3070 */
Gleb Natapov73880c82009-06-09 15:56:28 +03003071 smp_wmb();
3072 atomic_inc(&kvm->online_vcpus);
3073
Gleb Natapov73880c82009-06-09 15:56:28 +03003074 mutex_unlock(&kvm->lock);
Marcelo Tosatti42897d82012-11-27 23:29:02 -02003075 kvm_arch_vcpu_postcreate(vcpu);
Paolo Bonzini63d04342020-04-01 00:42:22 +02003076 kvm_create_vcpu_debugfs(vcpu);
Avi Kivitybccf2152007-02-21 18:04:26 +02003077 return r;
Avi Kivityc5ea7662007-02-20 18:41:05 +02003078
Jan Kiszkad7805922011-05-23 10:33:05 +02003079unlock_vcpu_destroy:
Glauber Costa7d8fece2008-09-17 23:16:59 -03003080 mutex_unlock(&kvm->lock);
Hollis Blanchardd40ccc62007-11-19 14:04:43 -06003081 kvm_arch_vcpu_destroy(vcpu);
Sean Christopherson8bd826d2019-12-18 13:55:30 -08003082vcpu_free_run_page:
3083 free_page((unsigned long)vcpu->run);
Sean Christophersone529ef62019-12-18 13:55:15 -08003084vcpu_free:
3085 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paolo Bonzini6c7caeb2016-06-13 14:48:25 +02003086vcpu_decrement:
3087 mutex_lock(&kvm->lock);
3088 kvm->created_vcpus--;
3089 mutex_unlock(&kvm->lock);
Avi Kivityc5ea7662007-02-20 18:41:05 +02003090 return r;
3091}
3092
Avi Kivity1961d272007-03-05 19:46:05 +02003093static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset)
3094{
3095 if (sigset) {
3096 sigdelsetmask(sigset, sigmask(SIGKILL)|sigmask(SIGSTOP));
3097 vcpu->sigset_active = 1;
3098 vcpu->sigset = *sigset;
3099 } else
3100 vcpu->sigset_active = 0;
3101 return 0;
3102}
3103
Avi Kivitybccf2152007-02-21 18:04:26 +02003104static long kvm_vcpu_ioctl(struct file *filp,
3105 unsigned int ioctl, unsigned long arg)
Avi Kivity6aa8b732006-12-10 02:21:36 -08003106{
Avi Kivitybccf2152007-02-21 18:04:26 +02003107 struct kvm_vcpu *vcpu = filp->private_data;
Al Viro2f3669872007-02-09 16:38:35 +00003108 void __user *argp = (void __user *)arg;
Carsten Otte313a3dc2007-10-11 19:16:52 +02003109 int r;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003110 struct kvm_fpu *fpu = NULL;
3111 struct kvm_sregs *kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003112
Avi Kivity6d4e4c42007-11-21 16:41:05 +02003113 if (vcpu->kvm->mm != current->mm)
3114 return -EIO;
Avi Kivity2122ff52010-05-13 11:25:04 +03003115
David Matlack2ea75be2014-09-19 16:03:25 -07003116 if (unlikely(_IOC_TYPE(ioctl) != KVMIO))
3117 return -EINVAL;
3118
Avi Kivity2122ff52010-05-13 11:25:04 +03003119 /*
Paolo Bonzini5cb09442017-12-12 17:41:34 +01003120 * Some architectures have vcpu ioctls that are asynchronous to vcpu
3121 * execution; mutex_lock() would break them.
Avi Kivity2122ff52010-05-13 11:25:04 +03003122 */
Paolo Bonzini5cb09442017-12-12 17:41:34 +01003123 r = kvm_arch_vcpu_async_ioctl(filp, ioctl, arg);
3124 if (r != -ENOIOCTLCMD)
Michael S. Tsirkin9fc77442012-09-16 11:50:30 +03003125 return r;
Avi Kivity2122ff52010-05-13 11:25:04 +03003126
Christoffer Dallec7660c2017-12-04 21:35:23 +01003127 if (mutex_lock_killable(&vcpu->mutex))
3128 return -EINTR;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003129 switch (ioctl) {
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003130 case KVM_RUN: {
3131 struct pid *oldpid;
Avi Kivityf0fe5102007-03-07 13:11:17 +02003132 r = -EINVAL;
3133 if (arg)
3134 goto out;
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003135 oldpid = rcu_access_pointer(vcpu->pid);
Eric W. Biederman71dbc8a2017-07-16 21:39:32 -05003136 if (unlikely(oldpid != task_pid(current))) {
Christian Borntraeger7a72f7a2014-08-05 16:44:14 +02003137 /* The thread running this VCPU changed. */
Christoffer Dallbd2a6392018-02-23 17:23:57 +01003138 struct pid *newpid;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003139
Christoffer Dallbd2a6392018-02-23 17:23:57 +01003140 r = kvm_arch_vcpu_run_pid_change(vcpu);
3141 if (r)
3142 break;
3143
3144 newpid = get_task_pid(current, PIDTYPE_PID);
Christian Borntraeger7a72f7a2014-08-05 16:44:14 +02003145 rcu_assign_pointer(vcpu->pid, newpid);
3146 if (oldpid)
3147 synchronize_rcu();
3148 put_pid(oldpid);
3149 }
Tianjia Zhang1b94f6f2020-04-16 13:10:57 +08003150 r = kvm_arch_vcpu_ioctl_run(vcpu);
Gleb Natapov64be5002010-10-24 16:49:08 +02003151 trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003152 break;
Christian Borntraeger0e4524a2017-07-06 14:44:28 +02003153 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003154 case KVM_GET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003155 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003156
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003157 r = -ENOMEM;
Ben Gardonb12ce362019-02-11 11:02:49 -08003158 kvm_regs = kzalloc(sizeof(struct kvm_regs), GFP_KERNEL_ACCOUNT);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003159 if (!kvm_regs)
3160 goto out;
3161 r = kvm_arch_vcpu_ioctl_get_regs(vcpu, kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003162 if (r)
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003163 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003164 r = -EFAULT;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003165 if (copy_to_user(argp, kvm_regs, sizeof(struct kvm_regs)))
3166 goto out_free1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003167 r = 0;
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003168out_free1:
3169 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003170 break;
3171 }
3172 case KVM_SET_REGS: {
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003173 struct kvm_regs *kvm_regs;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003174
Sasha Levinff5c2c02011-12-04 19:36:29 +02003175 kvm_regs = memdup_user(argp, sizeof(*kvm_regs));
3176 if (IS_ERR(kvm_regs)) {
3177 r = PTR_ERR(kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003178 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003179 }
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003180 r = kvm_arch_vcpu_ioctl_set_regs(vcpu, kvm_regs);
Xiantao Zhang3e4bb3a2008-02-25 18:52:20 +08003181 kfree(kvm_regs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003182 break;
3183 }
3184 case KVM_GET_SREGS: {
Ben Gardonb12ce362019-02-11 11:02:49 -08003185 kvm_sregs = kzalloc(sizeof(struct kvm_sregs),
3186 GFP_KERNEL_ACCOUNT);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003187 r = -ENOMEM;
3188 if (!kvm_sregs)
3189 goto out;
3190 r = kvm_arch_vcpu_ioctl_get_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003191 if (r)
3192 goto out;
3193 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003194 if (copy_to_user(argp, kvm_sregs, sizeof(struct kvm_sregs)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003195 goto out;
3196 r = 0;
3197 break;
3198 }
3199 case KVM_SET_SREGS: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003200 kvm_sregs = memdup_user(argp, sizeof(*kvm_sregs));
3201 if (IS_ERR(kvm_sregs)) {
3202 r = PTR_ERR(kvm_sregs);
Guo Chao18595412012-11-02 18:33:21 +08003203 kvm_sregs = NULL;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003204 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003205 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07003206 r = kvm_arch_vcpu_ioctl_set_sregs(vcpu, kvm_sregs);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003207 break;
3208 }
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003209 case KVM_GET_MP_STATE: {
3210 struct kvm_mp_state mp_state;
3211
3212 r = kvm_arch_vcpu_ioctl_get_mpstate(vcpu, &mp_state);
3213 if (r)
3214 goto out;
3215 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003216 if (copy_to_user(argp, &mp_state, sizeof(mp_state)))
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003217 goto out;
3218 r = 0;
3219 break;
3220 }
3221 case KVM_SET_MP_STATE: {
3222 struct kvm_mp_state mp_state;
3223
3224 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003225 if (copy_from_user(&mp_state, argp, sizeof(mp_state)))
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003226 goto out;
3227 r = kvm_arch_vcpu_ioctl_set_mpstate(vcpu, &mp_state);
Marcelo Tosatti62d9f0d2008-04-11 13:24:45 -03003228 break;
3229 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08003230 case KVM_TRANSLATE: {
3231 struct kvm_translation tr;
3232
3233 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003234 if (copy_from_user(&tr, argp, sizeof(tr)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003235 goto out;
Zhang Xiantao8b006792007-11-16 13:05:55 +08003236 r = kvm_arch_vcpu_ioctl_translate(vcpu, &tr);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003237 if (r)
3238 goto out;
3239 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003240 if (copy_to_user(argp, &tr, sizeof(tr)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003241 goto out;
3242 r = 0;
3243 break;
3244 }
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003245 case KVM_SET_GUEST_DEBUG: {
3246 struct kvm_guest_debug dbg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003247
3248 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003249 if (copy_from_user(&dbg, argp, sizeof(dbg)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003250 goto out;
Jan Kiszkad0bfb942008-12-15 13:52:10 +01003251 r = kvm_arch_vcpu_ioctl_set_guest_debug(vcpu, &dbg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003252 break;
3253 }
Avi Kivity1961d272007-03-05 19:46:05 +02003254 case KVM_SET_SIGNAL_MASK: {
3255 struct kvm_signal_mask __user *sigmask_arg = argp;
3256 struct kvm_signal_mask kvm_sigmask;
3257 sigset_t sigset, *p;
3258
3259 p = NULL;
3260 if (argp) {
3261 r = -EFAULT;
3262 if (copy_from_user(&kvm_sigmask, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003263 sizeof(kvm_sigmask)))
Avi Kivity1961d272007-03-05 19:46:05 +02003264 goto out;
3265 r = -EINVAL;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003266 if (kvm_sigmask.len != sizeof(sigset))
Avi Kivity1961d272007-03-05 19:46:05 +02003267 goto out;
3268 r = -EFAULT;
3269 if (copy_from_user(&sigset, sigmask_arg->sigset,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003270 sizeof(sigset)))
Avi Kivity1961d272007-03-05 19:46:05 +02003271 goto out;
3272 p = &sigset;
3273 }
Andi Kleen376d41f2010-06-10 13:10:47 +02003274 r = kvm_vcpu_ioctl_set_sigmask(vcpu, p);
Avi Kivity1961d272007-03-05 19:46:05 +02003275 break;
3276 }
Avi Kivityb8836732007-04-01 16:34:31 +03003277 case KVM_GET_FPU: {
Ben Gardonb12ce362019-02-11 11:02:49 -08003278 fpu = kzalloc(sizeof(struct kvm_fpu), GFP_KERNEL_ACCOUNT);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003279 r = -ENOMEM;
3280 if (!fpu)
3281 goto out;
3282 r = kvm_arch_vcpu_ioctl_get_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03003283 if (r)
3284 goto out;
3285 r = -EFAULT;
Dave Hansenfa3795a2008-08-11 10:01:46 -07003286 if (copy_to_user(argp, fpu, sizeof(struct kvm_fpu)))
Avi Kivityb8836732007-04-01 16:34:31 +03003287 goto out;
3288 r = 0;
3289 break;
3290 }
3291 case KVM_SET_FPU: {
Sasha Levinff5c2c02011-12-04 19:36:29 +02003292 fpu = memdup_user(argp, sizeof(*fpu));
3293 if (IS_ERR(fpu)) {
3294 r = PTR_ERR(fpu);
Guo Chao18595412012-11-02 18:33:21 +08003295 fpu = NULL;
Avi Kivityb8836732007-04-01 16:34:31 +03003296 goto out;
Sasha Levinff5c2c02011-12-04 19:36:29 +02003297 }
Dave Hansenfa3795a2008-08-11 10:01:46 -07003298 r = kvm_arch_vcpu_ioctl_set_fpu(vcpu, fpu);
Avi Kivityb8836732007-04-01 16:34:31 +03003299 break;
3300 }
Avi Kivitybccf2152007-02-21 18:04:26 +02003301 default:
Carsten Otte313a3dc2007-10-11 19:16:52 +02003302 r = kvm_arch_vcpu_ioctl(filp, ioctl, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02003303 }
3304out:
Christoffer Dallec7660c2017-12-04 21:35:23 +01003305 mutex_unlock(&vcpu->mutex);
Dave Hansenfa3795a2008-08-11 10:01:46 -07003306 kfree(fpu);
3307 kfree(kvm_sregs);
Avi Kivitybccf2152007-02-21 18:04:26 +02003308 return r;
3309}
3310
Christian Borntraegerde8e5d72015-02-03 09:35:15 +01003311#ifdef CONFIG_KVM_COMPAT
Alexander Graf1dda6062011-06-08 02:45:37 +02003312static long kvm_vcpu_compat_ioctl(struct file *filp,
3313 unsigned int ioctl, unsigned long arg)
3314{
3315 struct kvm_vcpu *vcpu = filp->private_data;
3316 void __user *argp = compat_ptr(arg);
3317 int r;
3318
3319 if (vcpu->kvm->mm != current->mm)
3320 return -EIO;
3321
3322 switch (ioctl) {
3323 case KVM_SET_SIGNAL_MASK: {
3324 struct kvm_signal_mask __user *sigmask_arg = argp;
3325 struct kvm_signal_mask kvm_sigmask;
Alexander Graf1dda6062011-06-08 02:45:37 +02003326 sigset_t sigset;
3327
3328 if (argp) {
3329 r = -EFAULT;
3330 if (copy_from_user(&kvm_sigmask, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003331 sizeof(kvm_sigmask)))
Alexander Graf1dda6062011-06-08 02:45:37 +02003332 goto out;
3333 r = -EINVAL;
Al Viro3968cf62017-09-03 21:45:17 -04003334 if (kvm_sigmask.len != sizeof(compat_sigset_t))
Alexander Graf1dda6062011-06-08 02:45:37 +02003335 goto out;
3336 r = -EFAULT;
Al Viro3968cf62017-09-03 21:45:17 -04003337 if (get_compat_sigset(&sigset, (void *)sigmask_arg->sigset))
Alexander Graf1dda6062011-06-08 02:45:37 +02003338 goto out;
Alan Cox760a9a32012-08-22 14:34:11 +01003339 r = kvm_vcpu_ioctl_set_sigmask(vcpu, &sigset);
3340 } else
3341 r = kvm_vcpu_ioctl_set_sigmask(vcpu, NULL);
Alexander Graf1dda6062011-06-08 02:45:37 +02003342 break;
3343 }
3344 default:
3345 r = kvm_vcpu_ioctl(filp, ioctl, arg);
3346 }
3347
3348out:
3349 return r;
3350}
3351#endif
3352
Cédric Le Goatera1cd3f02019-04-18 12:39:36 +02003353static int kvm_device_mmap(struct file *filp, struct vm_area_struct *vma)
3354{
3355 struct kvm_device *dev = filp->private_data;
3356
3357 if (dev->ops->mmap)
3358 return dev->ops->mmap(dev, vma);
3359
3360 return -ENODEV;
3361}
3362
Scott Wood852b6d52013-04-12 14:08:42 +00003363static int kvm_device_ioctl_attr(struct kvm_device *dev,
3364 int (*accessor)(struct kvm_device *dev,
3365 struct kvm_device_attr *attr),
3366 unsigned long arg)
3367{
3368 struct kvm_device_attr attr;
3369
3370 if (!accessor)
3371 return -EPERM;
3372
3373 if (copy_from_user(&attr, (void __user *)arg, sizeof(attr)))
3374 return -EFAULT;
3375
3376 return accessor(dev, &attr);
3377}
3378
3379static long kvm_device_ioctl(struct file *filp, unsigned int ioctl,
3380 unsigned long arg)
3381{
3382 struct kvm_device *dev = filp->private_data;
3383
Sean Christophersonddba9182019-02-15 12:48:39 -08003384 if (dev->kvm->mm != current->mm)
3385 return -EIO;
3386
Scott Wood852b6d52013-04-12 14:08:42 +00003387 switch (ioctl) {
3388 case KVM_SET_DEVICE_ATTR:
3389 return kvm_device_ioctl_attr(dev, dev->ops->set_attr, arg);
3390 case KVM_GET_DEVICE_ATTR:
3391 return kvm_device_ioctl_attr(dev, dev->ops->get_attr, arg);
3392 case KVM_HAS_DEVICE_ATTR:
3393 return kvm_device_ioctl_attr(dev, dev->ops->has_attr, arg);
3394 default:
3395 if (dev->ops->ioctl)
3396 return dev->ops->ioctl(dev, ioctl, arg);
3397
3398 return -ENOTTY;
3399 }
3400}
3401
Scott Wood852b6d52013-04-12 14:08:42 +00003402static int kvm_device_release(struct inode *inode, struct file *filp)
3403{
3404 struct kvm_device *dev = filp->private_data;
3405 struct kvm *kvm = dev->kvm;
3406
Cédric Le Goater2bde9b32019-04-18 12:39:41 +02003407 if (dev->ops->release) {
3408 mutex_lock(&kvm->lock);
3409 list_del(&dev->vm_node);
3410 dev->ops->release(dev);
3411 mutex_unlock(&kvm->lock);
3412 }
3413
Scott Wood852b6d52013-04-12 14:08:42 +00003414 kvm_put_kvm(kvm);
3415 return 0;
3416}
3417
3418static const struct file_operations kvm_device_fops = {
3419 .unlocked_ioctl = kvm_device_ioctl,
3420 .release = kvm_device_release,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003421 KVM_COMPAT(kvm_device_ioctl),
Cédric Le Goatera1cd3f02019-04-18 12:39:36 +02003422 .mmap = kvm_device_mmap,
Scott Wood852b6d52013-04-12 14:08:42 +00003423};
3424
3425struct kvm_device *kvm_device_from_filp(struct file *filp)
3426{
3427 if (filp->f_op != &kvm_device_fops)
3428 return NULL;
3429
3430 return filp->private_data;
3431}
3432
Steven Price8538cb22019-10-21 16:28:19 +01003433static const struct kvm_device_ops *kvm_device_ops_table[KVM_DEV_TYPE_MAX] = {
Will Deacond60eacb2014-09-02 10:27:33 +01003434#ifdef CONFIG_KVM_MPIC
3435 [KVM_DEV_TYPE_FSL_MPIC_20] = &kvm_mpic_ops,
3436 [KVM_DEV_TYPE_FSL_MPIC_42] = &kvm_mpic_ops,
3437#endif
Will Deacond60eacb2014-09-02 10:27:33 +01003438};
3439
Steven Price8538cb22019-10-21 16:28:19 +01003440int kvm_register_device_ops(const struct kvm_device_ops *ops, u32 type)
Will Deacond60eacb2014-09-02 10:27:33 +01003441{
3442 if (type >= ARRAY_SIZE(kvm_device_ops_table))
3443 return -ENOSPC;
3444
3445 if (kvm_device_ops_table[type] != NULL)
3446 return -EEXIST;
3447
3448 kvm_device_ops_table[type] = ops;
3449 return 0;
3450}
3451
Wanpeng Li571ee1b2014-10-09 18:30:08 +08003452void kvm_unregister_device_ops(u32 type)
3453{
3454 if (kvm_device_ops_table[type] != NULL)
3455 kvm_device_ops_table[type] = NULL;
3456}
3457
Scott Wood852b6d52013-04-12 14:08:42 +00003458static int kvm_ioctl_create_device(struct kvm *kvm,
3459 struct kvm_create_device *cd)
3460{
Steven Price8538cb22019-10-21 16:28:19 +01003461 const struct kvm_device_ops *ops = NULL;
Scott Wood852b6d52013-04-12 14:08:42 +00003462 struct kvm_device *dev;
3463 bool test = cd->flags & KVM_CREATE_DEVICE_TEST;
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003464 int type;
Scott Wood852b6d52013-04-12 14:08:42 +00003465 int ret;
3466
Will Deacond60eacb2014-09-02 10:27:33 +01003467 if (cd->type >= ARRAY_SIZE(kvm_device_ops_table))
Scott Wood852b6d52013-04-12 14:08:42 +00003468 return -ENODEV;
Will Deacond60eacb2014-09-02 10:27:33 +01003469
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003470 type = array_index_nospec(cd->type, ARRAY_SIZE(kvm_device_ops_table));
3471 ops = kvm_device_ops_table[type];
Will Deacond60eacb2014-09-02 10:27:33 +01003472 if (ops == NULL)
3473 return -ENODEV;
Scott Wood852b6d52013-04-12 14:08:42 +00003474
3475 if (test)
3476 return 0;
3477
Ben Gardonb12ce362019-02-11 11:02:49 -08003478 dev = kzalloc(sizeof(*dev), GFP_KERNEL_ACCOUNT);
Scott Wood852b6d52013-04-12 14:08:42 +00003479 if (!dev)
3480 return -ENOMEM;
3481
3482 dev->ops = ops;
3483 dev->kvm = kvm;
Scott Wood852b6d52013-04-12 14:08:42 +00003484
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003485 mutex_lock(&kvm->lock);
Paolo Bonzini1d487e92019-04-11 11:16:47 +02003486 ret = ops->create(dev, type);
Scott Wood852b6d52013-04-12 14:08:42 +00003487 if (ret < 0) {
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003488 mutex_unlock(&kvm->lock);
Scott Wood852b6d52013-04-12 14:08:42 +00003489 kfree(dev);
3490 return ret;
3491 }
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003492 list_add(&dev->vm_node, &kvm->devices);
3493 mutex_unlock(&kvm->lock);
Scott Wood852b6d52013-04-12 14:08:42 +00003494
Christoffer Dall023e9fd2016-08-09 19:13:00 +02003495 if (ops->init)
3496 ops->init(dev);
3497
Jann Horncfa39382019-01-26 01:54:33 +01003498 kvm_get_kvm(kvm);
Yann Droneaud24009b02013-08-24 22:14:07 +02003499 ret = anon_inode_getfd(ops->name, &kvm_device_fops, dev, O_RDWR | O_CLOEXEC);
Scott Wood852b6d52013-04-12 14:08:42 +00003500 if (ret < 0) {
Sean Christopherson149487b2019-10-21 15:58:42 -07003501 kvm_put_kvm_no_destroy(kvm);
Christoffer Dalla28ebea2016-08-09 19:13:01 +02003502 mutex_lock(&kvm->lock);
3503 list_del(&dev->vm_node);
3504 mutex_unlock(&kvm->lock);
Dan Carpentera0f1d212016-11-30 22:21:05 +03003505 ops->destroy(dev);
Scott Wood852b6d52013-04-12 14:08:42 +00003506 return ret;
3507 }
3508
Scott Wood852b6d52013-04-12 14:08:42 +00003509 cd->fd = ret;
3510 return 0;
3511}
3512
Alexander Graf92b591a2014-07-14 18:33:08 +02003513static long kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg)
3514{
3515 switch (arg) {
3516 case KVM_CAP_USER_MEMORY:
3517 case KVM_CAP_DESTROY_MEMORY_REGION_WORKS:
3518 case KVM_CAP_JOIN_MEMORY_REGIONS_WORKS:
Alexander Graf92b591a2014-07-14 18:33:08 +02003519 case KVM_CAP_INTERNAL_ERROR_DATA:
3520#ifdef CONFIG_HAVE_KVM_MSI
3521 case KVM_CAP_SIGNAL_MSI:
3522#endif
Paul Mackerras297e2102014-06-30 20:51:13 +10003523#ifdef CONFIG_HAVE_KVM_IRQFD
Paolo Bonzinidc9be0f2015-03-05 11:54:46 +01003524 case KVM_CAP_IRQFD:
Alexander Graf92b591a2014-07-14 18:33:08 +02003525 case KVM_CAP_IRQFD_RESAMPLE:
3526#endif
Jason Wange9ea5062015-09-15 14:41:59 +08003527 case KVM_CAP_IOEVENTFD_ANY_LENGTH:
Alexander Graf92b591a2014-07-14 18:33:08 +02003528 case KVM_CAP_CHECK_EXTENSION_VM:
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003529 case KVM_CAP_ENABLE_CAP_VM:
David Matlackacd05782020-04-17 15:14:46 -07003530 case KVM_CAP_HALT_POLL:
Alexander Graf92b591a2014-07-14 18:33:08 +02003531 return 1;
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003532#ifdef CONFIG_KVM_MMIO
Paolo Bonzini30422552017-03-31 13:53:22 +02003533 case KVM_CAP_COALESCED_MMIO:
3534 return KVM_COALESCED_MMIO_PAGE_OFFSET;
Peng Hao0804c842018-10-14 07:09:55 +08003535 case KVM_CAP_COALESCED_PIO:
3536 return 1;
Paolo Bonzini30422552017-03-31 13:53:22 +02003537#endif
Jay Zhou3c9bd402020-02-27 09:32:27 +08003538#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3539 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2:
3540 return KVM_DIRTY_LOG_MANUAL_CAPS;
3541#endif
Alexander Graf92b591a2014-07-14 18:33:08 +02003542#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3543 case KVM_CAP_IRQ_ROUTING:
3544 return KVM_MAX_IRQ_ROUTES;
3545#endif
Paolo Bonzinif481b062015-05-17 17:30:37 +02003546#if KVM_ADDRESS_SPACE_NUM > 1
3547 case KVM_CAP_MULTI_ADDRESS_SPACE:
3548 return KVM_ADDRESS_SPACE_NUM;
3549#endif
Paolo Bonzinic110ae52019-03-28 17:24:03 +01003550 case KVM_CAP_NR_MEMSLOTS:
3551 return KVM_USER_MEM_SLOTS;
Alexander Graf92b591a2014-07-14 18:33:08 +02003552 default:
3553 break;
3554 }
3555 return kvm_vm_ioctl_check_extension(kvm, arg);
3556}
3557
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003558int __attribute__((weak)) kvm_vm_ioctl_enable_cap(struct kvm *kvm,
3559 struct kvm_enable_cap *cap)
3560{
3561 return -EINVAL;
3562}
3563
3564static int kvm_vm_ioctl_enable_cap_generic(struct kvm *kvm,
3565 struct kvm_enable_cap *cap)
3566{
3567 switch (cap->cap) {
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003568#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
Jay Zhou3c9bd402020-02-27 09:32:27 +08003569 case KVM_CAP_MANUAL_DIRTY_LOG_PROTECT2: {
3570 u64 allowed_options = KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE;
3571
3572 if (cap->args[0] & KVM_DIRTY_LOG_MANUAL_PROTECT_ENABLE)
3573 allowed_options = KVM_DIRTY_LOG_MANUAL_CAPS;
3574
3575 if (cap->flags || (cap->args[0] & ~allowed_options))
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003576 return -EINVAL;
3577 kvm->manual_dirty_log_protect = cap->args[0];
3578 return 0;
Jay Zhou3c9bd402020-02-27 09:32:27 +08003579 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003580#endif
David Matlackacd05782020-04-17 15:14:46 -07003581 case KVM_CAP_HALT_POLL: {
3582 if (cap->flags || cap->args[0] != (unsigned int)cap->args[0])
3583 return -EINVAL;
3584
3585 kvm->max_halt_poll_ns = cap->args[0];
3586 return 0;
3587 }
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003588 default:
3589 return kvm_vm_ioctl_enable_cap(kvm, cap);
3590 }
3591}
3592
Avi Kivitybccf2152007-02-21 18:04:26 +02003593static long kvm_vm_ioctl(struct file *filp,
3594 unsigned int ioctl, unsigned long arg)
3595{
3596 struct kvm *kvm = filp->private_data;
3597 void __user *argp = (void __user *)arg;
Carsten Otte1fe779f2007-10-29 16:08:35 +01003598 int r;
Avi Kivitybccf2152007-02-21 18:04:26 +02003599
Avi Kivity6d4e4c42007-11-21 16:41:05 +02003600 if (kvm->mm != current->mm)
3601 return -EIO;
Avi Kivitybccf2152007-02-21 18:04:26 +02003602 switch (ioctl) {
3603 case KVM_CREATE_VCPU:
3604 r = kvm_vm_ioctl_create_vcpu(kvm, arg);
Avi Kivitybccf2152007-02-21 18:04:26 +02003605 break;
Paolo Bonzinie5d83c72017-02-16 10:40:56 +01003606 case KVM_ENABLE_CAP: {
3607 struct kvm_enable_cap cap;
3608
3609 r = -EFAULT;
3610 if (copy_from_user(&cap, argp, sizeof(cap)))
3611 goto out;
3612 r = kvm_vm_ioctl_enable_cap_generic(kvm, &cap);
3613 break;
3614 }
Izik Eidus6fc138d2007-10-09 19:20:39 +02003615 case KVM_SET_USER_MEMORY_REGION: {
3616 struct kvm_userspace_memory_region kvm_userspace_mem;
3617
3618 r = -EFAULT;
3619 if (copy_from_user(&kvm_userspace_mem, argp,
Xiubo Li893bdbf2015-02-26 14:58:19 +08003620 sizeof(kvm_userspace_mem)))
Izik Eidus6fc138d2007-10-09 19:20:39 +02003621 goto out;
3622
Takuya Yoshikawa47ae31e2013-02-27 19:43:00 +09003623 r = kvm_vm_ioctl_set_memory_region(kvm, &kvm_userspace_mem);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003624 break;
3625 }
3626 case KVM_GET_DIRTY_LOG: {
3627 struct kvm_dirty_log log;
3628
3629 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003630 if (copy_from_user(&log, argp, sizeof(log)))
Avi Kivity6aa8b732006-12-10 02:21:36 -08003631 goto out;
Avi Kivity2c6f5df2007-02-20 18:27:58 +02003632 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003633 break;
3634 }
Paolo Bonzini2a31b9d2018-10-23 02:36:47 +02003635#ifdef CONFIG_KVM_GENERIC_DIRTYLOG_READ_PROTECT
3636 case KVM_CLEAR_DIRTY_LOG: {
3637 struct kvm_clear_dirty_log log;
3638
3639 r = -EFAULT;
3640 if (copy_from_user(&log, argp, sizeof(log)))
3641 goto out;
3642 r = kvm_vm_ioctl_clear_dirty_log(kvm, &log);
3643 break;
3644 }
3645#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003646#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003647 case KVM_REGISTER_COALESCED_MMIO: {
3648 struct kvm_coalesced_mmio_zone zone;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003649
Laurent Vivier5f94c172008-05-30 16:05:54 +02003650 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003651 if (copy_from_user(&zone, argp, sizeof(zone)))
Laurent Vivier5f94c172008-05-30 16:05:54 +02003652 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02003653 r = kvm_vm_ioctl_register_coalesced_mmio(kvm, &zone);
Laurent Vivier5f94c172008-05-30 16:05:54 +02003654 break;
3655 }
3656 case KVM_UNREGISTER_COALESCED_MMIO: {
3657 struct kvm_coalesced_mmio_zone zone;
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08003658
Laurent Vivier5f94c172008-05-30 16:05:54 +02003659 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003660 if (copy_from_user(&zone, argp, sizeof(zone)))
Laurent Vivier5f94c172008-05-30 16:05:54 +02003661 goto out;
Laurent Vivier5f94c172008-05-30 16:05:54 +02003662 r = kvm_vm_ioctl_unregister_coalesced_mmio(kvm, &zone);
Laurent Vivier5f94c172008-05-30 16:05:54 +02003663 break;
3664 }
3665#endif
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003666 case KVM_IRQFD: {
3667 struct kvm_irqfd data;
3668
3669 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003670 if (copy_from_user(&data, argp, sizeof(data)))
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003671 goto out;
Alex Williamsond4db2932012-06-29 09:56:08 -06003672 r = kvm_irqfd(kvm, &data);
Gregory Haskins721eecbf2009-05-20 10:30:49 -04003673 break;
3674 }
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003675 case KVM_IOEVENTFD: {
3676 struct kvm_ioeventfd data;
3677
3678 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003679 if (copy_from_user(&data, argp, sizeof(data)))
Gregory Haskinsd34e6b12009-07-07 17:08:49 -04003680 goto out;
3681 r = kvm_ioeventfd(kvm, &data);
3682 break;
3683 }
Jan Kiszka07975ad2012-03-29 21:14:12 +02003684#ifdef CONFIG_HAVE_KVM_MSI
3685 case KVM_SIGNAL_MSI: {
3686 struct kvm_msi msi;
3687
3688 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003689 if (copy_from_user(&msi, argp, sizeof(msi)))
Jan Kiszka07975ad2012-03-29 21:14:12 +02003690 goto out;
3691 r = kvm_send_userspace_msi(kvm, &msi);
3692 break;
3693 }
3694#endif
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003695#ifdef __KVM_HAVE_IRQ_LINE
3696 case KVM_IRQ_LINE_STATUS:
3697 case KVM_IRQ_LINE: {
3698 struct kvm_irq_level irq_event;
3699
3700 r = -EFAULT;
Xiubo Li893bdbf2015-02-26 14:58:19 +08003701 if (copy_from_user(&irq_event, argp, sizeof(irq_event)))
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003702 goto out;
3703
Yang Zhangaa2fbe62013-04-11 19:21:40 +08003704 r = kvm_vm_ioctl_irq_line(kvm, &irq_event,
3705 ioctl == KVM_IRQ_LINE_STATUS);
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003706 if (r)
3707 goto out;
3708
3709 r = -EFAULT;
3710 if (ioctl == KVM_IRQ_LINE_STATUS) {
Xiubo Li893bdbf2015-02-26 14:58:19 +08003711 if (copy_to_user(argp, &irq_event, sizeof(irq_event)))
Christoffer Dall23d43cf2012-07-24 08:51:20 -04003712 goto out;
3713 }
3714
3715 r = 0;
3716 break;
3717 }
3718#endif
Alexander Grafaa8d5942013-04-15 21:12:53 +02003719#ifdef CONFIG_HAVE_KVM_IRQ_ROUTING
3720 case KVM_SET_GSI_ROUTING: {
3721 struct kvm_irq_routing routing;
3722 struct kvm_irq_routing __user *urouting;
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003723 struct kvm_irq_routing_entry *entries = NULL;
Alexander Grafaa8d5942013-04-15 21:12:53 +02003724
3725 r = -EFAULT;
3726 if (copy_from_user(&routing, argp, sizeof(routing)))
3727 goto out;
3728 r = -EINVAL;
David Hildenbrand5c0aea02017-04-28 17:06:20 +02003729 if (!kvm_arch_can_set_irq_routing(kvm))
3730 goto out;
Xiubo Licaf1ff22016-06-15 18:00:33 +08003731 if (routing.nr > KVM_MAX_IRQ_ROUTES)
Alexander Grafaa8d5942013-04-15 21:12:53 +02003732 goto out;
3733 if (routing.flags)
3734 goto out;
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003735 if (routing.nr) {
3736 r = -ENOMEM;
Kees Cook42bc47b2018-06-12 14:27:11 -07003737 entries = vmalloc(array_size(sizeof(*entries),
3738 routing.nr));
Paolo Bonzinif8c1b852016-06-01 14:09:22 +02003739 if (!entries)
3740 goto out;
3741 r = -EFAULT;
3742 urouting = argp;
3743 if (copy_from_user(entries, urouting->entries,
3744 routing.nr * sizeof(*entries)))
3745 goto out_free_irq_routing;
3746 }
Alexander Grafaa8d5942013-04-15 21:12:53 +02003747 r = kvm_set_irq_routing(kvm, entries, routing.nr,
3748 routing.flags);
Xiubo Lia642a172015-02-26 14:58:20 +08003749out_free_irq_routing:
Alexander Grafaa8d5942013-04-15 21:12:53 +02003750 vfree(entries);
3751 break;
3752 }
3753#endif /* CONFIG_HAVE_KVM_IRQ_ROUTING */
Scott Wood852b6d52013-04-12 14:08:42 +00003754 case KVM_CREATE_DEVICE: {
3755 struct kvm_create_device cd;
3756
3757 r = -EFAULT;
3758 if (copy_from_user(&cd, argp, sizeof(cd)))
3759 goto out;
3760
3761 r = kvm_ioctl_create_device(kvm, &cd);
3762 if (r)
3763 goto out;
3764
3765 r = -EFAULT;
3766 if (copy_to_user(argp, &cd, sizeof(cd)))
3767 goto out;
3768
3769 r = 0;
3770 break;
3771 }
Alexander Graf92b591a2014-07-14 18:33:08 +02003772 case KVM_CHECK_EXTENSION:
3773 r = kvm_vm_ioctl_check_extension_generic(kvm, arg);
3774 break;
Avi Kivityf17abe92007-02-21 19:28:04 +02003775 default:
Carsten Otte1fe779f2007-10-29 16:08:35 +01003776 r = kvm_arch_vm_ioctl(filp, ioctl, arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02003777 }
3778out:
3779 return r;
3780}
3781
Christian Borntraegerde8e5d72015-02-03 09:35:15 +01003782#ifdef CONFIG_KVM_COMPAT
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003783struct compat_kvm_dirty_log {
3784 __u32 slot;
3785 __u32 padding1;
3786 union {
3787 compat_uptr_t dirty_bitmap; /* one bit per page */
3788 __u64 padding2;
3789 };
3790};
3791
3792static long kvm_vm_compat_ioctl(struct file *filp,
3793 unsigned int ioctl, unsigned long arg)
3794{
3795 struct kvm *kvm = filp->private_data;
3796 int r;
3797
3798 if (kvm->mm != current->mm)
3799 return -EIO;
3800 switch (ioctl) {
3801 case KVM_GET_DIRTY_LOG: {
3802 struct compat_kvm_dirty_log compat_log;
3803 struct kvm_dirty_log log;
3804
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003805 if (copy_from_user(&compat_log, (void __user *)arg,
3806 sizeof(compat_log)))
Markus Elfringf6a3b162017-01-22 11:30:21 +01003807 return -EFAULT;
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003808 log.slot = compat_log.slot;
3809 log.padding1 = compat_log.padding1;
3810 log.padding2 = compat_log.padding2;
3811 log.dirty_bitmap = compat_ptr(compat_log.dirty_bitmap);
3812
3813 r = kvm_vm_ioctl_get_dirty_log(kvm, &log);
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003814 break;
3815 }
3816 default:
3817 r = kvm_vm_ioctl(filp, ioctl, arg);
3818 }
Arnd Bergmann6ff58942009-10-22 14:19:27 +02003819 return r;
3820}
3821#endif
3822
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01003823static struct file_operations kvm_vm_fops = {
Avi Kivityf17abe92007-02-21 19:28:04 +02003824 .release = kvm_vm_release,
3825 .unlocked_ioctl = kvm_vm_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003826 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003827 KVM_COMPAT(kvm_vm_compat_ioctl),
Avi Kivityf17abe92007-02-21 19:28:04 +02003828};
3829
Carsten Ottee08b9632012-01-04 10:25:20 +01003830static int kvm_dev_ioctl_create_vm(unsigned long type)
Avi Kivityf17abe92007-02-21 19:28:04 +02003831{
Heiko Carstensaac87632010-10-27 17:22:10 +02003832 int r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003833 struct kvm *kvm;
Al Viro506cfba2016-07-14 18:54:17 +02003834 struct file *file;
Avi Kivityf17abe92007-02-21 19:28:04 +02003835
Carsten Ottee08b9632012-01-04 10:25:20 +01003836 kvm = kvm_create_vm(type);
Avi Kivityd6d28162007-06-28 08:38:16 -04003837 if (IS_ERR(kvm))
3838 return PTR_ERR(kvm);
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003839#ifdef CONFIG_KVM_MMIO
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09003840 r = kvm_coalesced_mmio_init(kvm);
Markus Elfring78588332017-11-21 13:40:17 +01003841 if (r < 0)
3842 goto put_kvm;
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +09003843#endif
Al Viro506cfba2016-07-14 18:54:17 +02003844 r = get_unused_fd_flags(O_CLOEXEC);
Markus Elfring78588332017-11-21 13:40:17 +01003845 if (r < 0)
3846 goto put_kvm;
3847
Al Viro506cfba2016-07-14 18:54:17 +02003848 file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
3849 if (IS_ERR(file)) {
3850 put_unused_fd(r);
Markus Elfring78588332017-11-21 13:40:17 +01003851 r = PTR_ERR(file);
3852 goto put_kvm;
Al Viro506cfba2016-07-14 18:54:17 +02003853 }
Janosch Frank536a6f82016-05-18 13:26:23 +02003854
Paolo Bonzini525df862017-06-27 15:45:09 +02003855 /*
3856 * Don't call kvm_put_kvm anymore at this point; file->f_op is
3857 * already set, with ->release() being kvm_vm_release(). In error
3858 * cases it will be called by the final fput(file) and will take
3859 * care of doing kvm_put_kvm(kvm).
3860 */
Janosch Frank536a6f82016-05-18 13:26:23 +02003861 if (kvm_create_vm_debugfs(kvm, r) < 0) {
Al Viro506cfba2016-07-14 18:54:17 +02003862 put_unused_fd(r);
3863 fput(file);
Janosch Frank536a6f82016-05-18 13:26:23 +02003864 return -ENOMEM;
3865 }
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02003866 kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
Avi Kivityf17abe92007-02-21 19:28:04 +02003867
Al Viro506cfba2016-07-14 18:54:17 +02003868 fd_install(r, file);
Heiko Carstensaac87632010-10-27 17:22:10 +02003869 return r;
Markus Elfring78588332017-11-21 13:40:17 +01003870
3871put_kvm:
3872 kvm_put_kvm(kvm);
3873 return r;
Avi Kivityf17abe92007-02-21 19:28:04 +02003874}
3875
3876static long kvm_dev_ioctl(struct file *filp,
3877 unsigned int ioctl, unsigned long arg)
3878{
Avi Kivity07c45a32007-03-07 13:05:38 +02003879 long r = -EINVAL;
Avi Kivityf17abe92007-02-21 19:28:04 +02003880
3881 switch (ioctl) {
3882 case KVM_GET_API_VERSION:
Avi Kivityf0fe5102007-03-07 13:11:17 +02003883 if (arg)
3884 goto out;
Avi Kivityf17abe92007-02-21 19:28:04 +02003885 r = KVM_API_VERSION;
3886 break;
3887 case KVM_CREATE_VM:
Carsten Ottee08b9632012-01-04 10:25:20 +01003888 r = kvm_dev_ioctl_create_vm(arg);
Avi Kivityf17abe92007-02-21 19:28:04 +02003889 break;
Zhang Xiantao018d00d2007-11-15 23:07:47 +08003890 case KVM_CHECK_EXTENSION:
Alexander Graf784aa3d2014-07-14 18:27:35 +02003891 r = kvm_vm_ioctl_check_extension_generic(NULL, arg);
Avi Kivity5d308f42007-03-01 17:56:20 +02003892 break;
Avi Kivity07c45a32007-03-07 13:05:38 +02003893 case KVM_GET_VCPU_MMAP_SIZE:
Avi Kivity07c45a32007-03-07 13:05:38 +02003894 if (arg)
3895 goto out;
Avi Kivityadb1ff42008-01-24 15:13:08 +02003896 r = PAGE_SIZE; /* struct kvm_run */
3897#ifdef CONFIG_X86
3898 r += PAGE_SIZE; /* pio data page */
3899#endif
Paolo Bonzini4b4357e2017-03-31 13:53:23 +02003900#ifdef CONFIG_KVM_MMIO
Laurent Vivier5f94c172008-05-30 16:05:54 +02003901 r += PAGE_SIZE; /* coalesced mmio ring page */
3902#endif
Avi Kivity07c45a32007-03-07 13:05:38 +02003903 break;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04003904 case KVM_TRACE_ENABLE:
3905 case KVM_TRACE_PAUSE:
3906 case KVM_TRACE_DISABLE:
Marcelo Tosatti2023a292009-06-18 11:47:28 -03003907 r = -EOPNOTSUPP;
Feng(Eric) Liud4c9ff22008-04-10 08:47:53 -04003908 break;
Avi Kivity6aa8b732006-12-10 02:21:36 -08003909 default:
Carsten Otte043405e2007-10-10 17:16:19 +02003910 return kvm_arch_dev_ioctl(filp, ioctl, arg);
Avi Kivity6aa8b732006-12-10 02:21:36 -08003911 }
3912out:
3913 return r;
3914}
3915
Avi Kivity6aa8b732006-12-10 02:21:36 -08003916static struct file_operations kvm_chardev_ops = {
Avi Kivity6aa8b732006-12-10 02:21:36 -08003917 .unlocked_ioctl = kvm_dev_ioctl,
Arnd Bergmann6038f372010-08-15 18:52:59 +02003918 .llseek = noop_llseek,
Marc Zyngier7ddfd3e2018-06-17 10:16:21 +01003919 KVM_COMPAT(kvm_dev_ioctl),
Avi Kivity6aa8b732006-12-10 02:21:36 -08003920};
3921
3922static struct miscdevice kvm_dev = {
Avi Kivitybbe44322007-03-04 13:27:36 +02003923 KVM_MINOR,
Avi Kivity6aa8b732006-12-10 02:21:36 -08003924 "kvm",
3925 &kvm_chardev_ops,
3926};
3927
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09003928static void hardware_enable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03003929{
3930 int cpu = raw_smp_processor_id();
Alexander Graf10474ae2009-09-15 11:37:46 +02003931 int r;
Avi Kivity1b6c0162007-05-24 13:03:52 +03003932
Rusty Russell7f59f492008-12-07 21:25:45 +10303933 if (cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03003934 return;
Alexander Graf10474ae2009-09-15 11:37:46 +02003935
Rusty Russell7f59f492008-12-07 21:25:45 +10303936 cpumask_set_cpu(cpu, cpus_hardware_enabled);
Alexander Graf10474ae2009-09-15 11:37:46 +02003937
Radim Krčmář13a34e02014-08-28 15:13:03 +02003938 r = kvm_arch_hardware_enable();
Alexander Graf10474ae2009-09-15 11:37:46 +02003939
3940 if (r) {
3941 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
3942 atomic_inc(&hardware_enable_failed);
Xiubo Li1170adc2015-02-26 14:58:26 +08003943 pr_info("kvm: enabling virtualization on CPU%d failed\n", cpu);
Alexander Graf10474ae2009-09-15 11:37:46 +02003944 }
Avi Kivity1b6c0162007-05-24 13:03:52 +03003945}
3946
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00003947static int kvm_starting_cpu(unsigned int cpu)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09003948{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003949 raw_spin_lock(&kvm_count_lock);
Paolo Bonzini4fa92fb2013-09-10 12:57:17 +02003950 if (kvm_usage_count)
3951 hardware_enable_nolock(NULL);
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003952 raw_spin_unlock(&kvm_count_lock);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00003953 return 0;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09003954}
3955
3956static void hardware_disable_nolock(void *junk)
Avi Kivity1b6c0162007-05-24 13:03:52 +03003957{
3958 int cpu = raw_smp_processor_id();
3959
Rusty Russell7f59f492008-12-07 21:25:45 +10303960 if (!cpumask_test_cpu(cpu, cpus_hardware_enabled))
Avi Kivity1b6c0162007-05-24 13:03:52 +03003961 return;
Rusty Russell7f59f492008-12-07 21:25:45 +10303962 cpumask_clear_cpu(cpu, cpus_hardware_enabled);
Radim Krčmář13a34e02014-08-28 15:13:03 +02003963 kvm_arch_hardware_disable();
Avi Kivity1b6c0162007-05-24 13:03:52 +03003964}
3965
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00003966static int kvm_dying_cpu(unsigned int cpu)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09003967{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003968 raw_spin_lock(&kvm_count_lock);
Paolo Bonzini4fa92fb2013-09-10 12:57:17 +02003969 if (kvm_usage_count)
3970 hardware_disable_nolock(NULL);
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003971 raw_spin_unlock(&kvm_count_lock);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00003972 return 0;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09003973}
3974
Alexander Graf10474ae2009-09-15 11:37:46 +02003975static void hardware_disable_all_nolock(void)
3976{
3977 BUG_ON(!kvm_usage_count);
3978
3979 kvm_usage_count--;
3980 if (!kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09003981 on_each_cpu(hardware_disable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02003982}
3983
3984static void hardware_disable_all(void)
3985{
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003986 raw_spin_lock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02003987 hardware_disable_all_nolock();
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003988 raw_spin_unlock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02003989}
3990
3991static int hardware_enable_all(void)
3992{
3993 int r = 0;
3994
Paolo Bonzini4a937f92013-09-10 12:58:35 +02003995 raw_spin_lock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02003996
3997 kvm_usage_count++;
3998 if (kvm_usage_count == 1) {
3999 atomic_set(&hardware_enable_failed, 0);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004000 on_each_cpu(hardware_enable_nolock, NULL, 1);
Alexander Graf10474ae2009-09-15 11:37:46 +02004001
4002 if (atomic_read(&hardware_enable_failed)) {
4003 hardware_disable_all_nolock();
4004 r = -EBUSY;
4005 }
4006 }
4007
Paolo Bonzini4a937f92013-09-10 12:58:35 +02004008 raw_spin_unlock(&kvm_count_lock);
Alexander Graf10474ae2009-09-15 11:37:46 +02004009
4010 return r;
4011}
4012
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004013static int kvm_reboot(struct notifier_block *notifier, unsigned long val,
Mike Dayd77c26f2007-10-08 09:02:08 -04004014 void *v)
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004015{
Sheng Yang8e1c1812009-04-29 11:09:04 +08004016 /*
4017 * Some (well, at least mine) BIOSes hang on reboot if
4018 * in vmx root mode.
4019 *
4020 * And Intel TXT required VMX off for all cpu when system shutdown.
4021 */
Xiubo Li1170adc2015-02-26 14:58:26 +08004022 pr_info("kvm: exiting hardware virtualization\n");
Sheng Yang8e1c1812009-04-29 11:09:04 +08004023 kvm_rebooting = true;
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004024 on_each_cpu(hardware_disable_nolock, NULL, 1);
Rusty Russell9a2b85c2007-07-17 23:17:55 +10004025 return NOTIFY_OK;
4026}
4027
4028static struct notifier_block kvm_reboot_notifier = {
4029 .notifier_call = kvm_reboot,
4030 .priority = 0,
4031};
4032
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004033static void kvm_io_bus_destroy(struct kvm_io_bus *bus)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004034{
4035 int i;
4036
4037 for (i = 0; i < bus->dev_count; i++) {
Sasha Levin743eeb02011-07-27 16:00:48 +03004038 struct kvm_io_device *pos = bus->range[i].dev;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004039
4040 kvm_iodevice_destructor(pos);
4041 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004042 kfree(bus);
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004043}
4044
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004045static inline int kvm_io_bus_cmp(const struct kvm_io_range *r1,
Xiubo Li20e87b72015-02-26 14:58:25 +08004046 const struct kvm_io_range *r2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004047{
Jason Wang8f4216c72015-09-15 14:41:57 +08004048 gpa_t addr1 = r1->addr;
4049 gpa_t addr2 = r2->addr;
4050
4051 if (addr1 < addr2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004052 return -1;
Jason Wang8f4216c72015-09-15 14:41:57 +08004053
4054 /* If r2->len == 0, match the exact address. If r2->len != 0,
4055 * accept any overlapping write. Any order is acceptable for
4056 * overlapping ranges, because kvm_io_bus_get_first_dev ensures
4057 * we process all of them.
4058 */
4059 if (r2->len) {
4060 addr1 += r1->len;
4061 addr2 += r2->len;
4062 }
4063
4064 if (addr1 > addr2)
Sasha Levin743eeb02011-07-27 16:00:48 +03004065 return 1;
Jason Wang8f4216c72015-09-15 14:41:57 +08004066
Sasha Levin743eeb02011-07-27 16:00:48 +03004067 return 0;
4068}
4069
Paolo Bonzinia343c9b2013-07-16 13:03:29 +02004070static int kvm_io_bus_sort_cmp(const void *p1, const void *p2)
4071{
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004072 return kvm_io_bus_cmp(p1, p2);
Paolo Bonzinia343c9b2013-07-16 13:03:29 +02004073}
4074
Geoff Levand39369f72013-04-05 19:20:30 +00004075static int kvm_io_bus_get_first_dev(struct kvm_io_bus *bus,
Sasha Levin743eeb02011-07-27 16:00:48 +03004076 gpa_t addr, int len)
4077{
4078 struct kvm_io_range *range, key;
4079 int off;
4080
4081 key = (struct kvm_io_range) {
4082 .addr = addr,
4083 .len = len,
4084 };
4085
4086 range = bsearch(&key, bus->range, bus->dev_count,
4087 sizeof(struct kvm_io_range), kvm_io_bus_sort_cmp);
4088 if (range == NULL)
4089 return -ENOENT;
4090
4091 off = range - bus->range;
4092
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004093 while (off > 0 && kvm_io_bus_cmp(&key, &bus->range[off-1]) == 0)
Sasha Levin743eeb02011-07-27 16:00:48 +03004094 off--;
4095
4096 return off;
4097}
4098
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004099static int __kvm_io_bus_write(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004100 struct kvm_io_range *range, const void *val)
4101{
4102 int idx;
4103
4104 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
4105 if (idx < 0)
4106 return -EOPNOTSUPP;
4107
4108 while (idx < bus->dev_count &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004109 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004110 if (!kvm_iodevice_write(vcpu, bus->range[idx].dev, range->addr,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004111 range->len, val))
4112 return idx;
4113 idx++;
4114 }
4115
4116 return -EOPNOTSUPP;
4117}
4118
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004119/* kvm_io_bus_write - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004120int kvm_io_bus_write(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004121 int len, const void *val)
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004122{
Cornelia Huck126a5af2013-07-03 16:30:53 +02004123 struct kvm_io_bus *bus;
4124 struct kvm_io_range range;
4125 int r;
4126
4127 range = (struct kvm_io_range) {
4128 .addr = addr,
4129 .len = len,
4130 };
4131
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004132 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004133 if (!bus)
4134 return -ENOMEM;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004135 r = __kvm_io_bus_write(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004136 return r < 0 ? r : 0;
4137}
Leo Yana2420102019-02-22 16:10:09 +08004138EXPORT_SYMBOL_GPL(kvm_io_bus_write);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004139
4140/* kvm_io_bus_write_cookie - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004141int kvm_io_bus_write_cookie(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx,
4142 gpa_t addr, int len, const void *val, long cookie)
Cornelia Huck126a5af2013-07-03 16:30:53 +02004143{
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004144 struct kvm_io_bus *bus;
Sasha Levin743eeb02011-07-27 16:00:48 +03004145 struct kvm_io_range range;
4146
4147 range = (struct kvm_io_range) {
4148 .addr = addr,
4149 .len = len,
4150 };
Lai Jiangshan90d83dc2010-04-19 17:41:23 +08004151
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004152 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004153 if (!bus)
4154 return -ENOMEM;
Cornelia Huck126a5af2013-07-03 16:30:53 +02004155
4156 /* First try the device referenced by cookie. */
4157 if ((cookie >= 0) && (cookie < bus->dev_count) &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004158 (kvm_io_bus_cmp(&range, &bus->range[cookie]) == 0))
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004159 if (!kvm_iodevice_write(vcpu, bus->range[cookie].dev, addr, len,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004160 val))
4161 return cookie;
4162
4163 /*
4164 * cookie contained garbage; fall back to search and return the
4165 * correct cookie value.
4166 */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004167 return __kvm_io_bus_write(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004168}
4169
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004170static int __kvm_io_bus_read(struct kvm_vcpu *vcpu, struct kvm_io_bus *bus,
4171 struct kvm_io_range *range, void *val)
Cornelia Huck126a5af2013-07-03 16:30:53 +02004172{
4173 int idx;
4174
4175 idx = kvm_io_bus_get_first_dev(bus, range->addr, range->len);
Sasha Levin743eeb02011-07-27 16:00:48 +03004176 if (idx < 0)
4177 return -EOPNOTSUPP;
4178
4179 while (idx < bus->dev_count &&
Paolo Bonzinic21fbff2013-08-27 15:41:41 +02004180 kvm_io_bus_cmp(range, &bus->range[idx]) == 0) {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004181 if (!kvm_iodevice_read(vcpu, bus->range[idx].dev, range->addr,
Cornelia Huck126a5af2013-07-03 16:30:53 +02004182 range->len, val))
4183 return idx;
Sasha Levin743eeb02011-07-27 16:00:48 +03004184 idx++;
4185 }
4186
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004187 return -EOPNOTSUPP;
4188}
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004189
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004190/* kvm_io_bus_read - called under kvm->slots_lock */
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004191int kvm_io_bus_read(struct kvm_vcpu *vcpu, enum kvm_bus bus_idx, gpa_t addr,
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004192 int len, void *val)
Michael S. Tsirkinbda90202009-06-29 22:24:32 +03004193{
Cornelia Huck126a5af2013-07-03 16:30:53 +02004194 struct kvm_io_bus *bus;
4195 struct kvm_io_range range;
4196 int r;
4197
4198 range = (struct kvm_io_range) {
4199 .addr = addr,
4200 .len = len,
4201 };
4202
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004203 bus = srcu_dereference(vcpu->kvm->buses[bus_idx], &vcpu->kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004204 if (!bus)
4205 return -ENOMEM;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +00004206 r = __kvm_io_bus_read(vcpu, bus, &range, val);
Cornelia Huck126a5af2013-07-03 16:30:53 +02004207 return r < 0 ? r : 0;
4208}
4209
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004210/* Caller must hold slots_lock. */
Sasha Levin743eeb02011-07-27 16:00:48 +03004211int kvm_io_bus_register_dev(struct kvm *kvm, enum kvm_bus bus_idx, gpa_t addr,
4212 int len, struct kvm_io_device *dev)
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03004213{
Gal Hammerd4c67a72018-01-16 15:34:41 +02004214 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004215 struct kvm_io_bus *new_bus, *bus;
Gal Hammerd4c67a72018-01-16 15:34:41 +02004216 struct kvm_io_range range;
Gregory Haskins090b7af2009-07-07 17:08:44 -04004217
Christian Borntraeger4a12f952017-07-07 10:51:38 +02004218 bus = kvm_get_bus(kvm, bus_idx);
David Hildenbrand90db1042017-03-23 18:24:19 +01004219 if (!bus)
4220 return -ENOMEM;
4221
Amos Kong6ea34c92013-05-25 06:44:15 +08004222 /* exclude ioeventfd which is limited by maximum fd */
4223 if (bus->dev_count - bus->ioeventfd_count > NR_IOBUS_DEVS - 1)
Gregory Haskins090b7af2009-07-07 17:08:44 -04004224 return -ENOSPC;
4225
Gustavo A. R. Silva90952cd2019-01-30 17:07:47 +01004226 new_bus = kmalloc(struct_size(bus, range, bus->dev_count + 1),
Ben Gardonb12ce362019-02-11 11:02:49 -08004227 GFP_KERNEL_ACCOUNT);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004228 if (!new_bus)
4229 return -ENOMEM;
Gal Hammerd4c67a72018-01-16 15:34:41 +02004230
4231 range = (struct kvm_io_range) {
4232 .addr = addr,
4233 .len = len,
4234 .dev = dev,
4235 };
4236
4237 for (i = 0; i < bus->dev_count; i++)
4238 if (kvm_io_bus_cmp(&bus->range[i], &range) > 0)
4239 break;
4240
4241 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
4242 new_bus->dev_count++;
4243 new_bus->range[i] = range;
4244 memcpy(new_bus->range + i + 1, bus->range + i,
4245 (bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004246 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
4247 synchronize_srcu_expedited(&kvm->srcu);
4248 kfree(bus);
Gregory Haskins090b7af2009-07-07 17:08:44 -04004249
4250 return 0;
4251}
4252
Marcelo Tosatti79fac952009-12-23 14:35:26 -02004253/* Caller must hold slots_lock. */
David Hildenbrand90db1042017-03-23 18:24:19 +01004254void kvm_io_bus_unregister_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4255 struct kvm_io_device *dev)
Gregory Haskins090b7af2009-07-07 17:08:44 -04004256{
David Hildenbrand90db1042017-03-23 18:24:19 +01004257 int i;
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004258 struct kvm_io_bus *new_bus, *bus;
Michael S. Tsirkin6c474692009-06-29 22:24:26 +03004259
Christian Borntraeger4a12f952017-07-07 10:51:38 +02004260 bus = kvm_get_bus(kvm, bus_idx);
Peter Xudf630b82017-03-15 16:01:17 +08004261 if (!bus)
David Hildenbrand90db1042017-03-23 18:24:19 +01004262 return;
Peter Xudf630b82017-03-15 16:01:17 +08004263
Amos Konga13007162012-03-09 12:17:32 +08004264 for (i = 0; i < bus->dev_count; i++)
4265 if (bus->range[i].dev == dev) {
Gregory Haskins090b7af2009-07-07 17:08:44 -04004266 break;
4267 }
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004268
David Hildenbrand90db1042017-03-23 18:24:19 +01004269 if (i == bus->dev_count)
4270 return;
Amos Konga13007162012-03-09 12:17:32 +08004271
Gustavo A. R. Silva90952cd2019-01-30 17:07:47 +01004272 new_bus = kmalloc(struct_size(bus, range, bus->dev_count - 1),
Ben Gardonb12ce362019-02-11 11:02:49 -08004273 GFP_KERNEL_ACCOUNT);
David Hildenbrand90db1042017-03-23 18:24:19 +01004274 if (!new_bus) {
4275 pr_err("kvm: failed to shrink bus, removing it completely\n");
4276 goto broken;
4277 }
Amos Konga13007162012-03-09 12:17:32 +08004278
4279 memcpy(new_bus, bus, sizeof(*bus) + i * sizeof(struct kvm_io_range));
4280 new_bus->dev_count--;
4281 memcpy(new_bus->range + i, bus->range + i + 1,
4282 (new_bus->dev_count - i) * sizeof(struct kvm_io_range));
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004283
David Hildenbrand90db1042017-03-23 18:24:19 +01004284broken:
Marcelo Tosattie93f8a02009-12-23 14:35:24 -02004285 rcu_assign_pointer(kvm->buses[bus_idx], new_bus);
4286 synchronize_srcu_expedited(&kvm->srcu);
4287 kfree(bus);
David Hildenbrand90db1042017-03-23 18:24:19 +01004288 return;
Gregory Haskins2eeb2e92007-05-31 14:08:53 -04004289}
4290
Andre Przywara8a39d002016-07-15 12:43:26 +01004291struct kvm_io_device *kvm_io_bus_get_dev(struct kvm *kvm, enum kvm_bus bus_idx,
4292 gpa_t addr)
4293{
4294 struct kvm_io_bus *bus;
4295 int dev_idx, srcu_idx;
4296 struct kvm_io_device *iodev = NULL;
4297
4298 srcu_idx = srcu_read_lock(&kvm->srcu);
4299
4300 bus = srcu_dereference(kvm->buses[bus_idx], &kvm->srcu);
David Hildenbrand90db1042017-03-23 18:24:19 +01004301 if (!bus)
4302 goto out_unlock;
Andre Przywara8a39d002016-07-15 12:43:26 +01004303
4304 dev_idx = kvm_io_bus_get_first_dev(bus, addr, 1);
4305 if (dev_idx < 0)
4306 goto out_unlock;
4307
4308 iodev = bus->range[dev_idx].dev;
4309
4310out_unlock:
4311 srcu_read_unlock(&kvm->srcu, srcu_idx);
4312
4313 return iodev;
4314}
4315EXPORT_SYMBOL_GPL(kvm_io_bus_get_dev);
4316
Janosch Frank536a6f82016-05-18 13:26:23 +02004317static int kvm_debugfs_open(struct inode *inode, struct file *file,
4318 int (*get)(void *, u64 *), int (*set)(void *, u64),
4319 const char *fmt)
4320{
4321 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
4322 inode->i_private;
4323
4324 /* The debugfs files are a reference to the kvm struct which
4325 * is still valid when kvm_destroy_vm is called.
4326 * To avoid the race between open and the removal of the debugfs
4327 * directory we test against the users count.
4328 */
Elena Reshetovae3736c32017-02-20 13:06:21 +02004329 if (!refcount_inc_not_zero(&stat_data->kvm->users_count))
Janosch Frank536a6f82016-05-18 13:26:23 +02004330 return -ENOENT;
4331
Paolo Bonzini833b45d2019-09-30 18:48:44 +02004332 if (simple_attr_open(inode, file, get,
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004333 KVM_DBGFS_GET_MODE(stat_data->dbgfs_item) & 0222
4334 ? set : NULL,
4335 fmt)) {
Janosch Frank536a6f82016-05-18 13:26:23 +02004336 kvm_put_kvm(stat_data->kvm);
4337 return -ENOMEM;
4338 }
4339
4340 return 0;
4341}
4342
4343static int kvm_debugfs_release(struct inode *inode, struct file *file)
4344{
4345 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)
4346 inode->i_private;
4347
4348 simple_attr_release(inode, file);
4349 kvm_put_kvm(stat_data->kvm);
4350
4351 return 0;
4352}
4353
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004354static int kvm_get_stat_per_vm(struct kvm *kvm, size_t offset, u64 *val)
Janosch Frank536a6f82016-05-18 13:26:23 +02004355{
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004356 *val = *(ulong *)((void *)kvm + offset);
Janosch Frank536a6f82016-05-18 13:26:23 +02004357
4358 return 0;
4359}
4360
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004361static int kvm_clear_stat_per_vm(struct kvm *kvm, size_t offset)
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004362{
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004363 *(ulong *)((void *)kvm + offset) = 0;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004364
4365 return 0;
4366}
4367
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004368static int kvm_get_stat_per_vcpu(struct kvm *kvm, size_t offset, u64 *val)
Janosch Frank536a6f82016-05-18 13:26:23 +02004369{
4370 int i;
Janosch Frank536a6f82016-05-18 13:26:23 +02004371 struct kvm_vcpu *vcpu;
4372
4373 *val = 0;
4374
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004375 kvm_for_each_vcpu(i, vcpu, kvm)
4376 *val += *(u64 *)((void *)vcpu + offset);
Janosch Frank536a6f82016-05-18 13:26:23 +02004377
4378 return 0;
4379}
4380
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004381static int kvm_clear_stat_per_vcpu(struct kvm *kvm, size_t offset)
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004382{
4383 int i;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004384 struct kvm_vcpu *vcpu;
4385
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004386 kvm_for_each_vcpu(i, vcpu, kvm)
4387 *(u64 *)((void *)vcpu + offset) = 0;
4388
4389 return 0;
4390}
4391
4392static int kvm_stat_data_get(void *data, u64 *val)
4393{
4394 int r = -EFAULT;
4395 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4396
4397 switch (stat_data->dbgfs_item->kind) {
4398 case KVM_STAT_VM:
4399 r = kvm_get_stat_per_vm(stat_data->kvm,
4400 stat_data->dbgfs_item->offset, val);
4401 break;
4402 case KVM_STAT_VCPU:
4403 r = kvm_get_stat_per_vcpu(stat_data->kvm,
4404 stat_data->dbgfs_item->offset, val);
4405 break;
4406 }
4407
4408 return r;
4409}
4410
4411static int kvm_stat_data_clear(void *data, u64 val)
4412{
4413 int r = -EFAULT;
4414 struct kvm_stat_data *stat_data = (struct kvm_stat_data *)data;
4415
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004416 if (val)
4417 return -EINVAL;
4418
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004419 switch (stat_data->dbgfs_item->kind) {
4420 case KVM_STAT_VM:
4421 r = kvm_clear_stat_per_vm(stat_data->kvm,
4422 stat_data->dbgfs_item->offset);
4423 break;
4424 case KVM_STAT_VCPU:
4425 r = kvm_clear_stat_per_vcpu(stat_data->kvm,
4426 stat_data->dbgfs_item->offset);
4427 break;
4428 }
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004429
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004430 return r;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004431}
4432
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004433static int kvm_stat_data_open(struct inode *inode, struct file *file)
Janosch Frank536a6f82016-05-18 13:26:23 +02004434{
4435 __simple_attr_check_format("%llu\n", 0ull);
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004436 return kvm_debugfs_open(inode, file, kvm_stat_data_get,
4437 kvm_stat_data_clear, "%llu\n");
Janosch Frank536a6f82016-05-18 13:26:23 +02004438}
4439
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004440static const struct file_operations stat_fops_per_vm = {
4441 .owner = THIS_MODULE,
4442 .open = kvm_stat_data_open,
Janosch Frank536a6f82016-05-18 13:26:23 +02004443 .release = kvm_debugfs_release,
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004444 .read = simple_attr_read,
4445 .write = simple_attr_write,
4446 .llseek = no_llseek,
Janosch Frank536a6f82016-05-18 13:26:23 +02004447};
4448
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004449static int vm_stat_get(void *_offset, u64 *val)
Avi Kivityba1389b2007-11-18 16:24:12 +02004450{
4451 unsigned offset = (long)_offset;
Avi Kivityba1389b2007-11-18 16:24:12 +02004452 struct kvm *kvm;
Janosch Frank536a6f82016-05-18 13:26:23 +02004453 u64 tmp_val;
Avi Kivityba1389b2007-11-18 16:24:12 +02004454
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004455 *val = 0;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004456 mutex_lock(&kvm_lock);
Janosch Frank536a6f82016-05-18 13:26:23 +02004457 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004458 kvm_get_stat_per_vm(kvm, offset, &tmp_val);
Janosch Frank536a6f82016-05-18 13:26:23 +02004459 *val += tmp_val;
4460 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004461 mutex_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004462 return 0;
Avi Kivityba1389b2007-11-18 16:24:12 +02004463}
4464
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004465static int vm_stat_clear(void *_offset, u64 val)
4466{
4467 unsigned offset = (long)_offset;
4468 struct kvm *kvm;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004469
4470 if (val)
4471 return -EINVAL;
4472
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004473 mutex_lock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004474 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004475 kvm_clear_stat_per_vm(kvm, offset);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004476 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004477 mutex_unlock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004478
4479 return 0;
4480}
4481
4482DEFINE_SIMPLE_ATTRIBUTE(vm_stat_fops, vm_stat_get, vm_stat_clear, "%llu\n");
Avi Kivityba1389b2007-11-18 16:24:12 +02004483
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004484static int vcpu_stat_get(void *_offset, u64 *val)
Avi Kivity1165f5f2007-04-19 17:27:43 +03004485{
4486 unsigned offset = (long)_offset;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004487 struct kvm *kvm;
Janosch Frank536a6f82016-05-18 13:26:23 +02004488 u64 tmp_val;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004489
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004490 *val = 0;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004491 mutex_lock(&kvm_lock);
Janosch Frank536a6f82016-05-18 13:26:23 +02004492 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004493 kvm_get_stat_per_vcpu(kvm, offset, &tmp_val);
Janosch Frank536a6f82016-05-18 13:26:23 +02004494 *val += tmp_val;
4495 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004496 mutex_unlock(&kvm_lock);
Christoph Hellwig8b88b092008-02-08 04:20:26 -08004497 return 0;
Avi Kivity1165f5f2007-04-19 17:27:43 +03004498}
4499
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004500static int vcpu_stat_clear(void *_offset, u64 val)
4501{
4502 unsigned offset = (long)_offset;
4503 struct kvm *kvm;
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004504
4505 if (val)
4506 return -EINVAL;
4507
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004508 mutex_lock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004509 list_for_each_entry(kvm, &vm_list, vm_list) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004510 kvm_clear_stat_per_vcpu(kvm, offset);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004511 }
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004512 mutex_unlock(&kvm_lock);
Suraj Jitindar Singhce35ef22016-10-19 13:49:47 +11004513
4514 return 0;
4515}
4516
4517DEFINE_SIMPLE_ATTRIBUTE(vcpu_stat_fops, vcpu_stat_get, vcpu_stat_clear,
4518 "%llu\n");
Avi Kivityba1389b2007-11-18 16:24:12 +02004519
Alexey Dobriyan828c0952009-10-01 15:43:56 -07004520static const struct file_operations *stat_fops[] = {
Avi Kivityba1389b2007-11-18 16:24:12 +02004521 [KVM_STAT_VCPU] = &vcpu_stat_fops,
4522 [KVM_STAT_VM] = &vm_stat_fops,
4523};
Avi Kivity1165f5f2007-04-19 17:27:43 +03004524
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004525static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
4526{
4527 struct kobj_uevent_env *env;
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004528 unsigned long long created, active;
4529
4530 if (!kvm_dev.this_device || !kvm)
4531 return;
4532
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004533 mutex_lock(&kvm_lock);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004534 if (type == KVM_EVENT_CREATE_VM) {
4535 kvm_createvm_count++;
4536 kvm_active_vms++;
4537 } else if (type == KVM_EVENT_DESTROY_VM) {
4538 kvm_active_vms--;
4539 }
4540 created = kvm_createvm_count;
4541 active = kvm_active_vms;
Junaid Shahid0d9ce162019-01-03 17:14:28 -08004542 mutex_unlock(&kvm_lock);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004543
Ben Gardonb12ce362019-02-11 11:02:49 -08004544 env = kzalloc(sizeof(*env), GFP_KERNEL_ACCOUNT);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004545 if (!env)
4546 return;
4547
4548 add_uevent_var(env, "CREATED=%llu", created);
4549 add_uevent_var(env, "COUNT=%llu", active);
4550
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004551 if (type == KVM_EVENT_CREATE_VM) {
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004552 add_uevent_var(env, "EVENT=create");
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004553 kvm->userspace_pid = task_pid_nr(current);
4554 } else if (type == KVM_EVENT_DESTROY_VM) {
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004555 add_uevent_var(env, "EVENT=destroy");
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004556 }
4557 add_uevent_var(env, "PID=%d", kvm->userspace_pid);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004558
Greg Kroah-Hartman8ed05792019-02-28 16:34:37 +01004559 if (!IS_ERR_OR_NULL(kvm->debugfs_dentry)) {
Ben Gardonb12ce362019-02-11 11:02:49 -08004560 char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL_ACCOUNT);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004561
Claudio Imbrendafdeaf7e2017-07-24 13:40:03 +02004562 if (p) {
4563 tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX);
4564 if (!IS_ERR(tmp))
4565 add_uevent_var(env, "STATS_PATH=%s", tmp);
4566 kfree(p);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004567 }
4568 }
4569 /* no need for checks, since we are adding at most only 5 keys */
4570 env->envp[env->envp_idx++] = NULL;
4571 kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
4572 kfree(env);
Claudio Imbrenda286de8f2017-07-12 17:56:44 +02004573}
4574
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004575static void kvm_init_debug(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004576{
4577 struct kvm_stats_debugfs_item *p;
4578
Hollis Blanchard76f7c872008-04-15 16:05:42 -05004579 kvm_debugfs_dir = debugfs_create_dir("kvm", NULL);
Hamo4f69b682011-12-15 14:23:16 +08004580
Janosch Frank536a6f82016-05-18 13:26:23 +02004581 kvm_debugfs_num_entries = 0;
4582 for (p = debugfs_entries; p->name; ++p, kvm_debugfs_num_entries++) {
Milan Pandurov09cbcef2019-12-13 14:07:21 +01004583 debugfs_create_file(p->name, KVM_DBGFS_GET_MODE(p),
4584 kvm_debugfs_dir, (void *)(long)p->offset,
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004585 stat_fops[p->kind]);
Hamo4f69b682011-12-15 14:23:16 +08004586 }
Avi Kivity6aa8b732006-12-10 02:21:36 -08004587}
4588
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004589static int kvm_suspend(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08004590{
Alexander Graf10474ae2009-09-15 11:37:46 +02004591 if (kvm_usage_count)
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004592 hardware_disable_nolock(NULL);
Avi Kivity59ae6c62007-02-12 00:54:48 -08004593 return 0;
4594}
4595
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004596static void kvm_resume(void)
Avi Kivity59ae6c62007-02-12 00:54:48 -08004597{
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10004598 if (kvm_usage_count) {
Wanpeng Li2eb06c32019-05-17 16:49:49 +08004599#ifdef CONFIG_LOCKDEP
4600 WARN_ON(lockdep_is_held(&kvm_count_lock));
4601#endif
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004602 hardware_enable_nolock(NULL);
Zachary Amsdenca84d1a2010-08-19 22:07:28 -10004603 }
Avi Kivity59ae6c62007-02-12 00:54:48 -08004604}
4605
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004606static struct syscore_ops kvm_syscore_ops = {
Avi Kivity59ae6c62007-02-12 00:54:48 -08004607 .suspend = kvm_suspend,
4608 .resume = kvm_resume,
4609};
4610
Avi Kivity15ad7142007-07-11 18:17:21 +03004611static inline
4612struct kvm_vcpu *preempt_notifier_to_vcpu(struct preempt_notifier *pn)
4613{
4614 return container_of(pn, struct kvm_vcpu, preempt_notifier);
4615}
4616
4617static void kvm_sched_in(struct preempt_notifier *pn, int cpu)
4618{
4619 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
Xiubo Lif95ef0cd2015-02-26 14:58:23 +08004620
Wanpeng Li046ddee2019-08-01 11:30:14 +08004621 WRITE_ONCE(vcpu->preempted, false);
Wanpeng Lid73eb572019-07-18 19:39:06 +08004622 WRITE_ONCE(vcpu->ready, false);
Avi Kivity15ad7142007-07-11 18:17:21 +03004623
Paolo Bonzini7495e222020-01-09 09:57:19 -05004624 __this_cpu_write(kvm_running_vcpu, vcpu);
Radim Krčmáře790d9e2014-08-21 18:08:05 +02004625 kvm_arch_sched_in(vcpu, cpu);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004626 kvm_arch_vcpu_load(vcpu, cpu);
Avi Kivity15ad7142007-07-11 18:17:21 +03004627}
4628
4629static void kvm_sched_out(struct preempt_notifier *pn,
4630 struct task_struct *next)
4631{
4632 struct kvm_vcpu *vcpu = preempt_notifier_to_vcpu(pn);
4633
Wanpeng Lid73eb572019-07-18 19:39:06 +08004634 if (current->state == TASK_RUNNING) {
Wanpeng Li046ddee2019-08-01 11:30:14 +08004635 WRITE_ONCE(vcpu->preempted, true);
Wanpeng Lid73eb572019-07-18 19:39:06 +08004636 WRITE_ONCE(vcpu->ready, true);
4637 }
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004638 kvm_arch_vcpu_put(vcpu);
Paolo Bonzini7495e222020-01-09 09:57:19 -05004639 __this_cpu_write(kvm_running_vcpu, NULL);
4640}
4641
4642/**
4643 * kvm_get_running_vcpu - get the vcpu running on the current CPU.
Marc Zyngier1f03b2b2020-02-07 16:34:10 +00004644 *
4645 * We can disable preemption locally around accessing the per-CPU variable,
4646 * and use the resolved vcpu pointer after enabling preemption again,
4647 * because even if the current thread is migrated to another CPU, reading
4648 * the per-CPU value later will give us the same value as we update the
4649 * per-CPU variable in the preempt notifier handlers.
Paolo Bonzini7495e222020-01-09 09:57:19 -05004650 */
4651struct kvm_vcpu *kvm_get_running_vcpu(void)
4652{
Marc Zyngier1f03b2b2020-02-07 16:34:10 +00004653 struct kvm_vcpu *vcpu;
4654
4655 preempt_disable();
4656 vcpu = __this_cpu_read(kvm_running_vcpu);
4657 preempt_enable();
4658
4659 return vcpu;
Paolo Bonzini7495e222020-01-09 09:57:19 -05004660}
Wanpeng Li379a3c82020-04-28 14:23:27 +08004661EXPORT_SYMBOL_GPL(kvm_get_running_vcpu);
Paolo Bonzini7495e222020-01-09 09:57:19 -05004662
4663/**
4664 * kvm_get_running_vcpus - get the per-CPU array of currently running vcpus.
4665 */
4666struct kvm_vcpu * __percpu *kvm_get_running_vcpus(void)
4667{
4668 return &kvm_running_vcpu;
Avi Kivity15ad7142007-07-11 18:17:21 +03004669}
4670
Sean Christophersonb9904082020-03-21 13:25:55 -07004671struct kvm_cpu_compat_check {
4672 void *opaque;
4673 int *ret;
4674};
4675
4676static void check_processor_compat(void *data)
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004677{
Sean Christophersonb9904082020-03-21 13:25:55 -07004678 struct kvm_cpu_compat_check *c = data;
4679
4680 *c->ret = kvm_arch_check_processor_compat(c->opaque);
Sean Christophersonf257d6d2019-04-19 22:18:17 -07004681}
4682
Avi Kivity0ee75be2010-04-28 15:39:01 +03004683int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
Rusty Russellc16f8622007-07-30 21:12:19 +10004684 struct module *module)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004685{
Sean Christophersonb9904082020-03-21 13:25:55 -07004686 struct kvm_cpu_compat_check c;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004687 int r;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004688 int cpu;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004689
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004690 r = kvm_arch_init(opaque);
4691 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08004692 goto out_fail;
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004693
Asias He7dac16c2013-05-08 10:57:29 +08004694 /*
4695 * kvm_arch_init makes sure there's at most one caller
4696 * for architectures that support multiple implementations,
4697 * like intel and amd on x86.
Paolo Bonzini36343f62016-10-26 13:35:56 +02004698 * kvm_arch_init must be called before kvm_irqfd_init to avoid creating
4699 * conflicts in case kvm is already setup for another implementation.
Asias He7dac16c2013-05-08 10:57:29 +08004700 */
Paolo Bonzini36343f62016-10-26 13:35:56 +02004701 r = kvm_irqfd_init();
4702 if (r)
4703 goto out_irqfd;
Asias He7dac16c2013-05-08 10:57:29 +08004704
Avi Kivity8437a6172009-06-06 14:52:35 -07004705 if (!zalloc_cpumask_var(&cpus_hardware_enabled, GFP_KERNEL)) {
Rusty Russell7f59f492008-12-07 21:25:45 +10304706 r = -ENOMEM;
4707 goto out_free_0;
4708 }
4709
Sean Christophersonb9904082020-03-21 13:25:55 -07004710 r = kvm_arch_hardware_setup(opaque);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004711 if (r < 0)
Miaohe Linfaf0be22019-11-23 10:45:50 +08004712 goto out_free_1;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004713
Sean Christophersonb9904082020-03-21 13:25:55 -07004714 c.ret = &r;
4715 c.opaque = opaque;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004716 for_each_online_cpu(cpu) {
Sean Christophersonb9904082020-03-21 13:25:55 -07004717 smp_call_function_single(cpu, check_processor_compat, &c, 1);
Yang, Sheng002c7f72007-07-31 14:23:01 +03004718 if (r < 0)
Miaohe Linfaf0be22019-11-23 10:45:50 +08004719 goto out_free_2;
Yang, Sheng002c7f72007-07-31 14:23:01 +03004720 }
4721
Thomas Gleixner73c1b412016-12-21 20:19:54 +01004722 r = cpuhp_setup_state_nocalls(CPUHP_AP_KVM_STARTING, "kvm/cpu:starting",
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004723 kvm_starting_cpu, kvm_dying_cpu);
Avi Kivity774c47f2007-02-12 00:54:47 -08004724 if (r)
Zhang Xiantaod23087842007-11-29 15:35:39 +08004725 goto out_free_2;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004726 register_reboot_notifier(&kvm_reboot_notifier);
4727
Rusty Russellc16f8622007-07-30 21:12:19 +10004728 /* A kmem cache lets us meet the alignment requirements of fx_save. */
Avi Kivity0ee75be2010-04-28 15:39:01 +03004729 if (!vcpu_align)
4730 vcpu_align = __alignof__(struct kvm_vcpu);
Paolo Bonzini46515732017-10-26 15:45:46 +02004731 kvm_vcpu_cache =
4732 kmem_cache_create_usercopy("kvm_vcpu", vcpu_size, vcpu_align,
4733 SLAB_ACCOUNT,
4734 offsetof(struct kvm_vcpu, arch),
4735 sizeof_field(struct kvm_vcpu, arch),
4736 NULL);
Rusty Russellc16f8622007-07-30 21:12:19 +10004737 if (!kvm_vcpu_cache) {
4738 r = -ENOMEM;
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004739 goto out_free_3;
Rusty Russellc16f8622007-07-30 21:12:19 +10004740 }
4741
Gleb Natapovaf585b92010-10-14 11:22:46 +02004742 r = kvm_async_pf_init();
4743 if (r)
4744 goto out_free;
4745
Avi Kivity6aa8b732006-12-10 02:21:36 -08004746 kvm_chardev_ops.owner = module;
Christian Borntraeger3d3aab12008-12-02 11:17:32 +01004747 kvm_vm_fops.owner = module;
4748 kvm_vcpu_fops.owner = module;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004749
4750 r = misc_register(&kvm_dev);
4751 if (r) {
Xiubo Li1170adc2015-02-26 14:58:26 +08004752 pr_err("kvm: misc device register failed\n");
Gleb Natapovaf585b92010-10-14 11:22:46 +02004753 goto out_unreg;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004754 }
4755
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004756 register_syscore_ops(&kvm_syscore_ops);
4757
Avi Kivity15ad7142007-07-11 18:17:21 +03004758 kvm_preempt_ops.sched_in = kvm_sched_in;
4759 kvm_preempt_ops.sched_out = kvm_sched_out;
4760
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02004761 kvm_init_debug();
Darrick J. Wong0ea4ed82009-10-14 16:21:00 -07004762
Paolo Bonzini3c3c29f2014-09-24 13:02:46 +02004763 r = kvm_vfio_ops_init();
4764 WARN_ON(r);
4765
Avi Kivityc7addb92007-09-16 18:58:32 +02004766 return 0;
Avi Kivity6aa8b732006-12-10 02:21:36 -08004767
Gleb Natapovaf585b92010-10-14 11:22:46 +02004768out_unreg:
4769 kvm_async_pf_deinit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004770out_free:
Rusty Russellc16f8622007-07-30 21:12:19 +10004771 kmem_cache_destroy(kvm_vcpu_cache);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004772out_free_3:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004773 unregister_reboot_notifier(&kvm_reboot_notifier);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004774 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004775out_free_2:
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004776 kvm_arch_hardware_unsetup();
Miaohe Linfaf0be22019-11-23 10:45:50 +08004777out_free_1:
Rusty Russell7f59f492008-12-07 21:25:45 +10304778 free_cpumask_var(cpus_hardware_enabled);
Zhang Xiantaod23087842007-11-29 15:35:39 +08004779out_free_0:
Cornelia Hucka0f155e2013-02-28 12:33:18 +01004780 kvm_irqfd_exit();
Paolo Bonzini36343f62016-10-26 13:35:56 +02004781out_irqfd:
Asias He7dac16c2013-05-08 10:57:29 +08004782 kvm_arch_exit();
4783out_fail:
Avi Kivity6aa8b732006-12-10 02:21:36 -08004784 return r;
4785}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004786EXPORT_SYMBOL_GPL(kvm_init);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004787
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004788void kvm_exit(void)
Avi Kivity6aa8b732006-12-10 02:21:36 -08004789{
Janosch Frank4bd33b52015-10-14 12:37:35 +02004790 debugfs_remove_recursive(kvm_debugfs_dir);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004791 misc_deregister(&kvm_dev);
Rusty Russellc16f8622007-07-30 21:12:19 +10004792 kmem_cache_destroy(kvm_vcpu_cache);
Gleb Natapovaf585b92010-10-14 11:22:46 +02004793 kvm_async_pf_deinit();
Rafael J. Wysockifb3600c2011-03-23 22:16:23 +01004794 unregister_syscore_ops(&kvm_syscore_ops);
Avi Kivity6aa8b732006-12-10 02:21:36 -08004795 unregister_reboot_notifier(&kvm_reboot_notifier);
Thomas Gleixner8c18b2d2016-07-13 17:16:37 +00004796 cpuhp_remove_state_nocalls(CPUHP_AP_KVM_STARTING);
Takuya Yoshikawa75b71272010-11-16 17:37:41 +09004797 on_each_cpu(hardware_disable_nolock, NULL, 1);
Zhang Xiantaoe9b11c12007-11-14 20:38:21 +08004798 kvm_arch_hardware_unsetup();
Zhang Xiantaof8c16bb2007-11-14 20:40:21 +08004799 kvm_arch_exit();
Cornelia Hucka0f155e2013-02-28 12:33:18 +01004800 kvm_irqfd_exit();
Rusty Russell7f59f492008-12-07 21:25:45 +10304801 free_cpumask_var(cpus_hardware_enabled);
Wanpeng Li571ee1b2014-10-09 18:30:08 +08004802 kvm_vfio_ops_exit();
Avi Kivity6aa8b732006-12-10 02:21:36 -08004803}
Zhang Xiantaocb498ea2007-11-14 20:39:31 +08004804EXPORT_SYMBOL_GPL(kvm_exit);
Junaid Shahidc57c8042019-11-04 12:22:02 +01004805
4806struct kvm_vm_worker_thread_context {
4807 struct kvm *kvm;
4808 struct task_struct *parent;
4809 struct completion init_done;
4810 kvm_vm_thread_fn_t thread_fn;
4811 uintptr_t data;
4812 int err;
4813};
4814
4815static int kvm_vm_worker_thread(void *context)
4816{
4817 /*
4818 * The init_context is allocated on the stack of the parent thread, so
4819 * we have to locally copy anything that is needed beyond initialization
4820 */
4821 struct kvm_vm_worker_thread_context *init_context = context;
4822 struct kvm *kvm = init_context->kvm;
4823 kvm_vm_thread_fn_t thread_fn = init_context->thread_fn;
4824 uintptr_t data = init_context->data;
4825 int err;
4826
4827 err = kthread_park(current);
4828 /* kthread_park(current) is never supposed to return an error */
4829 WARN_ON(err != 0);
4830 if (err)
4831 goto init_complete;
4832
4833 err = cgroup_attach_task_all(init_context->parent, current);
4834 if (err) {
4835 kvm_err("%s: cgroup_attach_task_all failed with err %d\n",
4836 __func__, err);
4837 goto init_complete;
4838 }
4839
4840 set_user_nice(current, task_nice(init_context->parent));
4841
4842init_complete:
4843 init_context->err = err;
4844 complete(&init_context->init_done);
4845 init_context = NULL;
4846
4847 if (err)
4848 return err;
4849
4850 /* Wait to be woken up by the spawner before proceeding. */
4851 kthread_parkme();
4852
4853 if (!kthread_should_stop())
4854 err = thread_fn(kvm, data);
4855
4856 return err;
4857}
4858
4859int kvm_vm_create_worker_thread(struct kvm *kvm, kvm_vm_thread_fn_t thread_fn,
4860 uintptr_t data, const char *name,
4861 struct task_struct **thread_ptr)
4862{
4863 struct kvm_vm_worker_thread_context init_context = {};
4864 struct task_struct *thread;
4865
4866 *thread_ptr = NULL;
4867 init_context.kvm = kvm;
4868 init_context.parent = current;
4869 init_context.thread_fn = thread_fn;
4870 init_context.data = data;
4871 init_completion(&init_context.init_done);
4872
4873 thread = kthread_run(kvm_vm_worker_thread, &init_context,
4874 "%s-%d", name, task_pid_nr(current));
4875 if (IS_ERR(thread))
4876 return PTR_ERR(thread);
4877
4878 /* kthread_run is never supposed to return NULL */
4879 WARN_ON(thread == NULL);
4880
4881 wait_for_completion(&init_context.init_done);
4882
4883 if (!init_context.err)
4884 *thread_ptr = thread;
4885
4886 return init_context.err;
4887}