Debian Codegraph @

tools/ocaml/libs/xb/xs_ring_stubs.c

raw
1 /*
2 * Copyright (C) 2006-2007 XenSource Ltd.
3 * Copyright (C) 2008 Citrix Ltd.
4 * Author Vincent Hanquez <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 */
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdint.h>
24
25 #include <xenctrl.h>
26 #include <xen/io/xs_wire.h>
27
28 #include <caml/mlvalues.h>
29 #include <caml/memory.h>
30 #include <caml/alloc.h>
31 #include <caml/custom.h>
32 #include <caml/fail.h>
33 #include <caml/callback.h>
34
35 #include <sys/mman.h>
36 #include "mmap_stubs.h"
37
38 #define GET_C_STRUCT(a) ((struct mmap_interface *)Data_abstract_val(a))
39
40 /*
41 * Bytes_val has been introduced by Ocaml 4.06.1. So define our own version
42 * if needed.
43 */
44 #ifndef Bytes_val
45 #define Bytes_val(x) ((unsigned char *) Bp_val(x))
46 #endif
47
48 CAMLprim value ml_interface_read(value ml_interface,
49 value ml_buffer,
50 value ml_len)
51 {
52 CAMLparam3(ml_interface, ml_buffer, ml_len);
53 CAMLlocal1(ml_result);
54
55 struct mmap_interface *interface = GET_C_STRUCT(ml_interface);
56 unsigned char *buffer = Bytes_val(ml_buffer);
57 int len = Int_val(ml_len);
58 int result;
59
60 struct xenstore_domain_interface *intf = interface->addr;
61 XENSTORE_RING_IDX cons, prod; /* offsets only */
62 int total_data, data;
63 uint32_t connection;
64
65 cons = *(volatile uint32_t*)&intf->req_cons;
66 prod = *(volatile uint32_t*)&intf->req_prod;
67 connection = *(volatile uint32_t*)&intf->connection;
68
69 if (connection != XENSTORE_CONNECTED)
70 caml_raise_constant(*caml_named_value("Xb.Reconnect"));
71
72 xen_mb();
73
74 if ((prod - cons) > XENSTORE_RING_SIZE)
75 caml_failwith("bad connection");
76
77 /* Check for any pending data at all. */
78 total_data = prod - cons;
79 if (total_data == 0) {
80 /* No pending data at all. */
81 result = 0;
82 goto exit;
83 }
84 else if (total_data < len)
85 /* Some data - make a partial read. */
86 len = total_data;
87
88 /* Check whether data crosses the end of the ring. */
89 data = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(cons);
90 if (len < data)
91 /* Data within the remaining part of the ring. */
92 memcpy(buffer, intf->req + MASK_XENSTORE_IDX(cons), len);
93 else {
94 /* Data crosses the ring boundary. Read both halves. */
95 memcpy(buffer, intf->req + MASK_XENSTORE_IDX(cons), data);
96 memcpy(buffer + data, intf->req, len - data);
97 }
98
99 xen_mb();
100 intf->req_cons += len;
101 result = len;
102 exit:
103 ml_result = Val_int(result);
104 CAMLreturn(ml_result);
105 }
106
107 CAMLprim value ml_interface_write(value ml_interface,
108 value ml_buffer,
109 value ml_len)
110 {
111 CAMLparam3(ml_interface, ml_buffer, ml_len);
112 CAMLlocal1(ml_result);
113
114 struct mmap_interface *interface = GET_C_STRUCT(ml_interface);
115 const char *buffer = String_val(ml_buffer);
116 int len = Int_val(ml_len);
117 int result;
118
119 struct xenstore_domain_interface *intf = interface->addr;
120 XENSTORE_RING_IDX cons, prod;
121 int total_space, space;
122 uint32_t connection;
123
124 cons = *(volatile uint32_t*)&intf->rsp_cons;
125 prod = *(volatile uint32_t*)&intf->rsp_prod;
126 connection = *(volatile uint32_t*)&intf->connection;
127
128 if (connection != XENSTORE_CONNECTED)
129 caml_raise_constant(*caml_named_value("Xb.Reconnect"));
130
131 xen_mb();
132
133 if ((prod - cons) > XENSTORE_RING_SIZE)
134 caml_failwith("bad connection");
135
136 /* Check for space to write the full message. */
137 total_space = XENSTORE_RING_SIZE - (prod - cons);
138 if (total_space == 0) {
139 /* No space at all - exit having done nothing. */
140 result = 0;
141 goto exit;
142 }
143 else if (total_space < len)
144 /* Some space - make a partial write. */
145 len = total_space;
146
147 /* Check for space until the ring wraps. */
148 space = XENSTORE_RING_SIZE - MASK_XENSTORE_IDX(prod);
149 if (len < space)
150 /* Message fits inside the remaining part of the ring. */
151 memcpy(intf->rsp + MASK_XENSTORE_IDX(prod), buffer, len);
152 else {
153 /* Message wraps around the end of the ring. Write both halves. */
154 memcpy(intf->rsp + MASK_XENSTORE_IDX(prod), buffer, space);
155 memcpy(intf->rsp, buffer + space, len - space);
156 }
157
158 xen_mb();
159 intf->rsp_prod += len;
160 result = len;
161 exit:
162 ml_result = Val_int(result);
163 CAMLreturn(ml_result);
164 }
165
166 CAMLprim value ml_interface_set_server_features(value interface, value v)
167 {
168 CAMLparam2(interface, v);
169 struct xenstore_domain_interface *intf = GET_C_STRUCT(interface)->addr;
170 if (intf == (void*)MAP_FAILED)
171 caml_failwith("Interface closed");
172
173 intf->server_features = Int_val(v);
174
175 CAMLreturn(Val_unit);
176 }
177
178 CAMLprim value ml_interface_get_server_features(value interface)
179 {
180 CAMLparam1(interface);
181 struct xenstore_domain_interface *intf = GET_C_STRUCT(interface)->addr;
182
183 CAMLreturn(Val_int (intf->server_features));
184 }
185
186 CAMLprim value ml_interface_close(value interface)
187 {
188 CAMLparam1(interface);
189 struct xenstore_domain_interface *intf = GET_C_STRUCT(interface)->addr;
190 int i;
191
192 intf->req_cons = intf->req_prod = intf->rsp_cons = intf->rsp_prod = 0;
193 /* Ensure the unused space is full of invalid xenstore packets. */
194 for (i = 0; i < XENSTORE_RING_SIZE; i++) {
195 intf->req[i] = 0xff; /* XS_INVALID = 0xffff */
196 intf->rsp[i] = 0xff;
197 }
198 xen_mb ();
199 intf->connection = XENSTORE_CONNECTED;
200 CAMLreturn(Val_unit);
201 }