Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / include / xen / interface / memory.h
1 /******************************************************************************
2  * memory.h
3  *
4  * Memory reservation and information.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Copyright (c) 2005, Keir Fraser <keir@xensource.com>
25  */
26
27 #ifndef __XEN_PUBLIC_MEMORY_H__
28 #define __XEN_PUBLIC_MEMORY_H__
29
30 #include "xen.h"
31
32 /*
33  * Increase or decrease the specified domain's memory reservation. Returns a
34  * -ve errcode on failure, or the # extents successfully allocated or freed.
35  * arg == addr of struct xen_memory_reservation.
36  */
37 #define XENMEM_increase_reservation 0
38 #define XENMEM_decrease_reservation 1
39 #define XENMEM_populate_physmap     6
40
41 #if __XEN_INTERFACE_VERSION__ >= 0x00030209
42 /*
43  * Maximum # bits addressable by the user of the allocated region (e.g., I/O
44  * devices often have a 32-bit limitation even in 64-bit systems). If zero
45  * then the user has no addressing restriction. This field is not used by
46  * XENMEM_decrease_reservation.
47  */
48 #define XENMEMF_address_bits(x)     (x)
49 #define XENMEMF_get_address_bits(x) ((x) & 0xffu)
50 /* NUMA node to allocate from. */
51 #define XENMEMF_node(x)     (((x) + 1) << 8)
52 #define XENMEMF_get_node(x) ((((x) >> 8) - 1) & 0xffu)
53 /* Flag to populate physmap with populate-on-demand entries */
54 #define XENMEMF_populate_on_demand (1<<16)
55 /* Flag to request allocation only from the node specified */
56 #define XENMEMF_exact_node_request  (1<<17)
57 #define XENMEMF_exact_node(n) (XENMEMF_node(n) | XENMEMF_exact_node_request)
58 #endif
59
60 struct xen_memory_reservation {
61
62     /*
63      * XENMEM_increase_reservation:
64      *   OUT: MFN (*not* GMFN) bases of extents that were allocated
65      * XENMEM_decrease_reservation:
66      *   IN:  GMFN bases of extents to free
67      * XENMEM_populate_physmap:
68      *   IN:  GPFN bases of extents to populate with memory
69      *   OUT: GMFN bases of extents that were allocated
70      *   (NB. This command also updates the mach_to_phys translation table)
71      */
72     XEN_GUEST_HANDLE(xen_pfn_t) extent_start;
73
74     /* Number of extents, and size/alignment of each (2^extent_order pages). */
75     xen_ulong_t    nr_extents;
76     unsigned int   extent_order;
77
78 #if __XEN_INTERFACE_VERSION__ >= 0x00030209
79     /* XENMEMF flags. */
80     unsigned int   mem_flags;
81 #else
82     /*
83      * Maximum # bits addressable by the user of the allocated region (e.g.,
84      * I/O devices often have a 32-bit limitation even in 64-bit systems). If
85      * zero then the user has no addressing restriction.
86      * This field is not used by XENMEM_decrease_reservation.
87      */
88     unsigned int   address_bits;
89 #endif
90
91     /*
92      * Domain whose reservation is being changed.
93      * Unprivileged domains can specify only DOMID_SELF.
94      */
95     domid_t        domid;
96 };
97 DEFINE_GUEST_HANDLE_STRUCT(xen_memory_reservation);
98 typedef struct xen_memory_reservation xen_memory_reservation_t;
99 DEFINE_XEN_GUEST_HANDLE(xen_memory_reservation_t);
100
101 /*
102  * An atomic exchange of memory pages. If return code is zero then
103  * @out.extent_list provides GMFNs of the newly-allocated memory.
104  * Returns zero on complete success, otherwise a negative error code.
105  * On complete success then always @nr_exchanged == @in.nr_extents.
106  * On partial success @nr_exchanged indicates how much work was done.
107  */
108 #define XENMEM_exchange             11
109 struct xen_memory_exchange {
110     /*
111      * [IN] Details of memory extents to be exchanged (GMFN bases).
112      * Note that @in.address_bits is ignored and unused.
113      */
114     struct xen_memory_reservation in;
115
116     /*
117      * [IN/OUT] Details of new memory extents.
118      * We require that:
119      *  1. @in.domid == @out.domid
120      *  2. @in.nr_extents  << @in.extent_order ==
121      *     @out.nr_extents << @out.extent_order
122      *  3. @in.extent_start and @out.extent_start lists must not overlap
123      *  4. @out.extent_start lists GPFN bases to be populated
124      *  5. @out.extent_start is overwritten with allocated GMFN bases
125      */
126     struct xen_memory_reservation out;
127
128     /*
129      * [OUT] Number of input extents that were successfully exchanged:
130      *  1. The first @nr_exchanged input extents were successfully
131      *     deallocated.
132      *  2. The corresponding first entries in the output extent list correctly
133      *     indicate the GMFNs that were successfully exchanged.
134      *  3. All other input and output extents are untouched.
135      *  4. If not all input exents are exchanged then the return code of this
136      *     command will be non-zero.
137      *  5. THIS FIELD MUST BE INITIALISED TO ZERO BY THE CALLER!
138      */
139     xen_ulong_t nr_exchanged;
140 };
141 DEFINE_GUEST_HANDLE_STRUCT(xen_memory_exchange);
142 typedef struct xen_memory_exchange xen_memory_exchange_t;
143 DEFINE_XEN_GUEST_HANDLE(xen_memory_exchange_t);
144
145 /*
146  * Returns the maximum machine frame number of mapped RAM in this system.
147  * This command always succeeds (it never returns an error code).
148  * arg == NULL.
149  */
150 #define XENMEM_maximum_ram_page     2
151
152 /*
153  * Returns the current or maximum memory reservation, in pages, of the
154  * specified domain (may be DOMID_SELF). Returns -ve errcode on failure.
155  * arg == addr of domid_t.
156  */
157 #define XENMEM_current_reservation  3
158 #define XENMEM_maximum_reservation  4
159
160 /*
161  * Returns the maximum GPFN in use by the guest, or -ve errcode on failure.
162  */
163 #define XENMEM_maximum_gpfn         14
164
165 /*
166  * Returns a list of MFN bases of 2MB extents comprising the machine_to_phys
167  * mapping table. Architectures which do not have a m2p table do not implement
168  * this command.
169  * arg == addr of xen_machphys_mfn_list_t.
170  */
171 #define XENMEM_machphys_mfn_list    5
172 struct xen_machphys_mfn_list {
173     /*
174      * Size of the 'extent_start' array. Fewer entries will be filled if the
175      * machphys table is smaller than max_extents * 2MB.
176      */
177     unsigned int max_extents;
178
179     /*
180      * Pointer to buffer to fill with list of extent starts. If there are
181      * any large discontiguities in the machine address space, 2MB gaps in
182      * the machphys table will be represented by an MFN base of zero.
183      */
184     XEN_GUEST_HANDLE(xen_pfn_t) extent_start;
185
186     /*
187      * Number of extents written to the above array. This will be smaller
188      * than 'max_extents' if the machphys table is smaller than max_e * 2MB.
189      */
190     unsigned int nr_extents;
191 };
192 DEFINE_GUEST_HANDLE_STRUCT(xen_machphys_mfn_list);
193 typedef struct xen_machphys_mfn_list xen_machphys_mfn_list_t;
194 DEFINE_XEN_GUEST_HANDLE(xen_machphys_mfn_list_t);
195
196 /*
197  * Returns the location in virtual address space of the machine_to_phys
198  * mapping table. Architectures which do not have a m2p table, or which do not
199  * map it by default into guest address space, do not implement this command.
200  * arg == addr of xen_machphys_mapping_t.
201  */
202 #define XENMEM_machphys_mapping     12
203 struct xen_machphys_mapping {
204     xen_ulong_t v_start, v_end; /* Start and end virtual addresses.   */
205     xen_ulong_t max_mfn;        /* Maximum MFN that can be looked up. */
206 };
207 DEFINE_GUEST_HANDLE_STRUCT(xen_machphys_mapping);
208 typedef struct xen_machphys_mapping xen_machphys_mapping_t;
209 DEFINE_XEN_GUEST_HANDLE(xen_machphys_mapping_t);
210
211 /*
212  * Sets the GPFN at which a particular page appears in the specified guest's
213  * pseudophysical address space.
214  * arg == addr of xen_add_to_physmap_t.
215  */
216 #define XENMEM_add_to_physmap      7
217 struct xen_add_to_physmap {
218     /* Which domain to change the mapping for. */
219     domid_t domid;
220
221     /* Number of pages to go through for gmfn_range */
222     uint16_t    size;
223
224     /* Source mapping space. */
225 #define XENMAPSPACE_shared_info 0 /* shared info page */
226 #define XENMAPSPACE_grant_table 1 /* grant table page */
227 #define XENMAPSPACE_gmfn        2 /* GMFN */
228 #define XENMAPSPACE_gmfn_range  3 /* GMFN range */
229     unsigned int space;
230
231 #define XENMAPIDX_grant_table_status 0x80000000
232
233     /* Index into source mapping space. */
234     xen_ulong_t idx;
235
236     /* GPFN where the source mapping page should appear. */
237     xen_pfn_t     gpfn;
238 };
239 DEFINE_GUEST_HANDLE_STRUCT(xen_add_to_physmap);
240 typedef struct xen_add_to_physmap xen_add_to_physmap_t;
241 DEFINE_XEN_GUEST_HANDLE(xen_add_to_physmap_t);
242
243 /*
244  * Unmaps the page appearing at a particular GPFN from the specified guest's
245  * pseudophysical address space.
246  * arg == addr of xen_remove_from_physmap_t.
247  */
248 #define XENMEM_remove_from_physmap      15
249 struct xen_remove_from_physmap {
250     /* Which domain to change the mapping for. */
251     domid_t domid;
252
253     /* GPFN of the current mapping of the page. */
254     xen_pfn_t     gpfn;
255 };
256 typedef struct xen_remove_from_physmap xen_remove_from_physmap_t;
257 DEFINE_XEN_GUEST_HANDLE(xen_remove_from_physmap_t);
258
259 /*** REMOVED ***/
260 /*#define XENMEM_translate_gpfn_list  8*/
261
262 /*
263  * Returns the pseudo-physical memory map as it was when the domain
264  * was started (specified by XENMEM_set_memory_map).
265  * arg == addr of struct xen_memory_map.
266  */
267 #define XENMEM_memory_map           9
268 struct xen_memory_map {
269     /*
270      * On call the number of entries which can be stored in buffer. On
271      * return the number of entries which have been stored in
272      * buffer.
273      */
274     unsigned int nr_entries;
275
276     /*
277      * Entries in the buffer are in the same format as returned by the
278      * BIOS INT 0x15 EAX=0xE820 call.
279      */
280     XEN_GUEST_HANDLE(void) buffer;
281 };
282 DEFINE_GUEST_HANDLE_STRUCT(xen_memory_map);
283 typedef struct xen_memory_map xen_memory_map_t;
284 DEFINE_XEN_GUEST_HANDLE(xen_memory_map_t);
285
286 /*
287  * Returns the real physical memory map. Passes the same structure as
288  * XENMEM_memory_map.
289  * arg == addr of struct xen_memory_map.
290  */
291 #define XENMEM_machine_memory_map   10
292
293 /*
294  * Set the pseudo-physical memory map of a domain, as returned by
295  * XENMEM_memory_map.
296  * arg == addr of xen_foreign_memory_map_t.
297  */
298 #define XENMEM_set_memory_map       13
299 struct xen_foreign_memory_map {
300     domid_t domid;
301     struct xen_memory_map map;
302 };
303 typedef struct xen_foreign_memory_map xen_foreign_memory_map_t;
304 DEFINE_XEN_GUEST_HANDLE(xen_foreign_memory_map_t);
305
306 #define XENMEM_set_pod_target       16
307 #define XENMEM_get_pod_target       17
308 struct xen_pod_target {
309     /* IN */
310     uint64_t target_pages;
311     /* OUT */
312     uint64_t tot_pages;
313     uint64_t pod_cache_pages;
314     uint64_t pod_entries;
315     /* IN */
316     domid_t domid;
317 };
318 typedef struct xen_pod_target xen_pod_target_t;
319
320 #if defined(__XEN__) || defined(__XEN_TOOLS__)
321
322 #ifndef uint64_aligned_t
323 #define uint64_aligned_t uint64_t
324 #endif
325
326 /*
327  * Get the number of MFNs saved through memory sharing.
328  * The call never fails.
329  */
330 #define XENMEM_get_sharing_freed_pages    18
331 #define XENMEM_get_sharing_shared_pages   19
332
333 #define XENMEM_paging_op                    20
334 #define XENMEM_paging_op_nominate           0
335 #define XENMEM_paging_op_evict              1
336 #define XENMEM_paging_op_prep               2
337
338 #define XENMEM_access_op                    21
339 #define XENMEM_access_op_resume             0
340
341 struct xen_mem_event_op {
342     uint8_t     op;         /* XENMEM_*_op_* */
343     domid_t     domain;
344
345
346     /* PAGING_PREP IN: buffer to immediately fill page in */
347     uint64_aligned_t    buffer;
348     /* Other OPs */
349     uint64_aligned_t    gfn;           /* IN:  gfn of page being operated on */
350 };
351 typedef struct xen_mem_event_op xen_mem_event_op_t;
352 DEFINE_XEN_GUEST_HANDLE(xen_mem_event_op_t);
353
354 #define XENMEM_sharing_op                   22
355 #define XENMEM_sharing_op_nominate_gfn      0
356 #define XENMEM_sharing_op_nominate_gref     1
357 #define XENMEM_sharing_op_share             2
358 #define XENMEM_sharing_op_resume            3
359 #define XENMEM_sharing_op_debug_gfn         4
360 #define XENMEM_sharing_op_debug_mfn         5
361 #define XENMEM_sharing_op_debug_gref        6
362 #define XENMEM_sharing_op_add_physmap       7
363 #define XENMEM_sharing_op_audit             8
364
365 #define XENMEM_SHARING_OP_S_HANDLE_INVALID  (-10)
366 #define XENMEM_SHARING_OP_C_HANDLE_INVALID  (-9)
367
368 /* The following allows sharing of grant refs. This is useful
369  * for sharing utilities sitting as "filters" in IO backends
370  * (e.g. memshr + blktap(2)). The IO backend is only exposed
371  * to grant references, and this allows sharing of the grefs */
372 #define XENMEM_SHARING_OP_FIELD_IS_GREF_FLAG   (1ULL << 62)
373
374 #define XENMEM_SHARING_OP_FIELD_MAKE_GREF(field, val)  \
375     (field) = (XENMEM_SHARING_OP_FIELD_IS_GREF_FLAG | val)
376 #define XENMEM_SHARING_OP_FIELD_IS_GREF(field)         \
377     ((field) & XENMEM_SHARING_OP_FIELD_IS_GREF_FLAG)
378 #define XENMEM_SHARING_OP_FIELD_GET_GREF(field)        \
379     ((field) & (~XENMEM_SHARING_OP_FIELD_IS_GREF_FLAG))
380
381 struct xen_mem_sharing_op {
382     uint8_t     op;     /* XENMEM_sharing_op_* */
383     domid_t     domain;
384
385     union {
386         struct mem_sharing_op_nominate {  /* OP_NOMINATE_xxx           */
387             union {
388                 uint64_aligned_t gfn;     /* IN: gfn to nominate       */
389                 uint32_t      grant_ref;  /* IN: grant ref to nominate */
390             } u;
391             uint64_aligned_t  handle;     /* OUT: the handle           */
392         } nominate;
393         struct mem_sharing_op_share {     /* OP_SHARE/ADD_PHYSMAP */
394             uint64_aligned_t source_gfn;    /* IN: the gfn of the source page */
395             uint64_aligned_t source_handle; /* IN: handle to the source page */
396             uint64_aligned_t client_gfn;    /* IN: the client gfn */
397             uint64_aligned_t client_handle; /* IN: handle to the client page */
398             domid_t  client_domain; /* IN: the client domain id */
399         } share;
400         struct mem_sharing_op_debug {     /* OP_DEBUG_xxx */
401             union {
402                 uint64_aligned_t gfn;      /* IN: gfn to debug          */
403                 uint64_aligned_t mfn;      /* IN: mfn to debug          */
404                 uint32_t gref;     /* IN: gref to debug         */
405             } u;
406         } debug;
407     } u;
408 };
409 typedef struct xen_mem_sharing_op xen_mem_sharing_op_t;
410 DEFINE_XEN_GUEST_HANDLE(xen_mem_sharing_op_t);
411
412 #endif /* defined(__XEN__) || defined(__XEN_TOOLS__) */
413
414 #ifndef CONFIG_XEN
415 #include <linux/spinlock.h>
416
417 /*
418  * Prevent the balloon driver from changing the memory reservation
419  * during a driver critical region.
420  */
421 extern spinlock_t xen_reservation_lock;
422 #endif
423
424 #endif /* __XEN_PUBLIC_MEMORY_H__ */