aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ar71xx/mikrotik/config-default
blob: e324e4c2526d7a791affcfb87261617af83ac7da (plain)
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
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
/******************************************************************************
 * gnttab.c
 * 
 * Granting foreign access to our memory reservation.
 * 
 * Copyright (c) 2005, Christopher Clark
 * Copyright (c) 2004-2005, K A Fraser
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <asm/pgtable.h>
#include <asm-xen/xen-public/xen.h>
#include <asm/fixmap.h>
#include <asm/uaccess.h>
#include <asm-xen/xen_proc.h>
#include <asm-xen/linux-public/privcmd.h>
#include <asm-xen/gnttab.h>
#include <asm/synch_bitops.h>

#if 1
#define ASSERT(_p)							      \
	if ( !(_p) ) { printk(KERN_ALERT"Assertion '%s': line %d, file %s\n", \
	#_p , __LINE__, __FILE__); *(int*)0=0; }
#else
#define ASSERT(_p) ((void)0)
#endif

#define WPRINTK(fmt, args...)				\
	printk(KERN_WARNING "xen_grant: " fmt, ##args)


EXPORT_SYMBOL(gnttab_grant_foreign_access);
EXPORT_SYMBOL(gnttab_end_foreign_access_ref);
EXPORT_SYMBOL(gnttab_end_foreign_access);
EXPORT_SYMBOL(gnttab_query_foreign_access);
EXPORT_SYMBOL(gnttab_grant_foreign_transfer);
EXPORT_SYMBOL(gnttab_end_foreign_transfer_ref);
EXPORT_SYMBOL(gnttab_end_foreign_transfer);
EXPORT_SYMBOL(gnttab_alloc_grant_references);
EXPORT_SYMBOL(gnttab_free_grant_references);
EXPORT_SYMBOL(gnttab_free_grant_reference);
EXPORT_SYMBOL(gnttab_claim_grant_reference);
EXPORT_SYMBOL(gnttab_release_grant_reference);
EXPORT_SYMBOL(gnttab_grant_foreign_access_ref);
EXPORT_SYMBOL(gnttab_grant_foreign_transfer_ref);

/* External tools reserve first few grant table entries. */
#define NR_RESERVED_ENTRIES 8

#define NR_GRANT_ENTRIES (NR_GRANT_FRAMES * PAGE_SIZE / sizeof(grant_entry_t))
#define GNTTAB_LIST_END (NR_GRANT_ENTRIES + 1)

static grant_ref_t gnttab_list[NR_GRANT_ENTRIES];
static int gnttab_free_count;
static grant_ref_t gnttab_free_head;
static spinlock_t gnttab_list_lock = SPIN_LOCK_UNLOCKED;

static grant_entry_t *shared;

static struct gnttab_free_callback *gnttab_free_callback_list = NULL;

static int
get_free_entries(int count)
{
	unsigned long flags;
	int ref;
	grant_ref_t head;
	spin_lock_irqsave(&gnttab_list_lock, flags);
	if (gnttab_free_count < count) {
		spin_unlock_irqrestore(&gnttab_list_lock, flags);
		return -1;
	}
	ref = head = gnttab_free_head;
	gnttab_free_count -= count;
	while (count-- > 1)
		head = gnttab_list[head];
	gnttab_free_head = gnttab_list[head];
	gnttab_list[head] = GNTTAB_LIST_END;
	spin_unlock_irqrestore(&gnttab_list_lock, flags);
	return ref;
}

#define get_free_entry() get_free_entries(1)

static void
do_free_callbacks(void)
{
	struct gnttab_free_callback *callback, *next;

	callback = gnttab_free_callback_list;
	gnttab_free_callback_list = NULL;

	while (callback != NULL) {
		next = callback->next;
		if (gnttab_free_count >= callback->count) {
			callback->next = NULL;
			callback->fn(callback->arg);
		} else {
			callback->next = gnttab_free_callback_list;
			gnttab_free_callback_list = callback;
		}
		callback = next;
	}
}

static inline void
check_free_callbacks(void)
{
	if (unlikely(gnttab_free_callback_list))
		do_free_callbacks();
}

static void
put_free_entry(grant_ref_t ref)
{
	unsigned long flags;
	spin_lock_irqsave(&gnttab_list_lock, flags);
	gnttab_list[ref] = gnttab_free_head;
	gnttab_free_head = ref;
	gnttab_free_count++;
	check_free_callbacks();
	spin_unlock_irqrestore(&gnttab_list_lock, flags);
}

/*
 * Public grant-issuing interface functions
 */

int
gnttab_grant_foreign_access(domid_t domid, unsigned long frame, int readonly)
{
	int ref;
    
	if (unlikely((ref = get_free_entry()) == -1))
		return -ENOSPC;

	shared[ref].frame = frame;
	shared[ref].domid = domid;
	wmb();
	shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);

	return ref;
}

void
gnttab_grant_foreign_access_ref(grant_ref_t ref, domid_t domid,
				unsigned long frame, int readonly)
{
	shared[ref].frame = frame;
	shared[ref].domid = domid;
	wmb();
	shared[ref].flags = GTF_permit_access | (readonly ? GTF_readonly : 0);
}


int
gnttab_query_foreign_access(grant_ref_t ref)
{
	u16 nflags;

	nflags = shared[ref].flags;

	return (nflags & (GTF_reading|GTF_writing));
}

void
gnttab_end_foreign_access_ref(grant_ref_t ref, int readonly)
{
	u16 flags, nflags;

	nflags = shared[ref].flags;
	do {
		if ( (flags = nflags) & (GTF_reading|GTF_writing) )
			printk(KERN_ALERT "WARNING: g.e. still in use!\n");
	}
	while ((nflags = synch_cmpxchg(&shared[ref].flags, flags, 0)) !=
	       flags);
}

void
gnttab_end_foreign_access(grant_ref_t ref, int readonly)
{
	gnttab_end_foreign_access_ref(ref, readonly);
	put_free_entry(ref);
}

int
gnttab_grant_foreign_transfer(domid_t domid)
{
	int ref;

	if (unlikely((ref = get_free_entry()) == -1))
		return -ENOSPC;

	shared[ref].frame = 0;
	shared[ref].domid = domid;
	wmb();
	shared[ref].flags = GTF_accept_transfer;

	return ref;
}

void
gnttab_grant_foreign_transfer_ref(grant_ref_t ref, domid_t domid)
{
	shared[ref].frame = 0;
	shared[ref].domid = domid;
	wmb();
	shared[ref].flags = GTF_accept_transfer;
}

unsigned long
gnttab_end_foreign_transfer_ref(grant_ref_t ref)
{
	unsigned long frame = 0;
	u16           flags;

	flags = shared[ref].flags;

	/*
	 * If a transfer is committed then wait for the frame address to
	 * appear. Otherwise invalidate the grant entry against future use.
	 */
	if (likely(flags != GTF_accept_transfer) ||
	    (synch_cmpxchg(&shared[ref].flags, flags, 0) !=
	     GTF_accept_transfer))
		while (unlikely((frame = shared[ref].frame) == 0))
			cpu_relax();

	return frame;
}

unsigned long
gnttab_end_foreign_transfer(grant_ref_t ref)
{
	unsigned long frame = gnttab_end_foreign_transfer_ref(ref);
	put_free_entry(ref);
	return frame;
}

void
gnttab_free_grant_reference(grant_ref_t ref)
{

	put_free_entry(ref);
}

void
gnttab_free_grant_references(grant_ref_t head)
{
	grant_ref_t ref;
	unsigned long flags;
	int count = 1;
	if (head == GNTTAB_LIST_END)
		return;
	spin_lock_irqsave(&gnttab_list_lock, flags);
	ref = head;
	while (gnttab_list[ref] != GNTTAB_LIST_END) {
		ref = gnttab_list[ref];
		count++;
	}
	gnttab_list[ref] = gnttab_free_head;
	gnttab_free_head = head;
	gnttab_free_count += count;
	check_free_callbacks();
	spin_unlock_irqrestore(&gnttab_list_lock, flags);
}

int
gnttab_alloc_grant_references(u16 count, grant_ref_t *head)
{
	int h = get_free_entries(count);

	if (h == -1)
		return -ENOSPC;

	*head = h;

	return 0;
}

int
gnttab_claim_grant_reference(grant_ref_t *private_head)
{
	grant_ref_t g = *private_head;
	if (unlikely(g == GNTTAB_LIST_END))
		return -ENOSPC;
	*private_head = gnttab_list[g];
	return g;
}

void
gnttab_release_grant_reference(grant_ref_t *private_head, grant_ref_t  release)
{
	gnttab_list[release] = *private_head;
	*private_head = release;
}

void
gnttab_request_free_callback(struct gnttab_free_callback *callback,
			     void (*fn)(void *), void *arg, u16 count)
{
	unsigned long flags;
	spin_lock_irqsave(&gnttab_list_lock, flags);
	if (callback->next)
		goto out;
	callback->fn = fn;
	callback->arg = arg;
	callback->count = count;
	callback->next = gnttab_free_callback_list;
	gnttab_free_callback_list = callback;
	check_free_callbacks();
 out:
	spin_unlock_irqrestore(&gnttab_list_lock, flags);
}

/*
 * ProcFS operations
 */

#ifdef CONFIG_PROC_FS

static struct proc_dir_entry *grant_pde;

static int
grant_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
	    unsigned long data)
{
	int                     ret;
	privcmd_hypercall_t     hypercall;

	/*
	 * XXX Need safety checks here if using for anything other
	 *     than debugging.
	 */
	return -ENOSYS;

	if ( cmd != IOCTL_PRIVCMD_HYPERCALL )
		return -ENOSYS;

	if ( copy_from_user(&hypercall, (void *)data, sizeof(hypercall)) )
		return -EFAULT;

	if ( hypercall.op != __HYPERVISOR_grant_table_op )
		return -ENOSYS;

	/* hypercall-invoking asm taken from privcmd.c */
	__asm__ __volatile__ (
		"pushl %%ebx; pushl %%ecx; pushl %%edx; "
		"pushl %%esi; pushl %%edi; "
		"movl  4(%%eax),%%ebx ;"
		"movl  8(%%eax),%%ecx ;"
		"movl 12(%%eax),%%edx ;"
		"movl 16(%%eax),%%esi ;"
		"movl 20(%%eax),%%edi ;"
		"movl   (%%eax),%%eax ;"
		TRAP_INSTR "; "
		"popl %%edi; popl %%esi; popl %%edx; popl %%ecx; popl %%ebx"
		: "=a" (ret) : "0" (&hypercall) : "memory" );

	return ret;
}

static struct file_operations grant_file_ops = {
	ioctl:  grant_ioctl,
};

static int
grant_read(char *page, char **start, off_t off, int count, int *eof,
	   void *data)
{
	int             len;
	unsigned int    i;
	grant_entry_t  *gt;

	gt = (grant_entry_t *)shared;
	len = 0;

	for (i = 0; i < NR_GRANT_ENTRIES; i++) {
		if (len > (PAGE_SIZE - 200)) {
			len += sprintf( page + len, "Truncated.\n");
			break;
		}
	}

	if (gt[i].flags) {
		len += sprintf(page + len,
			       "Grant: ref (0x%x) flags (0x%hx) "
			       "dom (0x%hx) frame (0x%x)\n", 
			       i,
			       gt[i].flags,
			       gt[i].domid,
			       gt[i].frame );
	}

	*eof = 1;
	return len;
}

static int
grant_write(struct file *file, const char __user *buffer, unsigned long count,
	    void *data)
{
	/* TODO: implement this */
	return -ENOSYS;
}

#endif /* CONFIG_PROC_FS */

int
gnttab_resume(void)
{
	gnttab_setup_table_t setup;
	unsigned long        frames[NR_GRANT_FRAMES];
	int                  i;

	setup.dom        = DOMID_SELF;
	setup.nr_frames  = NR_GRANT_FRAMES;
	setup.frame_list = frames;

	BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_setup_table, &setup, 1));
	BUG_ON(setup.status != 0);

	for (i = 0; i < NR_GRANT_FRAMES; i++)
		set_fixmap(FIX_GNTTAB_END - i, frames[i] << PAGE_SHIFT);

	return 0;
}

int
gnttab_suspend(void)
{
	int i;

	for (i = 0; i < NR_GRANT_FRAMES; i++)
		clear_fixmap(FIX_GNTTAB_END - i);

	return 0;
}

static int __init
gnttab_init(void)
{
	int i;

	if (xen_init() < 0)
		return -ENODEV;

	BUG_ON(gnttab_resume());

	shared = (grant_entry_t *)fix_to_virt(FIX_GNTTAB_END);

	for (i = NR_RESERVED_ENTRIES; i < NR_GRANT_ENTRIES; i++)
		gnttab_list[i] = i + 1;
	gnttab_free_count = NR_GRANT_ENTRIES - NR_RESERVED_ENTRIES;
	gnttab_free_head  = NR_RESERVED_ENTRIES;

#ifdef CONFIG_PROC_FS
	/*
	 *  /proc/xen/grant : used by libxc to access grant tables
	 */
	if ((grant_pde = create_xen_proc_entry("grant", 0600)) == NULL) {
		WPRINTK("Unable to create grant xen proc entry\n");
		return -1;
	}

	grant_file_ops.read   = grant_pde->proc_fops->read;
	grant_file_ops.write  = grant_pde->proc_fops->write;

	grant_pde->proc_fops  = &grant_file_ops;

	grant_pde->read_proc  = &grant_read;
	grant_pde->write_proc = &grant_write;
#endif

	printk("Grant table initialized\n");
	return 0;
}

__initcall(gnttab_init);

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