book3s_rtas.c 6.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10
/*
 * Copyright 2012 Michael Ellerman, IBM Corporation.
 */

#include <linux/kernel.h>
#include <linux/kvm_host.h>
#include <linux/kvm.h>
#include <linux/err.h>

11
#include <linux/uaccess.h>
12 13 14 15
#include <asm/kvm_book3s.h>
#include <asm/kvm_ppc.h>
#include <asm/hvcall.h>
#include <asm/rtas.h>
16
#include <asm/xive.h>
17

18 19 20 21 22 23
#ifdef CONFIG_KVM_XICS
static void kvm_rtas_set_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
{
	u32 irq, server, priority;
	int rc;

24
	if (be32_to_cpu(args->nargs) != 3 || be32_to_cpu(args->nret) != 1) {
25 26 27 28
		rc = -3;
		goto out;
	}

29 30 31
	irq = be32_to_cpu(args->args[0]);
	server = be32_to_cpu(args->args[1]);
	priority = be32_to_cpu(args->args[2]);
32

33
	if (xics_on_xive())
34 35 36
		rc = kvmppc_xive_set_xive(vcpu->kvm, irq, server, priority);
	else
		rc = kvmppc_xics_set_xive(vcpu->kvm, irq, server, priority);
37 38 39
	if (rc)
		rc = -3;
out:
40
	args->rets[0] = cpu_to_be32(rc);
41 42 43 44 45 46 47
}

static void kvm_rtas_get_xive(struct kvm_vcpu *vcpu, struct rtas_args *args)
{
	u32 irq, server, priority;
	int rc;

48
	if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 3) {
49 50 51 52
		rc = -3;
		goto out;
	}

53
	irq = be32_to_cpu(args->args[0]);
54 55

	server = priority = 0;
56
	if (xics_on_xive())
57 58 59
		rc = kvmppc_xive_get_xive(vcpu->kvm, irq, &server, &priority);
	else
		rc = kvmppc_xics_get_xive(vcpu->kvm, irq, &server, &priority);
60 61 62 63 64
	if (rc) {
		rc = -3;
		goto out;
	}

65 66
	args->rets[1] = cpu_to_be32(server);
	args->rets[2] = cpu_to_be32(priority);
67
out:
68
	args->rets[0] = cpu_to_be32(rc);
69
}
70 71 72 73 74 75

static void kvm_rtas_int_off(struct kvm_vcpu *vcpu, struct rtas_args *args)
{
	u32 irq;
	int rc;

76
	if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
77 78 79 80
		rc = -3;
		goto out;
	}

81
	irq = be32_to_cpu(args->args[0]);
82

83
	if (xics_on_xive())
84 85 86
		rc = kvmppc_xive_int_off(vcpu->kvm, irq);
	else
		rc = kvmppc_xics_int_off(vcpu->kvm, irq);
87 88 89
	if (rc)
		rc = -3;
out:
90
	args->rets[0] = cpu_to_be32(rc);
91 92 93 94 95 96 97
}

static void kvm_rtas_int_on(struct kvm_vcpu *vcpu, struct rtas_args *args)
{
	u32 irq;
	int rc;

98
	if (be32_to_cpu(args->nargs) != 1 || be32_to_cpu(args->nret) != 1) {
99 100 101 102
		rc = -3;
		goto out;
	}

103
	irq = be32_to_cpu(args->args[0]);
104

105
	if (xics_on_xive())
106 107 108
		rc = kvmppc_xive_int_on(vcpu->kvm, irq);
	else
		rc = kvmppc_xics_int_on(vcpu->kvm, irq);
109 110 111
	if (rc)
		rc = -3;
out:
112
	args->rets[0] = cpu_to_be32(rc);
113
}
114
#endif /* CONFIG_KVM_XICS */
115 116 117 118 119 120

struct rtas_handler {
	void (*handler)(struct kvm_vcpu *vcpu, struct rtas_args *args);
	char *name;
};

121 122 123 124
static struct rtas_handler rtas_handlers[] = {
#ifdef CONFIG_KVM_XICS
	{ .name = "ibm,set-xive", .handler = kvm_rtas_set_xive },
	{ .name = "ibm,get-xive", .handler = kvm_rtas_get_xive },
125 126
	{ .name = "ibm,int-off",  .handler = kvm_rtas_int_off },
	{ .name = "ibm,int-on",   .handler = kvm_rtas_int_on },
127 128
#endif
};
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145

struct rtas_token_definition {
	struct list_head list;
	struct rtas_handler *handler;
	u64 token;
};

static int rtas_name_matches(char *s1, char *s2)
{
	struct kvm_rtas_token_args args;
	return !strncmp(s1, s2, sizeof(args.name));
}

static int rtas_token_undefine(struct kvm *kvm, char *name)
{
	struct rtas_token_definition *d, *tmp;

146
	lockdep_assert_held(&kvm->arch.rtas_token_lock);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

	list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
		if (rtas_name_matches(d->handler->name, name)) {
			list_del(&d->list);
			kfree(d);
			return 0;
		}
	}

	/* It's not an error to undefine an undefined token */
	return 0;
}

static int rtas_token_define(struct kvm *kvm, char *name, u64 token)
{
	struct rtas_token_definition *d;
	struct rtas_handler *h = NULL;
	bool found;
	int i;

167
	lockdep_assert_held(&kvm->arch.rtas_token_lock);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

	list_for_each_entry(d, &kvm->arch.rtas_tokens, list) {
		if (d->token == token)
			return -EEXIST;
	}

	found = false;
	for (i = 0; i < ARRAY_SIZE(rtas_handlers); i++) {
		h = &rtas_handlers[i];
		if (rtas_name_matches(h->name, name)) {
			found = true;
			break;
		}
	}

	if (!found)
		return -ENOENT;

	d = kzalloc(sizeof(*d), GFP_KERNEL);
	if (!d)
		return -ENOMEM;

	d->handler = h;
	d->token = token;

	list_add_tail(&d->list, &kvm->arch.rtas_tokens);

	return 0;
}

int kvm_vm_ioctl_rtas_define_token(struct kvm *kvm, void __user *argp)
{
	struct kvm_rtas_token_args args;
	int rc;

	if (copy_from_user(&args, argp, sizeof(args)))
		return -EFAULT;

206
	mutex_lock(&kvm->arch.rtas_token_lock);
207 208 209 210 211 212

	if (args.token)
		rc = rtas_token_define(kvm, args.name, args.token);
	else
		rc = rtas_token_undefine(kvm, args.name);

213
	mutex_unlock(&kvm->arch.rtas_token_lock);
214 215 216 217 218 219 220 221 222 223 224 225

	return rc;
}

int kvmppc_rtas_hcall(struct kvm_vcpu *vcpu)
{
	struct rtas_token_definition *d;
	struct rtas_args args;
	rtas_arg_t *orig_rets;
	gpa_t args_phys;
	int rc;

226 227 228 229 230
	/*
	 * r4 contains the guest physical address of the RTAS args
	 * Mask off the top 4 bits since this is a guest real address
	 */
	args_phys = kvmppc_get_gpr(vcpu, 4) & KVM_PAM;
231

232
	vcpu->srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
233
	rc = kvm_read_guest(vcpu->kvm, args_phys, &args, sizeof(args));
234
	srcu_read_unlock(&vcpu->kvm->srcu, vcpu->srcu_idx);
235 236 237 238 239 240 241 242 243 244
	if (rc)
		goto fail;

	/*
	 * args->rets is a pointer into args->args. Now that we've
	 * copied args we need to fix it up to point into our copy,
	 * not the guest args. We also need to save the original
	 * value so we can restore it on the way out.
	 */
	orig_rets = args.rets;
245 246 247 248 249 250 251 252 253 254 255
	if (be32_to_cpu(args.nargs) >= ARRAY_SIZE(args.args)) {
		/*
		 * Don't overflow our args array: ensure there is room for
		 * at least rets[0] (even if the call specifies 0 nret).
		 *
		 * Each handler must then check for the correct nargs and nret
		 * values, but they may always return failure in rets[0].
		 */
		rc = -EINVAL;
		goto fail;
	}
256
	args.rets = &args.args[be32_to_cpu(args.nargs)];
257

258
	mutex_lock(&vcpu->kvm->arch.rtas_token_lock);
259 260 261

	rc = -ENOENT;
	list_for_each_entry(d, &vcpu->kvm->arch.rtas_tokens, list) {
262
		if (d->token == be32_to_cpu(args.token)) {
263 264 265 266 267 268
			d->handler->handler(vcpu, &args);
			rc = 0;
			break;
		}
	}

269
	mutex_unlock(&vcpu->kvm->arch.rtas_token_lock);
270 271 272 273 274 275 276 277 278 279 280 281 282

	if (rc == 0) {
		args.rets = orig_rets;
		rc = kvm_write_guest(vcpu->kvm, args_phys, &args, sizeof(args));
		if (rc)
			goto fail;
	}

	return rc;

fail:
	/*
	 * We only get here if the guest has called RTAS with a bogus
283 284 285 286 287 288 289 290 291 292 293
	 * args pointer or nargs/nret values that would overflow the
	 * array. That means we can't get to the args, and so we can't
	 * fail the RTAS call. So fail right out to userspace, which
	 * should kill the guest.
	 *
	 * SLOF should actually pass the hcall return value from the
	 * rtas handler call in r3, so enter_rtas could be modified to
	 * return a failure indication in r3 and we could return such
	 * errors to the guest rather than failing to host userspace.
	 * However old guests that don't test for failure could then
	 * continue silently after errors, so for now we won't do this.
294 295 296
	 */
	return rc;
}
297
EXPORT_SYMBOL_GPL(kvmppc_rtas_hcall);
298 299 300 301 302 303 304 305 306 307

void kvmppc_rtas_tokens_free(struct kvm *kvm)
{
	struct rtas_token_definition *d, *tmp;

	list_for_each_entry_safe(d, tmp, &kvm->arch.rtas_tokens, list) {
		list_del(&d->list);
		kfree(d);
	}
}