aboutsummaryrefslogtreecommitdiffstats
path: root/linux-2.6-xen-sparse/arch/ia64/xen/xencomm.c
blob: 3767e893fc5cbcc1359d73c879228c02938bb2b1 (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
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
/*
 * Copyright (C) 2006 Hollis Blanchard <hollisb@us.ibm.com>, IBM Corporation
 *
 * 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/gfp.h>
#include <linux/mm.h>
#include <xen/interface/xen.h>
#include <asm/page.h>
#include <asm/xen/xencomm.h>

static int xencomm_debug = 0;

static unsigned long kernel_start_pa;

void
xencomm_init (void)
{
	kernel_start_pa = KERNEL_START - ia64_tpa(KERNEL_START);
}

/* Translate virtual address to physical address.  */
unsigned long
xencomm_vaddr_to_paddr(unsigned long vaddr)
{
#ifndef CONFIG_VMX_GUEST
	struct page *page;
	struct vm_area_struct *vma;
#endif

	if (vaddr == 0)
		return 0;

#ifdef __ia64__
	if (REGION_NUMBER(vaddr) == 5) {
		pgd_t *pgd;
		pud_t *pud;
		pmd_t *pmd;
		pte_t *ptep;

		/* On ia64, TASK_SIZE refers to current.  It is not initialized
		   during boot.
		   Furthermore the kernel is relocatable and __pa() doesn't
		   work on  addresses.  */
		if (vaddr >= KERNEL_START
		    && vaddr < (KERNEL_START + KERNEL_TR_PAGE_SIZE)) {
			return vaddr - kernel_start_pa;
		}

		/* In kernel area -- virtually mapped.  */
		pgd = pgd_offset_k(vaddr);
		if (pgd_none(*pgd) || pgd_bad(*pgd))
			return ~0UL;

		pud = pud_offset(pgd, vaddr);
		if (pud_none(*pud) || pud_bad(*pud))
			return ~0UL;

		pmd = pmd_offset(pud, vaddr);
		if (pmd_none(*pmd) || pmd_bad(*pmd))
			return ~0UL;

		ptep = pte_offset_kernel(pmd, vaddr);
		if (!ptep)
			return ~0UL;

		return (pte_val(*ptep) & _PFN_MASK) | (vaddr & ~PAGE_MASK);
	}
#endif

	if (vaddr > TASK_SIZE) {
		/* kernel address */
		return __pa(vaddr);
	}


#ifdef CONFIG_VMX_GUEST
	/* No privcmd within vmx guest.  */
	return ~0UL;
#else
	/* XXX double-check (lack of) locking */
	vma = find_extend_vma(current->mm, vaddr);
	if (!vma)
		return ~0UL;

	/* We assume the page is modified.  */
	page = follow_page(vma, vaddr, FOLL_WRITE | FOLL_TOUCH);
	if (!page)
		return ~0UL;

	return (page_to_pfn(page) << PAGE_SHIFT) | (vaddr & ~PAGE_MASK);
#endif
}

static int
xencomm_init_desc(struct xencomm_desc *desc, void *buffer, unsigned long bytes)
{
	unsigned long recorded = 0;
	int i = 0;

	BUG_ON((buffer == NULL) && (bytes > 0));

	/* record the physical pages used */
	if (buffer == NULL)
		desc->nr_addrs = 0;

	while ((recorded < bytes) && (i < desc->nr_addrs)) {
		unsigned long vaddr = (unsigned long)buffer + recorded;
		unsigned long paddr;
		int offset;
		int chunksz;

		offset = vaddr % PAGE_SIZE; /* handle partial pages */
		chunksz = min(PAGE_SIZE - offset, bytes - recorded);

		paddr = xencomm_vaddr_to_paddr(vaddr);
		if (paddr == ~0UL) {
			printk("%s: couldn't translate vaddr %lx\n",
			       __func__, vaddr);
			return -EINVAL;
		}

		desc->address[i++] = paddr;
		recorded += chunksz;
	}

	if (recorded < bytes) {
		printk("%s: could only translate %ld of %ld bytes\n",
		       __func__, recorded, bytes);
		return -ENOSPC;
	}

	/* mark remaining addresses invalid (just for safety) */
	while (i < desc->nr_addrs)
		desc->address[i++] = XENCOMM_INVALID;

	desc->magic = XENCOMM_MAGIC;

	return 0;
}

static struct xencomm_desc *
xencomm_alloc(gfp_t gfp_mask)
{
	struct xencomm_desc *desc;

	desc = (struct xencomm_desc *)__get_free_page(gfp_mask);
	if (desc == NULL)
		panic("%s: page allocation failed\n", __func__);

	desc->nr_addrs = (PAGE_SIZE - sizeof(struct xencomm_desc)) /
	                 sizeof(*desc->address);

	return desc;
}

void
xencomm_free(struct xencomm_handle *desc)
{
	if (desc)
		free_page((unsigned long)__va(desc));
}

int
xencomm_create(void *buffer, unsigned long bytes,
               struct xencomm_handle **ret, gfp_t gfp_mask)
{
	struct xencomm_desc *desc;
	struct xencomm_handle *handle;
	int rc;

	if (xencomm_debug)
		printk("%s: %p[%ld]\n", __func__, buffer, bytes);

	if (buffer == NULL || bytes == 0) {
		*ret = (struct xencomm_handle *)NULL;
		return 0;
	}

	desc = xencomm_alloc(gfp_mask);
	if (!desc) {
		printk("%s failure\n", "xencomm_alloc");
		return -ENOMEM;
	}
	handle = (struct xencomm_handle *)__pa(desc);

	rc = xencomm_init_desc(desc, buffer, bytes);
	if (rc) {
		printk("%s failure: %d\n", "xencomm_init_desc", rc);
		xencomm_free(handle);
		return rc;
	}

	*ret = handle;
	return 0;
}

/* "mini" routines, for stack-based communications: */

static void *
xencomm_alloc_mini(struct xencomm_mini *area, int *nbr_area)
{
	unsigned long base;
	unsigned int pageoffset;

	while (*nbr_area >= 0) {
		/* Allocate an area.  */
		(*nbr_area)--;

		base = (unsigned long)(area + *nbr_area);
		pageoffset = base % PAGE_SIZE;

		/* If the area does not cross a page, use it.  */
		if ((PAGE_SIZE - pageoffset) >= sizeof(struct xencomm_mini))
			return &area[*nbr_area];
	}
	/* No more area.  */
	return NULL;
}

int
xencomm_create_mini(struct xencomm_mini *area, int *nbr_area,
                    void *buffer, unsigned long bytes,
                    struct xencomm_handle **ret)
{
	struct xencomm_desc *desc;
	int rc;
	unsigned long res;

	desc = xencomm_alloc_mini(area, nbr_area);
	if (!desc)
		return -ENOMEM;
	desc->nr_addrs = XENCOMM_MINI_ADDRS;

	rc = xencomm_init_desc(desc, buffer, bytes);
	if (rc)
		return rc;

	res = xencomm_vaddr_to_paddr((unsigned long)desc);
	if (res == ~0UL)
		return -EINVAL;

	*ret = (struct xencomm_handle*)res;
	return 0;
}