/root/src/xen/xen/include/asm/guest_access.h
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | * guest_access.h |
3 | | * |
4 | | * Copyright (c) 2006, K A Fraser |
5 | | */ |
6 | | |
7 | | #ifndef __ASM_X86_GUEST_ACCESS_H__ |
8 | | #define __ASM_X86_GUEST_ACCESS_H__ |
9 | | |
10 | | #include <asm/uaccess.h> |
11 | | #include <asm/paging.h> |
12 | | #include <asm/hvm/support.h> |
13 | | #include <asm/hvm/guest_access.h> |
14 | | |
15 | | /* Raw access functions: no type checking. */ |
16 | | #define raw_copy_to_guest(dst, src, len) \ |
17 | 9 | (is_hvm_vcpu(current) ? \ |
18 | 8 | copy_to_user_hvm((dst), (src), (len)) : \ |
19 | 1 | copy_to_user((dst), (src), (len))) |
20 | | #define raw_copy_from_guest(dst, src, len) \ |
21 | 304k | (is_hvm_vcpu(current) ? \ |
22 | 304k | copy_from_user_hvm((dst), (src), (len)) : \ |
23 | 18.4E | copy_from_user((dst), (src), (len))) |
24 | | #define raw_clear_guest(dst, len) \ |
25 | 1 | (is_hvm_vcpu(current) ? \ |
26 | 0 | clear_user_hvm((dst), (len)) : \ |
27 | 1 | clear_user((dst), (len))) |
28 | | #define __raw_copy_to_guest(dst, src, len) \ |
29 | 264 | (is_hvm_vcpu(current) ? \ |
30 | 146 | copy_to_user_hvm((dst), (src), (len)) : \ |
31 | 118 | __copy_to_user((dst), (src), (len))) |
32 | | #define __raw_copy_from_guest(dst, src, len) \ |
33 | 0 | (is_hvm_vcpu(current) ? \ |
34 | 0 | copy_from_user_hvm((dst), (src), (len)) : \ |
35 | 0 | __copy_from_user((dst), (src), (len))) |
36 | | #define __raw_clear_guest(dst, len) \ |
37 | | (is_hvm_vcpu(current) ? \ |
38 | | clear_user_hvm((dst), (len)) : \ |
39 | | clear_user((dst), (len))) |
40 | | |
41 | | /* Is the guest handle a NULL reference? */ |
42 | 329k | #define guest_handle_is_null(hnd) ((hnd).p == NULL) |
43 | | |
44 | | /* Offset the given guest handle into the array it refers to. */ |
45 | 118k | #define guest_handle_add_offset(hnd, nr) ((hnd).p += (nr)) |
46 | 0 | #define guest_handle_subtract_offset(hnd, nr) ((hnd).p -= (nr)) |
47 | | |
48 | | /* Cast a guest handle (either XEN_GUEST_HANDLE or XEN_GUEST_HANDLE_PARAM) |
49 | | * to the specified type of XEN_GUEST_HANDLE_PARAM. */ |
50 | 8 | #define guest_handle_cast(hnd, type) ({ \ |
51 | 8 | type *_x = (hnd).p; \ |
52 | 8 | (XEN_GUEST_HANDLE_PARAM(type)) { _x }; \ |
53 | 8 | }) |
54 | | |
55 | | /* Cast a XEN_GUEST_HANDLE to XEN_GUEST_HANDLE_PARAM */ |
56 | 0 | #define guest_handle_to_param(hnd, type) ({ \ |
57 | 0 | /* type checking: make sure that the pointers inside \ |
58 | 0 | * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \ |
59 | 0 | * the same type, then return hnd */ \ |
60 | 0 | (void)((typeof(&(hnd).p)) 0 == \ |
61 | 0 | (typeof(&((XEN_GUEST_HANDLE_PARAM(type)) {}).p)) 0); \ |
62 | 0 | (hnd); \ |
63 | 0 | }) |
64 | | |
65 | | /* Cast a XEN_GUEST_HANDLE_PARAM to XEN_GUEST_HANDLE */ |
66 | 0 | #define guest_handle_from_param(hnd, type) ({ \ |
67 | 0 | /* type checking: make sure that the pointers inside \ |
68 | 0 | * XEN_GUEST_HANDLE and XEN_GUEST_HANDLE_PARAM are of \ |
69 | 0 | * the same type, then return hnd */ \ |
70 | 0 | (void)((typeof(&(hnd).p)) 0 == \ |
71 | 0 | (typeof(&((XEN_GUEST_HANDLE_PARAM(type)) {}).p)) 0); \ |
72 | 0 | (hnd); \ |
73 | 0 | }) |
74 | | |
75 | | #define guest_handle_for_field(hnd, type, fld) \ |
76 | | ((XEN_GUEST_HANDLE(type)) { &(hnd).p->fld }) |
77 | | |
78 | | #define guest_handle_from_ptr(ptr, type) \ |
79 | 4.58M | ((XEN_GUEST_HANDLE_PARAM(type)) { (type *)ptr }) |
80 | | #define const_guest_handle_from_ptr(ptr, type) \ |
81 | 0 | ((XEN_GUEST_HANDLE_PARAM(const_##type)) { (const type *)ptr }) |
82 | | |
83 | | /* |
84 | | * Copy an array of objects to guest context via a guest handle, |
85 | | * specifying an offset into the guest array. |
86 | | */ |
87 | 9 | #define copy_to_guest_offset(hnd, off, ptr, nr) ({ \ |
88 | 0 | const typeof(*(ptr)) *_s = (ptr); \ |
89 | 9 | char (*_d)[sizeof(*_s)] = (void *)(hnd).p; \ |
90 | 0 | ((void)((hnd).p == (ptr))); \ |
91 | 9 | raw_copy_to_guest(_d+(off), _s, sizeof(*_s)*(nr)); \ |
92 | 9 | }) |
93 | | |
94 | | /* |
95 | | * Copy an array of objects from guest context via a guest handle, |
96 | | * specifying an offset into the guest array. |
97 | | */ |
98 | 304k | #define copy_from_guest_offset(ptr, hnd, off, nr) ({ \ |
99 | 304k | const typeof(*(ptr)) *_s = (hnd).p; \ |
100 | 304k | typeof(*(ptr)) *_d = (ptr); \ |
101 | 304k | raw_copy_from_guest(_d, _s+(off), sizeof(*_d)*(nr));\ |
102 | 304k | }) |
103 | | |
104 | 1 | #define clear_guest_offset(hnd, off, nr) ({ \ |
105 | 1 | void *_d = (hnd).p; \ |
106 | 1 | raw_clear_guest(_d+(off), nr); \ |
107 | 1 | }) |
108 | | |
109 | | /* Copy sub-field of a structure to guest context via a guest handle. */ |
110 | 0 | #define copy_field_to_guest(hnd, ptr, field) ({ \ |
111 | 0 | const typeof(&(ptr)->field) _s = &(ptr)->field; \ |
112 | 0 | void *_d = &(hnd).p->field; \ |
113 | 0 | ((void)(&(hnd).p->field == &(ptr)->field)); \ |
114 | 0 | raw_copy_to_guest(_d, _s, sizeof(*_s)); \ |
115 | 0 | }) |
116 | | |
117 | | /* Copy sub-field of a structure from guest context via a guest handle. */ |
118 | | #define copy_field_from_guest(ptr, hnd, field) ({ \ |
119 | | const typeof(&(ptr)->field) _s = &(hnd).p->field; \ |
120 | | typeof(&(ptr)->field) _d = &(ptr)->field; \ |
121 | | raw_copy_from_guest(_d, _s, sizeof(*_d)); \ |
122 | | }) |
123 | | |
124 | | /* |
125 | | * Pre-validate a guest handle. |
126 | | * Allows use of faster __copy_* functions. |
127 | | */ |
128 | | #define guest_handle_okay(hnd, nr) \ |
129 | 0 | (paging_mode_external(current->domain) || \ |
130 | 0 | array_access_ok((hnd).p, (nr), sizeof(*(hnd).p))) |
131 | | #define guest_handle_subrange_okay(hnd, first, last) \ |
132 | 0 | (paging_mode_external(current->domain) || \ |
133 | 0 | array_access_ok((hnd).p + (first), \ |
134 | 0 | (last)-(first)+1, \ |
135 | 0 | sizeof(*(hnd).p))) |
136 | | |
137 | 270 | #define __copy_to_guest_offset(hnd, off, ptr, nr) ({ \ |
138 | 270 | const typeof(*(ptr)) *_s = (ptr); \ |
139 | 270 | char (*_d)[sizeof(*_s)] = (void *)(hnd).p; \ |
140 | 270 | ((void)((hnd).p == (ptr))); \ |
141 | 270 | __raw_copy_to_guest(_d+(off), _s, sizeof(*_s)*(nr));\ |
142 | 270 | }) |
143 | | |
144 | 0 | #define __copy_from_guest_offset(ptr, hnd, off, nr) ({ \ |
145 | 0 | const typeof(*(ptr)) *_s = (hnd).p; \ |
146 | 0 | typeof(*(ptr)) *_d = (ptr); \ |
147 | 0 | __raw_copy_from_guest(_d, _s+(off), sizeof(*_d)*(nr));\ |
148 | 0 | }) |
149 | | |
150 | | #define __clear_guest_offset(hnd, off, nr) ({ \ |
151 | | void *_d = (hnd).p; \ |
152 | | __raw_clear_guest(_d+(off), nr); \ |
153 | | }) |
154 | | |
155 | 18.4E | #define __copy_field_to_guest(hnd, ptr, field) ({ \ |
156 | 18.4E | const typeof(&(ptr)->field) _s = &(ptr)->field; \ |
157 | 18.4E | void *_d = &(hnd).p->field; \ |
158 | 18.4E | ((void)(&(hnd).p->field == &(ptr)->field)); \ |
159 | 18.4E | __raw_copy_to_guest(_d, _s, sizeof(*_s)); \ |
160 | 18.4E | }) |
161 | | |
162 | | #define __copy_field_from_guest(ptr, hnd, field) ({ \ |
163 | | const typeof(&(ptr)->field) _s = &(hnd).p->field; \ |
164 | | typeof(&(ptr)->field) _d = &(ptr)->field; \ |
165 | | __raw_copy_from_guest(_d, _s, sizeof(*_d)); \ |
166 | | }) |
167 | | |
168 | | #endif /* __ASM_X86_GUEST_ACCESS_H__ */ |