aboutsummaryrefslogtreecommitdiffstats
path: root/arch/ia64/kernel/paravirt_patch.c
blob: bfdfef1b1ffd2c57e8920cebc924cd13a4880d32 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/******************************************************************************
 * linux/arch/ia64/xen/paravirt_patch.c
 *
 * Copyright (c) 2008 Isaku Yamahata <yamahata at valinux co jp>
 *                    VA Linux Systems Japan K.K.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include <linux/init.h>
#include <asm/intrinsics.h>
#include <asm/kprobes.h>
#include <asm/paravirt.h>
#include <asm/paravirt_patch.h>

typedef union ia64_inst {
        struct {
		unsigned long long qp : 6;
		unsigned long long : 31;
		unsigned long long opcode : 4;
		unsigned long long reserved : 23;
        } generic;
        unsigned long long l;
} ia64_inst_t;

/*
 * flush_icache_range() can't be used here.
 * we are here before cpu_init() which initializes
 * ia64_i_cache_stride_shift. flush_icache_range() uses it.
 */
void __init_or_module
paravirt_flush_i_cache_range(const void *instr, unsigned long size)
{
	extern void paravirt_fc_i(const void *addr);
	unsigned long i;

	for (i = 0; i < size; i += sizeof(bundle_t))
		paravirt_fc_i(instr + i);
}

bundle_t* __init_or_module
paravirt_get_bundle(unsigned long tag)
{
	return (bundle_t *)(tag & ~3UL);
}

unsigned long __init_or_module
paravirt_get_slot(unsigned long tag)
{
	return tag & 3UL;
}

unsigned long __init_or_module
paravirt_get_num_inst(unsigned long stag, unsigned long etag)
{
	bundle_t *sbundle = paravirt_get_bundle(stag);
	unsigned long sslot = paravirt_get_slot(stag);
	bundle_t *ebundle = paravirt_get_bundle(etag);
	unsigned long eslot = paravirt_get_slot(etag);

	return (ebundle - sbundle) * 3 + eslot - sslot + 1;
}

unsigned long __init_or_module
paravirt_get_next_tag(unsigned long tag)
{
	unsigned long slot = paravirt_get_slot(tag);

	switch (slot) {
	case 0:
	case 1:
		return tag + 1;
	case 2: {
		bundle_t *bundle = paravirt_get_bundle(tag);
		return (unsigned long)(bundle + 1);
	}
	default:
		BUG();
	}
	/* NOTREACHED */
}

ia64_inst_t __init_or_module
paravirt_read_slot0(const bundle_t *bundle)
{
	ia64_inst_t inst;
	inst.l = bundle->quad0.slot0;
	return inst;
}

ia64_inst_t __init_or_module
paravirt_read_slot1(const bundle_t *bundle)
{
	ia64_inst_t inst;
	inst.l = bundle->quad0.slot1_p0 |
		((unsigned long long)bundle->quad1.slot1_p1 << 18UL);
	return inst;
}

ia64_inst_t __init_or_module
paravirt_read_slot2(const bundle_t *bundle)
{
	ia64_inst_t inst;
	inst.l = bundle->quad1.slot2;
	return inst;
}

ia64_inst_t __init_or_module
paravirt_read_inst(unsigned long tag)
{
	bundle_t *bundle = paravirt_get_bundle(tag);
	unsigned long slot = paravirt_get_slot(tag);

	switch (slot) {
	case 0:
		return paravirt_read_slot0(bundle);
	case 1:
		return paravirt_read_slot1(bundle);
	case 2:
		return paravirt_read_slot2(bundle);
	default:
		BUG();
	}
	/* NOTREACHED */
}

void __init_or_module
paravirt_write_slot0(bundle_t *bundle, ia64_inst_t inst)
{
	bundle->quad0.slot0 = inst.l;
}

void __init_or_module
paravirt_write_slot1(bundle_t *bundle, ia64_inst_t inst)
{
	bundle->quad0.slot1_p0 = inst.l;
	bundle->quad1.slot1_p1 = inst.l >> 18UL;
}

void __init_or_module
paravirt_write_slot2(bundle_t *bundle, ia64_inst_t inst)
{
	bundle->quad1.slot2 = inst.l;
}

void __init_or_module
paravirt_write_inst(unsigned long tag, ia64_inst_t inst)
{
	bundle_t *bundle = paravirt_get_bundle(tag);
	unsigned long slot = paravirt_get_slot(tag);

	switch (slot) {
	case 0:
		paravirt_write_slot0(bundle, inst);
		break;
	case 1:
		paravirt_write_slot1(bundle, inst);
		break;
	case 2:
		paravirt_write_slot2(bundle, inst);
		break;
	default:
		BUG();
		break;
	}
	paravirt_flush_i_cache_range(bundle, sizeof(*bundle));
}

/* for debug */
void
paravirt_print_bundle(const bundle_t *bundle)
{
	const unsigned long *quad = (const unsigned long *)bundle;
	ia64_inst_t slot0 = paravirt_read_slot0(bundle);
	ia64_inst_t slot1 = paravirt_read_slot1(bundle);
	ia64_inst_t slot2 = paravirt_read_slot2(bundle);

	printk(KERN_DEBUG
	       "bundle 0x%p 0x%016lx 0x%016lx\n", bundle, quad[0], quad[1]);
	printk(KERN_DEBUG
	       "bundle template 0x%x\n",
	       bundle->quad0.template);
	printk(KERN_DEBUG
	       "slot0 0x%lx slot1_p0 0x%lx slot1_p1 0x%lx slot2 0x%lx\n",
	       (unsigned long)bundle->quad0.slot0,
	       (unsigned long)bundle->quad0.slot1_p0,
	       (unsigned long)bundle->quad1.slot1_p1,
	       (unsigned long)bundle->quad1.slot2);
	printk(KERN_DEBUG
	       "slot0 0x%016llx slot1 0x%016llx slot2 0x%016llx\n",
	       slot0.l, slot1.l, slot2.l);
}

static int noreplace_paravirt __init_or_module = 0;

static int __init setup_noreplace_paravirt(char *str)
{
	noreplace_paravirt = 1;
	return 1;
}
__setup("noreplace-paravirt", setup_noreplace_paravirt);

#ifdef ASM_SUPPORTED
static void __init_or_module
fill_nop_bundle(void *sbundle, void *ebundle)
{
	extern const char paravirt_nop_bundle[];
	extern const unsigned long paravirt_nop_bundle_size;

	void *bundle = sbundle;

	BUG_ON((((unsigned long)sbundle) % sizeof(bundle_t)) != 0);
	BUG_ON((((unsigned long)ebundle) % sizeof(bundle_t)) != 0);

	while (bundle < ebundle) {
		memcpy(bundle, paravirt_nop_bundle, paravirt_nop_bundle_size);

		bundle += paravirt_nop_bundle_size;
	}
}

/* helper function */
unsigned long __init_or_module
__paravirt_patch_apply_bundle(void *sbundle, void *ebundle, unsigned long type,
			      const struct paravirt_patch_bundle_elem *elems,
			      unsigned long nelems,
			      const struct paravirt_patch_bundle_elem **found)
{
	unsigned long used = 0;
	unsigned long i;

	BUG_ON((((unsigned long)sbundle) % sizeof(bundle_t)) != 0);
	BUG_ON((((unsigned long)ebundle) % sizeof(bundle_t)) != 0);

	found = NULL;
	for (i = 0; i < nelems; i++) {
		const struct paravirt_patch_bundle_elem *p = &elems[i];
		if (p->type == type) {
			unsigned long need = p->ebundle - p->sbundle;
			unsigned long room = ebundle - sbundle;

			if (found != NULL)
				*found = p;

			if (room < need) {
				/* no room to replace. skip it */
				printk(KERN_DEBUG
				       "the space is too small to put "
				       "bundles. type %ld need %ld room %ld\n",
				       type, need, room);
				break;
			}

			used = need;
			memcpy(sbundle, p->sbundle, used);
			break;
		}
	}

	return used;
}

void __init_or_module
paravirt_patch_apply_bundle(const struct paravirt_patch_site_bundle *start,
			    const struct paravirt_patch_site_bundle *end)
{
	const struct paravirt_patch_site_bundle *p;

	if (noreplace_paravirt)
		return;
	if (pv_init_ops.patch_bundle == NULL)
		return;

	for (p = start; p < end; p++) {
		unsigned long used;

		used = (*pv_init_ops.patch_bundle)(p->sbundle, p->ebundle,
						   p->type);
		if (used == 0)
			continue;

		fill_nop_bundle(p->sbundle + used, p->ebundle);
		paravirt_flush_i_cache_range(p->sbundle,
					     p->ebundle - p->sbundle);
	}
	ia64_sync_i();
	ia64_srlz_i();
}

/*
 * nop.i, nop.m, nop.f instruction are same format.
 * but nop.b has differennt format.
 * This doesn't support nop.b for now.
 */
static void __init_or_module
fill_nop_inst(unsigned long stag, unsigned long etag)
{
	extern const bundle_t paravirt_nop_mfi_inst_bundle[];
	unsigned long tag;
	const ia64_inst_t nop_inst =
		paravirt_read_slot0(paravirt_nop_mfi_inst_bundle);

	for (tag = stag; tag < etag; tag = paravirt_get_next_tag(tag))
		paravirt_write_inst(tag, nop_inst);
}

void __init_or_module
paravirt_patch_apply_inst(const struct paravirt_patch_site_inst *start,
			  const struct paravirt_patch_site_inst *end)
{
	const struct paravirt_patch_site_inst *p;

	if (noreplace_paravirt)
		return;
	if (pv_init_ops.patch_inst == NULL)
		return;

	for (p = start; p < end; p++) {
		unsigned long tag;
		bundle_t *sbundle;
		bundle_t *ebundle;

		tag = (*pv_init_ops.patch_inst)(p->stag, p->etag, p->type);
		if (tag == p->stag)
			continue;

		fill_nop_inst(tag, p->etag);
		sbundle = paravirt_get_bundle(p->stag);
		ebundle = paravirt_get_bundle(p->etag) + 1;
		paravirt_flush_i_cache_range(sbundle, (ebundle - sbundle) *
					     sizeof(bundle_t));
	}
	ia64_sync_i();
	ia64_srlz_i();
}
#endif /* ASM_SUPPOTED */

/* brl.cond.sptk.many <target64> X3 */
typedef union inst_x3_op {
	ia64_inst_t inst;
	struct {
		unsigned long qp: 6;
		unsigned long btyp: 3;
		unsigned long unused: 3;
		unsigned long p: 1;
		unsigned long imm20b: 20;
		unsigned long wh: 2;
		unsigned long d: 1;
		unsigned long i: 1;
		unsigned long opcode: 4;
	};
	unsigned long l;
} inst_x3_op_t;

typedef union inst_x3_imm {
	ia64_inst_t inst;
	struct {
		unsigned long unused: 2;
		unsigned long imm39: 39;
	};
	unsigned long l;
} inst_x3_imm_t;

void __init_or_module
paravirt_patch_reloc_brl(unsigned long tag, const void *target)
{
	unsigned long tag_op = paravirt_get_next_tag(tag);
	unsigned long tag_imm = tag;
	bundle_t *bundle = paravirt_get_bundle(tag);

	ia64_inst_t inst_op = paravirt_read_inst(tag_op);
	ia64_inst_t inst_imm = paravirt_read_inst(tag_imm);

	inst_x3_op_t inst_x3_op = { .l = inst_op.l };
	inst_x3_imm_t inst_x3_imm = { .l = inst_imm.l };

	unsigned long imm60 =
		((unsigned long)target - (unsigned long)bundle) >> 4;

	BUG_ON(paravirt_get_slot(tag) != 1); /* MLX */
	BUG_ON(((unsigned long)target & (sizeof(bundle_t) - 1)) != 0);

	/* imm60[59] 1bit */
	inst_x3_op.i = (imm60 >> 59) & 1;
	/* imm60[19:0] 20bit */
	inst_x3_op.imm20b = imm60 & ((1UL << 20) - 1);
	/* imm60[58:20] 39bit */
	inst_x3_imm.imm39 = (imm60 >> 20) & ((1UL << 39) - 1);

	inst_op.l = inst_x3_op.l;
	inst_imm.l = inst_x3_imm.l;

	paravirt_write_inst(tag_op, inst_op);
	paravirt_write_inst(tag_imm, inst_imm);
}

/* br.cond.sptk.many <target25>	B1 */
typedef union inst_b1 {
	ia64_inst_t inst;
	struct {
		unsigned long qp: 6;
		unsigned long btype: 3;
		unsigned long unused: 3;
		unsigned long p: 1;
		unsigned long imm20b: 20;
		unsigned long wh: 2;
		unsigned long d: 1;
		unsigned long s: 1;
		unsigned long opcode: 4;
	};
	unsigned long l;
} inst_b1_t;

void __init
paravirt_patch_reloc_br(unsigned long tag, const void *target)
{
	bundle_t *bundle = paravirt_get_bundle(tag);
	ia64_inst_t inst = paravirt_read_inst(tag);
	unsigned long target25 = (unsigned long)target - (unsigned long)bundle;
	inst_b1_t inst_b1;

	BUG_ON(((unsigned long)target & (sizeof(bundle_t) - 1)) != 0);

	inst_b1.l = inst.l;
	if (target25 & (1UL << 63))
		inst_b1.s = 1;
	else
		inst_b1.s = 0;

	inst_b1.imm20b = target25 >> 4;
	inst.l = inst_b1.l;

	paravirt_write_inst(tag, inst);
}

void __init
__paravirt_patch_apply_branch(
	unsigned long tag, unsigned long type,
	const struct paravirt_patch_branch_target *entries,
	unsigned int nr_entries)
{
	unsigned int i;
	for (i = 0; i < nr_entries; i++) {
		if (entries[i].type == type) {
			paravirt_patch_reloc_br(tag, entries[i].entry);
			break;
		}
	}
}

static void __init
paravirt_patch_apply_branch(const struct paravirt_patch_site_branch *start,
			    const struct paravirt_patch_site_branch *end)
{
	const struct paravirt_patch_site_branch *p;

	if (noreplace_paravirt)
		return;
	if (pv_init_ops.patch_branch == NULL)
		return;

	for (p = start; p < end; p++)
		(*pv_init_ops.patch_branch)(p->tag, p->type);

	ia64_sync_i();
	ia64_srlz_i();
}

void __init
paravirt_patch_apply(void)
{
	extern const char __start_paravirt_bundles[];
	extern const char __stop_paravirt_bundles[];
	extern const char __start_paravirt_insts[];
	extern const char __stop_paravirt_insts[];
	extern const char __start_paravirt_branches[];
	extern const char __stop_paravirt_branches[];

	paravirt_patch_apply_bundle((const struct paravirt_patch_site_bundle *)
				    __start_paravirt_bundles,
				    (const struct paravirt_patch_site_bundle *)
				    __stop_paravirt_bundles);
	paravirt_patch_apply_inst((const struct paravirt_patch_site_inst *)
				  __start_paravirt_insts,
				  (const struct paravirt_patch_site_inst *)
				  __stop_paravirt_insts);
	paravirt_patch_apply_branch((const struct paravirt_patch_site_branch *)
				    __start_paravirt_branches,
				    (const struct paravirt_patch_site_branch *)
				    __stop_paravirt_branches);
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "linux"
 * c-basic-offset: 8
 * tab-width: 8
 * indent-tabs-mode: t
 * End:
 */