target/arm: v8M: Check state of exception being returned from
[qemu/ar7.git] / chardev / char-ringbuf.c
blob67397a8ce944b47379c0139984f96d8e649ddcd0
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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 deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "chardev/char.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-commands-char.h"
29 #include "qemu/base64.h"
30 #include "qemu/module.h"
31 #include "qemu/option.h"
33 /* Ring buffer chardev */
35 typedef struct {
36 Chardev parent;
37 size_t size;
38 size_t prod;
39 size_t cons;
40 uint8_t *cbuf;
41 } RingBufChardev;
43 #define RINGBUF_CHARDEV(obj) \
44 OBJECT_CHECK(RingBufChardev, (obj), TYPE_CHARDEV_RINGBUF)
46 static size_t ringbuf_count(const Chardev *chr)
48 const RingBufChardev *d = RINGBUF_CHARDEV(chr);
50 return d->prod - d->cons;
53 static int ringbuf_chr_write(Chardev *chr, const uint8_t *buf, int len)
55 RingBufChardev *d = RINGBUF_CHARDEV(chr);
56 int i;
58 if (!buf || (len < 0)) {
59 return -1;
62 for (i = 0; i < len; i++) {
63 d->cbuf[d->prod++ & (d->size - 1)] = buf[i];
64 if (d->prod - d->cons > d->size) {
65 d->cons = d->prod - d->size;
69 return len;
72 static int ringbuf_chr_read(Chardev *chr, uint8_t *buf, int len)
74 RingBufChardev *d = RINGBUF_CHARDEV(chr);
75 int i;
77 qemu_mutex_lock(&chr->chr_write_lock);
78 for (i = 0; i < len && d->cons != d->prod; i++) {
79 buf[i] = d->cbuf[d->cons++ & (d->size - 1)];
81 qemu_mutex_unlock(&chr->chr_write_lock);
83 return i;
86 static void char_ringbuf_finalize(Object *obj)
88 RingBufChardev *d = RINGBUF_CHARDEV(obj);
90 g_free(d->cbuf);
93 static void qemu_chr_open_ringbuf(Chardev *chr,
94 ChardevBackend *backend,
95 bool *be_opened,
96 Error **errp)
98 ChardevRingbuf *opts = backend->u.ringbuf.data;
99 RingBufChardev *d = RINGBUF_CHARDEV(chr);
101 d->size = opts->has_size ? opts->size : 65536;
103 /* The size must be power of 2 */
104 if (d->size & (d->size - 1)) {
105 error_setg(errp, "size of ringbuf chardev must be power of two");
106 return;
109 d->prod = 0;
110 d->cons = 0;
111 d->cbuf = g_malloc0(d->size);
114 void qmp_ringbuf_write(const char *device, const char *data,
115 bool has_format, enum DataFormat format,
116 Error **errp)
118 Chardev *chr;
119 const uint8_t *write_data;
120 int ret;
121 gsize write_count;
123 chr = qemu_chr_find(device);
124 if (!chr) {
125 error_setg(errp, "Device '%s' not found", device);
126 return;
129 if (!CHARDEV_IS_RINGBUF(chr)) {
130 error_setg(errp, "%s is not a ringbuf device", device);
131 return;
134 if (has_format && (format == DATA_FORMAT_BASE64)) {
135 write_data = qbase64_decode(data, -1,
136 &write_count,
137 errp);
138 if (!write_data) {
139 return;
141 } else {
142 write_data = (uint8_t *)data;
143 write_count = strlen(data);
146 ret = ringbuf_chr_write(chr, write_data, write_count);
148 if (write_data != (uint8_t *)data) {
149 g_free((void *)write_data);
152 if (ret < 0) {
153 error_setg(errp, "Failed to write to device %s", device);
154 return;
158 char *qmp_ringbuf_read(const char *device, int64_t size,
159 bool has_format, enum DataFormat format,
160 Error **errp)
162 Chardev *chr;
163 uint8_t *read_data;
164 size_t count;
165 char *data;
167 chr = qemu_chr_find(device);
168 if (!chr) {
169 error_setg(errp, "Device '%s' not found", device);
170 return NULL;
173 if (!CHARDEV_IS_RINGBUF(chr)) {
174 error_setg(errp, "%s is not a ringbuf device", device);
175 return NULL;
178 if (size <= 0) {
179 error_setg(errp, "size must be greater than zero");
180 return NULL;
183 count = ringbuf_count(chr);
184 size = size > count ? count : size;
185 read_data = g_malloc(size + 1);
187 ringbuf_chr_read(chr, read_data, size);
189 if (has_format && (format == DATA_FORMAT_BASE64)) {
190 data = g_base64_encode(read_data, size);
191 g_free(read_data);
192 } else {
194 * FIXME should read only complete, valid UTF-8 characters up
195 * to @size bytes. Invalid sequences should be replaced by a
196 * suitable replacement character. Except when (and only
197 * when) ring buffer lost characters since last read, initial
198 * continuation characters should be dropped.
200 read_data[size] = 0;
201 data = (char *)read_data;
204 return data;
207 static void qemu_chr_parse_ringbuf(QemuOpts *opts, ChardevBackend *backend,
208 Error **errp)
210 int val;
211 ChardevRingbuf *ringbuf;
213 backend->type = CHARDEV_BACKEND_KIND_RINGBUF;
214 ringbuf = backend->u.ringbuf.data = g_new0(ChardevRingbuf, 1);
215 qemu_chr_parse_common(opts, qapi_ChardevRingbuf_base(ringbuf));
217 val = qemu_opt_get_size(opts, "size", 0);
218 if (val != 0) {
219 ringbuf->has_size = true;
220 ringbuf->size = val;
224 static void char_ringbuf_class_init(ObjectClass *oc, void *data)
226 ChardevClass *cc = CHARDEV_CLASS(oc);
228 cc->parse = qemu_chr_parse_ringbuf;
229 cc->open = qemu_chr_open_ringbuf;
230 cc->chr_write = ringbuf_chr_write;
233 static const TypeInfo char_ringbuf_type_info = {
234 .name = TYPE_CHARDEV_RINGBUF,
235 .parent = TYPE_CHARDEV,
236 .class_init = char_ringbuf_class_init,
237 .instance_size = sizeof(RingBufChardev),
238 .instance_finalize = char_ringbuf_finalize,
241 /* Bug-compatibility: */
242 static const TypeInfo char_memory_type_info = {
243 .name = TYPE_CHARDEV_MEMORY,
244 .parent = TYPE_CHARDEV_RINGBUF,
247 static void register_types(void)
249 type_register_static(&char_ringbuf_type_info);
250 type_register_static(&char_memory_type_info);
253 type_init(register_types);